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"
35 #include <babeltrace/babeltrace.h>
36 #include <babeltrace/common-internal.h>
37 #include <babeltrace/values.h>
40 #include <sys/types.h>
41 #include "babeltrace-cfg.h"
42 #include "babeltrace-cfg-cli-args.h"
43 #include "babeltrace-cfg-cli-args-connect.h"
46 * Error printf() macro which prepends "Error: " the first time it's
47 * called. This gives a nicer feel than having a bunch of error prefixes
48 * (since the following lines usually describe the error and possible
49 * solutions), or the error prefix just at the end.
51 #define printf_err(fmt, args...) \
53 if (is_first_error) { \
54 fprintf(stderr, "Command line error: "); \
55 is_first_error = false; \
57 fprintf(stderr, fmt, ##args); \
60 static bool is_first_error
= true;
62 /* INI-style parsing FSM states */
63 enum ini_parsing_fsm_state
{
64 /* Expect a map key (identifier) */
67 /* Expect an equal character ('=') */
73 /* Expect a negative number value */
74 INI_EXPECT_VALUE_NUMBER_NEG
,
76 /* Expect a comma character (',') */
80 /* INI-style parsing state variables */
81 struct ini_parsing_state
{
82 /* Lexical scanner (owned by this) */
85 /* Output map value object being filled (owned by this) */
86 struct bt_value
*params
;
88 /* Next expected FSM state */
89 enum ini_parsing_fsm_state expecting
;
91 /* Last decoded map key (owned by this) */
94 /* Complete INI-style string to parse (not owned by this) */
97 /* Error buffer (not owned by this) */
101 /* Offset option with "is set" boolean */
107 /* Legacy "ctf"/"lttng-live" format options */
108 struct ctf_legacy_opts
{
109 struct offset_opt offset_s
;
110 struct offset_opt offset_ns
;
111 bool stream_intersection
;
114 /* Legacy "text" format options */
115 struct text_legacy_opts
{
117 * output, dbg_info_dir, dbg_info_target_prefix, names,
118 * and fields are owned by this.
121 GString
*dbg_info_dir
;
122 GString
*dbg_info_target_prefix
;
123 struct bt_value
*names
;
124 struct bt_value
*fields
;
132 bool dbg_info_full_path
;
136 /* Legacy input format format */
137 enum legacy_input_format
{
138 LEGACY_INPUT_FORMAT_NONE
= 0,
139 LEGACY_INPUT_FORMAT_CTF
,
140 LEGACY_INPUT_FORMAT_LTTNG_LIVE
,
143 /* Legacy output format format */
144 enum legacy_output_format
{
145 LEGACY_OUTPUT_FORMAT_NONE
= 0,
146 LEGACY_OUTPUT_FORMAT_TEXT
,
147 LEGACY_OUTPUT_FORMAT_DUMMY
,
151 * Prints the "out of memory" error.
154 void print_err_oom(void)
156 printf_err("Out of memory\n");
160 * Appends an "expecting token" error to the INI-style parsing state's
164 void ini_append_error_expecting(struct ini_parsing_state
*state
,
165 GScanner
*scanner
, const char *expecting
)
170 g_string_append_printf(state
->ini_error
, "Expecting %s:\n", expecting
);
172 /* Only print error if there's one line */
173 if (strchr(state
->arg
, '\n') != NULL
|| strlen(state
->arg
) == 0) {
177 g_string_append_printf(state
->ini_error
, "\n %s\n", state
->arg
);
178 pos
= g_scanner_cur_position(scanner
) + 4;
180 if (!g_scanner_eof(scanner
)) {
184 for (i
= 0; i
< pos
; ++i
) {
185 g_string_append_printf(state
->ini_error
, " ");
188 g_string_append_printf(state
->ini_error
, "^\n\n");
192 int ini_handle_state(struct ini_parsing_state
*state
)
195 GTokenType token_type
;
196 struct bt_value
*value
= NULL
;
198 token_type
= g_scanner_get_next_token(state
->scanner
);
199 if (token_type
== G_TOKEN_EOF
) {
200 if (state
->expecting
!= INI_EXPECT_COMMA
) {
201 switch (state
->expecting
) {
202 case INI_EXPECT_EQUAL
:
203 ini_append_error_expecting(state
,
204 state
->scanner
, "'='");
206 case INI_EXPECT_VALUE
:
207 case INI_EXPECT_VALUE_NUMBER_NEG
:
208 ini_append_error_expecting(state
,
209 state
->scanner
, "value");
211 case INI_EXPECT_MAP_KEY
:
212 ini_append_error_expecting(state
,
213 state
->scanner
, "unquoted map key");
226 switch (state
->expecting
) {
227 case INI_EXPECT_MAP_KEY
:
228 if (token_type
!= G_TOKEN_IDENTIFIER
) {
229 ini_append_error_expecting(state
, state
->scanner
,
234 free(state
->last_map_key
);
235 state
->last_map_key
=
236 strdup(state
->scanner
->value
.v_identifier
);
237 if (!state
->last_map_key
) {
238 g_string_append(state
->ini_error
,
243 if (bt_value_map_has_key(state
->params
, state
->last_map_key
)) {
244 g_string_append_printf(state
->ini_error
,
245 "Duplicate parameter key: `%s`\n",
246 state
->last_map_key
);
250 state
->expecting
= INI_EXPECT_EQUAL
;
252 case INI_EXPECT_EQUAL
:
253 if (token_type
!= G_TOKEN_CHAR
) {
254 ini_append_error_expecting(state
,
255 state
->scanner
, "'='");
259 if (state
->scanner
->value
.v_char
!= '=') {
260 ini_append_error_expecting(state
,
261 state
->scanner
, "'='");
265 state
->expecting
= INI_EXPECT_VALUE
;
267 case INI_EXPECT_VALUE
:
269 switch (token_type
) {
271 if (state
->scanner
->value
.v_char
== '-') {
272 /* Negative number */
274 INI_EXPECT_VALUE_NUMBER_NEG
;
277 ini_append_error_expecting(state
,
278 state
->scanner
, "value");
284 /* Positive integer */
285 uint64_t int_val
= state
->scanner
->value
.v_int64
;
287 if (int_val
> (1ULL << 63) - 1) {
288 g_string_append_printf(state
->ini_error
,
289 "Integer value %" PRIu64
" is outside the range of a 64-bit signed integer\n",
294 value
= bt_value_integer_create_init(
299 /* Positive floating point number */
300 value
= bt_value_float_create_init(
301 state
->scanner
->value
.v_float
);
305 value
= bt_value_string_create_init(
306 state
->scanner
->value
.v_string
);
308 case G_TOKEN_IDENTIFIER
:
311 * Using symbols would be appropriate here,
312 * but said symbols are allowed as map key,
313 * so it's easier to consider everything an
316 * If one of the known symbols is not
317 * recognized here, then fall back to creating
320 const char *id
= state
->scanner
->value
.v_identifier
;
322 if (!strcmp(id
, "null") || !strcmp(id
, "NULL") ||
323 !strcmp(id
, "nul")) {
324 value
= bt_value_null
;
325 } else if (!strcmp(id
, "true") || !strcmp(id
, "TRUE") ||
326 !strcmp(id
, "yes") ||
327 !strcmp(id
, "YES")) {
328 value
= bt_value_bool_create_init(true);
329 } else if (!strcmp(id
, "false") ||
330 !strcmp(id
, "FALSE") ||
333 value
= bt_value_bool_create_init(false);
335 value
= bt_value_string_create_init(id
);
340 /* Unset value variable will trigger the error */
345 ini_append_error_expecting(state
,
346 state
->scanner
, "value");
350 state
->expecting
= INI_EXPECT_COMMA
;
353 case INI_EXPECT_VALUE_NUMBER_NEG
:
355 switch (token_type
) {
358 /* Negative integer */
359 uint64_t int_val
= state
->scanner
->value
.v_int64
;
361 if (int_val
> (1ULL << 63) - 1) {
362 g_string_append_printf(state
->ini_error
,
363 "Integer value -%" PRIu64
" is outside the range of a 64-bit signed integer\n",
368 value
= bt_value_integer_create_init(
369 -((int64_t) int_val
));
373 /* Negative floating point number */
374 value
= bt_value_float_create_init(
375 -state
->scanner
->value
.v_float
);
378 /* Unset value variable will trigger the error */
383 ini_append_error_expecting(state
,
384 state
->scanner
, "value");
388 state
->expecting
= INI_EXPECT_COMMA
;
391 case INI_EXPECT_COMMA
:
392 if (token_type
!= G_TOKEN_CHAR
) {
393 ini_append_error_expecting(state
,
394 state
->scanner
, "','");
398 if (state
->scanner
->value
.v_char
!= ',') {
399 ini_append_error_expecting(state
,
400 state
->scanner
, "','");
404 state
->expecting
= INI_EXPECT_MAP_KEY
;
416 if (bt_value_map_insert(state
->params
,
417 state
->last_map_key
, value
)) {
418 /* Only override return value on error */
429 * Converts an INI-style argument to an equivalent map value object.
431 * Return value is owned by the caller.
434 struct bt_value
*bt_value_from_ini(const char *arg
, GString
*ini_error
)
436 /* Lexical scanner configuration */
437 GScannerConfig scanner_config
= {
438 /* Skip whitespaces */
439 .cset_skip_characters
= " \t\n",
441 /* Identifier syntax is: [a-zA-Z_][a-zA-Z0-9_.:-]* */
442 .cset_identifier_first
=
446 .cset_identifier_nth
=
451 /* "hello" and "Hello" two different keys */
452 .case_sensitive
= TRUE
,
455 .cpair_comment_single
= NULL
,
456 .skip_comment_multi
= TRUE
,
457 .skip_comment_single
= TRUE
,
458 .scan_comment_multi
= FALSE
,
461 * Do scan identifiers, including 1-char identifiers,
462 * but NULL is a normal identifier.
464 .scan_identifier
= TRUE
,
465 .scan_identifier_1char
= TRUE
,
466 .scan_identifier_NULL
= FALSE
,
469 * No specific symbols: null and boolean "symbols" are
470 * scanned as plain identifiers.
472 .scan_symbols
= FALSE
,
473 .symbol_2_token
= FALSE
,
474 .scope_0_fallback
= FALSE
,
477 * Scan "0b"-, "0"-, and "0x"-prefixed integers, but not
478 * integers prefixed with "$".
484 .scan_hex_dollar
= FALSE
,
486 /* Convert scanned numbers to integer tokens */
487 .numbers_2_int
= TRUE
,
489 /* Support both integers and floating-point numbers */
490 .int_2_float
= FALSE
,
492 /* Scan integers as 64-bit signed integers */
495 /* Only scan double-quoted strings */
496 .scan_string_sq
= FALSE
,
497 .scan_string_dq
= TRUE
,
499 /* Do not converter identifiers to string tokens */
500 .identifier_2_string
= FALSE
,
502 /* Scan characters as G_TOKEN_CHAR token */
503 .char_2_token
= FALSE
,
505 struct ini_parsing_state state
= {
508 .expecting
= INI_EXPECT_MAP_KEY
,
510 .ini_error
= ini_error
,
513 state
.params
= bt_value_map_create();
518 state
.scanner
= g_scanner_new(&scanner_config
);
519 if (!state
.scanner
) {
523 /* Let the scan begin */
524 g_scanner_input_text(state
.scanner
, arg
, strlen(arg
));
527 int ret
= ini_handle_state(&state
);
532 } else if (ret
> 0) {
541 BT_PUT(state
.params
);
545 g_scanner_destroy(state
.scanner
);
548 free(state
.last_map_key
);
553 * Returns the parameters map value object from a command-line
554 * parameter option's argument.
556 * Return value is owned by the caller.
559 struct bt_value
*bt_value_from_arg(const char *arg
)
561 struct bt_value
*params
= NULL
;
562 GString
*ini_error
= NULL
;
564 ini_error
= g_string_new(NULL
);
570 /* Try INI-style parsing */
571 params
= bt_value_from_ini(arg
, ini_error
);
573 printf_err("%s", ini_error
->str
);
579 g_string_free(ini_error
, TRUE
);
585 * Returns the plugin name, component class name, component class type,
586 * and component name from a command-line --component option's argument.
587 * arg must have the following format:
589 * [NAME:]TYPE.PLUGIN.CLS
591 * where NAME is the optional component name, TYPE is either `source`,
592 * `filter`, or `sink`, PLUGIN is the plugin name, and CLS is the
593 * component class name.
595 * On success, both *plugin and *component are not NULL. *plugin
596 * and *comp_cls are owned by the caller. On success, *name can be NULL
597 * if no component class name was found, and *comp_cls_type is set.
600 void plugin_comp_cls_names(const char *arg
, char **name
, char **plugin
,
601 char **comp_cls
, enum bt_component_class_type
*comp_cls_type
)
603 const char *at
= arg
;
604 GString
*gs_name
= NULL
;
605 GString
*gs_comp_cls_type
= NULL
;
606 GString
*gs_plugin
= NULL
;
607 GString
*gs_comp_cls
= NULL
;
613 assert(comp_cls_type
);
615 if (!bt_common_string_is_printable(arg
)) {
616 printf_err("Argument contains a non-printable character\n");
620 /* Parse the component name */
621 gs_name
= bt_common_string_until(at
, ".:\\", ":", &end_pos
);
626 if (arg
[end_pos
] == ':') {
630 g_string_assign(gs_name
, "");
633 /* Parse the component class type */
634 gs_comp_cls_type
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
635 if (!gs_comp_cls_type
|| at
[end_pos
] == '\0') {
636 printf_err("Missing component class type (`source`, `filter`, or `sink`)\n");
640 if (strcmp(gs_comp_cls_type
->str
, "source") == 0 ||
641 strcmp(gs_comp_cls_type
->str
, "src") == 0) {
642 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_SOURCE
;
643 } else if (strcmp(gs_comp_cls_type
->str
, "filter") == 0 ||
644 strcmp(gs_comp_cls_type
->str
, "flt") == 0) {
645 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_FILTER
;
646 } else if (strcmp(gs_comp_cls_type
->str
, "sink") == 0) {
647 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_SINK
;
649 printf_err("Unknown component class type: `%s`\n",
650 gs_comp_cls_type
->str
);
656 /* Parse the plugin name */
657 gs_plugin
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
658 if (!gs_plugin
|| gs_plugin
->len
== 0 || at
[end_pos
] == '\0') {
659 printf_err("Missing plugin or component class name\n");
665 /* Parse the component class name */
666 gs_comp_cls
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
667 if (!gs_comp_cls
|| gs_comp_cls
->len
== 0) {
668 printf_err("Missing component class name\n");
672 if (at
[end_pos
] != '\0') {
673 /* Found a non-escaped `.` */
678 if (gs_name
->len
== 0) {
680 g_string_free(gs_name
, TRUE
);
682 *name
= gs_name
->str
;
683 g_string_free(gs_name
, FALSE
);
686 g_string_free(gs_name
, TRUE
);
689 *plugin
= gs_plugin
->str
;
690 *comp_cls
= gs_comp_cls
->str
;
691 g_string_free(gs_plugin
, FALSE
);
692 g_string_free(gs_comp_cls
, FALSE
);
708 g_string_free(gs_name
, TRUE
);
712 g_string_free(gs_plugin
, TRUE
);
716 g_string_free(gs_comp_cls
, TRUE
);
719 if (gs_comp_cls_type
) {
720 g_string_free(gs_comp_cls_type
, TRUE
);
727 * Prints the Babeltrace version.
730 void print_version(void)
732 puts("Babeltrace " VERSION
);
736 * Destroys a component configuration.
739 void bt_config_component_destroy(struct bt_object
*obj
)
741 struct bt_config_component
*bt_config_component
=
742 container_of(obj
, struct bt_config_component
, base
);
748 if (bt_config_component
->plugin_name
) {
749 g_string_free(bt_config_component
->plugin_name
, TRUE
);
752 if (bt_config_component
->comp_cls_name
) {
753 g_string_free(bt_config_component
->comp_cls_name
, TRUE
);
756 if (bt_config_component
->instance_name
) {
757 g_string_free(bt_config_component
->instance_name
, TRUE
);
760 BT_PUT(bt_config_component
->params
);
761 g_free(bt_config_component
);
768 * Creates a component configuration using the given plugin name and
769 * component name. `plugin_name` and `comp_cls_name` are copied (belong
770 * to the return value).
772 * Return value is owned by the caller.
775 struct bt_config_component
*bt_config_component_create(
776 enum bt_component_class_type type
,
777 const char *plugin_name
, const char *comp_cls_name
)
779 struct bt_config_component
*cfg_component
= NULL
;
781 cfg_component
= g_new0(struct bt_config_component
, 1);
782 if (!cfg_component
) {
787 bt_object_init(cfg_component
, bt_config_component_destroy
);
788 cfg_component
->type
= type
;
789 cfg_component
->plugin_name
= g_string_new(plugin_name
);
790 if (!cfg_component
->plugin_name
) {
795 cfg_component
->comp_cls_name
= g_string_new(comp_cls_name
);
796 if (!cfg_component
->comp_cls_name
) {
801 cfg_component
->instance_name
= g_string_new(NULL
);
802 if (!cfg_component
->instance_name
) {
807 /* Start with empty parameters */
808 cfg_component
->params
= bt_value_map_create();
809 if (!cfg_component
->params
) {
817 BT_PUT(cfg_component
);
820 return cfg_component
;
824 * Creates a component configuration from a command-line --component
828 struct bt_config_component
*bt_config_component_from_arg(const char *arg
)
830 struct bt_config_component
*cfg_comp
= NULL
;
832 char *plugin_name
= NULL
;
833 char *comp_cls_name
= NULL
;
834 enum bt_component_class_type type
;
836 plugin_comp_cls_names(arg
, &name
, &plugin_name
, &comp_cls_name
, &type
);
837 if (!plugin_name
|| !comp_cls_name
) {
841 cfg_comp
= bt_config_component_create(type
, plugin_name
, comp_cls_name
);
847 g_string_assign(cfg_comp
->instance_name
, name
);
858 g_free(comp_cls_name
);
863 * Destroys a configuration.
866 void bt_config_destroy(struct bt_object
*obj
)
868 struct bt_config
*cfg
=
869 container_of(obj
, struct bt_config
, base
);
875 BT_PUT(cfg
->plugin_paths
);
877 switch (cfg
->command
) {
878 case BT_CONFIG_COMMAND_RUN
:
879 if (cfg
->cmd_data
.run
.sources
) {
880 g_ptr_array_free(cfg
->cmd_data
.run
.sources
, TRUE
);
883 if (cfg
->cmd_data
.run
.filters
) {
884 g_ptr_array_free(cfg
->cmd_data
.run
.filters
, TRUE
);
887 if (cfg
->cmd_data
.run
.sinks
) {
888 g_ptr_array_free(cfg
->cmd_data
.run
.sinks
, TRUE
);
891 if (cfg
->cmd_data
.run
.connections
) {
892 g_ptr_array_free(cfg
->cmd_data
.run
.connections
,
896 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
898 case BT_CONFIG_COMMAND_HELP
:
899 BT_PUT(cfg
->cmd_data
.help
.cfg_component
);
901 case BT_CONFIG_COMMAND_QUERY
:
902 BT_PUT(cfg
->cmd_data
.query
.cfg_component
);
904 if (cfg
->cmd_data
.query
.object
) {
905 g_string_free(cfg
->cmd_data
.query
.object
, TRUE
);
908 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
909 if (cfg
->cmd_data
.print_ctf_metadata
.path
) {
910 g_string_free(cfg
->cmd_data
.print_ctf_metadata
.path
,
913 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
917 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
918 if (cfg
->cmd_data
.print_lttng_live_sessions
.url
) {
920 cfg
->cmd_data
.print_lttng_live_sessions
.url
,
923 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
938 void destroy_glist_of_gstring(GList
*list
)
946 for (at
= list
; at
!= NULL
; at
= g_list_next(at
)) {
947 g_string_free(at
->data
, TRUE
);
954 * Creates a simple lexical scanner for parsing comma-delimited names
957 * Return value is owned by the caller.
960 GScanner
*create_csv_identifiers_scanner(void)
963 GScannerConfig scanner_config
= {
964 .cset_skip_characters
= " \t\n",
965 .cset_identifier_first
= G_CSET_a_2_z G_CSET_A_2_Z
"_",
966 .cset_identifier_nth
= G_CSET_a_2_z G_CSET_A_2_Z
":_-",
967 .case_sensitive
= TRUE
,
968 .cpair_comment_single
= NULL
,
969 .skip_comment_multi
= TRUE
,
970 .skip_comment_single
= TRUE
,
971 .scan_comment_multi
= FALSE
,
972 .scan_identifier
= TRUE
,
973 .scan_identifier_1char
= TRUE
,
974 .scan_identifier_NULL
= FALSE
,
975 .scan_symbols
= FALSE
,
976 .symbol_2_token
= FALSE
,
977 .scope_0_fallback
= FALSE
,
978 .scan_binary
= FALSE
,
982 .scan_hex_dollar
= FALSE
,
983 .numbers_2_int
= FALSE
,
984 .int_2_float
= FALSE
,
985 .store_int64
= FALSE
,
986 .scan_string_sq
= FALSE
,
987 .scan_string_dq
= FALSE
,
988 .identifier_2_string
= FALSE
,
989 .char_2_token
= TRUE
,
992 scanner
= g_scanner_new(&scanner_config
);
1001 * Converts a comma-delimited list of known names (--names option) to
1002 * an array value object containing those names as string value objects.
1004 * Return value is owned by the caller.
1007 struct bt_value
*names_from_arg(const char *arg
)
1009 GScanner
*scanner
= NULL
;
1010 struct bt_value
*names
= NULL
;
1011 bool found_all
= false, found_none
= false, found_item
= false;
1013 names
= bt_value_array_create();
1019 scanner
= create_csv_identifiers_scanner();
1024 g_scanner_input_text(scanner
, arg
, strlen(arg
));
1027 GTokenType token_type
= g_scanner_get_next_token(scanner
);
1029 switch (token_type
) {
1030 case G_TOKEN_IDENTIFIER
:
1032 const char *identifier
= scanner
->value
.v_identifier
;
1034 if (!strcmp(identifier
, "payload") ||
1035 !strcmp(identifier
, "args") ||
1036 !strcmp(identifier
, "arg")) {
1038 if (bt_value_array_append_string(names
,
1042 } else if (!strcmp(identifier
, "context") ||
1043 !strcmp(identifier
, "ctx")) {
1045 if (bt_value_array_append_string(names
,
1049 } else if (!strcmp(identifier
, "scope") ||
1050 !strcmp(identifier
, "header")) {
1052 if (bt_value_array_append_string(names
,
1056 } else if (!strcmp(identifier
, "all")) {
1058 if (bt_value_array_append_string(names
,
1062 } else if (!strcmp(identifier
, "none")) {
1064 if (bt_value_array_append_string(names
,
1069 printf_err("Unknown name: `%s`\n",
1085 if (found_none
&& found_all
) {
1086 printf_err("Only either `all` or `none` can be specified in the list given to the --names option, but not both.\n");
1090 * Legacy behavior is to clear the defaults (show none) when at
1091 * least one item is specified.
1093 if (found_item
&& !found_none
&& !found_all
) {
1094 if (bt_value_array_append_string(names
, "none")) {
1099 g_scanner_destroy(scanner
);
1106 g_scanner_destroy(scanner
);
1112 * Converts a comma-delimited list of known fields (--fields option) to
1113 * an array value object containing those fields as string
1116 * Return value is owned by the caller.
1119 struct bt_value
*fields_from_arg(const char *arg
)
1121 GScanner
*scanner
= NULL
;
1122 struct bt_value
*fields
;
1124 fields
= bt_value_array_create();
1130 scanner
= create_csv_identifiers_scanner();
1135 g_scanner_input_text(scanner
, arg
, strlen(arg
));
1138 GTokenType token_type
= g_scanner_get_next_token(scanner
);
1140 switch (token_type
) {
1141 case G_TOKEN_IDENTIFIER
:
1143 const char *identifier
= scanner
->value
.v_identifier
;
1145 if (!strcmp(identifier
, "trace") ||
1146 !strcmp(identifier
, "trace:hostname") ||
1147 !strcmp(identifier
, "trace:domain") ||
1148 !strcmp(identifier
, "trace:procname") ||
1149 !strcmp(identifier
, "trace:vpid") ||
1150 !strcmp(identifier
, "loglevel") ||
1151 !strcmp(identifier
, "emf") ||
1152 !strcmp(identifier
, "callsite") ||
1153 !strcmp(identifier
, "all")) {
1154 if (bt_value_array_append_string(fields
,
1159 printf_err("Unknown field: `%s`\n",
1181 g_scanner_destroy(scanner
);
1187 void append_param_arg(GString
*params_arg
, const char *key
, const char *value
)
1193 if (params_arg
->len
!= 0) {
1194 g_string_append_c(params_arg
, ',');
1197 g_string_append(params_arg
, key
);
1198 g_string_append_c(params_arg
, '=');
1199 g_string_append(params_arg
, value
);
1203 * Inserts the equivalent "prefix-NAME=yes" strings into params_arg
1204 * where the names are in names_array.
1207 int insert_flat_params_from_array(GString
*params_arg
,
1208 struct bt_value
*names_array
, const char *prefix
)
1212 GString
*tmpstr
= NULL
, *default_value
= NULL
;
1213 bool default_set
= false, non_default_set
= false;
1216 * names_array may be NULL if no CLI options were specified to
1217 * trigger its creation.
1223 tmpstr
= g_string_new(NULL
);
1230 default_value
= g_string_new(NULL
);
1231 if (!default_value
) {
1237 for (i
= 0; i
< bt_value_array_size(names_array
); i
++) {
1238 struct bt_value
*str_obj
= bt_value_array_get(names_array
, i
);
1240 bool is_default
= false;
1243 printf_err("Unexpected error\n");
1248 ret
= bt_value_string_get(str_obj
, &suffix
);
1251 printf_err("Unexpected error\n");
1255 g_string_assign(tmpstr
, prefix
);
1256 g_string_append(tmpstr
, "-");
1258 /* Special-case for "all" and "none". */
1259 if (!strcmp(suffix
, "all")) {
1261 g_string_assign(default_value
, "show");
1262 } else if (!strcmp(suffix
, "none")) {
1264 g_string_assign(default_value
, "hide");
1268 g_string_append(tmpstr
, "default");
1269 append_param_arg(params_arg
, tmpstr
->str
,
1270 default_value
->str
);
1272 non_default_set
= true;
1273 g_string_append(tmpstr
, suffix
);
1274 append_param_arg(params_arg
, tmpstr
->str
, "yes");
1278 /* Implicit field-default=hide if any non-default option is set. */
1279 if (non_default_set
&& !default_set
) {
1280 g_string_assign(tmpstr
, prefix
);
1281 g_string_append(tmpstr
, "-default");
1282 g_string_assign(default_value
, "hide");
1283 append_param_arg(params_arg
, tmpstr
->str
, default_value
->str
);
1287 if (default_value
) {
1288 g_string_free(default_value
, TRUE
);
1292 g_string_free(tmpstr
, TRUE
);
1305 OPT_CLOCK_FORCE_CORRELATE
,
1308 OPT_CLOCK_OFFSET_NS
,
1316 OPT_DEBUG_INFO_FULL_PATH
,
1317 OPT_DEBUG_INFO_TARGET_PREFIX
,
1327 OPT_OMIT_HOME_PLUGIN_PATH
,
1328 OPT_OMIT_SYSTEM_PLUGIN_PATH
,
1334 OPT_RESET_BASE_PARAMS
,
1338 OPT_STREAM_INTERSECTION
,
1345 enum bt_config_component_dest
{
1346 BT_CONFIG_COMPONENT_DEST_UNKNOWN
= -1,
1347 BT_CONFIG_COMPONENT_DEST_SOURCE
,
1348 BT_CONFIG_COMPONENT_DEST_FILTER
,
1349 BT_CONFIG_COMPONENT_DEST_SINK
,
1353 * Adds a configuration component to the appropriate configuration
1354 * array depending on the destination.
1357 void add_run_cfg_comp(struct bt_config
*cfg
,
1358 struct bt_config_component
*cfg_comp
,
1359 enum bt_config_component_dest dest
)
1364 case BT_CONFIG_COMPONENT_DEST_SOURCE
:
1365 g_ptr_array_add(cfg
->cmd_data
.run
.sources
, cfg_comp
);
1367 case BT_CONFIG_COMPONENT_DEST_FILTER
:
1368 g_ptr_array_add(cfg
->cmd_data
.run
.filters
, cfg_comp
);
1370 case BT_CONFIG_COMPONENT_DEST_SINK
:
1371 g_ptr_array_add(cfg
->cmd_data
.run
.sinks
, cfg_comp
);
1379 int add_run_cfg_comp_check_name(struct bt_config
*cfg
,
1380 struct bt_config_component
*cfg_comp
,
1381 enum bt_config_component_dest dest
,
1382 struct bt_value
*instance_names
)
1386 if (cfg_comp
->instance_name
->len
== 0) {
1387 printf_err("Found an unnamed component\n");
1392 if (bt_value_map_has_key(instance_names
, cfg_comp
->instance_name
->str
)) {
1393 printf_err("Duplicate component instance name:\n %s\n",
1394 cfg_comp
->instance_name
->str
);
1399 if (bt_value_map_insert(instance_names
,
1400 cfg_comp
->instance_name
->str
, bt_value_null
)) {
1406 add_run_cfg_comp(cfg
, cfg_comp
, dest
);
1413 int append_env_var_plugin_paths(struct bt_value
*plugin_paths
)
1418 if (bt_common_is_setuid_setgid()) {
1419 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1423 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
1428 ret
= bt_config_append_plugin_paths(plugin_paths
, envvar
);
1432 printf_err("Cannot append plugin paths from BABELTRACE_PLUGIN_PATH\n");
1439 int append_home_and_system_plugin_paths(struct bt_value
*plugin_paths
,
1440 bool omit_system_plugin_path
, bool omit_home_plugin_path
)
1444 if (!omit_home_plugin_path
) {
1445 if (bt_common_is_setuid_setgid()) {
1446 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1448 char *home_plugin_dir
=
1449 bt_common_get_home_plugin_path();
1451 if (home_plugin_dir
) {
1452 ret
= bt_config_append_plugin_paths(
1453 plugin_paths
, home_plugin_dir
);
1454 free(home_plugin_dir
);
1457 printf_err("Invalid home plugin path\n");
1464 if (!omit_system_plugin_path
) {
1465 if (bt_config_append_plugin_paths(plugin_paths
,
1466 bt_common_get_system_plugin_path())) {
1467 printf_err("Invalid system plugin path\n");
1473 printf_err("Cannot append home and system plugin paths\n");
1478 int append_home_and_system_plugin_paths_cfg(struct bt_config
*cfg
)
1480 return append_home_and_system_plugin_paths(cfg
->plugin_paths
,
1481 cfg
->omit_system_plugin_path
, cfg
->omit_home_plugin_path
);
1485 struct bt_config
*bt_config_base_create(enum bt_config_command command
,
1486 struct bt_value
*initial_plugin_paths
, bool needs_plugins
)
1488 struct bt_config
*cfg
;
1491 cfg
= g_new0(struct bt_config
, 1);
1497 bt_object_init(cfg
, bt_config_destroy
);
1498 cfg
->command
= command
;
1499 cfg
->command_needs_plugins
= needs_plugins
;
1501 if (initial_plugin_paths
) {
1502 cfg
->plugin_paths
= bt_get(initial_plugin_paths
);
1504 cfg
->plugin_paths
= bt_value_array_create();
1505 if (!cfg
->plugin_paths
) {
1521 struct bt_config
*bt_config_run_create(
1522 struct bt_value
*initial_plugin_paths
)
1524 struct bt_config
*cfg
;
1527 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_RUN
,
1528 initial_plugin_paths
, true);
1533 cfg
->cmd_data
.run
.sources
= g_ptr_array_new_with_free_func(
1534 (GDestroyNotify
) bt_put
);
1535 if (!cfg
->cmd_data
.run
.sources
) {
1540 cfg
->cmd_data
.run
.filters
= g_ptr_array_new_with_free_func(
1541 (GDestroyNotify
) bt_put
);
1542 if (!cfg
->cmd_data
.run
.filters
) {
1547 cfg
->cmd_data
.run
.sinks
= g_ptr_array_new_with_free_func(
1548 (GDestroyNotify
) bt_put
);
1549 if (!cfg
->cmd_data
.run
.sinks
) {
1554 cfg
->cmd_data
.run
.connections
= g_ptr_array_new_with_free_func(
1555 (GDestroyNotify
) bt_config_connection_destroy
);
1556 if (!cfg
->cmd_data
.run
.connections
) {
1571 struct bt_config
*bt_config_list_plugins_create(
1572 struct bt_value
*initial_plugin_paths
)
1574 struct bt_config
*cfg
;
1577 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_LIST_PLUGINS
,
1578 initial_plugin_paths
, true);
1593 struct bt_config
*bt_config_help_create(
1594 struct bt_value
*initial_plugin_paths
)
1596 struct bt_config
*cfg
;
1599 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_HELP
,
1600 initial_plugin_paths
, true);
1605 cfg
->cmd_data
.help
.cfg_component
=
1606 bt_config_component_create(BT_COMPONENT_CLASS_TYPE_UNKNOWN
,
1608 if (!cfg
->cmd_data
.help
.cfg_component
) {
1622 struct bt_config
*bt_config_query_create(
1623 struct bt_value
*initial_plugin_paths
)
1625 struct bt_config
*cfg
;
1628 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_QUERY
,
1629 initial_plugin_paths
, true);
1634 cfg
->cmd_data
.query
.object
= g_string_new(NULL
);
1635 if (!cfg
->cmd_data
.query
.object
) {
1650 struct bt_config
*bt_config_print_ctf_metadata_create(
1651 struct bt_value
*initial_plugin_paths
)
1653 struct bt_config
*cfg
;
1656 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_PRINT_CTF_METADATA
,
1657 initial_plugin_paths
, true);
1662 cfg
->cmd_data
.print_ctf_metadata
.path
= g_string_new(NULL
);
1663 if (!cfg
->cmd_data
.print_ctf_metadata
.path
) {
1668 cfg
->cmd_data
.print_ctf_metadata
.output_path
= g_string_new(NULL
);
1669 if (!cfg
->cmd_data
.print_ctf_metadata
.output_path
) {
1684 struct bt_config
*bt_config_print_lttng_live_sessions_create(
1685 struct bt_value
*initial_plugin_paths
)
1687 struct bt_config
*cfg
;
1690 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
,
1691 initial_plugin_paths
, true);
1696 cfg
->cmd_data
.print_lttng_live_sessions
.url
= g_string_new(NULL
);
1697 if (!cfg
->cmd_data
.print_lttng_live_sessions
.url
) {
1702 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
=
1704 if (!cfg
->cmd_data
.print_lttng_live_sessions
.output_path
) {
1719 int bt_config_append_plugin_paths_check_setuid_setgid(
1720 struct bt_value
*plugin_paths
, const char *arg
)
1724 if (bt_common_is_setuid_setgid()) {
1725 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1729 if (bt_config_append_plugin_paths(plugin_paths
, arg
)) {
1730 printf_err("Invalid --plugin-path option's argument:\n %s\n",
1741 * Prints the expected format for a --params option.
1744 void print_expected_params_format(FILE *fp
)
1746 fprintf(fp
, "Expected format of PARAMS\n");
1747 fprintf(fp
, "-------------------------\n");
1749 fprintf(fp
, " PARAM=VALUE[,PARAM=VALUE]...\n");
1751 fprintf(fp
, "The parameter string is a comma-separated list of PARAM=VALUE assignments,\n");
1752 fprintf(fp
, "where PARAM is the parameter name (C identifier plus the [:.-] characters),\n");
1753 fprintf(fp
, "and VALUE can be one of:\n");
1755 fprintf(fp
, "* `null`, `nul`, `NULL`: null value (no backticks).\n");
1756 fprintf(fp
, "* `true`, `TRUE`, `yes`, `YES`: true boolean value (no backticks).\n");
1757 fprintf(fp
, "* `false`, `FALSE`, `no`, `NO`: false boolean value (no backticks).\n");
1758 fprintf(fp
, "* Binary (`0b` prefix), octal (`0` prefix), decimal, or hexadecimal\n");
1759 fprintf(fp
, " (`0x` prefix) signed 64-bit integer.\n");
1760 fprintf(fp
, "* Double precision floating point number (scientific notation is accepted).\n");
1761 fprintf(fp
, "* Unquoted string with no special characters, and not matching any of\n");
1762 fprintf(fp
, " the null and boolean value symbols above.\n");
1763 fprintf(fp
, "* Double-quoted string (accepts escape characters).\n");
1765 fprintf(fp
, "You can put whitespaces allowed around individual `=` and `,` symbols.\n");
1767 fprintf(fp
, "Example:\n");
1769 fprintf(fp
, " many=null, fresh=yes, condition=false, squirrel=-782329,\n");
1770 fprintf(fp
, " observe=3.14, simple=beef, needs-quotes=\"some string\",\n");
1771 fprintf(fp
, " escape.chars-are:allowed=\"this is a \\\" double quote\"\n");
1773 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1774 fprintf(fp
, "babeltrace from a shell.\n");
1779 * Prints the help command usage.
1782 void print_help_usage(FILE *fp
)
1784 fprintf(fp
, "Usage: babeltrace [GENERAL OPTIONS] help [OPTIONS] PLUGIN\n");
1785 fprintf(fp
, " babeltrace [GENERAL OPTIONS] help [OPTIONS] TYPE.PLUGIN.CLS\n");
1787 fprintf(fp
, "Options:\n");
1789 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1790 fprintf(fp
, " (~/.local/lib/babeltrace/plugins)\n");
1791 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1792 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1793 fprintf(fp
, " dynamic plugins can be loaded\n");
1794 fprintf(fp
, " -h, --help Show this help and quit\n");
1796 fprintf(fp
, "See `babeltrace --help` for the list of general options.\n");
1798 fprintf(fp
, "Use `babeltrace list-plugins` to show the list of available plugins.\n");
1802 struct poptOption help_long_options
[] = {
1803 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
1804 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
1805 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
1806 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
1807 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
1808 { NULL
, 0, '\0', NULL
, 0, NULL
, NULL
},
1812 * Creates a Babeltrace config object from the arguments of a help
1815 * *retcode is set to the appropriate exit code to use.
1818 struct bt_config
*bt_config_help_from_args(int argc
, const char *argv
[],
1819 int *retcode
, bool force_omit_system_plugin_path
,
1820 bool force_omit_home_plugin_path
,
1821 struct bt_value
*initial_plugin_paths
)
1823 poptContext pc
= NULL
;
1827 struct bt_config
*cfg
= NULL
;
1828 const char *leftover
;
1829 char *plugin_name
= NULL
, *comp_cls_name
= NULL
;
1832 cfg
= bt_config_help_create(initial_plugin_paths
);
1837 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
1838 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
1839 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
1845 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
1846 help_long_options
, 0);
1848 printf_err("Cannot get popt context\n");
1852 poptReadDefaultConfig(pc
, 0);
1854 while ((opt
= poptGetNextOpt(pc
)) > 0) {
1855 arg
= poptGetOptArg(pc
);
1858 case OPT_PLUGIN_PATH
:
1859 if (bt_config_append_plugin_paths_check_setuid_setgid(
1860 cfg
->plugin_paths
, arg
)) {
1864 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
1865 cfg
->omit_system_plugin_path
= true;
1867 case OPT_OMIT_HOME_PLUGIN_PATH
:
1868 cfg
->omit_home_plugin_path
= true;
1871 print_help_usage(stdout
);
1876 printf_err("Unknown command-line option specified (option code %d)\n",
1885 /* Check for option parsing error */
1887 printf_err("While parsing command-line options, at option %s: %s\n",
1888 poptBadOption(pc
, 0), poptStrerror(opt
));
1892 leftover
= poptGetArg(pc
);
1894 plugin_comp_cls_names(leftover
, NULL
,
1895 &plugin_name
, &comp_cls_name
,
1896 &cfg
->cmd_data
.help
.cfg_component
->type
);
1897 if (plugin_name
&& comp_cls_name
) {
1898 /* Component class help */
1900 cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
1903 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
,
1906 /* Fall back to plugin help */
1907 cfg
->cmd_data
.help
.cfg_component
->type
=
1908 BT_COMPONENT_CLASS_TYPE_UNKNOWN
;
1910 cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
1914 print_help_usage(stdout
);
1920 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
1931 g_free(plugin_name
);
1932 g_free(comp_cls_name
);
1935 poptFreeContext(pc
);
1943 * Prints the help command usage.
1946 void print_query_usage(FILE *fp
)
1948 fprintf(fp
, "Usage: babeltrace [GEN OPTS] query [OPTS] TYPE.PLUGIN.CLS OBJECT\n");
1950 fprintf(fp
, "Options:\n");
1952 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1953 fprintf(fp
, " (~/.local/lib/babeltrace/plugins)\n");
1954 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1955 fprintf(fp
, " -p, --params=PARAMS Set the query parameters to PARAMS\n");
1956 fprintf(fp
, " (see the expected format of PARAMS below)\n");
1957 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1958 fprintf(fp
, " dynamic plugins can be loaded\n");
1959 fprintf(fp
, " -h, --help Show this help and quit\n");
1960 fprintf(fp
, "\n\n");
1961 print_expected_params_format(fp
);
1965 struct poptOption query_long_options
[] = {
1966 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
1967 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
1968 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
1969 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
1970 { "params", 'p', POPT_ARG_STRING
, NULL
, OPT_PARAMS
, NULL
, NULL
},
1971 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
1972 { NULL
, 0, '\0', NULL
, 0, NULL
, NULL
},
1976 * Creates a Babeltrace config object from the arguments of a query
1979 * *retcode is set to the appropriate exit code to use.
1982 struct bt_config
*bt_config_query_from_args(int argc
, const char *argv
[],
1983 int *retcode
, bool force_omit_system_plugin_path
,
1984 bool force_omit_home_plugin_path
,
1985 struct bt_value
*initial_plugin_paths
)
1987 poptContext pc
= NULL
;
1991 struct bt_config
*cfg
= NULL
;
1992 const char *leftover
;
1993 struct bt_value
*params
= bt_value_null
;
1996 cfg
= bt_config_query_create(initial_plugin_paths
);
2001 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
2002 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
2003 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
2009 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
2010 query_long_options
, 0);
2012 printf_err("Cannot get popt context\n");
2016 poptReadDefaultConfig(pc
, 0);
2018 while ((opt
= poptGetNextOpt(pc
)) > 0) {
2019 arg
= poptGetOptArg(pc
);
2022 case OPT_PLUGIN_PATH
:
2023 if (bt_config_append_plugin_paths_check_setuid_setgid(
2024 cfg
->plugin_paths
, arg
)) {
2028 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
2029 cfg
->omit_system_plugin_path
= true;
2031 case OPT_OMIT_HOME_PLUGIN_PATH
:
2032 cfg
->omit_home_plugin_path
= true;
2037 params
= bt_value_from_arg(arg
);
2039 printf_err("Invalid format for --params option's argument:\n %s\n",
2046 print_query_usage(stdout
);
2051 printf_err("Unknown command-line option specified (option code %d)\n",
2060 /* Check for option parsing error */
2062 printf_err("While parsing command-line options, at option %s: %s\n",
2063 poptBadOption(pc
, 0), poptStrerror(opt
));
2068 * We need exactly two leftover arguments which are the
2069 * mandatory component class specification and query object.
2071 leftover
= poptGetArg(pc
);
2073 cfg
->cmd_data
.query
.cfg_component
=
2074 bt_config_component_from_arg(leftover
);
2075 if (!cfg
->cmd_data
.query
.cfg_component
) {
2076 printf_err("Invalid format for component class specification:\n %s\n",
2082 BT_MOVE(cfg
->cmd_data
.query
.cfg_component
->params
, params
);
2084 print_query_usage(stdout
);
2090 leftover
= poptGetArg(pc
);
2092 if (strlen(leftover
) == 0) {
2093 printf_err("Invalid empty object\n");
2097 g_string_assign(cfg
->cmd_data
.query
.object
, leftover
);
2099 print_query_usage(stdout
);
2105 leftover
= poptGetArg(pc
);
2107 printf_err("Unexpected argument: %s\n", leftover
);
2111 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
2123 poptFreeContext(pc
);
2132 * Prints the list-plugins command usage.
2135 void print_list_plugins_usage(FILE *fp
)
2137 fprintf(fp
, "Usage: babeltrace [GENERAL OPTIONS] list-plugins [OPTIONS]\n");
2139 fprintf(fp
, "Options:\n");
2141 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2142 fprintf(fp
, " (~/.local/lib/babeltrace/plugins)\n");
2143 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2144 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2145 fprintf(fp
, " dynamic plugins can be loaded\n");
2146 fprintf(fp
, " -h, --help Show this help and quit\n");
2148 fprintf(fp
, "See `babeltrace --help` for the list of general options.\n");
2150 fprintf(fp
, "Use `babeltrace help` to get help for a specific plugin or component class.\n");
2154 struct poptOption list_plugins_long_options
[] = {
2155 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
2156 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
2157 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
2158 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
2159 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
2160 { NULL
, 0, '\0', NULL
, 0, NULL
, NULL
},
2164 * Creates a Babeltrace config object from the arguments of a
2165 * list-plugins command.
2167 * *retcode is set to the appropriate exit code to use.
2170 struct bt_config
*bt_config_list_plugins_from_args(int argc
, const char *argv
[],
2171 int *retcode
, bool force_omit_system_plugin_path
,
2172 bool force_omit_home_plugin_path
,
2173 struct bt_value
*initial_plugin_paths
)
2175 poptContext pc
= NULL
;
2179 struct bt_config
*cfg
= NULL
;
2180 const char *leftover
;
2183 cfg
= bt_config_list_plugins_create(initial_plugin_paths
);
2188 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
2189 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
2190 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
2196 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
2197 list_plugins_long_options
, 0);
2199 printf_err("Cannot get popt context\n");
2203 poptReadDefaultConfig(pc
, 0);
2205 while ((opt
= poptGetNextOpt(pc
)) > 0) {
2206 arg
= poptGetOptArg(pc
);
2209 case OPT_PLUGIN_PATH
:
2210 if (bt_config_append_plugin_paths_check_setuid_setgid(
2211 cfg
->plugin_paths
, arg
)) {
2215 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
2216 cfg
->omit_system_plugin_path
= true;
2218 case OPT_OMIT_HOME_PLUGIN_PATH
:
2219 cfg
->omit_home_plugin_path
= true;
2222 print_list_plugins_usage(stdout
);
2227 printf_err("Unknown command-line option specified (option code %d)\n",
2236 /* Check for option parsing error */
2238 printf_err("While parsing command-line options, at option %s: %s\n",
2239 poptBadOption(pc
, 0), poptStrerror(opt
));
2243 leftover
= poptGetArg(pc
);
2245 printf_err("Unexpected argument: %s\n", leftover
);
2249 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
2261 poptFreeContext(pc
);
2269 * Prints the run command usage.
2272 void print_run_usage(FILE *fp
)
2274 fprintf(fp
, "Usage: babeltrace [GENERAL OPTIONS] run [OPTIONS]\n");
2276 fprintf(fp
, "Options:\n");
2278 fprintf(fp
, " -b, --base-params=PARAMS Set PARAMS as the current base parameters\n");
2279 fprintf(fp
, " for all the following components until\n");
2280 fprintf(fp
, " --reset-base-params is encountered\n");
2281 fprintf(fp
, " (see the expected format of PARAMS below)\n");
2282 fprintf(fp
, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2283 fprintf(fp
, " Instantiate the component class CLS of type\n");
2284 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
2285 fprintf(fp
, " in the plugin PLUGIN, add it to the graph,\n");
2286 fprintf(fp
, " and optionally name it NAME (you can also\n");
2287 fprintf(fp
, " specify the name with --name)\n");
2288 fprintf(fp
, " -C, --connect=CONNECTION Connect two created components (see the\n");
2289 fprintf(fp
, " expected format of CONNECTION below)\n");
2290 fprintf(fp
, " --key=KEY Set the current initialization string\n");
2291 fprintf(fp
, " parameter key to KEY (see --value)\n");
2292 fprintf(fp
, " -n, --name=NAME Set the name of the current component\n");
2293 fprintf(fp
, " to NAME (must be unique amongst all the\n");
2294 fprintf(fp
, " names of the created components)\n");
2295 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2296 fprintf(fp
, " (~/.local/lib/babeltrace/plugins)\n");
2297 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2298 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2299 fprintf(fp
, " current component (see the expected format\n");
2300 fprintf(fp
, " of PARAMS below)\n");
2301 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2302 fprintf(fp
, " dynamic plugins can be loaded\n");
2303 fprintf(fp
, " -r, --reset-base-params Reset the current base parameters to an\n");
2304 fprintf(fp
, " empty map\n");
2305 fprintf(fp
, " --retry-duration=DUR When babeltrace(1) needs to retry to run\n");
2306 fprintf(fp
, " the graph later, retry in DUR µs\n");
2307 fprintf(fp
, " (default: 100000)\n");
2308 fprintf(fp
, " --value=VAL Add a string initialization parameter to\n");
2309 fprintf(fp
, " the current component with a name given by\n");
2310 fprintf(fp
, " the last argument of the --key option and a\n");
2311 fprintf(fp
, " value set to VAL\n");
2312 fprintf(fp
, " -h, --help Show this help and quit\n");
2314 fprintf(fp
, "See `babeltrace --help` for the list of general options.\n");
2315 fprintf(fp
, "\n\n");
2316 fprintf(fp
, "Expected format of CONNECTION\n");
2317 fprintf(fp
, "-----------------------------\n");
2319 fprintf(fp
, " UPSTREAM[.UPSTREAM-PORT]:DOWNSTREAM[.DOWNSTREAM-PORT]\n");
2321 fprintf(fp
, "UPSTREAM and DOWNSTREAM are names of the upstream and downstream\n");
2322 fprintf(fp
, "components to connect together. You must escape the following characters\n\n");
2323 fprintf(fp
, "with `\\`: `\\`, `.`, and `:`. You can set the name of the current\n");
2324 fprintf(fp
, "component with the --name option.\n");
2326 fprintf(fp
, "UPSTREAM-PORT and DOWNSTREAM-PORT are optional globbing patterns to\n");
2327 fprintf(fp
, "identify the upstream and downstream ports to use for the connection.\n");
2328 fprintf(fp
, "When the port is not specified, `*` is used.\n");
2330 fprintf(fp
, "When a component named UPSTREAM has an available port which matches the\n");
2331 fprintf(fp
, "UPSTREAM-PORT globbing pattern, it is connected to the first port which\n");
2332 fprintf(fp
, "matches the DOWNSTREAM-PORT globbing pattern of the component named\n");
2333 fprintf(fp
, "DOWNSTREAM.\n");
2335 fprintf(fp
, "The only special character in UPSTREAM-PORT and DOWNSTREAM-PORT is `*`\n");
2336 fprintf(fp
, "which matches anything. You must escape the following characters\n");
2337 fprintf(fp
, "with `\\`: `\\`, `*`, `?`, `[`, `.`, and `:`.\n");
2339 fprintf(fp
, "You can connect a source component to a filter or sink component. You\n");
2340 fprintf(fp
, "can connect a filter component to a sink component.\n");
2342 fprintf(fp
, "Examples:\n");
2344 fprintf(fp
, " my-src:my-sink\n");
2345 fprintf(fp
, " ctf-fs.*stream*:utils-muxer:*\n");
2347 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
2348 fprintf(fp
, "babeltrace from a shell.\n");
2349 fprintf(fp
, "\n\n");
2350 print_expected_params_format(fp
);
2354 * Creates a Babeltrace config object from the arguments of a run
2357 * *retcode is set to the appropriate exit code to use.
2360 struct bt_config
*bt_config_run_from_args(int argc
, const char *argv
[],
2361 int *retcode
, bool force_omit_system_plugin_path
,
2362 bool force_omit_home_plugin_path
,
2363 struct bt_value
*initial_plugin_paths
)
2365 poptContext pc
= NULL
;
2367 struct bt_config_component
*cur_cfg_comp
= NULL
;
2368 enum bt_config_component_dest cur_cfg_comp_dest
=
2369 BT_CONFIG_COMPONENT_DEST_UNKNOWN
;
2370 struct bt_value
*cur_base_params
= NULL
;
2372 struct bt_config
*cfg
= NULL
;
2373 struct bt_value
*instance_names
= NULL
;
2374 struct bt_value
*connection_args
= NULL
;
2375 GString
*cur_param_key
= NULL
;
2376 char error_buf
[256] = { 0 };
2377 long long retry_duration
= -1;
2378 struct poptOption run_long_options
[] = {
2379 { "base-params", 'b', POPT_ARG_STRING
, NULL
, OPT_BASE_PARAMS
, NULL
, NULL
},
2380 { "component", 'c', POPT_ARG_STRING
, NULL
, OPT_COMPONENT
, NULL
, NULL
},
2381 { "connect", 'C', POPT_ARG_STRING
, NULL
, OPT_CONNECT
, NULL
, NULL
},
2382 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
2383 { "key", '\0', POPT_ARG_STRING
, NULL
, OPT_KEY
, NULL
, NULL
},
2384 { "name", 'n', POPT_ARG_STRING
, NULL
, OPT_NAME
, NULL
, NULL
},
2385 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
2386 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
2387 { "params", 'p', POPT_ARG_STRING
, NULL
, OPT_PARAMS
, NULL
, NULL
},
2388 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
2389 { "reset-base-params", 'r', POPT_ARG_NONE
, NULL
, OPT_RESET_BASE_PARAMS
, NULL
, NULL
},
2390 { "retry-duration", '\0', POPT_ARG_LONGLONG
, &retry_duration
, OPT_RETRY_DURATION
, NULL
, NULL
},
2391 { "value", '\0', POPT_ARG_STRING
, NULL
, OPT_VALUE
, NULL
, NULL
},
2392 { NULL
, 0, '\0', NULL
, 0, NULL
, NULL
},
2396 cur_param_key
= g_string_new(NULL
);
2397 if (!cur_param_key
) {
2403 print_run_usage(stdout
);
2408 cfg
= bt_config_run_create(initial_plugin_paths
);
2413 cfg
->cmd_data
.run
.retry_duration_us
= 100000;
2414 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
2415 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
2416 cur_base_params
= bt_value_map_create();
2417 if (!cur_base_params
) {
2422 instance_names
= bt_value_map_create();
2423 if (!instance_names
) {
2428 connection_args
= bt_value_array_create();
2429 if (!connection_args
) {
2434 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
2440 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
2441 run_long_options
, 0);
2443 printf_err("Cannot get popt context\n");
2447 poptReadDefaultConfig(pc
, 0);
2449 while ((opt
= poptGetNextOpt(pc
)) > 0) {
2450 arg
= poptGetOptArg(pc
);
2453 case OPT_PLUGIN_PATH
:
2454 if (bt_config_append_plugin_paths_check_setuid_setgid(
2455 cfg
->plugin_paths
, arg
)) {
2459 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
2460 cfg
->omit_system_plugin_path
= true;
2462 case OPT_OMIT_HOME_PLUGIN_PATH
:
2463 cfg
->omit_home_plugin_path
= true;
2467 enum bt_config_component_dest new_dest
;
2470 ret
= add_run_cfg_comp_check_name(cfg
,
2471 cur_cfg_comp
, cur_cfg_comp_dest
,
2473 BT_PUT(cur_cfg_comp
);
2479 cur_cfg_comp
= bt_config_component_from_arg(arg
);
2480 if (!cur_cfg_comp
) {
2481 printf_err("Invalid format for --component option's argument:\n %s\n",
2486 switch (cur_cfg_comp
->type
) {
2487 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2488 new_dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
2490 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2491 new_dest
= BT_CONFIG_COMPONENT_DEST_FILTER
;
2493 case BT_COMPONENT_CLASS_TYPE_SINK
:
2494 new_dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
2500 assert(cur_base_params
);
2501 bt_put(cur_cfg_comp
->params
);
2502 cur_cfg_comp
->params
= bt_value_copy(cur_base_params
);
2503 if (!cur_cfg_comp
->params
) {
2508 cur_cfg_comp_dest
= new_dest
;
2513 struct bt_value
*params
;
2514 struct bt_value
*params_to_set
;
2516 if (!cur_cfg_comp
) {
2517 printf_err("Cannot add parameters to unavailable component:\n %s\n",
2522 params
= bt_value_from_arg(arg
);
2524 printf_err("Invalid format for --params option's argument:\n %s\n",
2529 params_to_set
= bt_value_map_extend(cur_cfg_comp
->params
,
2532 if (!params_to_set
) {
2533 printf_err("Cannot extend current component parameters with --params option's argument:\n %s\n",
2538 BT_MOVE(cur_cfg_comp
->params
, params_to_set
);
2542 if (strlen(arg
) == 0) {
2543 printf_err("Cannot set an empty string as the current parameter key\n");
2547 g_string_assign(cur_param_key
, arg
);
2550 if (!cur_cfg_comp
) {
2551 printf_err("Cannot set a parameter's value of unavailable component:\n %s\n",
2556 if (cur_param_key
->len
== 0) {
2557 printf_err("--value option specified without preceding --key option:\n %s\n",
2562 if (bt_value_map_insert_string(cur_cfg_comp
->params
,
2563 cur_param_key
->str
, arg
)) {
2569 if (!cur_cfg_comp
) {
2570 printf_err("Cannot set the name of unavailable component:\n %s\n",
2575 g_string_assign(cur_cfg_comp
->instance_name
, arg
);
2577 case OPT_BASE_PARAMS
:
2579 struct bt_value
*params
= bt_value_from_arg(arg
);
2582 printf_err("Invalid format for --base-params option's argument:\n %s\n",
2587 BT_MOVE(cur_base_params
, params
);
2590 case OPT_RESET_BASE_PARAMS
:
2591 BT_PUT(cur_base_params
);
2592 cur_base_params
= bt_value_map_create();
2593 if (!cur_base_params
) {
2599 if (bt_value_array_append_string(connection_args
,
2605 case OPT_RETRY_DURATION
:
2606 if (retry_duration
< 0) {
2607 printf_err("--retry-duration option's argument must be positive or 0: %lld\n",
2612 cfg
->cmd_data
.run
.retry_duration_us
=
2613 (uint64_t) retry_duration
;
2616 print_run_usage(stdout
);
2621 printf_err("Unknown command-line option specified (option code %d)\n",
2630 /* Check for option parsing error */
2632 printf_err("While parsing command-line options, at option %s: %s\n",
2633 poptBadOption(pc
, 0), poptStrerror(opt
));
2637 /* This command does not accept leftover arguments */
2638 if (poptPeekArg(pc
)) {
2639 printf_err("Unexpected argument: %s\n", poptPeekArg(pc
));
2643 /* Add current component */
2645 ret
= add_run_cfg_comp_check_name(cfg
, cur_cfg_comp
,
2646 cur_cfg_comp_dest
, instance_names
);
2647 BT_PUT(cur_cfg_comp
);
2653 if (cfg
->cmd_data
.run
.sources
->len
== 0) {
2654 printf_err("Incomplete graph: no source component\n");
2658 if (cfg
->cmd_data
.run
.sinks
->len
== 0) {
2659 printf_err("Incomplete graph: no sink component\n");
2663 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
2667 ret
= bt_config_cli_args_create_connections(cfg
, connection_args
,
2670 printf_err("Cannot creation connections:\n%s", error_buf
);
2682 poptFreeContext(pc
);
2685 if (cur_param_key
) {
2686 g_string_free(cur_param_key
, TRUE
);
2690 BT_PUT(cur_cfg_comp
);
2691 BT_PUT(cur_base_params
);
2692 BT_PUT(instance_names
);
2693 BT_PUT(connection_args
);
2698 struct bt_config
*bt_config_run_from_args_array(struct bt_value
*run_args
,
2699 int *retcode
, bool force_omit_system_plugin_path
,
2700 bool force_omit_home_plugin_path
,
2701 struct bt_value
*initial_plugin_paths
)
2703 struct bt_config
*cfg
= NULL
;
2706 const size_t argc
= bt_value_array_size(run_args
) + 1;
2708 argv
= calloc(argc
, sizeof(*argv
));
2716 len
= bt_value_array_size(run_args
);
2718 printf_err("Invalid executable arguments\n");
2721 for (i
= 0; i
< len
; i
++) {
2723 struct bt_value
*arg_value
= bt_value_array_get(run_args
, i
);
2727 ret
= bt_value_string_get(arg_value
, &arg
);
2734 cfg
= bt_config_run_from_args(argc
, argv
, retcode
,
2735 force_omit_system_plugin_path
, force_omit_home_plugin_path
,
2736 initial_plugin_paths
);
2744 * Prints the convert command usage.
2747 void print_convert_usage(FILE *fp
)
2749 fprintf(fp
, "Usage: babeltrace [GENERAL OPTIONS] [convert] [OPTIONS] [PATH/URL]\n");
2751 fprintf(fp
, "Options:\n");
2753 fprintf(fp
, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2754 fprintf(fp
, " Instantiate the component class CLS of type\n");
2755 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
2756 fprintf(fp
, " in the plugin PLUGIN, add it to the\n");
2757 fprintf(fp
, " conversion graph, and optionally name it\n");
2758 fprintf(fp
, " NAME (you can also specify the name with\n");
2759 fprintf(fp
, " --name)\n");
2760 fprintf(fp
, " --name=NAME Set the name of the current component\n");
2761 fprintf(fp
, " to NAME (must be unique amongst all the\n");
2762 fprintf(fp
, " names of the created components)\n");
2763 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2764 fprintf(fp
, " (~/.local/lib/babeltrace/plugins)\n");
2765 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2766 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2767 fprintf(fp
, " current component (see the expected format\n");
2768 fprintf(fp
, " of PARAMS below)\n");
2769 fprintf(fp
, " -P, --path=PATH Set the `path` string parameter of the\n");
2770 fprintf(fp
, " current component to PATH\n");
2771 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2772 fprintf(fp
, " --retry-duration=DUR When babeltrace(1) needs to retry to run\n");
2773 fprintf(fp
, " the graph later, retry in DUR µs\n");
2774 fprintf(fp
, " (default: 100000)\n");
2775 fprintf(fp
, " dynamic plugins can be loaded\n");
2776 fprintf(fp
, " --run-args Print the equivalent arguments for the\n");
2777 fprintf(fp
, " `run` command to the standard output,\n");
2778 fprintf(fp
, " formatted for a shell, and quit\n");
2779 fprintf(fp
, " --run-args-0 Print the equivalent arguments for the\n");
2780 fprintf(fp
, " `run` command to the standard output,\n");
2781 fprintf(fp
, " formatted for `xargs -0`, and quit\n");
2782 fprintf(fp
, " --stream-intersection Only process events when all streams\n");
2783 fprintf(fp
, " are active\n");
2784 fprintf(fp
, " -u, --url=URL Set the `url` string parameter of the\n");
2785 fprintf(fp
, " current component to URL\n");
2786 fprintf(fp
, " -h, --help Show this help and quit\n");
2788 fprintf(fp
, "Implicit `source.ctf.fs` component options:\n");
2790 fprintf(fp
, " --clock-offset=SEC Set clock offset to SEC seconds\n");
2791 fprintf(fp
, " --clock-offset-ns=NS Set clock offset to NS ns\n");
2793 fprintf(fp
, "Implicit `sink.text.pretty` component options:\n");
2795 fprintf(fp
, " --clock-cycles Print timestamps in clock cycles\n");
2796 fprintf(fp
, " --clock-date Print timestamp dates\n");
2797 fprintf(fp
, " --clock-gmt Print and parse timestamps in the GMT\n");
2798 fprintf(fp
, " time zone instead of the local time zone\n");
2799 fprintf(fp
, " --clock-seconds Print the timestamps as `SEC.NS` instead\n");
2800 fprintf(fp
, " of `hh:mm:ss.nnnnnnnnn`\n");
2801 fprintf(fp
, " --color=(never | auto | always)\n");
2802 fprintf(fp
, " Never, automatically, or always emit\n");
2803 fprintf(fp
, " console color codes\n");
2804 fprintf(fp
, " -f, --fields=FIELD[,FIELD]... Print additional fields; FIELD can be:\n");
2805 fprintf(fp
, " `all`, `trace`, `trace:hostname`,\n");
2806 fprintf(fp
, " `trace:domain`, `trace:procname`,\n");
2807 fprintf(fp
, " `trace:vpid`, `loglevel`, `emf`\n");
2808 fprintf(fp
, " -n, --names=NAME[,NAME]... Print field names; NAME can be:\n");
2809 fprintf(fp
, " `payload` (or `arg` or `args`), `none`,\n");
2810 fprintf(fp
, " `all`, `scope`, `header`, `context`\n");
2811 fprintf(fp
, " (or `ctx`)\n");
2812 fprintf(fp
, " --no-delta Do not print time delta between\n");
2813 fprintf(fp
, " consecutive events\n");
2814 fprintf(fp
, " -w, --output=PATH Write output text to PATH instead of\n");
2815 fprintf(fp
, " the standard output\n");
2817 fprintf(fp
, "Implicit `filter.utils.muxer` component options:\n");
2819 fprintf(fp
, " --clock-force-correlate Assume that clocks are inherently\n");
2820 fprintf(fp
, " correlated across traces\n");
2822 fprintf(fp
, "Implicit `filter.utils.trimmer` component options:\n");
2824 fprintf(fp
, " -b, --begin=BEGIN Set the beginning time of the conversion\n");
2825 fprintf(fp
, " time range to BEGIN (see the format of\n");
2826 fprintf(fp
, " BEGIN below)\n");
2827 fprintf(fp
, " -e, --end=END Set the end time of the conversion time\n");
2828 fprintf(fp
, " range to END (see the format of END below)\n");
2829 fprintf(fp
, " -t, --timerange=TIMERANGE Set conversion time range to TIMERANGE:\n");
2830 fprintf(fp
, " BEGIN,END or [BEGIN,END] (literally `[` and\n");
2831 fprintf(fp
, " `]`) (see the format of BEGIN/END below)\n");
2833 fprintf(fp
, "Implicit `filter.lttng-utils.debug-info` component options:\n");
2835 fprintf(fp
, " --debug-info Create an implicit\n");
2836 fprintf(fp
, " `filter.lttng-utils.debug-info` component\n");
2837 fprintf(fp
, " --debug-info-dir=DIR Search for debug info in directory DIR\n");
2838 fprintf(fp
, " instead of `/usr/lib/debug`\n");
2839 fprintf(fp
, " --debug-info-full-path Show full debug info source and\n");
2840 fprintf(fp
, " binary paths instead of just names\n");
2841 fprintf(fp
, " --debug-info-target-prefix=DIR\n");
2842 fprintf(fp
, " Use directory DIR as a prefix when\n");
2843 fprintf(fp
, " looking up executables during debug\n");
2844 fprintf(fp
, " info analysis\n");
2846 fprintf(fp
, "Legacy options that still work:\n");
2848 fprintf(fp
, " -i, --input-format=(ctf | lttng-live)\n");
2849 fprintf(fp
, " `ctf`:\n");
2850 fprintf(fp
, " Create an implicit `source.ctf.fs`\n");
2851 fprintf(fp
, " component\n");
2852 fprintf(fp
, " `lttng-live`:\n");
2853 fprintf(fp
, " Create an implicit `source.ctf.lttng-live`\n");
2854 fprintf(fp
, " component\n");
2855 fprintf(fp
, " -o, --output-format=(text | ctf | dummy | ctf-metadata)\n");
2856 fprintf(fp
, " `text`:\n");
2857 fprintf(fp
, " Create an implicit `sink.text.pretty`\n");
2858 fprintf(fp
, " component\n");
2859 fprintf(fp
, " `ctf`:\n");
2860 fprintf(fp
, " Create an implicit `sink.ctf.fs`\n");
2861 fprintf(fp
, " component\n");
2862 fprintf(fp
, " `dummy`:\n");
2863 fprintf(fp
, " Create an implicit `sink.utils.dummy`\n");
2864 fprintf(fp
, " component\n");
2865 fprintf(fp
, " `ctf-metadata`:\n");
2866 fprintf(fp
, " Query the `source.ctf.fs` component class\n");
2867 fprintf(fp
, " for metadata text and quit\n");
2869 fprintf(fp
, "See `babeltrace --help` for the list of general options.\n");
2870 fprintf(fp
, "\n\n");
2871 fprintf(fp
, "Format of BEGIN and END\n");
2872 fprintf(fp
, "-----------------------\n");
2874 fprintf(fp
, " [YYYY-MM-DD [hh:mm:]]ss[.nnnnnnnnn]\n");
2875 fprintf(fp
, "\n\n");
2876 print_expected_params_format(fp
);
2880 struct poptOption convert_long_options
[] = {
2881 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
2882 { "begin", 'b', POPT_ARG_STRING
, NULL
, OPT_BEGIN
, NULL
, NULL
},
2883 { "clock-cycles", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_CYCLES
, NULL
, NULL
},
2884 { "clock-date", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_DATE
, NULL
, NULL
},
2885 { "clock-force-correlate", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_FORCE_CORRELATE
, NULL
, NULL
},
2886 { "clock-gmt", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_GMT
, NULL
, NULL
},
2887 { "clock-offset", '\0', POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET
, NULL
, NULL
},
2888 { "clock-offset-ns", '\0', POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET_NS
, NULL
, NULL
},
2889 { "clock-seconds", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_SECONDS
, NULL
, NULL
},
2890 { "color", '\0', POPT_ARG_STRING
, NULL
, OPT_COLOR
, NULL
, NULL
},
2891 { "component", 'c', POPT_ARG_STRING
, NULL
, OPT_COMPONENT
, NULL
, NULL
},
2892 { "debug", 'd', POPT_ARG_NONE
, NULL
, OPT_DEBUG
, NULL
, NULL
},
2893 { "debug-info-dir", 0, POPT_ARG_STRING
, NULL
, OPT_DEBUG_INFO_DIR
, NULL
, NULL
},
2894 { "debug-info-full-path", 0, POPT_ARG_NONE
, NULL
, OPT_DEBUG_INFO_FULL_PATH
, NULL
, NULL
},
2895 { "debug-info-target-prefix", 0, POPT_ARG_STRING
, NULL
, OPT_DEBUG_INFO_TARGET_PREFIX
, NULL
, NULL
},
2896 { "end", 'e', POPT_ARG_STRING
, NULL
, OPT_END
, NULL
, NULL
},
2897 { "fields", 'f', POPT_ARG_STRING
, NULL
, OPT_FIELDS
, NULL
, NULL
},
2898 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
2899 { "input-format", 'i', POPT_ARG_STRING
, NULL
, OPT_INPUT_FORMAT
, NULL
, NULL
},
2900 { "name", '\0', POPT_ARG_STRING
, NULL
, OPT_NAME
, NULL
, NULL
},
2901 { "names", 'n', POPT_ARG_STRING
, NULL
, OPT_NAMES
, NULL
, NULL
},
2902 { "debug-info", '\0', POPT_ARG_NONE
, NULL
, OPT_DEBUG_INFO
, NULL
, NULL
},
2903 { "no-delta", '\0', POPT_ARG_NONE
, NULL
, OPT_NO_DELTA
, NULL
, NULL
},
2904 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
2905 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
2906 { "output", 'w', POPT_ARG_STRING
, NULL
, OPT_OUTPUT
, NULL
, NULL
},
2907 { "output-format", 'o', POPT_ARG_STRING
, NULL
, OPT_OUTPUT_FORMAT
, NULL
, NULL
},
2908 { "params", 'p', POPT_ARG_STRING
, NULL
, OPT_PARAMS
, NULL
, NULL
},
2909 { "path", 'P', POPT_ARG_STRING
, NULL
, OPT_PATH
, NULL
, NULL
},
2910 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
2911 { "retry-duration", '\0', POPT_ARG_STRING
, NULL
, OPT_RETRY_DURATION
, NULL
, NULL
},
2912 { "run-args", '\0', POPT_ARG_NONE
, NULL
, OPT_RUN_ARGS
, NULL
, NULL
},
2913 { "run-args-0", '\0', POPT_ARG_NONE
, NULL
, OPT_RUN_ARGS_0
, NULL
, NULL
},
2914 { "stream-intersection", '\0', POPT_ARG_NONE
, NULL
, OPT_STREAM_INTERSECTION
, NULL
, NULL
},
2915 { "timerange", '\0', POPT_ARG_STRING
, NULL
, OPT_TIMERANGE
, NULL
, NULL
},
2916 { "url", 'u', POPT_ARG_STRING
, NULL
, OPT_URL
, NULL
, NULL
},
2917 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
2918 { NULL
, 0, '\0', NULL
, 0, NULL
, NULL
},
2922 GString
*get_component_auto_name(const char *prefix
,
2923 struct bt_value
*existing_names
)
2926 GString
*auto_name
= g_string_new(NULL
);
2933 if (!bt_value_map_has_key(existing_names
, prefix
)) {
2934 g_string_assign(auto_name
, prefix
);
2939 g_string_printf(auto_name
, "%s-%d", prefix
, i
);
2941 } while (bt_value_map_has_key(existing_names
, auto_name
->str
));
2947 struct implicit_component_args
{
2951 GString
*params_arg
;
2952 struct bt_value
*extra_params
;
2956 int assign_name_to_implicit_component(struct implicit_component_args
*args
,
2957 const char *prefix
, struct bt_value
*existing_names
,
2958 GList
**comp_names
, bool append_to_comp_names
)
2961 GString
*name
= NULL
;
2963 if (!args
->exists
) {
2967 name
= get_component_auto_name(prefix
, existing_names
);
2974 g_string_assign(args
->name_arg
, name
->str
);
2976 if (bt_value_map_insert(existing_names
, name
->str
,
2983 if (append_to_comp_names
) {
2984 *comp_names
= g_list_append(*comp_names
, name
);
2990 g_string_free(name
, TRUE
);
2997 int append_run_args_for_implicit_component(
2998 struct implicit_component_args
*impl_args
,
2999 struct bt_value
*run_args
)
3004 if (!impl_args
->exists
) {
3008 if (bt_value_array_append_string(run_args
, "--component")) {
3013 if (bt_value_array_append_string(run_args
, impl_args
->comp_arg
->str
)) {
3018 if (bt_value_array_append_string(run_args
, "--name")) {
3023 if (bt_value_array_append_string(run_args
, impl_args
->name_arg
->str
)) {
3028 if (impl_args
->params_arg
->len
> 0) {
3029 if (bt_value_array_append_string(run_args
, "--params")) {
3034 if (bt_value_array_append_string(run_args
,
3035 impl_args
->params_arg
->str
)) {
3041 for (i
= 0; i
< bt_value_array_size(impl_args
->extra_params
); i
++) {
3042 struct bt_value
*elem
;
3045 elem
= bt_value_array_get(impl_args
->extra_params
, i
);
3050 assert(bt_value_is_string(elem
));
3051 if (bt_value_string_get(elem
, &arg
)) {
3055 ret
= bt_value_array_append_string(run_args
, arg
);
3073 void finalize_implicit_component_args(struct implicit_component_args
*args
)
3077 if (args
->comp_arg
) {
3078 g_string_free(args
->comp_arg
, TRUE
);
3081 if (args
->name_arg
) {
3082 g_string_free(args
->name_arg
, TRUE
);
3085 if (args
->params_arg
) {
3086 g_string_free(args
->params_arg
, TRUE
);
3089 bt_put(args
->extra_params
);
3093 void destroy_implicit_component_args(void *args
)
3099 finalize_implicit_component_args(args
);
3104 int init_implicit_component_args(struct implicit_component_args
*args
,
3105 const char *comp_arg
, bool exists
)
3109 args
->exists
= exists
;
3110 args
->comp_arg
= g_string_new(comp_arg
);
3111 args
->name_arg
= g_string_new(NULL
);
3112 args
->params_arg
= g_string_new(NULL
);
3113 args
->extra_params
= bt_value_array_create();
3115 if (!args
->comp_arg
|| !args
->name_arg
||
3116 !args
->params_arg
|| !args
->extra_params
) {
3118 finalize_implicit_component_args(args
);
3128 void append_implicit_component_param(struct implicit_component_args
*args
,
3129 const char *key
, const char *value
)
3134 append_param_arg(args
->params_arg
, key
, value
);
3138 int append_implicit_component_extra_param(struct implicit_component_args
*args
,
3139 const char *key
, const char *value
)
3147 if (bt_value_array_append_string(args
->extra_params
, "--key")) {
3153 if (bt_value_array_append_string(args
->extra_params
, key
)) {
3159 if (bt_value_array_append_string(args
->extra_params
, "--value")) {
3165 if (bt_value_array_append_string(args
->extra_params
, value
)) {
3176 int convert_append_name_param(enum bt_config_component_dest dest
,
3177 GString
*cur_name
, GString
*cur_name_prefix
,
3178 struct bt_value
*run_args
, struct bt_value
*all_names
,
3179 GList
**source_names
, GList
**filter_names
,
3184 if (cur_name_prefix
->len
> 0) {
3185 /* We're after a --component option */
3186 GString
*name
= NULL
;
3187 bool append_name_opt
= false;
3189 if (cur_name
->len
== 0) {
3191 * No explicit name was provided for the user
3194 name
= get_component_auto_name(cur_name_prefix
->str
,
3196 append_name_opt
= true;
3199 * An explicit name was provided for the user
3202 if (bt_value_map_has_key(all_names
,
3204 printf_err("Duplicate component instance name:\n %s\n",
3209 name
= g_string_new(cur_name
->str
);
3218 * Remember this name globally, for the uniqueness of
3219 * all component names.
3221 if (bt_value_map_insert(all_names
, name
->str
, bt_value_null
)) {
3227 * Append the --name option if necessary.
3229 if (append_name_opt
) {
3230 if (bt_value_array_append_string(run_args
, "--name")) {
3235 if (bt_value_array_append_string(run_args
, name
->str
)) {
3242 * Remember this name specifically for the type of the
3243 * component. This is to create connection arguments.
3246 case BT_CONFIG_COMPONENT_DEST_SOURCE
:
3247 *source_names
= g_list_append(*source_names
, name
);
3249 case BT_CONFIG_COMPONENT_DEST_FILTER
:
3250 *filter_names
= g_list_append(*filter_names
, name
);
3252 case BT_CONFIG_COMPONENT_DEST_SINK
:
3253 *sink_names
= g_list_append(*sink_names
, name
);
3259 g_string_assign(cur_name_prefix
, "");
3272 * Escapes `.`, `:`, and `\` of `input` with `\`.
3275 GString
*escape_dot_colon(const char *input
)
3277 GString
*output
= g_string_new(NULL
);
3285 for (ch
= input
; *ch
!= '\0'; ch
++) {
3286 if (*ch
== '\\' || *ch
== '.' || *ch
== ':') {
3287 g_string_append_c(output
, '\\');
3290 g_string_append_c(output
, *ch
);
3298 * Appends a --connect option to a list of arguments. `upstream_name`
3299 * and `downstream_name` are escaped with escape_dot_colon() in this
3303 int append_connect_arg(struct bt_value
*run_args
,
3304 const char *upstream_name
, const char *downstream_name
)
3307 GString
*e_upstream_name
= escape_dot_colon(upstream_name
);
3308 GString
*e_downstream_name
= escape_dot_colon(downstream_name
);
3309 GString
*arg
= g_string_new(NULL
);
3311 if (!e_upstream_name
|| !e_downstream_name
|| !arg
) {
3317 ret
= bt_value_array_append_string(run_args
, "--connect");
3324 g_string_append(arg
, e_upstream_name
->str
);
3325 g_string_append_c(arg
, ':');
3326 g_string_append(arg
, e_downstream_name
->str
);
3327 ret
= bt_value_array_append_string(run_args
, arg
->str
);
3336 g_string_free(arg
, TRUE
);
3339 if (e_upstream_name
) {
3340 g_string_free(e_upstream_name
, TRUE
);
3343 if (e_downstream_name
) {
3344 g_string_free(e_downstream_name
, TRUE
);
3351 * Appends the run command's --connect options for the convert command.
3354 int convert_auto_connect(struct bt_value
*run_args
,
3355 GList
*source_names
, GList
*filter_names
,
3359 GList
*source_at
= source_names
;
3360 GList
*filter_at
= filter_names
;
3362 GList
*sink_at
= sink_names
;
3364 assert(source_names
);
3365 assert(filter_names
);
3368 /* Connect all sources to the first filter */
3369 for (source_at
= source_names
; source_at
!= NULL
; source_at
= g_list_next(source_at
)) {
3370 GString
*source_name
= source_at
->data
;
3371 GString
*filter_name
= filter_at
->data
;
3373 ret
= append_connect_arg(run_args
, source_name
->str
,
3380 filter_prev
= filter_at
;
3381 filter_at
= g_list_next(filter_at
);
3383 /* Connect remaining filters */
3384 for (; filter_at
!= NULL
; filter_prev
= filter_at
, filter_at
= g_list_next(filter_at
)) {
3385 GString
*filter_name
= filter_at
->data
;
3386 GString
*filter_prev_name
= filter_prev
->data
;
3388 ret
= append_connect_arg(run_args
, filter_prev_name
->str
,
3395 /* Connect last filter to all sinks */
3396 for (sink_at
= sink_names
; sink_at
!= NULL
; sink_at
= g_list_next(sink_at
)) {
3397 GString
*filter_name
= filter_prev
->data
;
3398 GString
*sink_name
= sink_at
->data
;
3400 ret
= append_connect_arg(run_args
, filter_name
->str
,
3417 int split_timerange(const char *arg
, char **begin
, char **end
)
3420 const char *ch
= arg
;
3422 GString
*g_begin
= NULL
;
3423 GString
*g_end
= NULL
;
3431 g_begin
= bt_common_string_until(ch
, "", ",", &end_pos
);
3432 if (!g_begin
|| ch
[end_pos
] != ',' || g_begin
->len
== 0) {
3438 g_end
= bt_common_string_until(ch
, "", "]", &end_pos
);
3439 if (!g_end
|| g_end
->len
== 0) {
3445 *begin
= g_begin
->str
;
3447 g_string_free(g_begin
, FALSE
);
3448 g_string_free(g_end
, FALSE
);
3458 g_string_free(g_begin
, TRUE
);
3462 g_string_free(g_end
, TRUE
);
3469 int g_list_prepend_gstring(GList
**list
, const char *string
)
3472 GString
*gs
= g_string_new(string
);
3481 *list
= g_list_prepend(*list
, gs
);
3488 struct implicit_component_args
*create_implicit_component_args(void)
3490 struct implicit_component_args
*impl_args
=
3491 g_new0(struct implicit_component_args
, 1);
3497 if (init_implicit_component_args(impl_args
, NULL
, true)) {
3498 destroy_implicit_component_args(impl_args
);
3508 int fill_implicit_ctf_inputs_args(GPtrArray
*implicit_ctf_inputs_args
,
3509 struct implicit_component_args
*base_implicit_ctf_input_args
,
3515 for (leftover
= leftovers
; leftover
!= NULL
;
3516 leftover
= g_list_next(leftover
)) {
3517 GString
*gs_leftover
= leftover
->data
;
3518 struct implicit_component_args
*impl_args
=
3519 create_implicit_component_args();
3526 impl_args
->exists
= true;
3527 g_string_assign(impl_args
->comp_arg
,
3528 base_implicit_ctf_input_args
->comp_arg
->str
);
3529 g_string_assign(impl_args
->params_arg
,
3530 base_implicit_ctf_input_args
->params_arg
->str
);
3533 * We need our own copy of the extra parameters because
3534 * this is where the unique path goes.
3536 BT_PUT(impl_args
->extra_params
);
3537 impl_args
->extra_params
=
3538 bt_value_copy(base_implicit_ctf_input_args
->extra_params
);
3539 if (!impl_args
->extra_params
) {
3541 destroy_implicit_component_args(impl_args
);
3545 /* Append unique path parameter */
3546 ret
= append_implicit_component_extra_param(impl_args
,
3547 "path", gs_leftover
->str
);
3549 destroy_implicit_component_args(impl_args
);
3553 g_ptr_array_add(implicit_ctf_inputs_args
, impl_args
);
3566 * Creates a Babeltrace config object from the arguments of a convert
3569 * *retcode is set to the appropriate exit code to use.
3572 struct bt_config
*bt_config_convert_from_args(int argc
, const char *argv
[],
3573 int *retcode
, bool force_omit_system_plugin_path
,
3574 bool force_omit_home_plugin_path
,
3575 struct bt_value
*initial_plugin_paths
, char *log_level
)
3577 poptContext pc
= NULL
;
3579 enum bt_config_component_dest cur_comp_dest
=
3580 BT_CONFIG_COMPONENT_DEST_UNKNOWN
;
3582 struct bt_config
*cfg
= NULL
;
3583 bool got_input_format_opt
= false;
3584 bool got_output_format_opt
= false;
3585 bool trimmer_has_begin
= false;
3586 bool trimmer_has_end
= false;
3587 bool stream_intersection_mode
= false;
3588 GString
*cur_name
= NULL
;
3589 GString
*cur_name_prefix
= NULL
;
3590 const char *leftover
= NULL
;
3591 bool print_run_args
= false;
3592 bool print_run_args_0
= false;
3593 bool print_ctf_metadata
= false;
3594 struct bt_value
*run_args
= NULL
;
3595 struct bt_value
*all_names
= NULL
;
3596 GList
*source_names
= NULL
;
3597 GList
*filter_names
= NULL
;
3598 GList
*sink_names
= NULL
;
3599 GList
*leftovers
= NULL
;
3600 GPtrArray
*implicit_ctf_inputs_args
= NULL
;
3601 struct implicit_component_args base_implicit_ctf_input_args
= { 0 };
3602 struct implicit_component_args implicit_ctf_output_args
= { 0 };
3603 struct implicit_component_args implicit_lttng_live_args
= { 0 };
3604 struct implicit_component_args implicit_dummy_args
= { 0 };
3605 struct implicit_component_args implicit_text_args
= { 0 };
3606 struct implicit_component_args implicit_debug_info_args
= { 0 };
3607 struct implicit_component_args implicit_muxer_args
= { 0 };
3608 struct implicit_component_args implicit_trimmer_args
= { 0 };
3609 struct bt_value
*plugin_paths
= bt_get(initial_plugin_paths
);
3610 char error_buf
[256] = { 0 };
3612 struct bt_common_lttng_live_url_parts lttng_live_url_parts
= { 0 };
3613 char *output
= NULL
;
3618 print_convert_usage(stdout
);
3623 if (init_implicit_component_args(&base_implicit_ctf_input_args
,
3624 "source.ctf.fs", false)) {
3628 if (init_implicit_component_args(&implicit_ctf_output_args
,
3629 "sink.ctf.fs", false)) {
3633 if (init_implicit_component_args(&implicit_lttng_live_args
,
3634 "source.ctf.lttng-live", false)) {
3638 if (init_implicit_component_args(&implicit_text_args
,
3639 "sink.text.pretty", false)) {
3643 if (init_implicit_component_args(&implicit_dummy_args
,
3644 "sink.utils.dummy", false)) {
3648 if (init_implicit_component_args(&implicit_debug_info_args
,
3649 "filter.lttng-utils.debug-info", false)) {
3653 if (init_implicit_component_args(&implicit_muxer_args
,
3654 "filter.utils.muxer", true)) {
3658 if (init_implicit_component_args(&implicit_trimmer_args
,
3659 "filter.utils.trimmer", false)) {
3663 implicit_ctf_inputs_args
= g_ptr_array_new_with_free_func(
3664 (GDestroyNotify
) destroy_implicit_component_args
);
3665 if (!implicit_ctf_inputs_args
) {
3670 all_names
= bt_value_map_create();
3676 run_args
= bt_value_array_create();
3682 cur_name
= g_string_new(NULL
);
3688 cur_name_prefix
= g_string_new(NULL
);
3689 if (!cur_name_prefix
) {
3694 ret
= append_env_var_plugin_paths(plugin_paths
);
3700 * First pass: collect all arguments which need to be passed
3701 * as is to the run command. This pass can also add --name
3702 * arguments if needed to automatically name unnamed component
3703 * instances. Also it does the following transformations:
3705 * --path=PATH -> --key path --value PATH
3706 * --url=URL -> --key url --value URL
3708 * Also it appends the plugin paths of --plugin-path to
3711 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
3712 convert_long_options
, 0);
3714 printf_err("Cannot get popt context\n");
3718 poptReadDefaultConfig(pc
, 0);
3720 while ((opt
= poptGetNextOpt(pc
)) > 0) {
3722 char *plugin_name
= NULL
;
3723 char *comp_cls_name
= NULL
;
3725 arg
= poptGetOptArg(pc
);
3730 enum bt_component_class_type type
;
3731 const char *type_prefix
;
3733 /* Append current component's name if needed */
3734 ret
= convert_append_name_param(cur_comp_dest
, cur_name
,
3735 cur_name_prefix
, run_args
, all_names
,
3736 &source_names
, &filter_names
, &sink_names
);
3741 /* Parse the argument */
3742 plugin_comp_cls_names(arg
, &name
, &plugin_name
,
3743 &comp_cls_name
, &type
);
3744 if (!plugin_name
|| !comp_cls_name
) {
3745 printf_err("Invalid format for --component option's argument:\n %s\n",
3751 g_string_assign(cur_name
, name
);
3753 g_string_assign(cur_name
, "");
3757 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
3758 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
3759 type_prefix
= "source";
3761 case BT_COMPONENT_CLASS_TYPE_FILTER
:
3762 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_FILTER
;
3763 type_prefix
= "filter";
3765 case BT_COMPONENT_CLASS_TYPE_SINK
:
3766 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
3767 type_prefix
= "sink";
3773 if (bt_value_array_append_string(run_args
,
3779 if (bt_value_array_append_string(run_args
, arg
)) {
3784 g_string_assign(cur_name_prefix
, "");
3785 g_string_append_printf(cur_name_prefix
, "%s.%s.%s",
3786 type_prefix
, plugin_name
, comp_cls_name
);
3789 free(comp_cls_name
);
3792 comp_cls_name
= NULL
;
3796 if (cur_name_prefix
->len
== 0) {
3797 printf_err("No current component of which to set parameters:\n %s\n",
3802 if (bt_value_array_append_string(run_args
,
3808 if (bt_value_array_append_string(run_args
, arg
)) {
3814 if (cur_name_prefix
->len
== 0) {
3815 printf_err("No current component of which to set `path` parameter:\n %s\n",
3820 if (bt_value_array_append_string(run_args
, "--key")) {
3825 if (bt_value_array_append_string(run_args
, "path")) {
3830 if (bt_value_array_append_string(run_args
, "--value")) {
3835 if (bt_value_array_append_string(run_args
, arg
)) {
3841 if (cur_name_prefix
->len
== 0) {
3842 printf_err("No current component of which to set `url` parameter:\n %s\n",
3847 if (bt_value_array_append_string(run_args
, "--key")) {
3852 if (bt_value_array_append_string(run_args
, "url")) {
3857 if (bt_value_array_append_string(run_args
, "--value")) {
3862 if (bt_value_array_append_string(run_args
, arg
)) {
3868 if (cur_name_prefix
->len
== 0) {
3869 printf_err("No current component to name:\n %s\n",
3874 if (bt_value_array_append_string(run_args
, "--name")) {
3879 if (bt_value_array_append_string(run_args
, arg
)) {
3884 g_string_assign(cur_name
, arg
);
3886 case OPT_OMIT_HOME_PLUGIN_PATH
:
3887 force_omit_home_plugin_path
= true;
3889 if (bt_value_array_append_string(run_args
,
3890 "--omit-home-plugin-path")) {
3895 case OPT_RETRY_DURATION
:
3896 if (bt_value_array_append_string(run_args
,
3897 "--retry-duration")) {
3902 if (bt_value_array_append_string(run_args
, arg
)) {
3907 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
3908 force_omit_system_plugin_path
= true;
3910 if (bt_value_array_append_string(run_args
,
3911 "--omit-system-plugin-path")) {
3916 case OPT_PLUGIN_PATH
:
3917 if (bt_config_append_plugin_paths_check_setuid_setgid(
3918 plugin_paths
, arg
)) {
3922 if (bt_value_array_append_string(run_args
,
3928 if (bt_value_array_append_string(run_args
, arg
)) {
3934 print_convert_usage(stdout
);
3939 case OPT_CLOCK_CYCLES
:
3940 case OPT_CLOCK_DATE
:
3941 case OPT_CLOCK_FORCE_CORRELATE
:
3943 case OPT_CLOCK_OFFSET
:
3944 case OPT_CLOCK_OFFSET_NS
:
3945 case OPT_CLOCK_SECONDS
:
3948 case OPT_DEBUG_INFO
:
3949 case OPT_DEBUG_INFO_DIR
:
3950 case OPT_DEBUG_INFO_FULL_PATH
:
3951 case OPT_DEBUG_INFO_TARGET_PREFIX
:
3954 case OPT_INPUT_FORMAT
:
3957 case OPT_OUTPUT_FORMAT
:
3960 case OPT_RUN_ARGS_0
:
3961 case OPT_STREAM_INTERSECTION
:
3964 /* Ignore in this pass */
3967 printf_err("Unknown command-line option specified (option code %d)\n",
3976 /* Append current component's name if needed */
3977 ret
= convert_append_name_param(cur_comp_dest
, cur_name
,
3978 cur_name_prefix
, run_args
, all_names
, &source_names
,
3979 &filter_names
, &sink_names
);
3984 /* Check for option parsing error */
3986 printf_err("While parsing command-line options, at option %s: %s\n",
3987 poptBadOption(pc
, 0), poptStrerror(opt
));
3991 poptFreeContext(pc
);
3996 * Second pass: transform the convert-specific options and
3997 * arguments into implicit component instances for the run
4000 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
4001 convert_long_options
, 0);
4003 printf_err("Cannot get popt context\n");
4007 poptReadDefaultConfig(pc
, 0);
4009 while ((opt
= poptGetNextOpt(pc
)) > 0) {
4010 arg
= poptGetOptArg(pc
);
4014 if (trimmer_has_begin
) {
4015 printf("At --begin option: --begin or --timerange option already specified\n %s\n",
4020 trimmer_has_begin
= true;
4021 ret
= append_implicit_component_extra_param(
4022 &implicit_trimmer_args
, "begin", arg
);
4023 implicit_trimmer_args
.exists
= true;
4029 if (trimmer_has_end
) {
4030 printf("At --end option: --end or --timerange option already specified\n %s\n",
4035 trimmer_has_end
= true;
4036 ret
= append_implicit_component_extra_param(
4037 &implicit_trimmer_args
, "end", arg
);
4038 implicit_trimmer_args
.exists
= true;
4048 if (trimmer_has_begin
|| trimmer_has_end
) {
4049 printf("At --timerange option: --begin, --end, or --timerange option already specified\n %s\n",
4054 ret
= split_timerange(arg
, &begin
, &end
);
4056 printf_err("Invalid --timerange option's argument: expecting BEGIN,END or [BEGIN,END]:\n %s\n",
4061 ret
= append_implicit_component_extra_param(
4062 &implicit_trimmer_args
, "begin", begin
);
4063 ret
|= append_implicit_component_extra_param(
4064 &implicit_trimmer_args
, "end", end
);
4065 implicit_trimmer_args
.exists
= true;
4073 case OPT_CLOCK_CYCLES
:
4074 append_implicit_component_param(
4075 &implicit_text_args
, "clock-cycles", "yes");
4076 implicit_text_args
.exists
= true;
4078 case OPT_CLOCK_DATE
:
4079 append_implicit_component_param(
4080 &implicit_text_args
, "clock-date", "yes");
4081 implicit_text_args
.exists
= true;
4083 case OPT_CLOCK_FORCE_CORRELATE
:
4084 append_implicit_component_param(
4085 &implicit_muxer_args
,
4086 "assume-absolute-clock-classes", "yes");
4089 append_implicit_component_param(
4090 &implicit_text_args
, "clock-gmt", "yes");
4091 append_implicit_component_param(
4092 &implicit_trimmer_args
, "clock-gmt", "yes");
4093 implicit_text_args
.exists
= true;
4095 case OPT_CLOCK_OFFSET
:
4096 base_implicit_ctf_input_args
.exists
= true;
4097 append_implicit_component_param(
4098 &base_implicit_ctf_input_args
,
4099 "clock-class-offset-s", arg
);
4101 case OPT_CLOCK_OFFSET_NS
:
4102 base_implicit_ctf_input_args
.exists
= true;
4103 append_implicit_component_param(
4104 &base_implicit_ctf_input_args
,
4105 "clock-class-offset-ns", arg
);
4107 case OPT_CLOCK_SECONDS
:
4108 append_implicit_component_param(
4109 &implicit_text_args
, "clock-seconds", "yes");
4110 implicit_text_args
.exists
= true;
4113 implicit_text_args
.exists
= true;
4114 ret
= append_implicit_component_extra_param(
4115 &implicit_text_args
, "color", arg
);
4120 case OPT_DEBUG_INFO
:
4121 implicit_debug_info_args
.exists
= true;
4123 case OPT_DEBUG_INFO_DIR
:
4124 implicit_debug_info_args
.exists
= true;
4125 ret
= append_implicit_component_extra_param(
4126 &implicit_debug_info_args
, "debug-info-dir", arg
);
4131 case OPT_DEBUG_INFO_FULL_PATH
:
4132 implicit_debug_info_args
.exists
= true;
4133 append_implicit_component_param(
4134 &implicit_debug_info_args
, "full-path", "yes");
4136 case OPT_DEBUG_INFO_TARGET_PREFIX
:
4137 implicit_debug_info_args
.exists
= true;
4138 ret
= append_implicit_component_extra_param(
4139 &implicit_debug_info_args
,
4140 "target-prefix", arg
);
4147 struct bt_value
*fields
= fields_from_arg(arg
);
4153 implicit_text_args
.exists
= true;
4154 ret
= insert_flat_params_from_array(
4155 implicit_text_args
.params_arg
,
4165 struct bt_value
*names
= names_from_arg(arg
);
4171 implicit_text_args
.exists
= true;
4172 ret
= insert_flat_params_from_array(
4173 implicit_text_args
.params_arg
,
4182 append_implicit_component_param(
4183 &implicit_text_args
, "no-delta", "yes");
4184 implicit_text_args
.exists
= true;
4186 case OPT_INPUT_FORMAT
:
4187 if (got_input_format_opt
) {
4188 printf_err("Duplicate --input-format option\n");
4192 got_input_format_opt
= true;
4194 if (strcmp(arg
, "ctf") == 0) {
4195 base_implicit_ctf_input_args
.exists
= true;
4196 } else if (strcmp(arg
, "lttng-live") == 0) {
4197 implicit_lttng_live_args
.exists
= true;
4199 printf_err("Unknown legacy input format:\n %s\n",
4204 case OPT_OUTPUT_FORMAT
:
4205 if (got_output_format_opt
) {
4206 printf_err("Duplicate --output-format option\n");
4210 got_output_format_opt
= true;
4212 if (strcmp(arg
, "text") == 0) {
4213 implicit_text_args
.exists
= true;
4214 } else if (strcmp(arg
, "ctf") == 0) {
4215 implicit_ctf_output_args
.exists
= true;
4216 } else if (strcmp(arg
, "dummy") == 0) {
4217 implicit_dummy_args
.exists
= true;
4218 } else if (strcmp(arg
, "ctf-metadata") == 0) {
4219 print_ctf_metadata
= true;
4221 printf_err("Unknown legacy output format:\n %s\n",
4228 printf_err("Duplicate --output option\n");
4232 output
= strdup(arg
);
4239 if (print_run_args_0
) {
4240 printf_err("Cannot specify --run-args and --run-args-0\n");
4244 print_run_args
= true;
4246 case OPT_RUN_ARGS_0
:
4247 if (print_run_args
) {
4248 printf_err("Cannot specify --run-args and --run-args-0\n");
4252 print_run_args_0
= true;
4254 case OPT_STREAM_INTERSECTION
:
4256 * Applies to all traces implementing the trace-info
4259 stream_intersection_mode
= true;
4262 if (*log_level
!= 'V' && *log_level
!= 'D') {
4275 /* Check for option parsing error */
4277 printf_err("While parsing command-line options, at option %s: %s\n",
4278 poptBadOption(pc
, 0), poptStrerror(opt
));
4283 * Legacy behaviour: --verbose used to make the `text` output
4284 * format print more information. --verbose is now equivalent to
4285 * the INFO log level, which is why we compare to 'I' here.
4287 if (*log_level
== 'I') {
4288 append_implicit_component_param(&implicit_text_args
,
4293 * Append home and system plugin paths now that we possibly got
4296 if (append_home_and_system_plugin_paths(plugin_paths
,
4297 force_omit_system_plugin_path
,
4298 force_omit_home_plugin_path
)) {
4302 /* Consume and keep leftover arguments */
4303 while ((leftover
= poptGetArg(pc
))) {
4304 GString
*gs_leftover
= g_string_new(leftover
);
4311 leftovers
= g_list_append(leftovers
, gs_leftover
);
4313 g_string_free(gs_leftover
, TRUE
);
4319 /* Print CTF metadata or print LTTng live sessions */
4320 if (print_ctf_metadata
) {
4321 GString
*gs_leftover
;
4323 if (g_list_length(leftovers
) == 0) {
4324 printf_err("--output-format=ctf-metadata specified without a path\n");
4328 if (g_list_length(leftovers
) > 1) {
4329 printf_err("Too many paths specified for --output-format=ctf-metadata\n");
4333 cfg
= bt_config_print_ctf_metadata_create(plugin_paths
);
4338 gs_leftover
= leftovers
->data
;
4339 g_string_assign(cfg
->cmd_data
.print_ctf_metadata
.path
,
4344 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
4352 * If -o ctf was specified, make sure an output path (--output)
4353 * was also specified. --output does not imply -o ctf because
4354 * it's also used for the default, implicit -o text if -o ctf
4357 if (implicit_ctf_output_args
.exists
) {
4359 printf_err("--output-format=ctf specified without --output (trace output path)\n");
4364 * At this point we know that -o ctf AND --output were
4365 * specified. Make sure that no options were specified
4366 * which would imply -o text because --output would be
4367 * ambiguous in this case. For example, this is wrong:
4369 * babeltrace --names=all -o ctf --output=/tmp/path my-trace
4371 * because --names=all implies -o text, and --output
4372 * could apply to both the sink.text.pretty and
4373 * sink.ctf.fs implicit components.
4375 if (implicit_text_args
.exists
) {
4376 printf_err("Ambiguous --output option: --output-format=ctf specified but another option implies --output-format=text\n");
4382 * If -o dummy and -o ctf were not specified, and if there are
4383 * no explicit sink components, then use an implicit
4384 * `sink.text.pretty` component.
4386 if (!implicit_dummy_args
.exists
&& !implicit_ctf_output_args
.exists
&&
4388 implicit_text_args
.exists
= true;
4392 * Set implicit `sink.text.pretty` or `sink.ctf.fs` component's
4393 * `path` parameter if --output was specified.
4396 if (implicit_text_args
.exists
) {
4397 append_implicit_component_extra_param(&implicit_text_args
,
4399 } else if (implicit_ctf_output_args
.exists
) {
4400 append_implicit_component_extra_param(&implicit_ctf_output_args
,
4405 /* Decide where the leftover argument(s) go */
4406 if (g_list_length(leftovers
) > 0) {
4407 if (implicit_lttng_live_args
.exists
) {
4408 GString
*gs_leftover
;
4410 if (g_list_length(leftovers
) > 1) {
4411 printf_err("Too many URLs specified for --output-format=lttng-live\n");
4415 gs_leftover
= leftovers
->data
;
4416 lttng_live_url_parts
=
4417 bt_common_parse_lttng_live_url(gs_leftover
->str
,
4418 error_buf
, sizeof(error_buf
));
4419 if (!lttng_live_url_parts
.proto
) {
4420 printf_err("Invalid LTTng live URL format: %s\n",
4425 if (!lttng_live_url_parts
.session_name
) {
4426 /* Print LTTng live sessions */
4427 cfg
= bt_config_print_lttng_live_sessions_create(
4433 g_string_assign(cfg
->cmd_data
.print_lttng_live_sessions
.url
,
4438 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
4445 ret
= append_implicit_component_extra_param(
4446 &implicit_lttng_live_args
, "url",
4453 * Append one implicit component argument set
4454 * for each leftover (souce.ctf.fs paths). Copy
4455 * the base implicit component arguments.
4456 * Note that they still have to be named later.
4458 ret
= fill_implicit_ctf_inputs_args(
4459 implicit_ctf_inputs_args
,
4460 &base_implicit_ctf_input_args
, leftovers
);
4468 * Ensure mutual exclusion between implicit `source.ctf.fs` and
4469 * `source.ctf.lttng-live` components.
4471 if (base_implicit_ctf_input_args
.exists
&&
4472 implicit_lttng_live_args
.exists
) {
4473 printf_err("Cannot create both implicit `%s` and `%s` components\n",
4474 base_implicit_ctf_input_args
.comp_arg
->str
,
4475 implicit_lttng_live_args
.comp_arg
->str
);
4480 * If the implicit `source.ctf.fs` or `source.ctf.lttng-live`
4481 * components exists, make sure there's at least one leftover
4482 * (which is the path or URL).
4484 if (base_implicit_ctf_input_args
.exists
&&
4485 g_list_length(leftovers
) == 0) {
4486 printf_err("Missing path for implicit `%s` component\n",
4487 base_implicit_ctf_input_args
.comp_arg
->str
);
4491 if (implicit_lttng_live_args
.exists
&& g_list_length(leftovers
) == 0) {
4492 printf_err("Missing URL for implicit `%s` component\n",
4493 implicit_lttng_live_args
.comp_arg
->str
);
4497 /* Assign names to implicit components */
4498 for (i
= 0; i
< implicit_ctf_inputs_args
->len
; i
++) {
4499 struct implicit_component_args
*impl_args
=
4500 g_ptr_array_index(implicit_ctf_inputs_args
, i
);
4502 ret
= assign_name_to_implicit_component(impl_args
,
4503 "source-ctf-fs", all_names
, &source_names
, true);
4509 ret
= assign_name_to_implicit_component(&implicit_lttng_live_args
,
4510 "lttng-live", all_names
, &source_names
, true);
4515 ret
= assign_name_to_implicit_component(&implicit_text_args
,
4516 "pretty", all_names
, &sink_names
, true);
4521 ret
= assign_name_to_implicit_component(&implicit_ctf_output_args
,
4522 "sink-ctf-fs", all_names
, &sink_names
, true);
4527 ret
= assign_name_to_implicit_component(&implicit_dummy_args
,
4528 "dummy", all_names
, &sink_names
, true);
4533 ret
= assign_name_to_implicit_component(&implicit_muxer_args
,
4534 "muxer", all_names
, NULL
, false);
4539 ret
= assign_name_to_implicit_component(&implicit_trimmer_args
,
4540 "trimmer", all_names
, NULL
, false);
4545 ret
= assign_name_to_implicit_component(&implicit_debug_info_args
,
4546 "debug-info", all_names
, NULL
, false);
4551 /* Make sure there's at least one source and one sink */
4552 if (!source_names
) {
4553 printf_err("No source component\n");
4558 printf_err("No sink component\n");
4563 * Prepend the muxer, the trimmer, and the debug info to the
4564 * filter chain so that we have:
4566 * sources -> muxer -> [trimmer] -> [debug info] ->
4567 * [user filters] -> sinks
4569 if (implicit_debug_info_args
.exists
) {
4570 if (g_list_prepend_gstring(&filter_names
,
4571 implicit_debug_info_args
.name_arg
->str
)) {
4576 if (implicit_trimmer_args
.exists
) {
4577 if (g_list_prepend_gstring(&filter_names
,
4578 implicit_trimmer_args
.name_arg
->str
)) {
4583 if (g_list_prepend_gstring(&filter_names
,
4584 implicit_muxer_args
.name_arg
->str
)) {
4589 * Append the equivalent run arguments for the implicit
4592 for (i
= 0; i
< implicit_ctf_inputs_args
->len
; i
++) {
4593 struct implicit_component_args
*impl_args
=
4594 g_ptr_array_index(implicit_ctf_inputs_args
, i
);
4596 ret
= append_run_args_for_implicit_component(impl_args
,
4603 ret
= append_run_args_for_implicit_component(&implicit_lttng_live_args
,
4609 ret
= append_run_args_for_implicit_component(&implicit_text_args
,
4615 ret
= append_run_args_for_implicit_component(&implicit_ctf_output_args
,
4621 ret
= append_run_args_for_implicit_component(&implicit_dummy_args
,
4627 ret
= append_run_args_for_implicit_component(&implicit_muxer_args
,
4633 ret
= append_run_args_for_implicit_component(&implicit_trimmer_args
,
4639 ret
= append_run_args_for_implicit_component(&implicit_debug_info_args
,
4645 /* Auto-connect components */
4646 ret
= convert_auto_connect(run_args
, source_names
, filter_names
,
4649 printf_err("Cannot auto-connect components\n");
4654 * We have all the run command arguments now. Depending on
4655 * --run-args, we pass this to the run command or print them
4658 if (print_run_args
|| print_run_args_0
) {
4659 if (stream_intersection_mode
) {
4660 printf_err("Cannot specify --stream-intersection with --run-args or --run-args-0\n");
4664 for (i
= 0; i
< bt_value_array_size(run_args
); i
++) {
4665 struct bt_value
*arg_value
=
4666 bt_value_array_get(run_args
, i
);
4668 GString
*quoted
= NULL
;
4669 const char *arg_to_print
;
4672 ret
= bt_value_string_get(arg_value
, &arg
);
4676 if (print_run_args
) {
4677 quoted
= bt_common_shell_quote(arg
, true);
4682 arg_to_print
= quoted
->str
;
4687 printf("%s", arg_to_print
);
4690 g_string_free(quoted
, TRUE
);
4693 if (i
< bt_value_array_size(run_args
) - 1) {
4694 if (print_run_args
) {
4707 cfg
= bt_config_run_from_args_array(run_args
, retcode
,
4708 force_omit_system_plugin_path
, force_omit_home_plugin_path
,
4709 initial_plugin_paths
);
4714 cfg
->cmd_data
.run
.stream_intersection_mode
= stream_intersection_mode
;
4723 poptFreeContext(pc
);
4730 g_string_free(cur_name
, TRUE
);
4733 if (cur_name_prefix
) {
4734 g_string_free(cur_name_prefix
, TRUE
);
4737 if (implicit_ctf_inputs_args
) {
4738 g_ptr_array_free(implicit_ctf_inputs_args
, TRUE
);
4743 destroy_glist_of_gstring(source_names
);
4744 destroy_glist_of_gstring(filter_names
);
4745 destroy_glist_of_gstring(sink_names
);
4746 destroy_glist_of_gstring(leftovers
);
4747 finalize_implicit_component_args(&base_implicit_ctf_input_args
);
4748 finalize_implicit_component_args(&implicit_ctf_output_args
);
4749 finalize_implicit_component_args(&implicit_lttng_live_args
);
4750 finalize_implicit_component_args(&implicit_dummy_args
);
4751 finalize_implicit_component_args(&implicit_text_args
);
4752 finalize_implicit_component_args(&implicit_debug_info_args
);
4753 finalize_implicit_component_args(&implicit_muxer_args
);
4754 finalize_implicit_component_args(&implicit_trimmer_args
);
4755 bt_put(plugin_paths
);
4756 bt_common_destroy_lttng_live_url_parts(<tng_live_url_parts
);
4761 * Prints the Babeltrace 2.x general usage.
4764 void print_gen_usage(FILE *fp
)
4766 fprintf(fp
, "Usage: babeltrace [GENERAL OPTIONS] [COMMAND] [COMMAND ARGUMENTS]\n");
4768 fprintf(fp
, "General options:\n");
4770 fprintf(fp
, " -d, --debug Enable debug mode (same as --log-level=V)\n");
4771 fprintf(fp
, " -h, --help Show this help and quit\n");
4772 fprintf(fp
, " --log-level=LVL Set all log levels to LVL (`N`, `V`, `D`,\n");
4773 fprintf(fp
, " `I`, `W` (default), `E`, or `F`)\n");
4774 fprintf(fp
, " -v, --verbose Enable verbose mode (same as --log-level=I)\n");
4775 fprintf(fp
, " -V, --version Show version and quit\n");
4777 fprintf(fp
, "Available commands:\n");
4779 fprintf(fp
, " convert Convert and trim traces (default)\n");
4780 fprintf(fp
, " help Get help for a plugin or a component class\n");
4781 fprintf(fp
, " list-plugins List available plugins and their content\n");
4782 fprintf(fp
, " query Query objects from a component class\n");
4783 fprintf(fp
, " run Build a processing graph and run it\n");
4785 fprintf(fp
, "Use `babeltrace COMMAND --help` to show the help of COMMAND.\n");
4789 char log_level_from_arg(const char *arg
)
4793 if (strcmp(arg
, "VERBOSE") == 0 ||
4794 strcmp(arg
, "V") == 0) {
4796 } else if (strcmp(arg
, "DEBUG") == 0 ||
4797 strcmp(arg
, "D") == 0) {
4799 } else if (strcmp(arg
, "INFO") == 0 ||
4800 strcmp(arg
, "I") == 0) {
4802 } else if (strcmp(arg
, "WARN") == 0 ||
4803 strcmp(arg
, "WARNING") == 0 ||
4804 strcmp(arg
, "W") == 0) {
4806 } else if (strcmp(arg
, "ERROR") == 0 ||
4807 strcmp(arg
, "E") == 0) {
4809 } else if (strcmp(arg
, "FATAL") == 0 ||
4810 strcmp(arg
, "F") == 0) {
4812 } else if (strcmp(arg
, "NONE") == 0 ||
4813 strcmp(arg
, "N") == 0) {
4820 struct bt_config
*bt_config_cli_args_create(int argc
, const char *argv
[],
4821 int *retcode
, bool force_omit_system_plugin_path
,
4822 bool force_omit_home_plugin_path
,
4823 struct bt_value
*initial_plugin_paths
)
4825 struct bt_config
*config
= NULL
;
4827 const char **command_argv
= NULL
;
4828 int command_argc
= -1;
4829 const char *command_name
= NULL
;
4830 char log_level
= 'U';
4833 COMMAND_TYPE_NONE
= -1,
4834 COMMAND_TYPE_RUN
= 0,
4835 COMMAND_TYPE_CONVERT
,
4836 COMMAND_TYPE_LIST_PLUGINS
,
4839 } command_type
= COMMAND_TYPE_NONE
;
4843 if (!initial_plugin_paths
) {
4844 initial_plugin_paths
= bt_value_array_create();
4845 if (!initial_plugin_paths
) {
4850 bt_get(initial_plugin_paths
);
4856 print_gen_usage(stdout
);
4860 for (i
= 1; i
< argc
; i
++) {
4861 const char *cur_arg
= argv
[i
];
4862 const char *next_arg
= i
== (argc
- 1) ? NULL
: argv
[i
+ 1];
4864 if (strcmp(cur_arg
, "-d") == 0 ||
4865 strcmp(cur_arg
, "--debug") == 0) {
4867 } else if (strcmp(cur_arg
, "-v") == 0 ||
4868 strcmp(cur_arg
, "--verbose") == 0) {
4869 if (log_level
!= 'V' && log_level
!= 'D') {
4871 * Legacy: do not override a previous
4872 * --debug because --verbose and --debug
4873 * can be specified together (in this
4874 * case we want the lowest log level to
4879 } else if (strcmp(cur_arg
, "--log-level") == 0) {
4881 printf_err("Missing log level value for --log-level option\n");
4886 log_level
= log_level_from_arg(next_arg
);
4887 if (log_level
== 'U') {
4888 printf_err("Invalid argument for --log-level option:\n %s\n",
4895 } else if (strncmp(cur_arg
, "--log-level=", 12) == 0) {
4896 const char *arg
= &cur_arg
[12];
4898 log_level
= log_level_from_arg(arg
);
4899 if (log_level
== 'U') {
4900 printf_err("Invalid argument for --log-level option:\n %s\n",
4905 } else if (strcmp(cur_arg
, "-V") == 0 ||
4906 strcmp(cur_arg
, "--version") == 0) {
4909 } else if (strcmp(cur_arg
, "-h") == 0 ||
4910 strcmp(cur_arg
, "--help") == 0) {
4911 print_gen_usage(stdout
);
4915 * First unknown argument: is it a known command
4918 command_argv
= &argv
[i
];
4919 command_argc
= argc
- i
;
4921 if (strcmp(cur_arg
, "convert") == 0) {
4922 command_type
= COMMAND_TYPE_CONVERT
;
4923 } else if (strcmp(cur_arg
, "list-plugins") == 0) {
4924 command_type
= COMMAND_TYPE_LIST_PLUGINS
;
4925 } else if (strcmp(cur_arg
, "help") == 0) {
4926 command_type
= COMMAND_TYPE_HELP
;
4927 } else if (strcmp(cur_arg
, "query") == 0) {
4928 command_type
= COMMAND_TYPE_QUERY
;
4929 } else if (strcmp(cur_arg
, "run") == 0) {
4930 command_type
= COMMAND_TYPE_RUN
;
4933 * Unknown argument, but not a known
4934 * command name: assume the default
4935 * `convert` command.
4937 command_type
= COMMAND_TYPE_CONVERT
;
4938 command_name
= "convert";
4939 command_argv
= &argv
[i
- 1];
4940 command_argc
= argc
- i
+ 1;
4946 if (command_type
== COMMAND_TYPE_NONE
) {
4948 * We only got non-help, non-version general options
4949 * like --verbose and --debug, without any other
4950 * arguments, so we can't do anything useful: print the
4953 print_gen_usage(stdout
);
4957 assert(command_argv
);
4958 assert(command_argc
>= 0);
4960 switch (command_type
) {
4961 case COMMAND_TYPE_RUN
:
4962 config
= bt_config_run_from_args(command_argc
, command_argv
,
4963 retcode
, force_omit_system_plugin_path
,
4964 force_omit_home_plugin_path
, initial_plugin_paths
);
4966 case COMMAND_TYPE_CONVERT
:
4967 config
= bt_config_convert_from_args(command_argc
, command_argv
,
4968 retcode
, force_omit_system_plugin_path
,
4969 force_omit_home_plugin_path
,
4970 initial_plugin_paths
, &log_level
);
4972 case COMMAND_TYPE_LIST_PLUGINS
:
4973 config
= bt_config_list_plugins_from_args(command_argc
,
4974 command_argv
, retcode
, force_omit_system_plugin_path
,
4975 force_omit_home_plugin_path
, initial_plugin_paths
);
4977 case COMMAND_TYPE_HELP
:
4978 config
= bt_config_help_from_args(command_argc
,
4979 command_argv
, retcode
, force_omit_system_plugin_path
,
4980 force_omit_home_plugin_path
, initial_plugin_paths
);
4982 case COMMAND_TYPE_QUERY
:
4983 config
= bt_config_query_from_args(command_argc
,
4984 command_argv
, retcode
, force_omit_system_plugin_path
,
4985 force_omit_home_plugin_path
, initial_plugin_paths
);
4992 if (log_level
== 'U') {
4996 config
->log_level
= log_level
;
4997 config
->command_name
= command_name
;
5001 bt_put(initial_plugin_paths
);