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 const struct bt_argpar_opt_descr list_plugins_options
[] = {
1769 /* id, short_name, long_name, with_arg */
1770 { OPT_HELP
, 'h', "help", false },
1771 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
1772 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
1773 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
1774 BT_ARGPAR_OPT_DESCR_SENTINEL
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
)
1790 struct bt_config
*cfg
= NULL
;
1791 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
1794 cfg
= bt_config_list_plugins_create(initial_plugin_paths
);
1799 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
1800 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
1801 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
1807 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, list_plugins_options
, true);
1808 if (argpar_parse_ret
.error
) {
1809 BT_CLI_LOGE_APPEND_CAUSE(
1810 "While parsing `list-plugins` command's command-line arguments: %s",
1811 argpar_parse_ret
.error
->str
);
1815 if (help_option_is_specified(&argpar_parse_ret
)) {
1816 print_list_plugins_usage(stdout
);
1818 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1822 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
1823 struct bt_argpar_item
*argpar_item
=
1824 g_ptr_array_index(argpar_parse_ret
.items
, i
);
1825 struct bt_argpar_item_opt
*argpar_item_opt
;
1828 if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
1829 struct bt_argpar_item_non_opt
*argpar_item_non_opt
1830 = (struct bt_argpar_item_non_opt
*) argpar_item
;
1832 BT_CLI_LOGE_APPEND_CAUSE("Unexpected argument: `%s`.",
1833 argpar_item_non_opt
->arg
);
1837 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
1838 arg
= argpar_item_opt
->arg
;
1840 switch (argpar_item_opt
->descr
->id
) {
1841 case OPT_PLUGIN_PATH
:
1842 if (bt_config_append_plugin_paths_check_setuid_setgid(
1843 cfg
->plugin_paths
, arg
)) {
1847 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
1848 cfg
->omit_system_plugin_path
= true;
1850 case OPT_OMIT_HOME_PLUGIN_PATH
:
1851 cfg
->omit_home_plugin_path
= true;
1854 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1855 argpar_item_opt
->descr
->id
);
1860 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
1868 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1871 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
1877 * Prints the run command usage.
1880 void print_run_usage(FILE *fp
)
1882 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] run [OPTIONS]\n");
1884 fprintf(fp
, "Options:\n");
1886 fprintf(fp
, " -b, --base-params=PARAMS Set PARAMS as the current base parameters\n");
1887 fprintf(fp
, " for all the following components until\n");
1888 fprintf(fp
, " --reset-base-params is encountered\n");
1889 fprintf(fp
, " (see the expected format of PARAMS below)\n");
1890 fprintf(fp
, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
1891 fprintf(fp
, " Instantiate the component class CLS of type\n");
1892 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
1893 fprintf(fp
, " in the plugin PLUGIN, add it to the graph,\n");
1894 fprintf(fp
, " and optionally name it NAME (you can also\n");
1895 fprintf(fp
, " specify the name with --name)\n");
1896 fprintf(fp
, " -x, --connect=CONNECTION Connect two created components (see the\n");
1897 fprintf(fp
, " expected format of CONNECTION below)\n");
1898 fprintf(fp
, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
1899 fprintf(fp
, " (`N`, `V`, `D`, `I`, `W`, `E`, or `F`)\n");
1900 fprintf(fp
, " -n, --name=NAME Set the name of the current component\n");
1901 fprintf(fp
, " to NAME (must be unique amongst all the\n");
1902 fprintf(fp
, " names of the created components)\n");
1903 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1904 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
1905 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1906 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
1907 fprintf(fp
, " current component (see the expected format\n");
1908 fprintf(fp
, " of PARAMS below)\n");
1909 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1910 fprintf(fp
, " dynamic plugins can be loaded\n");
1911 fprintf(fp
, " -r, --reset-base-params Reset the current base parameters to an\n");
1912 fprintf(fp
, " empty map\n");
1913 fprintf(fp
, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
1914 fprintf(fp
, " the graph later, retry in DUR µs\n");
1915 fprintf(fp
, " (default: 100000)\n");
1916 fprintf(fp
, " -h, --help Show this help and quit\n");
1918 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1919 fprintf(fp
, "\n\n");
1920 fprintf(fp
, "Expected format of CONNECTION\n");
1921 fprintf(fp
, "-----------------------------\n");
1923 fprintf(fp
, " UPSTREAM[.UPSTREAM-PORT]:DOWNSTREAM[.DOWNSTREAM-PORT]\n");
1925 fprintf(fp
, "UPSTREAM and DOWNSTREAM are names of the upstream and downstream\n");
1926 fprintf(fp
, "components to connect together. You must escape the following characters\n\n");
1927 fprintf(fp
, "with `\\`: `\\`, `.`, and `:`. You can set the name of the current\n");
1928 fprintf(fp
, "component with the --name option.\n");
1930 fprintf(fp
, "UPSTREAM-PORT and DOWNSTREAM-PORT are optional globbing patterns to\n");
1931 fprintf(fp
, "identify the upstream and downstream ports to use for the connection.\n");
1932 fprintf(fp
, "When the port is not specified, `*` is used.\n");
1934 fprintf(fp
, "When a component named UPSTREAM has an available port which matches the\n");
1935 fprintf(fp
, "UPSTREAM-PORT globbing pattern, it is connected to the first port which\n");
1936 fprintf(fp
, "matches the DOWNSTREAM-PORT globbing pattern of the component named\n");
1937 fprintf(fp
, "DOWNSTREAM.\n");
1939 fprintf(fp
, "The only special character in UPSTREAM-PORT and DOWNSTREAM-PORT is `*`\n");
1940 fprintf(fp
, "which matches anything. You must escape the following characters\n");
1941 fprintf(fp
, "with `\\`: `\\`, `*`, `?`, `[`, `.`, and `:`.\n");
1943 fprintf(fp
, "You can connect a source component to a filter or sink component. You\n");
1944 fprintf(fp
, "can connect a filter component to a sink component.\n");
1946 fprintf(fp
, "Examples:\n");
1948 fprintf(fp
, " my-src:my-sink\n");
1949 fprintf(fp
, " ctf-fs.*stream*:utils-muxer:*\n");
1951 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1952 fprintf(fp
, "babeltrace2 from a shell.\n");
1953 fprintf(fp
, "\n\n");
1954 print_expected_params_format(fp
);
1958 * Creates a Babeltrace config object from the arguments of a run
1961 * *retcode is set to the appropriate exit code to use.
1964 struct bt_config
*bt_config_run_from_args(int argc
, const char *argv
[],
1965 int *retcode
, bool force_omit_system_plugin_path
,
1966 bool force_omit_home_plugin_path
,
1967 const bt_value
*initial_plugin_paths
, int default_log_level
)
1969 struct bt_config_component
*cur_cfg_comp
= NULL
;
1970 enum bt_config_component_dest cur_cfg_comp_dest
=
1971 BT_CONFIG_COMPONENT_DEST_UNKNOWN
;
1972 bt_value
*cur_base_params
= NULL
;
1974 struct bt_config
*cfg
= NULL
;
1975 bt_value
*instance_names
= NULL
;
1976 bt_value
*connection_args
= NULL
;
1977 char error_buf
[256] = { 0 };
1978 long retry_duration
= -1;
1979 bt_value_map_extend_status extend_status
;
1980 GString
*error_str
= NULL
;
1981 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
1984 static const struct bt_argpar_opt_descr run_options
[] = {
1985 { OPT_BASE_PARAMS
, 'b', "base-params", true },
1986 { OPT_COMPONENT
, 'c', "component", true },
1987 { OPT_CONNECT
, 'x', "connect", true },
1988 { OPT_HELP
, 'h', "help", false },
1989 { OPT_LOG_LEVEL
, 'l', "log-level", true },
1990 { OPT_NAME
, 'n', "name", true },
1991 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
1992 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
1993 { OPT_PARAMS
, 'p', "params", true },
1994 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
1995 { OPT_RESET_BASE_PARAMS
, 'r', "reset-base-params", false },
1996 { OPT_RETRY_DURATION
, '\0', "retry-duration", true },
1997 BT_ARGPAR_OPT_DESCR_SENTINEL
2002 error_str
= g_string_new(NULL
);
2004 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2009 print_run_usage(stdout
);
2014 cfg
= bt_config_run_create(initial_plugin_paths
);
2019 cfg
->cmd_data
.run
.retry_duration_us
= 100000;
2020 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
2021 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
2022 cur_base_params
= bt_value_map_create();
2023 if (!cur_base_params
) {
2024 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2028 instance_names
= bt_value_map_create();
2029 if (!instance_names
) {
2030 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2034 connection_args
= bt_value_array_create();
2035 if (!connection_args
) {
2036 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2040 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
2046 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, run_options
, true);
2047 if (argpar_parse_ret
.error
) {
2048 BT_CLI_LOGE_APPEND_CAUSE(
2049 "While parsing `run` command's command-line arguments: %s",
2050 argpar_parse_ret
.error
->str
);
2054 if (help_option_is_specified(&argpar_parse_ret
)) {
2055 print_run_usage(stdout
);
2057 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2061 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
2062 struct bt_argpar_item
*argpar_item
=
2063 g_ptr_array_index(argpar_parse_ret
.items
, i
);
2064 struct bt_argpar_item_opt
*argpar_item_opt
;
2067 /* This command does not accept leftover arguments. */
2068 if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
2069 struct bt_argpar_item_non_opt
*argpar_nonopt_item
=
2070 (struct bt_argpar_item_non_opt
*) argpar_item
;
2072 BT_CLI_LOGE_APPEND_CAUSE("Unexpected argument: `%s`",
2073 argpar_nonopt_item
->arg
);
2077 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
2078 arg
= argpar_item_opt
->arg
;
2080 switch (argpar_item_opt
->descr
->id
) {
2081 case OPT_PLUGIN_PATH
:
2082 if (bt_config_append_plugin_paths_check_setuid_setgid(
2083 cfg
->plugin_paths
, arg
)) {
2087 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
2088 cfg
->omit_system_plugin_path
= true;
2090 case OPT_OMIT_HOME_PLUGIN_PATH
:
2091 cfg
->omit_home_plugin_path
= true;
2095 enum bt_config_component_dest new_dest
;
2098 ret
= add_run_cfg_comp_check_name(cfg
,
2099 cur_cfg_comp
, cur_cfg_comp_dest
,
2101 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2107 cur_cfg_comp
= bt_config_component_from_arg(arg
,
2109 if (!cur_cfg_comp
) {
2110 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --component option's argument:\n %s",
2115 switch (cur_cfg_comp
->type
) {
2116 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2117 new_dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
2119 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2120 new_dest
= BT_CONFIG_COMPONENT_DEST_FILTER
;
2122 case BT_COMPONENT_CLASS_TYPE_SINK
:
2123 new_dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
2129 BT_ASSERT(cur_base_params
);
2130 bt_value_put_ref(cur_cfg_comp
->params
);
2131 if (bt_value_copy(cur_base_params
,
2132 &cur_cfg_comp
->params
) < 0) {
2133 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2137 cur_cfg_comp_dest
= new_dest
;
2143 bt_value
*params_to_set
;
2145 if (!cur_cfg_comp
) {
2146 BT_CLI_LOGE_APPEND_CAUSE("Cannot add parameters to unavailable component:\n %s",
2151 params
= cli_value_from_arg(arg
, error_str
);
2153 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
2158 extend_status
= bt_value_map_extend(
2159 cur_cfg_comp
->params
, params
, ¶ms_to_set
);
2160 BT_VALUE_PUT_REF_AND_RESET(params
);
2161 if (extend_status
!= BT_VALUE_MAP_EXTEND_STATUS_OK
) {
2162 BT_CLI_LOGE_APPEND_CAUSE("Cannot extend current component parameters with --params option's argument:\n %s",
2167 BT_OBJECT_MOVE_REF(cur_cfg_comp
->params
, params_to_set
);
2171 if (!cur_cfg_comp
) {
2172 BT_CLI_LOGE_APPEND_CAUSE("Cannot set the name of unavailable component:\n %s",
2177 g_string_assign(cur_cfg_comp
->instance_name
, arg
);
2180 if (!cur_cfg_comp
) {
2181 BT_CLI_LOGE_APPEND_CAUSE("Cannot set the log level of unavailable component:\n %s",
2186 cur_cfg_comp
->log_level
=
2187 bt_log_get_level_from_string(arg
);
2188 if (cur_cfg_comp
->log_level
< 0) {
2189 BT_CLI_LOGE_APPEND_CAUSE("Invalid argument for --log-level option:\n %s",
2194 case OPT_BASE_PARAMS
:
2196 bt_value
*params
= cli_value_from_arg(arg
, error_str
);
2199 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --base-params option's argument:\n %s",
2204 BT_OBJECT_MOVE_REF(cur_base_params
, params
);
2207 case OPT_RESET_BASE_PARAMS
:
2208 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
2209 cur_base_params
= bt_value_map_create();
2210 if (!cur_base_params
) {
2211 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2216 if (bt_value_array_append_string_element(
2217 connection_args
, arg
)) {
2218 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2222 case OPT_RETRY_DURATION
: {
2224 size_t arg_len
= strlen(argpar_item_opt
->arg
);
2226 retry_duration
= g_ascii_strtoll(argpar_item_opt
->arg
, &end
, 10);
2228 if (arg_len
== 0 || end
!= (argpar_item_opt
->arg
+ arg_len
)) {
2229 BT_CLI_LOGE_APPEND_CAUSE(
2230 "Could not parse --retry-duration option's argument as an unsigned integer: `%s`",
2231 argpar_item_opt
->arg
);
2235 if (retry_duration
< 0) {
2236 BT_CLI_LOGE_APPEND_CAUSE("--retry-duration option's argument must be positive or 0: %ld",
2241 cfg
->cmd_data
.run
.retry_duration_us
=
2242 (uint64_t) retry_duration
;
2246 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
2247 argpar_item_opt
->descr
->id
);
2252 /* Add current component */
2254 ret
= add_run_cfg_comp_check_name(cfg
, cur_cfg_comp
,
2255 cur_cfg_comp_dest
, instance_names
);
2256 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2262 if (cfg
->cmd_data
.run
.sources
->len
== 0) {
2263 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no source component.");
2267 if (cfg
->cmd_data
.run
.sinks
->len
== 0) {
2268 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no sink component.");
2272 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
2276 ret
= bt_config_cli_args_create_connections(cfg
,
2280 BT_CLI_LOGE_APPEND_CAUSE("Cannot creation connections:\n%s", error_buf
);
2288 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2292 g_string_free(error_str
, TRUE
);
2295 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
2296 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2297 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
2298 BT_VALUE_PUT_REF_AND_RESET(instance_names
);
2299 BT_VALUE_PUT_REF_AND_RESET(connection_args
);
2304 struct bt_config
*bt_config_run_from_args_array(const bt_value
*run_args
,
2305 int *retcode
, bool force_omit_system_plugin_path
,
2306 bool force_omit_home_plugin_path
,
2307 const bt_value
*initial_plugin_paths
, int default_log_level
)
2309 struct bt_config
*cfg
= NULL
;
2312 const size_t argc
= bt_value_array_get_size(run_args
);
2314 argv
= calloc(argc
, sizeof(*argv
));
2316 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2320 len
= bt_value_array_get_size(run_args
);
2322 BT_CLI_LOGE_APPEND_CAUSE("Invalid executable arguments.");
2325 for (i
= 0; i
< len
; i
++) {
2326 const bt_value
*arg_value
=
2327 bt_value_array_borrow_element_by_index_const(run_args
,
2331 BT_ASSERT(arg_value
);
2332 arg
= bt_value_string_get(arg_value
);
2337 cfg
= bt_config_run_from_args(argc
, argv
, retcode
,
2338 force_omit_system_plugin_path
, force_omit_home_plugin_path
,
2339 initial_plugin_paths
, default_log_level
);
2347 * Prints the convert command usage.
2350 void print_convert_usage(FILE *fp
)
2352 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] [convert] [OPTIONS] [PATH/URL]\n");
2354 fprintf(fp
, "Options:\n");
2356 fprintf(fp
, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2357 fprintf(fp
, " Instantiate the component class CLS of type\n");
2358 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
2359 fprintf(fp
, " in the plugin PLUGIN, add it to the\n");
2360 fprintf(fp
, " conversion graph, and optionally name it\n");
2361 fprintf(fp
, " NAME (you can also specify the name with\n");
2362 fprintf(fp
, " --name)\n");
2363 fprintf(fp
, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
2364 fprintf(fp
, " (`N`, `V`, `D`, `I`, `W`, `E`, or `F`)\n");
2365 fprintf(fp
, " --name=NAME Set the name of the current component\n");
2366 fprintf(fp
, " to NAME (must be unique amongst all the\n");
2367 fprintf(fp
, " names of the created components)\n");
2368 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2369 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
2370 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2371 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2372 fprintf(fp
, " current component (see the expected format\n");
2373 fprintf(fp
, " of PARAMS below)\n");
2374 fprintf(fp
, " -P, --path=PATH Set the `path` string parameter of the\n");
2375 fprintf(fp
, " current component to PATH\n");
2376 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2377 fprintf(fp
, " dynamic plugins can be loaded\n");
2378 fprintf(fp
, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
2379 fprintf(fp
, " the graph later, retry in DUR µs\n");
2380 fprintf(fp
, " (default: 100000)\n");
2381 fprintf(fp
, " dynamic plugins can be loaded\n");
2382 fprintf(fp
, " --run-args Print the equivalent arguments for the\n");
2383 fprintf(fp
, " `run` command to the standard output,\n");
2384 fprintf(fp
, " formatted for a shell, and quit\n");
2385 fprintf(fp
, " --run-args-0 Print the equivalent arguments for the\n");
2386 fprintf(fp
, " `run` command to the standard output,\n");
2387 fprintf(fp
, " formatted for `xargs -0`, and quit\n");
2388 fprintf(fp
, " --stream-intersection Only process events when all streams\n");
2389 fprintf(fp
, " are active\n");
2390 fprintf(fp
, " -u, --url=URL Set the `url` string parameter of the\n");
2391 fprintf(fp
, " current component to URL\n");
2392 fprintf(fp
, " -h, --help Show this help and quit\n");
2394 fprintf(fp
, "Implicit `source.ctf.fs` component options:\n");
2396 fprintf(fp
, " --clock-offset=SEC Set clock offset to SEC seconds\n");
2397 fprintf(fp
, " --clock-offset-ns=NS Set clock offset to NS ns\n");
2399 fprintf(fp
, "Implicit `sink.text.pretty` component options:\n");
2401 fprintf(fp
, " --clock-cycles Print timestamps in clock cycles\n");
2402 fprintf(fp
, " --clock-date Print timestamp dates\n");
2403 fprintf(fp
, " --clock-gmt Print and parse timestamps in the GMT\n");
2404 fprintf(fp
, " time zone instead of the local time zone\n");
2405 fprintf(fp
, " --clock-seconds Print the timestamps as `SEC.NS` instead\n");
2406 fprintf(fp
, " of `hh:mm:ss.nnnnnnnnn`\n");
2407 fprintf(fp
, " --color=(never | auto | always)\n");
2408 fprintf(fp
, " Never, automatically, or always emit\n");
2409 fprintf(fp
, " console color codes\n");
2410 fprintf(fp
, " -f, --fields=FIELD[,FIELD]... Print additional fields; FIELD can be:\n");
2411 fprintf(fp
, " `all`, `trace`, `trace:hostname`,\n");
2412 fprintf(fp
, " `trace:domain`, `trace:procname`,\n");
2413 fprintf(fp
, " `trace:vpid`, `loglevel`, `emf`\n");
2414 fprintf(fp
, " -n, --names=NAME[,NAME]... Print field names; NAME can be:\n");
2415 fprintf(fp
, " `payload` (or `arg` or `args`), `none`,\n");
2416 fprintf(fp
, " `all`, `scope`, `header`, `context`\n");
2417 fprintf(fp
, " (or `ctx`)\n");
2418 fprintf(fp
, " --no-delta Do not print time delta between\n");
2419 fprintf(fp
, " consecutive events\n");
2420 fprintf(fp
, " -w, --output=PATH Write output text to PATH instead of\n");
2421 fprintf(fp
, " the standard output\n");
2423 fprintf(fp
, "Implicit `filter.utils.muxer` component options:\n");
2425 fprintf(fp
, " --clock-force-correlate Assume that clocks are inherently\n");
2426 fprintf(fp
, " correlated across traces\n");
2428 fprintf(fp
, "Implicit `filter.utils.trimmer` component options:\n");
2430 fprintf(fp
, " -b, --begin=BEGIN Set the beginning time of the conversion\n");
2431 fprintf(fp
, " time range to BEGIN (see the format of\n");
2432 fprintf(fp
, " BEGIN below)\n");
2433 fprintf(fp
, " -e, --end=END Set the end time of the conversion time\n");
2434 fprintf(fp
, " range to END (see the format of END below)\n");
2435 fprintf(fp
, " -t, --timerange=TIMERANGE Set conversion time range to TIMERANGE:\n");
2436 fprintf(fp
, " BEGIN,END or [BEGIN,END] (literally `[` and\n");
2437 fprintf(fp
, " `]`) (see the format of BEGIN/END below)\n");
2439 fprintf(fp
, "Implicit `filter.lttng-utils.debug-info` component options:\n");
2441 fprintf(fp
, " --debug-info Create an implicit\n");
2442 fprintf(fp
, " `filter.lttng-utils.debug-info` component\n");
2443 fprintf(fp
, " --debug-info-dir=DIR Search for debug info in directory DIR\n");
2444 fprintf(fp
, " instead of `/usr/lib/debug`\n");
2445 fprintf(fp
, " --debug-info-full-path Show full debug info source and\n");
2446 fprintf(fp
, " binary paths instead of just names\n");
2447 fprintf(fp
, " --debug-info-target-prefix=DIR\n");
2448 fprintf(fp
, " Use directory DIR as a prefix when\n");
2449 fprintf(fp
, " looking up executables during debug\n");
2450 fprintf(fp
, " info analysis\n");
2452 fprintf(fp
, "Legacy options that still work:\n");
2454 fprintf(fp
, " -i, --input-format=(ctf | lttng-live)\n");
2455 fprintf(fp
, " `ctf`:\n");
2456 fprintf(fp
, " Create an implicit `source.ctf.fs`\n");
2457 fprintf(fp
, " component\n");
2458 fprintf(fp
, " `lttng-live`:\n");
2459 fprintf(fp
, " Create an implicit `source.ctf.lttng-live`\n");
2460 fprintf(fp
, " component\n");
2461 fprintf(fp
, " -o, --output-format=(text | ctf | dummy | ctf-metadata)\n");
2462 fprintf(fp
, " `text`:\n");
2463 fprintf(fp
, " Create an implicit `sink.text.pretty`\n");
2464 fprintf(fp
, " component\n");
2465 fprintf(fp
, " `ctf`:\n");
2466 fprintf(fp
, " Create an implicit `sink.ctf.fs`\n");
2467 fprintf(fp
, " component\n");
2468 fprintf(fp
, " `dummy`:\n");
2469 fprintf(fp
, " Create an implicit `sink.utils.dummy`\n");
2470 fprintf(fp
, " component\n");
2471 fprintf(fp
, " `ctf-metadata`:\n");
2472 fprintf(fp
, " Query the `source.ctf.fs` component class\n");
2473 fprintf(fp
, " for metadata text and quit\n");
2475 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
2476 fprintf(fp
, "\n\n");
2477 fprintf(fp
, "Format of BEGIN and END\n");
2478 fprintf(fp
, "-----------------------\n");
2480 fprintf(fp
, " [YYYY-MM-DD [hh:mm:]]ss[.nnnnnnnnn]\n");
2481 fprintf(fp
, "\n\n");
2482 print_expected_params_format(fp
);
2486 const struct bt_argpar_opt_descr convert_options
[] = {
2487 /* id, short_name, long_name, with_arg */
2488 { OPT_BEGIN
, 'b', "begin", true },
2489 { OPT_CLOCK_CYCLES
, '\0', "clock-cycles", false },
2490 { OPT_CLOCK_DATE
, '\0', "clock-date", false },
2491 { OPT_CLOCK_FORCE_CORRELATE
, '\0', "clock-force-correlate", false },
2492 { OPT_CLOCK_GMT
, '\0', "clock-gmt", false },
2493 { OPT_CLOCK_OFFSET
, '\0', "clock-offset", true },
2494 { OPT_CLOCK_OFFSET_NS
, '\0', "clock-offset-ns", true },
2495 { OPT_CLOCK_SECONDS
, '\0', "clock-seconds", false },
2496 { OPT_COLOR
, '\0', "color", true },
2497 { OPT_COMPONENT
, 'c', "component", true },
2498 { OPT_DEBUG
, 'd', "debug", false },
2499 { OPT_DEBUG_INFO_DIR
, '\0', "debug-info-dir", true },
2500 { OPT_DEBUG_INFO_FULL_PATH
, '\0', "debug-info-full-path", false },
2501 { OPT_DEBUG_INFO_TARGET_PREFIX
, '\0', "debug-info-target-prefix", true },
2502 { OPT_END
, 'e', "end", true },
2503 { OPT_FIELDS
, 'f', "fields", true },
2504 { OPT_HELP
, 'h', "help", false },
2505 { OPT_INPUT_FORMAT
, 'i', "input-format", true },
2506 { OPT_LOG_LEVEL
, 'l', "log-level", true },
2507 { OPT_NAME
, '\0', "name", true },
2508 { OPT_NAMES
, 'n', "names", true },
2509 { OPT_DEBUG_INFO
, '\0', "debug-info", false },
2510 { OPT_NO_DELTA
, '\0', "no-delta", false },
2511 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
2512 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
2513 { OPT_OUTPUT
, 'w', "output", true },
2514 { OPT_OUTPUT_FORMAT
, 'o', "output-format", true },
2515 { OPT_PARAMS
, 'p', "params", true },
2516 { OPT_PATH
, 'P', "path", true },
2517 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
2518 { OPT_RETRY_DURATION
, '\0', "retry-duration", true },
2519 { OPT_RUN_ARGS
, '\0', "run-args", false },
2520 { OPT_RUN_ARGS_0
, '\0', "run-args-0", false },
2521 { OPT_STREAM_INTERSECTION
, '\0', "stream-intersection", false },
2522 { OPT_TIMERANGE
, '\0', "timerange", true },
2523 { OPT_URL
, 'u', "url", true },
2524 { OPT_VERBOSE
, 'v', "verbose", false },
2525 BT_ARGPAR_OPT_DESCR_SENTINEL
2529 GString
*get_component_auto_name(const char *prefix
,
2530 const bt_value
*existing_names
)
2533 GString
*auto_name
= g_string_new(NULL
);
2536 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2540 if (!bt_value_map_has_entry(existing_names
, prefix
)) {
2541 g_string_assign(auto_name
, prefix
);
2546 g_string_printf(auto_name
, "%s-%d", prefix
, i
);
2548 } while (bt_value_map_has_entry(existing_names
, auto_name
->str
));
2554 struct implicit_component_args
{
2557 /* The component class name (e.g. src.ctf.fs). */
2560 /* The component instance name. */
2563 GString
*params_arg
;
2564 bt_value
*extra_params
;
2568 int assign_name_to_implicit_component(struct implicit_component_args
*args
,
2569 const char *prefix
, bt_value
*existing_names
,
2570 GList
**comp_names
, bool append_to_comp_names
)
2573 GString
*name
= NULL
;
2575 if (!args
->exists
) {
2579 name
= get_component_auto_name(prefix
,
2587 g_string_assign(args
->name_arg
, name
->str
);
2589 if (bt_value_map_insert_entry(existing_names
, name
->str
,
2591 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2596 if (append_to_comp_names
) {
2597 *comp_names
= g_list_append(*comp_names
, name
);
2603 g_string_free(name
, TRUE
);
2610 int append_run_args_for_implicit_component(
2611 struct implicit_component_args
*impl_args
,
2617 if (!impl_args
->exists
) {
2621 if (bt_value_array_append_string_element(run_args
, "--component")) {
2622 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2626 if (bt_value_array_append_string_element(run_args
, impl_args
->comp_arg
->str
)) {
2627 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2631 if (bt_value_array_append_string_element(run_args
, "--name")) {
2632 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2636 if (bt_value_array_append_string_element(run_args
, impl_args
->name_arg
->str
)) {
2637 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2641 if (impl_args
->params_arg
->len
> 0) {
2642 if (bt_value_array_append_string_element(run_args
, "--params")) {
2643 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2647 if (bt_value_array_append_string_element(run_args
,
2648 impl_args
->params_arg
->str
)) {
2649 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2654 for (i
= 0; i
< bt_value_array_get_size(impl_args
->extra_params
);
2656 const bt_value
*elem
;
2659 elem
= bt_value_array_borrow_element_by_index(impl_args
->extra_params
,
2665 BT_ASSERT(bt_value_is_string(elem
));
2666 arg
= bt_value_string_get(elem
);
2667 ret
= bt_value_array_append_string_element(run_args
, arg
);
2669 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2683 /* Free the fields of a `struct implicit_component_args`. */
2686 void finalize_implicit_component_args(struct implicit_component_args
*args
)
2690 if (args
->comp_arg
) {
2691 g_string_free(args
->comp_arg
, TRUE
);
2694 if (args
->name_arg
) {
2695 g_string_free(args
->name_arg
, TRUE
);
2698 if (args
->params_arg
) {
2699 g_string_free(args
->params_arg
, TRUE
);
2702 bt_value_put_ref(args
->extra_params
);
2705 /* Destroy a dynamically-allocated `struct implicit_component_args`. */
2708 void destroy_implicit_component_args(struct implicit_component_args
*args
)
2710 finalize_implicit_component_args(args
);
2714 /* Initialize the fields of an already allocated `struct implicit_component_args`. */
2717 int init_implicit_component_args(struct implicit_component_args
*args
,
2718 const char *comp_arg
, bool exists
)
2722 args
->exists
= exists
;
2723 args
->comp_arg
= g_string_new(comp_arg
);
2724 args
->name_arg
= g_string_new(NULL
);
2725 args
->params_arg
= g_string_new(NULL
);
2726 args
->extra_params
= bt_value_array_create();
2728 if (!args
->comp_arg
|| !args
->name_arg
||
2729 !args
->params_arg
|| !args
->extra_params
) {
2731 finalize_implicit_component_args(args
);
2732 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2740 /* Dynamically allocate and initialize a `struct implicit_component_args`. */
2743 struct implicit_component_args
*create_implicit_component_args(
2744 const char *comp_arg
)
2746 struct implicit_component_args
*args
;
2749 args
= g_new(struct implicit_component_args
, 1);
2751 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2755 status
= init_implicit_component_args(args
, comp_arg
, true);
2766 void append_implicit_component_param(struct implicit_component_args
*args
,
2767 const char *key
, const char *value
)
2772 append_param_arg(args
->params_arg
, key
, value
);
2776 * Append the given parameter (`key=value`) to all component specifications
2777 * in `implicit_comp_args` (an array of `struct implicit_component_args *`)
2778 * which match `comp_arg`.
2780 * Return the number of matching components.
2784 int append_multiple_implicit_components_param(GPtrArray
*implicit_comp_args
,
2785 const char *comp_arg
, const char *key
, const char *value
)
2790 for (i
= 0; i
< implicit_comp_args
->len
; i
++) {
2791 struct implicit_component_args
*args
= implicit_comp_args
->pdata
[i
];
2793 if (strcmp(args
->comp_arg
->str
, comp_arg
) == 0) {
2794 append_implicit_component_param(args
, key
, value
);
2802 /* Escape value to make it suitable to use as a string parameter value. */
2804 gchar
*escape_string_value(const char *value
)
2809 ret
= g_string_new(NULL
);
2811 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2820 g_string_append_c(ret
, '\\');
2824 g_string_append_c(ret
, *in
);
2830 return g_string_free(ret
, FALSE
);
2834 int bt_value_to_cli_param_value_append(const bt_value
*value
, GString
*buf
)
2840 switch (bt_value_get_type(value
)) {
2841 case BT_VALUE_TYPE_STRING
:
2843 const char *str_value
= bt_value_string_get(value
);
2844 gchar
*escaped_str_value
;
2846 escaped_str_value
= escape_string_value(str_value
);
2847 if (!escaped_str_value
) {
2851 g_string_append_printf(buf
, "\"%s\"", escaped_str_value
);
2853 g_free(escaped_str_value
);
2856 case BT_VALUE_TYPE_ARRAY
: {
2857 g_string_append_c(buf
, '[');
2858 uint64_t sz
= bt_value_array_get_size(value
);
2859 for (uint64_t i
= 0; i
< sz
; i
++) {
2860 const bt_value
*item
;
2864 g_string_append(buf
, ", ");
2867 item
= bt_value_array_borrow_element_by_index_const(
2869 ret
= bt_value_to_cli_param_value_append(item
, buf
);
2875 g_string_append_c(buf
, ']');
2889 * Convert `value` to its equivalent representation as a command line parameter
2894 gchar
*bt_value_to_cli_param_value(bt_value
*value
)
2897 gchar
*result
= NULL
;
2899 buf
= g_string_new(NULL
);
2901 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2905 if (bt_value_to_cli_param_value_append(value
, buf
)) {
2909 result
= g_string_free(buf
, FALSE
);
2916 g_string_free(buf
, TRUE
);
2924 int append_parameter_to_args(bt_value
*args
, const char *key
, bt_value
*value
)
2927 BT_ASSERT(bt_value_get_type(args
) == BT_VALUE_TYPE_ARRAY
);
2932 gchar
*str_value
= NULL
;
2933 GString
*parameter
= NULL
;
2935 if (bt_value_array_append_string_element(args
, "--params")) {
2936 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2941 str_value
= bt_value_to_cli_param_value(value
);
2947 parameter
= g_string_new(NULL
);
2949 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2954 g_string_printf(parameter
, "%s=%s", key
, str_value
);
2956 if (bt_value_array_append_string_element(args
, parameter
->str
)) {
2957 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2964 g_string_free(parameter
, TRUE
);
2977 int append_string_parameter_to_args(bt_value
*args
, const char *key
, const char *value
)
2979 bt_value
*str_value
;
2982 str_value
= bt_value_string_create_init(value
);
2985 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2990 ret
= append_parameter_to_args(args
, key
, str_value
);
2993 BT_VALUE_PUT_REF_AND_RESET(str_value
);
2998 int append_implicit_component_extra_param(struct implicit_component_args
*args
,
2999 const char *key
, const char *value
)
3001 return append_string_parameter_to_args(args
->extra_params
, key
, value
);
3005 int convert_append_name_param(enum bt_config_component_dest dest
,
3006 GString
*cur_name
, GString
*cur_name_prefix
,
3008 bt_value
*all_names
,
3009 GList
**source_names
, GList
**filter_names
,
3014 if (cur_name_prefix
->len
> 0) {
3015 /* We're after a --component option */
3016 GString
*name
= NULL
;
3017 bool append_name_opt
= false;
3019 if (cur_name
->len
== 0) {
3021 * No explicit name was provided for the user
3024 name
= get_component_auto_name(cur_name_prefix
->str
,
3026 append_name_opt
= true;
3029 * An explicit name was provided for the user
3032 if (bt_value_map_has_entry(all_names
,
3034 BT_CLI_LOGE_APPEND_CAUSE("Duplicate component instance name:\n %s",
3039 name
= g_string_new(cur_name
->str
);
3043 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3048 * Remember this name globally, for the uniqueness of
3049 * all component names.
3051 if (bt_value_map_insert_entry(all_names
, name
->str
, bt_value_null
)) {
3052 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3057 * Append the --name option if necessary.
3059 if (append_name_opt
) {
3060 if (bt_value_array_append_string_element(run_args
, "--name")) {
3061 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3065 if (bt_value_array_append_string_element(run_args
, name
->str
)) {
3066 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3072 * Remember this name specifically for the type of the
3073 * component. This is to create connection arguments.
3076 case BT_CONFIG_COMPONENT_DEST_SOURCE
:
3077 *source_names
= g_list_append(*source_names
, name
);
3079 case BT_CONFIG_COMPONENT_DEST_FILTER
:
3080 *filter_names
= g_list_append(*filter_names
, name
);
3082 case BT_CONFIG_COMPONENT_DEST_SINK
:
3083 *sink_names
= g_list_append(*sink_names
, name
);
3089 g_string_assign(cur_name_prefix
, "");
3102 * Escapes `.`, `:`, and `\` of `input` with `\`.
3105 GString
*escape_dot_colon(const char *input
)
3107 GString
*output
= g_string_new(NULL
);
3111 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3115 for (ch
= input
; *ch
!= '\0'; ch
++) {
3116 if (*ch
== '\\' || *ch
== '.' || *ch
== ':') {
3117 g_string_append_c(output
, '\\');
3120 g_string_append_c(output
, *ch
);
3128 * Appends a --connect option to a list of arguments. `upstream_name`
3129 * and `downstream_name` are escaped with escape_dot_colon() in this
3133 int append_connect_arg(bt_value
*run_args
,
3134 const char *upstream_name
, const char *downstream_name
)
3137 GString
*e_upstream_name
= escape_dot_colon(upstream_name
);
3138 GString
*e_downstream_name
= escape_dot_colon(downstream_name
);
3139 GString
*arg
= g_string_new(NULL
);
3141 if (!e_upstream_name
|| !e_downstream_name
|| !arg
) {
3142 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3147 ret
= bt_value_array_append_string_element(run_args
, "--connect");
3149 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3154 g_string_append(arg
, e_upstream_name
->str
);
3155 g_string_append_c(arg
, ':');
3156 g_string_append(arg
, e_downstream_name
->str
);
3157 ret
= bt_value_array_append_string_element(run_args
, arg
->str
);
3159 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3166 g_string_free(arg
, TRUE
);
3169 if (e_upstream_name
) {
3170 g_string_free(e_upstream_name
, TRUE
);
3173 if (e_downstream_name
) {
3174 g_string_free(e_downstream_name
, TRUE
);
3181 * Appends the run command's --connect options for the convert command.
3184 int convert_auto_connect(bt_value
*run_args
,
3185 GList
*source_names
, GList
*filter_names
,
3189 GList
*source_at
= source_names
;
3190 GList
*filter_at
= filter_names
;
3192 GList
*sink_at
= sink_names
;
3194 BT_ASSERT(source_names
);
3195 BT_ASSERT(filter_names
);
3196 BT_ASSERT(sink_names
);
3198 /* Connect all sources to the first filter */
3199 for (source_at
= source_names
; source_at
; source_at
= g_list_next(source_at
)) {
3200 GString
*source_name
= source_at
->data
;
3201 GString
*filter_name
= filter_at
->data
;
3203 ret
= append_connect_arg(run_args
, source_name
->str
,
3210 filter_prev
= filter_at
;
3211 filter_at
= g_list_next(filter_at
);
3213 /* Connect remaining filters */
3214 for (; filter_at
; filter_prev
= filter_at
, filter_at
= g_list_next(filter_at
)) {
3215 GString
*filter_name
= filter_at
->data
;
3216 GString
*filter_prev_name
= filter_prev
->data
;
3218 ret
= append_connect_arg(run_args
, filter_prev_name
->str
,
3225 /* Connect last filter to all sinks */
3226 for (sink_at
= sink_names
; sink_at
; sink_at
= g_list_next(sink_at
)) {
3227 GString
*filter_name
= filter_prev
->data
;
3228 GString
*sink_name
= sink_at
->data
;
3230 ret
= append_connect_arg(run_args
, filter_name
->str
,
3247 int split_timerange(const char *arg
, char **begin
, char **end
)
3250 const char *ch
= arg
;
3252 GString
*g_begin
= NULL
;
3253 GString
*g_end
= NULL
;
3261 g_begin
= bt_common_string_until(ch
, "", ",", &end_pos
);
3262 if (!g_begin
|| ch
[end_pos
] != ',' || g_begin
->len
== 0) {
3268 g_end
= bt_common_string_until(ch
, "", "]", &end_pos
);
3269 if (!g_end
|| g_end
->len
== 0) {
3275 *begin
= g_begin
->str
;
3277 g_string_free(g_begin
, FALSE
);
3278 g_string_free(g_end
, FALSE
);
3288 g_string_free(g_begin
, TRUE
);
3292 g_string_free(g_end
, TRUE
);
3299 int g_list_prepend_gstring(GList
**list
, const char *string
)
3302 GString
*gs
= g_string_new(string
);
3307 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3311 *list
= g_list_prepend(*list
, gs
);
3318 * Create `struct implicit_component_args` structures for each of the source
3319 * components we identified. Add them to `component_args`.
3323 void create_implicit_component_args_from_auto_discovered_sources(
3324 const struct auto_source_discovery
*auto_disc
, GPtrArray
*component_args
)
3326 gchar
*cc_name
= NULL
;
3327 struct implicit_component_args
*comp
= NULL
;
3331 len
= auto_disc
->results
->len
;
3333 for (i
= 0; i
< len
; i
++) {
3334 struct auto_source_discovery_result
*res
=
3335 g_ptr_array_index(auto_disc
->results
, i
);
3338 cc_name
= g_strdup_printf("source.%s.%s", res
->plugin_name
, res
->source_cc_name
);
3340 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3344 comp
= create_implicit_component_args(cc_name
);
3349 status
= append_parameter_to_args(comp
->extra_params
, "inputs", res
->inputs
);
3354 g_ptr_array_add(component_args
, comp
);
3362 destroy_implicit_component_args(comp
);
3367 * Creates a Babeltrace config object from the arguments of a convert
3370 * *retcode is set to the appropriate exit code to use.
3373 struct bt_config
*bt_config_convert_from_args(int argc
, const char *argv
[],
3374 int *retcode
, bool force_omit_system_plugin_path
,
3375 bool force_omit_home_plugin_path
,
3376 const bt_value
*initial_plugin_paths
, int *default_log_level
)
3378 enum bt_config_component_dest cur_comp_dest
=
3379 BT_CONFIG_COMPONENT_DEST_UNKNOWN
;
3381 struct bt_config
*cfg
= NULL
;
3382 bool got_input_format_opt
= false;
3383 bool got_output_format_opt
= false;
3384 bool trimmer_has_begin
= false;
3385 bool trimmer_has_end
= false;
3386 bool stream_intersection_mode
= false;
3387 GString
*cur_name
= NULL
;
3388 GString
*cur_name_prefix
= NULL
;
3389 bool print_run_args
= false;
3390 bool print_run_args_0
= false;
3391 bool print_ctf_metadata
= false;
3392 bt_value
*run_args
= NULL
;
3393 bt_value
*all_names
= NULL
;
3394 GList
*source_names
= NULL
;
3395 GList
*filter_names
= NULL
;
3396 GList
*sink_names
= NULL
;
3397 bt_value
*leftovers
= NULL
;
3398 struct implicit_component_args implicit_ctf_output_args
= { 0 };
3399 struct implicit_component_args implicit_lttng_live_args
= { 0 };
3400 struct implicit_component_args implicit_dummy_args
= { 0 };
3401 struct implicit_component_args implicit_text_args
= { 0 };
3402 struct implicit_component_args implicit_debug_info_args
= { 0 };
3403 struct implicit_component_args implicit_muxer_args
= { 0 };
3404 struct implicit_component_args implicit_trimmer_args
= { 0 };
3405 bt_value
*plugin_paths
;
3406 char error_buf
[256] = { 0 };
3408 struct bt_common_lttng_live_url_parts lttng_live_url_parts
= { 0 };
3409 char *output
= NULL
;
3410 struct auto_source_discovery auto_disc
= { NULL
};
3411 GString
*auto_disc_comp_name
= NULL
;
3412 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
3415 * Array of `struct implicit_component_args *` created for the sources
3416 * we have auto-discovered.
3418 GPtrArray
*discovered_source_args
= NULL
;
3421 * If set, restrict automatic source discovery to this component class
3424 const char *auto_source_discovery_restrict_plugin_name
= NULL
;
3425 const char *auto_source_discovery_restrict_component_class_name
= NULL
;
3427 gchar
*ctf_fs_source_clock_class_offset_arg
= NULL
;
3428 gchar
*ctf_fs_source_clock_class_offset_ns_arg
= NULL
;
3430 (void) bt_value_copy(initial_plugin_paths
, &plugin_paths
);
3435 print_convert_usage(stdout
);
3440 if (init_implicit_component_args(&implicit_ctf_output_args
,
3441 "sink.ctf.fs", false)) {
3445 if (init_implicit_component_args(&implicit_lttng_live_args
,
3446 "source.ctf.lttng-live", false)) {
3450 if (init_implicit_component_args(&implicit_text_args
,
3451 "sink.text.pretty", false)) {
3455 if (init_implicit_component_args(&implicit_dummy_args
,
3456 "sink.utils.dummy", false)) {
3460 if (init_implicit_component_args(&implicit_debug_info_args
,
3461 "filter.lttng-utils.debug-info", false)) {
3465 if (init_implicit_component_args(&implicit_muxer_args
,
3466 "filter.utils.muxer", true)) {
3470 if (init_implicit_component_args(&implicit_trimmer_args
,
3471 "filter.utils.trimmer", false)) {
3475 all_names
= bt_value_map_create();
3477 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3481 run_args
= bt_value_array_create();
3483 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3487 cur_name
= g_string_new(NULL
);
3489 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3493 cur_name_prefix
= g_string_new(NULL
);
3494 if (!cur_name_prefix
) {
3495 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3499 ret
= append_env_var_plugin_paths(plugin_paths
);
3504 leftovers
= bt_value_array_create();
3506 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3510 if (auto_source_discovery_init(&auto_disc
) != 0) {
3514 discovered_source_args
=
3515 g_ptr_array_new_with_free_func((GDestroyNotify
) destroy_implicit_component_args
);
3516 if (!discovered_source_args
) {
3517 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3521 auto_disc_comp_name
= g_string_new(NULL
);
3522 if (!auto_disc_comp_name
) {
3523 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3528 * First pass: collect all arguments which need to be passed
3529 * as is to the run command. This pass can also add --name
3530 * arguments if needed to automatically name unnamed component
3531 * instances. Also it does the following transformations:
3533 * --path=PATH -> --params=path="PATH"
3534 * --url=URL -> --params=url="URL"
3536 * Also it appends the plugin paths of --plugin-path to
3539 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, convert_options
, true);
3540 if (argpar_parse_ret
.error
) {
3541 BT_CLI_LOGE_APPEND_CAUSE(
3542 "While parsing `convert` command's command-line arguments: %s",
3543 argpar_parse_ret
.error
->str
);
3547 if (help_option_is_specified(&argpar_parse_ret
)) {
3548 print_convert_usage(stdout
);
3550 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
3554 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
3555 struct bt_argpar_item
*argpar_item
=
3556 g_ptr_array_index(argpar_parse_ret
.items
, i
);
3557 struct bt_argpar_item_opt
*argpar_item_opt
;
3559 char *plugin_name
= NULL
;
3560 char *comp_cls_name
= NULL
;
3563 if (argpar_item
->type
!= BT_ARGPAR_ITEM_TYPE_OPT
) {
3567 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
3568 arg
= argpar_item_opt
->arg
;
3570 switch (argpar_item_opt
->descr
->id
) {
3573 bt_component_class_type type
;
3574 const char *type_prefix
;
3576 /* Append current component's name if needed */
3577 ret
= convert_append_name_param(cur_comp_dest
, cur_name
,
3578 cur_name_prefix
, run_args
, all_names
,
3579 &source_names
, &filter_names
, &sink_names
);
3584 /* Parse the argument */
3585 plugin_comp_cls_names(arg
, &name
, &plugin_name
,
3586 &comp_cls_name
, &type
);
3587 if (!plugin_name
|| !comp_cls_name
) {
3588 BT_CLI_LOGE_APPEND_CAUSE(
3589 "Invalid format for --component option's argument:\n %s",
3595 g_string_assign(cur_name
, name
);
3597 g_string_assign(cur_name
, "");
3601 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
3602 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
3603 type_prefix
= "source";
3605 case BT_COMPONENT_CLASS_TYPE_FILTER
:
3606 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_FILTER
;
3607 type_prefix
= "filter";
3609 case BT_COMPONENT_CLASS_TYPE_SINK
:
3610 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
3611 type_prefix
= "sink";
3617 if (bt_value_array_append_string_element(run_args
,
3619 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3623 if (bt_value_array_append_string_element(run_args
, arg
)) {
3624 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3628 g_string_assign(cur_name_prefix
, "");
3629 g_string_append_printf(cur_name_prefix
, "%s.%s.%s",
3630 type_prefix
, plugin_name
, comp_cls_name
);
3633 free(comp_cls_name
);
3636 comp_cls_name
= NULL
;
3640 if (cur_name_prefix
->len
== 0) {
3641 BT_CLI_LOGE_APPEND_CAUSE("No current component of which to set parameters:\n %s",
3646 if (bt_value_array_append_string_element(run_args
,
3648 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3652 if (bt_value_array_append_string_element(run_args
, arg
)) {
3653 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3658 if (cur_name_prefix
->len
== 0) {
3659 BT_CLI_LOGE_APPEND_CAUSE("No current component of which to set `path` parameter:\n %s",
3664 if (append_string_parameter_to_args(run_args
, "path", arg
)) {
3669 if (cur_name_prefix
->len
== 0) {
3670 BT_CLI_LOGE_APPEND_CAUSE("No current component of which to set `url` parameter:\n %s",
3676 if (append_string_parameter_to_args(run_args
, "url", arg
)) {
3681 if (cur_name_prefix
->len
== 0) {
3682 BT_CLI_LOGE_APPEND_CAUSE("No current component to name:\n %s",
3687 if (bt_value_array_append_string_element(run_args
, "--name")) {
3688 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3692 if (bt_value_array_append_string_element(run_args
, arg
)) {
3693 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3697 g_string_assign(cur_name
, arg
);
3700 if (cur_name_prefix
->len
== 0) {
3701 BT_CLI_LOGE_APPEND_CAUSE("No current component to assign a log level to:\n %s",
3706 if (bt_value_array_append_string_element(run_args
, "--log-level")) {
3707 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3711 if (bt_value_array_append_string_element(run_args
, arg
)) {
3712 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3717 case OPT_OMIT_HOME_PLUGIN_PATH
:
3718 force_omit_home_plugin_path
= true;
3720 if (bt_value_array_append_string_element(run_args
,
3721 "--omit-home-plugin-path")) {
3722 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3726 case OPT_RETRY_DURATION
:
3727 if (bt_value_array_append_string_element(run_args
,
3728 "--retry-duration")) {
3729 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3733 if (bt_value_array_append_string_element(run_args
, arg
)) {
3734 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3738 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
3739 force_omit_system_plugin_path
= true;
3741 if (bt_value_array_append_string_element(run_args
,
3742 "--omit-system-plugin-path")) {
3743 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3747 case OPT_PLUGIN_PATH
:
3748 if (bt_config_append_plugin_paths_check_setuid_setgid(
3749 plugin_paths
, arg
)) {
3753 if (bt_value_array_append_string_element(run_args
,
3755 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3759 if (bt_value_array_append_string_element(run_args
, arg
)) {
3760 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3765 case OPT_CLOCK_CYCLES
:
3766 case OPT_CLOCK_DATE
:
3767 case OPT_CLOCK_FORCE_CORRELATE
:
3769 case OPT_CLOCK_OFFSET
:
3770 case OPT_CLOCK_OFFSET_NS
:
3771 case OPT_CLOCK_SECONDS
:
3774 case OPT_DEBUG_INFO
:
3775 case OPT_DEBUG_INFO_DIR
:
3776 case OPT_DEBUG_INFO_FULL_PATH
:
3777 case OPT_DEBUG_INFO_TARGET_PREFIX
:
3780 case OPT_INPUT_FORMAT
:
3783 case OPT_OUTPUT_FORMAT
:
3786 case OPT_RUN_ARGS_0
:
3787 case OPT_STREAM_INTERSECTION
:
3790 /* Ignore in this pass */
3793 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
3794 argpar_item_opt
->descr
->id
);
3799 /* Append current component's name if needed */
3800 ret
= convert_append_name_param(cur_comp_dest
, cur_name
,
3801 cur_name_prefix
, run_args
, all_names
, &source_names
,
3802 &filter_names
, &sink_names
);
3808 * Second pass: transform the convert-specific options and
3809 * arguments into implicit component instances for the run
3812 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
3813 struct bt_argpar_item
*argpar_item
=
3814 g_ptr_array_index(argpar_parse_ret
.items
, i
);
3815 struct bt_argpar_item_opt
*argpar_item_opt
;
3818 if (argpar_item
->type
!= BT_ARGPAR_ITEM_TYPE_OPT
) {
3822 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
3823 arg
= argpar_item_opt
->arg
;
3825 switch (argpar_item_opt
->descr
->id
) {
3827 if (trimmer_has_begin
) {
3828 printf("At --begin option: --begin or --timerange option already specified\n %s\n",
3833 trimmer_has_begin
= true;
3834 ret
= append_implicit_component_extra_param(
3835 &implicit_trimmer_args
, "begin", arg
);
3836 implicit_trimmer_args
.exists
= true;
3842 if (trimmer_has_end
) {
3843 printf("At --end option: --end or --timerange option already specified\n %s\n",
3848 trimmer_has_end
= true;
3849 ret
= append_implicit_component_extra_param(
3850 &implicit_trimmer_args
, "end", arg
);
3851 implicit_trimmer_args
.exists
= true;
3861 if (trimmer_has_begin
|| trimmer_has_end
) {
3862 printf("At --timerange option: --begin, --end, or --timerange option already specified\n %s\n",
3867 ret
= split_timerange(arg
, &begin
, &end
);
3869 BT_CLI_LOGE_APPEND_CAUSE("Invalid --timerange option's argument: expecting BEGIN,END or [BEGIN,END]:\n %s",
3874 ret
= append_implicit_component_extra_param(
3875 &implicit_trimmer_args
, "begin", begin
);
3876 ret
|= append_implicit_component_extra_param(
3877 &implicit_trimmer_args
, "end", end
);
3878 implicit_trimmer_args
.exists
= true;
3886 case OPT_CLOCK_CYCLES
:
3887 append_implicit_component_param(
3888 &implicit_text_args
, "clock-cycles", "yes");
3889 implicit_text_args
.exists
= true;
3891 case OPT_CLOCK_DATE
:
3892 append_implicit_component_param(
3893 &implicit_text_args
, "clock-date", "yes");
3894 implicit_text_args
.exists
= true;
3896 case OPT_CLOCK_FORCE_CORRELATE
:
3897 append_implicit_component_param(
3898 &implicit_muxer_args
,
3899 "assume-absolute-clock-classes", "yes");
3902 append_implicit_component_param(
3903 &implicit_text_args
, "clock-gmt", "yes");
3904 append_implicit_component_param(
3905 &implicit_trimmer_args
, "gmt", "yes");
3906 implicit_text_args
.exists
= true;
3908 case OPT_CLOCK_OFFSET
:
3909 if (ctf_fs_source_clock_class_offset_arg
) {
3910 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --clock-offset option\n");
3914 ctf_fs_source_clock_class_offset_arg
= g_strdup(arg
);
3915 if (!ctf_fs_source_clock_class_offset_arg
) {
3916 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3920 case OPT_CLOCK_OFFSET_NS
:
3921 if (ctf_fs_source_clock_class_offset_ns_arg
) {
3922 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --clock-offset-ns option\n");
3926 ctf_fs_source_clock_class_offset_ns_arg
= g_strdup(arg
);
3927 if (!ctf_fs_source_clock_class_offset_ns_arg
) {
3928 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3932 case OPT_CLOCK_SECONDS
:
3933 append_implicit_component_param(
3934 &implicit_text_args
, "clock-seconds", "yes");
3935 implicit_text_args
.exists
= true;
3938 implicit_text_args
.exists
= true;
3939 ret
= append_implicit_component_extra_param(
3940 &implicit_text_args
, "color", arg
);
3945 case OPT_DEBUG_INFO
:
3946 implicit_debug_info_args
.exists
= true;
3948 case OPT_DEBUG_INFO_DIR
:
3949 implicit_debug_info_args
.exists
= true;
3950 ret
= append_implicit_component_extra_param(
3951 &implicit_debug_info_args
, "debug-info-dir", arg
);
3956 case OPT_DEBUG_INFO_FULL_PATH
:
3957 implicit_debug_info_args
.exists
= true;
3958 append_implicit_component_param(
3959 &implicit_debug_info_args
, "full-path", "yes");
3961 case OPT_DEBUG_INFO_TARGET_PREFIX
:
3962 implicit_debug_info_args
.exists
= true;
3963 ret
= append_implicit_component_extra_param(
3964 &implicit_debug_info_args
,
3965 "target-prefix", arg
);
3972 bt_value
*fields
= fields_from_arg(arg
);
3978 implicit_text_args
.exists
= true;
3979 ret
= insert_flat_params_from_array(
3980 implicit_text_args
.params_arg
,
3982 bt_value_put_ref(fields
);
3990 bt_value
*names
= names_from_arg(arg
);
3996 implicit_text_args
.exists
= true;
3997 ret
= insert_flat_params_from_array(
3998 implicit_text_args
.params_arg
,
4000 bt_value_put_ref(names
);
4007 append_implicit_component_param(
4008 &implicit_text_args
, "no-delta", "yes");
4009 implicit_text_args
.exists
= true;
4011 case OPT_INPUT_FORMAT
:
4012 if (got_input_format_opt
) {
4013 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --input-format option.");
4017 got_input_format_opt
= true;
4019 if (strcmp(arg
, "ctf") == 0) {
4020 auto_source_discovery_restrict_plugin_name
= "ctf";
4021 auto_source_discovery_restrict_component_class_name
= "fs";
4022 } else if (strcmp(arg
, "lttng-live") == 0) {
4023 auto_source_discovery_restrict_plugin_name
= "ctf";
4024 auto_source_discovery_restrict_component_class_name
= "lttng-live";
4025 implicit_lttng_live_args
.exists
= true;
4027 BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy input format:\n %s",
4032 case OPT_OUTPUT_FORMAT
:
4033 if (got_output_format_opt
) {
4034 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output-format option.");
4038 got_output_format_opt
= true;
4040 if (strcmp(arg
, "text") == 0) {
4041 implicit_text_args
.exists
= true;
4042 } else if (strcmp(arg
, "ctf") == 0) {
4043 implicit_ctf_output_args
.exists
= true;
4044 } else if (strcmp(arg
, "dummy") == 0) {
4045 implicit_dummy_args
.exists
= true;
4046 } else if (strcmp(arg
, "ctf-metadata") == 0) {
4047 print_ctf_metadata
= true;
4049 BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy output format:\n %s",
4056 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output option");
4060 output
= strdup(arg
);
4062 BT_CLI_LOGE_APPEND_CAUSE_OOM();
4067 if (print_run_args_0
) {
4068 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0.");
4072 print_run_args
= true;
4074 case OPT_RUN_ARGS_0
:
4075 if (print_run_args
) {
4076 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0.");
4080 print_run_args_0
= true;
4082 case OPT_STREAM_INTERSECTION
:
4084 * Applies to all traces implementing the
4085 * babeltrace.trace-info query.
4087 stream_intersection_mode
= true;
4090 if (*default_log_level
!= BT_LOG_TRACE
&&
4091 *default_log_level
!= BT_LOG_DEBUG
) {
4092 *default_log_level
= BT_LOG_INFO
;
4096 *default_log_level
= BT_LOG_TRACE
;
4104 * Legacy behaviour: --verbose used to make the `text` output
4105 * format print more information. --verbose is now equivalent to
4106 * the INFO log level, which is why we compare to `BT_LOG_INFO`
4109 if (*default_log_level
== BT_LOG_INFO
) {
4110 append_implicit_component_param(&implicit_text_args
,
4115 * Append home and system plugin paths now that we possibly got
4118 if (append_home_and_system_plugin_paths(plugin_paths
,
4119 force_omit_system_plugin_path
,
4120 force_omit_home_plugin_path
)) {
4124 /* Consume and keep leftover arguments */
4125 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
4126 struct bt_argpar_item
*argpar_item
=
4127 g_ptr_array_index(argpar_parse_ret
.items
, i
);
4128 struct bt_argpar_item_non_opt
*argpar_item_non_opt
;
4130 if (argpar_item
->type
!= BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
4134 argpar_item_non_opt
= (struct bt_argpar_item_non_opt
*) argpar_item
;
4136 if (bt_value_array_append_string_element(leftovers
, argpar_item_non_opt
->arg
) !=
4137 BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
4138 BT_CLI_LOGE_APPEND_CAUSE_OOM();
4143 /* Print CTF metadata or print LTTng live sessions */
4144 if (print_ctf_metadata
) {
4145 const bt_value
*bt_val_leftover
;
4147 if (bt_value_array_is_empty(leftovers
)) {
4148 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf-metadata specified without a path.");
4152 if (bt_value_array_get_size(leftovers
) > 1) {
4153 BT_CLI_LOGE_APPEND_CAUSE("Too many paths specified for --output-format=ctf-metadata.");
4157 cfg
= bt_config_print_ctf_metadata_create(plugin_paths
);
4162 bt_val_leftover
= bt_value_array_borrow_element_by_index_const(leftovers
, 0);
4163 g_string_assign(cfg
->cmd_data
.print_ctf_metadata
.path
,
4164 bt_value_string_get(bt_val_leftover
));
4168 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
4176 * If -o ctf was specified, make sure an output path (--output)
4177 * was also specified. --output does not imply -o ctf because
4178 * it's also used for the default, implicit -o text if -o ctf
4181 if (implicit_ctf_output_args
.exists
) {
4183 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf specified without --output (trace output path).");
4188 * At this point we know that -o ctf AND --output were
4189 * specified. Make sure that no options were specified
4190 * which would imply -o text because --output would be
4191 * ambiguous in this case. For example, this is wrong:
4193 * babeltrace2 --names=all -o ctf --output=/tmp/path my-trace
4195 * because --names=all implies -o text, and --output
4196 * could apply to both the sink.text.pretty and
4197 * sink.ctf.fs implicit components.
4199 if (implicit_text_args
.exists
) {
4200 BT_CLI_LOGE_APPEND_CAUSE("Ambiguous --output option: --output-format=ctf specified but another option implies --output-format=text.");
4206 * If -o dummy and -o ctf were not specified, and if there are
4207 * no explicit sink components, then use an implicit
4208 * `sink.text.pretty` component.
4210 if (!implicit_dummy_args
.exists
&& !implicit_ctf_output_args
.exists
&&
4212 implicit_text_args
.exists
= true;
4216 * Set implicit `sink.text.pretty` or `sink.ctf.fs` component's
4217 * `path` parameter if --output was specified.
4220 if (implicit_text_args
.exists
) {
4221 append_implicit_component_extra_param(&implicit_text_args
,
4223 } else if (implicit_ctf_output_args
.exists
) {
4224 append_implicit_component_extra_param(&implicit_ctf_output_args
,
4229 /* Decide where the leftover argument(s) go */
4230 if (bt_value_array_get_size(leftovers
) > 0) {
4231 if (implicit_lttng_live_args
.exists
) {
4232 const bt_value
*bt_val_leftover
;
4234 if (bt_value_array_get_size(leftovers
) > 1) {
4235 BT_CLI_LOGE_APPEND_CAUSE("Too many URLs specified for --input-format=lttng-live.");
4239 bt_val_leftover
= bt_value_array_borrow_element_by_index_const(leftovers
, 0);
4240 lttng_live_url_parts
=
4241 bt_common_parse_lttng_live_url(bt_value_string_get(bt_val_leftover
),
4242 error_buf
, sizeof(error_buf
));
4243 if (!lttng_live_url_parts
.proto
) {
4244 BT_CLI_LOGE_APPEND_CAUSE("Invalid LTTng live URL format: %s.",
4249 if (!lttng_live_url_parts
.session_name
) {
4250 /* Print LTTng live sessions */
4251 cfg
= bt_config_print_lttng_live_sessions_create(
4257 g_string_assign(cfg
->cmd_data
.print_lttng_live_sessions
.url
,
4258 bt_value_string_get(bt_val_leftover
));
4262 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
4269 ret
= append_implicit_component_extra_param(
4270 &implicit_lttng_live_args
, "url",
4271 bt_value_string_get(bt_val_leftover
));
4276 ret
= append_implicit_component_extra_param(
4277 &implicit_lttng_live_args
,
4278 "session-not-found-action", "end");
4285 status
= auto_discover_source_components(plugin_paths
, leftovers
,
4286 auto_source_discovery_restrict_plugin_name
,
4287 auto_source_discovery_restrict_component_class_name
,
4288 *default_log_level
>= 0 ? *default_log_level
: cli_default_log_level
,
4295 create_implicit_component_args_from_auto_discovered_sources(
4296 &auto_disc
, discovered_source_args
);
4300 /* If --clock-offset was given, apply it to any src.ctf.fs component. */
4301 if (ctf_fs_source_clock_class_offset_arg
) {
4304 n
= append_multiple_implicit_components_param(
4305 discovered_source_args
, "source.ctf.fs", "clock-class-offset-s",
4306 ctf_fs_source_clock_class_offset_arg
);
4309 BT_CLI_LOGE_APPEND_CAUSE("--clock-offset specified, but no source.ctf.fs component instantiated.");
4314 /* If --clock-offset-ns was given, apply it to any src.ctf.fs component. */
4315 if (ctf_fs_source_clock_class_offset_ns_arg
) {
4318 n
= append_multiple_implicit_components_param(
4319 discovered_source_args
, "source.ctf.fs", "clock-class-offset-ns",
4320 ctf_fs_source_clock_class_offset_ns_arg
);
4323 BT_CLI_LOGE_APPEND_CAUSE("--clock-offset-ns specified, but no source.ctf.fs component instantiated.");
4329 * If the implicit `source.ctf.lttng-live` component exists, make sure
4330 * there's at least one leftover (which is the URL).
4332 if (implicit_lttng_live_args
.exists
&& bt_value_array_is_empty(leftovers
)) {
4333 BT_CLI_LOGE_APPEND_CAUSE("Missing URL for implicit `%s` component.",
4334 implicit_lttng_live_args
.comp_arg
->str
);
4338 /* Assign names to implicit components */
4339 for (i
= 0; i
< discovered_source_args
->len
; i
++) {
4340 struct implicit_component_args
*args
;
4343 args
= discovered_source_args
->pdata
[i
];
4345 g_string_printf(auto_disc_comp_name
, "auto-disc-%s", args
->comp_arg
->str
);
4347 /* Give it a name like `auto-disc-src-ctf-fs`. */
4348 for (j
= 0; j
< auto_disc_comp_name
->len
; j
++) {
4349 if (auto_disc_comp_name
->str
[j
] == '.') {
4350 auto_disc_comp_name
->str
[j
] = '-';
4354 ret
= assign_name_to_implicit_component(args
,
4355 auto_disc_comp_name
->str
, all_names
, &source_names
, true);
4361 ret
= assign_name_to_implicit_component(&implicit_lttng_live_args
,
4362 "lttng-live", all_names
, &source_names
, true);
4367 ret
= assign_name_to_implicit_component(&implicit_text_args
,
4368 "pretty", all_names
, &sink_names
, true);
4373 ret
= assign_name_to_implicit_component(&implicit_ctf_output_args
,
4374 "sink-ctf-fs", all_names
, &sink_names
, true);
4379 ret
= assign_name_to_implicit_component(&implicit_dummy_args
,
4380 "dummy", all_names
, &sink_names
, true);
4385 ret
= assign_name_to_implicit_component(&implicit_muxer_args
,
4386 "muxer", all_names
, NULL
, false);
4391 ret
= assign_name_to_implicit_component(&implicit_trimmer_args
,
4392 "trimmer", all_names
, NULL
, false);
4397 ret
= assign_name_to_implicit_component(&implicit_debug_info_args
,
4398 "debug-info", all_names
, NULL
, false);
4403 /* Make sure there's at least one source and one sink */
4404 if (!source_names
) {
4405 BT_CLI_LOGE_APPEND_CAUSE("No source component.");
4410 BT_CLI_LOGE_APPEND_CAUSE("No sink component.");
4415 * Prepend the muxer, the trimmer, and the debug info to the
4416 * filter chain so that we have:
4418 * sources -> muxer -> [trimmer] -> [debug info] ->
4419 * [user filters] -> sinks
4421 if (implicit_debug_info_args
.exists
) {
4422 if (g_list_prepend_gstring(&filter_names
,
4423 implicit_debug_info_args
.name_arg
->str
)) {
4428 if (implicit_trimmer_args
.exists
) {
4429 if (g_list_prepend_gstring(&filter_names
,
4430 implicit_trimmer_args
.name_arg
->str
)) {
4435 if (g_list_prepend_gstring(&filter_names
,
4436 implicit_muxer_args
.name_arg
->str
)) {
4441 * Append the equivalent run arguments for the implicit
4444 for (i
= 0; i
< discovered_source_args
->len
; i
++) {
4445 struct implicit_component_args
*args
=
4446 discovered_source_args
->pdata
[i
];
4448 ret
= append_run_args_for_implicit_component(args
, run_args
);
4454 ret
= append_run_args_for_implicit_component(&implicit_lttng_live_args
,
4460 ret
= append_run_args_for_implicit_component(&implicit_text_args
,
4466 ret
= append_run_args_for_implicit_component(&implicit_ctf_output_args
,
4472 ret
= append_run_args_for_implicit_component(&implicit_dummy_args
,
4478 ret
= append_run_args_for_implicit_component(&implicit_muxer_args
,
4484 ret
= append_run_args_for_implicit_component(&implicit_trimmer_args
,
4490 ret
= append_run_args_for_implicit_component(&implicit_debug_info_args
,
4496 /* Auto-connect components */
4497 ret
= convert_auto_connect(run_args
, source_names
, filter_names
,
4500 BT_CLI_LOGE_APPEND_CAUSE("Cannot auto-connect components.");
4505 * We have all the run command arguments now. Depending on
4506 * --run-args, we pass this to the run command or print them
4509 if (print_run_args
|| print_run_args_0
) {
4510 if (stream_intersection_mode
) {
4511 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --stream-intersection with --run-args or --run-args-0.");
4515 for (i
= 0; i
< bt_value_array_get_size(run_args
); i
++) {
4516 const bt_value
*arg_value
=
4517 bt_value_array_borrow_element_by_index(run_args
,
4520 GString
*quoted
= NULL
;
4521 const char *arg_to_print
;
4523 BT_ASSERT(arg_value
);
4524 arg
= bt_value_string_get(arg_value
);
4526 if (print_run_args
) {
4527 quoted
= bt_common_shell_quote(arg
, true);
4532 arg_to_print
= quoted
->str
;
4537 printf("%s", arg_to_print
);
4540 g_string_free(quoted
, TRUE
);
4543 if (i
< bt_value_array_get_size(run_args
) - 1) {
4544 if (print_run_args
) {
4553 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4558 * If the log level is still unset at this point, set it to
4559 * the program's default.
4561 if (*default_log_level
< 0) {
4562 *default_log_level
= cli_default_log_level
;
4565 cfg
= bt_config_run_from_args_array(run_args
, retcode
,
4566 force_omit_system_plugin_path
,
4567 force_omit_home_plugin_path
,
4568 initial_plugin_paths
, *default_log_level
);
4573 cfg
->cmd_data
.run
.stream_intersection_mode
= stream_intersection_mode
;
4578 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4582 * If the log level is still unset at this point, set it to
4583 * the program's default.
4585 if (*default_log_level
< 0) {
4586 *default_log_level
= cli_default_log_level
;
4589 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
4594 g_string_free(cur_name
, TRUE
);
4597 if (cur_name_prefix
) {
4598 g_string_free(cur_name_prefix
, TRUE
);
4601 bt_value_put_ref(run_args
);
4602 bt_value_put_ref(all_names
);
4603 destroy_glist_of_gstring(source_names
);
4604 destroy_glist_of_gstring(filter_names
);
4605 destroy_glist_of_gstring(sink_names
);
4606 bt_value_put_ref(leftovers
);
4607 finalize_implicit_component_args(&implicit_ctf_output_args
);
4608 finalize_implicit_component_args(&implicit_lttng_live_args
);
4609 finalize_implicit_component_args(&implicit_dummy_args
);
4610 finalize_implicit_component_args(&implicit_text_args
);
4611 finalize_implicit_component_args(&implicit_debug_info_args
);
4612 finalize_implicit_component_args(&implicit_muxer_args
);
4613 finalize_implicit_component_args(&implicit_trimmer_args
);
4614 bt_value_put_ref(plugin_paths
);
4615 bt_common_destroy_lttng_live_url_parts(<tng_live_url_parts
);
4616 auto_source_discovery_fini(&auto_disc
);
4618 if (discovered_source_args
) {
4619 g_ptr_array_free(discovered_source_args
, TRUE
);
4622 g_free(ctf_fs_source_clock_class_offset_arg
);
4623 g_free(ctf_fs_source_clock_class_offset_ns_arg
);
4625 if (auto_disc_comp_name
) {
4626 g_string_free(auto_disc_comp_name
, TRUE
);
4633 * Prints the Babeltrace 2.x general usage.
4636 void print_gen_usage(FILE *fp
)
4638 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] [COMMAND] [COMMAND ARGUMENTS]\n");
4640 fprintf(fp
, "General options:\n");
4642 fprintf(fp
, " -d, --debug Enable debug mode (same as --log-level=V)\n");
4643 fprintf(fp
, " -h, --help Show this help and quit\n");
4644 fprintf(fp
, " -l, --log-level=LVL Set the default log level to LVL (`N`, `V`, `D`,\n");
4645 fprintf(fp
, " `I`, `W` (default), `E`, or `F`)\n");
4646 fprintf(fp
, " -v, --verbose Enable verbose mode (same as --log-level=I)\n");
4647 fprintf(fp
, " -V, --version Show version and quit\n");
4649 fprintf(fp
, "Available commands:\n");
4651 fprintf(fp
, " convert Convert and trim traces (default)\n");
4652 fprintf(fp
, " help Get help for a plugin or a component class\n");
4653 fprintf(fp
, " list-plugins List available plugins and their content\n");
4654 fprintf(fp
, " query Query objects from a component class\n");
4655 fprintf(fp
, " run Build a processing graph and run it\n");
4657 fprintf(fp
, "Use `babeltrace2 COMMAND --help` to show the help of COMMAND.\n");
4660 struct bt_config
*bt_config_cli_args_create(int argc
, const char *argv
[],
4661 int *retcode
, bool force_omit_system_plugin_path
,
4662 bool force_omit_home_plugin_path
,
4663 const bt_value
*initial_plugin_paths
)
4665 struct bt_config
*config
= NULL
;
4668 const char **top_level_argv
;
4669 int command_argc
= -1;
4670 const char **command_argv
= NULL
;
4671 const char *command_name
= NULL
;
4672 int default_log_level
= -1;
4673 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
4675 /* Top-level option descriptions. */
4676 static const struct bt_argpar_opt_descr descrs
[] = {
4677 { OPT_DEBUG
, 'd', "debug", false },
4678 { OPT_HELP
, 'h', "help", false },
4679 { OPT_LOG_LEVEL
, 'l', "log-level", true },
4680 { OPT_VERBOSE
, 'v', "verbose", false },
4681 { OPT_VERSION
, 'V', "version", false},
4682 BT_ARGPAR_OPT_DESCR_SENTINEL
4686 COMMAND_TYPE_NONE
= -1,
4687 COMMAND_TYPE_RUN
= 0,
4688 COMMAND_TYPE_CONVERT
,
4689 COMMAND_TYPE_LIST_PLUGINS
,
4692 } command_type
= COMMAND_TYPE_NONE
;
4696 if (!initial_plugin_paths
) {
4697 initial_plugin_paths
= bt_value_array_create();
4698 if (!initial_plugin_paths
) {
4703 bt_value_get_ref(initial_plugin_paths
);
4709 print_gen_usage(stdout
);
4713 /* Skip first argument, the name of the program. */
4714 top_level_argc
= argc
- 1;
4715 top_level_argv
= argv
+ 1;
4716 argpar_parse_ret
= bt_argpar_parse(top_level_argc
, top_level_argv
,
4719 if (argpar_parse_ret
.error
) {
4720 BT_CLI_LOGE_APPEND_CAUSE(
4721 "While parsing command-line arguments: %s",
4722 argpar_parse_ret
.error
->str
);
4726 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
4727 struct bt_argpar_item
*item
;
4729 item
= g_ptr_array_index(argpar_parse_ret
.items
, i
);
4731 if (item
->type
== BT_ARGPAR_ITEM_TYPE_OPT
) {
4732 struct bt_argpar_item_opt
*item_opt
=
4733 (struct bt_argpar_item_opt
*) item
;
4735 switch (item_opt
->descr
->id
) {
4737 default_log_level
= BT_LOG_TRACE
;
4741 * Legacy: do not override a previous
4742 * --debug because --verbose and --debug
4743 * can be specified together (in this
4744 * case we want the lowest log level to
4747 default_log_level
= BT_LOG_INFO
;
4751 bt_log_get_level_from_string(item_opt
->arg
);
4752 if (default_log_level
< 0) {
4753 BT_CLI_LOGE_APPEND_CAUSE(
4754 "Invalid argument for --log-level option:\n %s",
4763 print_gen_usage(stdout
);
4766 } else if (item
->type
== BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
4767 struct bt_argpar_item_non_opt
*item_non_opt
=
4768 (struct bt_argpar_item_non_opt
*) item
;
4770 * First unknown argument: is it a known command
4774 top_level_argc
- item_non_opt
->orig_index
- 1;
4776 &top_level_argv
[item_non_opt
->orig_index
+ 1];
4778 if (strcmp(item_non_opt
->arg
, "convert") == 0) {
4779 command_type
= COMMAND_TYPE_CONVERT
;
4780 } else if (strcmp(item_non_opt
->arg
, "list-plugins") == 0) {
4781 command_type
= COMMAND_TYPE_LIST_PLUGINS
;
4782 } else if (strcmp(item_non_opt
->arg
, "help") == 0) {
4783 command_type
= COMMAND_TYPE_HELP
;
4784 } else if (strcmp(item_non_opt
->arg
, "query") == 0) {
4785 command_type
= COMMAND_TYPE_QUERY
;
4786 } else if (strcmp(item_non_opt
->arg
, "run") == 0) {
4787 command_type
= COMMAND_TYPE_RUN
;
4790 * Non-option argument, but not a known
4791 * command name: assume the default
4792 * `convert` command.
4794 command_type
= COMMAND_TYPE_CONVERT
;
4795 command_name
= "convert";
4803 if (command_type
== COMMAND_TYPE_NONE
) {
4804 if (argpar_parse_ret
.ingested_orig_args
== top_level_argc
) {
4806 * We only got non-help, non-version general options
4807 * like --verbose and --debug, without any other
4808 * arguments, so we can't do anything useful: print the
4811 print_gen_usage(stdout
);
4816 * We stopped on an unknown option argument (and therefore
4817 * didn't see a command name). Assume `convert` command.
4819 command_type
= COMMAND_TYPE_CONVERT
;
4820 command_name
= "convert";
4822 top_level_argc
- argpar_parse_ret
.ingested_orig_args
;
4824 &top_level_argv
[argpar_parse_ret
.ingested_orig_args
];
4827 BT_ASSERT(command_argv
);
4828 BT_ASSERT(command_argc
>= 0);
4831 * The convert command can set its own default log level for
4832 * backward compatibility reasons. It only does so if there's no
4833 * log level yet, so do not force one for this command.
4835 if (command_type
!= COMMAND_TYPE_CONVERT
&& default_log_level
< 0) {
4836 /* Default log level */
4837 default_log_level
= cli_default_log_level
;
4840 switch (command_type
) {
4841 case COMMAND_TYPE_RUN
:
4842 config
= bt_config_run_from_args(command_argc
, command_argv
,
4843 retcode
, force_omit_system_plugin_path
,
4844 force_omit_home_plugin_path
, initial_plugin_paths
,
4847 case COMMAND_TYPE_CONVERT
:
4848 config
= bt_config_convert_from_args(command_argc
, command_argv
,
4849 retcode
, force_omit_system_plugin_path
,
4850 force_omit_home_plugin_path
,
4851 initial_plugin_paths
, &default_log_level
);
4853 case COMMAND_TYPE_LIST_PLUGINS
:
4854 config
= bt_config_list_plugins_from_args(command_argc
,
4855 command_argv
, retcode
, force_omit_system_plugin_path
,
4856 force_omit_home_plugin_path
, initial_plugin_paths
);
4858 case COMMAND_TYPE_HELP
:
4859 config
= bt_config_help_from_args(command_argc
,
4860 command_argv
, retcode
, force_omit_system_plugin_path
,
4861 force_omit_home_plugin_path
, initial_plugin_paths
,
4864 case COMMAND_TYPE_QUERY
:
4865 config
= bt_config_query_from_args(command_argc
,
4866 command_argv
, retcode
, force_omit_system_plugin_path
,
4867 force_omit_home_plugin_path
, initial_plugin_paths
,
4875 BT_ASSERT(default_log_level
>= BT_LOG_TRACE
);
4876 config
->log_level
= default_log_level
;
4877 config
->command_name
= command_name
;
4886 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
4887 bt_value_put_ref(initial_plugin_paths
);