2 * Babeltrace trace converter - parameter parsing
4 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #define BT_LOG_TAG "CLI/CFG-CLI-ARGS"
31 #include "common/assert.h"
35 #include <babeltrace2/babeltrace.h>
36 #include "common/common.h"
39 #include <sys/types.h>
40 #include "argpar/argpar.h"
41 #include "babeltrace2-cfg.h"
42 #include "babeltrace2-cfg-cli-args.h"
43 #include "babeltrace2-cfg-cli-args-connect.h"
44 #include "babeltrace2-cfg-cli-params-arg.h"
45 #include "babeltrace2-plugins.h"
46 #include "babeltrace2-cfg-src-auto-disc.h"
47 #include "common/version.h"
49 static const int cli_default_log_level
= BT_LOG_WARNING
;
51 /* INI-style parsing FSM states */
52 enum ini_parsing_fsm_state
{
53 /* Expect a map key (identifier) */
56 /* Expect an equal character ('=') */
62 /* Expect a comma character (',') */
66 /* INI-style parsing state variables */
67 struct ini_parsing_state
{
68 /* Lexical scanner (owned by this) */
71 /* Output map value object being filled (owned by this) */
74 /* Next expected FSM state */
75 enum ini_parsing_fsm_state expecting
;
77 /* Last decoded map key (owned by this) */
80 /* Complete INI-style string to parse (not owned by this) */
83 /* Error buffer (not owned by this) */
87 /* Offset option with "is set" boolean */
93 /* Legacy "ctf"/"lttng-live" format options */
94 struct ctf_legacy_opts
{
95 struct offset_opt offset_s
;
96 struct offset_opt offset_ns
;
97 bool stream_intersection
;
100 /* Legacy "text" format options */
101 struct text_legacy_opts
{
103 * output, dbg_info_dir, dbg_info_target_prefix, names,
104 * and fields are owned by this.
107 GString
*dbg_info_dir
;
108 GString
*dbg_info_target_prefix
;
109 const bt_value
*names
;
110 const bt_value
*fields
;
118 bool dbg_info_full_path
;
122 /* Legacy input format format */
123 enum legacy_input_format
{
124 LEGACY_INPUT_FORMAT_NONE
= 0,
125 LEGACY_INPUT_FORMAT_CTF
,
126 LEGACY_INPUT_FORMAT_LTTNG_LIVE
,
129 /* Legacy output format format */
130 enum legacy_output_format
{
131 LEGACY_OUTPUT_FORMAT_NONE
= 0,
132 LEGACY_OUTPUT_FORMAT_TEXT
,
133 LEGACY_OUTPUT_FORMAT_DUMMY
,
136 #define BT_CLI_LOGE_APPEND_CAUSE_OOM() BT_CLI_LOGE_APPEND_CAUSE("Out of memory.")
139 * Returns the plugin name, component class name, component class type,
140 * and component name from a command-line --component option's argument.
141 * arg must have the following format:
143 * [NAME:]TYPE.PLUGIN.CLS
145 * where NAME is the optional component name, TYPE is either `source`,
146 * `filter`, or `sink`, PLUGIN is the plugin name, and CLS is the
147 * component class name.
149 * On success, both *plugin and *component are not NULL. *plugin
150 * and *comp_cls are owned by the caller. On success, *name can be NULL
151 * if no component class name was found, and *comp_cls_type is set.
154 void plugin_comp_cls_names(const char *arg
, char **name
, char **plugin
,
155 char **comp_cls
, bt_component_class_type
*comp_cls_type
)
157 const char *at
= arg
;
158 GString
*gs_name
= NULL
;
159 GString
*gs_comp_cls_type
= NULL
;
160 GString
*gs_plugin
= NULL
;
161 GString
*gs_comp_cls
= NULL
;
167 BT_ASSERT(comp_cls_type
);
169 if (!bt_common_string_is_printable(arg
)) {
170 BT_CLI_LOGE_APPEND_CAUSE("Argument contains a non-printable character.");
174 /* Parse the component name */
175 gs_name
= bt_common_string_until(at
, ".:\\", ":", &end_pos
);
180 if (arg
[end_pos
] == ':') {
184 g_string_assign(gs_name
, "");
187 /* Parse the component class type */
188 gs_comp_cls_type
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
189 if (!gs_comp_cls_type
|| at
[end_pos
] == '\0') {
190 BT_CLI_LOGE_APPEND_CAUSE("Missing component class type (`source`, `filter`, or `sink`).");
194 if (strcmp(gs_comp_cls_type
->str
, "source") == 0 ||
195 strcmp(gs_comp_cls_type
->str
, "src") == 0) {
196 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_SOURCE
;
197 } else if (strcmp(gs_comp_cls_type
->str
, "filter") == 0 ||
198 strcmp(gs_comp_cls_type
->str
, "flt") == 0) {
199 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_FILTER
;
200 } else if (strcmp(gs_comp_cls_type
->str
, "sink") == 0) {
201 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_SINK
;
203 BT_CLI_LOGE_APPEND_CAUSE("Unknown component class type: `%s`.",
204 gs_comp_cls_type
->str
);
210 /* Parse the plugin name */
211 gs_plugin
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
212 if (!gs_plugin
|| gs_plugin
->len
== 0 || at
[end_pos
] == '\0') {
213 BT_CLI_LOGE_APPEND_CAUSE("Missing plugin or component class name.");
219 /* Parse the component class name */
220 gs_comp_cls
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
221 if (!gs_comp_cls
|| gs_comp_cls
->len
== 0) {
222 BT_CLI_LOGE_APPEND_CAUSE("Missing component class name.");
226 if (at
[end_pos
] != '\0') {
227 /* Found a non-escaped `.` */
232 if (gs_name
->len
== 0) {
234 g_string_free(gs_name
, TRUE
);
236 *name
= gs_name
->str
;
237 g_string_free(gs_name
, FALSE
);
240 g_string_free(gs_name
, TRUE
);
243 *plugin
= gs_plugin
->str
;
244 *comp_cls
= gs_comp_cls
->str
;
245 g_string_free(gs_plugin
, FALSE
);
246 g_string_free(gs_comp_cls
, FALSE
);
262 g_string_free(gs_name
, TRUE
);
266 g_string_free(gs_plugin
, TRUE
);
270 g_string_free(gs_comp_cls
, TRUE
);
273 if (gs_comp_cls_type
) {
274 g_string_free(gs_comp_cls_type
, TRUE
);
281 * Prints the Babeltrace version.
284 void print_version(void)
286 if (GIT_VERSION
[0] == '\0') {
287 puts("Babeltrace " VERSION
);
289 puts("Babeltrace " VERSION
" - " GIT_VERSION
);
294 * Destroys a component configuration.
297 void bt_config_component_destroy(bt_object
*obj
)
299 struct bt_config_component
*bt_config_component
=
300 container_of(obj
, struct bt_config_component
, base
);
306 if (bt_config_component
->plugin_name
) {
307 g_string_free(bt_config_component
->plugin_name
, TRUE
);
310 if (bt_config_component
->comp_cls_name
) {
311 g_string_free(bt_config_component
->comp_cls_name
, TRUE
);
314 if (bt_config_component
->instance_name
) {
315 g_string_free(bt_config_component
->instance_name
, TRUE
);
318 BT_VALUE_PUT_REF_AND_RESET(bt_config_component
->params
);
319 g_free(bt_config_component
);
326 * Creates a component configuration using the given plugin name and
327 * component name. `plugin_name` and `comp_cls_name` are copied (belong
328 * to the return value).
330 * Return value is owned by the caller.
333 struct bt_config_component
*bt_config_component_create(
334 bt_component_class_type type
,
335 const char *plugin_name
, const char *comp_cls_name
,
338 struct bt_config_component
*cfg_component
= NULL
;
340 cfg_component
= g_new0(struct bt_config_component
, 1);
341 if (!cfg_component
) {
342 BT_CLI_LOGE_APPEND_CAUSE_OOM();
346 bt_object_init_shared(&cfg_component
->base
,
347 bt_config_component_destroy
);
348 cfg_component
->type
= type
;
349 cfg_component
->plugin_name
= g_string_new(plugin_name
);
350 if (!cfg_component
->plugin_name
) {
351 BT_CLI_LOGE_APPEND_CAUSE_OOM();
355 cfg_component
->comp_cls_name
= g_string_new(comp_cls_name
);
356 if (!cfg_component
->comp_cls_name
) {
357 BT_CLI_LOGE_APPEND_CAUSE_OOM();
361 cfg_component
->instance_name
= g_string_new(NULL
);
362 if (!cfg_component
->instance_name
) {
363 BT_CLI_LOGE_APPEND_CAUSE_OOM();
367 cfg_component
->log_level
= init_log_level
;
369 /* Start with empty parameters */
370 cfg_component
->params
= bt_value_map_create();
371 if (!cfg_component
->params
) {
372 BT_CLI_LOGE_APPEND_CAUSE_OOM();
379 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
382 return cfg_component
;
386 * Creates a component configuration from a command-line --component
390 struct bt_config_component
*bt_config_component_from_arg(const char *arg
,
393 struct bt_config_component
*cfg_comp
= NULL
;
395 char *plugin_name
= NULL
;
396 char *comp_cls_name
= NULL
;
397 bt_component_class_type type
;
399 plugin_comp_cls_names(arg
, &name
, &plugin_name
, &comp_cls_name
, &type
);
400 if (!plugin_name
|| !comp_cls_name
) {
404 cfg_comp
= bt_config_component_create(type
, plugin_name
, comp_cls_name
,
411 g_string_assign(cfg_comp
->instance_name
, name
);
417 BT_OBJECT_PUT_REF_AND_RESET(cfg_comp
);
422 g_free(comp_cls_name
);
427 * Destroys a configuration.
430 void bt_config_destroy(bt_object
*obj
)
432 struct bt_config
*cfg
=
433 container_of(obj
, struct bt_config
, base
);
439 BT_VALUE_PUT_REF_AND_RESET(cfg
->plugin_paths
);
441 switch (cfg
->command
) {
442 case BT_CONFIG_COMMAND_RUN
:
443 if (cfg
->cmd_data
.run
.sources
) {
444 g_ptr_array_free(cfg
->cmd_data
.run
.sources
, TRUE
);
447 if (cfg
->cmd_data
.run
.filters
) {
448 g_ptr_array_free(cfg
->cmd_data
.run
.filters
, TRUE
);
451 if (cfg
->cmd_data
.run
.sinks
) {
452 g_ptr_array_free(cfg
->cmd_data
.run
.sinks
, TRUE
);
455 if (cfg
->cmd_data
.run
.connections
) {
456 g_ptr_array_free(cfg
->cmd_data
.run
.connections
,
460 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
462 case BT_CONFIG_COMMAND_HELP
:
463 BT_OBJECT_PUT_REF_AND_RESET(cfg
->cmd_data
.help
.cfg_component
);
465 case BT_CONFIG_COMMAND_QUERY
:
466 BT_OBJECT_PUT_REF_AND_RESET(cfg
->cmd_data
.query
.cfg_component
);
468 if (cfg
->cmd_data
.query
.object
) {
469 g_string_free(cfg
->cmd_data
.query
.object
, TRUE
);
472 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
473 if (cfg
->cmd_data
.print_ctf_metadata
.path
) {
474 g_string_free(cfg
->cmd_data
.print_ctf_metadata
.path
,
477 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
481 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
482 if (cfg
->cmd_data
.print_lttng_live_sessions
.url
) {
484 cfg
->cmd_data
.print_lttng_live_sessions
.url
,
487 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
502 void destroy_glist_of_gstring(GList
*list
)
510 for (at
= list
; at
; at
= g_list_next(at
)) {
511 g_string_free(at
->data
, TRUE
);
518 * Creates a simple lexical scanner for parsing comma-delimited names
521 * Return value is owned by the caller.
524 GScanner
*create_csv_identifiers_scanner(void)
527 GScannerConfig scanner_config
= {
528 .cset_skip_characters
= " \t\n",
529 .cset_identifier_first
= G_CSET_a_2_z G_CSET_A_2_Z
"_",
530 .cset_identifier_nth
= G_CSET_a_2_z G_CSET_A_2_Z
":_-",
531 .case_sensitive
= TRUE
,
532 .cpair_comment_single
= NULL
,
533 .skip_comment_multi
= TRUE
,
534 .skip_comment_single
= TRUE
,
535 .scan_comment_multi
= FALSE
,
536 .scan_identifier
= TRUE
,
537 .scan_identifier_1char
= TRUE
,
538 .scan_identifier_NULL
= FALSE
,
539 .scan_symbols
= FALSE
,
540 .symbol_2_token
= FALSE
,
541 .scope_0_fallback
= FALSE
,
542 .scan_binary
= FALSE
,
546 .scan_hex_dollar
= FALSE
,
547 .numbers_2_int
= FALSE
,
548 .int_2_float
= FALSE
,
549 .store_int64
= FALSE
,
550 .scan_string_sq
= FALSE
,
551 .scan_string_dq
= FALSE
,
552 .identifier_2_string
= FALSE
,
553 .char_2_token
= TRUE
,
556 scanner
= g_scanner_new(&scanner_config
);
558 BT_CLI_LOGE_APPEND_CAUSE_OOM();
565 * Converts a comma-delimited list of known names (--names option) to
566 * an array value object containing those names as string value objects.
568 * Return value is owned by the caller.
571 bt_value
*names_from_arg(const char *arg
)
573 GScanner
*scanner
= NULL
;
574 bt_value
*names
= NULL
;
575 bool found_all
= false, found_none
= false, found_item
= false;
577 names
= bt_value_array_create();
579 BT_CLI_LOGE_APPEND_CAUSE_OOM();
583 scanner
= create_csv_identifiers_scanner();
588 g_scanner_input_text(scanner
, arg
, strlen(arg
));
591 GTokenType token_type
= g_scanner_get_next_token(scanner
);
593 switch (token_type
) {
594 case G_TOKEN_IDENTIFIER
:
596 const char *identifier
= scanner
->value
.v_identifier
;
598 if (strcmp(identifier
, "payload") == 0 ||
599 strcmp(identifier
, "args") == 0 ||
600 strcmp(identifier
, "arg") == 0) {
602 if (bt_value_array_append_string_element(names
,
606 } else if (strcmp(identifier
, "context") == 0 ||
607 strcmp(identifier
, "ctx") == 0) {
609 if (bt_value_array_append_string_element(names
,
613 } else if (strcmp(identifier
, "scope") == 0 ||
614 strcmp(identifier
, "header") == 0) {
616 if (bt_value_array_append_string_element(names
,
620 } else if (strcmp(identifier
, "all") == 0) {
622 if (bt_value_array_append_string_element(names
,
626 } else if (strcmp(identifier
, "none") == 0) {
628 if (bt_value_array_append_string_element(names
,
633 BT_CLI_LOGE_APPEND_CAUSE("Unknown name: `%s`.",
649 if (found_none
&& found_all
) {
650 BT_CLI_LOGE_APPEND_CAUSE("Only either `all` or `none` can be specified in the list given to the --names option, but not both.");
654 * Legacy behavior is to clear the defaults (show none) when at
655 * least one item is specified.
657 if (found_item
&& !found_none
&& !found_all
) {
658 if (bt_value_array_append_string_element(names
, "none")) {
663 g_scanner_destroy(scanner
);
668 BT_VALUE_PUT_REF_AND_RESET(names
);
670 g_scanner_destroy(scanner
);
676 * Converts a comma-delimited list of known fields (--fields option) to
677 * an array value object containing those fields as string
680 * Return value is owned by the caller.
683 bt_value
*fields_from_arg(const char *arg
)
685 GScanner
*scanner
= NULL
;
688 fields
= bt_value_array_create();
690 BT_CLI_LOGE_APPEND_CAUSE_OOM();
694 scanner
= create_csv_identifiers_scanner();
699 g_scanner_input_text(scanner
, arg
, strlen(arg
));
702 GTokenType token_type
= g_scanner_get_next_token(scanner
);
704 switch (token_type
) {
705 case G_TOKEN_IDENTIFIER
:
707 const char *identifier
= scanner
->value
.v_identifier
;
709 if (strcmp(identifier
, "trace") == 0 ||
710 strcmp(identifier
, "trace:hostname") == 0 ||
711 strcmp(identifier
, "trace:domain") == 0 ||
712 strcmp(identifier
, "trace:procname") == 0 ||
713 strcmp(identifier
, "trace:vpid") == 0 ||
714 strcmp(identifier
, "loglevel") == 0 ||
715 strcmp(identifier
, "emf") == 0 ||
716 strcmp(identifier
, "callsite") == 0 ||
717 strcmp(identifier
, "all") == 0) {
718 if (bt_value_array_append_string_element(fields
,
723 BT_CLI_LOGE_APPEND_CAUSE("Unknown field: `%s`.",
741 BT_VALUE_PUT_REF_AND_RESET(fields
);
745 g_scanner_destroy(scanner
);
751 void append_param_arg(GString
*params_arg
, const char *key
, const char *value
)
753 BT_ASSERT(params_arg
);
757 if (params_arg
->len
!= 0) {
758 g_string_append_c(params_arg
, ',');
761 g_string_append(params_arg
, key
);
762 g_string_append_c(params_arg
, '=');
763 g_string_append(params_arg
, value
);
767 * Inserts the equivalent "prefix-NAME=yes" strings into params_arg
768 * where the names are in names_array.
771 int insert_flat_params_from_array(GString
*params_arg
,
772 const bt_value
*names_array
, const char *prefix
)
776 GString
*tmpstr
= NULL
, *default_value
= NULL
;
777 bool default_set
= false, non_default_set
= false;
780 * names_array may be NULL if no CLI options were specified to
781 * trigger its creation.
787 tmpstr
= g_string_new(NULL
);
789 BT_CLI_LOGE_APPEND_CAUSE_OOM();
794 default_value
= g_string_new(NULL
);
795 if (!default_value
) {
796 BT_CLI_LOGE_APPEND_CAUSE_OOM();
801 for (i
= 0; i
< bt_value_array_get_size(names_array
); i
++) {
802 const bt_value
*str_obj
=
803 bt_value_array_borrow_element_by_index_const(names_array
,
806 bool is_default
= false;
809 BT_CLI_LOGE_APPEND_CAUSE("Unexpected error.");
814 suffix
= bt_value_string_get(str_obj
);
816 g_string_assign(tmpstr
, prefix
);
817 g_string_append(tmpstr
, "-");
819 /* Special-case for "all" and "none". */
820 if (strcmp(suffix
, "all") == 0) {
822 g_string_assign(default_value
, "show");
823 } else if (strcmp(suffix
, "none") == 0) {
825 g_string_assign(default_value
, "hide");
829 g_string_append(tmpstr
, "default");
830 append_param_arg(params_arg
, tmpstr
->str
,
833 non_default_set
= true;
834 g_string_append(tmpstr
, suffix
);
835 append_param_arg(params_arg
, tmpstr
->str
, "yes");
839 /* Implicit field-default=hide if any non-default option is set. */
840 if (non_default_set
&& !default_set
) {
841 g_string_assign(tmpstr
, prefix
);
842 g_string_append(tmpstr
, "-default");
843 g_string_assign(default_value
, "hide");
844 append_param_arg(params_arg
, tmpstr
->str
, default_value
->str
);
849 g_string_free(default_value
, TRUE
);
853 g_string_free(tmpstr
, TRUE
);
866 OPT_CLOCK_FORCE_CORRELATE
,
877 OPT_DEBUG_INFO_FULL_PATH
,
878 OPT_DEBUG_INFO_TARGET_PREFIX
,
888 OPT_OMIT_HOME_PLUGIN_PATH
,
889 OPT_OMIT_SYSTEM_PLUGIN_PATH
,
895 OPT_RESET_BASE_PARAMS
,
899 OPT_STREAM_INTERSECTION
,
906 enum bt_config_component_dest
{
907 BT_CONFIG_COMPONENT_DEST_UNKNOWN
= -1,
908 BT_CONFIG_COMPONENT_DEST_SOURCE
,
909 BT_CONFIG_COMPONENT_DEST_FILTER
,
910 BT_CONFIG_COMPONENT_DEST_SINK
,
914 * Adds a configuration component to the appropriate configuration
915 * array depending on the destination.
918 void add_run_cfg_comp(struct bt_config
*cfg
,
919 struct bt_config_component
*cfg_comp
,
920 enum bt_config_component_dest dest
)
922 bt_object_get_ref(cfg_comp
);
925 case BT_CONFIG_COMPONENT_DEST_SOURCE
:
926 g_ptr_array_add(cfg
->cmd_data
.run
.sources
, cfg_comp
);
928 case BT_CONFIG_COMPONENT_DEST_FILTER
:
929 g_ptr_array_add(cfg
->cmd_data
.run
.filters
, cfg_comp
);
931 case BT_CONFIG_COMPONENT_DEST_SINK
:
932 g_ptr_array_add(cfg
->cmd_data
.run
.sinks
, cfg_comp
);
940 int add_run_cfg_comp_check_name(struct bt_config
*cfg
,
941 struct bt_config_component
*cfg_comp
,
942 enum bt_config_component_dest dest
,
943 bt_value
*instance_names
)
947 if (cfg_comp
->instance_name
->len
== 0) {
948 BT_CLI_LOGE_APPEND_CAUSE("Found an unnamed component.");
953 if (bt_value_map_has_entry(instance_names
,
954 cfg_comp
->instance_name
->str
)) {
955 BT_CLI_LOGE_APPEND_CAUSE("Duplicate component instance name:\n %s",
956 cfg_comp
->instance_name
->str
);
961 if (bt_value_map_insert_entry(instance_names
,
962 cfg_comp
->instance_name
->str
, bt_value_null
)) {
963 BT_CLI_LOGE_APPEND_CAUSE_OOM();
968 add_run_cfg_comp(cfg
, cfg_comp
, dest
);
975 int append_env_var_plugin_paths(bt_value
*plugin_paths
)
980 if (bt_common_is_setuid_setgid()) {
981 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
985 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
990 ret
= bt_config_append_plugin_paths(plugin_paths
, envvar
);
994 BT_CLI_LOGE_APPEND_CAUSE("Cannot append plugin paths from BABELTRACE_PLUGIN_PATH.");
1001 int append_home_and_system_plugin_paths(bt_value
*plugin_paths
,
1002 bool omit_system_plugin_path
, bool omit_home_plugin_path
)
1006 if (!omit_home_plugin_path
) {
1007 if (bt_common_is_setuid_setgid()) {
1008 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1010 char *home_plugin_dir
= bt_common_get_home_plugin_path(
1011 BT_LOG_OUTPUT_LEVEL
);
1013 if (home_plugin_dir
) {
1014 ret
= bt_config_append_plugin_paths(
1015 plugin_paths
, home_plugin_dir
);
1016 free(home_plugin_dir
);
1019 BT_CLI_LOGE_APPEND_CAUSE("Invalid home plugin path.");
1026 if (!omit_system_plugin_path
) {
1027 if (bt_config_append_plugin_paths(plugin_paths
,
1028 bt_common_get_system_plugin_path())) {
1029 BT_CLI_LOGE_APPEND_CAUSE("Invalid system plugin path.");
1035 BT_CLI_LOGE_APPEND_CAUSE("Cannot append home and system plugin paths.");
1040 int append_home_and_system_plugin_paths_cfg(struct bt_config
*cfg
)
1042 return append_home_and_system_plugin_paths(cfg
->plugin_paths
,
1043 cfg
->omit_system_plugin_path
, cfg
->omit_home_plugin_path
);
1047 struct bt_config
*bt_config_base_create(enum bt_config_command command
,
1048 const bt_value
*initial_plugin_paths
,
1051 struct bt_config
*cfg
;
1054 cfg
= g_new0(struct bt_config
, 1);
1056 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1060 bt_object_init_shared(&cfg
->base
, bt_config_destroy
);
1061 cfg
->command
= command
;
1062 cfg
->command_needs_plugins
= needs_plugins
;
1064 if (initial_plugin_paths
) {
1065 bt_value
*initial_plugin_paths_copy
;
1067 (void) bt_value_copy(initial_plugin_paths
,
1068 &initial_plugin_paths_copy
);
1069 cfg
->plugin_paths
= initial_plugin_paths_copy
;
1071 cfg
->plugin_paths
= bt_value_array_create();
1072 if (!cfg
->plugin_paths
) {
1073 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1081 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1088 struct bt_config
*bt_config_run_create(
1089 const bt_value
*initial_plugin_paths
)
1091 struct bt_config
*cfg
;
1094 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_RUN
,
1095 initial_plugin_paths
, true);
1100 cfg
->cmd_data
.run
.sources
= g_ptr_array_new_with_free_func(
1101 (GDestroyNotify
) bt_object_put_ref
);
1102 if (!cfg
->cmd_data
.run
.sources
) {
1103 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1107 cfg
->cmd_data
.run
.filters
= g_ptr_array_new_with_free_func(
1108 (GDestroyNotify
) bt_object_put_ref
);
1109 if (!cfg
->cmd_data
.run
.filters
) {
1110 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1114 cfg
->cmd_data
.run
.sinks
= g_ptr_array_new_with_free_func(
1115 (GDestroyNotify
) bt_object_put_ref
);
1116 if (!cfg
->cmd_data
.run
.sinks
) {
1117 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1121 cfg
->cmd_data
.run
.connections
= g_ptr_array_new_with_free_func(
1122 (GDestroyNotify
) bt_config_connection_destroy
);
1123 if (!cfg
->cmd_data
.run
.connections
) {
1124 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1131 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1138 struct bt_config
*bt_config_list_plugins_create(
1139 const bt_value
*initial_plugin_paths
)
1141 struct bt_config
*cfg
;
1144 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_LIST_PLUGINS
,
1145 initial_plugin_paths
, true);
1153 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1160 struct bt_config
*bt_config_help_create(
1161 const bt_value
*initial_plugin_paths
,
1162 int default_log_level
)
1164 struct bt_config
*cfg
;
1167 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_HELP
,
1168 initial_plugin_paths
, true);
1173 cfg
->cmd_data
.help
.cfg_component
=
1174 bt_config_component_create(-1, NULL
, NULL
, default_log_level
);
1175 if (!cfg
->cmd_data
.help
.cfg_component
) {
1182 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1189 struct bt_config
*bt_config_query_create(
1190 const bt_value
*initial_plugin_paths
)
1192 struct bt_config
*cfg
;
1195 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_QUERY
,
1196 initial_plugin_paths
, true);
1201 cfg
->cmd_data
.query
.object
= g_string_new(NULL
);
1202 if (!cfg
->cmd_data
.query
.object
) {
1203 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1210 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1217 struct bt_config
*bt_config_print_ctf_metadata_create(
1218 const bt_value
*initial_plugin_paths
)
1220 struct bt_config
*cfg
;
1223 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_PRINT_CTF_METADATA
,
1224 initial_plugin_paths
, true);
1229 cfg
->cmd_data
.print_ctf_metadata
.path
= g_string_new(NULL
);
1230 if (!cfg
->cmd_data
.print_ctf_metadata
.path
) {
1231 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1235 cfg
->cmd_data
.print_ctf_metadata
.output_path
= g_string_new(NULL
);
1236 if (!cfg
->cmd_data
.print_ctf_metadata
.output_path
) {
1237 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1244 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1251 struct bt_config
*bt_config_print_lttng_live_sessions_create(
1252 const bt_value
*initial_plugin_paths
)
1254 struct bt_config
*cfg
;
1257 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
,
1258 initial_plugin_paths
, true);
1263 cfg
->cmd_data
.print_lttng_live_sessions
.url
= g_string_new(NULL
);
1264 if (!cfg
->cmd_data
.print_lttng_live_sessions
.url
) {
1265 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1269 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
=
1271 if (!cfg
->cmd_data
.print_lttng_live_sessions
.output_path
) {
1272 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1279 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1286 int bt_config_append_plugin_paths_check_setuid_setgid(
1287 bt_value
*plugin_paths
, const char *arg
)
1291 if (bt_common_is_setuid_setgid()) {
1292 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1296 if (bt_config_append_plugin_paths(plugin_paths
, arg
)) {
1297 BT_CLI_LOGE_APPEND_CAUSE("Invalid --plugin-path option's argument:\n %s",
1308 * Prints the expected format for a --params option.
1311 void print_expected_params_format(FILE *fp
)
1313 fprintf(fp
, "Expected format of PARAMS\n");
1314 fprintf(fp
, "-------------------------\n");
1316 fprintf(fp
, " PARAM=VALUE[,PARAM=VALUE]...\n");
1318 fprintf(fp
, "The parameter string is a comma-separated list of PARAM=VALUE assignments,\n");
1319 fprintf(fp
, "where PARAM is the parameter name (C identifier plus the [:.-] characters),\n");
1320 fprintf(fp
, "and VALUE can be one of:\n");
1322 fprintf(fp
, "* `null`, `nul`, `NULL`: null value (no backticks).\n");
1323 fprintf(fp
, "* `true`, `TRUE`, `yes`, `YES`: true boolean value (no backticks).\n");
1324 fprintf(fp
, "* `false`, `FALSE`, `no`, `NO`: false boolean value (no backticks).\n");
1325 fprintf(fp
, "* Binary (`0b` prefix), octal (`0` prefix), decimal, or hexadecimal\n");
1326 fprintf(fp
, " (`0x` prefix) unsigned (with `+` prefix) or signed 64-bit integer.\n");
1327 fprintf(fp
, "* Double precision floating point number (scientific notation is accepted).\n");
1328 fprintf(fp
, "* Unquoted string with no special characters, and not matching any of\n");
1329 fprintf(fp
, " the null and boolean value symbols above.\n");
1330 fprintf(fp
, "* Double-quoted string (accepts escape characters).\n");
1331 fprintf(fp
, "* Array, formatted as an opening `[`, a list of comma-separated values\n");
1332 fprintf(fp
, " (as described by the current list) and a closing `]`.\n");
1334 fprintf(fp
, "You can put whitespaces allowed around individual `=` and `,` symbols.\n");
1336 fprintf(fp
, "Example:\n");
1338 fprintf(fp
, " many=null, fresh=yes, condition=false, squirrel=-782329,\n");
1339 fprintf(fp
, " play=+23, observe=3.14, simple=beef, needs-quotes=\"some string\",\n");
1340 fprintf(fp
, " escape.chars-are:allowed=\"this is a \\\" double quote\",\n");
1341 fprintf(fp
, " things=[1, \"2\", 3]\n");
1343 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1344 fprintf(fp
, "babeltrace2 from a shell.\n");
1348 bool help_option_is_specified(
1349 const struct bt_argpar_parse_ret
*argpar_parse_ret
)
1352 bool specified
= false;
1354 for (i
= 0; i
< argpar_parse_ret
->items
->len
; i
++) {
1355 struct bt_argpar_item
*argpar_item
=
1356 g_ptr_array_index(argpar_parse_ret
->items
, i
);
1357 struct bt_argpar_item_opt
*argpar_item_opt
;
1359 if (argpar_item
->type
!= BT_ARGPAR_ITEM_TYPE_OPT
) {
1363 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
1364 if (argpar_item_opt
->descr
->id
== OPT_HELP
) {
1374 * Prints the help command usage.
1377 void print_help_usage(FILE *fp
)
1379 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] help [OPTIONS] PLUGIN\n");
1380 fprintf(fp
, " babeltrace2 [GENERAL OPTIONS] help [OPTIONS] TYPE.PLUGIN.CLS\n");
1382 fprintf(fp
, "Options:\n");
1384 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1385 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
1386 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1387 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1388 fprintf(fp
, " dynamic plugins can be loaded\n");
1389 fprintf(fp
, " -h, --help Show this help and quit\n");
1391 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1393 fprintf(fp
, "Use `babeltrace2 list-plugins` to show the list of available plugins.\n");
1397 const struct bt_argpar_opt_descr help_options
[] = {
1398 /* id, short_name, long_name, with_arg */
1399 { OPT_HELP
, 'h', "help", false },
1400 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
1401 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
1402 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
1403 BT_ARGPAR_OPT_DESCR_SENTINEL
1407 * Creates a Babeltrace config object from the arguments of a help
1410 * *retcode is set to the appropriate exit code to use.
1413 struct bt_config
*bt_config_help_from_args(int argc
, const char *argv
[],
1414 int *retcode
, bool force_omit_system_plugin_path
,
1415 bool force_omit_home_plugin_path
,
1416 const bt_value
*initial_plugin_paths
, int default_log_level
)
1419 struct bt_config
*cfg
= NULL
;
1420 const char *leftover
= NULL
;
1421 char *plugin_name
= NULL
, *comp_cls_name
= NULL
;
1422 struct bt_argpar_parse_ret argpar_parse_ret
;
1425 cfg
= bt_config_help_create(initial_plugin_paths
, default_log_level
);
1430 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
1431 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
1432 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
1438 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, help_options
, true);
1439 if (argpar_parse_ret
.error
) {
1440 BT_CLI_LOGE_APPEND_CAUSE(
1441 "While parsing `help` command's command-line arguments: %s",
1442 argpar_parse_ret
.error
->str
);
1446 if (help_option_is_specified(&argpar_parse_ret
)) {
1447 print_help_usage(stdout
);
1449 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1453 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
1454 struct bt_argpar_item
*argpar_item
=
1455 g_ptr_array_index(argpar_parse_ret
.items
, i
);
1457 if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_OPT
) {
1458 struct bt_argpar_item_opt
*argpar_item_opt
;
1460 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
1461 arg
= argpar_item_opt
->arg
;
1463 switch (argpar_item_opt
->descr
->id
) {
1464 case OPT_PLUGIN_PATH
:
1465 if (bt_config_append_plugin_paths_check_setuid_setgid(
1466 cfg
->plugin_paths
, arg
)) {
1470 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
1471 cfg
->omit_system_plugin_path
= true;
1473 case OPT_OMIT_HOME_PLUGIN_PATH
:
1474 cfg
->omit_home_plugin_path
= true;
1477 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1478 argpar_item_opt
->descr
->id
);
1482 struct bt_argpar_item_non_opt
*argpar_item_non_opt
1483 = (struct bt_argpar_item_non_opt
*) argpar_item
;
1486 BT_CLI_LOGE_APPEND_CAUSE("Extraneous command-line argument specified to `help` command: `%s`.",
1487 argpar_item_non_opt
->arg
);
1491 leftover
= argpar_item_non_opt
->arg
;
1496 plugin_comp_cls_names(leftover
, NULL
,
1497 &plugin_name
, &comp_cls_name
,
1498 &cfg
->cmd_data
.help
.cfg_component
->type
);
1499 if (plugin_name
&& comp_cls_name
) {
1500 /* Component class help */
1502 cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
1505 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
,
1508 /* Fall back to plugin help */
1510 cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
1514 print_help_usage(stdout
);
1516 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1520 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
1528 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1531 g_free(plugin_name
);
1532 g_free(comp_cls_name
);
1534 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
1540 * Prints the help command usage.
1543 void print_query_usage(FILE *fp
)
1545 fprintf(fp
, "Usage: babeltrace2 [GEN OPTS] query [OPTS] TYPE.PLUGIN.CLS OBJECT\n");
1547 fprintf(fp
, "Options:\n");
1549 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1550 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
1551 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1552 fprintf(fp
, " -p, --params=PARAMS Set the query parameters to PARAMS\n");
1553 fprintf(fp
, " (see the expected format of PARAMS below)\n");
1554 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1555 fprintf(fp
, " dynamic plugins can be loaded\n");
1556 fprintf(fp
, " -h, --help Show this help and quit\n");
1557 fprintf(fp
, "\n\n");
1558 print_expected_params_format(fp
);
1562 struct poptOption query_long_options
[] = {
1563 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
1564 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
1565 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
1566 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
1567 { "params", 'p', POPT_ARG_STRING
, NULL
, OPT_PARAMS
, NULL
, NULL
},
1568 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
1569 { NULL
, 0, '\0', NULL
, 0, NULL
, NULL
},
1573 * Creates a Babeltrace config object from the arguments of a query
1576 * *retcode is set to the appropriate exit code to use.
1579 struct bt_config
*bt_config_query_from_args(int argc
, const char *argv
[],
1580 int *retcode
, bool force_omit_system_plugin_path
,
1581 bool force_omit_home_plugin_path
,
1582 const bt_value
*initial_plugin_paths
,
1583 int default_log_level
)
1585 poptContext pc
= NULL
;
1589 struct bt_config
*cfg
= NULL
;
1590 const char *leftover
;
1592 GString
*error_str
= NULL
;
1594 params
= bt_value_null
;
1595 bt_value_get_ref(bt_value_null
);
1598 cfg
= bt_config_query_create(initial_plugin_paths
);
1603 error_str
= g_string_new(NULL
);
1605 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1609 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
1610 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
1611 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
1617 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
1618 query_long_options
, POPT_CONTEXT_KEEP_FIRST
);
1620 BT_CLI_LOGE_APPEND_CAUSE("Cannot get popt context.");
1624 poptReadDefaultConfig(pc
, 0);
1626 while ((opt
= poptGetNextOpt(pc
)) > 0) {
1627 arg
= poptGetOptArg(pc
);
1630 case OPT_PLUGIN_PATH
:
1631 if (bt_config_append_plugin_paths_check_setuid_setgid(
1632 cfg
->plugin_paths
, arg
)) {
1636 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
1637 cfg
->omit_system_plugin_path
= true;
1639 case OPT_OMIT_HOME_PLUGIN_PATH
:
1640 cfg
->omit_home_plugin_path
= true;
1644 bt_value_put_ref(params
);
1645 params
= cli_value_from_arg(arg
, error_str
);
1647 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
1654 print_query_usage(stdout
);
1656 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1659 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1668 /* Check for option parsing error */
1670 BT_CLI_LOGE_APPEND_CAUSE("While parsing command-line options, at option %s: `%s`.",
1671 poptBadOption(pc
, 0), poptStrerror(opt
));
1676 * We need exactly two leftover arguments which are the
1677 * mandatory component class specification and query object.
1679 leftover
= poptGetArg(pc
);
1681 cfg
->cmd_data
.query
.cfg_component
=
1682 bt_config_component_from_arg(leftover
,
1684 if (!cfg
->cmd_data
.query
.cfg_component
) {
1685 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for component class specification:\n %s",
1691 BT_OBJECT_MOVE_REF(cfg
->cmd_data
.query
.cfg_component
->params
,
1694 print_query_usage(stdout
);
1696 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1700 leftover
= poptGetArg(pc
);
1702 if (strlen(leftover
) == 0) {
1703 BT_CLI_LOGE_APPEND_CAUSE("Invalid empty object.");
1707 g_string_assign(cfg
->cmd_data
.query
.object
, leftover
);
1709 print_query_usage(stdout
);
1711 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1715 leftover
= poptGetArg(pc
);
1717 BT_CLI_LOGE_APPEND_CAUSE("Unexpected argument: `%s`.", leftover
);
1721 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
1729 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1733 poptFreeContext(pc
);
1737 g_string_free(error_str
, TRUE
);
1740 bt_value_put_ref(params
);
1746 * Prints the list-plugins command usage.
1749 void print_list_plugins_usage(FILE *fp
)
1751 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] list-plugins [OPTIONS]\n");
1753 fprintf(fp
, "Options:\n");
1755 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1756 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
1757 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1758 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1759 fprintf(fp
, " dynamic plugins can be loaded\n");
1760 fprintf(fp
, " -h, --help Show this help and quit\n");
1762 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1764 fprintf(fp
, "Use `babeltrace2 help` to get help for a specific plugin or component class.\n");
1768 struct poptOption list_plugins_long_options
[] = {
1769 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
1770 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
1771 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
1772 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
1773 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
1774 { NULL
, 0, '\0', NULL
, 0, NULL
, NULL
},
1778 * Creates a Babeltrace config object from the arguments of a
1779 * list-plugins command.
1781 * *retcode is set to the appropriate exit code to use.
1784 struct bt_config
*bt_config_list_plugins_from_args(int argc
, const char *argv
[],
1785 int *retcode
, bool force_omit_system_plugin_path
,
1786 bool force_omit_home_plugin_path
,
1787 const bt_value
*initial_plugin_paths
)
1789 poptContext pc
= NULL
;
1793 struct bt_config
*cfg
= NULL
;
1794 const char *leftover
;
1797 cfg
= bt_config_list_plugins_create(initial_plugin_paths
);
1802 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
1803 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
1804 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
1810 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
1811 list_plugins_long_options
, POPT_CONTEXT_KEEP_FIRST
);
1813 BT_CLI_LOGE_APPEND_CAUSE("Cannot get popt context.");
1817 poptReadDefaultConfig(pc
, 0);
1819 while ((opt
= poptGetNextOpt(pc
)) > 0) {
1820 arg
= poptGetOptArg(pc
);
1823 case OPT_PLUGIN_PATH
:
1824 if (bt_config_append_plugin_paths_check_setuid_setgid(
1825 cfg
->plugin_paths
, arg
)) {
1829 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
1830 cfg
->omit_system_plugin_path
= true;
1832 case OPT_OMIT_HOME_PLUGIN_PATH
:
1833 cfg
->omit_home_plugin_path
= true;
1836 print_list_plugins_usage(stdout
);
1838 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1841 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1850 /* Check for option parsing error */
1852 BT_CLI_LOGE_APPEND_CAUSE("While parsing command-line options, at option %s: %s",
1853 poptBadOption(pc
, 0), poptStrerror(opt
));
1857 leftover
= poptGetArg(pc
);
1859 BT_CLI_LOGE_APPEND_CAUSE("Unexpected argument: `%s`.", leftover
);
1863 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
1871 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1875 poptFreeContext(pc
);
1883 * Prints the run command usage.
1886 void print_run_usage(FILE *fp
)
1888 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] run [OPTIONS]\n");
1890 fprintf(fp
, "Options:\n");
1892 fprintf(fp
, " -b, --base-params=PARAMS Set PARAMS as the current base parameters\n");
1893 fprintf(fp
, " for all the following components until\n");
1894 fprintf(fp
, " --reset-base-params is encountered\n");
1895 fprintf(fp
, " (see the expected format of PARAMS below)\n");
1896 fprintf(fp
, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
1897 fprintf(fp
, " Instantiate the component class CLS of type\n");
1898 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
1899 fprintf(fp
, " in the plugin PLUGIN, add it to the graph,\n");
1900 fprintf(fp
, " and optionally name it NAME (you can also\n");
1901 fprintf(fp
, " specify the name with --name)\n");
1902 fprintf(fp
, " -x, --connect=CONNECTION Connect two created components (see the\n");
1903 fprintf(fp
, " expected format of CONNECTION below)\n");
1904 fprintf(fp
, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
1905 fprintf(fp
, " (`N`, `V`, `D`, `I`, `W`, `E`, or `F`)\n");
1906 fprintf(fp
, " -n, --name=NAME Set the name of the current component\n");
1907 fprintf(fp
, " to NAME (must be unique amongst all the\n");
1908 fprintf(fp
, " names of the created components)\n");
1909 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1910 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
1911 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1912 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
1913 fprintf(fp
, " current component (see the expected format\n");
1914 fprintf(fp
, " of PARAMS below)\n");
1915 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1916 fprintf(fp
, " dynamic plugins can be loaded\n");
1917 fprintf(fp
, " -r, --reset-base-params Reset the current base parameters to an\n");
1918 fprintf(fp
, " empty map\n");
1919 fprintf(fp
, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
1920 fprintf(fp
, " the graph later, retry in DUR µs\n");
1921 fprintf(fp
, " (default: 100000)\n");
1922 fprintf(fp
, " -h, --help Show this help and quit\n");
1924 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1925 fprintf(fp
, "\n\n");
1926 fprintf(fp
, "Expected format of CONNECTION\n");
1927 fprintf(fp
, "-----------------------------\n");
1929 fprintf(fp
, " UPSTREAM[.UPSTREAM-PORT]:DOWNSTREAM[.DOWNSTREAM-PORT]\n");
1931 fprintf(fp
, "UPSTREAM and DOWNSTREAM are names of the upstream and downstream\n");
1932 fprintf(fp
, "components to connect together. You must escape the following characters\n\n");
1933 fprintf(fp
, "with `\\`: `\\`, `.`, and `:`. You can set the name of the current\n");
1934 fprintf(fp
, "component with the --name option.\n");
1936 fprintf(fp
, "UPSTREAM-PORT and DOWNSTREAM-PORT are optional globbing patterns to\n");
1937 fprintf(fp
, "identify the upstream and downstream ports to use for the connection.\n");
1938 fprintf(fp
, "When the port is not specified, `*` is used.\n");
1940 fprintf(fp
, "When a component named UPSTREAM has an available port which matches the\n");
1941 fprintf(fp
, "UPSTREAM-PORT globbing pattern, it is connected to the first port which\n");
1942 fprintf(fp
, "matches the DOWNSTREAM-PORT globbing pattern of the component named\n");
1943 fprintf(fp
, "DOWNSTREAM.\n");
1945 fprintf(fp
, "The only special character in UPSTREAM-PORT and DOWNSTREAM-PORT is `*`\n");
1946 fprintf(fp
, "which matches anything. You must escape the following characters\n");
1947 fprintf(fp
, "with `\\`: `\\`, `*`, `?`, `[`, `.`, and `:`.\n");
1949 fprintf(fp
, "You can connect a source component to a filter or sink component. You\n");
1950 fprintf(fp
, "can connect a filter component to a sink component.\n");
1952 fprintf(fp
, "Examples:\n");
1954 fprintf(fp
, " my-src:my-sink\n");
1955 fprintf(fp
, " ctf-fs.*stream*:utils-muxer:*\n");
1957 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1958 fprintf(fp
, "babeltrace2 from a shell.\n");
1959 fprintf(fp
, "\n\n");
1960 print_expected_params_format(fp
);
1964 * Creates a Babeltrace config object from the arguments of a run
1967 * *retcode is set to the appropriate exit code to use.
1970 struct bt_config
*bt_config_run_from_args(int argc
, const char *argv
[],
1971 int *retcode
, bool force_omit_system_plugin_path
,
1972 bool force_omit_home_plugin_path
,
1973 const bt_value
*initial_plugin_paths
, int default_log_level
)
1975 struct bt_config_component
*cur_cfg_comp
= NULL
;
1976 enum bt_config_component_dest cur_cfg_comp_dest
=
1977 BT_CONFIG_COMPONENT_DEST_UNKNOWN
;
1978 bt_value
*cur_base_params
= NULL
;
1980 struct bt_config
*cfg
= NULL
;
1981 bt_value
*instance_names
= NULL
;
1982 bt_value
*connection_args
= NULL
;
1983 char error_buf
[256] = { 0 };
1984 long retry_duration
= -1;
1985 bt_value_map_extend_status extend_status
;
1986 GString
*error_str
= NULL
;
1987 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
1990 static const struct bt_argpar_opt_descr run_options
[] = {
1991 { OPT_BASE_PARAMS
, 'b', "base-params", true },
1992 { OPT_COMPONENT
, 'c', "component", true },
1993 { OPT_CONNECT
, 'x', "connect", true },
1994 { OPT_HELP
, 'h', "help", false },
1995 { OPT_LOG_LEVEL
, 'l', "log-level", true },
1996 { OPT_NAME
, 'n', "name", true },
1997 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
1998 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
1999 { OPT_PARAMS
, 'p', "params", true },
2000 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
2001 { OPT_RESET_BASE_PARAMS
, 'r', "reset-base-params", false },
2002 { OPT_RETRY_DURATION
, '\0', "retry-duration", true },
2003 BT_ARGPAR_OPT_DESCR_SENTINEL
2008 error_str
= g_string_new(NULL
);
2010 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2015 print_run_usage(stdout
);
2020 cfg
= bt_config_run_create(initial_plugin_paths
);
2025 cfg
->cmd_data
.run
.retry_duration_us
= 100000;
2026 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
2027 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
2028 cur_base_params
= bt_value_map_create();
2029 if (!cur_base_params
) {
2030 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2034 instance_names
= bt_value_map_create();
2035 if (!instance_names
) {
2036 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2040 connection_args
= bt_value_array_create();
2041 if (!connection_args
) {
2042 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2046 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
2052 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, run_options
, true);
2053 if (argpar_parse_ret
.error
) {
2054 BT_CLI_LOGE_APPEND_CAUSE(
2055 "While parsing `run` command's command-line arguments: %s",
2056 argpar_parse_ret
.error
->str
);
2060 if (help_option_is_specified(&argpar_parse_ret
)) {
2061 print_run_usage(stdout
);
2063 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2067 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
2068 struct bt_argpar_item
*argpar_item
=
2069 g_ptr_array_index(argpar_parse_ret
.items
, i
);
2070 struct bt_argpar_item_opt
*argpar_item_opt
;
2073 /* This command does not accept leftover arguments. */
2074 if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
2075 struct bt_argpar_item_non_opt
*argpar_nonopt_item
=
2076 (struct bt_argpar_item_non_opt
*) argpar_item
;
2078 BT_CLI_LOGE_APPEND_CAUSE("Unexpected argument: `%s`",
2079 argpar_nonopt_item
->arg
);
2083 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
2084 arg
= argpar_item_opt
->arg
;
2086 switch (argpar_item_opt
->descr
->id
) {
2087 case OPT_PLUGIN_PATH
:
2088 if (bt_config_append_plugin_paths_check_setuid_setgid(
2089 cfg
->plugin_paths
, arg
)) {
2093 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
2094 cfg
->omit_system_plugin_path
= true;
2096 case OPT_OMIT_HOME_PLUGIN_PATH
:
2097 cfg
->omit_home_plugin_path
= true;
2101 enum bt_config_component_dest new_dest
;
2104 ret
= add_run_cfg_comp_check_name(cfg
,
2105 cur_cfg_comp
, cur_cfg_comp_dest
,
2107 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2113 cur_cfg_comp
= bt_config_component_from_arg(arg
,
2115 if (!cur_cfg_comp
) {
2116 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --component option's argument:\n %s",
2121 switch (cur_cfg_comp
->type
) {
2122 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2123 new_dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
2125 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2126 new_dest
= BT_CONFIG_COMPONENT_DEST_FILTER
;
2128 case BT_COMPONENT_CLASS_TYPE_SINK
:
2129 new_dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
2135 BT_ASSERT(cur_base_params
);
2136 bt_value_put_ref(cur_cfg_comp
->params
);
2137 if (bt_value_copy(cur_base_params
,
2138 &cur_cfg_comp
->params
) < 0) {
2139 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2143 cur_cfg_comp_dest
= new_dest
;
2149 bt_value
*params_to_set
;
2151 if (!cur_cfg_comp
) {
2152 BT_CLI_LOGE_APPEND_CAUSE("Cannot add parameters to unavailable component:\n %s",
2157 params
= cli_value_from_arg(arg
, error_str
);
2159 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
2164 extend_status
= bt_value_map_extend(
2165 cur_cfg_comp
->params
, params
, ¶ms_to_set
);
2166 BT_VALUE_PUT_REF_AND_RESET(params
);
2167 if (extend_status
!= BT_VALUE_MAP_EXTEND_STATUS_OK
) {
2168 BT_CLI_LOGE_APPEND_CAUSE("Cannot extend current component parameters with --params option's argument:\n %s",
2173 BT_OBJECT_MOVE_REF(cur_cfg_comp
->params
, params_to_set
);
2177 if (!cur_cfg_comp
) {
2178 BT_CLI_LOGE_APPEND_CAUSE("Cannot set the name of unavailable component:\n %s",
2183 g_string_assign(cur_cfg_comp
->instance_name
, arg
);
2186 if (!cur_cfg_comp
) {
2187 BT_CLI_LOGE_APPEND_CAUSE("Cannot set the log level of unavailable component:\n %s",
2192 cur_cfg_comp
->log_level
=
2193 bt_log_get_level_from_string(arg
);
2194 if (cur_cfg_comp
->log_level
< 0) {
2195 BT_CLI_LOGE_APPEND_CAUSE("Invalid argument for --log-level option:\n %s",
2200 case OPT_BASE_PARAMS
:
2202 bt_value
*params
= cli_value_from_arg(arg
, error_str
);
2205 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --base-params option's argument:\n %s",
2210 BT_OBJECT_MOVE_REF(cur_base_params
, params
);
2213 case OPT_RESET_BASE_PARAMS
:
2214 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
2215 cur_base_params
= bt_value_map_create();
2216 if (!cur_base_params
) {
2217 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2222 if (bt_value_array_append_string_element(
2223 connection_args
, arg
)) {
2224 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2228 case OPT_RETRY_DURATION
: {
2230 size_t arg_len
= strlen(argpar_item_opt
->arg
);
2232 retry_duration
= g_ascii_strtoll(argpar_item_opt
->arg
, &end
, 10);
2234 if (arg_len
== 0 || end
!= (argpar_item_opt
->arg
+ arg_len
)) {
2235 BT_CLI_LOGE_APPEND_CAUSE(
2236 "Could not parse --retry-duration option's argument as an unsigned integer: `%s`",
2237 argpar_item_opt
->arg
);
2241 if (retry_duration
< 0) {
2242 BT_CLI_LOGE_APPEND_CAUSE("--retry-duration option's argument must be positive or 0: %ld",
2247 cfg
->cmd_data
.run
.retry_duration_us
=
2248 (uint64_t) retry_duration
;
2252 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
2253 argpar_item_opt
->descr
->id
);
2258 /* Add current component */
2260 ret
= add_run_cfg_comp_check_name(cfg
, cur_cfg_comp
,
2261 cur_cfg_comp_dest
, instance_names
);
2262 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2268 if (cfg
->cmd_data
.run
.sources
->len
== 0) {
2269 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no source component.");
2273 if (cfg
->cmd_data
.run
.sinks
->len
== 0) {
2274 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no sink component.");
2278 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
2282 ret
= bt_config_cli_args_create_connections(cfg
,
2286 BT_CLI_LOGE_APPEND_CAUSE("Cannot creation connections:\n%s", error_buf
);
2294 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2298 g_string_free(error_str
, TRUE
);
2301 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
2302 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2303 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
2304 BT_VALUE_PUT_REF_AND_RESET(instance_names
);
2305 BT_VALUE_PUT_REF_AND_RESET(connection_args
);
2310 struct bt_config
*bt_config_run_from_args_array(const bt_value
*run_args
,
2311 int *retcode
, bool force_omit_system_plugin_path
,
2312 bool force_omit_home_plugin_path
,
2313 const bt_value
*initial_plugin_paths
, int default_log_level
)
2315 struct bt_config
*cfg
= NULL
;
2318 const size_t argc
= bt_value_array_get_size(run_args
);
2320 argv
= calloc(argc
, sizeof(*argv
));
2322 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2326 len
= bt_value_array_get_size(run_args
);
2328 BT_CLI_LOGE_APPEND_CAUSE("Invalid executable arguments.");
2331 for (i
= 0; i
< len
; i
++) {
2332 const bt_value
*arg_value
=
2333 bt_value_array_borrow_element_by_index_const(run_args
,
2337 BT_ASSERT(arg_value
);
2338 arg
= bt_value_string_get(arg_value
);
2343 cfg
= bt_config_run_from_args(argc
, argv
, retcode
,
2344 force_omit_system_plugin_path
, force_omit_home_plugin_path
,
2345 initial_plugin_paths
, default_log_level
);
2353 * Prints the convert command usage.
2356 void print_convert_usage(FILE *fp
)
2358 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] [convert] [OPTIONS] [PATH/URL]\n");
2360 fprintf(fp
, "Options:\n");
2362 fprintf(fp
, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2363 fprintf(fp
, " Instantiate the component class CLS of type\n");
2364 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
2365 fprintf(fp
, " in the plugin PLUGIN, add it to the\n");
2366 fprintf(fp
, " conversion graph, and optionally name it\n");
2367 fprintf(fp
, " NAME (you can also specify the name with\n");
2368 fprintf(fp
, " --name)\n");
2369 fprintf(fp
, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
2370 fprintf(fp
, " (`N`, `V`, `D`, `I`, `W`, `E`, or `F`)\n");
2371 fprintf(fp
, " --name=NAME Set the name of the current component\n");
2372 fprintf(fp
, " to NAME (must be unique amongst all the\n");
2373 fprintf(fp
, " names of the created components)\n");
2374 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2375 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
2376 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2377 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2378 fprintf(fp
, " current component (see the expected format\n");
2379 fprintf(fp
, " of PARAMS below)\n");
2380 fprintf(fp
, " -P, --path=PATH Set the `path` string parameter of the\n");
2381 fprintf(fp
, " current component to PATH\n");
2382 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2383 fprintf(fp
, " dynamic plugins can be loaded\n");
2384 fprintf(fp
, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
2385 fprintf(fp
, " the graph later, retry in DUR µs\n");
2386 fprintf(fp
, " (default: 100000)\n");
2387 fprintf(fp
, " dynamic plugins can be loaded\n");
2388 fprintf(fp
, " --run-args Print the equivalent arguments for the\n");
2389 fprintf(fp
, " `run` command to the standard output,\n");
2390 fprintf(fp
, " formatted for a shell, and quit\n");
2391 fprintf(fp
, " --run-args-0 Print the equivalent arguments for the\n");
2392 fprintf(fp
, " `run` command to the standard output,\n");
2393 fprintf(fp
, " formatted for `xargs -0`, and quit\n");
2394 fprintf(fp
, " --stream-intersection Only process events when all streams\n");
2395 fprintf(fp
, " are active\n");
2396 fprintf(fp
, " -u, --url=URL Set the `url` string parameter of the\n");
2397 fprintf(fp
, " current component to URL\n");
2398 fprintf(fp
, " -h, --help Show this help and quit\n");
2400 fprintf(fp
, "Implicit `source.ctf.fs` component options:\n");
2402 fprintf(fp
, " --clock-offset=SEC Set clock offset to SEC seconds\n");
2403 fprintf(fp
, " --clock-offset-ns=NS Set clock offset to NS ns\n");
2405 fprintf(fp
, "Implicit `sink.text.pretty` component options:\n");
2407 fprintf(fp
, " --clock-cycles Print timestamps in clock cycles\n");
2408 fprintf(fp
, " --clock-date Print timestamp dates\n");
2409 fprintf(fp
, " --clock-gmt Print and parse timestamps in the GMT\n");
2410 fprintf(fp
, " time zone instead of the local time zone\n");
2411 fprintf(fp
, " --clock-seconds Print the timestamps as `SEC.NS` instead\n");
2412 fprintf(fp
, " of `hh:mm:ss.nnnnnnnnn`\n");
2413 fprintf(fp
, " --color=(never | auto | always)\n");
2414 fprintf(fp
, " Never, automatically, or always emit\n");
2415 fprintf(fp
, " console color codes\n");
2416 fprintf(fp
, " -f, --fields=FIELD[,FIELD]... Print additional fields; FIELD can be:\n");
2417 fprintf(fp
, " `all`, `trace`, `trace:hostname`,\n");
2418 fprintf(fp
, " `trace:domain`, `trace:procname`,\n");
2419 fprintf(fp
, " `trace:vpid`, `loglevel`, `emf`\n");
2420 fprintf(fp
, " -n, --names=NAME[,NAME]... Print field names; NAME can be:\n");
2421 fprintf(fp
, " `payload` (or `arg` or `args`), `none`,\n");
2422 fprintf(fp
, " `all`, `scope`, `header`, `context`\n");
2423 fprintf(fp
, " (or `ctx`)\n");
2424 fprintf(fp
, " --no-delta Do not print time delta between\n");
2425 fprintf(fp
, " consecutive events\n");
2426 fprintf(fp
, " -w, --output=PATH Write output text to PATH instead of\n");
2427 fprintf(fp
, " the standard output\n");
2429 fprintf(fp
, "Implicit `filter.utils.muxer` component options:\n");
2431 fprintf(fp
, " --clock-force-correlate Assume that clocks are inherently\n");
2432 fprintf(fp
, " correlated across traces\n");
2434 fprintf(fp
, "Implicit `filter.utils.trimmer` component options:\n");
2436 fprintf(fp
, " -b, --begin=BEGIN Set the beginning time of the conversion\n");
2437 fprintf(fp
, " time range to BEGIN (see the format of\n");
2438 fprintf(fp
, " BEGIN below)\n");
2439 fprintf(fp
, " -e, --end=END Set the end time of the conversion time\n");
2440 fprintf(fp
, " range to END (see the format of END below)\n");
2441 fprintf(fp
, " -t, --timerange=TIMERANGE Set conversion time range to TIMERANGE:\n");
2442 fprintf(fp
, " BEGIN,END or [BEGIN,END] (literally `[` and\n");
2443 fprintf(fp
, " `]`) (see the format of BEGIN/END below)\n");
2445 fprintf(fp
, "Implicit `filter.lttng-utils.debug-info` component options:\n");
2447 fprintf(fp
, " --debug-info Create an implicit\n");
2448 fprintf(fp
, " `filter.lttng-utils.debug-info` component\n");
2449 fprintf(fp
, " --debug-info-dir=DIR Search for debug info in directory DIR\n");
2450 fprintf(fp
, " instead of `/usr/lib/debug`\n");
2451 fprintf(fp
, " --debug-info-full-path Show full debug info source and\n");
2452 fprintf(fp
, " binary paths instead of just names\n");
2453 fprintf(fp
, " --debug-info-target-prefix=DIR\n");
2454 fprintf(fp
, " Use directory DIR as a prefix when\n");
2455 fprintf(fp
, " looking up executables during debug\n");
2456 fprintf(fp
, " info analysis\n");
2458 fprintf(fp
, "Legacy options that still work:\n");
2460 fprintf(fp
, " -i, --input-format=(ctf | lttng-live)\n");
2461 fprintf(fp
, " `ctf`:\n");
2462 fprintf(fp
, " Create an implicit `source.ctf.fs`\n");
2463 fprintf(fp
, " component\n");
2464 fprintf(fp
, " `lttng-live`:\n");
2465 fprintf(fp
, " Create an implicit `source.ctf.lttng-live`\n");
2466 fprintf(fp
, " component\n");
2467 fprintf(fp
, " -o, --output-format=(text | ctf | dummy | ctf-metadata)\n");
2468 fprintf(fp
, " `text`:\n");
2469 fprintf(fp
, " Create an implicit `sink.text.pretty`\n");
2470 fprintf(fp
, " component\n");
2471 fprintf(fp
, " `ctf`:\n");
2472 fprintf(fp
, " Create an implicit `sink.ctf.fs`\n");
2473 fprintf(fp
, " component\n");
2474 fprintf(fp
, " `dummy`:\n");
2475 fprintf(fp
, " Create an implicit `sink.utils.dummy`\n");
2476 fprintf(fp
, " component\n");
2477 fprintf(fp
, " `ctf-metadata`:\n");
2478 fprintf(fp
, " Query the `source.ctf.fs` component class\n");
2479 fprintf(fp
, " for metadata text and quit\n");
2481 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
2482 fprintf(fp
, "\n\n");
2483 fprintf(fp
, "Format of BEGIN and END\n");
2484 fprintf(fp
, "-----------------------\n");
2486 fprintf(fp
, " [YYYY-MM-DD [hh:mm:]]ss[.nnnnnnnnn]\n");
2487 fprintf(fp
, "\n\n");
2488 print_expected_params_format(fp
);
2492 const struct bt_argpar_opt_descr convert_options
[] = {
2493 /* id, short_name, long_name, with_arg */
2494 { OPT_BEGIN
, 'b', "begin", true },
2495 { OPT_CLOCK_CYCLES
, '\0', "clock-cycles", false },
2496 { OPT_CLOCK_DATE
, '\0', "clock-date", false },
2497 { OPT_CLOCK_FORCE_CORRELATE
, '\0', "clock-force-correlate", false },
2498 { OPT_CLOCK_GMT
, '\0', "clock-gmt", false },
2499 { OPT_CLOCK_OFFSET
, '\0', "clock-offset", true },
2500 { OPT_CLOCK_OFFSET_NS
, '\0', "clock-offset-ns", true },
2501 { OPT_CLOCK_SECONDS
, '\0', "clock-seconds", false },
2502 { OPT_COLOR
, '\0', "color", true },
2503 { OPT_COMPONENT
, 'c', "component", true },
2504 { OPT_DEBUG
, 'd', "debug", false },
2505 { OPT_DEBUG_INFO_DIR
, '\0', "debug-info-dir", true },
2506 { OPT_DEBUG_INFO_FULL_PATH
, '\0', "debug-info-full-path", false },
2507 { OPT_DEBUG_INFO_TARGET_PREFIX
, '\0', "debug-info-target-prefix", true },
2508 { OPT_END
, 'e', "end", true },
2509 { OPT_FIELDS
, 'f', "fields", true },
2510 { OPT_HELP
, 'h', "help", false },
2511 { OPT_INPUT_FORMAT
, 'i', "input-format", true },
2512 { OPT_LOG_LEVEL
, 'l', "log-level", true },
2513 { OPT_NAME
, '\0', "name", true },
2514 { OPT_NAMES
, 'n', "names", true },
2515 { OPT_DEBUG_INFO
, '\0', "debug-info", false },
2516 { OPT_NO_DELTA
, '\0', "no-delta", false },
2517 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
2518 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
2519 { OPT_OUTPUT
, 'w', "output", true },
2520 { OPT_OUTPUT_FORMAT
, 'o', "output-format", true },
2521 { OPT_PARAMS
, 'p', "params", true },
2522 { OPT_PATH
, 'P', "path", true },
2523 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
2524 { OPT_RETRY_DURATION
, '\0', "retry-duration", true },
2525 { OPT_RUN_ARGS
, '\0', "run-args", false },
2526 { OPT_RUN_ARGS_0
, '\0', "run-args-0", false },
2527 { OPT_STREAM_INTERSECTION
, '\0', "stream-intersection", false },
2528 { OPT_TIMERANGE
, '\0', "timerange", true },
2529 { OPT_URL
, 'u', "url", true },
2530 { OPT_VERBOSE
, 'v', "verbose", false },
2531 BT_ARGPAR_OPT_DESCR_SENTINEL
2535 GString
*get_component_auto_name(const char *prefix
,
2536 const bt_value
*existing_names
)
2539 GString
*auto_name
= g_string_new(NULL
);
2542 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2546 if (!bt_value_map_has_entry(existing_names
, prefix
)) {
2547 g_string_assign(auto_name
, prefix
);
2552 g_string_printf(auto_name
, "%s-%d", prefix
, i
);
2554 } while (bt_value_map_has_entry(existing_names
, auto_name
->str
));
2560 struct implicit_component_args
{
2563 /* The component class name (e.g. src.ctf.fs). */
2566 /* The component instance name. */
2569 GString
*params_arg
;
2570 bt_value
*extra_params
;
2574 int assign_name_to_implicit_component(struct implicit_component_args
*args
,
2575 const char *prefix
, bt_value
*existing_names
,
2576 GList
**comp_names
, bool append_to_comp_names
)
2579 GString
*name
= NULL
;
2581 if (!args
->exists
) {
2585 name
= get_component_auto_name(prefix
,
2593 g_string_assign(args
->name_arg
, name
->str
);
2595 if (bt_value_map_insert_entry(existing_names
, name
->str
,
2597 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2602 if (append_to_comp_names
) {
2603 *comp_names
= g_list_append(*comp_names
, name
);
2609 g_string_free(name
, TRUE
);
2616 int append_run_args_for_implicit_component(
2617 struct implicit_component_args
*impl_args
,
2623 if (!impl_args
->exists
) {
2627 if (bt_value_array_append_string_element(run_args
, "--component")) {
2628 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2632 if (bt_value_array_append_string_element(run_args
, impl_args
->comp_arg
->str
)) {
2633 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2637 if (bt_value_array_append_string_element(run_args
, "--name")) {
2638 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2642 if (bt_value_array_append_string_element(run_args
, impl_args
->name_arg
->str
)) {
2643 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2647 if (impl_args
->params_arg
->len
> 0) {
2648 if (bt_value_array_append_string_element(run_args
, "--params")) {
2649 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2653 if (bt_value_array_append_string_element(run_args
,
2654 impl_args
->params_arg
->str
)) {
2655 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2660 for (i
= 0; i
< bt_value_array_get_size(impl_args
->extra_params
);
2662 const bt_value
*elem
;
2665 elem
= bt_value_array_borrow_element_by_index(impl_args
->extra_params
,
2671 BT_ASSERT(bt_value_is_string(elem
));
2672 arg
= bt_value_string_get(elem
);
2673 ret
= bt_value_array_append_string_element(run_args
, arg
);
2675 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2689 /* Free the fields of a `struct implicit_component_args`. */
2692 void finalize_implicit_component_args(struct implicit_component_args
*args
)
2696 if (args
->comp_arg
) {
2697 g_string_free(args
->comp_arg
, TRUE
);
2700 if (args
->name_arg
) {
2701 g_string_free(args
->name_arg
, TRUE
);
2704 if (args
->params_arg
) {
2705 g_string_free(args
->params_arg
, TRUE
);
2708 bt_value_put_ref(args
->extra_params
);
2711 /* Destroy a dynamically-allocated `struct implicit_component_args`. */
2714 void destroy_implicit_component_args(struct implicit_component_args
*args
)
2716 finalize_implicit_component_args(args
);
2720 /* Initialize the fields of an already allocated `struct implicit_component_args`. */
2723 int init_implicit_component_args(struct implicit_component_args
*args
,
2724 const char *comp_arg
, bool exists
)
2728 args
->exists
= exists
;
2729 args
->comp_arg
= g_string_new(comp_arg
);
2730 args
->name_arg
= g_string_new(NULL
);
2731 args
->params_arg
= g_string_new(NULL
);
2732 args
->extra_params
= bt_value_array_create();
2734 if (!args
->comp_arg
|| !args
->name_arg
||
2735 !args
->params_arg
|| !args
->extra_params
) {
2737 finalize_implicit_component_args(args
);
2738 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2746 /* Dynamically allocate and initialize a `struct implicit_component_args`. */
2749 struct implicit_component_args
*create_implicit_component_args(
2750 const char *comp_arg
)
2752 struct implicit_component_args
*args
;
2755 args
= g_new(struct implicit_component_args
, 1);
2757 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2761 status
= init_implicit_component_args(args
, comp_arg
, true);
2772 void append_implicit_component_param(struct implicit_component_args
*args
,
2773 const char *key
, const char *value
)
2778 append_param_arg(args
->params_arg
, key
, value
);
2782 * Append the given parameter (`key=value`) to all component specifications
2783 * in `implicit_comp_args` (an array of `struct implicit_component_args *`)
2784 * which match `comp_arg`.
2786 * Return the number of matching components.
2790 int append_multiple_implicit_components_param(GPtrArray
*implicit_comp_args
,
2791 const char *comp_arg
, const char *key
, const char *value
)
2796 for (i
= 0; i
< implicit_comp_args
->len
; i
++) {
2797 struct implicit_component_args
*args
= implicit_comp_args
->pdata
[i
];
2799 if (strcmp(args
->comp_arg
->str
, comp_arg
) == 0) {
2800 append_implicit_component_param(args
, key
, value
);
2808 /* Escape value to make it suitable to use as a string parameter value. */
2810 gchar
*escape_string_value(const char *value
)
2815 ret
= g_string_new(NULL
);
2817 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2826 g_string_append_c(ret
, '\\');
2830 g_string_append_c(ret
, *in
);
2836 return g_string_free(ret
, FALSE
);
2840 int bt_value_to_cli_param_value_append(const bt_value
*value
, GString
*buf
)
2846 switch (bt_value_get_type(value
)) {
2847 case BT_VALUE_TYPE_STRING
:
2849 const char *str_value
= bt_value_string_get(value
);
2850 gchar
*escaped_str_value
;
2852 escaped_str_value
= escape_string_value(str_value
);
2853 if (!escaped_str_value
) {
2857 g_string_append_printf(buf
, "\"%s\"", escaped_str_value
);
2859 g_free(escaped_str_value
);
2862 case BT_VALUE_TYPE_ARRAY
: {
2863 g_string_append_c(buf
, '[');
2864 uint64_t sz
= bt_value_array_get_size(value
);
2865 for (uint64_t i
= 0; i
< sz
; i
++) {
2866 const bt_value
*item
;
2870 g_string_append(buf
, ", ");
2873 item
= bt_value_array_borrow_element_by_index_const(
2875 ret
= bt_value_to_cli_param_value_append(item
, buf
);
2881 g_string_append_c(buf
, ']');
2895 * Convert `value` to its equivalent representation as a command line parameter
2900 gchar
*bt_value_to_cli_param_value(bt_value
*value
)
2903 gchar
*result
= NULL
;
2905 buf
= g_string_new(NULL
);
2907 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2911 if (bt_value_to_cli_param_value_append(value
, buf
)) {
2915 result
= g_string_free(buf
, FALSE
);
2922 g_string_free(buf
, TRUE
);
2930 int append_parameter_to_args(bt_value
*args
, const char *key
, bt_value
*value
)
2933 BT_ASSERT(bt_value_get_type(args
) == BT_VALUE_TYPE_ARRAY
);
2938 gchar
*str_value
= NULL
;
2939 GString
*parameter
= NULL
;
2941 if (bt_value_array_append_string_element(args
, "--params")) {
2942 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2947 str_value
= bt_value_to_cli_param_value(value
);
2953 parameter
= g_string_new(NULL
);
2955 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2960 g_string_printf(parameter
, "%s=%s", key
, str_value
);
2962 if (bt_value_array_append_string_element(args
, parameter
->str
)) {
2963 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2970 g_string_free(parameter
, TRUE
);
2983 int append_string_parameter_to_args(bt_value
*args
, const char *key
, const char *value
)
2985 bt_value
*str_value
;
2988 str_value
= bt_value_string_create_init(value
);
2991 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2996 ret
= append_parameter_to_args(args
, key
, str_value
);
2999 BT_VALUE_PUT_REF_AND_RESET(str_value
);
3004 int append_implicit_component_extra_param(struct implicit_component_args
*args
,
3005 const char *key
, const char *value
)
3007 return append_string_parameter_to_args(args
->extra_params
, key
, value
);
3011 int convert_append_name_param(enum bt_config_component_dest dest
,
3012 GString
*cur_name
, GString
*cur_name_prefix
,
3014 bt_value
*all_names
,
3015 GList
**source_names
, GList
**filter_names
,
3020 if (cur_name_prefix
->len
> 0) {
3021 /* We're after a --component option */
3022 GString
*name
= NULL
;
3023 bool append_name_opt
= false;
3025 if (cur_name
->len
== 0) {
3027 * No explicit name was provided for the user
3030 name
= get_component_auto_name(cur_name_prefix
->str
,
3032 append_name_opt
= true;
3035 * An explicit name was provided for the user
3038 if (bt_value_map_has_entry(all_names
,
3040 BT_CLI_LOGE_APPEND_CAUSE("Duplicate component instance name:\n %s",
3045 name
= g_string_new(cur_name
->str
);
3049 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3054 * Remember this name globally, for the uniqueness of
3055 * all component names.
3057 if (bt_value_map_insert_entry(all_names
, name
->str
, bt_value_null
)) {
3058 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3063 * Append the --name option if necessary.
3065 if (append_name_opt
) {
3066 if (bt_value_array_append_string_element(run_args
, "--name")) {
3067 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3071 if (bt_value_array_append_string_element(run_args
, name
->str
)) {
3072 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3078 * Remember this name specifically for the type of the
3079 * component. This is to create connection arguments.
3082 case BT_CONFIG_COMPONENT_DEST_SOURCE
:
3083 *source_names
= g_list_append(*source_names
, name
);
3085 case BT_CONFIG_COMPONENT_DEST_FILTER
:
3086 *filter_names
= g_list_append(*filter_names
, name
);
3088 case BT_CONFIG_COMPONENT_DEST_SINK
:
3089 *sink_names
= g_list_append(*sink_names
, name
);
3095 g_string_assign(cur_name_prefix
, "");
3108 * Escapes `.`, `:`, and `\` of `input` with `\`.
3111 GString
*escape_dot_colon(const char *input
)
3113 GString
*output
= g_string_new(NULL
);
3117 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3121 for (ch
= input
; *ch
!= '\0'; ch
++) {
3122 if (*ch
== '\\' || *ch
== '.' || *ch
== ':') {
3123 g_string_append_c(output
, '\\');
3126 g_string_append_c(output
, *ch
);
3134 * Appends a --connect option to a list of arguments. `upstream_name`
3135 * and `downstream_name` are escaped with escape_dot_colon() in this
3139 int append_connect_arg(bt_value
*run_args
,
3140 const char *upstream_name
, const char *downstream_name
)
3143 GString
*e_upstream_name
= escape_dot_colon(upstream_name
);
3144 GString
*e_downstream_name
= escape_dot_colon(downstream_name
);
3145 GString
*arg
= g_string_new(NULL
);
3147 if (!e_upstream_name
|| !e_downstream_name
|| !arg
) {
3148 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3153 ret
= bt_value_array_append_string_element(run_args
, "--connect");
3155 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3160 g_string_append(arg
, e_upstream_name
->str
);
3161 g_string_append_c(arg
, ':');
3162 g_string_append(arg
, e_downstream_name
->str
);
3163 ret
= bt_value_array_append_string_element(run_args
, arg
->str
);
3165 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3172 g_string_free(arg
, TRUE
);
3175 if (e_upstream_name
) {
3176 g_string_free(e_upstream_name
, TRUE
);
3179 if (e_downstream_name
) {
3180 g_string_free(e_downstream_name
, TRUE
);
3187 * Appends the run command's --connect options for the convert command.
3190 int convert_auto_connect(bt_value
*run_args
,
3191 GList
*source_names
, GList
*filter_names
,
3195 GList
*source_at
= source_names
;
3196 GList
*filter_at
= filter_names
;
3198 GList
*sink_at
= sink_names
;
3200 BT_ASSERT(source_names
);
3201 BT_ASSERT(filter_names
);
3202 BT_ASSERT(sink_names
);
3204 /* Connect all sources to the first filter */
3205 for (source_at
= source_names
; source_at
; source_at
= g_list_next(source_at
)) {
3206 GString
*source_name
= source_at
->data
;
3207 GString
*filter_name
= filter_at
->data
;
3209 ret
= append_connect_arg(run_args
, source_name
->str
,
3216 filter_prev
= filter_at
;
3217 filter_at
= g_list_next(filter_at
);
3219 /* Connect remaining filters */
3220 for (; filter_at
; filter_prev
= filter_at
, filter_at
= g_list_next(filter_at
)) {
3221 GString
*filter_name
= filter_at
->data
;
3222 GString
*filter_prev_name
= filter_prev
->data
;
3224 ret
= append_connect_arg(run_args
, filter_prev_name
->str
,
3231 /* Connect last filter to all sinks */
3232 for (sink_at
= sink_names
; sink_at
; sink_at
= g_list_next(sink_at
)) {
3233 GString
*filter_name
= filter_prev
->data
;
3234 GString
*sink_name
= sink_at
->data
;
3236 ret
= append_connect_arg(run_args
, filter_name
->str
,
3253 int split_timerange(const char *arg
, char **begin
, char **end
)
3256 const char *ch
= arg
;
3258 GString
*g_begin
= NULL
;
3259 GString
*g_end
= NULL
;
3267 g_begin
= bt_common_string_until(ch
, "", ",", &end_pos
);
3268 if (!g_begin
|| ch
[end_pos
] != ',' || g_begin
->len
== 0) {
3274 g_end
= bt_common_string_until(ch
, "", "]", &end_pos
);
3275 if (!g_end
|| g_end
->len
== 0) {
3281 *begin
= g_begin
->str
;
3283 g_string_free(g_begin
, FALSE
);
3284 g_string_free(g_end
, FALSE
);
3294 g_string_free(g_begin
, TRUE
);
3298 g_string_free(g_end
, TRUE
);
3305 int g_list_prepend_gstring(GList
**list
, const char *string
)
3308 GString
*gs
= g_string_new(string
);
3313 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3317 *list
= g_list_prepend(*list
, gs
);
3324 * Create `struct implicit_component_args` structures for each of the source
3325 * components we identified. Add them to `component_args`.
3329 void create_implicit_component_args_from_auto_discovered_sources(
3330 const struct auto_source_discovery
*auto_disc
, GPtrArray
*component_args
)
3332 gchar
*cc_name
= NULL
;
3333 struct implicit_component_args
*comp
= NULL
;
3337 len
= auto_disc
->results
->len
;
3339 for (i
= 0; i
< len
; i
++) {
3340 struct auto_source_discovery_result
*res
=
3341 g_ptr_array_index(auto_disc
->results
, i
);
3344 cc_name
= g_strdup_printf("source.%s.%s", res
->plugin_name
, res
->source_cc_name
);
3346 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3350 comp
= create_implicit_component_args(cc_name
);
3355 status
= append_parameter_to_args(comp
->extra_params
, "inputs", res
->inputs
);
3360 g_ptr_array_add(component_args
, comp
);
3368 destroy_implicit_component_args(comp
);
3373 * Creates a Babeltrace config object from the arguments of a convert
3376 * *retcode is set to the appropriate exit code to use.
3379 struct bt_config
*bt_config_convert_from_args(int argc
, const char *argv
[],
3380 int *retcode
, bool force_omit_system_plugin_path
,
3381 bool force_omit_home_plugin_path
,
3382 const bt_value
*initial_plugin_paths
, int *default_log_level
)
3384 enum bt_config_component_dest cur_comp_dest
=
3385 BT_CONFIG_COMPONENT_DEST_UNKNOWN
;
3387 struct bt_config
*cfg
= NULL
;
3388 bool got_input_format_opt
= false;
3389 bool got_output_format_opt
= false;
3390 bool trimmer_has_begin
= false;
3391 bool trimmer_has_end
= false;
3392 bool stream_intersection_mode
= false;
3393 GString
*cur_name
= NULL
;
3394 GString
*cur_name_prefix
= NULL
;
3395 bool print_run_args
= false;
3396 bool print_run_args_0
= false;
3397 bool print_ctf_metadata
= false;
3398 bt_value
*run_args
= NULL
;
3399 bt_value
*all_names
= NULL
;
3400 GList
*source_names
= NULL
;
3401 GList
*filter_names
= NULL
;
3402 GList
*sink_names
= NULL
;
3403 bt_value
*leftovers
= NULL
;
3404 struct implicit_component_args implicit_ctf_output_args
= { 0 };
3405 struct implicit_component_args implicit_lttng_live_args
= { 0 };
3406 struct implicit_component_args implicit_dummy_args
= { 0 };
3407 struct implicit_component_args implicit_text_args
= { 0 };
3408 struct implicit_component_args implicit_debug_info_args
= { 0 };
3409 struct implicit_component_args implicit_muxer_args
= { 0 };
3410 struct implicit_component_args implicit_trimmer_args
= { 0 };
3411 bt_value
*plugin_paths
;
3412 char error_buf
[256] = { 0 };
3414 struct bt_common_lttng_live_url_parts lttng_live_url_parts
= { 0 };
3415 char *output
= NULL
;
3416 struct auto_source_discovery auto_disc
= { NULL
};
3417 GString
*auto_disc_comp_name
= NULL
;
3418 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
3421 * Array of `struct implicit_component_args *` created for the sources
3422 * we have auto-discovered.
3424 GPtrArray
*discovered_source_args
= NULL
;
3427 * If set, restrict automatic source discovery to this component class
3430 const char *auto_source_discovery_restrict_plugin_name
= NULL
;
3431 const char *auto_source_discovery_restrict_component_class_name
= NULL
;
3433 gchar
*ctf_fs_source_clock_class_offset_arg
= NULL
;
3434 gchar
*ctf_fs_source_clock_class_offset_ns_arg
= NULL
;
3436 (void) bt_value_copy(initial_plugin_paths
, &plugin_paths
);
3441 print_convert_usage(stdout
);
3446 if (init_implicit_component_args(&implicit_ctf_output_args
,
3447 "sink.ctf.fs", false)) {
3451 if (init_implicit_component_args(&implicit_lttng_live_args
,
3452 "source.ctf.lttng-live", false)) {
3456 if (init_implicit_component_args(&implicit_text_args
,
3457 "sink.text.pretty", false)) {
3461 if (init_implicit_component_args(&implicit_dummy_args
,
3462 "sink.utils.dummy", false)) {
3466 if (init_implicit_component_args(&implicit_debug_info_args
,
3467 "filter.lttng-utils.debug-info", false)) {
3471 if (init_implicit_component_args(&implicit_muxer_args
,
3472 "filter.utils.muxer", true)) {
3476 if (init_implicit_component_args(&implicit_trimmer_args
,
3477 "filter.utils.trimmer", false)) {
3481 all_names
= bt_value_map_create();
3483 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3487 run_args
= bt_value_array_create();
3489 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3493 cur_name
= g_string_new(NULL
);
3495 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3499 cur_name_prefix
= g_string_new(NULL
);
3500 if (!cur_name_prefix
) {
3501 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3505 ret
= append_env_var_plugin_paths(plugin_paths
);
3510 leftovers
= bt_value_array_create();
3512 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3516 if (auto_source_discovery_init(&auto_disc
) != 0) {
3520 discovered_source_args
=
3521 g_ptr_array_new_with_free_func((GDestroyNotify
) destroy_implicit_component_args
);
3522 if (!discovered_source_args
) {
3523 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3527 auto_disc_comp_name
= g_string_new(NULL
);
3528 if (!auto_disc_comp_name
) {
3529 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3534 * First pass: collect all arguments which need to be passed
3535 * as is to the run command. This pass can also add --name
3536 * arguments if needed to automatically name unnamed component
3537 * instances. Also it does the following transformations:
3539 * --path=PATH -> --params=path="PATH"
3540 * --url=URL -> --params=url="URL"
3542 * Also it appends the plugin paths of --plugin-path to
3545 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, convert_options
, true);
3546 if (argpar_parse_ret
.error
) {
3547 BT_CLI_LOGE_APPEND_CAUSE(
3548 "While parsing `convert` command's command-line arguments: %s",
3549 argpar_parse_ret
.error
->str
);
3553 if (help_option_is_specified(&argpar_parse_ret
)) {
3554 print_convert_usage(stdout
);
3556 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
3560 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
3561 struct bt_argpar_item
*argpar_item
=
3562 g_ptr_array_index(argpar_parse_ret
.items
, i
);
3563 struct bt_argpar_item_opt
*argpar_item_opt
;
3565 char *plugin_name
= NULL
;
3566 char *comp_cls_name
= NULL
;
3569 if (argpar_item
->type
!= BT_ARGPAR_ITEM_TYPE_OPT
) {
3573 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
3574 arg
= argpar_item_opt
->arg
;
3576 switch (argpar_item_opt
->descr
->id
) {
3579 bt_component_class_type type
;
3580 const char *type_prefix
;
3582 /* Append current component's name if needed */
3583 ret
= convert_append_name_param(cur_comp_dest
, cur_name
,
3584 cur_name_prefix
, run_args
, all_names
,
3585 &source_names
, &filter_names
, &sink_names
);
3590 /* Parse the argument */
3591 plugin_comp_cls_names(arg
, &name
, &plugin_name
,
3592 &comp_cls_name
, &type
);
3593 if (!plugin_name
|| !comp_cls_name
) {
3594 BT_CLI_LOGE_APPEND_CAUSE(
3595 "Invalid format for --component option's argument:\n %s",
3601 g_string_assign(cur_name
, name
);
3603 g_string_assign(cur_name
, "");
3607 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
3608 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
3609 type_prefix
= "source";
3611 case BT_COMPONENT_CLASS_TYPE_FILTER
:
3612 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_FILTER
;
3613 type_prefix
= "filter";
3615 case BT_COMPONENT_CLASS_TYPE_SINK
:
3616 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
3617 type_prefix
= "sink";
3623 if (bt_value_array_append_string_element(run_args
,
3625 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3629 if (bt_value_array_append_string_element(run_args
, arg
)) {
3630 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3634 g_string_assign(cur_name_prefix
, "");
3635 g_string_append_printf(cur_name_prefix
, "%s.%s.%s",
3636 type_prefix
, plugin_name
, comp_cls_name
);
3639 free(comp_cls_name
);
3642 comp_cls_name
= NULL
;
3646 if (cur_name_prefix
->len
== 0) {
3647 BT_CLI_LOGE_APPEND_CAUSE("No current component of which to set parameters:\n %s",
3652 if (bt_value_array_append_string_element(run_args
,
3654 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3658 if (bt_value_array_append_string_element(run_args
, arg
)) {
3659 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3664 if (cur_name_prefix
->len
== 0) {
3665 BT_CLI_LOGE_APPEND_CAUSE("No current component of which to set `path` parameter:\n %s",
3670 if (append_string_parameter_to_args(run_args
, "path", arg
)) {
3675 if (cur_name_prefix
->len
== 0) {
3676 BT_CLI_LOGE_APPEND_CAUSE("No current component of which to set `url` parameter:\n %s",
3682 if (append_string_parameter_to_args(run_args
, "url", arg
)) {
3687 if (cur_name_prefix
->len
== 0) {
3688 BT_CLI_LOGE_APPEND_CAUSE("No current component to name:\n %s",
3693 if (bt_value_array_append_string_element(run_args
, "--name")) {
3694 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3698 if (bt_value_array_append_string_element(run_args
, arg
)) {
3699 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3703 g_string_assign(cur_name
, arg
);
3706 if (cur_name_prefix
->len
== 0) {
3707 BT_CLI_LOGE_APPEND_CAUSE("No current component to assign a log level to:\n %s",
3712 if (bt_value_array_append_string_element(run_args
, "--log-level")) {
3713 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3717 if (bt_value_array_append_string_element(run_args
, arg
)) {
3718 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3723 case OPT_OMIT_HOME_PLUGIN_PATH
:
3724 force_omit_home_plugin_path
= true;
3726 if (bt_value_array_append_string_element(run_args
,
3727 "--omit-home-plugin-path")) {
3728 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3732 case OPT_RETRY_DURATION
:
3733 if (bt_value_array_append_string_element(run_args
,
3734 "--retry-duration")) {
3735 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3739 if (bt_value_array_append_string_element(run_args
, arg
)) {
3740 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3744 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
3745 force_omit_system_plugin_path
= true;
3747 if (bt_value_array_append_string_element(run_args
,
3748 "--omit-system-plugin-path")) {
3749 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3753 case OPT_PLUGIN_PATH
:
3754 if (bt_config_append_plugin_paths_check_setuid_setgid(
3755 plugin_paths
, arg
)) {
3759 if (bt_value_array_append_string_element(run_args
,
3761 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3765 if (bt_value_array_append_string_element(run_args
, arg
)) {
3766 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3771 case OPT_CLOCK_CYCLES
:
3772 case OPT_CLOCK_DATE
:
3773 case OPT_CLOCK_FORCE_CORRELATE
:
3775 case OPT_CLOCK_OFFSET
:
3776 case OPT_CLOCK_OFFSET_NS
:
3777 case OPT_CLOCK_SECONDS
:
3780 case OPT_DEBUG_INFO
:
3781 case OPT_DEBUG_INFO_DIR
:
3782 case OPT_DEBUG_INFO_FULL_PATH
:
3783 case OPT_DEBUG_INFO_TARGET_PREFIX
:
3786 case OPT_INPUT_FORMAT
:
3789 case OPT_OUTPUT_FORMAT
:
3792 case OPT_RUN_ARGS_0
:
3793 case OPT_STREAM_INTERSECTION
:
3796 /* Ignore in this pass */
3799 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
3800 argpar_item_opt
->descr
->id
);
3805 /* Append current component's name if needed */
3806 ret
= convert_append_name_param(cur_comp_dest
, cur_name
,
3807 cur_name_prefix
, run_args
, all_names
, &source_names
,
3808 &filter_names
, &sink_names
);
3814 * Second pass: transform the convert-specific options and
3815 * arguments into implicit component instances for the run
3818 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
3819 struct bt_argpar_item
*argpar_item
=
3820 g_ptr_array_index(argpar_parse_ret
.items
, i
);
3821 struct bt_argpar_item_opt
*argpar_item_opt
;
3824 if (argpar_item
->type
!= BT_ARGPAR_ITEM_TYPE_OPT
) {
3828 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
3829 arg
= argpar_item_opt
->arg
;
3831 switch (argpar_item_opt
->descr
->id
) {
3833 if (trimmer_has_begin
) {
3834 printf("At --begin option: --begin or --timerange option already specified\n %s\n",
3839 trimmer_has_begin
= true;
3840 ret
= append_implicit_component_extra_param(
3841 &implicit_trimmer_args
, "begin", arg
);
3842 implicit_trimmer_args
.exists
= true;
3848 if (trimmer_has_end
) {
3849 printf("At --end option: --end or --timerange option already specified\n %s\n",
3854 trimmer_has_end
= true;
3855 ret
= append_implicit_component_extra_param(
3856 &implicit_trimmer_args
, "end", arg
);
3857 implicit_trimmer_args
.exists
= true;
3867 if (trimmer_has_begin
|| trimmer_has_end
) {
3868 printf("At --timerange option: --begin, --end, or --timerange option already specified\n %s\n",
3873 ret
= split_timerange(arg
, &begin
, &end
);
3875 BT_CLI_LOGE_APPEND_CAUSE("Invalid --timerange option's argument: expecting BEGIN,END or [BEGIN,END]:\n %s",
3880 ret
= append_implicit_component_extra_param(
3881 &implicit_trimmer_args
, "begin", begin
);
3882 ret
|= append_implicit_component_extra_param(
3883 &implicit_trimmer_args
, "end", end
);
3884 implicit_trimmer_args
.exists
= true;
3892 case OPT_CLOCK_CYCLES
:
3893 append_implicit_component_param(
3894 &implicit_text_args
, "clock-cycles", "yes");
3895 implicit_text_args
.exists
= true;
3897 case OPT_CLOCK_DATE
:
3898 append_implicit_component_param(
3899 &implicit_text_args
, "clock-date", "yes");
3900 implicit_text_args
.exists
= true;
3902 case OPT_CLOCK_FORCE_CORRELATE
:
3903 append_implicit_component_param(
3904 &implicit_muxer_args
,
3905 "assume-absolute-clock-classes", "yes");
3908 append_implicit_component_param(
3909 &implicit_text_args
, "clock-gmt", "yes");
3910 append_implicit_component_param(
3911 &implicit_trimmer_args
, "gmt", "yes");
3912 implicit_text_args
.exists
= true;
3914 case OPT_CLOCK_OFFSET
:
3915 if (ctf_fs_source_clock_class_offset_arg
) {
3916 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --clock-offset option\n");
3920 ctf_fs_source_clock_class_offset_arg
= g_strdup(arg
);
3921 if (!ctf_fs_source_clock_class_offset_arg
) {
3922 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3926 case OPT_CLOCK_OFFSET_NS
:
3927 if (ctf_fs_source_clock_class_offset_ns_arg
) {
3928 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --clock-offset-ns option\n");
3932 ctf_fs_source_clock_class_offset_ns_arg
= g_strdup(arg
);
3933 if (!ctf_fs_source_clock_class_offset_ns_arg
) {
3934 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3938 case OPT_CLOCK_SECONDS
:
3939 append_implicit_component_param(
3940 &implicit_text_args
, "clock-seconds", "yes");
3941 implicit_text_args
.exists
= true;
3944 implicit_text_args
.exists
= true;
3945 ret
= append_implicit_component_extra_param(
3946 &implicit_text_args
, "color", arg
);
3951 case OPT_DEBUG_INFO
:
3952 implicit_debug_info_args
.exists
= true;
3954 case OPT_DEBUG_INFO_DIR
:
3955 implicit_debug_info_args
.exists
= true;
3956 ret
= append_implicit_component_extra_param(
3957 &implicit_debug_info_args
, "debug-info-dir", arg
);
3962 case OPT_DEBUG_INFO_FULL_PATH
:
3963 implicit_debug_info_args
.exists
= true;
3964 append_implicit_component_param(
3965 &implicit_debug_info_args
, "full-path", "yes");
3967 case OPT_DEBUG_INFO_TARGET_PREFIX
:
3968 implicit_debug_info_args
.exists
= true;
3969 ret
= append_implicit_component_extra_param(
3970 &implicit_debug_info_args
,
3971 "target-prefix", arg
);
3978 bt_value
*fields
= fields_from_arg(arg
);
3984 implicit_text_args
.exists
= true;
3985 ret
= insert_flat_params_from_array(
3986 implicit_text_args
.params_arg
,
3988 bt_value_put_ref(fields
);
3996 bt_value
*names
= names_from_arg(arg
);
4002 implicit_text_args
.exists
= true;
4003 ret
= insert_flat_params_from_array(
4004 implicit_text_args
.params_arg
,
4006 bt_value_put_ref(names
);
4013 append_implicit_component_param(
4014 &implicit_text_args
, "no-delta", "yes");
4015 implicit_text_args
.exists
= true;
4017 case OPT_INPUT_FORMAT
:
4018 if (got_input_format_opt
) {
4019 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --input-format option.");
4023 got_input_format_opt
= true;
4025 if (strcmp(arg
, "ctf") == 0) {
4026 auto_source_discovery_restrict_plugin_name
= "ctf";
4027 auto_source_discovery_restrict_component_class_name
= "fs";
4028 } else if (strcmp(arg
, "lttng-live") == 0) {
4029 auto_source_discovery_restrict_plugin_name
= "ctf";
4030 auto_source_discovery_restrict_component_class_name
= "lttng-live";
4031 implicit_lttng_live_args
.exists
= true;
4033 BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy input format:\n %s",
4038 case OPT_OUTPUT_FORMAT
:
4039 if (got_output_format_opt
) {
4040 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output-format option.");
4044 got_output_format_opt
= true;
4046 if (strcmp(arg
, "text") == 0) {
4047 implicit_text_args
.exists
= true;
4048 } else if (strcmp(arg
, "ctf") == 0) {
4049 implicit_ctf_output_args
.exists
= true;
4050 } else if (strcmp(arg
, "dummy") == 0) {
4051 implicit_dummy_args
.exists
= true;
4052 } else if (strcmp(arg
, "ctf-metadata") == 0) {
4053 print_ctf_metadata
= true;
4055 BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy output format:\n %s",
4062 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output option");
4066 output
= strdup(arg
);
4068 BT_CLI_LOGE_APPEND_CAUSE_OOM();
4073 if (print_run_args_0
) {
4074 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0.");
4078 print_run_args
= true;
4080 case OPT_RUN_ARGS_0
:
4081 if (print_run_args
) {
4082 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0.");
4086 print_run_args_0
= true;
4088 case OPT_STREAM_INTERSECTION
:
4090 * Applies to all traces implementing the
4091 * babeltrace.trace-info query.
4093 stream_intersection_mode
= true;
4096 if (*default_log_level
!= BT_LOG_TRACE
&&
4097 *default_log_level
!= BT_LOG_DEBUG
) {
4098 *default_log_level
= BT_LOG_INFO
;
4102 *default_log_level
= BT_LOG_TRACE
;
4110 * Legacy behaviour: --verbose used to make the `text` output
4111 * format print more information. --verbose is now equivalent to
4112 * the INFO log level, which is why we compare to `BT_LOG_INFO`
4115 if (*default_log_level
== BT_LOG_INFO
) {
4116 append_implicit_component_param(&implicit_text_args
,
4121 * Append home and system plugin paths now that we possibly got
4124 if (append_home_and_system_plugin_paths(plugin_paths
,
4125 force_omit_system_plugin_path
,
4126 force_omit_home_plugin_path
)) {
4130 /* Consume and keep leftover arguments */
4131 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
4132 struct bt_argpar_item
*argpar_item
=
4133 g_ptr_array_index(argpar_parse_ret
.items
, i
);
4134 struct bt_argpar_item_non_opt
*argpar_item_non_opt
;
4136 if (argpar_item
->type
!= BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
4140 argpar_item_non_opt
= (struct bt_argpar_item_non_opt
*) argpar_item
;
4142 if (bt_value_array_append_string_element(leftovers
, argpar_item_non_opt
->arg
) !=
4143 BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
4144 BT_CLI_LOGE_APPEND_CAUSE_OOM();
4149 /* Print CTF metadata or print LTTng live sessions */
4150 if (print_ctf_metadata
) {
4151 const bt_value
*bt_val_leftover
;
4153 if (bt_value_array_is_empty(leftovers
)) {
4154 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf-metadata specified without a path.");
4158 if (bt_value_array_get_size(leftovers
) > 1) {
4159 BT_CLI_LOGE_APPEND_CAUSE("Too many paths specified for --output-format=ctf-metadata.");
4163 cfg
= bt_config_print_ctf_metadata_create(plugin_paths
);
4168 bt_val_leftover
= bt_value_array_borrow_element_by_index_const(leftovers
, 0);
4169 g_string_assign(cfg
->cmd_data
.print_ctf_metadata
.path
,
4170 bt_value_string_get(bt_val_leftover
));
4174 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
4182 * If -o ctf was specified, make sure an output path (--output)
4183 * was also specified. --output does not imply -o ctf because
4184 * it's also used for the default, implicit -o text if -o ctf
4187 if (implicit_ctf_output_args
.exists
) {
4189 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf specified without --output (trace output path).");
4194 * At this point we know that -o ctf AND --output were
4195 * specified. Make sure that no options were specified
4196 * which would imply -o text because --output would be
4197 * ambiguous in this case. For example, this is wrong:
4199 * babeltrace2 --names=all -o ctf --output=/tmp/path my-trace
4201 * because --names=all implies -o text, and --output
4202 * could apply to both the sink.text.pretty and
4203 * sink.ctf.fs implicit components.
4205 if (implicit_text_args
.exists
) {
4206 BT_CLI_LOGE_APPEND_CAUSE("Ambiguous --output option: --output-format=ctf specified but another option implies --output-format=text.");
4212 * If -o dummy and -o ctf were not specified, and if there are
4213 * no explicit sink components, then use an implicit
4214 * `sink.text.pretty` component.
4216 if (!implicit_dummy_args
.exists
&& !implicit_ctf_output_args
.exists
&&
4218 implicit_text_args
.exists
= true;
4222 * Set implicit `sink.text.pretty` or `sink.ctf.fs` component's
4223 * `path` parameter if --output was specified.
4226 if (implicit_text_args
.exists
) {
4227 append_implicit_component_extra_param(&implicit_text_args
,
4229 } else if (implicit_ctf_output_args
.exists
) {
4230 append_implicit_component_extra_param(&implicit_ctf_output_args
,
4235 /* Decide where the leftover argument(s) go */
4236 if (bt_value_array_get_size(leftovers
) > 0) {
4237 if (implicit_lttng_live_args
.exists
) {
4238 const bt_value
*bt_val_leftover
;
4240 if (bt_value_array_get_size(leftovers
) > 1) {
4241 BT_CLI_LOGE_APPEND_CAUSE("Too many URLs specified for --input-format=lttng-live.");
4245 bt_val_leftover
= bt_value_array_borrow_element_by_index_const(leftovers
, 0);
4246 lttng_live_url_parts
=
4247 bt_common_parse_lttng_live_url(bt_value_string_get(bt_val_leftover
),
4248 error_buf
, sizeof(error_buf
));
4249 if (!lttng_live_url_parts
.proto
) {
4250 BT_CLI_LOGE_APPEND_CAUSE("Invalid LTTng live URL format: %s.",
4255 if (!lttng_live_url_parts
.session_name
) {
4256 /* Print LTTng live sessions */
4257 cfg
= bt_config_print_lttng_live_sessions_create(
4263 g_string_assign(cfg
->cmd_data
.print_lttng_live_sessions
.url
,
4264 bt_value_string_get(bt_val_leftover
));
4268 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
4275 ret
= append_implicit_component_extra_param(
4276 &implicit_lttng_live_args
, "url",
4277 bt_value_string_get(bt_val_leftover
));
4282 ret
= append_implicit_component_extra_param(
4283 &implicit_lttng_live_args
,
4284 "session-not-found-action", "end");
4291 status
= auto_discover_source_components(plugin_paths
, leftovers
,
4292 auto_source_discovery_restrict_plugin_name
,
4293 auto_source_discovery_restrict_component_class_name
,
4294 *default_log_level
>= 0 ? *default_log_level
: cli_default_log_level
,
4301 create_implicit_component_args_from_auto_discovered_sources(
4302 &auto_disc
, discovered_source_args
);
4306 /* If --clock-offset was given, apply it to any src.ctf.fs component. */
4307 if (ctf_fs_source_clock_class_offset_arg
) {
4310 n
= append_multiple_implicit_components_param(
4311 discovered_source_args
, "source.ctf.fs", "clock-class-offset-s",
4312 ctf_fs_source_clock_class_offset_arg
);
4315 BT_CLI_LOGE_APPEND_CAUSE("--clock-offset specified, but no source.ctf.fs component instantiated.");
4320 /* If --clock-offset-ns was given, apply it to any src.ctf.fs component. */
4321 if (ctf_fs_source_clock_class_offset_ns_arg
) {
4324 n
= append_multiple_implicit_components_param(
4325 discovered_source_args
, "source.ctf.fs", "clock-class-offset-ns",
4326 ctf_fs_source_clock_class_offset_ns_arg
);
4329 BT_CLI_LOGE_APPEND_CAUSE("--clock-offset-ns specified, but no source.ctf.fs component instantiated.");
4335 * If the implicit `source.ctf.lttng-live` component exists, make sure
4336 * there's at least one leftover (which is the URL).
4338 if (implicit_lttng_live_args
.exists
&& bt_value_array_is_empty(leftovers
)) {
4339 BT_CLI_LOGE_APPEND_CAUSE("Missing URL for implicit `%s` component.",
4340 implicit_lttng_live_args
.comp_arg
->str
);
4344 /* Assign names to implicit components */
4345 for (i
= 0; i
< discovered_source_args
->len
; i
++) {
4346 struct implicit_component_args
*args
;
4349 args
= discovered_source_args
->pdata
[i
];
4351 g_string_printf(auto_disc_comp_name
, "auto-disc-%s", args
->comp_arg
->str
);
4353 /* Give it a name like `auto-disc-src-ctf-fs`. */
4354 for (j
= 0; j
< auto_disc_comp_name
->len
; j
++) {
4355 if (auto_disc_comp_name
->str
[j
] == '.') {
4356 auto_disc_comp_name
->str
[j
] = '-';
4360 ret
= assign_name_to_implicit_component(args
,
4361 auto_disc_comp_name
->str
, all_names
, &source_names
, true);
4367 ret
= assign_name_to_implicit_component(&implicit_lttng_live_args
,
4368 "lttng-live", all_names
, &source_names
, true);
4373 ret
= assign_name_to_implicit_component(&implicit_text_args
,
4374 "pretty", all_names
, &sink_names
, true);
4379 ret
= assign_name_to_implicit_component(&implicit_ctf_output_args
,
4380 "sink-ctf-fs", all_names
, &sink_names
, true);
4385 ret
= assign_name_to_implicit_component(&implicit_dummy_args
,
4386 "dummy", all_names
, &sink_names
, true);
4391 ret
= assign_name_to_implicit_component(&implicit_muxer_args
,
4392 "muxer", all_names
, NULL
, false);
4397 ret
= assign_name_to_implicit_component(&implicit_trimmer_args
,
4398 "trimmer", all_names
, NULL
, false);
4403 ret
= assign_name_to_implicit_component(&implicit_debug_info_args
,
4404 "debug-info", all_names
, NULL
, false);
4409 /* Make sure there's at least one source and one sink */
4410 if (!source_names
) {
4411 BT_CLI_LOGE_APPEND_CAUSE("No source component.");
4416 BT_CLI_LOGE_APPEND_CAUSE("No sink component.");
4421 * Prepend the muxer, the trimmer, and the debug info to the
4422 * filter chain so that we have:
4424 * sources -> muxer -> [trimmer] -> [debug info] ->
4425 * [user filters] -> sinks
4427 if (implicit_debug_info_args
.exists
) {
4428 if (g_list_prepend_gstring(&filter_names
,
4429 implicit_debug_info_args
.name_arg
->str
)) {
4434 if (implicit_trimmer_args
.exists
) {
4435 if (g_list_prepend_gstring(&filter_names
,
4436 implicit_trimmer_args
.name_arg
->str
)) {
4441 if (g_list_prepend_gstring(&filter_names
,
4442 implicit_muxer_args
.name_arg
->str
)) {
4447 * Append the equivalent run arguments for the implicit
4450 for (i
= 0; i
< discovered_source_args
->len
; i
++) {
4451 struct implicit_component_args
*args
=
4452 discovered_source_args
->pdata
[i
];
4454 ret
= append_run_args_for_implicit_component(args
, run_args
);
4460 ret
= append_run_args_for_implicit_component(&implicit_lttng_live_args
,
4466 ret
= append_run_args_for_implicit_component(&implicit_text_args
,
4472 ret
= append_run_args_for_implicit_component(&implicit_ctf_output_args
,
4478 ret
= append_run_args_for_implicit_component(&implicit_dummy_args
,
4484 ret
= append_run_args_for_implicit_component(&implicit_muxer_args
,
4490 ret
= append_run_args_for_implicit_component(&implicit_trimmer_args
,
4496 ret
= append_run_args_for_implicit_component(&implicit_debug_info_args
,
4502 /* Auto-connect components */
4503 ret
= convert_auto_connect(run_args
, source_names
, filter_names
,
4506 BT_CLI_LOGE_APPEND_CAUSE("Cannot auto-connect components.");
4511 * We have all the run command arguments now. Depending on
4512 * --run-args, we pass this to the run command or print them
4515 if (print_run_args
|| print_run_args_0
) {
4516 if (stream_intersection_mode
) {
4517 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --stream-intersection with --run-args or --run-args-0.");
4521 for (i
= 0; i
< bt_value_array_get_size(run_args
); i
++) {
4522 const bt_value
*arg_value
=
4523 bt_value_array_borrow_element_by_index(run_args
,
4526 GString
*quoted
= NULL
;
4527 const char *arg_to_print
;
4529 BT_ASSERT(arg_value
);
4530 arg
= bt_value_string_get(arg_value
);
4532 if (print_run_args
) {
4533 quoted
= bt_common_shell_quote(arg
, true);
4538 arg_to_print
= quoted
->str
;
4543 printf("%s", arg_to_print
);
4546 g_string_free(quoted
, TRUE
);
4549 if (i
< bt_value_array_get_size(run_args
) - 1) {
4550 if (print_run_args
) {
4559 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4564 * If the log level is still unset at this point, set it to
4565 * the program's default.
4567 if (*default_log_level
< 0) {
4568 *default_log_level
= cli_default_log_level
;
4571 cfg
= bt_config_run_from_args_array(run_args
, retcode
,
4572 force_omit_system_plugin_path
,
4573 force_omit_home_plugin_path
,
4574 initial_plugin_paths
, *default_log_level
);
4579 cfg
->cmd_data
.run
.stream_intersection_mode
= stream_intersection_mode
;
4584 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4588 * If the log level is still unset at this point, set it to
4589 * the program's default.
4591 if (*default_log_level
< 0) {
4592 *default_log_level
= cli_default_log_level
;
4595 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
4600 g_string_free(cur_name
, TRUE
);
4603 if (cur_name_prefix
) {
4604 g_string_free(cur_name_prefix
, TRUE
);
4607 bt_value_put_ref(run_args
);
4608 bt_value_put_ref(all_names
);
4609 destroy_glist_of_gstring(source_names
);
4610 destroy_glist_of_gstring(filter_names
);
4611 destroy_glist_of_gstring(sink_names
);
4612 bt_value_put_ref(leftovers
);
4613 finalize_implicit_component_args(&implicit_ctf_output_args
);
4614 finalize_implicit_component_args(&implicit_lttng_live_args
);
4615 finalize_implicit_component_args(&implicit_dummy_args
);
4616 finalize_implicit_component_args(&implicit_text_args
);
4617 finalize_implicit_component_args(&implicit_debug_info_args
);
4618 finalize_implicit_component_args(&implicit_muxer_args
);
4619 finalize_implicit_component_args(&implicit_trimmer_args
);
4620 bt_value_put_ref(plugin_paths
);
4621 bt_common_destroy_lttng_live_url_parts(<tng_live_url_parts
);
4622 auto_source_discovery_fini(&auto_disc
);
4624 if (discovered_source_args
) {
4625 g_ptr_array_free(discovered_source_args
, TRUE
);
4628 g_free(ctf_fs_source_clock_class_offset_arg
);
4629 g_free(ctf_fs_source_clock_class_offset_ns_arg
);
4631 if (auto_disc_comp_name
) {
4632 g_string_free(auto_disc_comp_name
, TRUE
);
4639 * Prints the Babeltrace 2.x general usage.
4642 void print_gen_usage(FILE *fp
)
4644 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] [COMMAND] [COMMAND ARGUMENTS]\n");
4646 fprintf(fp
, "General options:\n");
4648 fprintf(fp
, " -d, --debug Enable debug mode (same as --log-level=V)\n");
4649 fprintf(fp
, " -h, --help Show this help and quit\n");
4650 fprintf(fp
, " -l, --log-level=LVL Set the default log level to LVL (`N`, `V`, `D`,\n");
4651 fprintf(fp
, " `I`, `W` (default), `E`, or `F`)\n");
4652 fprintf(fp
, " -v, --verbose Enable verbose mode (same as --log-level=I)\n");
4653 fprintf(fp
, " -V, --version Show version and quit\n");
4655 fprintf(fp
, "Available commands:\n");
4657 fprintf(fp
, " convert Convert and trim traces (default)\n");
4658 fprintf(fp
, " help Get help for a plugin or a component class\n");
4659 fprintf(fp
, " list-plugins List available plugins and their content\n");
4660 fprintf(fp
, " query Query objects from a component class\n");
4661 fprintf(fp
, " run Build a processing graph and run it\n");
4663 fprintf(fp
, "Use `babeltrace2 COMMAND --help` to show the help of COMMAND.\n");
4666 struct bt_config
*bt_config_cli_args_create(int argc
, const char *argv
[],
4667 int *retcode
, bool force_omit_system_plugin_path
,
4668 bool force_omit_home_plugin_path
,
4669 const bt_value
*initial_plugin_paths
)
4671 struct bt_config
*config
= NULL
;
4674 const char **top_level_argv
;
4675 int command_argc
= -1;
4676 const char **command_argv
= NULL
;
4677 const char *command_name
= NULL
;
4678 int default_log_level
= -1;
4679 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
4681 /* Top-level option descriptions. */
4682 static const struct bt_argpar_opt_descr descrs
[] = {
4683 { OPT_DEBUG
, 'd', "debug", false },
4684 { OPT_HELP
, 'h', "help", false },
4685 { OPT_LOG_LEVEL
, 'l', "log-level", true },
4686 { OPT_VERBOSE
, 'v', "verbose", false },
4687 { OPT_VERSION
, 'V', "version", false},
4688 BT_ARGPAR_OPT_DESCR_SENTINEL
4692 COMMAND_TYPE_NONE
= -1,
4693 COMMAND_TYPE_RUN
= 0,
4694 COMMAND_TYPE_CONVERT
,
4695 COMMAND_TYPE_LIST_PLUGINS
,
4698 } command_type
= COMMAND_TYPE_NONE
;
4702 if (!initial_plugin_paths
) {
4703 initial_plugin_paths
= bt_value_array_create();
4704 if (!initial_plugin_paths
) {
4709 bt_value_get_ref(initial_plugin_paths
);
4715 print_gen_usage(stdout
);
4719 /* Skip first argument, the name of the program. */
4720 top_level_argc
= argc
- 1;
4721 top_level_argv
= argv
+ 1;
4722 argpar_parse_ret
= bt_argpar_parse(top_level_argc
, top_level_argv
,
4725 if (argpar_parse_ret
.error
) {
4726 BT_CLI_LOGE_APPEND_CAUSE(
4727 "While parsing command-line arguments: %s",
4728 argpar_parse_ret
.error
->str
);
4732 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
4733 struct bt_argpar_item
*item
;
4735 item
= g_ptr_array_index(argpar_parse_ret
.items
, i
);
4737 if (item
->type
== BT_ARGPAR_ITEM_TYPE_OPT
) {
4738 struct bt_argpar_item_opt
*item_opt
=
4739 (struct bt_argpar_item_opt
*) item
;
4741 switch (item_opt
->descr
->id
) {
4743 default_log_level
= BT_LOG_TRACE
;
4747 * Legacy: do not override a previous
4748 * --debug because --verbose and --debug
4749 * can be specified together (in this
4750 * case we want the lowest log level to
4753 default_log_level
= BT_LOG_INFO
;
4757 bt_log_get_level_from_string(item_opt
->arg
);
4758 if (default_log_level
< 0) {
4759 BT_CLI_LOGE_APPEND_CAUSE(
4760 "Invalid argument for --log-level option:\n %s",
4769 print_gen_usage(stdout
);
4772 } else if (item
->type
== BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
4773 struct bt_argpar_item_non_opt
*item_non_opt
=
4774 (struct bt_argpar_item_non_opt
*) item
;
4776 * First unknown argument: is it a known command
4780 top_level_argc
- item_non_opt
->orig_index
- 1;
4782 &top_level_argv
[item_non_opt
->orig_index
+ 1];
4784 if (strcmp(item_non_opt
->arg
, "convert") == 0) {
4785 command_type
= COMMAND_TYPE_CONVERT
;
4786 } else if (strcmp(item_non_opt
->arg
, "list-plugins") == 0) {
4787 command_type
= COMMAND_TYPE_LIST_PLUGINS
;
4788 } else if (strcmp(item_non_opt
->arg
, "help") == 0) {
4789 command_type
= COMMAND_TYPE_HELP
;
4790 } else if (strcmp(item_non_opt
->arg
, "query") == 0) {
4791 command_type
= COMMAND_TYPE_QUERY
;
4792 } else if (strcmp(item_non_opt
->arg
, "run") == 0) {
4793 command_type
= COMMAND_TYPE_RUN
;
4796 * Non-option argument, but not a known
4797 * command name: assume the default
4798 * `convert` command.
4800 command_type
= COMMAND_TYPE_CONVERT
;
4801 command_name
= "convert";
4809 if (command_type
== COMMAND_TYPE_NONE
) {
4810 if (argpar_parse_ret
.ingested_orig_args
== top_level_argc
) {
4812 * We only got non-help, non-version general options
4813 * like --verbose and --debug, without any other
4814 * arguments, so we can't do anything useful: print the
4817 print_gen_usage(stdout
);
4822 * We stopped on an unknown option argument (and therefore
4823 * didn't see a command name). Assume `convert` command.
4825 command_type
= COMMAND_TYPE_CONVERT
;
4826 command_name
= "convert";
4828 top_level_argc
- argpar_parse_ret
.ingested_orig_args
;
4830 &top_level_argv
[argpar_parse_ret
.ingested_orig_args
];
4833 BT_ASSERT(command_argv
);
4834 BT_ASSERT(command_argc
>= 0);
4837 * The convert command can set its own default log level for
4838 * backward compatibility reasons. It only does so if there's no
4839 * log level yet, so do not force one for this command.
4841 if (command_type
!= COMMAND_TYPE_CONVERT
&& default_log_level
< 0) {
4842 /* Default log level */
4843 default_log_level
= cli_default_log_level
;
4846 switch (command_type
) {
4847 case COMMAND_TYPE_RUN
:
4848 config
= bt_config_run_from_args(command_argc
, command_argv
,
4849 retcode
, force_omit_system_plugin_path
,
4850 force_omit_home_plugin_path
, initial_plugin_paths
,
4853 case COMMAND_TYPE_CONVERT
:
4854 config
= bt_config_convert_from_args(command_argc
, command_argv
,
4855 retcode
, force_omit_system_plugin_path
,
4856 force_omit_home_plugin_path
,
4857 initial_plugin_paths
, &default_log_level
);
4859 case COMMAND_TYPE_LIST_PLUGINS
:
4860 config
= bt_config_list_plugins_from_args(command_argc
,
4861 command_argv
, retcode
, force_omit_system_plugin_path
,
4862 force_omit_home_plugin_path
, initial_plugin_paths
);
4864 case COMMAND_TYPE_HELP
:
4865 config
= bt_config_help_from_args(command_argc
,
4866 command_argv
, retcode
, force_omit_system_plugin_path
,
4867 force_omit_home_plugin_path
, initial_plugin_paths
,
4870 case COMMAND_TYPE_QUERY
:
4871 config
= bt_config_query_from_args(command_argc
,
4872 command_argv
, retcode
, force_omit_system_plugin_path
,
4873 force_omit_home_plugin_path
, initial_plugin_paths
,
4881 BT_ASSERT(default_log_level
>= BT_LOG_TRACE
);
4882 config
->log_level
= default_log_level
;
4883 config
->command_name
= command_name
;
4892 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
4893 bt_value_put_ref(initial_plugin_paths
);