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 const struct bt_argpar_opt_descr query_options
[] = {
1563 /* id, short_name, long_name, with_arg */
1564 { OPT_HELP
, 'h', "help", false },
1565 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
1566 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
1567 { OPT_PARAMS
, 'p', "params", true },
1568 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
1569 BT_ARGPAR_OPT_DESCR_SENTINEL
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
)
1586 struct bt_config
*cfg
= NULL
;
1587 const char *component_class_spec
= NULL
;
1588 const char *query_object
= NULL
;
1590 GString
*error_str
= NULL
;
1591 struct bt_argpar_parse_ret argpar_parse_ret
;
1593 params
= bt_value_null
;
1594 bt_value_get_ref(bt_value_null
);
1597 cfg
= bt_config_query_create(initial_plugin_paths
);
1602 error_str
= g_string_new(NULL
);
1604 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1608 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
1609 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
1610 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
1616 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, query_options
, true);
1617 if (argpar_parse_ret
.error
) {
1618 BT_CLI_LOGE_APPEND_CAUSE(
1619 "While parsing `query` command's command-line arguments: %s",
1620 argpar_parse_ret
.error
->str
);
1624 if (help_option_is_specified(&argpar_parse_ret
)) {
1625 print_query_usage(stdout
);
1627 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1631 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
1632 struct bt_argpar_item
*argpar_item
=
1633 g_ptr_array_index(argpar_parse_ret
.items
, i
);
1635 if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_OPT
) {
1636 struct bt_argpar_item_opt
*argpar_item_opt
=
1637 (struct bt_argpar_item_opt
*) argpar_item
;
1638 const char *arg
= argpar_item_opt
->arg
;
1640 switch (argpar_item_opt
->descr
->id
) {
1641 case OPT_PLUGIN_PATH
:
1642 if (bt_config_append_plugin_paths_check_setuid_setgid(
1643 cfg
->plugin_paths
, arg
)) {
1647 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
1648 cfg
->omit_system_plugin_path
= true;
1650 case OPT_OMIT_HOME_PLUGIN_PATH
:
1651 cfg
->omit_home_plugin_path
= true;
1655 bt_value_put_ref(params
);
1656 params
= cli_value_from_arg(arg
, error_str
);
1658 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
1665 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1666 argpar_item_opt
->descr
->id
);
1670 struct bt_argpar_item_non_opt
*argpar_item_non_opt
1671 = (struct bt_argpar_item_non_opt
*) argpar_item
;
1674 * We need exactly two leftover arguments which are the
1675 * mandatory component class specification and query object.
1677 if (!component_class_spec
) {
1678 component_class_spec
= argpar_item_non_opt
->arg
;
1679 } else if (!query_object
) {
1680 query_object
= argpar_item_non_opt
->arg
;
1682 BT_CLI_LOGE_APPEND_CAUSE("Extraneous command-line argument specified to `query` command: `%s`.",
1683 argpar_item_non_opt
->arg
);
1689 if (!component_class_spec
|| !query_object
) {
1690 print_query_usage(stdout
);
1692 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1696 cfg
->cmd_data
.query
.cfg_component
=
1697 bt_config_component_from_arg(component_class_spec
,
1699 if (!cfg
->cmd_data
.query
.cfg_component
) {
1700 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for component class specification:\n %s",
1701 component_class_spec
);
1706 BT_OBJECT_MOVE_REF(cfg
->cmd_data
.query
.cfg_component
->params
, params
);
1708 if (strlen(query_object
) == 0) {
1709 BT_CLI_LOGE_APPEND_CAUSE("Invalid empty object.");
1713 g_string_assign(cfg
->cmd_data
.query
.object
, query_object
);
1715 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
1723 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1726 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
1729 g_string_free(error_str
, TRUE
);
1732 bt_value_put_ref(params
);
1737 * Prints the list-plugins command usage.
1740 void print_list_plugins_usage(FILE *fp
)
1742 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] list-plugins [OPTIONS]\n");
1744 fprintf(fp
, "Options:\n");
1746 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1747 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
1748 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1749 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1750 fprintf(fp
, " dynamic plugins can be loaded\n");
1751 fprintf(fp
, " -h, --help Show this help and quit\n");
1753 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1755 fprintf(fp
, "Use `babeltrace2 help` to get help for a specific plugin or component class.\n");
1759 const struct bt_argpar_opt_descr list_plugins_options
[] = {
1760 /* id, short_name, long_name, with_arg */
1761 { OPT_HELP
, 'h', "help", false },
1762 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
1763 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
1764 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
1765 BT_ARGPAR_OPT_DESCR_SENTINEL
1769 * Creates a Babeltrace config object from the arguments of a
1770 * list-plugins command.
1772 * *retcode is set to the appropriate exit code to use.
1775 struct bt_config
*bt_config_list_plugins_from_args(int argc
, const char *argv
[],
1776 int *retcode
, bool force_omit_system_plugin_path
,
1777 bool force_omit_home_plugin_path
,
1778 const bt_value
*initial_plugin_paths
)
1781 struct bt_config
*cfg
= NULL
;
1782 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
1785 cfg
= bt_config_list_plugins_create(initial_plugin_paths
);
1790 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
1791 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
1792 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
1798 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, list_plugins_options
, true);
1799 if (argpar_parse_ret
.error
) {
1800 BT_CLI_LOGE_APPEND_CAUSE(
1801 "While parsing `list-plugins` command's command-line arguments: %s",
1802 argpar_parse_ret
.error
->str
);
1806 if (help_option_is_specified(&argpar_parse_ret
)) {
1807 print_list_plugins_usage(stdout
);
1809 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1813 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
1814 struct bt_argpar_item
*argpar_item
=
1815 g_ptr_array_index(argpar_parse_ret
.items
, i
);
1816 struct bt_argpar_item_opt
*argpar_item_opt
;
1819 if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
1820 struct bt_argpar_item_non_opt
*argpar_item_non_opt
1821 = (struct bt_argpar_item_non_opt
*) argpar_item
;
1823 BT_CLI_LOGE_APPEND_CAUSE("Unexpected argument: `%s`.",
1824 argpar_item_non_opt
->arg
);
1828 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
1829 arg
= argpar_item_opt
->arg
;
1831 switch (argpar_item_opt
->descr
->id
) {
1832 case OPT_PLUGIN_PATH
:
1833 if (bt_config_append_plugin_paths_check_setuid_setgid(
1834 cfg
->plugin_paths
, arg
)) {
1838 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
1839 cfg
->omit_system_plugin_path
= true;
1841 case OPT_OMIT_HOME_PLUGIN_PATH
:
1842 cfg
->omit_home_plugin_path
= true;
1845 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1846 argpar_item_opt
->descr
->id
);
1851 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
1859 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1862 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
1868 * Prints the run command usage.
1871 void print_run_usage(FILE *fp
)
1873 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] run [OPTIONS]\n");
1875 fprintf(fp
, "Options:\n");
1877 fprintf(fp
, " -b, --base-params=PARAMS Set PARAMS as the current base parameters\n");
1878 fprintf(fp
, " for all the following components until\n");
1879 fprintf(fp
, " --reset-base-params is encountered\n");
1880 fprintf(fp
, " (see the expected format of PARAMS below)\n");
1881 fprintf(fp
, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
1882 fprintf(fp
, " Instantiate the component class CLS of type\n");
1883 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
1884 fprintf(fp
, " in the plugin PLUGIN, add it to the graph,\n");
1885 fprintf(fp
, " and optionally name it NAME (you can also\n");
1886 fprintf(fp
, " specify the name with --name)\n");
1887 fprintf(fp
, " -x, --connect=CONNECTION Connect two created components (see the\n");
1888 fprintf(fp
, " expected format of CONNECTION below)\n");
1889 fprintf(fp
, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
1890 fprintf(fp
, " (`N`, `V`, `D`, `I`, `W`, `E`, or `F`)\n");
1891 fprintf(fp
, " -n, --name=NAME Set the name of the current component\n");
1892 fprintf(fp
, " to NAME (must be unique amongst all the\n");
1893 fprintf(fp
, " names of the created components)\n");
1894 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1895 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
1896 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1897 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
1898 fprintf(fp
, " current component (see the expected format\n");
1899 fprintf(fp
, " of PARAMS below)\n");
1900 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1901 fprintf(fp
, " dynamic plugins can be loaded\n");
1902 fprintf(fp
, " -r, --reset-base-params Reset the current base parameters to an\n");
1903 fprintf(fp
, " empty map\n");
1904 fprintf(fp
, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
1905 fprintf(fp
, " the graph later, retry in DUR µs\n");
1906 fprintf(fp
, " (default: 100000)\n");
1907 fprintf(fp
, " -h, --help Show this help and quit\n");
1909 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1910 fprintf(fp
, "\n\n");
1911 fprintf(fp
, "Expected format of CONNECTION\n");
1912 fprintf(fp
, "-----------------------------\n");
1914 fprintf(fp
, " UPSTREAM[.UPSTREAM-PORT]:DOWNSTREAM[.DOWNSTREAM-PORT]\n");
1916 fprintf(fp
, "UPSTREAM and DOWNSTREAM are names of the upstream and downstream\n");
1917 fprintf(fp
, "components to connect together. You must escape the following characters\n\n");
1918 fprintf(fp
, "with `\\`: `\\`, `.`, and `:`. You can set the name of the current\n");
1919 fprintf(fp
, "component with the --name option.\n");
1921 fprintf(fp
, "UPSTREAM-PORT and DOWNSTREAM-PORT are optional globbing patterns to\n");
1922 fprintf(fp
, "identify the upstream and downstream ports to use for the connection.\n");
1923 fprintf(fp
, "When the port is not specified, `*` is used.\n");
1925 fprintf(fp
, "When a component named UPSTREAM has an available port which matches the\n");
1926 fprintf(fp
, "UPSTREAM-PORT globbing pattern, it is connected to the first port which\n");
1927 fprintf(fp
, "matches the DOWNSTREAM-PORT globbing pattern of the component named\n");
1928 fprintf(fp
, "DOWNSTREAM.\n");
1930 fprintf(fp
, "The only special character in UPSTREAM-PORT and DOWNSTREAM-PORT is `*`\n");
1931 fprintf(fp
, "which matches anything. You must escape the following characters\n");
1932 fprintf(fp
, "with `\\`: `\\`, `*`, `?`, `[`, `.`, and `:`.\n");
1934 fprintf(fp
, "You can connect a source component to a filter or sink component. You\n");
1935 fprintf(fp
, "can connect a filter component to a sink component.\n");
1937 fprintf(fp
, "Examples:\n");
1939 fprintf(fp
, " my-src:my-sink\n");
1940 fprintf(fp
, " ctf-fs.*stream*:utils-muxer:*\n");
1942 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1943 fprintf(fp
, "babeltrace2 from a shell.\n");
1944 fprintf(fp
, "\n\n");
1945 print_expected_params_format(fp
);
1949 * Creates a Babeltrace config object from the arguments of a run
1952 * *retcode is set to the appropriate exit code to use.
1955 struct bt_config
*bt_config_run_from_args(int argc
, const char *argv
[],
1956 int *retcode
, bool force_omit_system_plugin_path
,
1957 bool force_omit_home_plugin_path
,
1958 const bt_value
*initial_plugin_paths
, int default_log_level
)
1960 struct bt_config_component
*cur_cfg_comp
= NULL
;
1961 enum bt_config_component_dest cur_cfg_comp_dest
=
1962 BT_CONFIG_COMPONENT_DEST_UNKNOWN
;
1963 bt_value
*cur_base_params
= NULL
;
1965 struct bt_config
*cfg
= NULL
;
1966 bt_value
*instance_names
= NULL
;
1967 bt_value
*connection_args
= NULL
;
1968 char error_buf
[256] = { 0 };
1969 long retry_duration
= -1;
1970 bt_value_map_extend_status extend_status
;
1971 GString
*error_str
= NULL
;
1972 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
1975 static const struct bt_argpar_opt_descr run_options
[] = {
1976 { OPT_BASE_PARAMS
, 'b', "base-params", true },
1977 { OPT_COMPONENT
, 'c', "component", true },
1978 { OPT_CONNECT
, 'x', "connect", true },
1979 { OPT_HELP
, 'h', "help", false },
1980 { OPT_LOG_LEVEL
, 'l', "log-level", true },
1981 { OPT_NAME
, 'n', "name", true },
1982 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
1983 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
1984 { OPT_PARAMS
, 'p', "params", true },
1985 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
1986 { OPT_RESET_BASE_PARAMS
, 'r', "reset-base-params", false },
1987 { OPT_RETRY_DURATION
, '\0', "retry-duration", true },
1988 BT_ARGPAR_OPT_DESCR_SENTINEL
1993 error_str
= g_string_new(NULL
);
1995 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2000 print_run_usage(stdout
);
2005 cfg
= bt_config_run_create(initial_plugin_paths
);
2010 cfg
->cmd_data
.run
.retry_duration_us
= 100000;
2011 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
2012 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
2013 cur_base_params
= bt_value_map_create();
2014 if (!cur_base_params
) {
2015 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2019 instance_names
= bt_value_map_create();
2020 if (!instance_names
) {
2021 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2025 connection_args
= bt_value_array_create();
2026 if (!connection_args
) {
2027 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2031 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
2037 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, run_options
, true);
2038 if (argpar_parse_ret
.error
) {
2039 BT_CLI_LOGE_APPEND_CAUSE(
2040 "While parsing `run` command's command-line arguments: %s",
2041 argpar_parse_ret
.error
->str
);
2045 if (help_option_is_specified(&argpar_parse_ret
)) {
2046 print_run_usage(stdout
);
2048 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2052 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
2053 struct bt_argpar_item
*argpar_item
=
2054 g_ptr_array_index(argpar_parse_ret
.items
, i
);
2055 struct bt_argpar_item_opt
*argpar_item_opt
;
2058 /* This command does not accept leftover arguments. */
2059 if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
2060 struct bt_argpar_item_non_opt
*argpar_nonopt_item
=
2061 (struct bt_argpar_item_non_opt
*) argpar_item
;
2063 BT_CLI_LOGE_APPEND_CAUSE("Unexpected argument: `%s`",
2064 argpar_nonopt_item
->arg
);
2068 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
2069 arg
= argpar_item_opt
->arg
;
2071 switch (argpar_item_opt
->descr
->id
) {
2072 case OPT_PLUGIN_PATH
:
2073 if (bt_config_append_plugin_paths_check_setuid_setgid(
2074 cfg
->plugin_paths
, arg
)) {
2078 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
2079 cfg
->omit_system_plugin_path
= true;
2081 case OPT_OMIT_HOME_PLUGIN_PATH
:
2082 cfg
->omit_home_plugin_path
= true;
2086 enum bt_config_component_dest new_dest
;
2089 ret
= add_run_cfg_comp_check_name(cfg
,
2090 cur_cfg_comp
, cur_cfg_comp_dest
,
2092 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2098 cur_cfg_comp
= bt_config_component_from_arg(arg
,
2100 if (!cur_cfg_comp
) {
2101 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --component option's argument:\n %s",
2106 switch (cur_cfg_comp
->type
) {
2107 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2108 new_dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
2110 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2111 new_dest
= BT_CONFIG_COMPONENT_DEST_FILTER
;
2113 case BT_COMPONENT_CLASS_TYPE_SINK
:
2114 new_dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
2120 BT_ASSERT(cur_base_params
);
2121 bt_value_put_ref(cur_cfg_comp
->params
);
2122 if (bt_value_copy(cur_base_params
,
2123 &cur_cfg_comp
->params
) < 0) {
2124 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2128 cur_cfg_comp_dest
= new_dest
;
2134 bt_value
*params_to_set
;
2136 if (!cur_cfg_comp
) {
2137 BT_CLI_LOGE_APPEND_CAUSE("Cannot add parameters to unavailable component:\n %s",
2142 params
= cli_value_from_arg(arg
, error_str
);
2144 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
2149 extend_status
= bt_value_map_extend(
2150 cur_cfg_comp
->params
, params
, ¶ms_to_set
);
2151 BT_VALUE_PUT_REF_AND_RESET(params
);
2152 if (extend_status
!= BT_VALUE_MAP_EXTEND_STATUS_OK
) {
2153 BT_CLI_LOGE_APPEND_CAUSE("Cannot extend current component parameters with --params option's argument:\n %s",
2158 BT_OBJECT_MOVE_REF(cur_cfg_comp
->params
, params_to_set
);
2162 if (!cur_cfg_comp
) {
2163 BT_CLI_LOGE_APPEND_CAUSE("Cannot set the name of unavailable component:\n %s",
2168 g_string_assign(cur_cfg_comp
->instance_name
, arg
);
2171 if (!cur_cfg_comp
) {
2172 BT_CLI_LOGE_APPEND_CAUSE("Cannot set the log level of unavailable component:\n %s",
2177 cur_cfg_comp
->log_level
=
2178 bt_log_get_level_from_string(arg
);
2179 if (cur_cfg_comp
->log_level
< 0) {
2180 BT_CLI_LOGE_APPEND_CAUSE("Invalid argument for --log-level option:\n %s",
2185 case OPT_BASE_PARAMS
:
2187 bt_value
*params
= cli_value_from_arg(arg
, error_str
);
2190 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --base-params option's argument:\n %s",
2195 BT_OBJECT_MOVE_REF(cur_base_params
, params
);
2198 case OPT_RESET_BASE_PARAMS
:
2199 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
2200 cur_base_params
= bt_value_map_create();
2201 if (!cur_base_params
) {
2202 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2207 if (bt_value_array_append_string_element(
2208 connection_args
, arg
)) {
2209 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2213 case OPT_RETRY_DURATION
: {
2215 size_t arg_len
= strlen(argpar_item_opt
->arg
);
2217 retry_duration
= g_ascii_strtoll(argpar_item_opt
->arg
, &end
, 10);
2219 if (arg_len
== 0 || end
!= (argpar_item_opt
->arg
+ arg_len
)) {
2220 BT_CLI_LOGE_APPEND_CAUSE(
2221 "Could not parse --retry-duration option's argument as an unsigned integer: `%s`",
2222 argpar_item_opt
->arg
);
2226 if (retry_duration
< 0) {
2227 BT_CLI_LOGE_APPEND_CAUSE("--retry-duration option's argument must be positive or 0: %ld",
2232 cfg
->cmd_data
.run
.retry_duration_us
=
2233 (uint64_t) retry_duration
;
2237 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
2238 argpar_item_opt
->descr
->id
);
2243 /* Add current component */
2245 ret
= add_run_cfg_comp_check_name(cfg
, cur_cfg_comp
,
2246 cur_cfg_comp_dest
, instance_names
);
2247 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2253 if (cfg
->cmd_data
.run
.sources
->len
== 0) {
2254 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no source component.");
2258 if (cfg
->cmd_data
.run
.sinks
->len
== 0) {
2259 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no sink component.");
2263 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
2267 ret
= bt_config_cli_args_create_connections(cfg
,
2271 BT_CLI_LOGE_APPEND_CAUSE("Cannot creation connections:\n%s", error_buf
);
2279 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2283 g_string_free(error_str
, TRUE
);
2286 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
2287 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2288 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
2289 BT_VALUE_PUT_REF_AND_RESET(instance_names
);
2290 BT_VALUE_PUT_REF_AND_RESET(connection_args
);
2295 struct bt_config
*bt_config_run_from_args_array(const bt_value
*run_args
,
2296 int *retcode
, bool force_omit_system_plugin_path
,
2297 bool force_omit_home_plugin_path
,
2298 const bt_value
*initial_plugin_paths
, int default_log_level
)
2300 struct bt_config
*cfg
= NULL
;
2303 const size_t argc
= bt_value_array_get_size(run_args
);
2305 argv
= calloc(argc
, sizeof(*argv
));
2307 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2311 len
= bt_value_array_get_size(run_args
);
2313 BT_CLI_LOGE_APPEND_CAUSE("Invalid executable arguments.");
2316 for (i
= 0; i
< len
; i
++) {
2317 const bt_value
*arg_value
=
2318 bt_value_array_borrow_element_by_index_const(run_args
,
2322 BT_ASSERT(arg_value
);
2323 arg
= bt_value_string_get(arg_value
);
2328 cfg
= bt_config_run_from_args(argc
, argv
, retcode
,
2329 force_omit_system_plugin_path
, force_omit_home_plugin_path
,
2330 initial_plugin_paths
, default_log_level
);
2338 * Prints the convert command usage.
2341 void print_convert_usage(FILE *fp
)
2343 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] [convert] [OPTIONS] [PATH/URL]\n");
2345 fprintf(fp
, "Options:\n");
2347 fprintf(fp
, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2348 fprintf(fp
, " Instantiate the component class CLS of type\n");
2349 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
2350 fprintf(fp
, " in the plugin PLUGIN, add it to the\n");
2351 fprintf(fp
, " conversion graph, and optionally name it\n");
2352 fprintf(fp
, " NAME (you can also specify the name with\n");
2353 fprintf(fp
, " --name)\n");
2354 fprintf(fp
, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
2355 fprintf(fp
, " (`N`, `V`, `D`, `I`, `W`, `E`, or `F`)\n");
2356 fprintf(fp
, " --name=NAME Set the name of the current component\n");
2357 fprintf(fp
, " to NAME (must be unique amongst all the\n");
2358 fprintf(fp
, " names of the created components)\n");
2359 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2360 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
2361 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2362 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2363 fprintf(fp
, " current component (see the expected format\n");
2364 fprintf(fp
, " of PARAMS below)\n");
2365 fprintf(fp
, " -P, --path=PATH Set the `path` string parameter of the\n");
2366 fprintf(fp
, " current component to PATH\n");
2367 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2368 fprintf(fp
, " dynamic plugins can be loaded\n");
2369 fprintf(fp
, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
2370 fprintf(fp
, " the graph later, retry in DUR µs\n");
2371 fprintf(fp
, " (default: 100000)\n");
2372 fprintf(fp
, " dynamic plugins can be loaded\n");
2373 fprintf(fp
, " --run-args Print the equivalent arguments for the\n");
2374 fprintf(fp
, " `run` command to the standard output,\n");
2375 fprintf(fp
, " formatted for a shell, and quit\n");
2376 fprintf(fp
, " --run-args-0 Print the equivalent arguments for the\n");
2377 fprintf(fp
, " `run` command to the standard output,\n");
2378 fprintf(fp
, " formatted for `xargs -0`, and quit\n");
2379 fprintf(fp
, " --stream-intersection Only process events when all streams\n");
2380 fprintf(fp
, " are active\n");
2381 fprintf(fp
, " -u, --url=URL Set the `url` string parameter of the\n");
2382 fprintf(fp
, " current component to URL\n");
2383 fprintf(fp
, " -h, --help Show this help and quit\n");
2385 fprintf(fp
, "Implicit `source.ctf.fs` component options:\n");
2387 fprintf(fp
, " --clock-offset=SEC Set clock offset to SEC seconds\n");
2388 fprintf(fp
, " --clock-offset-ns=NS Set clock offset to NS ns\n");
2390 fprintf(fp
, "Implicit `sink.text.pretty` component options:\n");
2392 fprintf(fp
, " --clock-cycles Print timestamps in clock cycles\n");
2393 fprintf(fp
, " --clock-date Print timestamp dates\n");
2394 fprintf(fp
, " --clock-gmt Print and parse timestamps in the GMT\n");
2395 fprintf(fp
, " time zone instead of the local time zone\n");
2396 fprintf(fp
, " --clock-seconds Print the timestamps as `SEC.NS` instead\n");
2397 fprintf(fp
, " of `hh:mm:ss.nnnnnnnnn`\n");
2398 fprintf(fp
, " --color=(never | auto | always)\n");
2399 fprintf(fp
, " Never, automatically, or always emit\n");
2400 fprintf(fp
, " console color codes\n");
2401 fprintf(fp
, " -f, --fields=FIELD[,FIELD]... Print additional fields; FIELD can be:\n");
2402 fprintf(fp
, " `all`, `trace`, `trace:hostname`,\n");
2403 fprintf(fp
, " `trace:domain`, `trace:procname`,\n");
2404 fprintf(fp
, " `trace:vpid`, `loglevel`, `emf`\n");
2405 fprintf(fp
, " -n, --names=NAME[,NAME]... Print field names; NAME can be:\n");
2406 fprintf(fp
, " `payload` (or `arg` or `args`), `none`,\n");
2407 fprintf(fp
, " `all`, `scope`, `header`, `context`\n");
2408 fprintf(fp
, " (or `ctx`)\n");
2409 fprintf(fp
, " --no-delta Do not print time delta between\n");
2410 fprintf(fp
, " consecutive events\n");
2411 fprintf(fp
, " -w, --output=PATH Write output text to PATH instead of\n");
2412 fprintf(fp
, " the standard output\n");
2414 fprintf(fp
, "Implicit `filter.utils.muxer` component options:\n");
2416 fprintf(fp
, " --clock-force-correlate Assume that clocks are inherently\n");
2417 fprintf(fp
, " correlated across traces\n");
2419 fprintf(fp
, "Implicit `filter.utils.trimmer` component options:\n");
2421 fprintf(fp
, " -b, --begin=BEGIN Set the beginning time of the conversion\n");
2422 fprintf(fp
, " time range to BEGIN (see the format of\n");
2423 fprintf(fp
, " BEGIN below)\n");
2424 fprintf(fp
, " -e, --end=END Set the end time of the conversion time\n");
2425 fprintf(fp
, " range to END (see the format of END below)\n");
2426 fprintf(fp
, " -t, --timerange=TIMERANGE Set conversion time range to TIMERANGE:\n");
2427 fprintf(fp
, " BEGIN,END or [BEGIN,END] (literally `[` and\n");
2428 fprintf(fp
, " `]`) (see the format of BEGIN/END below)\n");
2430 fprintf(fp
, "Implicit `filter.lttng-utils.debug-info` component options:\n");
2432 fprintf(fp
, " --debug-info Create an implicit\n");
2433 fprintf(fp
, " `filter.lttng-utils.debug-info` component\n");
2434 fprintf(fp
, " --debug-info-dir=DIR Search for debug info in directory DIR\n");
2435 fprintf(fp
, " instead of `/usr/lib/debug`\n");
2436 fprintf(fp
, " --debug-info-full-path Show full debug info source and\n");
2437 fprintf(fp
, " binary paths instead of just names\n");
2438 fprintf(fp
, " --debug-info-target-prefix=DIR\n");
2439 fprintf(fp
, " Use directory DIR as a prefix when\n");
2440 fprintf(fp
, " looking up executables during debug\n");
2441 fprintf(fp
, " info analysis\n");
2443 fprintf(fp
, "Legacy options that still work:\n");
2445 fprintf(fp
, " -i, --input-format=(ctf | lttng-live)\n");
2446 fprintf(fp
, " `ctf`:\n");
2447 fprintf(fp
, " Create an implicit `source.ctf.fs`\n");
2448 fprintf(fp
, " component\n");
2449 fprintf(fp
, " `lttng-live`:\n");
2450 fprintf(fp
, " Create an implicit `source.ctf.lttng-live`\n");
2451 fprintf(fp
, " component\n");
2452 fprintf(fp
, " -o, --output-format=(text | ctf | dummy | ctf-metadata)\n");
2453 fprintf(fp
, " `text`:\n");
2454 fprintf(fp
, " Create an implicit `sink.text.pretty`\n");
2455 fprintf(fp
, " component\n");
2456 fprintf(fp
, " `ctf`:\n");
2457 fprintf(fp
, " Create an implicit `sink.ctf.fs`\n");
2458 fprintf(fp
, " component\n");
2459 fprintf(fp
, " `dummy`:\n");
2460 fprintf(fp
, " Create an implicit `sink.utils.dummy`\n");
2461 fprintf(fp
, " component\n");
2462 fprintf(fp
, " `ctf-metadata`:\n");
2463 fprintf(fp
, " Query the `source.ctf.fs` component class\n");
2464 fprintf(fp
, " for metadata text and quit\n");
2466 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
2467 fprintf(fp
, "\n\n");
2468 fprintf(fp
, "Format of BEGIN and END\n");
2469 fprintf(fp
, "-----------------------\n");
2471 fprintf(fp
, " [YYYY-MM-DD [hh:mm:]]ss[.nnnnnnnnn]\n");
2472 fprintf(fp
, "\n\n");
2473 print_expected_params_format(fp
);
2477 const struct bt_argpar_opt_descr convert_options
[] = {
2478 /* id, short_name, long_name, with_arg */
2479 { OPT_BEGIN
, 'b', "begin", true },
2480 { OPT_CLOCK_CYCLES
, '\0', "clock-cycles", false },
2481 { OPT_CLOCK_DATE
, '\0', "clock-date", false },
2482 { OPT_CLOCK_FORCE_CORRELATE
, '\0', "clock-force-correlate", false },
2483 { OPT_CLOCK_GMT
, '\0', "clock-gmt", false },
2484 { OPT_CLOCK_OFFSET
, '\0', "clock-offset", true },
2485 { OPT_CLOCK_OFFSET_NS
, '\0', "clock-offset-ns", true },
2486 { OPT_CLOCK_SECONDS
, '\0', "clock-seconds", false },
2487 { OPT_COLOR
, '\0', "color", true },
2488 { OPT_COMPONENT
, 'c', "component", true },
2489 { OPT_DEBUG
, 'd', "debug", false },
2490 { OPT_DEBUG_INFO_DIR
, '\0', "debug-info-dir", true },
2491 { OPT_DEBUG_INFO_FULL_PATH
, '\0', "debug-info-full-path", false },
2492 { OPT_DEBUG_INFO_TARGET_PREFIX
, '\0', "debug-info-target-prefix", true },
2493 { OPT_END
, 'e', "end", true },
2494 { OPT_FIELDS
, 'f', "fields", true },
2495 { OPT_HELP
, 'h', "help", false },
2496 { OPT_INPUT_FORMAT
, 'i', "input-format", true },
2497 { OPT_LOG_LEVEL
, 'l', "log-level", true },
2498 { OPT_NAME
, '\0', "name", true },
2499 { OPT_NAMES
, 'n', "names", true },
2500 { OPT_DEBUG_INFO
, '\0', "debug-info", false },
2501 { OPT_NO_DELTA
, '\0', "no-delta", false },
2502 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
2503 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
2504 { OPT_OUTPUT
, 'w', "output", true },
2505 { OPT_OUTPUT_FORMAT
, 'o', "output-format", true },
2506 { OPT_PARAMS
, 'p', "params", true },
2507 { OPT_PATH
, 'P', "path", true },
2508 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
2509 { OPT_RETRY_DURATION
, '\0', "retry-duration", true },
2510 { OPT_RUN_ARGS
, '\0', "run-args", false },
2511 { OPT_RUN_ARGS_0
, '\0', "run-args-0", false },
2512 { OPT_STREAM_INTERSECTION
, '\0', "stream-intersection", false },
2513 { OPT_TIMERANGE
, '\0', "timerange", true },
2514 { OPT_URL
, 'u', "url", true },
2515 { OPT_VERBOSE
, 'v', "verbose", false },
2516 BT_ARGPAR_OPT_DESCR_SENTINEL
2520 GString
*get_component_auto_name(const char *prefix
,
2521 const bt_value
*existing_names
)
2524 GString
*auto_name
= g_string_new(NULL
);
2527 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2531 if (!bt_value_map_has_entry(existing_names
, prefix
)) {
2532 g_string_assign(auto_name
, prefix
);
2537 g_string_printf(auto_name
, "%s-%d", prefix
, i
);
2539 } while (bt_value_map_has_entry(existing_names
, auto_name
->str
));
2545 struct implicit_component_args
{
2548 /* The component class name (e.g. src.ctf.fs). */
2551 /* The component instance name. */
2554 GString
*params_arg
;
2555 bt_value
*extra_params
;
2559 int assign_name_to_implicit_component(struct implicit_component_args
*args
,
2560 const char *prefix
, bt_value
*existing_names
,
2561 GList
**comp_names
, bool append_to_comp_names
)
2564 GString
*name
= NULL
;
2566 if (!args
->exists
) {
2570 name
= get_component_auto_name(prefix
,
2578 g_string_assign(args
->name_arg
, name
->str
);
2580 if (bt_value_map_insert_entry(existing_names
, name
->str
,
2582 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2587 if (append_to_comp_names
) {
2588 *comp_names
= g_list_append(*comp_names
, name
);
2594 g_string_free(name
, TRUE
);
2601 int append_run_args_for_implicit_component(
2602 struct implicit_component_args
*impl_args
,
2608 if (!impl_args
->exists
) {
2612 if (bt_value_array_append_string_element(run_args
, "--component")) {
2613 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2617 if (bt_value_array_append_string_element(run_args
, impl_args
->comp_arg
->str
)) {
2618 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2622 if (bt_value_array_append_string_element(run_args
, "--name")) {
2623 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2627 if (bt_value_array_append_string_element(run_args
, impl_args
->name_arg
->str
)) {
2628 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2632 if (impl_args
->params_arg
->len
> 0) {
2633 if (bt_value_array_append_string_element(run_args
, "--params")) {
2634 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2638 if (bt_value_array_append_string_element(run_args
,
2639 impl_args
->params_arg
->str
)) {
2640 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2645 for (i
= 0; i
< bt_value_array_get_size(impl_args
->extra_params
);
2647 const bt_value
*elem
;
2650 elem
= bt_value_array_borrow_element_by_index(impl_args
->extra_params
,
2656 BT_ASSERT(bt_value_is_string(elem
));
2657 arg
= bt_value_string_get(elem
);
2658 ret
= bt_value_array_append_string_element(run_args
, arg
);
2660 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2674 /* Free the fields of a `struct implicit_component_args`. */
2677 void finalize_implicit_component_args(struct implicit_component_args
*args
)
2681 if (args
->comp_arg
) {
2682 g_string_free(args
->comp_arg
, TRUE
);
2685 if (args
->name_arg
) {
2686 g_string_free(args
->name_arg
, TRUE
);
2689 if (args
->params_arg
) {
2690 g_string_free(args
->params_arg
, TRUE
);
2693 bt_value_put_ref(args
->extra_params
);
2696 /* Destroy a dynamically-allocated `struct implicit_component_args`. */
2699 void destroy_implicit_component_args(struct implicit_component_args
*args
)
2701 finalize_implicit_component_args(args
);
2705 /* Initialize the fields of an already allocated `struct implicit_component_args`. */
2708 int init_implicit_component_args(struct implicit_component_args
*args
,
2709 const char *comp_arg
, bool exists
)
2713 args
->exists
= exists
;
2714 args
->comp_arg
= g_string_new(comp_arg
);
2715 args
->name_arg
= g_string_new(NULL
);
2716 args
->params_arg
= g_string_new(NULL
);
2717 args
->extra_params
= bt_value_array_create();
2719 if (!args
->comp_arg
|| !args
->name_arg
||
2720 !args
->params_arg
|| !args
->extra_params
) {
2722 finalize_implicit_component_args(args
);
2723 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2731 /* Dynamically allocate and initialize a `struct implicit_component_args`. */
2734 struct implicit_component_args
*create_implicit_component_args(
2735 const char *comp_arg
)
2737 struct implicit_component_args
*args
;
2740 args
= g_new(struct implicit_component_args
, 1);
2742 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2746 status
= init_implicit_component_args(args
, comp_arg
, true);
2757 void append_implicit_component_param(struct implicit_component_args
*args
,
2758 const char *key
, const char *value
)
2763 append_param_arg(args
->params_arg
, key
, value
);
2767 * Append the given parameter (`key=value`) to all component specifications
2768 * in `implicit_comp_args` (an array of `struct implicit_component_args *`)
2769 * which match `comp_arg`.
2771 * Return the number of matching components.
2775 int append_multiple_implicit_components_param(GPtrArray
*implicit_comp_args
,
2776 const char *comp_arg
, const char *key
, const char *value
)
2781 for (i
= 0; i
< implicit_comp_args
->len
; i
++) {
2782 struct implicit_component_args
*args
= implicit_comp_args
->pdata
[i
];
2784 if (strcmp(args
->comp_arg
->str
, comp_arg
) == 0) {
2785 append_implicit_component_param(args
, key
, value
);
2793 /* Escape value to make it suitable to use as a string parameter value. */
2795 gchar
*escape_string_value(const char *value
)
2800 ret
= g_string_new(NULL
);
2802 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2811 g_string_append_c(ret
, '\\');
2815 g_string_append_c(ret
, *in
);
2821 return g_string_free(ret
, FALSE
);
2825 int bt_value_to_cli_param_value_append(const bt_value
*value
, GString
*buf
)
2831 switch (bt_value_get_type(value
)) {
2832 case BT_VALUE_TYPE_STRING
:
2834 const char *str_value
= bt_value_string_get(value
);
2835 gchar
*escaped_str_value
;
2837 escaped_str_value
= escape_string_value(str_value
);
2838 if (!escaped_str_value
) {
2842 g_string_append_printf(buf
, "\"%s\"", escaped_str_value
);
2844 g_free(escaped_str_value
);
2847 case BT_VALUE_TYPE_ARRAY
: {
2848 g_string_append_c(buf
, '[');
2849 uint64_t sz
= bt_value_array_get_size(value
);
2850 for (uint64_t i
= 0; i
< sz
; i
++) {
2851 const bt_value
*item
;
2855 g_string_append(buf
, ", ");
2858 item
= bt_value_array_borrow_element_by_index_const(
2860 ret
= bt_value_to_cli_param_value_append(item
, buf
);
2866 g_string_append_c(buf
, ']');
2880 * Convert `value` to its equivalent representation as a command line parameter
2885 gchar
*bt_value_to_cli_param_value(bt_value
*value
)
2888 gchar
*result
= NULL
;
2890 buf
= g_string_new(NULL
);
2892 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2896 if (bt_value_to_cli_param_value_append(value
, buf
)) {
2900 result
= g_string_free(buf
, FALSE
);
2907 g_string_free(buf
, TRUE
);
2915 int append_parameter_to_args(bt_value
*args
, const char *key
, bt_value
*value
)
2918 BT_ASSERT(bt_value_get_type(args
) == BT_VALUE_TYPE_ARRAY
);
2923 gchar
*str_value
= NULL
;
2924 GString
*parameter
= NULL
;
2926 if (bt_value_array_append_string_element(args
, "--params")) {
2927 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2932 str_value
= bt_value_to_cli_param_value(value
);
2938 parameter
= g_string_new(NULL
);
2940 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2945 g_string_printf(parameter
, "%s=%s", key
, str_value
);
2947 if (bt_value_array_append_string_element(args
, parameter
->str
)) {
2948 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2955 g_string_free(parameter
, TRUE
);
2968 int append_string_parameter_to_args(bt_value
*args
, const char *key
, const char *value
)
2970 bt_value
*str_value
;
2973 str_value
= bt_value_string_create_init(value
);
2976 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2981 ret
= append_parameter_to_args(args
, key
, str_value
);
2984 BT_VALUE_PUT_REF_AND_RESET(str_value
);
2989 int append_implicit_component_extra_param(struct implicit_component_args
*args
,
2990 const char *key
, const char *value
)
2992 return append_string_parameter_to_args(args
->extra_params
, key
, value
);
2996 int convert_append_name_param(enum bt_config_component_dest dest
,
2997 GString
*cur_name
, GString
*cur_name_prefix
,
2999 bt_value
*all_names
,
3000 GList
**source_names
, GList
**filter_names
,
3005 if (cur_name_prefix
->len
> 0) {
3006 /* We're after a --component option */
3007 GString
*name
= NULL
;
3008 bool append_name_opt
= false;
3010 if (cur_name
->len
== 0) {
3012 * No explicit name was provided for the user
3015 name
= get_component_auto_name(cur_name_prefix
->str
,
3017 append_name_opt
= true;
3020 * An explicit name was provided for the user
3023 if (bt_value_map_has_entry(all_names
,
3025 BT_CLI_LOGE_APPEND_CAUSE("Duplicate component instance name:\n %s",
3030 name
= g_string_new(cur_name
->str
);
3034 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3039 * Remember this name globally, for the uniqueness of
3040 * all component names.
3042 if (bt_value_map_insert_entry(all_names
, name
->str
, bt_value_null
)) {
3043 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3048 * Append the --name option if necessary.
3050 if (append_name_opt
) {
3051 if (bt_value_array_append_string_element(run_args
, "--name")) {
3052 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3056 if (bt_value_array_append_string_element(run_args
, name
->str
)) {
3057 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3063 * Remember this name specifically for the type of the
3064 * component. This is to create connection arguments.
3067 case BT_CONFIG_COMPONENT_DEST_SOURCE
:
3068 *source_names
= g_list_append(*source_names
, name
);
3070 case BT_CONFIG_COMPONENT_DEST_FILTER
:
3071 *filter_names
= g_list_append(*filter_names
, name
);
3073 case BT_CONFIG_COMPONENT_DEST_SINK
:
3074 *sink_names
= g_list_append(*sink_names
, name
);
3080 g_string_assign(cur_name_prefix
, "");
3093 * Escapes `.`, `:`, and `\` of `input` with `\`.
3096 GString
*escape_dot_colon(const char *input
)
3098 GString
*output
= g_string_new(NULL
);
3102 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3106 for (ch
= input
; *ch
!= '\0'; ch
++) {
3107 if (*ch
== '\\' || *ch
== '.' || *ch
== ':') {
3108 g_string_append_c(output
, '\\');
3111 g_string_append_c(output
, *ch
);
3119 * Appends a --connect option to a list of arguments. `upstream_name`
3120 * and `downstream_name` are escaped with escape_dot_colon() in this
3124 int append_connect_arg(bt_value
*run_args
,
3125 const char *upstream_name
, const char *downstream_name
)
3128 GString
*e_upstream_name
= escape_dot_colon(upstream_name
);
3129 GString
*e_downstream_name
= escape_dot_colon(downstream_name
);
3130 GString
*arg
= g_string_new(NULL
);
3132 if (!e_upstream_name
|| !e_downstream_name
|| !arg
) {
3133 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3138 ret
= bt_value_array_append_string_element(run_args
, "--connect");
3140 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3145 g_string_append(arg
, e_upstream_name
->str
);
3146 g_string_append_c(arg
, ':');
3147 g_string_append(arg
, e_downstream_name
->str
);
3148 ret
= bt_value_array_append_string_element(run_args
, arg
->str
);
3150 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3157 g_string_free(arg
, TRUE
);
3160 if (e_upstream_name
) {
3161 g_string_free(e_upstream_name
, TRUE
);
3164 if (e_downstream_name
) {
3165 g_string_free(e_downstream_name
, TRUE
);
3172 * Appends the run command's --connect options for the convert command.
3175 int convert_auto_connect(bt_value
*run_args
,
3176 GList
*source_names
, GList
*filter_names
,
3180 GList
*source_at
= source_names
;
3181 GList
*filter_at
= filter_names
;
3183 GList
*sink_at
= sink_names
;
3185 BT_ASSERT(source_names
);
3186 BT_ASSERT(filter_names
);
3187 BT_ASSERT(sink_names
);
3189 /* Connect all sources to the first filter */
3190 for (source_at
= source_names
; source_at
; source_at
= g_list_next(source_at
)) {
3191 GString
*source_name
= source_at
->data
;
3192 GString
*filter_name
= filter_at
->data
;
3194 ret
= append_connect_arg(run_args
, source_name
->str
,
3201 filter_prev
= filter_at
;
3202 filter_at
= g_list_next(filter_at
);
3204 /* Connect remaining filters */
3205 for (; filter_at
; filter_prev
= filter_at
, filter_at
= g_list_next(filter_at
)) {
3206 GString
*filter_name
= filter_at
->data
;
3207 GString
*filter_prev_name
= filter_prev
->data
;
3209 ret
= append_connect_arg(run_args
, filter_prev_name
->str
,
3216 /* Connect last filter to all sinks */
3217 for (sink_at
= sink_names
; sink_at
; sink_at
= g_list_next(sink_at
)) {
3218 GString
*filter_name
= filter_prev
->data
;
3219 GString
*sink_name
= sink_at
->data
;
3221 ret
= append_connect_arg(run_args
, filter_name
->str
,
3238 int split_timerange(const char *arg
, char **begin
, char **end
)
3241 const char *ch
= arg
;
3243 GString
*g_begin
= NULL
;
3244 GString
*g_end
= NULL
;
3252 g_begin
= bt_common_string_until(ch
, "", ",", &end_pos
);
3253 if (!g_begin
|| ch
[end_pos
] != ',' || g_begin
->len
== 0) {
3259 g_end
= bt_common_string_until(ch
, "", "]", &end_pos
);
3260 if (!g_end
|| g_end
->len
== 0) {
3266 *begin
= g_begin
->str
;
3268 g_string_free(g_begin
, FALSE
);
3269 g_string_free(g_end
, FALSE
);
3279 g_string_free(g_begin
, TRUE
);
3283 g_string_free(g_end
, TRUE
);
3290 int g_list_prepend_gstring(GList
**list
, const char *string
)
3293 GString
*gs
= g_string_new(string
);
3298 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3302 *list
= g_list_prepend(*list
, gs
);
3309 * Create `struct implicit_component_args` structures for each of the source
3310 * components we identified. Add them to `component_args`.
3314 void create_implicit_component_args_from_auto_discovered_sources(
3315 const struct auto_source_discovery
*auto_disc
, GPtrArray
*component_args
)
3317 gchar
*cc_name
= NULL
;
3318 struct implicit_component_args
*comp
= NULL
;
3322 len
= auto_disc
->results
->len
;
3324 for (i
= 0; i
< len
; i
++) {
3325 struct auto_source_discovery_result
*res
=
3326 g_ptr_array_index(auto_disc
->results
, i
);
3329 cc_name
= g_strdup_printf("source.%s.%s", res
->plugin_name
, res
->source_cc_name
);
3331 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3335 comp
= create_implicit_component_args(cc_name
);
3340 status
= append_parameter_to_args(comp
->extra_params
, "inputs", res
->inputs
);
3345 g_ptr_array_add(component_args
, comp
);
3353 destroy_implicit_component_args(comp
);
3358 * Creates a Babeltrace config object from the arguments of a convert
3361 * *retcode is set to the appropriate exit code to use.
3364 struct bt_config
*bt_config_convert_from_args(int argc
, const char *argv
[],
3365 int *retcode
, bool force_omit_system_plugin_path
,
3366 bool force_omit_home_plugin_path
,
3367 const bt_value
*initial_plugin_paths
, int *default_log_level
)
3369 enum bt_config_component_dest cur_comp_dest
=
3370 BT_CONFIG_COMPONENT_DEST_UNKNOWN
;
3372 struct bt_config
*cfg
= NULL
;
3373 bool got_input_format_opt
= false;
3374 bool got_output_format_opt
= false;
3375 bool trimmer_has_begin
= false;
3376 bool trimmer_has_end
= false;
3377 bool stream_intersection_mode
= false;
3378 GString
*cur_name
= NULL
;
3379 GString
*cur_name_prefix
= NULL
;
3380 bool print_run_args
= false;
3381 bool print_run_args_0
= false;
3382 bool print_ctf_metadata
= false;
3383 bt_value
*run_args
= NULL
;
3384 bt_value
*all_names
= NULL
;
3385 GList
*source_names
= NULL
;
3386 GList
*filter_names
= NULL
;
3387 GList
*sink_names
= NULL
;
3388 bt_value
*leftovers
= NULL
;
3389 struct implicit_component_args implicit_ctf_output_args
= { 0 };
3390 struct implicit_component_args implicit_lttng_live_args
= { 0 };
3391 struct implicit_component_args implicit_dummy_args
= { 0 };
3392 struct implicit_component_args implicit_text_args
= { 0 };
3393 struct implicit_component_args implicit_debug_info_args
= { 0 };
3394 struct implicit_component_args implicit_muxer_args
= { 0 };
3395 struct implicit_component_args implicit_trimmer_args
= { 0 };
3396 bt_value
*plugin_paths
;
3397 char error_buf
[256] = { 0 };
3399 struct bt_common_lttng_live_url_parts lttng_live_url_parts
= { 0 };
3400 char *output
= NULL
;
3401 struct auto_source_discovery auto_disc
= { NULL
};
3402 GString
*auto_disc_comp_name
= NULL
;
3403 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
3406 * Array of `struct implicit_component_args *` created for the sources
3407 * we have auto-discovered.
3409 GPtrArray
*discovered_source_args
= NULL
;
3412 * If set, restrict automatic source discovery to this component class
3415 const char *auto_source_discovery_restrict_plugin_name
= NULL
;
3416 const char *auto_source_discovery_restrict_component_class_name
= NULL
;
3418 gchar
*ctf_fs_source_clock_class_offset_arg
= NULL
;
3419 gchar
*ctf_fs_source_clock_class_offset_ns_arg
= NULL
;
3421 (void) bt_value_copy(initial_plugin_paths
, &plugin_paths
);
3426 print_convert_usage(stdout
);
3431 if (init_implicit_component_args(&implicit_ctf_output_args
,
3432 "sink.ctf.fs", false)) {
3436 if (init_implicit_component_args(&implicit_lttng_live_args
,
3437 "source.ctf.lttng-live", false)) {
3441 if (init_implicit_component_args(&implicit_text_args
,
3442 "sink.text.pretty", false)) {
3446 if (init_implicit_component_args(&implicit_dummy_args
,
3447 "sink.utils.dummy", false)) {
3451 if (init_implicit_component_args(&implicit_debug_info_args
,
3452 "filter.lttng-utils.debug-info", false)) {
3456 if (init_implicit_component_args(&implicit_muxer_args
,
3457 "filter.utils.muxer", true)) {
3461 if (init_implicit_component_args(&implicit_trimmer_args
,
3462 "filter.utils.trimmer", false)) {
3466 all_names
= bt_value_map_create();
3468 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3472 run_args
= bt_value_array_create();
3474 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3478 cur_name
= g_string_new(NULL
);
3480 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3484 cur_name_prefix
= g_string_new(NULL
);
3485 if (!cur_name_prefix
) {
3486 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3490 ret
= append_env_var_plugin_paths(plugin_paths
);
3495 leftovers
= bt_value_array_create();
3497 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3501 if (auto_source_discovery_init(&auto_disc
) != 0) {
3505 discovered_source_args
=
3506 g_ptr_array_new_with_free_func((GDestroyNotify
) destroy_implicit_component_args
);
3507 if (!discovered_source_args
) {
3508 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3512 auto_disc_comp_name
= g_string_new(NULL
);
3513 if (!auto_disc_comp_name
) {
3514 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3519 * First pass: collect all arguments which need to be passed
3520 * as is to the run command. This pass can also add --name
3521 * arguments if needed to automatically name unnamed component
3522 * instances. Also it does the following transformations:
3524 * --path=PATH -> --params=path="PATH"
3525 * --url=URL -> --params=url="URL"
3527 * Also it appends the plugin paths of --plugin-path to
3530 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, convert_options
, true);
3531 if (argpar_parse_ret
.error
) {
3532 BT_CLI_LOGE_APPEND_CAUSE(
3533 "While parsing `convert` command's command-line arguments: %s",
3534 argpar_parse_ret
.error
->str
);
3538 if (help_option_is_specified(&argpar_parse_ret
)) {
3539 print_convert_usage(stdout
);
3541 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
3545 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
3546 struct bt_argpar_item
*argpar_item
=
3547 g_ptr_array_index(argpar_parse_ret
.items
, i
);
3548 struct bt_argpar_item_opt
*argpar_item_opt
;
3550 char *plugin_name
= NULL
;
3551 char *comp_cls_name
= NULL
;
3554 if (argpar_item
->type
!= BT_ARGPAR_ITEM_TYPE_OPT
) {
3558 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
3559 arg
= argpar_item_opt
->arg
;
3561 switch (argpar_item_opt
->descr
->id
) {
3564 bt_component_class_type type
;
3565 const char *type_prefix
;
3567 /* Append current component's name if needed */
3568 ret
= convert_append_name_param(cur_comp_dest
, cur_name
,
3569 cur_name_prefix
, run_args
, all_names
,
3570 &source_names
, &filter_names
, &sink_names
);
3575 /* Parse the argument */
3576 plugin_comp_cls_names(arg
, &name
, &plugin_name
,
3577 &comp_cls_name
, &type
);
3578 if (!plugin_name
|| !comp_cls_name
) {
3579 BT_CLI_LOGE_APPEND_CAUSE(
3580 "Invalid format for --component option's argument:\n %s",
3586 g_string_assign(cur_name
, name
);
3588 g_string_assign(cur_name
, "");
3592 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
3593 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
3594 type_prefix
= "source";
3596 case BT_COMPONENT_CLASS_TYPE_FILTER
:
3597 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_FILTER
;
3598 type_prefix
= "filter";
3600 case BT_COMPONENT_CLASS_TYPE_SINK
:
3601 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
3602 type_prefix
= "sink";
3608 if (bt_value_array_append_string_element(run_args
,
3610 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3614 if (bt_value_array_append_string_element(run_args
, arg
)) {
3615 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3619 g_string_assign(cur_name_prefix
, "");
3620 g_string_append_printf(cur_name_prefix
, "%s.%s.%s",
3621 type_prefix
, plugin_name
, comp_cls_name
);
3624 free(comp_cls_name
);
3627 comp_cls_name
= NULL
;
3631 if (cur_name_prefix
->len
== 0) {
3632 BT_CLI_LOGE_APPEND_CAUSE("No current component of which to set parameters:\n %s",
3637 if (bt_value_array_append_string_element(run_args
,
3639 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3643 if (bt_value_array_append_string_element(run_args
, arg
)) {
3644 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3649 if (cur_name_prefix
->len
== 0) {
3650 BT_CLI_LOGE_APPEND_CAUSE("No current component of which to set `path` parameter:\n %s",
3655 if (append_string_parameter_to_args(run_args
, "path", arg
)) {
3660 if (cur_name_prefix
->len
== 0) {
3661 BT_CLI_LOGE_APPEND_CAUSE("No current component of which to set `url` parameter:\n %s",
3667 if (append_string_parameter_to_args(run_args
, "url", arg
)) {
3672 if (cur_name_prefix
->len
== 0) {
3673 BT_CLI_LOGE_APPEND_CAUSE("No current component to name:\n %s",
3678 if (bt_value_array_append_string_element(run_args
, "--name")) {
3679 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3683 if (bt_value_array_append_string_element(run_args
, arg
)) {
3684 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3688 g_string_assign(cur_name
, arg
);
3691 if (cur_name_prefix
->len
== 0) {
3692 BT_CLI_LOGE_APPEND_CAUSE("No current component to assign a log level to:\n %s",
3697 if (bt_value_array_append_string_element(run_args
, "--log-level")) {
3698 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3702 if (bt_value_array_append_string_element(run_args
, arg
)) {
3703 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3708 case OPT_OMIT_HOME_PLUGIN_PATH
:
3709 force_omit_home_plugin_path
= true;
3711 if (bt_value_array_append_string_element(run_args
,
3712 "--omit-home-plugin-path")) {
3713 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3717 case OPT_RETRY_DURATION
:
3718 if (bt_value_array_append_string_element(run_args
,
3719 "--retry-duration")) {
3720 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3724 if (bt_value_array_append_string_element(run_args
, arg
)) {
3725 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3729 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
3730 force_omit_system_plugin_path
= true;
3732 if (bt_value_array_append_string_element(run_args
,
3733 "--omit-system-plugin-path")) {
3734 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3738 case OPT_PLUGIN_PATH
:
3739 if (bt_config_append_plugin_paths_check_setuid_setgid(
3740 plugin_paths
, arg
)) {
3744 if (bt_value_array_append_string_element(run_args
,
3746 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3750 if (bt_value_array_append_string_element(run_args
, arg
)) {
3751 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3756 case OPT_CLOCK_CYCLES
:
3757 case OPT_CLOCK_DATE
:
3758 case OPT_CLOCK_FORCE_CORRELATE
:
3760 case OPT_CLOCK_OFFSET
:
3761 case OPT_CLOCK_OFFSET_NS
:
3762 case OPT_CLOCK_SECONDS
:
3765 case OPT_DEBUG_INFO
:
3766 case OPT_DEBUG_INFO_DIR
:
3767 case OPT_DEBUG_INFO_FULL_PATH
:
3768 case OPT_DEBUG_INFO_TARGET_PREFIX
:
3771 case OPT_INPUT_FORMAT
:
3774 case OPT_OUTPUT_FORMAT
:
3777 case OPT_RUN_ARGS_0
:
3778 case OPT_STREAM_INTERSECTION
:
3781 /* Ignore in this pass */
3784 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
3785 argpar_item_opt
->descr
->id
);
3790 /* Append current component's name if needed */
3791 ret
= convert_append_name_param(cur_comp_dest
, cur_name
,
3792 cur_name_prefix
, run_args
, all_names
, &source_names
,
3793 &filter_names
, &sink_names
);
3799 * Second pass: transform the convert-specific options and
3800 * arguments into implicit component instances for the run
3803 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
3804 struct bt_argpar_item
*argpar_item
=
3805 g_ptr_array_index(argpar_parse_ret
.items
, i
);
3806 struct bt_argpar_item_opt
*argpar_item_opt
;
3809 if (argpar_item
->type
!= BT_ARGPAR_ITEM_TYPE_OPT
) {
3813 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
3814 arg
= argpar_item_opt
->arg
;
3816 switch (argpar_item_opt
->descr
->id
) {
3818 if (trimmer_has_begin
) {
3819 printf("At --begin option: --begin or --timerange option already specified\n %s\n",
3824 trimmer_has_begin
= true;
3825 ret
= append_implicit_component_extra_param(
3826 &implicit_trimmer_args
, "begin", arg
);
3827 implicit_trimmer_args
.exists
= true;
3833 if (trimmer_has_end
) {
3834 printf("At --end option: --end or --timerange option already specified\n %s\n",
3839 trimmer_has_end
= true;
3840 ret
= append_implicit_component_extra_param(
3841 &implicit_trimmer_args
, "end", arg
);
3842 implicit_trimmer_args
.exists
= true;
3852 if (trimmer_has_begin
|| trimmer_has_end
) {
3853 printf("At --timerange option: --begin, --end, or --timerange option already specified\n %s\n",
3858 ret
= split_timerange(arg
, &begin
, &end
);
3860 BT_CLI_LOGE_APPEND_CAUSE("Invalid --timerange option's argument: expecting BEGIN,END or [BEGIN,END]:\n %s",
3865 ret
= append_implicit_component_extra_param(
3866 &implicit_trimmer_args
, "begin", begin
);
3867 ret
|= append_implicit_component_extra_param(
3868 &implicit_trimmer_args
, "end", end
);
3869 implicit_trimmer_args
.exists
= true;
3877 case OPT_CLOCK_CYCLES
:
3878 append_implicit_component_param(
3879 &implicit_text_args
, "clock-cycles", "yes");
3880 implicit_text_args
.exists
= true;
3882 case OPT_CLOCK_DATE
:
3883 append_implicit_component_param(
3884 &implicit_text_args
, "clock-date", "yes");
3885 implicit_text_args
.exists
= true;
3887 case OPT_CLOCK_FORCE_CORRELATE
:
3888 append_implicit_component_param(
3889 &implicit_muxer_args
,
3890 "assume-absolute-clock-classes", "yes");
3893 append_implicit_component_param(
3894 &implicit_text_args
, "clock-gmt", "yes");
3895 append_implicit_component_param(
3896 &implicit_trimmer_args
, "gmt", "yes");
3897 implicit_text_args
.exists
= true;
3899 case OPT_CLOCK_OFFSET
:
3900 if (ctf_fs_source_clock_class_offset_arg
) {
3901 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --clock-offset option\n");
3905 ctf_fs_source_clock_class_offset_arg
= g_strdup(arg
);
3906 if (!ctf_fs_source_clock_class_offset_arg
) {
3907 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3911 case OPT_CLOCK_OFFSET_NS
:
3912 if (ctf_fs_source_clock_class_offset_ns_arg
) {
3913 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --clock-offset-ns option\n");
3917 ctf_fs_source_clock_class_offset_ns_arg
= g_strdup(arg
);
3918 if (!ctf_fs_source_clock_class_offset_ns_arg
) {
3919 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3923 case OPT_CLOCK_SECONDS
:
3924 append_implicit_component_param(
3925 &implicit_text_args
, "clock-seconds", "yes");
3926 implicit_text_args
.exists
= true;
3929 implicit_text_args
.exists
= true;
3930 ret
= append_implicit_component_extra_param(
3931 &implicit_text_args
, "color", arg
);
3936 case OPT_DEBUG_INFO
:
3937 implicit_debug_info_args
.exists
= true;
3939 case OPT_DEBUG_INFO_DIR
:
3940 implicit_debug_info_args
.exists
= true;
3941 ret
= append_implicit_component_extra_param(
3942 &implicit_debug_info_args
, "debug-info-dir", arg
);
3947 case OPT_DEBUG_INFO_FULL_PATH
:
3948 implicit_debug_info_args
.exists
= true;
3949 append_implicit_component_param(
3950 &implicit_debug_info_args
, "full-path", "yes");
3952 case OPT_DEBUG_INFO_TARGET_PREFIX
:
3953 implicit_debug_info_args
.exists
= true;
3954 ret
= append_implicit_component_extra_param(
3955 &implicit_debug_info_args
,
3956 "target-prefix", arg
);
3963 bt_value
*fields
= fields_from_arg(arg
);
3969 implicit_text_args
.exists
= true;
3970 ret
= insert_flat_params_from_array(
3971 implicit_text_args
.params_arg
,
3973 bt_value_put_ref(fields
);
3981 bt_value
*names
= names_from_arg(arg
);
3987 implicit_text_args
.exists
= true;
3988 ret
= insert_flat_params_from_array(
3989 implicit_text_args
.params_arg
,
3991 bt_value_put_ref(names
);
3998 append_implicit_component_param(
3999 &implicit_text_args
, "no-delta", "yes");
4000 implicit_text_args
.exists
= true;
4002 case OPT_INPUT_FORMAT
:
4003 if (got_input_format_opt
) {
4004 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --input-format option.");
4008 got_input_format_opt
= true;
4010 if (strcmp(arg
, "ctf") == 0) {
4011 auto_source_discovery_restrict_plugin_name
= "ctf";
4012 auto_source_discovery_restrict_component_class_name
= "fs";
4013 } else if (strcmp(arg
, "lttng-live") == 0) {
4014 auto_source_discovery_restrict_plugin_name
= "ctf";
4015 auto_source_discovery_restrict_component_class_name
= "lttng-live";
4016 implicit_lttng_live_args
.exists
= true;
4018 BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy input format:\n %s",
4023 case OPT_OUTPUT_FORMAT
:
4024 if (got_output_format_opt
) {
4025 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output-format option.");
4029 got_output_format_opt
= true;
4031 if (strcmp(arg
, "text") == 0) {
4032 implicit_text_args
.exists
= true;
4033 } else if (strcmp(arg
, "ctf") == 0) {
4034 implicit_ctf_output_args
.exists
= true;
4035 } else if (strcmp(arg
, "dummy") == 0) {
4036 implicit_dummy_args
.exists
= true;
4037 } else if (strcmp(arg
, "ctf-metadata") == 0) {
4038 print_ctf_metadata
= true;
4040 BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy output format:\n %s",
4047 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output option");
4051 output
= strdup(arg
);
4053 BT_CLI_LOGE_APPEND_CAUSE_OOM();
4058 if (print_run_args_0
) {
4059 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0.");
4063 print_run_args
= true;
4065 case OPT_RUN_ARGS_0
:
4066 if (print_run_args
) {
4067 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0.");
4071 print_run_args_0
= true;
4073 case OPT_STREAM_INTERSECTION
:
4075 * Applies to all traces implementing the
4076 * babeltrace.trace-info query.
4078 stream_intersection_mode
= true;
4081 if (*default_log_level
!= BT_LOG_TRACE
&&
4082 *default_log_level
!= BT_LOG_DEBUG
) {
4083 *default_log_level
= BT_LOG_INFO
;
4087 *default_log_level
= BT_LOG_TRACE
;
4095 * Legacy behaviour: --verbose used to make the `text` output
4096 * format print more information. --verbose is now equivalent to
4097 * the INFO log level, which is why we compare to `BT_LOG_INFO`
4100 if (*default_log_level
== BT_LOG_INFO
) {
4101 append_implicit_component_param(&implicit_text_args
,
4106 * Append home and system plugin paths now that we possibly got
4109 if (append_home_and_system_plugin_paths(plugin_paths
,
4110 force_omit_system_plugin_path
,
4111 force_omit_home_plugin_path
)) {
4115 /* Consume and keep leftover arguments */
4116 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
4117 struct bt_argpar_item
*argpar_item
=
4118 g_ptr_array_index(argpar_parse_ret
.items
, i
);
4119 struct bt_argpar_item_non_opt
*argpar_item_non_opt
;
4121 if (argpar_item
->type
!= BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
4125 argpar_item_non_opt
= (struct bt_argpar_item_non_opt
*) argpar_item
;
4127 if (bt_value_array_append_string_element(leftovers
, argpar_item_non_opt
->arg
) !=
4128 BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
4129 BT_CLI_LOGE_APPEND_CAUSE_OOM();
4134 /* Print CTF metadata or print LTTng live sessions */
4135 if (print_ctf_metadata
) {
4136 const bt_value
*bt_val_leftover
;
4138 if (bt_value_array_is_empty(leftovers
)) {
4139 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf-metadata specified without a path.");
4143 if (bt_value_array_get_size(leftovers
) > 1) {
4144 BT_CLI_LOGE_APPEND_CAUSE("Too many paths specified for --output-format=ctf-metadata.");
4148 cfg
= bt_config_print_ctf_metadata_create(plugin_paths
);
4153 bt_val_leftover
= bt_value_array_borrow_element_by_index_const(leftovers
, 0);
4154 g_string_assign(cfg
->cmd_data
.print_ctf_metadata
.path
,
4155 bt_value_string_get(bt_val_leftover
));
4159 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
4167 * If -o ctf was specified, make sure an output path (--output)
4168 * was also specified. --output does not imply -o ctf because
4169 * it's also used for the default, implicit -o text if -o ctf
4172 if (implicit_ctf_output_args
.exists
) {
4174 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf specified without --output (trace output path).");
4179 * At this point we know that -o ctf AND --output were
4180 * specified. Make sure that no options were specified
4181 * which would imply -o text because --output would be
4182 * ambiguous in this case. For example, this is wrong:
4184 * babeltrace2 --names=all -o ctf --output=/tmp/path my-trace
4186 * because --names=all implies -o text, and --output
4187 * could apply to both the sink.text.pretty and
4188 * sink.ctf.fs implicit components.
4190 if (implicit_text_args
.exists
) {
4191 BT_CLI_LOGE_APPEND_CAUSE("Ambiguous --output option: --output-format=ctf specified but another option implies --output-format=text.");
4197 * If -o dummy and -o ctf were not specified, and if there are
4198 * no explicit sink components, then use an implicit
4199 * `sink.text.pretty` component.
4201 if (!implicit_dummy_args
.exists
&& !implicit_ctf_output_args
.exists
&&
4203 implicit_text_args
.exists
= true;
4207 * Set implicit `sink.text.pretty` or `sink.ctf.fs` component's
4208 * `path` parameter if --output was specified.
4211 if (implicit_text_args
.exists
) {
4212 append_implicit_component_extra_param(&implicit_text_args
,
4214 } else if (implicit_ctf_output_args
.exists
) {
4215 append_implicit_component_extra_param(&implicit_ctf_output_args
,
4220 /* Decide where the leftover argument(s) go */
4221 if (bt_value_array_get_size(leftovers
) > 0) {
4222 if (implicit_lttng_live_args
.exists
) {
4223 const bt_value
*bt_val_leftover
;
4225 if (bt_value_array_get_size(leftovers
) > 1) {
4226 BT_CLI_LOGE_APPEND_CAUSE("Too many URLs specified for --input-format=lttng-live.");
4230 bt_val_leftover
= bt_value_array_borrow_element_by_index_const(leftovers
, 0);
4231 lttng_live_url_parts
=
4232 bt_common_parse_lttng_live_url(bt_value_string_get(bt_val_leftover
),
4233 error_buf
, sizeof(error_buf
));
4234 if (!lttng_live_url_parts
.proto
) {
4235 BT_CLI_LOGE_APPEND_CAUSE("Invalid LTTng live URL format: %s.",
4240 if (!lttng_live_url_parts
.session_name
) {
4241 /* Print LTTng live sessions */
4242 cfg
= bt_config_print_lttng_live_sessions_create(
4248 g_string_assign(cfg
->cmd_data
.print_lttng_live_sessions
.url
,
4249 bt_value_string_get(bt_val_leftover
));
4253 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
4260 ret
= append_implicit_component_extra_param(
4261 &implicit_lttng_live_args
, "url",
4262 bt_value_string_get(bt_val_leftover
));
4267 ret
= append_implicit_component_extra_param(
4268 &implicit_lttng_live_args
,
4269 "session-not-found-action", "end");
4276 status
= auto_discover_source_components(plugin_paths
, leftovers
,
4277 auto_source_discovery_restrict_plugin_name
,
4278 auto_source_discovery_restrict_component_class_name
,
4279 *default_log_level
>= 0 ? *default_log_level
: cli_default_log_level
,
4286 create_implicit_component_args_from_auto_discovered_sources(
4287 &auto_disc
, discovered_source_args
);
4291 /* If --clock-offset was given, apply it to any src.ctf.fs component. */
4292 if (ctf_fs_source_clock_class_offset_arg
) {
4295 n
= append_multiple_implicit_components_param(
4296 discovered_source_args
, "source.ctf.fs", "clock-class-offset-s",
4297 ctf_fs_source_clock_class_offset_arg
);
4300 BT_CLI_LOGE_APPEND_CAUSE("--clock-offset specified, but no source.ctf.fs component instantiated.");
4305 /* If --clock-offset-ns was given, apply it to any src.ctf.fs component. */
4306 if (ctf_fs_source_clock_class_offset_ns_arg
) {
4309 n
= append_multiple_implicit_components_param(
4310 discovered_source_args
, "source.ctf.fs", "clock-class-offset-ns",
4311 ctf_fs_source_clock_class_offset_ns_arg
);
4314 BT_CLI_LOGE_APPEND_CAUSE("--clock-offset-ns specified, but no source.ctf.fs component instantiated.");
4320 * If the implicit `source.ctf.lttng-live` component exists, make sure
4321 * there's at least one leftover (which is the URL).
4323 if (implicit_lttng_live_args
.exists
&& bt_value_array_is_empty(leftovers
)) {
4324 BT_CLI_LOGE_APPEND_CAUSE("Missing URL for implicit `%s` component.",
4325 implicit_lttng_live_args
.comp_arg
->str
);
4329 /* Assign names to implicit components */
4330 for (i
= 0; i
< discovered_source_args
->len
; i
++) {
4331 struct implicit_component_args
*args
;
4334 args
= discovered_source_args
->pdata
[i
];
4336 g_string_printf(auto_disc_comp_name
, "auto-disc-%s", args
->comp_arg
->str
);
4338 /* Give it a name like `auto-disc-src-ctf-fs`. */
4339 for (j
= 0; j
< auto_disc_comp_name
->len
; j
++) {
4340 if (auto_disc_comp_name
->str
[j
] == '.') {
4341 auto_disc_comp_name
->str
[j
] = '-';
4345 ret
= assign_name_to_implicit_component(args
,
4346 auto_disc_comp_name
->str
, all_names
, &source_names
, true);
4352 ret
= assign_name_to_implicit_component(&implicit_lttng_live_args
,
4353 "lttng-live", all_names
, &source_names
, true);
4358 ret
= assign_name_to_implicit_component(&implicit_text_args
,
4359 "pretty", all_names
, &sink_names
, true);
4364 ret
= assign_name_to_implicit_component(&implicit_ctf_output_args
,
4365 "sink-ctf-fs", all_names
, &sink_names
, true);
4370 ret
= assign_name_to_implicit_component(&implicit_dummy_args
,
4371 "dummy", all_names
, &sink_names
, true);
4376 ret
= assign_name_to_implicit_component(&implicit_muxer_args
,
4377 "muxer", all_names
, NULL
, false);
4382 ret
= assign_name_to_implicit_component(&implicit_trimmer_args
,
4383 "trimmer", all_names
, NULL
, false);
4388 ret
= assign_name_to_implicit_component(&implicit_debug_info_args
,
4389 "debug-info", all_names
, NULL
, false);
4394 /* Make sure there's at least one source and one sink */
4395 if (!source_names
) {
4396 BT_CLI_LOGE_APPEND_CAUSE("No source component.");
4401 BT_CLI_LOGE_APPEND_CAUSE("No sink component.");
4406 * Prepend the muxer, the trimmer, and the debug info to the
4407 * filter chain so that we have:
4409 * sources -> muxer -> [trimmer] -> [debug info] ->
4410 * [user filters] -> sinks
4412 if (implicit_debug_info_args
.exists
) {
4413 if (g_list_prepend_gstring(&filter_names
,
4414 implicit_debug_info_args
.name_arg
->str
)) {
4419 if (implicit_trimmer_args
.exists
) {
4420 if (g_list_prepend_gstring(&filter_names
,
4421 implicit_trimmer_args
.name_arg
->str
)) {
4426 if (g_list_prepend_gstring(&filter_names
,
4427 implicit_muxer_args
.name_arg
->str
)) {
4432 * Append the equivalent run arguments for the implicit
4435 for (i
= 0; i
< discovered_source_args
->len
; i
++) {
4436 struct implicit_component_args
*args
=
4437 discovered_source_args
->pdata
[i
];
4439 ret
= append_run_args_for_implicit_component(args
, run_args
);
4445 ret
= append_run_args_for_implicit_component(&implicit_lttng_live_args
,
4451 ret
= append_run_args_for_implicit_component(&implicit_text_args
,
4457 ret
= append_run_args_for_implicit_component(&implicit_ctf_output_args
,
4463 ret
= append_run_args_for_implicit_component(&implicit_dummy_args
,
4469 ret
= append_run_args_for_implicit_component(&implicit_muxer_args
,
4475 ret
= append_run_args_for_implicit_component(&implicit_trimmer_args
,
4481 ret
= append_run_args_for_implicit_component(&implicit_debug_info_args
,
4487 /* Auto-connect components */
4488 ret
= convert_auto_connect(run_args
, source_names
, filter_names
,
4491 BT_CLI_LOGE_APPEND_CAUSE("Cannot auto-connect components.");
4496 * We have all the run command arguments now. Depending on
4497 * --run-args, we pass this to the run command or print them
4500 if (print_run_args
|| print_run_args_0
) {
4501 if (stream_intersection_mode
) {
4502 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --stream-intersection with --run-args or --run-args-0.");
4506 for (i
= 0; i
< bt_value_array_get_size(run_args
); i
++) {
4507 const bt_value
*arg_value
=
4508 bt_value_array_borrow_element_by_index(run_args
,
4511 GString
*quoted
= NULL
;
4512 const char *arg_to_print
;
4514 BT_ASSERT(arg_value
);
4515 arg
= bt_value_string_get(arg_value
);
4517 if (print_run_args
) {
4518 quoted
= bt_common_shell_quote(arg
, true);
4523 arg_to_print
= quoted
->str
;
4528 printf("%s", arg_to_print
);
4531 g_string_free(quoted
, TRUE
);
4534 if (i
< bt_value_array_get_size(run_args
) - 1) {
4535 if (print_run_args
) {
4544 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4549 * If the log level is still unset at this point, set it to
4550 * the program's default.
4552 if (*default_log_level
< 0) {
4553 *default_log_level
= cli_default_log_level
;
4556 cfg
= bt_config_run_from_args_array(run_args
, retcode
,
4557 force_omit_system_plugin_path
,
4558 force_omit_home_plugin_path
,
4559 initial_plugin_paths
, *default_log_level
);
4564 cfg
->cmd_data
.run
.stream_intersection_mode
= stream_intersection_mode
;
4569 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4573 * If the log level is still unset at this point, set it to
4574 * the program's default.
4576 if (*default_log_level
< 0) {
4577 *default_log_level
= cli_default_log_level
;
4580 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
4585 g_string_free(cur_name
, TRUE
);
4588 if (cur_name_prefix
) {
4589 g_string_free(cur_name_prefix
, TRUE
);
4592 bt_value_put_ref(run_args
);
4593 bt_value_put_ref(all_names
);
4594 destroy_glist_of_gstring(source_names
);
4595 destroy_glist_of_gstring(filter_names
);
4596 destroy_glist_of_gstring(sink_names
);
4597 bt_value_put_ref(leftovers
);
4598 finalize_implicit_component_args(&implicit_ctf_output_args
);
4599 finalize_implicit_component_args(&implicit_lttng_live_args
);
4600 finalize_implicit_component_args(&implicit_dummy_args
);
4601 finalize_implicit_component_args(&implicit_text_args
);
4602 finalize_implicit_component_args(&implicit_debug_info_args
);
4603 finalize_implicit_component_args(&implicit_muxer_args
);
4604 finalize_implicit_component_args(&implicit_trimmer_args
);
4605 bt_value_put_ref(plugin_paths
);
4606 bt_common_destroy_lttng_live_url_parts(<tng_live_url_parts
);
4607 auto_source_discovery_fini(&auto_disc
);
4609 if (discovered_source_args
) {
4610 g_ptr_array_free(discovered_source_args
, TRUE
);
4613 g_free(ctf_fs_source_clock_class_offset_arg
);
4614 g_free(ctf_fs_source_clock_class_offset_ns_arg
);
4616 if (auto_disc_comp_name
) {
4617 g_string_free(auto_disc_comp_name
, TRUE
);
4624 * Prints the Babeltrace 2.x general usage.
4627 void print_gen_usage(FILE *fp
)
4629 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] [COMMAND] [COMMAND ARGUMENTS]\n");
4631 fprintf(fp
, "General options:\n");
4633 fprintf(fp
, " -d, --debug Enable debug mode (same as --log-level=V)\n");
4634 fprintf(fp
, " -h, --help Show this help and quit\n");
4635 fprintf(fp
, " -l, --log-level=LVL Set the default log level to LVL (`N`, `V`, `D`,\n");
4636 fprintf(fp
, " `I`, `W` (default), `E`, or `F`)\n");
4637 fprintf(fp
, " -v, --verbose Enable verbose mode (same as --log-level=I)\n");
4638 fprintf(fp
, " -V, --version Show version and quit\n");
4640 fprintf(fp
, "Available commands:\n");
4642 fprintf(fp
, " convert Convert and trim traces (default)\n");
4643 fprintf(fp
, " help Get help for a plugin or a component class\n");
4644 fprintf(fp
, " list-plugins List available plugins and their content\n");
4645 fprintf(fp
, " query Query objects from a component class\n");
4646 fprintf(fp
, " run Build a processing graph and run it\n");
4648 fprintf(fp
, "Use `babeltrace2 COMMAND --help` to show the help of COMMAND.\n");
4651 struct bt_config
*bt_config_cli_args_create(int argc
, const char *argv
[],
4652 int *retcode
, bool force_omit_system_plugin_path
,
4653 bool force_omit_home_plugin_path
,
4654 const bt_value
*initial_plugin_paths
)
4656 struct bt_config
*config
= NULL
;
4659 const char **top_level_argv
;
4660 int command_argc
= -1;
4661 const char **command_argv
= NULL
;
4662 const char *command_name
= NULL
;
4663 int default_log_level
= -1;
4664 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
4666 /* Top-level option descriptions. */
4667 static const struct bt_argpar_opt_descr descrs
[] = {
4668 { OPT_DEBUG
, 'd', "debug", false },
4669 { OPT_HELP
, 'h', "help", false },
4670 { OPT_LOG_LEVEL
, 'l', "log-level", true },
4671 { OPT_VERBOSE
, 'v', "verbose", false },
4672 { OPT_VERSION
, 'V', "version", false},
4673 BT_ARGPAR_OPT_DESCR_SENTINEL
4677 COMMAND_TYPE_NONE
= -1,
4678 COMMAND_TYPE_RUN
= 0,
4679 COMMAND_TYPE_CONVERT
,
4680 COMMAND_TYPE_LIST_PLUGINS
,
4683 } command_type
= COMMAND_TYPE_NONE
;
4687 if (!initial_plugin_paths
) {
4688 initial_plugin_paths
= bt_value_array_create();
4689 if (!initial_plugin_paths
) {
4694 bt_value_get_ref(initial_plugin_paths
);
4700 print_gen_usage(stdout
);
4704 /* Skip first argument, the name of the program. */
4705 top_level_argc
= argc
- 1;
4706 top_level_argv
= argv
+ 1;
4707 argpar_parse_ret
= bt_argpar_parse(top_level_argc
, top_level_argv
,
4710 if (argpar_parse_ret
.error
) {
4711 BT_CLI_LOGE_APPEND_CAUSE(
4712 "While parsing command-line arguments: %s",
4713 argpar_parse_ret
.error
->str
);
4717 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
4718 struct bt_argpar_item
*item
;
4720 item
= g_ptr_array_index(argpar_parse_ret
.items
, i
);
4722 if (item
->type
== BT_ARGPAR_ITEM_TYPE_OPT
) {
4723 struct bt_argpar_item_opt
*item_opt
=
4724 (struct bt_argpar_item_opt
*) item
;
4726 switch (item_opt
->descr
->id
) {
4728 default_log_level
= BT_LOG_TRACE
;
4732 * Legacy: do not override a previous
4733 * --debug because --verbose and --debug
4734 * can be specified together (in this
4735 * case we want the lowest log level to
4738 default_log_level
= BT_LOG_INFO
;
4742 bt_log_get_level_from_string(item_opt
->arg
);
4743 if (default_log_level
< 0) {
4744 BT_CLI_LOGE_APPEND_CAUSE(
4745 "Invalid argument for --log-level option:\n %s",
4754 print_gen_usage(stdout
);
4757 } else if (item
->type
== BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
4758 struct bt_argpar_item_non_opt
*item_non_opt
=
4759 (struct bt_argpar_item_non_opt
*) item
;
4761 * First unknown argument: is it a known command
4765 top_level_argc
- item_non_opt
->orig_index
- 1;
4767 &top_level_argv
[item_non_opt
->orig_index
+ 1];
4769 if (strcmp(item_non_opt
->arg
, "convert") == 0) {
4770 command_type
= COMMAND_TYPE_CONVERT
;
4771 } else if (strcmp(item_non_opt
->arg
, "list-plugins") == 0) {
4772 command_type
= COMMAND_TYPE_LIST_PLUGINS
;
4773 } else if (strcmp(item_non_opt
->arg
, "help") == 0) {
4774 command_type
= COMMAND_TYPE_HELP
;
4775 } else if (strcmp(item_non_opt
->arg
, "query") == 0) {
4776 command_type
= COMMAND_TYPE_QUERY
;
4777 } else if (strcmp(item_non_opt
->arg
, "run") == 0) {
4778 command_type
= COMMAND_TYPE_RUN
;
4781 * Non-option argument, but not a known
4782 * command name: assume the default
4783 * `convert` command.
4785 command_type
= COMMAND_TYPE_CONVERT
;
4786 command_name
= "convert";
4794 if (command_type
== COMMAND_TYPE_NONE
) {
4795 if (argpar_parse_ret
.ingested_orig_args
== top_level_argc
) {
4797 * We only got non-help, non-version general options
4798 * like --verbose and --debug, without any other
4799 * arguments, so we can't do anything useful: print the
4802 print_gen_usage(stdout
);
4807 * We stopped on an unknown option argument (and therefore
4808 * didn't see a command name). Assume `convert` command.
4810 command_type
= COMMAND_TYPE_CONVERT
;
4811 command_name
= "convert";
4813 top_level_argc
- argpar_parse_ret
.ingested_orig_args
;
4815 &top_level_argv
[argpar_parse_ret
.ingested_orig_args
];
4818 BT_ASSERT(command_argv
);
4819 BT_ASSERT(command_argc
>= 0);
4822 * The convert command can set its own default log level for
4823 * backward compatibility reasons. It only does so if there's no
4824 * log level yet, so do not force one for this command.
4826 if (command_type
!= COMMAND_TYPE_CONVERT
&& default_log_level
< 0) {
4827 /* Default log level */
4828 default_log_level
= cli_default_log_level
;
4831 switch (command_type
) {
4832 case COMMAND_TYPE_RUN
:
4833 config
= bt_config_run_from_args(command_argc
, command_argv
,
4834 retcode
, force_omit_system_plugin_path
,
4835 force_omit_home_plugin_path
, initial_plugin_paths
,
4838 case COMMAND_TYPE_CONVERT
:
4839 config
= bt_config_convert_from_args(command_argc
, command_argv
,
4840 retcode
, force_omit_system_plugin_path
,
4841 force_omit_home_plugin_path
,
4842 initial_plugin_paths
, &default_log_level
);
4844 case COMMAND_TYPE_LIST_PLUGINS
:
4845 config
= bt_config_list_plugins_from_args(command_argc
,
4846 command_argv
, retcode
, force_omit_system_plugin_path
,
4847 force_omit_home_plugin_path
, initial_plugin_paths
);
4849 case COMMAND_TYPE_HELP
:
4850 config
= bt_config_help_from_args(command_argc
,
4851 command_argv
, retcode
, force_omit_system_plugin_path
,
4852 force_omit_home_plugin_path
, initial_plugin_paths
,
4855 case COMMAND_TYPE_QUERY
:
4856 config
= bt_config_query_from_args(command_argc
,
4857 command_argv
, retcode
, force_omit_system_plugin_path
,
4858 force_omit_home_plugin_path
, initial_plugin_paths
,
4866 BT_ASSERT(default_log_level
>= BT_LOG_TRACE
);
4867 config
->log_level
= default_log_level
;
4868 config
->command_name
= command_name
;
4877 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
4878 bt_value_put_ref(initial_plugin_paths
);