2 * SPDX-License-Identifier: MIT
4 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
6 * Babeltrace trace converter - parameter parsing
9 #define BT_LOG_TAG "CLI/CFG-CLI-ARGS"
15 #include "common/assert.h"
19 #include <babeltrace2/babeltrace.h>
20 #include "common/common.h"
22 #include <sys/types.h>
23 #include "argpar/argpar.h"
24 #include "babeltrace2-cfg.h"
25 #include "babeltrace2-cfg-cli-args.h"
26 #include "babeltrace2-cfg-cli-args-connect.h"
27 #include "param-parse/param-parse.h"
28 #include "babeltrace2-log-level.h"
29 #include "babeltrace2-plugins.h"
30 #include "babeltrace2-query.h"
31 #include "autodisc/autodisc.h"
32 #include "common/version.h"
34 #define BT_CLI_LOGE_APPEND_CAUSE_OOM() BT_CLI_LOGE_APPEND_CAUSE("Out of memory.")
37 * Returns the plugin name, component class name, component class type,
38 * and component name from a command-line --component option's argument.
39 * arg must have the following format:
41 * [NAME:]TYPE.PLUGIN.CLS
43 * where NAME is the optional component name, TYPE is either `source`,
44 * `filter`, or `sink`, PLUGIN is the plugin name, and CLS is the
45 * component class name.
47 * On success, both *plugin and *component are not NULL. *plugin
48 * and *comp_cls are owned by the caller. On success, *name can be NULL
49 * if no component class name was found, and *comp_cls_type is set.
52 void plugin_comp_cls_names(const char *arg
, char **name
, char **plugin
,
53 char **comp_cls
, bt_component_class_type
*comp_cls_type
)
56 GString
*gs_name
= NULL
;
57 GString
*gs_comp_cls_type
= NULL
;
58 GString
*gs_plugin
= NULL
;
59 GString
*gs_comp_cls
= NULL
;
65 BT_ASSERT(comp_cls_type
);
67 if (!bt_common_string_is_printable(arg
)) {
68 BT_CLI_LOGE_APPEND_CAUSE("Argument contains a non-printable character.");
72 /* Parse the component name */
73 gs_name
= bt_common_string_until(at
, ".:\\", ":", &end_pos
);
78 if (arg
[end_pos
] == ':') {
82 g_string_assign(gs_name
, "");
85 /* Parse the component class type */
86 gs_comp_cls_type
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
87 if (!gs_comp_cls_type
|| at
[end_pos
] == '\0') {
88 BT_CLI_LOGE_APPEND_CAUSE("Missing component class type (`source`, `filter`, or `sink`).");
92 if (strcmp(gs_comp_cls_type
->str
, "source") == 0 ||
93 strcmp(gs_comp_cls_type
->str
, "src") == 0) {
94 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_SOURCE
;
95 } else if (strcmp(gs_comp_cls_type
->str
, "filter") == 0 ||
96 strcmp(gs_comp_cls_type
->str
, "flt") == 0) {
97 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_FILTER
;
98 } else if (strcmp(gs_comp_cls_type
->str
, "sink") == 0) {
99 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_SINK
;
101 BT_CLI_LOGE_APPEND_CAUSE("Unknown component class type: `%s`.",
102 gs_comp_cls_type
->str
);
108 /* Parse the plugin name */
109 gs_plugin
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
110 if (!gs_plugin
|| gs_plugin
->len
== 0 || at
[end_pos
] == '\0') {
111 BT_CLI_LOGE_APPEND_CAUSE("Missing plugin or component class name.");
117 /* Parse the component class name */
118 gs_comp_cls
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
119 if (!gs_comp_cls
|| gs_comp_cls
->len
== 0) {
120 BT_CLI_LOGE_APPEND_CAUSE("Missing component class name.");
124 if (at
[end_pos
] != '\0') {
125 /* Found a non-escaped `.` */
130 if (gs_name
->len
== 0) {
132 g_string_free(gs_name
, TRUE
);
134 *name
= gs_name
->str
;
135 g_string_free(gs_name
, FALSE
);
138 g_string_free(gs_name
, TRUE
);
141 *plugin
= gs_plugin
->str
;
142 *comp_cls
= gs_comp_cls
->str
;
143 g_string_free(gs_plugin
, FALSE
);
144 g_string_free(gs_comp_cls
, FALSE
);
160 g_string_free(gs_name
, TRUE
);
164 g_string_free(gs_plugin
, TRUE
);
168 g_string_free(gs_comp_cls
, TRUE
);
171 if (gs_comp_cls_type
) {
172 g_string_free(gs_comp_cls_type
, TRUE
);
179 void print_and_indent(const char *str
)
181 const char *ch
= &str
[0];
183 for (; *ch
!= '\0'; ch
++) {
197 * Prints the Babeltrace version.
200 void print_version(void)
202 bool has_extra_name
= strlen(BT_VERSION_EXTRA_NAME
) > 0;
203 bool has_extra_description
= strlen(BT_VERSION_EXTRA_DESCRIPTION
) > 0;
204 bool has_extra_patch_names
= strlen(BT_VERSION_EXTRA_PATCHES
) > 0;
205 bool has_extra
= has_extra_name
|| has_extra_description
||
206 has_extra_patch_names
;
208 printf("%sBabeltrace %s%s",
209 bt_common_color_bold(),
211 bt_common_color_reset());
213 if (strlen(BT_VERSION_NAME
) > 0) {
214 printf(" \"%s%s%s%s\"",
215 bt_common_color_fg_bright_blue(),
216 bt_common_color_bold(),
218 bt_common_color_reset());
221 if (strlen(BT_VERSION_GIT
) > 0) {
223 bt_common_color_fg_yellow(),
225 bt_common_color_reset());
230 if (strlen(BT_VERSION_DESCRIPTION
) > 0) {
231 unsigned int columns
;
234 if (bt_common_get_term_size(&columns
, NULL
) < 0) {
235 /* Width not found: default to 80 */
239 descr
= bt_common_fold(BT_VERSION_DESCRIPTION
, columns
, 0);
241 printf("\n%s\n", descr
->str
);
242 g_string_free(descr
, TRUE
);
248 if (has_extra_name
) {
249 printf("%sExtra name%s: %s\n",
250 bt_common_color_fg_cyan(),
251 bt_common_color_reset(),
252 BT_VERSION_EXTRA_NAME
);
255 if (has_extra_description
) {
256 printf("%sExtra description%s:\n ",
257 bt_common_color_fg_cyan(),
258 bt_common_color_reset());
259 print_and_indent(BT_VERSION_EXTRA_DESCRIPTION
);
262 if (has_extra_patch_names
) {
263 printf("%sExtra patch names%s:\n ",
264 bt_common_color_fg_cyan(),
265 bt_common_color_reset());
266 print_and_indent(BT_VERSION_EXTRA_PATCHES
);
272 * Destroys a component configuration.
275 void bt_config_component_destroy(bt_object
*obj
)
277 struct bt_config_component
*bt_config_component
=
278 container_of(obj
, struct bt_config_component
, base
);
284 if (bt_config_component
->plugin_name
) {
285 g_string_free(bt_config_component
->plugin_name
, TRUE
);
288 if (bt_config_component
->comp_cls_name
) {
289 g_string_free(bt_config_component
->comp_cls_name
, TRUE
);
292 if (bt_config_component
->instance_name
) {
293 g_string_free(bt_config_component
->instance_name
, TRUE
);
296 BT_VALUE_PUT_REF_AND_RESET(bt_config_component
->params
);
297 g_free(bt_config_component
);
304 * Creates a component configuration using the given plugin name and
305 * component name. `plugin_name` and `comp_cls_name` are copied (belong
306 * to the return value).
308 * Return value is owned by the caller.
311 struct bt_config_component
*bt_config_component_create(
312 bt_component_class_type type
,
313 const char *plugin_name
, const char *comp_cls_name
,
316 struct bt_config_component
*cfg_component
= NULL
;
318 cfg_component
= g_new0(struct bt_config_component
, 1);
319 if (!cfg_component
) {
320 BT_CLI_LOGE_APPEND_CAUSE_OOM();
324 bt_object_init_shared(&cfg_component
->base
,
325 bt_config_component_destroy
);
326 cfg_component
->type
= type
;
327 cfg_component
->plugin_name
= g_string_new(plugin_name
);
328 if (!cfg_component
->plugin_name
) {
329 BT_CLI_LOGE_APPEND_CAUSE_OOM();
333 cfg_component
->comp_cls_name
= g_string_new(comp_cls_name
);
334 if (!cfg_component
->comp_cls_name
) {
335 BT_CLI_LOGE_APPEND_CAUSE_OOM();
339 cfg_component
->instance_name
= g_string_new(NULL
);
340 if (!cfg_component
->instance_name
) {
341 BT_CLI_LOGE_APPEND_CAUSE_OOM();
345 cfg_component
->log_level
= init_log_level
;
347 /* Start with empty parameters */
348 cfg_component
->params
= bt_value_map_create();
349 if (!cfg_component
->params
) {
350 BT_CLI_LOGE_APPEND_CAUSE_OOM();
357 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
360 return cfg_component
;
364 * Creates a component configuration from a command-line --component
368 struct bt_config_component
*bt_config_component_from_arg(const char *arg
,
371 struct bt_config_component
*cfg_comp
= NULL
;
373 char *plugin_name
= NULL
;
374 char *comp_cls_name
= NULL
;
375 bt_component_class_type type
;
377 plugin_comp_cls_names(arg
, &name
, &plugin_name
, &comp_cls_name
, &type
);
378 if (!plugin_name
|| !comp_cls_name
) {
382 cfg_comp
= bt_config_component_create(type
, plugin_name
, comp_cls_name
,
389 g_string_assign(cfg_comp
->instance_name
, name
);
395 BT_OBJECT_PUT_REF_AND_RESET(cfg_comp
);
400 g_free(comp_cls_name
);
405 * Destroys a configuration.
408 void bt_config_destroy(bt_object
*obj
)
410 struct bt_config
*cfg
=
411 container_of(obj
, struct bt_config
, base
);
417 BT_VALUE_PUT_REF_AND_RESET(cfg
->plugin_paths
);
419 switch (cfg
->command
) {
420 case BT_CONFIG_COMMAND_RUN
:
421 if (cfg
->cmd_data
.run
.sources
) {
422 g_ptr_array_free(cfg
->cmd_data
.run
.sources
, TRUE
);
425 if (cfg
->cmd_data
.run
.filters
) {
426 g_ptr_array_free(cfg
->cmd_data
.run
.filters
, TRUE
);
429 if (cfg
->cmd_data
.run
.sinks
) {
430 g_ptr_array_free(cfg
->cmd_data
.run
.sinks
, TRUE
);
433 if (cfg
->cmd_data
.run
.connections
) {
434 g_ptr_array_free(cfg
->cmd_data
.run
.connections
,
438 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
440 case BT_CONFIG_COMMAND_HELP
:
441 BT_OBJECT_PUT_REF_AND_RESET(cfg
->cmd_data
.help
.cfg_component
);
443 case BT_CONFIG_COMMAND_QUERY
:
444 BT_OBJECT_PUT_REF_AND_RESET(cfg
->cmd_data
.query
.cfg_component
);
446 if (cfg
->cmd_data
.query
.object
) {
447 g_string_free(cfg
->cmd_data
.query
.object
, TRUE
);
450 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
451 if (cfg
->cmd_data
.print_ctf_metadata
.path
) {
452 g_string_free(cfg
->cmd_data
.print_ctf_metadata
.path
,
455 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
459 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
460 if (cfg
->cmd_data
.print_lttng_live_sessions
.url
) {
462 cfg
->cmd_data
.print_lttng_live_sessions
.url
,
465 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
480 void destroy_glist_of_gstring(GList
*list
)
488 for (at
= list
; at
; at
= g_list_next(at
)) {
489 g_string_free(at
->data
, TRUE
);
496 * Creates a simple lexical scanner for parsing comma-delimited names
499 * Return value is owned by the caller.
502 GScanner
*create_csv_identifiers_scanner(void)
505 GScannerConfig scanner_config
= {
506 .cset_skip_characters
= (gchar
*) " \t\n",
507 .cset_identifier_first
= (gchar
*) G_CSET_a_2_z G_CSET_A_2_Z
"_",
508 .cset_identifier_nth
= (gchar
*) G_CSET_a_2_z G_CSET_A_2_Z
":_-",
509 .case_sensitive
= TRUE
,
510 .cpair_comment_single
= NULL
,
511 .skip_comment_multi
= TRUE
,
512 .skip_comment_single
= TRUE
,
513 .scan_comment_multi
= FALSE
,
514 .scan_identifier
= TRUE
,
515 .scan_identifier_1char
= TRUE
,
516 .scan_identifier_NULL
= FALSE
,
517 .scan_symbols
= FALSE
,
518 .symbol_2_token
= FALSE
,
519 .scope_0_fallback
= FALSE
,
520 .scan_binary
= FALSE
,
524 .scan_hex_dollar
= FALSE
,
525 .numbers_2_int
= FALSE
,
526 .int_2_float
= FALSE
,
527 .store_int64
= FALSE
,
528 .scan_string_sq
= FALSE
,
529 .scan_string_dq
= FALSE
,
530 .identifier_2_string
= FALSE
,
531 .char_2_token
= TRUE
,
534 scanner
= g_scanner_new(&scanner_config
);
536 BT_CLI_LOGE_APPEND_CAUSE_OOM();
543 * Converts a comma-delimited list of known names (--names option) to
544 * an array value object containing those names as string value objects.
546 * Return value is owned by the caller.
549 bt_value
*names_from_arg(const char *arg
)
551 GScanner
*scanner
= NULL
;
552 bt_value
*names
= NULL
;
553 bool found_all
= false, found_none
= false, found_item
= false;
555 names
= bt_value_array_create();
557 BT_CLI_LOGE_APPEND_CAUSE_OOM();
561 scanner
= create_csv_identifiers_scanner();
566 g_scanner_input_text(scanner
, arg
, strlen(arg
));
569 GTokenType token_type
= g_scanner_get_next_token(scanner
);
571 switch (token_type
) {
572 case G_TOKEN_IDENTIFIER
:
574 const char *identifier
= scanner
->value
.v_identifier
;
576 if (strcmp(identifier
, "payload") == 0 ||
577 strcmp(identifier
, "args") == 0 ||
578 strcmp(identifier
, "arg") == 0) {
580 if (bt_value_array_append_string_element(names
,
584 } else if (strcmp(identifier
, "context") == 0 ||
585 strcmp(identifier
, "ctx") == 0) {
587 if (bt_value_array_append_string_element(names
,
591 } else if (strcmp(identifier
, "scope") == 0 ||
592 strcmp(identifier
, "header") == 0) {
594 if (bt_value_array_append_string_element(names
,
598 } else if (strcmp(identifier
, "all") == 0) {
600 if (bt_value_array_append_string_element(names
,
604 } else if (strcmp(identifier
, "none") == 0) {
606 if (bt_value_array_append_string_element(names
,
611 BT_CLI_LOGE_APPEND_CAUSE("Unknown name: `%s`.",
627 if (found_none
&& found_all
) {
628 BT_CLI_LOGE_APPEND_CAUSE("Only either `all` or `none` can be specified in the list given to the --names option, but not both.");
632 * Legacy behavior is to clear the defaults (show none) when at
633 * least one item is specified.
635 if (found_item
&& !found_none
&& !found_all
) {
636 if (bt_value_array_append_string_element(names
, "none")) {
641 g_scanner_destroy(scanner
);
646 BT_VALUE_PUT_REF_AND_RESET(names
);
648 g_scanner_destroy(scanner
);
654 * Converts a comma-delimited list of known fields (--fields option) to
655 * an array value object containing those fields as string
658 * Return value is owned by the caller.
661 bt_value
*fields_from_arg(const char *arg
)
663 GScanner
*scanner
= NULL
;
666 fields
= bt_value_array_create();
668 BT_CLI_LOGE_APPEND_CAUSE_OOM();
672 scanner
= create_csv_identifiers_scanner();
677 g_scanner_input_text(scanner
, arg
, strlen(arg
));
680 GTokenType token_type
= g_scanner_get_next_token(scanner
);
682 switch (token_type
) {
683 case G_TOKEN_IDENTIFIER
:
685 const char *identifier
= scanner
->value
.v_identifier
;
687 if (strcmp(identifier
, "trace") == 0 ||
688 strcmp(identifier
, "trace:hostname") == 0 ||
689 strcmp(identifier
, "trace:domain") == 0 ||
690 strcmp(identifier
, "trace:procname") == 0 ||
691 strcmp(identifier
, "trace:vpid") == 0 ||
692 strcmp(identifier
, "loglevel") == 0 ||
693 strcmp(identifier
, "emf") == 0 ||
694 strcmp(identifier
, "callsite") == 0 ||
695 strcmp(identifier
, "all") == 0) {
696 if (bt_value_array_append_string_element(fields
,
701 BT_CLI_LOGE_APPEND_CAUSE("Unknown field: `%s`.",
719 BT_VALUE_PUT_REF_AND_RESET(fields
);
723 g_scanner_destroy(scanner
);
729 void append_param_arg(GString
*params_arg
, const char *key
, const char *value
)
731 BT_ASSERT(params_arg
);
735 if (params_arg
->len
!= 0) {
736 g_string_append_c(params_arg
, ',');
739 g_string_append(params_arg
, key
);
740 g_string_append_c(params_arg
, '=');
741 g_string_append(params_arg
, value
);
745 * Inserts the equivalent "prefix-NAME=yes" strings into params_arg
746 * where the names are in names_array.
749 int insert_flat_params_from_array(GString
*params_arg
,
750 const bt_value
*names_array
, const char *prefix
)
754 GString
*tmpstr
= NULL
, *default_value
= NULL
;
755 bool default_set
= false, non_default_set
= false;
758 * names_array may be NULL if no CLI options were specified to
759 * trigger its creation.
765 tmpstr
= g_string_new(NULL
);
767 BT_CLI_LOGE_APPEND_CAUSE_OOM();
772 default_value
= g_string_new(NULL
);
773 if (!default_value
) {
774 BT_CLI_LOGE_APPEND_CAUSE_OOM();
779 for (i
= 0; i
< bt_value_array_get_length(names_array
); i
++) {
780 const bt_value
*str_obj
=
781 bt_value_array_borrow_element_by_index_const(names_array
,
784 bool is_default
= false;
786 suffix
= bt_value_string_get(str_obj
);
788 g_string_assign(tmpstr
, prefix
);
789 g_string_append(tmpstr
, "-");
791 /* Special-case for "all" and "none". */
792 if (strcmp(suffix
, "all") == 0) {
794 g_string_assign(default_value
, "show");
795 } else if (strcmp(suffix
, "none") == 0) {
797 g_string_assign(default_value
, "hide");
801 g_string_append(tmpstr
, "default");
802 append_param_arg(params_arg
, tmpstr
->str
,
805 non_default_set
= true;
806 g_string_append(tmpstr
, suffix
);
807 append_param_arg(params_arg
, tmpstr
->str
, "yes");
811 /* Implicit field-default=hide if any non-default option is set. */
812 if (non_default_set
&& !default_set
) {
813 g_string_assign(tmpstr
, prefix
);
814 g_string_append(tmpstr
, "-default");
815 g_string_assign(default_value
, "hide");
816 append_param_arg(params_arg
, tmpstr
->str
, default_value
->str
);
821 g_string_free(default_value
, TRUE
);
825 g_string_free(tmpstr
, TRUE
);
838 OPT_CLOCK_FORCE_CORRELATE
,
849 OPT_DEBUG_INFO_FULL_PATH
,
850 OPT_DEBUG_INFO_TARGET_PREFIX
,
859 OPT_OMIT_HOME_PLUGIN_PATH
,
860 OPT_OMIT_SYSTEM_PLUGIN_PATH
,
865 OPT_RESET_BASE_PARAMS
,
869 OPT_STREAM_INTERSECTION
,
875 enum bt_config_component_dest
{
876 BT_CONFIG_COMPONENT_DEST_UNKNOWN
= -1,
877 BT_CONFIG_COMPONENT_DEST_SOURCE
,
878 BT_CONFIG_COMPONENT_DEST_FILTER
,
879 BT_CONFIG_COMPONENT_DEST_SINK
,
883 * Adds a configuration component to the appropriate configuration
884 * array depending on the destination.
887 void add_run_cfg_comp(struct bt_config
*cfg
,
888 struct bt_config_component
*cfg_comp
,
889 enum bt_config_component_dest dest
)
891 bt_object_get_ref(cfg_comp
);
894 case BT_CONFIG_COMPONENT_DEST_SOURCE
:
895 g_ptr_array_add(cfg
->cmd_data
.run
.sources
, cfg_comp
);
897 case BT_CONFIG_COMPONENT_DEST_FILTER
:
898 g_ptr_array_add(cfg
->cmd_data
.run
.filters
, cfg_comp
);
900 case BT_CONFIG_COMPONENT_DEST_SINK
:
901 g_ptr_array_add(cfg
->cmd_data
.run
.sinks
, cfg_comp
);
909 int add_run_cfg_comp_check_name(struct bt_config
*cfg
,
910 struct bt_config_component
*cfg_comp
,
911 enum bt_config_component_dest dest
,
912 bt_value
*instance_names
)
916 if (cfg_comp
->instance_name
->len
== 0) {
917 BT_CLI_LOGE_APPEND_CAUSE("Found an unnamed component.");
922 if (bt_value_map_has_entry(instance_names
,
923 cfg_comp
->instance_name
->str
)) {
924 BT_CLI_LOGE_APPEND_CAUSE("Duplicate component instance name:\n %s",
925 cfg_comp
->instance_name
->str
);
930 if (bt_value_map_insert_entry(instance_names
,
931 cfg_comp
->instance_name
->str
, bt_value_null
)) {
932 BT_CLI_LOGE_APPEND_CAUSE_OOM();
937 add_run_cfg_comp(cfg
, cfg_comp
, dest
);
944 int append_env_var_plugin_paths(bt_value
*plugin_paths
)
949 if (bt_common_is_setuid_setgid()) {
950 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
954 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
959 ret
= bt_config_append_plugin_paths(plugin_paths
, envvar
);
963 BT_CLI_LOGE_APPEND_CAUSE("Cannot append plugin paths from BABELTRACE_PLUGIN_PATH.");
970 int append_home_and_system_plugin_paths(bt_value
*plugin_paths
,
971 bool omit_system_plugin_path
, bool omit_home_plugin_path
)
975 if (!omit_home_plugin_path
) {
976 if (bt_common_is_setuid_setgid()) {
977 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
979 char *home_plugin_dir
= bt_common_get_home_plugin_path(
980 BT_LOG_OUTPUT_LEVEL
);
982 if (home_plugin_dir
) {
983 ret
= bt_config_append_plugin_paths(
984 plugin_paths
, home_plugin_dir
);
985 free(home_plugin_dir
);
988 BT_CLI_LOGE_APPEND_CAUSE("Invalid home plugin path.");
995 if (!omit_system_plugin_path
) {
996 if (bt_config_append_plugin_paths(plugin_paths
,
997 bt_common_get_system_plugin_path())) {
998 BT_CLI_LOGE_APPEND_CAUSE("Invalid system plugin path.");
1004 BT_CLI_LOGE_APPEND_CAUSE("Cannot append home and system plugin paths.");
1009 struct bt_config
*bt_config_base_create(enum bt_config_command command
,
1010 const bt_value
*plugin_paths
, bool needs_plugins
)
1012 struct bt_config
*cfg
;
1015 cfg
= g_new0(struct bt_config
, 1);
1017 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1021 bt_object_init_shared(&cfg
->base
, bt_config_destroy
);
1022 cfg
->command
= command
;
1023 cfg
->command_needs_plugins
= needs_plugins
;
1026 bt_value
*plugin_paths_copy
;
1028 (void) bt_value_copy(plugin_paths
,
1029 &plugin_paths_copy
);
1030 cfg
->plugin_paths
= plugin_paths_copy
;
1032 cfg
->plugin_paths
= bt_value_array_create();
1033 if (!cfg
->plugin_paths
) {
1034 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1042 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1049 struct bt_config
*bt_config_run_create(const bt_value
*plugin_paths
)
1051 struct bt_config
*cfg
;
1054 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_RUN
,
1055 plugin_paths
, true);
1060 cfg
->cmd_data
.run
.sources
= g_ptr_array_new_with_free_func(
1061 (GDestroyNotify
) bt_object_put_ref
);
1062 if (!cfg
->cmd_data
.run
.sources
) {
1063 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1067 cfg
->cmd_data
.run
.filters
= g_ptr_array_new_with_free_func(
1068 (GDestroyNotify
) bt_object_put_ref
);
1069 if (!cfg
->cmd_data
.run
.filters
) {
1070 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1074 cfg
->cmd_data
.run
.sinks
= g_ptr_array_new_with_free_func(
1075 (GDestroyNotify
) bt_object_put_ref
);
1076 if (!cfg
->cmd_data
.run
.sinks
) {
1077 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1081 cfg
->cmd_data
.run
.connections
= g_ptr_array_new_with_free_func(
1082 (GDestroyNotify
) bt_config_connection_destroy
);
1083 if (!cfg
->cmd_data
.run
.connections
) {
1084 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1091 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1098 struct bt_config
*bt_config_list_plugins_create(const bt_value
*plugin_paths
)
1100 return bt_config_base_create(BT_CONFIG_COMMAND_LIST_PLUGINS
,
1101 plugin_paths
, true);
1105 struct bt_config
*bt_config_help_create(const bt_value
*plugin_paths
,
1106 int default_log_level
)
1108 struct bt_config
*cfg
;
1111 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_HELP
,
1112 plugin_paths
, true);
1117 cfg
->cmd_data
.help
.cfg_component
=
1118 bt_config_component_create(-1, NULL
, NULL
, default_log_level
);
1119 if (!cfg
->cmd_data
.help
.cfg_component
) {
1126 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1133 struct bt_config
*bt_config_query_create(const bt_value
*plugin_paths
)
1135 struct bt_config
*cfg
;
1138 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_QUERY
,
1139 plugin_paths
, true);
1144 cfg
->cmd_data
.query
.object
= g_string_new(NULL
);
1145 if (!cfg
->cmd_data
.query
.object
) {
1146 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1153 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1160 struct bt_config
*bt_config_print_ctf_metadata_create(
1161 const bt_value
*plugin_paths
)
1163 struct bt_config
*cfg
;
1166 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_PRINT_CTF_METADATA
,
1167 plugin_paths
, true);
1172 cfg
->cmd_data
.print_ctf_metadata
.path
= g_string_new(NULL
);
1173 if (!cfg
->cmd_data
.print_ctf_metadata
.path
) {
1174 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1178 cfg
->cmd_data
.print_ctf_metadata
.output_path
= g_string_new(NULL
);
1179 if (!cfg
->cmd_data
.print_ctf_metadata
.output_path
) {
1180 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1187 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1194 struct bt_config
*bt_config_print_lttng_live_sessions_create(
1195 const bt_value
*plugin_paths
)
1197 struct bt_config
*cfg
;
1200 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
,
1201 plugin_paths
, true);
1206 cfg
->cmd_data
.print_lttng_live_sessions
.url
= g_string_new(NULL
);
1207 if (!cfg
->cmd_data
.print_lttng_live_sessions
.url
) {
1208 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1212 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
=
1214 if (!cfg
->cmd_data
.print_lttng_live_sessions
.output_path
) {
1215 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1222 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1229 int bt_config_append_plugin_paths_check_setuid_setgid(
1230 bt_value
*plugin_paths
, const char *arg
)
1234 if (bt_common_is_setuid_setgid()) {
1235 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1239 if (bt_config_append_plugin_paths(plugin_paths
, arg
)) {
1240 BT_CLI_LOGE_APPEND_CAUSE("Invalid --plugin-path option's argument:\n %s",
1251 * Prints the expected format for a --params option.
1254 void print_expected_params_format(FILE *fp
)
1256 fprintf(fp
, "Expected format of PARAMS\n");
1257 fprintf(fp
, "-------------------------\n");
1259 fprintf(fp
, " PARAM=VALUE[,PARAM=VALUE]...\n");
1261 fprintf(fp
, "The parameter string is a comma-separated list of PARAM=VALUE assignments,\n");
1262 fprintf(fp
, "where PARAM is the parameter name (C identifier plus the [:.-] characters),\n");
1263 fprintf(fp
, "and VALUE can be one of:\n");
1265 fprintf(fp
, "* `null`, `nul`, `NULL`: null value (no backticks).\n");
1266 fprintf(fp
, "* `true`, `TRUE`, `yes`, `YES`: true boolean value (no backticks).\n");
1267 fprintf(fp
, "* `false`, `FALSE`, `no`, `NO`: false boolean value (no backticks).\n");
1268 fprintf(fp
, "* Binary (`0b` prefix), octal (`0` prefix), decimal, or hexadecimal\n");
1269 fprintf(fp
, " (`0x` prefix) unsigned (with `+` prefix) or signed 64-bit integer.\n");
1270 fprintf(fp
, "* Double precision floating point number (scientific notation is accepted).\n");
1271 fprintf(fp
, "* Unquoted string with no special characters, and not matching any of\n");
1272 fprintf(fp
, " the null and boolean value symbols above.\n");
1273 fprintf(fp
, "* Double-quoted string (accepts escape characters).\n");
1274 fprintf(fp
, "* Array, formatted as an opening `[`, a list of comma-separated values\n");
1275 fprintf(fp
, " (as described by the current list) and a closing `]`.\n");
1276 fprintf(fp
, "* Map, formatted as an opening `{`, a comma-separated list of PARAM=VALUE\n");
1277 fprintf(fp
, " assignments and a closing `}`.\n");
1279 fprintf(fp
, "You can put whitespaces allowed around individual `=` and `,` symbols.\n");
1281 fprintf(fp
, "Example:\n");
1283 fprintf(fp
, " many=null, fresh=yes, condition=false, squirrel=-782329,\n");
1284 fprintf(fp
, " play=+23, observe=3.14, simple=beef, needs-quotes=\"some string\",\n");
1285 fprintf(fp
, " escape.chars-are:allowed=\"this is a \\\" double quote\",\n");
1286 fprintf(fp
, " things=[1, \"2\", 3]\n");
1288 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1289 fprintf(fp
, "babeltrace2 from a shell.\n");
1293 bool help_option_is_specified(
1294 const struct argpar_parse_ret
*argpar_parse_ret
)
1297 bool specified
= false;
1299 for (i
= 0; i
< argpar_parse_ret
->items
->n_items
; i
++) {
1300 struct argpar_item
*argpar_item
=
1301 argpar_parse_ret
->items
->items
[i
];
1302 struct argpar_item_opt
*argpar_item_opt
;
1304 if (argpar_item
->type
!= ARGPAR_ITEM_TYPE_OPT
) {
1308 argpar_item_opt
= (struct argpar_item_opt
*) argpar_item
;
1309 if (argpar_item_opt
->descr
->id
== OPT_HELP
) {
1319 * Prints the help command usage.
1322 void print_help_usage(FILE *fp
)
1324 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] help [OPTIONS] PLUGIN\n");
1325 fprintf(fp
, " babeltrace2 [GENERAL OPTIONS] help [OPTIONS] TYPE.PLUGIN.CLS\n");
1327 fprintf(fp
, "Options:\n");
1329 fprintf(fp
, " -h, --help Show this help and quit\n");
1331 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1333 fprintf(fp
, "Use `babeltrace2 list-plugins` to show the list of available plugins.\n");
1337 const struct argpar_opt_descr help_options
[] = {
1338 /* id, short_name, long_name, with_arg */
1339 { OPT_HELP
, 'h', "help", false },
1340 ARGPAR_OPT_DESCR_SENTINEL
1344 * Creates a Babeltrace config object from the arguments of a help
1347 * *retcode is set to the appropriate exit code to use.
1350 struct bt_config
*bt_config_help_from_args(int argc
, const char *argv
[],
1351 int *retcode
, const bt_value
*plugin_paths
,
1352 int default_log_level
)
1354 struct bt_config
*cfg
= NULL
;
1355 char *plugin_name
= NULL
, *comp_cls_name
= NULL
;
1356 struct argpar_parse_ret argpar_parse_ret
= { 0 };
1357 struct argpar_item_non_opt
*non_opt
;
1358 GString
*substring
= NULL
;
1362 cfg
= bt_config_help_create(plugin_paths
, default_log_level
);
1368 argpar_parse_ret
= argpar_parse(argc
, argv
, help_options
, true);
1369 if (argpar_parse_ret
.error
) {
1370 BT_CLI_LOGE_APPEND_CAUSE(
1371 "While parsing `help` command's command-line arguments: %s",
1372 argpar_parse_ret
.error
);
1376 if (help_option_is_specified(&argpar_parse_ret
)) {
1377 print_help_usage(stdout
);
1379 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1383 if (argpar_parse_ret
.items
->n_items
== 0) {
1384 BT_CLI_LOGE_APPEND_CAUSE(
1385 "Missing plugin name or component class descriptor.");
1387 } else if (argpar_parse_ret
.items
->n_items
> 1) {
1389 * At this point we know there are least two non-option
1390 * arguments because we don't reach here with `--help`,
1393 non_opt
= (struct argpar_item_non_opt
*) argpar_parse_ret
.items
->items
[1];
1394 BT_CLI_LOGE_APPEND_CAUSE(
1395 "Extraneous command-line argument specified to `help` command: `%s`.",
1400 non_opt
= (struct argpar_item_non_opt
*) argpar_parse_ret
.items
->items
[0];
1402 /* Look for unescaped dots in the argument. */
1403 substring
= bt_common_string_until(non_opt
->arg
, ".\\", ".", &end_pos
);
1405 BT_CLI_LOGE_APPEND_CAUSE("Could not consume argument: arg=%s",
1410 if (end_pos
== strlen(non_opt
->arg
)) {
1411 /* Didn't find an unescaped dot, treat it as a plugin name. */
1412 g_string_assign(cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
1416 * Found an unescaped dot, treat it as a component class name.
1418 plugin_comp_cls_names(non_opt
->arg
, NULL
, &plugin_name
, &comp_cls_name
,
1419 &cfg
->cmd_data
.help
.cfg_component
->type
);
1420 if (!plugin_name
|| !comp_cls_name
) {
1421 BT_CLI_LOGE_APPEND_CAUSE(
1422 "Could not parse argument as a component class name: arg=%s",
1427 g_string_assign(cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
1429 g_string_assign(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
,
1437 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1440 g_free(plugin_name
);
1441 g_free(comp_cls_name
);
1444 g_string_free(substring
, TRUE
);
1447 argpar_parse_ret_fini(&argpar_parse_ret
);
1453 * Prints the help command usage.
1456 void print_query_usage(FILE *fp
)
1458 fprintf(fp
, "Usage: babeltrace2 [GEN OPTS] query [OPTS] TYPE.PLUGIN.CLS OBJECT\n");
1460 fprintf(fp
, "Options:\n");
1462 fprintf(fp
, " -p, --params=PARAMS Set the query parameters to PARAMS (see the expected\n");
1463 fprintf(fp
, " format of PARAMS below)\n");
1464 fprintf(fp
, " -h, --help Show this help and quit\n");
1465 fprintf(fp
, "\n\n");
1466 print_expected_params_format(fp
);
1470 const struct argpar_opt_descr query_options
[] = {
1471 /* id, short_name, long_name, with_arg */
1472 { OPT_HELP
, 'h', "help", false },
1473 { OPT_PARAMS
, 'p', "params", true },
1474 ARGPAR_OPT_DESCR_SENTINEL
1478 * Creates a Babeltrace config object from the arguments of a query
1481 * *retcode is set to the appropriate exit code to use.
1484 struct bt_config
*bt_config_query_from_args(int argc
, const char *argv
[],
1485 int *retcode
, const bt_value
*plugin_paths
,
1486 int default_log_level
)
1489 struct bt_config
*cfg
= NULL
;
1490 const char *component_class_spec
= NULL
;
1491 const char *query_object
= NULL
;
1492 GString
*error_str
= NULL
;
1493 struct argpar_parse_ret argpar_parse_ret
= { 0 };
1495 bt_value
*params
= bt_value_map_create();
1497 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1502 cfg
= bt_config_query_create(plugin_paths
);
1507 error_str
= g_string_new(NULL
);
1509 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1514 argpar_parse_ret
= argpar_parse(argc
, argv
, query_options
, true);
1515 if (argpar_parse_ret
.error
) {
1516 BT_CLI_LOGE_APPEND_CAUSE(
1517 "While parsing `query` command's command-line arguments: %s",
1518 argpar_parse_ret
.error
);
1522 if (help_option_is_specified(&argpar_parse_ret
)) {
1523 print_query_usage(stdout
);
1525 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1529 for (i
= 0; i
< argpar_parse_ret
.items
->n_items
; i
++) {
1530 struct argpar_item
*argpar_item
=
1531 argpar_parse_ret
.items
->items
[i
];
1533 if (argpar_item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1534 struct argpar_item_opt
*argpar_item_opt
=
1535 (struct argpar_item_opt
*) argpar_item
;
1536 const char *arg
= argpar_item_opt
->arg
;
1538 switch (argpar_item_opt
->descr
->id
) {
1541 bt_value
*parsed_params
= bt_param_parse(arg
, error_str
);
1542 bt_value_map_extend_status extend_status
;
1543 if (!parsed_params
) {
1544 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
1549 extend_status
= bt_value_map_extend(params
, parsed_params
);
1550 BT_VALUE_PUT_REF_AND_RESET(parsed_params
);
1551 if (extend_status
) {
1552 BT_CLI_LOGE_APPEND_CAUSE("Cannot extend current parameters with --params option's argument:\n %s",
1559 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1560 argpar_item_opt
->descr
->id
);
1564 struct argpar_item_non_opt
*argpar_item_non_opt
1565 = (struct argpar_item_non_opt
*) argpar_item
;
1568 * We need exactly two non-option arguments
1569 * which are the mandatory component class
1570 * specification and query object.
1572 if (!component_class_spec
) {
1573 component_class_spec
= argpar_item_non_opt
->arg
;
1574 } else if (!query_object
) {
1575 query_object
= argpar_item_non_opt
->arg
;
1577 BT_CLI_LOGE_APPEND_CAUSE("Extraneous command-line argument specified to `query` command: `%s`.",
1578 argpar_item_non_opt
->arg
);
1584 if (!component_class_spec
|| !query_object
) {
1585 print_query_usage(stdout
);
1587 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1591 cfg
->cmd_data
.query
.cfg_component
=
1592 bt_config_component_from_arg(component_class_spec
,
1594 if (!cfg
->cmd_data
.query
.cfg_component
) {
1595 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for component class specification:\n %s",
1596 component_class_spec
);
1601 BT_OBJECT_MOVE_REF(cfg
->cmd_data
.query
.cfg_component
->params
, params
);
1603 if (strlen(query_object
) == 0) {
1604 BT_CLI_LOGE_APPEND_CAUSE("Invalid empty object.");
1608 g_string_assign(cfg
->cmd_data
.query
.object
, query_object
);
1613 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1616 argpar_parse_ret_fini(&argpar_parse_ret
);
1619 g_string_free(error_str
, TRUE
);
1622 bt_value_put_ref(params
);
1627 * Prints the list-plugins command usage.
1630 void print_list_plugins_usage(FILE *fp
)
1632 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] list-plugins [OPTIONS]\n");
1634 fprintf(fp
, "Options:\n");
1636 fprintf(fp
, " -h, --help Show this help and quit\n");
1638 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1640 fprintf(fp
, "Use `babeltrace2 help` to get help for a specific plugin or component class.\n");
1644 const struct argpar_opt_descr list_plugins_options
[] = {
1645 /* id, short_name, long_name, with_arg */
1646 { OPT_HELP
, 'h', "help", false },
1647 ARGPAR_OPT_DESCR_SENTINEL
1651 * Creates a Babeltrace config object from the arguments of a
1652 * list-plugins command.
1654 * *retcode is set to the appropriate exit code to use.
1657 struct bt_config
*bt_config_list_plugins_from_args(int argc
, const char *argv
[],
1658 int *retcode
, const bt_value
*plugin_paths
)
1660 struct bt_config
*cfg
= NULL
;
1661 struct argpar_parse_ret argpar_parse_ret
= { 0 };
1664 cfg
= bt_config_list_plugins_create(plugin_paths
);
1670 argpar_parse_ret
= argpar_parse(argc
, argv
, list_plugins_options
, true);
1671 if (argpar_parse_ret
.error
) {
1672 BT_CLI_LOGE_APPEND_CAUSE(
1673 "While parsing `list-plugins` command's command-line arguments: %s",
1674 argpar_parse_ret
.error
);
1678 if (help_option_is_specified(&argpar_parse_ret
)) {
1679 print_list_plugins_usage(stdout
);
1681 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1685 if (argpar_parse_ret
.items
->n_items
> 0) {
1687 * At this point we know there's at least one non-option
1688 * argument because we don't reach here with `--help`,
1691 struct argpar_item_non_opt
*non_opt
=
1692 (struct argpar_item_non_opt
*) argpar_parse_ret
.items
->items
[0];
1694 BT_CLI_LOGE_APPEND_CAUSE(
1695 "Extraneous command-line argument specified to `list-plugins` command: `%s`.",
1704 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1707 argpar_parse_ret_fini(&argpar_parse_ret
);
1713 * Prints the run command usage.
1716 void print_run_usage(FILE *fp
)
1718 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] run [OPTIONS]\n");
1720 fprintf(fp
, "Options:\n");
1722 fprintf(fp
, " -b, --base-params=PARAMS Set PARAMS as the current base parameters\n");
1723 fprintf(fp
, " for all the following components until\n");
1724 fprintf(fp
, " --reset-base-params is encountered\n");
1725 fprintf(fp
, " (see the expected format of PARAMS below)\n");
1726 fprintf(fp
, " -c, --component=NAME:TYPE.PLUGIN.CLS\n");
1727 fprintf(fp
, " Instantiate the component class CLS of type\n");
1728 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
1729 fprintf(fp
, " in the plugin PLUGIN, add it to the graph,\n");
1730 fprintf(fp
, " and name it NAME");
1731 fprintf(fp
, " -x, --connect=CONNECTION Connect two created components (see the\n");
1732 fprintf(fp
, " expected format of CONNECTION below)\n");
1733 fprintf(fp
, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
1734 fprintf(fp
, " (`N`, `T`, `D`, `I`, `W`, `E`, or `F`)\n");
1735 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
1736 fprintf(fp
, " current component (see the expected format\n");
1737 fprintf(fp
, " of PARAMS below)\n");
1738 fprintf(fp
, " -r, --reset-base-params Reset the current base parameters to an\n");
1739 fprintf(fp
, " empty map\n");
1740 fprintf(fp
, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
1741 fprintf(fp
, " the graph later, retry in DUR µs\n");
1742 fprintf(fp
, " (default: 100000)\n");
1743 fprintf(fp
, " -h, --help Show this help and quit\n");
1745 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1746 fprintf(fp
, "\n\n");
1747 fprintf(fp
, "Expected format of CONNECTION\n");
1748 fprintf(fp
, "-----------------------------\n");
1750 fprintf(fp
, " UPSTREAM[.UPSTREAM-PORT]:DOWNSTREAM[.DOWNSTREAM-PORT]\n");
1752 fprintf(fp
, "UPSTREAM and DOWNSTREAM are names of the upstream and downstream\n");
1753 fprintf(fp
, "components to connect together. You must escape the following characters\n\n");
1754 fprintf(fp
, "with `\\`: `\\`, `.`, and `:`. You must set the name of the current\n");
1755 fprintf(fp
, "component using the NAME prefix of the --component option.\n");
1757 fprintf(fp
, "UPSTREAM-PORT and DOWNSTREAM-PORT are optional globbing patterns to\n");
1758 fprintf(fp
, "identify the upstream and downstream ports to use for the connection.\n");
1759 fprintf(fp
, "When the port is not specified, `*` is used.\n");
1761 fprintf(fp
, "When a component named UPSTREAM has an available port which matches the\n");
1762 fprintf(fp
, "UPSTREAM-PORT globbing pattern, it is connected to the first port which\n");
1763 fprintf(fp
, "matches the DOWNSTREAM-PORT globbing pattern of the component named\n");
1764 fprintf(fp
, "DOWNSTREAM.\n");
1766 fprintf(fp
, "The only special character in UPSTREAM-PORT and DOWNSTREAM-PORT is `*`\n");
1767 fprintf(fp
, "which matches anything. You must escape the following characters\n");
1768 fprintf(fp
, "with `\\`: `\\`, `*`, `?`, `[`, `.`, and `:`.\n");
1770 fprintf(fp
, "You can connect a source component to a filter or sink component. You\n");
1771 fprintf(fp
, "can connect a filter component to a sink component.\n");
1773 fprintf(fp
, "Examples:\n");
1775 fprintf(fp
, " my-src:my-sink\n");
1776 fprintf(fp
, " ctf-fs.*stream*:utils-muxer:*\n");
1778 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1779 fprintf(fp
, "babeltrace2 from a shell.\n");
1780 fprintf(fp
, "\n\n");
1781 print_expected_params_format(fp
);
1785 * Creates a Babeltrace config object from the arguments of a run
1788 * *retcode is set to the appropriate exit code to use.
1791 struct bt_config
*bt_config_run_from_args(int argc
, const char *argv
[],
1792 int *retcode
, const bt_value
*plugin_paths
,
1793 int default_log_level
)
1795 struct bt_config_component
*cur_cfg_comp
= NULL
;
1796 bt_value
*cur_base_params
= NULL
;
1798 struct bt_config
*cfg
= NULL
;
1799 bt_value
*instance_names
= NULL
;
1800 bt_value
*connection_args
= NULL
;
1801 char error_buf
[256] = { 0 };
1802 long retry_duration
= -1;
1803 bt_value_map_extend_status extend_status
;
1804 GString
*error_str
= NULL
;
1805 struct argpar_parse_ret argpar_parse_ret
= { 0 };
1808 static const struct argpar_opt_descr run_options
[] = {
1809 { OPT_BASE_PARAMS
, 'b', "base-params", true },
1810 { OPT_COMPONENT
, 'c', "component", true },
1811 { OPT_CONNECT
, 'x', "connect", true },
1812 { OPT_HELP
, 'h', "help", false },
1813 { OPT_LOG_LEVEL
, 'l', "log-level", true },
1814 { OPT_PARAMS
, 'p', "params", true },
1815 { OPT_RESET_BASE_PARAMS
, 'r', "reset-base-params", false },
1816 { OPT_RETRY_DURATION
, '\0', "retry-duration", true },
1817 ARGPAR_OPT_DESCR_SENTINEL
1822 error_str
= g_string_new(NULL
);
1824 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1829 print_run_usage(stdout
);
1834 cfg
= bt_config_run_create(plugin_paths
);
1839 cfg
->cmd_data
.run
.retry_duration_us
= 100000;
1840 cur_base_params
= bt_value_map_create();
1841 if (!cur_base_params
) {
1842 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1846 instance_names
= bt_value_map_create();
1847 if (!instance_names
) {
1848 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1852 connection_args
= bt_value_array_create();
1853 if (!connection_args
) {
1854 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1859 argpar_parse_ret
= argpar_parse(argc
, argv
, run_options
, true);
1860 if (argpar_parse_ret
.error
) {
1861 BT_CLI_LOGE_APPEND_CAUSE(
1862 "While parsing `run` command's command-line arguments: %s",
1863 argpar_parse_ret
.error
);
1867 if (help_option_is_specified(&argpar_parse_ret
)) {
1868 print_run_usage(stdout
);
1870 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1874 for (i
= 0; i
< argpar_parse_ret
.items
->n_items
; i
++) {
1875 struct argpar_item
*argpar_item
=
1876 argpar_parse_ret
.items
->items
[i
];
1877 struct argpar_item_opt
*argpar_item_opt
;
1880 /* This command does not accept non-option arguments.*/
1881 if (argpar_item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
) {
1882 struct argpar_item_non_opt
*argpar_nonopt_item
=
1883 (struct argpar_item_non_opt
*) argpar_item
;
1885 BT_CLI_LOGE_APPEND_CAUSE("Unexpected argument: `%s`",
1886 argpar_nonopt_item
->arg
);
1890 argpar_item_opt
= (struct argpar_item_opt
*) argpar_item
;
1891 arg
= argpar_item_opt
->arg
;
1893 switch (argpar_item_opt
->descr
->id
) {
1896 enum bt_config_component_dest dest
;
1898 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
1899 cur_cfg_comp
= bt_config_component_from_arg(arg
,
1901 if (!cur_cfg_comp
) {
1902 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --component option's argument:\n %s",
1907 switch (cur_cfg_comp
->type
) {
1908 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1909 dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
1911 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1912 dest
= BT_CONFIG_COMPONENT_DEST_FILTER
;
1914 case BT_COMPONENT_CLASS_TYPE_SINK
:
1915 dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
1921 BT_ASSERT(cur_base_params
);
1922 bt_value_put_ref(cur_cfg_comp
->params
);
1923 if (bt_value_copy(cur_base_params
,
1924 &cur_cfg_comp
->params
) < 0) {
1925 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1929 ret
= add_run_cfg_comp_check_name(cfg
,
1942 if (!cur_cfg_comp
) {
1943 BT_CLI_LOGE_APPEND_CAUSE("Cannot add parameters to unavailable component:\n %s",
1948 params
= bt_param_parse(arg
, error_str
);
1950 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
1955 extend_status
= bt_value_map_extend(cur_cfg_comp
->params
,
1957 BT_VALUE_PUT_REF_AND_RESET(params
);
1958 if (extend_status
!= BT_VALUE_MAP_EXTEND_STATUS_OK
) {
1959 BT_CLI_LOGE_APPEND_CAUSE("Cannot extend current component parameters with --params option's argument:\n %s",
1967 if (!cur_cfg_comp
) {
1968 BT_CLI_LOGE_APPEND_CAUSE("Cannot set the log level of unavailable component:\n %s",
1973 cur_cfg_comp
->log_level
=
1974 bt_log_get_level_from_string(arg
);
1975 if (cur_cfg_comp
->log_level
< 0) {
1976 BT_CLI_LOGE_APPEND_CAUSE("Invalid argument for --log-level option:\n %s",
1981 case OPT_BASE_PARAMS
:
1983 bt_value
*params
= bt_param_parse(arg
, error_str
);
1986 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --base-params option's argument:\n %s",
1991 BT_OBJECT_MOVE_REF(cur_base_params
, params
);
1994 case OPT_RESET_BASE_PARAMS
:
1995 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
1996 cur_base_params
= bt_value_map_create();
1997 if (!cur_base_params
) {
1998 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2003 if (bt_value_array_append_string_element(
2004 connection_args
, arg
)) {
2005 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2009 case OPT_RETRY_DURATION
: {
2011 size_t arg_len
= strlen(argpar_item_opt
->arg
);
2013 retry_duration
= g_ascii_strtoll(argpar_item_opt
->arg
, &end
, 10);
2015 if (arg_len
== 0 || end
!= (argpar_item_opt
->arg
+ arg_len
)) {
2016 BT_CLI_LOGE_APPEND_CAUSE(
2017 "Could not parse --retry-duration option's argument as an unsigned integer: `%s`",
2018 argpar_item_opt
->arg
);
2022 if (retry_duration
< 0) {
2023 BT_CLI_LOGE_APPEND_CAUSE("--retry-duration option's argument must be positive or 0: %ld",
2028 cfg
->cmd_data
.run
.retry_duration_us
=
2029 (uint64_t) retry_duration
;
2033 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
2034 argpar_item_opt
->descr
->id
);
2039 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2041 if (cfg
->cmd_data
.run
.sources
->len
== 0) {
2042 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no source component.");
2046 if (cfg
->cmd_data
.run
.sinks
->len
== 0) {
2047 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no sink component.");
2051 ret
= bt_config_cli_args_create_connections(cfg
,
2055 BT_CLI_LOGE_APPEND_CAUSE("Cannot creation connections:\n%s", error_buf
);
2063 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2067 g_string_free(error_str
, TRUE
);
2070 argpar_parse_ret_fini(&argpar_parse_ret
);
2071 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2072 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
2073 BT_VALUE_PUT_REF_AND_RESET(instance_names
);
2074 BT_VALUE_PUT_REF_AND_RESET(connection_args
);
2079 struct bt_config
*bt_config_run_from_args_array(const bt_value
*run_args
,
2080 int *retcode
, const bt_value
*plugin_paths
,
2081 int default_log_level
)
2083 struct bt_config
*cfg
= NULL
;
2085 uint64_t i
, len
= bt_value_array_get_length(run_args
);
2087 BT_ASSERT(len
<= SIZE_MAX
);
2088 argv
= calloc((size_t) len
, sizeof(*argv
));
2090 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2094 for (i
= 0; i
< len
; i
++) {
2095 const bt_value
*arg_value
=
2096 bt_value_array_borrow_element_by_index_const(run_args
,
2100 arg
= bt_value_string_get(arg_value
);
2105 cfg
= bt_config_run_from_args((int) len
, argv
, retcode
,
2106 plugin_paths
, default_log_level
);
2114 * Prints the convert command usage.
2117 void print_convert_usage(FILE *fp
)
2119 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] [convert] [OPTIONS] [PATH/URL]\n");
2121 fprintf(fp
, "Options:\n");
2123 fprintf(fp
, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2124 fprintf(fp
, " Instantiate the component class CLS of type\n");
2125 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
2126 fprintf(fp
, " in the plugin PLUGIN, add it to the\n");
2127 fprintf(fp
, " conversion graph, and optionally name it\n");
2128 fprintf(fp
, " NAME\n");
2129 fprintf(fp
, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
2130 fprintf(fp
, " (`N`, `T`, `D`, `I`, `W`, `E`, or `F`)\n");
2131 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2132 fprintf(fp
, " current component (see the expected format\n");
2133 fprintf(fp
, " of PARAMS below)\n");
2134 fprintf(fp
, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
2135 fprintf(fp
, " the graph later, retry in DUR µs\n");
2136 fprintf(fp
, " (default: 100000)\n");
2137 fprintf(fp
, " dynamic plugins can be loaded\n");
2138 fprintf(fp
, " --run-args Print the equivalent arguments for the\n");
2139 fprintf(fp
, " `run` command to the standard output,\n");
2140 fprintf(fp
, " formatted for a shell, and quit\n");
2141 fprintf(fp
, " --run-args-0 Print the equivalent arguments for the\n");
2142 fprintf(fp
, " `run` command to the standard output,\n");
2143 fprintf(fp
, " formatted for `xargs -0`, and quit\n");
2144 fprintf(fp
, " --stream-intersection Only process events when all streams\n");
2145 fprintf(fp
, " are active\n");
2146 fprintf(fp
, " -h, --help Show this help and quit\n");
2148 fprintf(fp
, "Implicit `source.ctf.fs` component options:\n");
2150 fprintf(fp
, " --clock-force-correlate Force the origin of all clocks\n");
2151 fprintf(fp
, " to the Unix epoch\n");
2152 fprintf(fp
, " --clock-offset=SEC Set clock offset to SEC seconds\n");
2153 fprintf(fp
, " --clock-offset-ns=NS Set clock offset to NS ns\n");
2155 fprintf(fp
, "Implicit `sink.text.pretty` component options:\n");
2157 fprintf(fp
, " --clock-cycles Print timestamps in clock cycles\n");
2158 fprintf(fp
, " --clock-date Print timestamp dates\n");
2159 fprintf(fp
, " --clock-gmt Print and parse timestamps in the GMT\n");
2160 fprintf(fp
, " time zone instead of the local time zone\n");
2161 fprintf(fp
, " --clock-seconds Print the timestamps as `SEC.NS` instead\n");
2162 fprintf(fp
, " of `hh:mm:ss.nnnnnnnnn`\n");
2163 fprintf(fp
, " --color=(never | auto | always)\n");
2164 fprintf(fp
, " Never, automatically, or always emit\n");
2165 fprintf(fp
, " console color codes\n");
2166 fprintf(fp
, " -f, --fields=FIELD[,FIELD]... Print additional fields; FIELD can be:\n");
2167 fprintf(fp
, " `all`, `trace`, `trace:hostname`,\n");
2168 fprintf(fp
, " `trace:domain`, `trace:procname`,\n");
2169 fprintf(fp
, " `trace:vpid`, `loglevel`, `emf`\n");
2170 fprintf(fp
, " -n, --names=NAME[,NAME]... Print field names; NAME can be:\n");
2171 fprintf(fp
, " `payload` (or `arg` or `args`), `none`,\n");
2172 fprintf(fp
, " `all`, `scope`, `header`, `context`\n");
2173 fprintf(fp
, " (or `ctx`)\n");
2174 fprintf(fp
, " --no-delta Do not print time delta between\n");
2175 fprintf(fp
, " consecutive events\n");
2176 fprintf(fp
, " -w, --output=PATH Write output text to PATH instead of\n");
2177 fprintf(fp
, " the standard output\n");
2179 fprintf(fp
, "Implicit `filter.utils.trimmer` component options:\n");
2181 fprintf(fp
, " -b, --begin=BEGIN Set the beginning time of the conversion\n");
2182 fprintf(fp
, " time range to BEGIN (see the format of\n");
2183 fprintf(fp
, " BEGIN below)\n");
2184 fprintf(fp
, " -e, --end=END Set the end time of the conversion time\n");
2185 fprintf(fp
, " range to END (see the format of END below)\n");
2186 fprintf(fp
, " -t, --timerange=TIMERANGE Set conversion time range to TIMERANGE:\n");
2187 fprintf(fp
, " BEGIN,END or [BEGIN,END] (literally `[` and\n");
2188 fprintf(fp
, " `]`) (see the format of BEGIN/END below)\n");
2190 fprintf(fp
, "Implicit `filter.lttng-utils.debug-info` component options:\n");
2192 fprintf(fp
, " --debug-info Create an implicit\n");
2193 fprintf(fp
, " `filter.lttng-utils.debug-info` component\n");
2194 fprintf(fp
, " --debug-info-dir=DIR Search for debug info in directory DIR\n");
2195 fprintf(fp
, " instead of `/usr/lib/debug`\n");
2196 fprintf(fp
, " --debug-info-full-path Show full debug info source and\n");
2197 fprintf(fp
, " binary paths instead of just names\n");
2198 fprintf(fp
, " --debug-info-target-prefix=DIR\n");
2199 fprintf(fp
, " Use directory DIR as a prefix when\n");
2200 fprintf(fp
, " looking up executables during debug\n");
2201 fprintf(fp
, " info analysis\n");
2203 fprintf(fp
, "Legacy options that still work:\n");
2205 fprintf(fp
, " -i, --input-format=(ctf | lttng-live)\n");
2206 fprintf(fp
, " `ctf`:\n");
2207 fprintf(fp
, " Create an implicit `source.ctf.fs`\n");
2208 fprintf(fp
, " component\n");
2209 fprintf(fp
, " `lttng-live`:\n");
2210 fprintf(fp
, " Create an implicit `source.ctf.lttng-live`\n");
2211 fprintf(fp
, " component\n");
2212 fprintf(fp
, " -o, --output-format=(text | ctf | dummy | ctf-metadata)\n");
2213 fprintf(fp
, " `text`:\n");
2214 fprintf(fp
, " Create an implicit `sink.text.pretty`\n");
2215 fprintf(fp
, " component\n");
2216 fprintf(fp
, " `ctf`:\n");
2217 fprintf(fp
, " Create an implicit `sink.ctf.fs`\n");
2218 fprintf(fp
, " component\n");
2219 fprintf(fp
, " `dummy`:\n");
2220 fprintf(fp
, " Create an implicit `sink.utils.dummy`\n");
2221 fprintf(fp
, " component\n");
2222 fprintf(fp
, " `ctf-metadata`:\n");
2223 fprintf(fp
, " Query the `source.ctf.fs` component class\n");
2224 fprintf(fp
, " for metadata text and quit\n");
2226 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
2227 fprintf(fp
, "\n\n");
2228 fprintf(fp
, "Format of BEGIN and END\n");
2229 fprintf(fp
, "-----------------------\n");
2231 fprintf(fp
, " [YYYY-MM-DD [hh:mm:]]ss[.nnnnnnnnn]\n");
2232 fprintf(fp
, "\n\n");
2233 print_expected_params_format(fp
);
2237 const struct argpar_opt_descr convert_options
[] = {
2238 /* id, short_name, long_name, with_arg */
2239 { OPT_BEGIN
, 'b', "begin", true },
2240 { OPT_CLOCK_CYCLES
, '\0', "clock-cycles", false },
2241 { OPT_CLOCK_DATE
, '\0', "clock-date", false },
2242 { OPT_CLOCK_FORCE_CORRELATE
, '\0', "clock-force-correlate", false },
2243 { OPT_CLOCK_GMT
, '\0', "clock-gmt", false },
2244 { OPT_CLOCK_OFFSET
, '\0', "clock-offset", true },
2245 { OPT_CLOCK_OFFSET_NS
, '\0', "clock-offset-ns", true },
2246 { OPT_CLOCK_SECONDS
, '\0', "clock-seconds", false },
2247 { OPT_COLOR
, '\0', "color", true },
2248 { OPT_COMPONENT
, 'c', "component", true },
2249 { OPT_DEBUG
, 'd', "debug", false },
2250 { OPT_DEBUG_INFO_DIR
, '\0', "debug-info-dir", true },
2251 { OPT_DEBUG_INFO_FULL_PATH
, '\0', "debug-info-full-path", false },
2252 { OPT_DEBUG_INFO_TARGET_PREFIX
, '\0', "debug-info-target-prefix", true },
2253 { OPT_END
, 'e', "end", true },
2254 { OPT_FIELDS
, 'f', "fields", true },
2255 { OPT_HELP
, 'h', "help", false },
2256 { OPT_INPUT_FORMAT
, 'i', "input-format", true },
2257 { OPT_LOG_LEVEL
, 'l', "log-level", true },
2258 { OPT_NAMES
, 'n', "names", true },
2259 { OPT_DEBUG_INFO
, '\0', "debug-info", false },
2260 { OPT_NO_DELTA
, '\0', "no-delta", false },
2261 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
2262 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
2263 { OPT_OUTPUT
, 'w', "output", true },
2264 { OPT_OUTPUT_FORMAT
, 'o', "output-format", true },
2265 { OPT_PARAMS
, 'p', "params", true },
2266 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
2267 { OPT_RETRY_DURATION
, '\0', "retry-duration", true },
2268 { OPT_RUN_ARGS
, '\0', "run-args", false },
2269 { OPT_RUN_ARGS_0
, '\0', "run-args-0", false },
2270 { OPT_STREAM_INTERSECTION
, '\0', "stream-intersection", false },
2271 { OPT_TIMERANGE
, '\0', "timerange", true },
2272 { OPT_VERBOSE
, 'v', "verbose", false },
2273 ARGPAR_OPT_DESCR_SENTINEL
2277 GString
*get_component_auto_name(const char *prefix
,
2278 const bt_value
*existing_names
)
2281 GString
*auto_name
= g_string_new(NULL
);
2284 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2288 if (!bt_value_map_has_entry(existing_names
, prefix
)) {
2289 g_string_assign(auto_name
, prefix
);
2294 g_string_printf(auto_name
, "%s-%d", prefix
, i
);
2296 } while (bt_value_map_has_entry(existing_names
, auto_name
->str
));
2302 struct implicit_component_args
{
2305 /* The component class name (e.g. src.ctf.fs). */
2308 /* The component instance name. */
2311 GString
*params_arg
;
2312 bt_value
*extra_params
;
2316 int assign_name_to_implicit_component(struct implicit_component_args
*args
,
2317 const char *prefix
, bt_value
*existing_names
,
2318 GList
**comp_names
, bool append_to_comp_names
)
2321 GString
*name
= NULL
;
2323 if (!args
->exists
) {
2327 name
= get_component_auto_name(prefix
,
2335 g_string_assign(args
->name_arg
, name
->str
);
2337 if (bt_value_map_insert_entry(existing_names
, name
->str
,
2339 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2344 if (append_to_comp_names
) {
2345 *comp_names
= g_list_append(*comp_names
, name
);
2351 g_string_free(name
, TRUE
);
2358 int append_run_args_for_implicit_component(
2359 struct implicit_component_args
*impl_args
,
2364 GString
*component_arg_for_run
= NULL
;
2366 if (!impl_args
->exists
) {
2370 component_arg_for_run
= g_string_new(NULL
);
2371 if (!component_arg_for_run
) {
2372 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2376 /* Build the full `name:type.plugin.cls`. */
2377 BT_ASSERT(!strchr(impl_args
->name_arg
->str
, '\\'));
2378 BT_ASSERT(!strchr(impl_args
->name_arg
->str
, ':'));
2379 g_string_printf(component_arg_for_run
, "%s:%s",
2380 impl_args
->name_arg
->str
, impl_args
->comp_arg
->str
);
2382 if (bt_value_array_append_string_element(run_args
, "--component")) {
2383 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2387 if (bt_value_array_append_string_element(run_args
,
2388 component_arg_for_run
->str
)) {
2389 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2393 if (impl_args
->params_arg
->len
> 0) {
2394 if (bt_value_array_append_string_element(run_args
, "--params")) {
2395 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2399 if (bt_value_array_append_string_element(run_args
,
2400 impl_args
->params_arg
->str
)) {
2401 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2406 for (i
= 0; i
< bt_value_array_get_length(impl_args
->extra_params
); i
++) {
2407 const bt_value
*elem
;
2410 elem
= bt_value_array_borrow_element_by_index(
2411 impl_args
->extra_params
, i
);
2413 BT_ASSERT(bt_value_is_string(elem
));
2414 arg
= bt_value_string_get(elem
);
2415 ret
= bt_value_array_append_string_element(run_args
, arg
);
2417 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2428 if (component_arg_for_run
) {
2429 g_string_free(component_arg_for_run
, TRUE
);
2435 /* Free the fields of a `struct implicit_component_args`. */
2438 void finalize_implicit_component_args(struct implicit_component_args
*args
)
2442 if (args
->comp_arg
) {
2443 g_string_free(args
->comp_arg
, TRUE
);
2446 if (args
->name_arg
) {
2447 g_string_free(args
->name_arg
, TRUE
);
2450 if (args
->params_arg
) {
2451 g_string_free(args
->params_arg
, TRUE
);
2454 bt_value_put_ref(args
->extra_params
);
2457 /* Destroy a dynamically-allocated `struct implicit_component_args`. */
2460 void destroy_implicit_component_args(struct implicit_component_args
*args
)
2462 finalize_implicit_component_args(args
);
2466 /* Initialize the fields of an already allocated `struct implicit_component_args`. */
2469 int init_implicit_component_args(struct implicit_component_args
*args
,
2470 const char *comp_arg
, bool exists
)
2474 args
->exists
= exists
;
2475 args
->comp_arg
= g_string_new(comp_arg
);
2476 args
->name_arg
= g_string_new(NULL
);
2477 args
->params_arg
= g_string_new(NULL
);
2478 args
->extra_params
= bt_value_array_create();
2480 if (!args
->comp_arg
|| !args
->name_arg
||
2481 !args
->params_arg
|| !args
->extra_params
) {
2483 finalize_implicit_component_args(args
);
2484 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2492 /* Dynamically allocate and initialize a `struct implicit_component_args`. */
2495 struct implicit_component_args
*create_implicit_component_args(
2496 const char *comp_arg
)
2498 struct implicit_component_args
*args
;
2501 args
= g_new(struct implicit_component_args
, 1);
2503 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2507 status
= init_implicit_component_args(args
, comp_arg
, true);
2518 void append_implicit_component_param(struct implicit_component_args
*args
,
2519 const char *key
, const char *value
)
2524 append_param_arg(args
->params_arg
, key
, value
);
2528 * Append the given parameter (`key=value`) to all component specifications
2529 * in `implicit_comp_args` (an array of `struct implicit_component_args *`)
2530 * which match `comp_arg`.
2532 * Return the number of matching components.
2536 int append_multiple_implicit_components_param(GPtrArray
*implicit_comp_args
,
2537 const char *comp_arg
, const char *key
, const char *value
)
2542 for (i
= 0; i
< implicit_comp_args
->len
; i
++) {
2543 struct implicit_component_args
*args
= implicit_comp_args
->pdata
[i
];
2545 if (strcmp(args
->comp_arg
->str
, comp_arg
) == 0) {
2546 append_implicit_component_param(args
, key
, value
);
2554 /* Escape value to make it suitable to use as a string parameter value. */
2556 gchar
*escape_string_value(const char *value
)
2561 ret
= g_string_new(NULL
);
2563 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2572 g_string_append_c(ret
, '\\');
2576 g_string_append_c(ret
, *in
);
2582 return g_string_free(ret
, FALSE
);
2586 int bt_value_to_cli_param_value_append(const bt_value
*value
, GString
*buf
)
2592 switch (bt_value_get_type(value
)) {
2593 case BT_VALUE_TYPE_STRING
:
2595 const char *str_value
= bt_value_string_get(value
);
2596 gchar
*escaped_str_value
;
2598 escaped_str_value
= escape_string_value(str_value
);
2599 if (!escaped_str_value
) {
2603 g_string_append_printf(buf
, "\"%s\"", escaped_str_value
);
2605 g_free(escaped_str_value
);
2608 case BT_VALUE_TYPE_ARRAY
: {
2609 g_string_append_c(buf
, '[');
2610 uint64_t sz
= bt_value_array_get_length(value
);
2611 for (uint64_t i
= 0; i
< sz
; i
++) {
2612 const bt_value
*item
;
2615 g_string_append(buf
, ", ");
2618 item
= bt_value_array_borrow_element_by_index_const(
2620 ret
= bt_value_to_cli_param_value_append(item
, buf
);
2626 g_string_append_c(buf
, ']');
2640 * Convert `value` to its equivalent representation as a command line parameter
2645 gchar
*bt_value_to_cli_param_value(bt_value
*value
)
2648 gchar
*result
= NULL
;
2650 buf
= g_string_new(NULL
);
2652 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2656 if (bt_value_to_cli_param_value_append(value
, buf
)) {
2660 result
= g_string_free(buf
, FALSE
);
2667 g_string_free(buf
, TRUE
);
2675 int append_parameter_to_args(bt_value
*args
, const char *key
, bt_value
*value
)
2678 BT_ASSERT(bt_value_get_type(args
) == BT_VALUE_TYPE_ARRAY
);
2683 gchar
*str_value
= NULL
;
2684 GString
*parameter
= NULL
;
2686 if (bt_value_array_append_string_element(args
, "--params")) {
2687 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2692 str_value
= bt_value_to_cli_param_value(value
);
2698 parameter
= g_string_new(NULL
);
2700 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2705 g_string_printf(parameter
, "%s=%s", key
, str_value
);
2707 if (bt_value_array_append_string_element(args
, parameter
->str
)) {
2708 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2715 g_string_free(parameter
, TRUE
);
2728 int append_string_parameter_to_args(bt_value
*args
, const char *key
, const char *value
)
2730 bt_value
*str_value
;
2733 str_value
= bt_value_string_create_init(value
);
2736 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2741 ret
= append_parameter_to_args(args
, key
, str_value
);
2744 BT_VALUE_PUT_REF_AND_RESET(str_value
);
2749 int append_implicit_component_extra_param(struct implicit_component_args
*args
,
2750 const char *key
, const char *value
)
2752 return append_string_parameter_to_args(args
->extra_params
, key
, value
);
2756 * Escapes `.`, `:`, and `\` of `input` with `\`.
2759 GString
*escape_dot_colon(const char *input
)
2761 GString
*output
= g_string_new(NULL
);
2765 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2769 for (ch
= input
; *ch
!= '\0'; ch
++) {
2770 if (*ch
== '\\' || *ch
== '.' || *ch
== ':') {
2771 g_string_append_c(output
, '\\');
2774 g_string_append_c(output
, *ch
);
2782 * Appends a --connect option to a list of arguments. `upstream_name`
2783 * and `downstream_name` are escaped with escape_dot_colon() in this
2787 int append_connect_arg(bt_value
*run_args
,
2788 const char *upstream_name
, const char *downstream_name
)
2791 GString
*e_upstream_name
= escape_dot_colon(upstream_name
);
2792 GString
*e_downstream_name
= escape_dot_colon(downstream_name
);
2793 GString
*arg
= g_string_new(NULL
);
2795 if (!e_upstream_name
|| !e_downstream_name
|| !arg
) {
2796 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2801 ret
= bt_value_array_append_string_element(run_args
, "--connect");
2803 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2808 g_string_append(arg
, e_upstream_name
->str
);
2809 g_string_append_c(arg
, ':');
2810 g_string_append(arg
, e_downstream_name
->str
);
2811 ret
= bt_value_array_append_string_element(run_args
, arg
->str
);
2813 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2820 g_string_free(arg
, TRUE
);
2823 if (e_upstream_name
) {
2824 g_string_free(e_upstream_name
, TRUE
);
2827 if (e_downstream_name
) {
2828 g_string_free(e_downstream_name
, TRUE
);
2835 * Appends the run command's --connect options for the convert command.
2838 int convert_auto_connect(bt_value
*run_args
,
2839 GList
*source_names
, GList
*filter_names
,
2843 GList
*source_at
= source_names
;
2844 GList
*filter_at
= filter_names
;
2846 GList
*sink_at
= sink_names
;
2848 BT_ASSERT(source_names
);
2849 BT_ASSERT(filter_names
);
2850 BT_ASSERT(sink_names
);
2852 /* Connect all sources to the first filter */
2853 for (source_at
= source_names
; source_at
; source_at
= g_list_next(source_at
)) {
2854 GString
*source_name
= source_at
->data
;
2855 GString
*filter_name
= filter_at
->data
;
2857 ret
= append_connect_arg(run_args
, source_name
->str
,
2864 filter_prev
= filter_at
;
2865 filter_at
= g_list_next(filter_at
);
2867 /* Connect remaining filters */
2868 for (; filter_at
; filter_prev
= filter_at
, filter_at
= g_list_next(filter_at
)) {
2869 GString
*filter_name
= filter_at
->data
;
2870 GString
*filter_prev_name
= filter_prev
->data
;
2872 ret
= append_connect_arg(run_args
, filter_prev_name
->str
,
2879 /* Connect last filter to all sinks */
2880 for (sink_at
= sink_names
; sink_at
; sink_at
= g_list_next(sink_at
)) {
2881 GString
*filter_name
= filter_prev
->data
;
2882 GString
*sink_name
= sink_at
->data
;
2884 ret
= append_connect_arg(run_args
, filter_name
->str
,
2901 int split_timerange(const char *arg
, char **begin
, char **end
)
2904 const char *ch
= arg
;
2906 GString
*g_begin
= NULL
;
2907 GString
*g_end
= NULL
;
2915 g_begin
= bt_common_string_until(ch
, "", ",", &end_pos
);
2916 if (!g_begin
|| ch
[end_pos
] != ',' || g_begin
->len
== 0) {
2922 g_end
= bt_common_string_until(ch
, "", "]", &end_pos
);
2923 if (!g_end
|| g_end
->len
== 0) {
2929 *begin
= g_begin
->str
;
2931 g_string_free(g_begin
, FALSE
);
2932 g_string_free(g_end
, FALSE
);
2942 g_string_free(g_begin
, TRUE
);
2946 g_string_free(g_end
, TRUE
);
2953 int g_list_prepend_gstring(GList
**list
, const char *string
)
2956 GString
*gs
= g_string_new(string
);
2961 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2965 *list
= g_list_prepend(*list
, gs
);
2972 * Create `struct implicit_component_args` structures for each of the
2973 * source components we identified. Add them to `component_args`.
2975 * `non_opts` is an array of the non-option arguments passed on the command
2978 * `non_opt_params` is an array where each element is an array of
2979 * strings containing all the arguments to `--params` that apply to the
2980 * non-option argument at the same index. For example, if, for a
2981 * non-option argument, the following `--params` options applied:
2983 * --params=a=2 --params=b=3,c=4
2985 * its entry in `non_opt_params` would contain
2987 * ["a=2", "b=3,c=4"]
2991 int create_implicit_component_args_from_auto_discovered_sources(
2992 const struct auto_source_discovery
*auto_disc
,
2993 const bt_value
*non_opts
,
2994 const bt_value
*non_opt_params
,
2995 const bt_value
*non_opt_loglevels
,
2996 GPtrArray
*component_args
)
2998 gchar
*cc_name
= NULL
;
2999 struct implicit_component_args
*comp
= NULL
;
3003 len
= auto_disc
->results
->len
;
3005 for (i
= 0; i
< len
; i
++) {
3006 struct auto_source_discovery_result
*res
=
3007 g_ptr_array_index(auto_disc
->results
, i
);
3008 uint64_t orig_indices_i
, orig_indices_count
;
3011 cc_name
= g_strdup_printf("source.%s.%s", res
->plugin_name
, res
->source_cc_name
);
3013 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3017 comp
= create_implicit_component_args(cc_name
);
3023 * Append parameters and log levels of all the
3024 * non-option arguments that contributed to this
3025 * component instance coming into existence.
3027 orig_indices_count
= bt_value_array_get_length(res
->original_input_indices
);
3028 for (orig_indices_i
= 0; orig_indices_i
< orig_indices_count
; orig_indices_i
++) {
3029 const bt_value
*orig_idx_value
=
3030 bt_value_array_borrow_element_by_index(
3031 res
->original_input_indices
, orig_indices_i
);
3032 uint64_t orig_idx
= bt_value_integer_unsigned_get(orig_idx_value
);
3033 const bt_value
*params_array
=
3034 bt_value_array_borrow_element_by_index_const(
3035 non_opt_params
, orig_idx
);
3036 uint64_t params_i
, params_count
;
3037 const bt_value
*loglevel_value
;
3039 params_count
= bt_value_array_get_length(params_array
);
3040 for (params_i
= 0; params_i
< params_count
; params_i
++) {
3041 const bt_value
*params_value
=
3042 bt_value_array_borrow_element_by_index_const(
3043 params_array
, params_i
);
3044 const char *params
= bt_value_string_get(params_value
);
3045 bt_value_array_append_element_status append_status
;
3047 append_status
= bt_value_array_append_string_element(
3048 comp
->extra_params
, "--params");
3049 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3050 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3054 append_status
= bt_value_array_append_string_element(
3055 comp
->extra_params
, params
);
3056 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3057 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3062 loglevel_value
= bt_value_array_borrow_element_by_index_const(
3063 non_opt_loglevels
, orig_idx
);
3064 if (bt_value_get_type(loglevel_value
) == BT_VALUE_TYPE_STRING
) {
3065 const char *loglevel
= bt_value_string_get(loglevel_value
);
3066 bt_value_array_append_element_status append_status
;
3068 append_status
= bt_value_array_append_string_element(
3069 comp
->extra_params
, "--log-level");
3070 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3071 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3075 append_status
= bt_value_array_append_string_element(
3076 comp
->extra_params
, loglevel
);
3077 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3078 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3085 * If single input and a src.ctf.fs component, provide the
3086 * relative path from the path passed on the command line to the
3089 if (bt_value_array_get_length(res
->inputs
) == 1 &&
3090 strcmp(res
->plugin_name
, "ctf") == 0 &&
3091 strcmp(res
->source_cc_name
, "fs") == 0) {
3092 const bt_value
*orig_idx_value
=
3093 bt_value_array_borrow_element_by_index(
3094 res
->original_input_indices
, 0);
3095 uint64_t orig_idx
= bt_value_integer_unsigned_get(orig_idx_value
);
3096 const bt_value
*non_opt_value
=
3097 bt_value_array_borrow_element_by_index_const(
3098 non_opts
, orig_idx
);
3099 const char *non_opt
= bt_value_string_get(non_opt_value
);
3100 const bt_value
*input_value
=
3101 bt_value_array_borrow_element_by_index_const(
3103 const char *input
= bt_value_string_get(input_value
);
3105 BT_ASSERT(orig_indices_count
== 1);
3106 BT_ASSERT(g_str_has_prefix(input
, non_opt
));
3108 input
+= strlen(non_opt
);
3110 while (G_IS_DIR_SEPARATOR(*input
)) {
3114 if (strlen(input
) > 0) {
3115 append_string_parameter_to_args(comp
->extra_params
,
3116 "trace-name", input
);
3120 status
= append_parameter_to_args(comp
->extra_params
, "inputs", res
->inputs
);
3125 g_ptr_array_add(component_args
, comp
);
3139 destroy_implicit_component_args(comp
);
3146 * As we iterate the arguments to the convert command, this tracks what is the
3147 * type of the current item, to which some contextual options (e.g. --params)
3150 enum convert_current_item_type
{
3151 /* There is no current item. */
3152 CONVERT_CURRENT_ITEM_TYPE_NONE
,
3154 /* Current item is a component. */
3155 CONVERT_CURRENT_ITEM_TYPE_COMPONENT
,
3157 /* Current item is a non-option argument. */
3158 CONVERT_CURRENT_ITEM_TYPE_NON_OPT
,
3162 * Creates a Babeltrace config object from the arguments of a convert
3165 * *retcode is set to the appropriate exit code to use.
3168 struct bt_config
*bt_config_convert_from_args(int argc
, const char *argv
[],
3169 int *retcode
, const bt_value
*plugin_paths
,
3170 int *default_log_level
, const bt_interrupter
*interrupter
)
3172 enum convert_current_item_type current_item_type
=
3173 CONVERT_CURRENT_ITEM_TYPE_NONE
;
3175 struct bt_config
*cfg
= NULL
;
3176 bool got_input_format_opt
= false;
3177 bool got_output_format_opt
= false;
3178 bool trimmer_has_begin
= false;
3179 bool trimmer_has_end
= false;
3180 bool stream_intersection_mode
= false;
3181 bool print_run_args
= false;
3182 bool print_run_args_0
= false;
3183 bool print_ctf_metadata
= false;
3184 bt_value
*run_args
= NULL
;
3185 bt_value
*all_names
= NULL
;
3186 GList
*source_names
= NULL
;
3187 GList
*filter_names
= NULL
;
3188 GList
*sink_names
= NULL
;
3189 bt_value
*non_opts
= NULL
;
3190 bt_value
*non_opt_params
= NULL
;
3191 bt_value
*non_opt_loglevels
= NULL
;
3192 struct implicit_component_args implicit_ctf_output_args
= { 0 };
3193 struct implicit_component_args implicit_lttng_live_args
= { 0 };
3194 struct implicit_component_args implicit_dummy_args
= { 0 };
3195 struct implicit_component_args implicit_text_args
= { 0 };
3196 struct implicit_component_args implicit_debug_info_args
= { 0 };
3197 struct implicit_component_args implicit_muxer_args
= { 0 };
3198 struct implicit_component_args implicit_trimmer_args
= { 0 };
3199 char error_buf
[256] = { 0 };
3201 struct bt_common_lttng_live_url_parts lttng_live_url_parts
= { 0 };
3202 char *output
= NULL
;
3203 struct auto_source_discovery auto_disc
= { NULL
};
3204 GString
*auto_disc_comp_name
= NULL
;
3205 struct argpar_parse_ret argpar_parse_ret
= { 0 };
3206 GString
*name_gstr
= NULL
;
3207 GString
*component_arg_for_run
= NULL
;
3208 bt_value
*live_inputs_array_val
= NULL
;
3211 * Array of `struct implicit_component_args *` created for the sources
3212 * we have auto-discovered.
3214 GPtrArray
*discovered_source_args
= NULL
;
3217 * If set, restrict automatic source discovery to this component class
3220 const char *auto_source_discovery_restrict_plugin_name
= NULL
;
3221 const char *auto_source_discovery_restrict_component_class_name
= NULL
;
3223 bool ctf_fs_source_force_clock_class_unix_epoch_origin
= false;
3224 gchar
*ctf_fs_source_clock_class_offset_arg
= NULL
;
3225 gchar
*ctf_fs_source_clock_class_offset_ns_arg
= NULL
;
3229 print_convert_usage(stdout
);
3234 if (init_implicit_component_args(&implicit_ctf_output_args
,
3235 "sink.ctf.fs", false)) {
3239 if (init_implicit_component_args(&implicit_lttng_live_args
,
3240 "source.ctf.lttng-live", false)) {
3244 if (init_implicit_component_args(&implicit_text_args
,
3245 "sink.text.pretty", false)) {
3249 if (init_implicit_component_args(&implicit_dummy_args
,
3250 "sink.utils.dummy", false)) {
3254 if (init_implicit_component_args(&implicit_debug_info_args
,
3255 "filter.lttng-utils.debug-info", false)) {
3259 if (init_implicit_component_args(&implicit_muxer_args
,
3260 "filter.utils.muxer", true)) {
3264 if (init_implicit_component_args(&implicit_trimmer_args
,
3265 "filter.utils.trimmer", false)) {
3269 all_names
= bt_value_map_create();
3271 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3275 run_args
= bt_value_array_create();
3277 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3281 component_arg_for_run
= g_string_new(NULL
);
3282 if (!component_arg_for_run
) {
3283 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3287 non_opts
= bt_value_array_create();
3289 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3293 non_opt_params
= bt_value_array_create();
3294 if (!non_opt_params
) {
3295 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3299 non_opt_loglevels
= bt_value_array_create();
3300 if (!non_opt_loglevels
) {
3301 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3305 if (auto_source_discovery_init(&auto_disc
) != 0) {
3309 discovered_source_args
=
3310 g_ptr_array_new_with_free_func((GDestroyNotify
) destroy_implicit_component_args
);
3311 if (!discovered_source_args
) {
3312 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3316 auto_disc_comp_name
= g_string_new(NULL
);
3317 if (!auto_disc_comp_name
) {
3318 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3323 * First pass: collect all arguments which need to be passed
3324 * as is to the run command. This pass can also add --name
3325 * arguments if needed to automatically name unnamed component
3328 argpar_parse_ret
= argpar_parse(argc
, argv
, convert_options
, true);
3329 if (argpar_parse_ret
.error
) {
3330 BT_CLI_LOGE_APPEND_CAUSE(
3331 "While parsing `convert` command's command-line arguments: %s",
3332 argpar_parse_ret
.error
);
3336 if (help_option_is_specified(&argpar_parse_ret
)) {
3337 print_convert_usage(stdout
);
3339 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
3343 for (i
= 0; i
< argpar_parse_ret
.items
->n_items
; i
++) {
3344 struct argpar_item
*argpar_item
=
3345 argpar_parse_ret
.items
->items
[i
];
3346 struct argpar_item_opt
*argpar_item_opt
;
3348 char *plugin_name
= NULL
;
3349 char *comp_cls_name
= NULL
;
3352 if (argpar_item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
3353 argpar_item_opt
= (struct argpar_item_opt
*) argpar_item
;
3354 arg
= argpar_item_opt
->arg
;
3356 switch (argpar_item_opt
->descr
->id
) {
3359 bt_component_class_type type
;
3361 current_item_type
= CONVERT_CURRENT_ITEM_TYPE_COMPONENT
;
3363 /* Parse the argument */
3364 plugin_comp_cls_names(arg
, &name
, &plugin_name
,
3365 &comp_cls_name
, &type
);
3366 if (!plugin_name
|| !comp_cls_name
) {
3367 BT_CLI_LOGE_APPEND_CAUSE(
3368 "Invalid format for --component option's argument:\n %s",
3375 * Name was given by the user, verify it isn't
3378 if (bt_value_map_has_entry(all_names
, name
)) {
3379 BT_CLI_LOGE_APPEND_CAUSE(
3380 "Duplicate component instance name:\n %s",
3385 name_gstr
= g_string_new(name
);
3387 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3391 g_string_assign(component_arg_for_run
, arg
);
3393 /* Name not given by user, generate one. */
3394 name_gstr
= get_component_auto_name(arg
, all_names
);
3399 g_string_printf(component_arg_for_run
, "%s:%s",
3400 name_gstr
->str
, arg
);
3403 if (bt_value_array_append_string_element(run_args
,
3405 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3409 if (bt_value_array_append_string_element(run_args
,
3410 component_arg_for_run
->str
)) {
3411 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3416 * Remember this name globally, for the uniqueness of
3417 * all component names.
3419 if (bt_value_map_insert_entry(all_names
,
3420 name_gstr
->str
, bt_value_null
)) {
3421 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3426 * Remember this name specifically for the type of the
3427 * component. This is to create connection arguments.
3429 * The list takes ownership of `name_gstr`.
3432 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
3433 source_names
= g_list_append(source_names
, name_gstr
);
3435 case BT_COMPONENT_CLASS_TYPE_FILTER
:
3436 filter_names
= g_list_append(filter_names
, name_gstr
);
3438 case BT_COMPONENT_CLASS_TYPE_SINK
:
3439 sink_names
= g_list_append(sink_names
, name_gstr
);
3448 free(comp_cls_name
);
3451 comp_cls_name
= NULL
;
3455 if (current_item_type
== CONVERT_CURRENT_ITEM_TYPE_COMPONENT
) {
3457 * The current item is a component (--component option),
3458 * pass it directly to the run args.
3460 if (bt_value_array_append_string_element(run_args
,
3462 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3466 if (bt_value_array_append_string_element(run_args
, arg
)) {
3467 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3470 } else if (current_item_type
== CONVERT_CURRENT_ITEM_TYPE_NON_OPT
) {
3472 * The current item is a
3473 * non-option argument, record
3474 * it in `non_opt_params`.
3477 bt_value_array_append_element_status append_element_status
;
3478 uint64_t idx
= bt_value_array_get_length(non_opt_params
) - 1;
3480 array
= bt_value_array_borrow_element_by_index(non_opt_params
, idx
);
3482 append_element_status
= bt_value_array_append_string_element(array
, arg
);
3483 if (append_element_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3484 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3488 BT_CLI_LOGE_APPEND_CAUSE(
3489 "No current component (--component option) or non-option argument of which to set parameters:\n %s",
3495 if (current_item_type
== CONVERT_CURRENT_ITEM_TYPE_COMPONENT
) {
3496 if (bt_value_array_append_string_element(run_args
, "--log-level")) {
3497 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3501 if (bt_value_array_append_string_element(run_args
, arg
)) {
3502 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3505 } else if (current_item_type
== CONVERT_CURRENT_ITEM_TYPE_NON_OPT
) {
3506 uint64_t idx
= bt_value_array_get_length(non_opt_loglevels
) - 1;
3507 enum bt_value_array_set_element_by_index_status set_element_status
;
3508 bt_value
*log_level_str_value
;
3510 log_level_str_value
= bt_value_string_create_init(arg
);
3511 if (!log_level_str_value
) {
3512 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3516 set_element_status
=
3517 bt_value_array_set_element_by_index(non_opt_loglevels
,
3518 idx
, log_level_str_value
);
3519 bt_value_put_ref(log_level_str_value
);
3520 if (set_element_status
!= BT_VALUE_ARRAY_SET_ELEMENT_BY_INDEX_STATUS_OK
) {
3521 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3525 BT_CLI_LOGE_APPEND_CAUSE(
3526 "No current component (--component option) or non-option argument to assign a log level to:\n %s",
3532 case OPT_RETRY_DURATION
:
3533 if (bt_value_array_append_string_element(run_args
,
3534 "--retry-duration")) {
3535 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3539 if (bt_value_array_append_string_element(run_args
, arg
)) {
3540 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3545 case OPT_CLOCK_CYCLES
:
3546 case OPT_CLOCK_DATE
:
3547 case OPT_CLOCK_FORCE_CORRELATE
:
3549 case OPT_CLOCK_OFFSET
:
3550 case OPT_CLOCK_OFFSET_NS
:
3551 case OPT_CLOCK_SECONDS
:
3554 case OPT_DEBUG_INFO
:
3555 case OPT_DEBUG_INFO_DIR
:
3556 case OPT_DEBUG_INFO_FULL_PATH
:
3557 case OPT_DEBUG_INFO_TARGET_PREFIX
:
3560 case OPT_INPUT_FORMAT
:
3563 case OPT_OUTPUT_FORMAT
:
3566 case OPT_RUN_ARGS_0
:
3567 case OPT_STREAM_INTERSECTION
:
3570 /* Ignore in this pass */
3573 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
3574 argpar_item_opt
->descr
->id
);
3577 } else if (argpar_item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
) {
3578 struct argpar_item_non_opt
*argpar_item_non_opt
;
3579 bt_value_array_append_element_status append_status
;
3581 current_item_type
= CONVERT_CURRENT_ITEM_TYPE_NON_OPT
;
3583 argpar_item_non_opt
= (struct argpar_item_non_opt
*) argpar_item
;
3585 append_status
= bt_value_array_append_string_element(non_opts
,
3586 argpar_item_non_opt
->arg
);
3587 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3588 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3592 append_status
= bt_value_array_append_empty_array_element(
3593 non_opt_params
, NULL
);
3594 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3595 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3599 append_status
= bt_value_array_append_element(non_opt_loglevels
, bt_value_null
);
3600 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3601 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3610 * Second pass: transform the convert-specific options and
3611 * arguments into implicit component instances for the run
3614 for (i
= 0; i
< argpar_parse_ret
.items
->n_items
; i
++) {
3615 struct argpar_item
*argpar_item
=
3616 argpar_parse_ret
.items
->items
[i
];
3617 struct argpar_item_opt
*argpar_item_opt
;
3620 if (argpar_item
->type
!= ARGPAR_ITEM_TYPE_OPT
) {
3624 argpar_item_opt
= (struct argpar_item_opt
*) argpar_item
;
3625 arg
= argpar_item_opt
->arg
;
3627 switch (argpar_item_opt
->descr
->id
) {
3629 if (trimmer_has_begin
) {
3630 BT_CLI_LOGE_APPEND_CAUSE("At --begin option: --begin or --timerange option already specified\n %s\n",
3635 trimmer_has_begin
= true;
3636 ret
= append_implicit_component_extra_param(
3637 &implicit_trimmer_args
, "begin", arg
);
3638 implicit_trimmer_args
.exists
= true;
3644 if (trimmer_has_end
) {
3645 BT_CLI_LOGE_APPEND_CAUSE("At --end option: --end or --timerange option already specified\n %s\n",
3650 trimmer_has_end
= true;
3651 ret
= append_implicit_component_extra_param(
3652 &implicit_trimmer_args
, "end", arg
);
3653 implicit_trimmer_args
.exists
= true;
3663 if (trimmer_has_begin
|| trimmer_has_end
) {
3664 BT_CLI_LOGE_APPEND_CAUSE("At --timerange option: --begin, --end, or --timerange option already specified\n %s\n",
3669 ret
= split_timerange(arg
, &begin
, &end
);
3671 BT_CLI_LOGE_APPEND_CAUSE("Invalid --timerange option's argument: expecting BEGIN,END or [BEGIN,END]:\n %s",
3676 ret
= append_implicit_component_extra_param(
3677 &implicit_trimmer_args
, "begin", begin
);
3678 ret
|= append_implicit_component_extra_param(
3679 &implicit_trimmer_args
, "end", end
);
3680 implicit_trimmer_args
.exists
= true;
3688 case OPT_CLOCK_CYCLES
:
3689 append_implicit_component_param(
3690 &implicit_text_args
, "clock-cycles", "yes");
3691 implicit_text_args
.exists
= true;
3693 case OPT_CLOCK_DATE
:
3694 append_implicit_component_param(
3695 &implicit_text_args
, "clock-date", "yes");
3696 implicit_text_args
.exists
= true;
3698 case OPT_CLOCK_FORCE_CORRELATE
:
3699 ctf_fs_source_force_clock_class_unix_epoch_origin
= true;
3702 append_implicit_component_param(
3703 &implicit_text_args
, "clock-gmt", "yes");
3704 append_implicit_component_param(
3705 &implicit_trimmer_args
, "gmt", "yes");
3706 implicit_text_args
.exists
= true;
3708 case OPT_CLOCK_OFFSET
:
3709 if (ctf_fs_source_clock_class_offset_arg
) {
3710 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --clock-offset option\n");
3714 ctf_fs_source_clock_class_offset_arg
= g_strdup(arg
);
3715 if (!ctf_fs_source_clock_class_offset_arg
) {
3716 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3720 case OPT_CLOCK_OFFSET_NS
:
3721 if (ctf_fs_source_clock_class_offset_ns_arg
) {
3722 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --clock-offset-ns option\n");
3726 ctf_fs_source_clock_class_offset_ns_arg
= g_strdup(arg
);
3727 if (!ctf_fs_source_clock_class_offset_ns_arg
) {
3728 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3732 case OPT_CLOCK_SECONDS
:
3733 append_implicit_component_param(
3734 &implicit_text_args
, "clock-seconds", "yes");
3735 implicit_text_args
.exists
= true;
3738 implicit_text_args
.exists
= true;
3739 ret
= append_implicit_component_extra_param(
3740 &implicit_text_args
, "color", arg
);
3745 case OPT_DEBUG_INFO
:
3746 implicit_debug_info_args
.exists
= true;
3748 case OPT_DEBUG_INFO_DIR
:
3749 implicit_debug_info_args
.exists
= true;
3750 ret
= append_implicit_component_extra_param(
3751 &implicit_debug_info_args
, "debug-info-dir", arg
);
3756 case OPT_DEBUG_INFO_FULL_PATH
:
3757 implicit_debug_info_args
.exists
= true;
3758 append_implicit_component_param(
3759 &implicit_debug_info_args
, "full-path", "yes");
3761 case OPT_DEBUG_INFO_TARGET_PREFIX
:
3762 implicit_debug_info_args
.exists
= true;
3763 ret
= append_implicit_component_extra_param(
3764 &implicit_debug_info_args
,
3765 "target-prefix", arg
);
3772 bt_value
*fields
= fields_from_arg(arg
);
3778 implicit_text_args
.exists
= true;
3779 ret
= insert_flat_params_from_array(
3780 implicit_text_args
.params_arg
,
3782 bt_value_put_ref(fields
);
3790 bt_value
*names
= names_from_arg(arg
);
3796 implicit_text_args
.exists
= true;
3797 ret
= insert_flat_params_from_array(
3798 implicit_text_args
.params_arg
,
3800 bt_value_put_ref(names
);
3807 append_implicit_component_param(
3808 &implicit_text_args
, "no-delta", "yes");
3809 implicit_text_args
.exists
= true;
3811 case OPT_INPUT_FORMAT
:
3812 if (got_input_format_opt
) {
3813 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --input-format option.");
3817 got_input_format_opt
= true;
3819 if (strcmp(arg
, "ctf") == 0) {
3820 auto_source_discovery_restrict_plugin_name
= "ctf";
3821 auto_source_discovery_restrict_component_class_name
= "fs";
3822 } else if (strcmp(arg
, "lttng-live") == 0) {
3823 auto_source_discovery_restrict_plugin_name
= "ctf";
3824 auto_source_discovery_restrict_component_class_name
= "lttng-live";
3825 implicit_lttng_live_args
.exists
= true;
3827 BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy input format:\n %s",
3832 case OPT_OUTPUT_FORMAT
:
3833 if (got_output_format_opt
) {
3834 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output-format option.");
3838 got_output_format_opt
= true;
3840 if (strcmp(arg
, "text") == 0) {
3841 implicit_text_args
.exists
= true;
3842 } else if (strcmp(arg
, "ctf") == 0) {
3843 implicit_ctf_output_args
.exists
= true;
3844 } else if (strcmp(arg
, "dummy") == 0) {
3845 implicit_dummy_args
.exists
= true;
3846 } else if (strcmp(arg
, "ctf-metadata") == 0) {
3847 print_ctf_metadata
= true;
3849 BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy output format:\n %s",
3856 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output option");
3860 output
= strdup(arg
);
3862 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3867 if (print_run_args_0
) {
3868 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0.");
3872 print_run_args
= true;
3874 case OPT_RUN_ARGS_0
:
3875 if (print_run_args
) {
3876 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0.");
3880 print_run_args_0
= true;
3882 case OPT_STREAM_INTERSECTION
:
3884 * Applies to all traces implementing the
3885 * babeltrace.trace-infos query.
3887 stream_intersection_mode
= true;
3890 *default_log_level
=
3891 logging_level_min(*default_log_level
, BT_LOG_INFO
);
3894 *default_log_level
=
3895 logging_level_min(*default_log_level
, BT_LOG_TRACE
);
3902 set_auto_log_levels(default_log_level
);
3905 * Legacy behaviour: --verbose used to make the `text` output
3906 * format print more information. --verbose is now equivalent to
3907 * the INFO log level, which is why we compare to `BT_LOG_INFO`
3910 if (*default_log_level
== BT_LOG_INFO
) {
3911 append_implicit_component_param(&implicit_text_args
,
3915 /* Print CTF metadata or print LTTng live sessions */
3916 if (print_ctf_metadata
) {
3917 const bt_value
*bt_val_non_opt
;
3919 if (bt_value_array_is_empty(non_opts
)) {
3920 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf-metadata specified without a path.");
3924 if (bt_value_array_get_length(non_opts
) > 1) {
3925 BT_CLI_LOGE_APPEND_CAUSE("Too many paths specified for --output-format=ctf-metadata.");
3929 cfg
= bt_config_print_ctf_metadata_create(plugin_paths
);
3934 bt_val_non_opt
= bt_value_array_borrow_element_by_index_const(non_opts
, 0);
3935 g_string_assign(cfg
->cmd_data
.print_ctf_metadata
.path
,
3936 bt_value_string_get(bt_val_non_opt
));
3940 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
3948 * If -o ctf was specified, make sure an output path (--output)
3949 * was also specified. --output does not imply -o ctf because
3950 * it's also used for the default, implicit -o text if -o ctf
3953 if (implicit_ctf_output_args
.exists
) {
3955 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf specified without --output (trace output path).");
3960 * At this point we know that -o ctf AND --output were
3961 * specified. Make sure that no options were specified
3962 * which would imply -o text because --output would be
3963 * ambiguous in this case. For example, this is wrong:
3965 * babeltrace2 --names=all -o ctf --output=/tmp/path my-trace
3967 * because --names=all implies -o text, and --output
3968 * could apply to both the sink.text.pretty and
3969 * sink.ctf.fs implicit components.
3971 if (implicit_text_args
.exists
) {
3972 BT_CLI_LOGE_APPEND_CAUSE("Ambiguous --output option: --output-format=ctf specified but another option implies --output-format=text.");
3978 * If -o dummy and -o ctf were not specified, and if there are
3979 * no explicit sink components, then use an implicit
3980 * `sink.text.pretty` component.
3982 if (!implicit_dummy_args
.exists
&& !implicit_ctf_output_args
.exists
&&
3984 implicit_text_args
.exists
= true;
3988 * Set implicit `sink.text.pretty` or `sink.ctf.fs` component's
3989 * `path` parameter if --output was specified.
3992 if (implicit_text_args
.exists
) {
3993 append_implicit_component_extra_param(&implicit_text_args
,
3995 } else if (implicit_ctf_output_args
.exists
) {
3996 append_implicit_component_extra_param(&implicit_ctf_output_args
,
4001 /* Decide where the non-option argument(s) go */
4002 if (bt_value_array_get_length(non_opts
) > 0) {
4003 if (implicit_lttng_live_args
.exists
) {
4004 const bt_value
*bt_val_non_opt
;
4006 if (bt_value_array_get_length(non_opts
) > 1) {
4007 BT_CLI_LOGE_APPEND_CAUSE("Too many URLs specified for --input-format=lttng-live.");
4011 bt_val_non_opt
= bt_value_array_borrow_element_by_index_const(non_opts
, 0);
4012 lttng_live_url_parts
=
4013 bt_common_parse_lttng_live_url(bt_value_string_get(bt_val_non_opt
),
4014 error_buf
, sizeof(error_buf
));
4015 if (!lttng_live_url_parts
.proto
) {
4016 BT_CLI_LOGE_APPEND_CAUSE("Invalid LTTng live URL format: %s.",
4021 if (!lttng_live_url_parts
.session_name
) {
4022 /* Print LTTng live sessions */
4023 cfg
= bt_config_print_lttng_live_sessions_create(
4029 g_string_assign(cfg
->cmd_data
.print_lttng_live_sessions
.url
,
4030 bt_value_string_get(bt_val_non_opt
));
4034 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
4041 live_inputs_array_val
= bt_value_array_create();
4042 if (!live_inputs_array_val
) {
4043 BT_CLI_LOGE_APPEND_CAUSE_OOM();
4047 if (bt_value_array_append_string_element(
4048 live_inputs_array_val
,
4049 bt_value_string_get(bt_val_non_opt
))) {
4050 BT_CLI_LOGE_APPEND_CAUSE_OOM();
4054 ret
= append_parameter_to_args(
4055 implicit_lttng_live_args
.extra_params
,
4056 "inputs", live_inputs_array_val
);
4061 ret
= append_implicit_component_extra_param(
4062 &implicit_lttng_live_args
,
4063 "session-not-found-action", "end");
4069 size_t plugin_count
;
4070 const bt_plugin
**plugins
;
4071 const bt_plugin
*plugin
;
4073 status
= require_loaded_plugins(plugin_paths
);
4078 if (auto_source_discovery_restrict_plugin_name
) {
4080 plugin
= borrow_loaded_plugin_by_name(auto_source_discovery_restrict_plugin_name
);
4083 plugin_count
= get_loaded_plugins_count();
4084 plugins
= borrow_loaded_plugins();
4087 status
= auto_discover_source_components(non_opts
, plugins
, plugin_count
,
4088 auto_source_discovery_restrict_component_class_name
,
4089 *default_log_level
, &auto_disc
, interrupter
);
4092 if (status
== AUTO_SOURCE_DISCOVERY_STATUS_INTERRUPTED
) {
4093 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
4094 "Babeltrace CLI", "Automatic source discovery interrupted by the user");
4099 status
= create_implicit_component_args_from_auto_discovered_sources(
4100 &auto_disc
, non_opts
, non_opt_params
, non_opt_loglevels
,
4101 discovered_source_args
);
4110 * If --clock-force-correlated was given, apply it to any src.ctf.fs
4113 if (ctf_fs_source_force_clock_class_unix_epoch_origin
) {
4116 n
= append_multiple_implicit_components_param(
4117 discovered_source_args
, "source.ctf.fs", "force-clock-class-origin-unix-epoch",
4120 BT_CLI_LOGE_APPEND_CAUSE("--clock-force-correlate specified, but no source.ctf.fs component instantiated.");
4125 /* If --clock-offset was given, apply it to any src.ctf.fs component. */
4126 if (ctf_fs_source_clock_class_offset_arg
) {
4129 n
= append_multiple_implicit_components_param(
4130 discovered_source_args
, "source.ctf.fs", "clock-class-offset-s",
4131 ctf_fs_source_clock_class_offset_arg
);
4134 BT_CLI_LOGE_APPEND_CAUSE("--clock-offset specified, but no source.ctf.fs component instantiated.");
4139 /* If --clock-offset-ns was given, apply it to any src.ctf.fs component. */
4140 if (ctf_fs_source_clock_class_offset_ns_arg
) {
4143 n
= append_multiple_implicit_components_param(
4144 discovered_source_args
, "source.ctf.fs", "clock-class-offset-ns",
4145 ctf_fs_source_clock_class_offset_ns_arg
);
4148 BT_CLI_LOGE_APPEND_CAUSE("--clock-offset-ns specified, but no source.ctf.fs component instantiated.");
4154 * If the implicit `source.ctf.lttng-live` component exists,
4155 * make sure there's at least one non-option argument (which is
4158 if (implicit_lttng_live_args
.exists
&& bt_value_array_is_empty(non_opts
)) {
4159 BT_CLI_LOGE_APPEND_CAUSE("Missing URL for implicit `%s` component.",
4160 implicit_lttng_live_args
.comp_arg
->str
);
4164 /* Assign names to implicit components */
4165 for (i
= 0; i
< discovered_source_args
->len
; i
++) {
4166 struct implicit_component_args
*args
;
4169 args
= discovered_source_args
->pdata
[i
];
4171 g_string_printf(auto_disc_comp_name
, "auto-disc-%s", args
->comp_arg
->str
);
4173 /* Give it a name like `auto-disc-src-ctf-fs`. */
4174 for (j
= 0; j
< auto_disc_comp_name
->len
; j
++) {
4175 if (auto_disc_comp_name
->str
[j
] == '.') {
4176 auto_disc_comp_name
->str
[j
] = '-';
4180 ret
= assign_name_to_implicit_component(args
,
4181 auto_disc_comp_name
->str
, all_names
, &source_names
, true);
4187 ret
= assign_name_to_implicit_component(&implicit_lttng_live_args
,
4188 "lttng-live", all_names
, &source_names
, true);
4193 ret
= assign_name_to_implicit_component(&implicit_text_args
,
4194 "pretty", all_names
, &sink_names
, true);
4199 ret
= assign_name_to_implicit_component(&implicit_ctf_output_args
,
4200 "sink-ctf-fs", all_names
, &sink_names
, true);
4205 ret
= assign_name_to_implicit_component(&implicit_dummy_args
,
4206 "dummy", all_names
, &sink_names
, true);
4211 ret
= assign_name_to_implicit_component(&implicit_muxer_args
,
4212 "muxer", all_names
, NULL
, false);
4217 ret
= assign_name_to_implicit_component(&implicit_trimmer_args
,
4218 "trimmer", all_names
, NULL
, false);
4223 ret
= assign_name_to_implicit_component(&implicit_debug_info_args
,
4224 "debug-info", all_names
, NULL
, false);
4229 /* Make sure there's at least one source and one sink */
4230 if (!source_names
) {
4231 BT_CLI_LOGE_APPEND_CAUSE("No source component.");
4236 BT_CLI_LOGE_APPEND_CAUSE("No sink component.");
4240 /* Make sure there's a single sink component */
4241 if (g_list_length(sink_names
) != 1) {
4242 BT_CLI_LOGE_APPEND_CAUSE(
4243 "More than one sink component specified.");
4248 * Prepend the muxer, the trimmer, and the debug info to the
4249 * filter chain so that we have:
4251 * sources -> muxer -> [trimmer] -> [debug info] ->
4252 * [user filters] -> sinks
4254 if (implicit_debug_info_args
.exists
) {
4255 if (g_list_prepend_gstring(&filter_names
,
4256 implicit_debug_info_args
.name_arg
->str
)) {
4261 if (implicit_trimmer_args
.exists
) {
4262 if (g_list_prepend_gstring(&filter_names
,
4263 implicit_trimmer_args
.name_arg
->str
)) {
4268 if (g_list_prepend_gstring(&filter_names
,
4269 implicit_muxer_args
.name_arg
->str
)) {
4274 * Append the equivalent run arguments for the implicit
4277 for (i
= 0; i
< discovered_source_args
->len
; i
++) {
4278 struct implicit_component_args
*args
=
4279 discovered_source_args
->pdata
[i
];
4281 ret
= append_run_args_for_implicit_component(args
, run_args
);
4287 ret
= append_run_args_for_implicit_component(&implicit_lttng_live_args
,
4293 ret
= append_run_args_for_implicit_component(&implicit_text_args
,
4299 ret
= append_run_args_for_implicit_component(&implicit_ctf_output_args
,
4305 ret
= append_run_args_for_implicit_component(&implicit_dummy_args
,
4311 ret
= append_run_args_for_implicit_component(&implicit_muxer_args
,
4317 ret
= append_run_args_for_implicit_component(&implicit_trimmer_args
,
4323 ret
= append_run_args_for_implicit_component(&implicit_debug_info_args
,
4329 /* Auto-connect components */
4330 ret
= convert_auto_connect(run_args
, source_names
, filter_names
,
4333 BT_CLI_LOGE_APPEND_CAUSE("Cannot auto-connect components.");
4338 * We have all the run command arguments now. Depending on
4339 * --run-args, we pass this to the run command or print them
4342 if (print_run_args
|| print_run_args_0
) {
4343 uint64_t args_idx
, args_len
;
4344 if (stream_intersection_mode
) {
4345 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --stream-intersection with --run-args or --run-args-0.");
4349 args_len
= bt_value_array_get_length(run_args
);
4350 for (args_idx
= 0; args_idx
< args_len
; args_idx
++) {
4351 const bt_value
*arg_value
=
4352 bt_value_array_borrow_element_by_index(run_args
,
4355 GString
*quoted
= NULL
;
4356 const char *arg_to_print
;
4358 arg
= bt_value_string_get(arg_value
);
4360 if (print_run_args
) {
4361 quoted
= bt_common_shell_quote(arg
, true);
4366 arg_to_print
= quoted
->str
;
4371 printf("%s", arg_to_print
);
4374 g_string_free(quoted
, TRUE
);
4377 if (args_idx
< args_len
- 1) {
4378 if (print_run_args
) {
4387 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4391 cfg
= bt_config_run_from_args_array(run_args
, retcode
,
4392 plugin_paths
, *default_log_level
);
4397 cfg
->cmd_data
.run
.stream_intersection_mode
= stream_intersection_mode
;
4402 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4405 argpar_parse_ret_fini(&argpar_parse_ret
);
4409 if (component_arg_for_run
) {
4410 g_string_free(component_arg_for_run
, TRUE
);
4414 g_string_free(name_gstr
, TRUE
);
4417 bt_value_put_ref(live_inputs_array_val
);
4418 bt_value_put_ref(run_args
);
4419 bt_value_put_ref(all_names
);
4420 destroy_glist_of_gstring(source_names
);
4421 destroy_glist_of_gstring(filter_names
);
4422 destroy_glist_of_gstring(sink_names
);
4423 bt_value_put_ref(non_opt_params
);
4424 bt_value_put_ref(non_opt_loglevels
);
4425 bt_value_put_ref(non_opts
);
4426 finalize_implicit_component_args(&implicit_ctf_output_args
);
4427 finalize_implicit_component_args(&implicit_lttng_live_args
);
4428 finalize_implicit_component_args(&implicit_dummy_args
);
4429 finalize_implicit_component_args(&implicit_text_args
);
4430 finalize_implicit_component_args(&implicit_debug_info_args
);
4431 finalize_implicit_component_args(&implicit_muxer_args
);
4432 finalize_implicit_component_args(&implicit_trimmer_args
);
4433 bt_common_destroy_lttng_live_url_parts(<tng_live_url_parts
);
4434 auto_source_discovery_fini(&auto_disc
);
4436 if (discovered_source_args
) {
4437 g_ptr_array_free(discovered_source_args
, TRUE
);
4440 g_free(ctf_fs_source_clock_class_offset_arg
);
4441 g_free(ctf_fs_source_clock_class_offset_ns_arg
);
4443 if (auto_disc_comp_name
) {
4444 g_string_free(auto_disc_comp_name
, TRUE
);
4451 * Prints the Babeltrace 2.x general usage.
4454 void print_gen_usage(FILE *fp
)
4456 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] [COMMAND] [COMMAND ARGUMENTS]\n");
4458 fprintf(fp
, "General options:\n");
4460 fprintf(fp
, " -d, --debug Enable debug mode (same as --log-level=T)\n");
4461 fprintf(fp
, " -h, --help Show this help and quit\n");
4462 fprintf(fp
, " -l, --log-level=LVL Set the default log level to LVL (`N`, `T`, `D`,\n");
4463 fprintf(fp
, " `I`, `W` (default), `E`, or `F`)\n");
4464 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
4465 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
4466 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
4467 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
4468 fprintf(fp
, " dynamic plugins can be loaded\n");
4469 fprintf(fp
, " -v, --verbose Enable verbose mode (same as --log-level=I)\n");
4470 fprintf(fp
, " -V, --version Show version and quit\n");
4472 fprintf(fp
, "Available commands:\n");
4474 fprintf(fp
, " convert Convert and trim traces (default)\n");
4475 fprintf(fp
, " help Get help for a plugin or a component class\n");
4476 fprintf(fp
, " list-plugins List available plugins and their content\n");
4477 fprintf(fp
, " query Query objects from a component class\n");
4478 fprintf(fp
, " run Build a processing graph and run it\n");
4480 fprintf(fp
, "Use `babeltrace2 COMMAND --help` to show the help of COMMAND.\n");
4483 struct bt_config
*bt_config_cli_args_create(int argc
, const char *argv
[],
4484 int *retcode
, bool omit_system_plugin_path
,
4485 bool omit_home_plugin_path
,
4486 const bt_value
*initial_plugin_paths
,
4487 const bt_interrupter
*interrupter
)
4489 struct bt_config
*config
= NULL
;
4492 const char **top_level_argv
;
4493 int command_argc
= -1;
4494 const char **command_argv
= NULL
;
4495 const char *command_name
= NULL
;
4496 int default_log_level
= -1;
4497 struct argpar_parse_ret argpar_parse_ret
= { 0 };
4498 bt_value
*plugin_paths
= NULL
;
4500 /* Top-level option descriptions. */
4501 static const struct argpar_opt_descr descrs
[] = {
4502 { OPT_DEBUG
, 'd', "debug", false },
4503 { OPT_HELP
, 'h', "help", false },
4504 { OPT_LOG_LEVEL
, 'l', "log-level", true },
4505 { OPT_VERBOSE
, 'v', "verbose", false },
4506 { OPT_VERSION
, 'V', "version", false},
4507 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
4508 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
4509 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
4510 ARGPAR_OPT_DESCR_SENTINEL
4514 COMMAND_TYPE_NONE
= -1,
4515 COMMAND_TYPE_RUN
= 0,
4516 COMMAND_TYPE_CONVERT
,
4517 COMMAND_TYPE_LIST_PLUGINS
,
4520 } command_type
= COMMAND_TYPE_NONE
;
4524 if (!initial_plugin_paths
) {
4525 plugin_paths
= bt_value_array_create();
4526 if (!plugin_paths
) {
4530 bt_value_copy_status copy_status
= bt_value_copy(
4531 initial_plugin_paths
, &plugin_paths
);
4537 BT_ASSERT(plugin_paths
);
4540 * The `BABELTRACE_PLUGIN_PATH` paths take precedence over the
4541 * `--plugin-path` option's paths, so append it now before
4542 * parsing the general options.
4544 if (append_env_var_plugin_paths(plugin_paths
)) {
4551 print_gen_usage(stdout
);
4555 /* Skip first argument, the name of the program. */
4556 top_level_argc
= argc
- 1;
4557 top_level_argv
= argv
+ 1;
4558 argpar_parse_ret
= argpar_parse(top_level_argc
, top_level_argv
,
4561 if (argpar_parse_ret
.error
) {
4562 BT_CLI_LOGE_APPEND_CAUSE(
4563 "While parsing command-line arguments: %s",
4564 argpar_parse_ret
.error
);
4568 for (i
= 0; i
< argpar_parse_ret
.items
->n_items
; i
++) {
4569 struct argpar_item
*item
;
4571 item
= argpar_parse_ret
.items
->items
[i
];
4573 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
4574 struct argpar_item_opt
*item_opt
=
4575 (struct argpar_item_opt
*) item
;
4577 switch (item_opt
->descr
->id
) {
4580 logging_level_min(default_log_level
, BT_LOG_TRACE
);
4584 logging_level_min(default_log_level
, BT_LOG_INFO
);
4588 int level
= bt_log_get_level_from_string(item_opt
->arg
);
4591 BT_CLI_LOGE_APPEND_CAUSE(
4592 "Invalid argument for --log-level option:\n %s",
4598 logging_level_min(default_log_level
, level
);
4601 case OPT_PLUGIN_PATH
:
4602 if (bt_config_append_plugin_paths_check_setuid_setgid(
4603 plugin_paths
, item_opt
->arg
)) {
4607 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
4608 omit_system_plugin_path
= true;
4610 case OPT_OMIT_HOME_PLUGIN_PATH
:
4611 omit_home_plugin_path
= true;
4617 print_gen_usage(stdout
);
4620 } else if (item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
) {
4621 struct argpar_item_non_opt
*item_non_opt
=
4622 (struct argpar_item_non_opt
*) item
;
4624 * First unknown argument: is it a known command
4628 top_level_argc
- item_non_opt
->orig_index
- 1;
4630 &top_level_argv
[item_non_opt
->orig_index
+ 1];
4632 if (strcmp(item_non_opt
->arg
, "convert") == 0) {
4633 command_type
= COMMAND_TYPE_CONVERT
;
4634 command_name
= "convert";
4635 } else if (strcmp(item_non_opt
->arg
, "list-plugins") == 0) {
4636 command_type
= COMMAND_TYPE_LIST_PLUGINS
;
4637 command_name
= "list-plugins";
4638 } else if (strcmp(item_non_opt
->arg
, "help") == 0) {
4639 command_type
= COMMAND_TYPE_HELP
;
4640 command_name
= "help";
4641 } else if (strcmp(item_non_opt
->arg
, "query") == 0) {
4642 command_type
= COMMAND_TYPE_QUERY
;
4643 command_name
= "query";
4644 } else if (strcmp(item_non_opt
->arg
, "run") == 0) {
4645 command_type
= COMMAND_TYPE_RUN
;
4646 command_name
= "run";
4649 * Non-option argument, but not a known
4650 * command name: assume the default
4651 * `convert` command.
4653 command_type
= COMMAND_TYPE_CONVERT
;
4654 command_name
= "convert";
4662 if (command_type
== COMMAND_TYPE_NONE
) {
4663 if (argpar_parse_ret
.ingested_orig_args
== top_level_argc
) {
4665 * We only got non-help, non-version general options
4666 * like --verbose and --debug, without any other
4667 * arguments, so we can't do anything useful: print the
4670 print_gen_usage(stdout
);
4675 * We stopped on an unknown option argument (and therefore
4676 * didn't see a command name). Assume `convert` command.
4678 command_type
= COMMAND_TYPE_CONVERT
;
4679 command_name
= "convert";
4681 top_level_argc
- argpar_parse_ret
.ingested_orig_args
;
4683 &top_level_argv
[argpar_parse_ret
.ingested_orig_args
];
4686 BT_ASSERT(command_argv
);
4687 BT_ASSERT(command_argc
>= 0);
4690 * For all commands other than `convert`, we now know the log level to
4691 * use, so we can apply it with `set_auto_log_levels`.
4693 * The convert command has `--debug` and `--verbose` arguments that are
4694 * equivalent to the top-level arguments of the same name. So after it
4695 * has parsed its arguments, `bt_config_convert_from_args` calls
4696 * `set_auto_log_levels` itself.
4698 if (command_type
!= COMMAND_TYPE_CONVERT
) {
4699 set_auto_log_levels(&default_log_level
);
4703 * At this point, `plugin_paths` contains the initial plugin
4704 * paths, the paths from the `BABELTRACE_PLUGIN_PATH` paths, and
4705 * the paths from the `--plugin-path` option.
4707 * Now append the user and system plugin paths.
4709 if (append_home_and_system_plugin_paths(plugin_paths
,
4710 omit_system_plugin_path
, omit_home_plugin_path
)) {
4714 switch (command_type
) {
4715 case COMMAND_TYPE_RUN
:
4716 config
= bt_config_run_from_args(command_argc
, command_argv
,
4717 retcode
, plugin_paths
,
4720 case COMMAND_TYPE_CONVERT
:
4721 config
= bt_config_convert_from_args(command_argc
, command_argv
,
4722 retcode
, plugin_paths
, &default_log_level
, interrupter
);
4724 case COMMAND_TYPE_LIST_PLUGINS
:
4725 config
= bt_config_list_plugins_from_args(command_argc
,
4726 command_argv
, retcode
, plugin_paths
);
4728 case COMMAND_TYPE_HELP
:
4729 config
= bt_config_help_from_args(command_argc
,
4730 command_argv
, retcode
, plugin_paths
,
4733 case COMMAND_TYPE_QUERY
:
4734 config
= bt_config_query_from_args(command_argc
,
4735 command_argv
, retcode
, plugin_paths
,
4743 BT_ASSERT(default_log_level
>= BT_LOG_TRACE
);
4744 config
->log_level
= default_log_level
;
4745 config
->command_name
= command_name
;
4754 argpar_parse_ret_fini(&argpar_parse_ret
);
4755 bt_value_put_ref(plugin_paths
);