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
32 #include <babeltrace/babeltrace.h>
33 #include <babeltrace/common-internal.h>
34 #include <babeltrace/values.h>
37 #include <sys/types.h>
39 #include "babeltrace-cfg.h"
40 #include "babeltrace-cfg-connect.h"
42 #define DEFAULT_SOURCE_COMPONENT_NAME "ctf.fs"
43 #define DEFAULT_SINK_COMPONENT_NAME "text.text"
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, "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
;
135 /* Legacy input format format */
136 enum legacy_input_format
{
137 LEGACY_INPUT_FORMAT_NONE
= 0,
138 LEGACY_INPUT_FORMAT_CTF
,
139 LEGACY_INPUT_FORMAT_LTTNG_LIVE
,
142 /* Legacy output format format */
143 enum legacy_output_format
{
144 LEGACY_OUTPUT_FORMAT_NONE
= 0,
145 LEGACY_OUTPUT_FORMAT_TEXT
,
146 LEGACY_OUTPUT_FORMAT_CTF_METADATA
,
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 * Prints duplicate legacy output format error.
163 void print_err_dup_legacy_output(void)
165 printf_err("More than one legacy output format specified\n");
169 * Prints duplicate legacy input format error.
172 void print_err_dup_legacy_input(void)
174 printf_err("More than one legacy input format specified\n");
178 * Checks if any of the "text" legacy options is set.
181 bool text_legacy_opts_is_any_set(struct text_legacy_opts
*opts
)
183 return (opts
->output
&& opts
->output
->len
> 0) ||
184 (opts
->dbg_info_dir
&& opts
->dbg_info_dir
->len
> 0) ||
185 (opts
->dbg_info_target_prefix
&&
186 opts
->dbg_info_target_prefix
->len
> 0) ||
187 bt_value_array_size(opts
->names
) > 0 ||
188 bt_value_array_size(opts
->fields
) > 0 ||
189 opts
->no_delta
|| opts
->clock_cycles
|| opts
->clock_seconds
||
190 opts
->clock_date
|| opts
->clock_gmt
||
191 opts
->dbg_info_full_path
;
195 * Checks if any of the "ctf" legacy options is set.
198 bool ctf_legacy_opts_is_any_set(struct ctf_legacy_opts
*opts
)
200 return opts
->offset_s
.is_set
|| opts
->offset_ns
.is_set
||
201 opts
->stream_intersection
;
205 * Appends an "expecting token" error to the INI-style parsing state's
209 void ini_append_error_expecting(struct ini_parsing_state
*state
,
210 GScanner
*scanner
, const char *expecting
)
215 g_string_append_printf(state
->ini_error
, "Expecting %s:\n", expecting
);
217 /* Only print error if there's one line */
218 if (strchr(state
->arg
, '\n') != NULL
|| strlen(state
->arg
) == 0) {
222 g_string_append_printf(state
->ini_error
, "\n %s\n", state
->arg
);
223 pos
= g_scanner_cur_position(scanner
) + 4;
225 if (!g_scanner_eof(scanner
)) {
229 for (i
= 0; i
< pos
; ++i
) {
230 g_string_append_printf(state
->ini_error
, " ");
233 g_string_append_printf(state
->ini_error
, "^\n\n");
237 int ini_handle_state(struct ini_parsing_state
*state
)
240 GTokenType token_type
;
241 struct bt_value
*value
= NULL
;
243 token_type
= g_scanner_get_next_token(state
->scanner
);
244 if (token_type
== G_TOKEN_EOF
) {
245 if (state
->expecting
!= INI_EXPECT_COMMA
) {
246 switch (state
->expecting
) {
247 case INI_EXPECT_EQUAL
:
248 ini_append_error_expecting(state
,
249 state
->scanner
, "'='");
251 case INI_EXPECT_VALUE
:
252 case INI_EXPECT_VALUE_NUMBER_NEG
:
253 ini_append_error_expecting(state
,
254 state
->scanner
, "value");
256 case INI_EXPECT_MAP_KEY
:
257 ini_append_error_expecting(state
,
258 state
->scanner
, "unquoted map key");
271 switch (state
->expecting
) {
272 case INI_EXPECT_MAP_KEY
:
273 if (token_type
!= G_TOKEN_IDENTIFIER
) {
274 ini_append_error_expecting(state
, state
->scanner
,
279 free(state
->last_map_key
);
280 state
->last_map_key
=
281 strdup(state
->scanner
->value
.v_identifier
);
282 if (!state
->last_map_key
) {
283 g_string_append(state
->ini_error
,
288 if (bt_value_map_has_key(state
->params
, state
->last_map_key
)) {
289 g_string_append_printf(state
->ini_error
,
290 "Duplicate parameter key: `%s`\n",
291 state
->last_map_key
);
295 state
->expecting
= INI_EXPECT_EQUAL
;
297 case INI_EXPECT_EQUAL
:
298 if (token_type
!= G_TOKEN_CHAR
) {
299 ini_append_error_expecting(state
,
300 state
->scanner
, "'='");
304 if (state
->scanner
->value
.v_char
!= '=') {
305 ini_append_error_expecting(state
,
306 state
->scanner
, "'='");
310 state
->expecting
= INI_EXPECT_VALUE
;
312 case INI_EXPECT_VALUE
:
314 switch (token_type
) {
316 if (state
->scanner
->value
.v_char
== '-') {
317 /* Negative number */
319 INI_EXPECT_VALUE_NUMBER_NEG
;
322 ini_append_error_expecting(state
,
323 state
->scanner
, "value");
329 /* Positive integer */
330 uint64_t int_val
= state
->scanner
->value
.v_int64
;
332 if (int_val
> (1ULL << 63) - 1) {
333 g_string_append_printf(state
->ini_error
,
334 "Integer value %" PRIu64
" is outside the range of a 64-bit signed integer\n",
339 value
= bt_value_integer_create_init(
344 /* Positive floating point number */
345 value
= bt_value_float_create_init(
346 state
->scanner
->value
.v_float
);
350 value
= bt_value_string_create_init(
351 state
->scanner
->value
.v_string
);
353 case G_TOKEN_IDENTIFIER
:
356 * Using symbols would be appropriate here,
357 * but said symbols are allowed as map key,
358 * so it's easier to consider everything an
361 * If one of the known symbols is not
362 * recognized here, then fall back to creating
365 const char *id
= state
->scanner
->value
.v_identifier
;
367 if (!strcmp(id
, "null") || !strcmp(id
, "NULL") ||
368 !strcmp(id
, "nul")) {
369 value
= bt_value_null
;
370 } else if (!strcmp(id
, "true") || !strcmp(id
, "TRUE") ||
371 !strcmp(id
, "yes") ||
372 !strcmp(id
, "YES")) {
373 value
= bt_value_bool_create_init(true);
374 } else if (!strcmp(id
, "false") ||
375 !strcmp(id
, "FALSE") ||
378 value
= bt_value_bool_create_init(false);
380 value
= bt_value_string_create_init(id
);
385 /* Unset value variable will trigger the error */
390 ini_append_error_expecting(state
,
391 state
->scanner
, "value");
395 state
->expecting
= INI_EXPECT_COMMA
;
398 case INI_EXPECT_VALUE_NUMBER_NEG
:
400 switch (token_type
) {
403 /* Negative integer */
404 uint64_t int_val
= state
->scanner
->value
.v_int64
;
406 if (int_val
> (1ULL << 63) - 1) {
407 g_string_append_printf(state
->ini_error
,
408 "Integer value -%" PRIu64
" is outside the range of a 64-bit signed integer\n",
413 value
= bt_value_integer_create_init(
414 -((int64_t) int_val
));
418 /* Negative floating point number */
419 value
= bt_value_float_create_init(
420 -state
->scanner
->value
.v_float
);
423 /* Unset value variable will trigger the error */
428 ini_append_error_expecting(state
,
429 state
->scanner
, "value");
433 state
->expecting
= INI_EXPECT_COMMA
;
436 case INI_EXPECT_COMMA
:
437 if (token_type
!= G_TOKEN_CHAR
) {
438 ini_append_error_expecting(state
,
439 state
->scanner
, "','");
443 if (state
->scanner
->value
.v_char
!= ',') {
444 ini_append_error_expecting(state
,
445 state
->scanner
, "','");
449 state
->expecting
= INI_EXPECT_MAP_KEY
;
461 if (bt_value_map_insert(state
->params
,
462 state
->last_map_key
, value
)) {
463 /* Only override return value on error */
474 * Converts an INI-style argument to an equivalent map value object.
476 * Return value is owned by the caller.
479 struct bt_value
*bt_value_from_ini(const char *arg
, GString
*ini_error
)
481 /* Lexical scanner configuration */
482 GScannerConfig scanner_config
= {
483 /* Skip whitespaces */
484 .cset_skip_characters
= " \t\n",
486 /* Identifier syntax is: [a-zA-Z_][a-zA-Z0-9_.:-]* */
487 .cset_identifier_first
=
491 .cset_identifier_nth
=
496 /* "hello" and "Hello" two different keys */
497 .case_sensitive
= TRUE
,
500 .cpair_comment_single
= NULL
,
501 .skip_comment_multi
= TRUE
,
502 .skip_comment_single
= TRUE
,
503 .scan_comment_multi
= FALSE
,
506 * Do scan identifiers, including 1-char identifiers,
507 * but NULL is a normal identifier.
509 .scan_identifier
= TRUE
,
510 .scan_identifier_1char
= TRUE
,
511 .scan_identifier_NULL
= FALSE
,
514 * No specific symbols: null and boolean "symbols" are
515 * scanned as plain identifiers.
517 .scan_symbols
= FALSE
,
518 .symbol_2_token
= FALSE
,
519 .scope_0_fallback
= FALSE
,
522 * Scan "0b"-, "0"-, and "0x"-prefixed integers, but not
523 * integers prefixed with "$".
529 .scan_hex_dollar
= FALSE
,
531 /* Convert scanned numbers to integer tokens */
532 .numbers_2_int
= TRUE
,
534 /* Support both integers and floating-point numbers */
535 .int_2_float
= FALSE
,
537 /* Scan integers as 64-bit signed integers */
540 /* Only scan double-quoted strings */
541 .scan_string_sq
= FALSE
,
542 .scan_string_dq
= TRUE
,
544 /* Do not converter identifiers to string tokens */
545 .identifier_2_string
= FALSE
,
547 /* Scan characters as G_TOKEN_CHAR token */
548 .char_2_token
= FALSE
,
550 struct ini_parsing_state state
= {
553 .expecting
= INI_EXPECT_MAP_KEY
,
555 .ini_error
= ini_error
,
558 state
.params
= bt_value_map_create();
563 state
.scanner
= g_scanner_new(&scanner_config
);
564 if (!state
.scanner
) {
568 /* Let the scan begin */
569 g_scanner_input_text(state
.scanner
, arg
, strlen(arg
));
572 int ret
= ini_handle_state(&state
);
577 } else if (ret
> 0) {
586 BT_PUT(state
.params
);
590 g_scanner_destroy(state
.scanner
);
593 free(state
.last_map_key
);
598 * Returns the parameters map value object from a command-line
599 * parameter option's argument.
601 * Return value is owned by the caller.
604 struct bt_value
*bt_value_from_arg(const char *arg
)
606 struct bt_value
*params
= NULL
;
607 GString
*ini_error
= NULL
;
609 ini_error
= g_string_new(NULL
);
615 /* Try INI-style parsing */
616 params
= bt_value_from_ini(arg
, ini_error
);
618 printf_err("%s", ini_error
->str
);
624 g_string_free(ini_error
, TRUE
);
630 * Returns the plugin and component names from a command-line
631 * source/sink option's argument. arg must have the following format:
635 * where PLUGIN is the plugin name, and COMPONENT is the component
638 * On success, both *plugin and *component are not NULL. *plugin
639 * and *component are owned by the caller.
642 void plugin_component_names_from_arg(const char *arg
, char **plugin
,
648 size_t component_len
;
650 /* Initialize both return values to NULL: not found */
654 dot
= strchr(arg
, '.');
660 end
= arg
+ strlen(arg
);
661 plugin_len
= dot
- arg
;
662 component_len
= end
- dot
- 1;
663 if (plugin_len
== 0 || component_len
== 0) {
667 *plugin
= g_malloc0(plugin_len
+ 1);
673 g_strlcpy(*plugin
, arg
, plugin_len
+ 1);
674 *component
= g_malloc0(component_len
+ 1);
680 g_strlcpy(*component
, dot
+ 1, component_len
+ 1);
687 * Prints the Babeltrace version.
690 void print_version(void)
692 puts("Babeltrace " VERSION
);
696 * Destroys a component configuration.
699 void bt_config_component_destroy(struct bt_object
*obj
)
701 struct bt_config_component
*bt_config_component
=
702 container_of(obj
, struct bt_config_component
, base
);
708 if (bt_config_component
->plugin_name
) {
709 g_string_free(bt_config_component
->plugin_name
, TRUE
);
712 if (bt_config_component
->component_name
) {
713 g_string_free(bt_config_component
->component_name
, TRUE
);
716 if (bt_config_component
->instance_name
) {
717 g_string_free(bt_config_component
->instance_name
, TRUE
);
720 BT_PUT(bt_config_component
->params
);
721 g_free(bt_config_component
);
728 * Creates a component configuration using the given plugin name and
729 * component name. plugin_name and component_name are copied (belong to
732 * Return value is owned by the caller.
735 struct bt_config_component
*bt_config_component_create(
736 enum bt_component_class_type type
,
737 const char *plugin_name
, const char *component_name
)
739 struct bt_config_component
*cfg_component
= NULL
;
741 cfg_component
= g_new0(struct bt_config_component
, 1);
742 if (!cfg_component
) {
747 bt_object_init(cfg_component
, bt_config_component_destroy
);
748 cfg_component
->type
= type
;
749 cfg_component
->plugin_name
= g_string_new(plugin_name
);
750 if (!cfg_component
->plugin_name
) {
755 cfg_component
->component_name
= g_string_new(component_name
);
756 if (!cfg_component
->component_name
) {
761 cfg_component
->instance_name
= g_string_new(NULL
);
762 if (!cfg_component
->instance_name
) {
767 /* Start with empty parameters */
768 cfg_component
->params
= bt_value_map_create();
769 if (!cfg_component
->params
) {
777 BT_PUT(cfg_component
);
780 return cfg_component
;
784 * Creates a component configuration from a command-line source/sink
787 struct bt_config_component
*bt_config_component_from_arg(
788 enum bt_component_class_type type
, const char *arg
)
790 struct bt_config_component
*bt_config_component
= NULL
;
792 char *component_name
;
794 plugin_component_names_from_arg(arg
, &plugin_name
, &component_name
);
795 if (!plugin_name
|| !component_name
) {
796 printf_err("Cannot get plugin or component class name\n");
800 bt_config_component
= bt_config_component_create(type
, plugin_name
,
802 if (!bt_config_component
) {
809 BT_PUT(bt_config_component
);
813 g_free(component_name
);
814 return bt_config_component
;
818 * Destroys a configuration.
821 void bt_config_destroy(struct bt_object
*obj
)
823 struct bt_config
*cfg
=
824 container_of(obj
, struct bt_config
, base
);
830 switch (cfg
->command
) {
831 case BT_CONFIG_COMMAND_CONVERT
:
832 if (cfg
->cmd_data
.convert
.sources
) {
833 g_ptr_array_free(cfg
->cmd_data
.convert
.sources
, TRUE
);
836 if (cfg
->cmd_data
.convert
.filters
) {
837 g_ptr_array_free(cfg
->cmd_data
.convert
.filters
, TRUE
);
840 if (cfg
->cmd_data
.convert
.sinks
) {
841 g_ptr_array_free(cfg
->cmd_data
.convert
.sinks
, TRUE
);
844 if (cfg
->cmd_data
.convert
.connections
) {
845 g_ptr_array_free(cfg
->cmd_data
.convert
.connections
,
849 BT_PUT(cfg
->cmd_data
.convert
.plugin_paths
);
851 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
852 BT_PUT(cfg
->cmd_data
.list_plugins
.plugin_paths
);
854 case BT_CONFIG_COMMAND_HELP
:
855 BT_PUT(cfg
->cmd_data
.help
.plugin_paths
);
856 BT_PUT(cfg
->cmd_data
.help
.cfg_component
);
858 case BT_CONFIG_COMMAND_QUERY_INFO
:
859 BT_PUT(cfg
->cmd_data
.query_info
.plugin_paths
);
860 BT_PUT(cfg
->cmd_data
.query_info
.cfg_component
);
862 if (cfg
->cmd_data
.query_info
.action
) {
863 g_string_free(cfg
->cmd_data
.query_info
.action
, TRUE
);
876 static void destroy_gstring(void *data
)
878 g_string_free(data
, TRUE
);
882 * Extracts the various paths from the string arg, delimited by ':',
883 * and appends them to the array value object plugin_paths.
885 enum bt_value_status
bt_config_append_plugin_paths(
886 struct bt_value
*plugin_paths
, const char *arg
)
888 enum bt_value_status status
= BT_VALUE_STATUS_OK
;
889 GPtrArray
*dirs
= g_ptr_array_new_with_free_func(destroy_gstring
);
894 status
= BT_VALUE_STATUS_ERROR
;
898 ret
= bt_common_append_plugin_path_dirs(arg
, dirs
);
900 status
= BT_VALUE_STATUS_ERROR
;
904 for (i
= 0; i
< dirs
->len
; i
++) {
905 GString
*dir
= g_ptr_array_index(dirs
, i
);
907 bt_value_array_append_string(plugin_paths
, dir
->str
);
911 g_ptr_array_free(dirs
, TRUE
);
916 * Creates a simple lexical scanner for parsing comma-delimited names
919 * Return value is owned by the caller.
922 GScanner
*create_csv_identifiers_scanner(void)
924 GScannerConfig scanner_config
= {
925 .cset_skip_characters
= " \t\n",
926 .cset_identifier_first
= G_CSET_a_2_z G_CSET_A_2_Z
"_",
927 .cset_identifier_nth
= G_CSET_a_2_z G_CSET_A_2_Z
":_-",
928 .case_sensitive
= TRUE
,
929 .cpair_comment_single
= NULL
,
930 .skip_comment_multi
= TRUE
,
931 .skip_comment_single
= TRUE
,
932 .scan_comment_multi
= FALSE
,
933 .scan_identifier
= TRUE
,
934 .scan_identifier_1char
= TRUE
,
935 .scan_identifier_NULL
= FALSE
,
936 .scan_symbols
= FALSE
,
937 .symbol_2_token
= FALSE
,
938 .scope_0_fallback
= FALSE
,
939 .scan_binary
= FALSE
,
943 .scan_hex_dollar
= FALSE
,
944 .numbers_2_int
= FALSE
,
945 .int_2_float
= FALSE
,
946 .store_int64
= FALSE
,
947 .scan_string_sq
= FALSE
,
948 .scan_string_dq
= FALSE
,
949 .identifier_2_string
= FALSE
,
950 .char_2_token
= TRUE
,
953 return g_scanner_new(&scanner_config
);
957 * Inserts a string (if exists and not empty) or null to a map value
961 enum bt_value_status
map_insert_string_or_null(struct bt_value
*map
,
962 const char *key
, GString
*string
)
964 enum bt_value_status ret
;
966 if (string
&& string
->len
> 0) {
967 ret
= bt_value_map_insert_string(map
, key
, string
->str
);
969 ret
= bt_value_map_insert(map
, key
, bt_value_null
);
975 * Converts a comma-delimited list of known names (--names option) to
976 * an array value object containing those names as string value objects.
978 * Return value is owned by the caller.
981 struct bt_value
*names_from_arg(const char *arg
)
983 GScanner
*scanner
= NULL
;
984 struct bt_value
*names
= NULL
;
985 bool found_all
= false, found_none
= false, found_item
= false;
987 names
= bt_value_array_create();
993 scanner
= create_csv_identifiers_scanner();
999 g_scanner_input_text(scanner
, arg
, strlen(arg
));
1002 GTokenType token_type
= g_scanner_get_next_token(scanner
);
1004 switch (token_type
) {
1005 case G_TOKEN_IDENTIFIER
:
1007 const char *identifier
= scanner
->value
.v_identifier
;
1009 if (!strcmp(identifier
, "payload") ||
1010 !strcmp(identifier
, "args") ||
1011 !strcmp(identifier
, "arg")) {
1013 if (bt_value_array_append_string(names
,
1017 } else if (!strcmp(identifier
, "context") ||
1018 !strcmp(identifier
, "ctx")) {
1020 if (bt_value_array_append_string(names
,
1024 } else if (!strcmp(identifier
, "scope") ||
1025 !strcmp(identifier
, "header")) {
1027 if (bt_value_array_append_string(names
,
1031 } else if (!strcmp(identifier
, "all")) {
1033 if (bt_value_array_append_string(names
,
1037 } else if (!strcmp(identifier
, "none")) {
1039 if (bt_value_array_append_string(names
,
1044 printf_err("Unknown field name: `%s`\n",
1060 if (found_none
&& found_all
) {
1061 printf_err("Only either `all` or `none` can be specified in the list given to the --names option, but not both.\n");
1065 * Legacy behavior is to clear the defaults (show none) when at
1066 * least one item is specified.
1068 if (found_item
&& !found_none
&& !found_all
) {
1069 if (bt_value_array_append_string(names
, "none")) {
1074 g_scanner_destroy(scanner
);
1081 g_scanner_destroy(scanner
);
1088 * Converts a comma-delimited list of known fields (--fields option) to
1089 * an array value object containing those fields as string
1092 * Return value is owned by the caller.
1095 struct bt_value
*fields_from_arg(const char *arg
)
1097 GScanner
*scanner
= NULL
;
1098 struct bt_value
*fields
;
1100 fields
= bt_value_array_create();
1106 scanner
= create_csv_identifiers_scanner();
1112 g_scanner_input_text(scanner
, arg
, strlen(arg
));
1115 GTokenType token_type
= g_scanner_get_next_token(scanner
);
1117 switch (token_type
) {
1118 case G_TOKEN_IDENTIFIER
:
1120 const char *identifier
= scanner
->value
.v_identifier
;
1122 if (!strcmp(identifier
, "trace") ||
1123 !strcmp(identifier
, "trace:hostname") ||
1124 !strcmp(identifier
, "trace:domain") ||
1125 !strcmp(identifier
, "trace:procname") ||
1126 !strcmp(identifier
, "trace:vpid") ||
1127 !strcmp(identifier
, "loglevel") ||
1128 !strcmp(identifier
, "emf") ||
1129 !strcmp(identifier
, "callsite") ||
1130 !strcmp(identifier
, "all")) {
1131 if (bt_value_array_append_string(fields
,
1136 printf_err("Unknown field name: `%s`\n",
1158 g_scanner_destroy(scanner
);
1164 * Inserts the equivalent "prefix-name" true boolean value objects into
1165 * map_obj where the names are in array_obj.
1168 int insert_flat_names_fields_from_array(struct bt_value
*map_obj
,
1169 struct bt_value
*array_obj
, const char *prefix
)
1173 GString
*tmpstr
= NULL
, *default_value
= NULL
;
1176 * array_obj may be NULL if no CLI options were specified to
1177 * trigger its creation.
1183 tmpstr
= g_string_new(NULL
);
1190 default_value
= g_string_new(NULL
);
1191 if (!default_value
) {
1197 for (i
= 0; i
< bt_value_array_size(array_obj
); i
++) {
1198 struct bt_value
*str_obj
= bt_value_array_get(array_obj
, i
);
1200 bool is_default
= false;
1203 printf_err("Unexpected error\n");
1208 ret
= bt_value_string_get(str_obj
, &suffix
);
1211 printf_err("Unexpected error\n");
1215 g_string_assign(tmpstr
, prefix
);
1216 g_string_append(tmpstr
, "-");
1218 /* Special-case for "all" and "none". */
1219 if (!strcmp(suffix
, "all")) {
1221 g_string_assign(default_value
, "show");
1222 } else if (!strcmp(suffix
, "none")) {
1224 g_string_assign(default_value
, "hide");
1227 g_string_append(tmpstr
, "default");
1228 ret
= map_insert_string_or_null(map_obj
,
1236 g_string_append(tmpstr
, suffix
);
1237 ret
= bt_value_map_insert_bool(map_obj
, tmpstr
->str
,
1247 if (default_value
) {
1248 g_string_free(default_value
, TRUE
);
1251 g_string_free(tmpstr
, TRUE
);
1258 * Returns the parameters (map value object) corresponding to the
1259 * legacy text format options.
1261 * Return value is owned by the caller.
1264 struct bt_value
*params_from_text_legacy_opts(
1265 struct text_legacy_opts
*text_legacy_opts
)
1267 struct bt_value
*params
;
1269 params
= bt_value_map_create();
1275 if (map_insert_string_or_null(params
, "output-path",
1276 text_legacy_opts
->output
)) {
1281 if (map_insert_string_or_null(params
, "debug-info-dir",
1282 text_legacy_opts
->dbg_info_dir
)) {
1287 if (map_insert_string_or_null(params
, "debug-info-target-prefix",
1288 text_legacy_opts
->dbg_info_target_prefix
)) {
1293 if (bt_value_map_insert_bool(params
, "debug-info-full-path",
1294 text_legacy_opts
->dbg_info_full_path
)) {
1299 if (bt_value_map_insert_bool(params
, "no-delta",
1300 text_legacy_opts
->no_delta
)) {
1305 if (bt_value_map_insert_bool(params
, "clock-cycles",
1306 text_legacy_opts
->clock_cycles
)) {
1311 if (bt_value_map_insert_bool(params
, "clock-seconds",
1312 text_legacy_opts
->clock_seconds
)) {
1317 if (bt_value_map_insert_bool(params
, "clock-date",
1318 text_legacy_opts
->clock_date
)) {
1323 if (bt_value_map_insert_bool(params
, "clock-gmt",
1324 text_legacy_opts
->clock_gmt
)) {
1329 if (insert_flat_names_fields_from_array(params
,
1330 text_legacy_opts
->names
, "name")) {
1334 if (insert_flat_names_fields_from_array(params
,
1335 text_legacy_opts
->fields
, "field")) {
1349 int append_sinks_from_legacy_opts(GPtrArray
*sinks
,
1350 enum legacy_output_format legacy_output_format
,
1351 struct text_legacy_opts
*text_legacy_opts
)
1354 struct bt_value
*params
= NULL
;
1355 const char *plugin_name
;
1356 const char *component_name
;
1357 struct bt_config_component
*bt_config_component
= NULL
;
1359 switch (legacy_output_format
) {
1360 case LEGACY_OUTPUT_FORMAT_TEXT
:
1361 plugin_name
= "text";
1362 component_name
= "text";
1364 case LEGACY_OUTPUT_FORMAT_CTF_METADATA
:
1365 plugin_name
= "ctf";
1366 component_name
= "metadata-text";
1368 case LEGACY_OUTPUT_FORMAT_DUMMY
:
1369 plugin_name
= "utils";
1370 component_name
= "dummy";
1377 if (legacy_output_format
== LEGACY_OUTPUT_FORMAT_TEXT
) {
1378 /* Legacy "text" output format has parameters */
1379 params
= params_from_text_legacy_opts(text_legacy_opts
);
1385 * Legacy "dummy" and "ctf-metadata" output formats do
1386 * not have parameters.
1388 params
= bt_value_map_create();
1395 /* Create a component configuration */
1396 bt_config_component
= bt_config_component_create(
1397 BT_COMPONENT_CLASS_TYPE_SINK
, plugin_name
, component_name
);
1398 if (!bt_config_component
) {
1402 BT_MOVE(bt_config_component
->params
, params
);
1404 /* Move created component configuration to the array */
1405 g_ptr_array_add(sinks
, bt_config_component
);
1419 * Returns the parameters (map value object) corresponding to the
1420 * given legacy CTF format options.
1422 * Return value is owned by the caller.
1425 struct bt_value
*params_from_ctf_legacy_opts(
1426 struct ctf_legacy_opts
*ctf_legacy_opts
)
1428 struct bt_value
*params
;
1430 params
= bt_value_map_create();
1436 if (bt_value_map_insert_integer(params
, "offset-s",
1437 ctf_legacy_opts
->offset_s
.value
)) {
1442 if (bt_value_map_insert_integer(params
, "offset-ns",
1443 ctf_legacy_opts
->offset_ns
.value
)) {
1448 if (bt_value_map_insert_bool(params
, "stream-intersection",
1449 ctf_legacy_opts
->stream_intersection
)) {
1464 int append_sources_from_legacy_opts(GPtrArray
*sources
,
1465 enum legacy_input_format legacy_input_format
,
1466 struct ctf_legacy_opts
*ctf_legacy_opts
,
1467 struct bt_value
*legacy_input_paths
)
1471 struct bt_value
*base_params
;
1472 struct bt_value
*params
= NULL
;
1473 struct bt_value
*input_path
= NULL
;
1474 struct bt_value
*input_path_copy
= NULL
;
1475 const char *input_key
;
1476 const char *component_name
;
1478 switch (legacy_input_format
) {
1479 case LEGACY_INPUT_FORMAT_CTF
:
1481 component_name
= "fs";
1483 case LEGACY_INPUT_FORMAT_LTTNG_LIVE
:
1485 component_name
= "lttng-live";
1492 base_params
= params_from_ctf_legacy_opts(ctf_legacy_opts
);
1497 for (i
= 0; i
< bt_value_array_size(legacy_input_paths
); i
++) {
1498 struct bt_config_component
*bt_config_component
= NULL
;
1500 /* Copy base parameters as current parameters */
1501 params
= bt_value_copy(base_params
);
1506 /* Get current input path string value object */
1507 input_path
= bt_value_array_get(legacy_input_paths
, i
);
1512 /* Copy current input path value object */
1513 input_path_copy
= bt_value_copy(input_path
);
1514 if (!input_path_copy
) {
1518 /* Insert input path value object into current parameters */
1519 ret
= bt_value_map_insert(params
, input_key
, input_path_copy
);
1524 /* Create a component configuration */
1525 bt_config_component
= bt_config_component_create(
1526 BT_COMPONENT_CLASS_TYPE_SOURCE
, "ctf", component_name
);
1527 if (!bt_config_component
) {
1531 BT_MOVE(bt_config_component
->params
, params
);
1533 /* Move created component configuration to the array */
1534 g_ptr_array_add(sources
, bt_config_component
);
1536 /* Put current stuff */
1538 BT_PUT(input_path_copy
);
1547 BT_PUT(base_params
);
1550 BT_PUT(input_path_copy
);
1555 * Escapes a string for the shell. The string is escaped knowing that
1556 * it's a parameter string value (double-quoted), and that it will be
1557 * entered between single quotes in the shell.
1559 * Return value is owned by the caller.
1562 char *str_shell_escape(const char *input
)
1565 const char *at
= input
;
1566 GString
*str
= g_string_new(NULL
);
1572 while (*at
!= '\0') {
1575 g_string_append(str
, "\\\\");
1578 g_string_append(str
, "\\\"");
1581 g_string_append(str
, "'\"'\"'");
1584 g_string_append(str
, "\\n");
1587 g_string_append(str
, "\\t");
1590 g_string_append_c(str
, *at
);
1600 g_string_free(str
, FALSE
);
1607 int append_prefixed_flag_params(GString
*str
, struct bt_value
*flags
,
1617 for (i
= 0; i
< bt_value_array_size(flags
); i
++) {
1618 struct bt_value
*value
= bt_value_array_get(flags
, i
);
1626 if (bt_value_string_get(value
, &flag
)) {
1632 g_string_append_printf(str
, ",%s-%s=true", prefix
, flag
);
1641 * Appends a boolean parameter string.
1644 void g_string_append_bool_param(GString
*str
, const char *name
, bool value
)
1646 g_string_append_printf(str
, ",%s=%s", name
, value
? "true" : "false");
1650 * Appends a path parameter string, or null if it's empty.
1653 int g_string_append_string_path_param(GString
*str
, const char *name
,
1658 if (path
->len
> 0) {
1659 char *escaped_path
= str_shell_escape(path
->str
);
1661 if (!escaped_path
) {
1666 g_string_append_printf(str
, "%s=\"%s\"", name
, escaped_path
);
1669 g_string_append_printf(str
, "%s=null", name
);
1682 * Prints the non-legacy sink options equivalent to the specified
1683 * legacy output format options.
1686 void print_output_legacy_to_sinks(
1687 enum legacy_output_format legacy_output_format
,
1688 struct text_legacy_opts
*text_legacy_opts
)
1690 const char *output_format
;
1691 GString
*str
= NULL
;
1693 str
= g_string_new(" ");
1699 switch (legacy_output_format
) {
1700 case LEGACY_OUTPUT_FORMAT_TEXT
:
1701 output_format
= "text";
1703 case LEGACY_OUTPUT_FORMAT_CTF_METADATA
:
1704 output_format
= "ctf-metadata";
1706 case LEGACY_OUTPUT_FORMAT_DUMMY
:
1707 output_format
= "dummy";
1713 printf_err("Both `%s` legacy output format and non-legacy sink component\ninstances(s) specified.\n\n",
1715 printf_err("Specify the following non-legacy sink component instance instead of the\nlegacy `%s` output format options:\n\n",
1717 g_string_append(str
, "-o ");
1719 switch (legacy_output_format
) {
1720 case LEGACY_OUTPUT_FORMAT_TEXT
:
1721 g_string_append(str
, "text.text");
1723 case LEGACY_OUTPUT_FORMAT_CTF_METADATA
:
1724 g_string_append(str
, "ctf.metadata-text");
1726 case LEGACY_OUTPUT_FORMAT_DUMMY
:
1727 g_string_append(str
, "utils.dummy");
1733 if (legacy_output_format
== LEGACY_OUTPUT_FORMAT_TEXT
&&
1734 text_legacy_opts_is_any_set(text_legacy_opts
)) {
1737 g_string_append(str
, " -p '");
1739 if (g_string_append_string_path_param(str
, "output-path",
1740 text_legacy_opts
->output
)) {
1744 g_string_append(str
, ",");
1746 if (g_string_append_string_path_param(str
, "debug-info-dir",
1747 text_legacy_opts
->dbg_info_dir
)) {
1751 g_string_append(str
, ",");
1753 if (g_string_append_string_path_param(str
,
1754 "debug-info-target-prefix",
1755 text_legacy_opts
->dbg_info_target_prefix
)) {
1759 g_string_append_bool_param(str
, "no-delta",
1760 text_legacy_opts
->no_delta
);
1761 g_string_append_bool_param(str
, "clock-cycles",
1762 text_legacy_opts
->clock_cycles
);
1763 g_string_append_bool_param(str
, "clock-seconds",
1764 text_legacy_opts
->clock_seconds
);
1765 g_string_append_bool_param(str
, "clock-date",
1766 text_legacy_opts
->clock_date
);
1767 g_string_append_bool_param(str
, "clock-gmt",
1768 text_legacy_opts
->clock_gmt
);
1769 ret
= append_prefixed_flag_params(str
, text_legacy_opts
->names
,
1775 ret
= append_prefixed_flag_params(str
, text_legacy_opts
->fields
,
1781 /* Remove last comma and close single quote */
1782 g_string_append(str
, "'");
1785 printf_err("%s\n\n", str
->str
);
1789 g_string_free(str
, TRUE
);
1795 * Prints the non-legacy source options equivalent to the specified
1796 * legacy input format options.
1799 void print_input_legacy_to_sources(enum legacy_input_format legacy_input_format
,
1800 struct bt_value
*legacy_input_paths
,
1801 struct ctf_legacy_opts
*ctf_legacy_opts
)
1803 const char *input_format
;
1804 GString
*str
= NULL
;
1807 str
= g_string_new(" ");
1813 switch (legacy_input_format
) {
1814 case LEGACY_INPUT_FORMAT_CTF
:
1815 input_format
= "ctf";
1817 case LEGACY_INPUT_FORMAT_LTTNG_LIVE
:
1818 input_format
= "lttng-live";
1824 printf_err("Both `%s` legacy input format and non-legacy source component\ninstance(s) specified.\n\n",
1826 printf_err("Specify the following non-legacy source component instance(s) instead of the\nlegacy `%s` input format options and positional arguments:\n\n",
1829 for (i
= 0; i
< bt_value_array_size(legacy_input_paths
); i
++) {
1830 struct bt_value
*input_value
=
1831 bt_value_array_get(legacy_input_paths
, i
);
1832 const char *input
= NULL
;
1833 char *escaped_input
;
1836 assert(input_value
);
1837 ret
= bt_value_string_get(input_value
, &input
);
1838 BT_PUT(input_value
);
1839 assert(!ret
&& input
);
1840 escaped_input
= str_shell_escape(input
);
1841 if (!escaped_input
) {
1846 g_string_append(str
, "-i ctf.");
1848 switch (legacy_input_format
) {
1849 case LEGACY_INPUT_FORMAT_CTF
:
1850 g_string_append(str
, "fs -p 'path=\"");
1852 case LEGACY_INPUT_FORMAT_LTTNG_LIVE
:
1853 g_string_append(str
, "lttng-live -p 'url=\"");
1859 g_string_append(str
, escaped_input
);
1860 g_string_append(str
, "\"");
1861 g_string_append_printf(str
, ",offset-s=%" PRId64
,
1862 ctf_legacy_opts
->offset_s
.value
);
1863 g_string_append_printf(str
, ",offset-ns=%" PRId64
,
1864 ctf_legacy_opts
->offset_ns
.value
);
1865 g_string_append_bool_param(str
, "stream-intersection",
1866 ctf_legacy_opts
->stream_intersection
);
1867 g_string_append(str
, "' ");
1868 g_free(escaped_input
);
1871 printf_err("%s\n\n", str
->str
);
1875 g_string_free(str
, TRUE
);
1881 * Validates a given configuration, with optional legacy input and
1882 * output formats options. Prints useful error messages if anything
1885 * Returns true when the configuration is valid.
1888 bool validate_cfg(struct bt_config
*cfg
,
1889 enum legacy_input_format
*legacy_input_format
,
1890 enum legacy_output_format
*legacy_output_format
,
1891 struct bt_value
*legacy_input_paths
,
1892 struct ctf_legacy_opts
*ctf_legacy_opts
,
1893 struct text_legacy_opts
*text_legacy_opts
)
1895 bool legacy_input
= false;
1896 bool legacy_output
= false;
1898 /* Determine if the input and output should be legacy-style */
1899 if (*legacy_input_format
!= LEGACY_INPUT_FORMAT_NONE
||
1900 !bt_value_array_is_empty(legacy_input_paths
) ||
1901 ctf_legacy_opts_is_any_set(ctf_legacy_opts
)) {
1902 legacy_input
= true;
1905 if (*legacy_output_format
!= LEGACY_OUTPUT_FORMAT_NONE
||
1906 text_legacy_opts_is_any_set(text_legacy_opts
)) {
1907 legacy_output
= true;
1911 /* If no legacy input format was specified, default to CTF */
1912 if (*legacy_input_format
== LEGACY_INPUT_FORMAT_NONE
) {
1913 *legacy_input_format
= LEGACY_INPUT_FORMAT_CTF
;
1916 /* Make sure at least one input path exists */
1917 if (bt_value_array_is_empty(legacy_input_paths
)) {
1918 switch (*legacy_input_format
) {
1919 case LEGACY_INPUT_FORMAT_CTF
:
1920 printf_err("No input path specified for legacy `ctf` input format\n");
1922 case LEGACY_INPUT_FORMAT_LTTNG_LIVE
:
1923 printf_err("No URL specified for legacy `lttng-live` input format\n");
1931 /* Make sure no non-legacy sources are specified */
1932 if (cfg
->cmd_data
.convert
.sources
->len
!= 0) {
1933 print_input_legacy_to_sources(*legacy_input_format
,
1934 legacy_input_paths
, ctf_legacy_opts
);
1939 if (legacy_output
) {
1941 * If no legacy output format was specified, default to
1944 if (*legacy_output_format
== LEGACY_OUTPUT_FORMAT_NONE
) {
1945 *legacy_output_format
= LEGACY_OUTPUT_FORMAT_TEXT
;
1949 * If any "text" option was specified, the output must be
1952 if (text_legacy_opts_is_any_set(text_legacy_opts
) &&
1953 *legacy_output_format
!=
1954 LEGACY_OUTPUT_FORMAT_TEXT
) {
1955 printf_err("Options for legacy `text` output format specified with a different legacy output format\n");
1959 /* Make sure no non-legacy sinks are specified */
1960 if (cfg
->cmd_data
.convert
.sinks
->len
!= 0) {
1961 print_output_legacy_to_sinks(*legacy_output_format
,
1968 * If the output is the legacy "ctf-metadata" format, then the
1969 * input should be the legacy "ctf" input format.
1971 if (*legacy_output_format
== LEGACY_OUTPUT_FORMAT_CTF_METADATA
&&
1972 *legacy_input_format
!= LEGACY_INPUT_FORMAT_CTF
) {
1973 printf_err("Legacy `ctf-metadata` output format requires using legacy `ctf` input format\n");
1984 * Parses a 64-bit signed integer.
1986 * Returns a negative value if anything goes wrong.
1989 int parse_int64(const char *arg
, int64_t *val
)
1994 *val
= strtoll(arg
, &endptr
, 0);
1995 if (*endptr
!= '\0' || arg
== endptr
|| errno
!= 0) {
2009 OPT_CLOCK_FORCE_CORRELATE
,
2012 OPT_CLOCK_OFFSET_NS
,
2017 OPT_DEBUG_INFO_FULL_PATH
,
2018 OPT_DEBUG_INFO_TARGET_PREFIX
,
2028 OPT_OMIT_HOME_PLUGIN_PATH
,
2029 OPT_OMIT_SYSTEM_PLUGIN_PATH
,
2035 OPT_RESET_BASE_PARAMS
,
2038 OPT_STREAM_INTERSECTION
,
2044 * Sets the value of a given legacy offset option and marks it as set.
2046 static void set_offset_value(struct offset_opt
*offset_opt
, int64_t value
)
2048 offset_opt
->value
= value
;
2049 offset_opt
->is_set
= true;
2052 enum bt_config_component_dest
{
2053 BT_CONFIG_COMPONENT_DEST_SOURCE
,
2054 BT_CONFIG_COMPONENT_DEST_SINK
,
2058 * Adds a configuration component to the appropriate configuration
2059 * array depending on the destination.
2061 static void add_cfg_comp(struct bt_config
*cfg
,
2062 struct bt_config_component
*cfg_comp
,
2063 enum bt_config_component_dest dest
)
2065 if (dest
== BT_CONFIG_COMPONENT_DEST_SOURCE
) {
2066 g_ptr_array_add(cfg
->cmd_data
.convert
.sources
, cfg_comp
);
2068 g_ptr_array_add(cfg
->cmd_data
.convert
.sinks
, cfg_comp
);
2072 static int split_timerange(const char *arg
, const char **begin
, const char **end
)
2076 /* Try to match [begin,end] */
2077 c
= strchr(arg
, '[');
2091 /* Try to match begin,end */
2105 static int append_env_var_plugin_paths(struct bt_value
*plugin_paths
)
2110 if (bt_common_is_setuid_setgid()) {
2111 printf_debug("Skipping non-system plugin paths for setuid/setgid binary\n");
2115 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
2120 ret
= bt_config_append_plugin_paths(plugin_paths
, envvar
);
2126 static int append_home_and_system_plugin_paths(struct bt_value
*plugin_paths
,
2127 bool omit_system_plugin_path
, bool omit_home_plugin_path
)
2131 if (!omit_home_plugin_path
) {
2132 if (bt_common_is_setuid_setgid()) {
2133 printf_debug("Skipping non-system plugin paths for setuid/setgid binary\n");
2135 char *home_plugin_dir
=
2136 bt_common_get_home_plugin_path();
2138 if (home_plugin_dir
) {
2139 ret
= bt_config_append_plugin_paths(
2142 free(home_plugin_dir
);
2145 printf_err("Invalid home plugin path\n");
2152 if (!omit_system_plugin_path
) {
2153 if (bt_config_append_plugin_paths(plugin_paths
,
2154 bt_common_get_system_plugin_path())) {
2155 printf_err("Invalid system plugin path\n");
2164 static int append_sources_from_implicit_params(GPtrArray
*sources
,
2165 struct bt_config_component
*implicit_source_comp
)
2168 size_t len
= sources
->len
;
2170 for (i
= 0; i
< len
; i
++) {
2171 struct bt_config_component
*comp
;
2172 struct bt_value
*params_to_set
;
2174 comp
= g_ptr_array_index(sources
, i
);
2175 params_to_set
= bt_value_map_extend(comp
->params
,
2176 implicit_source_comp
->params
);
2177 if (!params_to_set
) {
2178 printf_err("Cannot extend legacy component parameters with non-legacy parameters\n");
2181 BT_MOVE(comp
->params
, params_to_set
);
2188 static struct bt_config
*bt_config_base_create(enum bt_config_command command
)
2190 struct bt_config
*cfg
;
2193 cfg
= g_new0(struct bt_config
, 1);
2199 bt_object_init(cfg
, bt_config_destroy
);
2200 cfg
->command
= command
;
2210 static struct bt_config
*bt_config_convert_create(
2211 struct bt_value
*initial_plugin_paths
)
2213 struct bt_config
*cfg
;
2216 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_CONVERT
);
2222 cfg
->cmd_data
.convert
.sources
= g_ptr_array_new_with_free_func(
2223 (GDestroyNotify
) bt_put
);
2224 if (!cfg
->cmd_data
.convert
.sources
) {
2229 cfg
->cmd_data
.convert
.filters
= g_ptr_array_new_with_free_func(
2230 (GDestroyNotify
) bt_put
);
2231 if (!cfg
->cmd_data
.convert
.filters
) {
2236 cfg
->cmd_data
.convert
.sinks
= g_ptr_array_new_with_free_func(
2237 (GDestroyNotify
) bt_put
);
2238 if (!cfg
->cmd_data
.convert
.sinks
) {
2243 cfg
->cmd_data
.convert
.connections
= g_ptr_array_new_with_free_func(
2244 (GDestroyNotify
) bt_config_connection_destroy
);
2245 if (!cfg
->cmd_data
.convert
.connections
) {
2250 if (initial_plugin_paths
) {
2251 cfg
->cmd_data
.convert
.plugin_paths
=
2252 bt_get(initial_plugin_paths
);
2254 cfg
->cmd_data
.convert
.plugin_paths
= bt_value_array_create();
2255 if (!cfg
->cmd_data
.convert
.plugin_paths
) {
2270 static struct bt_config
*bt_config_list_plugins_create(
2271 struct bt_value
*initial_plugin_paths
)
2273 struct bt_config
*cfg
;
2276 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_LIST_PLUGINS
);
2282 if (initial_plugin_paths
) {
2283 cfg
->cmd_data
.list_plugins
.plugin_paths
=
2284 bt_get(initial_plugin_paths
);
2286 cfg
->cmd_data
.list_plugins
.plugin_paths
=
2287 bt_value_array_create();
2288 if (!cfg
->cmd_data
.list_plugins
.plugin_paths
) {
2303 static struct bt_config
*bt_config_help_create(
2304 struct bt_value
*initial_plugin_paths
)
2306 struct bt_config
*cfg
;
2309 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_HELP
);
2315 if (initial_plugin_paths
) {
2316 cfg
->cmd_data
.help
.plugin_paths
=
2317 bt_get(initial_plugin_paths
);
2319 cfg
->cmd_data
.help
.plugin_paths
=
2320 bt_value_array_create();
2321 if (!cfg
->cmd_data
.help
.plugin_paths
) {
2327 cfg
->cmd_data
.help
.cfg_component
=
2328 bt_config_component_create(BT_COMPONENT_CLASS_TYPE_UNKNOWN
,
2330 if (!cfg
->cmd_data
.help
.cfg_component
) {
2344 static struct bt_config
*bt_config_query_info_create(
2345 struct bt_value
*initial_plugin_paths
)
2347 struct bt_config
*cfg
;
2350 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_QUERY_INFO
);
2356 if (initial_plugin_paths
) {
2357 cfg
->cmd_data
.query_info
.plugin_paths
=
2358 bt_get(initial_plugin_paths
);
2360 cfg
->cmd_data
.query_info
.plugin_paths
=
2361 bt_value_array_create();
2362 if (!cfg
->cmd_data
.query_info
.plugin_paths
) {
2368 cfg
->cmd_data
.query_info
.action
= g_string_new(NULL
);
2369 if (!cfg
->cmd_data
.query_info
.action
) {
2384 * Prints the expected format for a --params option.
2387 void print_expected_params_format(FILE *fp
)
2389 fprintf(fp
, "Expected format of PARAMS\n");
2390 fprintf(fp
, "-------------------------\n");
2392 fprintf(fp
, " PARAM=VALUE[,PARAM=VALUE]...\n");
2394 fprintf(fp
, "The parameter string is a comma-separated list of PARAM=VALUE assignments,\n");
2395 fprintf(fp
, "where PARAM is the parameter name (C identifier plus [:.-] characters), and\n");
2396 fprintf(fp
, "VALUE can be one of:\n");
2398 fprintf(fp
, "* `null`, `nul`, `NULL`: null value (no backticks).\n");
2399 fprintf(fp
, "* `true`, `TRUE`, `yes`, `YES`: true boolean value (no backticks).\n");
2400 fprintf(fp
, "* `false`, `FALSE`, `no`, `NO`: false boolean value (no backticks).\n");
2401 fprintf(fp
, "* Binary (`0b` prefix), octal (`0` prefix), decimal, or hexadecimal\n");
2402 fprintf(fp
, " (`0x` prefix) signed 64-bit integer.\n");
2403 fprintf(fp
, "* Double precision floating point number (scientific notation is accepted).\n");
2404 fprintf(fp
, "* Unquoted string with no special characters, and not matching any of\n");
2405 fprintf(fp
, " the null and boolean value symbols above.\n");
2406 fprintf(fp
, "* Double-quoted string (accepts escape characters).\n");
2408 fprintf(fp
, "Whitespaces are allowed around individual `=` and `,` tokens.\n");
2410 fprintf(fp
, "Example:\n");
2412 fprintf(fp
, " many=null, fresh=yes, condition=false, squirrel=-782329,\n");
2413 fprintf(fp
, " observe=3.14, simple=beef, needs-quotes=\"some string\",\n");
2414 fprintf(fp
, " escape.chars-are:allowed=\"this is a \\\" double quote\"\n");
2416 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run babeltrace\n");
2417 fprintf(fp
, "from a shell.\n");
2422 * Prints the help command usage.
2425 void print_help_usage(FILE *fp
)
2427 fprintf(fp
, "Usage: babeltrace [GENERAL OPTIONS] help [OPTIONS] PLUGIN\n");
2428 fprintf(fp
, " babeltrace [GENERAL OPTIONS] help [OPTIONS] --source=PLUGIN.COMPCLS\n");
2429 fprintf(fp
, " babeltrace [GENERAL OPTIONS] help [OPTIONS] --filter=PLUGIN.COMPCLS\n");
2430 fprintf(fp
, " babeltrace [GENERAL OPTIONS] help [OPTIONS] --sink=PLUGIN.COMPCLS\n");
2432 fprintf(fp
, "Options:\n");
2434 fprintf(fp
, " --filter=PLUGIN.COMPCLS Get help for the filter component class\n");
2435 fprintf(fp
, " COMPCLS found in the plugin PLUGIN\n");
2436 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2437 fprintf(fp
, " (~/.local/lib/babeltrace/plugins)\n");
2438 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2439 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2440 fprintf(fp
, " dynamic plugins can be loaded\n");
2441 fprintf(fp
, " --sink=PLUGIN.COMPCLS Get help for the sink component class\n");
2442 fprintf(fp
, " COMPCLS found in the plugin PLUGIN\n");
2443 fprintf(fp
, " --source=PLUGIN.COMPCLS Get help for the source component class\n");
2444 fprintf(fp
, " COMPCLS found in the plugin PLUGIN\n");
2445 fprintf(fp
, " -h --help Show this help and quit\n");
2447 fprintf(fp
, "See `babeltrace --help` for the list of general options.\n");
2449 fprintf(fp
, "Use `babeltrace list-plugins` to show the list of available plugins.\n");
2452 static struct poptOption help_long_options
[] = {
2453 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
2454 { "filter", '\0', POPT_ARG_STRING
, NULL
, OPT_FILTER
, NULL
, NULL
},
2455 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
2456 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
2457 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
2458 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
2459 { "sink", '\0', POPT_ARG_STRING
, NULL
, OPT_SINK
, NULL
, NULL
},
2460 { "source", '\0', POPT_ARG_STRING
, NULL
, OPT_SOURCE
, NULL
, NULL
},
2461 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
2465 * Creates a Babeltrace config object from the arguments of a help
2468 * *retcode is set to the appropriate exit code to use.
2470 struct bt_config
*bt_config_help_from_args(int argc
, const char *argv
[],
2471 int *retcode
, bool omit_system_plugin_path
,
2472 bool omit_home_plugin_path
,
2473 struct bt_value
*initial_plugin_paths
)
2475 poptContext pc
= NULL
;
2479 struct bt_config
*cfg
= NULL
;
2480 const char *leftover
;
2481 char *plugin_name
= NULL
, *component_name
= NULL
;
2482 char *plugin_comp_cls_names
= NULL
;
2485 cfg
= bt_config_help_create(initial_plugin_paths
);
2491 cfg
->cmd_data
.help
.omit_system_plugin_path
= omit_system_plugin_path
;
2492 cfg
->cmd_data
.help
.omit_home_plugin_path
= omit_home_plugin_path
;
2493 ret
= append_env_var_plugin_paths(cfg
->cmd_data
.help
.plugin_paths
);
2495 printf_err("Cannot append plugin paths from BABELTRACE_PLUGIN_PATH\n");
2500 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
2501 help_long_options
, 0);
2503 printf_err("Cannot get popt context\n");
2507 poptReadDefaultConfig(pc
, 0);
2509 while ((opt
= poptGetNextOpt(pc
)) > 0) {
2510 arg
= poptGetOptArg(pc
);
2513 case OPT_PLUGIN_PATH
:
2514 if (bt_common_is_setuid_setgid()) {
2515 printf_debug("Skipping non-system plugin paths for setuid/setgid binary\n");
2517 if (bt_config_append_plugin_paths(
2518 cfg
->cmd_data
.help
.plugin_paths
,
2520 printf_err("Invalid --plugin-path option's argument:\n %s\n",
2526 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
2527 cfg
->cmd_data
.help
.omit_system_plugin_path
= true;
2529 case OPT_OMIT_HOME_PLUGIN_PATH
:
2530 cfg
->cmd_data
.help
.omit_home_plugin_path
= true;
2535 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
2536 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
2537 printf_err("Cannot specify more than one plugin and component class:\n %s\n",
2544 cfg
->cmd_data
.help
.cfg_component
->type
=
2545 BT_COMPONENT_CLASS_TYPE_SOURCE
;
2548 cfg
->cmd_data
.help
.cfg_component
->type
=
2549 BT_COMPONENT_CLASS_TYPE_FILTER
;
2552 cfg
->cmd_data
.help
.cfg_component
->type
=
2553 BT_COMPONENT_CLASS_TYPE_SINK
;
2558 plugin_comp_cls_names
= strdup(arg
);
2559 if (!plugin_comp_cls_names
) {
2565 print_help_usage(stdout
);
2570 printf_err("Unknown command-line option specified (option code %d)\n",
2579 /* Check for option parsing error */
2581 printf_err("While parsing command-line options, at option %s: %s\n",
2582 poptBadOption(pc
, 0), poptStrerror(opt
));
2586 leftover
= poptGetArg(pc
);
2588 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
2589 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
2590 printf_err("Cannot specify plugin name and --source/--filter/--sink component class:\n %s\n",
2595 g_string_assign(cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
2598 if (cfg
->cmd_data
.help
.cfg_component
->type
==
2599 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
2600 print_help_usage(stdout
);
2606 plugin_component_names_from_arg(plugin_comp_cls_names
,
2607 &plugin_name
, &component_name
);
2608 if (plugin_name
&& component_name
) {
2609 g_string_assign(cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
2611 g_string_assign(cfg
->cmd_data
.help
.cfg_component
->component_name
,
2614 printf_err("Invalid --source/--filter/--sink option's argument:\n %s\n",
2615 plugin_comp_cls_names
);
2620 if (append_home_and_system_plugin_paths(
2621 cfg
->cmd_data
.help
.plugin_paths
,
2622 cfg
->cmd_data
.help
.omit_system_plugin_path
,
2623 cfg
->cmd_data
.help
.omit_home_plugin_path
)) {
2624 printf_err("Cannot append home and system plugin paths\n");
2635 free(plugin_comp_cls_names
);
2636 g_free(plugin_name
);
2637 g_free(component_name
);
2640 poptFreeContext(pc
);
2648 * Prints the help command usage.
2651 void print_query_info_usage(FILE *fp
)
2653 fprintf(fp
, "Usage: babeltrace [GEN OPTS] query-info [OPTS] ACTION --source=PLUGIN.COMPCLS\n");
2654 fprintf(fp
, " babeltrace [GEN OPTS] query-info [OPTS] ACTION --filter=PLUGIN.COMPCLS\n");
2655 fprintf(fp
, " babeltrace [GEN OPTS] query-info [OPTS] ACTION --sink=PLUGIN.COMPCLS\n");
2657 fprintf(fp
, "Options:\n");
2659 fprintf(fp
, " --filter=PLUGIN.COMPCLS Query info from the filter component class\n");
2660 fprintf(fp
, " COMPCLS found in the plugin PLUGIN\n");
2661 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2662 fprintf(fp
, " (~/.local/lib/babeltrace/plugins)\n");
2663 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2664 fprintf(fp
, " -p, --params=PARAMS Set the query parameters to PARAMS\n");
2665 fprintf(fp
, " (see the expected format of PARAMS below)\n");
2666 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2667 fprintf(fp
, " dynamic plugins can be loaded\n");
2668 fprintf(fp
, " --sink=PLUGIN.COMPCLS Query info from the sink component class\n");
2669 fprintf(fp
, " COMPCLS found in the plugin PLUGIN\n");
2670 fprintf(fp
, " --source=PLUGIN.COMPCLS Query info from the source component class\n");
2671 fprintf(fp
, " COMPCLS found in the plugin PLUGIN\n");
2672 fprintf(fp
, " -h --help Show this help and quit\n");
2673 fprintf(fp
, "\n\n");
2674 print_expected_params_format(fp
);
2677 static struct poptOption query_info_long_options
[] = {
2678 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
2679 { "filter", '\0', POPT_ARG_STRING
, NULL
, OPT_FILTER
, NULL
, NULL
},
2680 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
2681 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
2682 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
2683 { "params", 'p', POPT_ARG_STRING
, NULL
, OPT_PARAMS
, NULL
, NULL
},
2684 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
2685 { "sink", '\0', POPT_ARG_STRING
, NULL
, OPT_SINK
, NULL
, NULL
},
2686 { "source", '\0', POPT_ARG_STRING
, NULL
, OPT_SOURCE
, NULL
, NULL
},
2687 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
2691 * Creates a Babeltrace config object from the arguments of a query-info
2694 * *retcode is set to the appropriate exit code to use.
2696 struct bt_config
*bt_config_query_info_from_args(int argc
, const char *argv
[],
2697 int *retcode
, bool omit_system_plugin_path
,
2698 bool omit_home_plugin_path
,
2699 struct bt_value
*initial_plugin_paths
)
2701 poptContext pc
= NULL
;
2705 struct bt_config
*cfg
= NULL
;
2706 const char *leftover
;
2707 struct bt_value
*params
= bt_value_null
;
2710 cfg
= bt_config_query_info_create(initial_plugin_paths
);
2716 cfg
->cmd_data
.query_info
.omit_system_plugin_path
=
2717 omit_system_plugin_path
;
2718 cfg
->cmd_data
.query_info
.omit_home_plugin_path
= omit_home_plugin_path
;
2719 ret
= append_env_var_plugin_paths(cfg
->cmd_data
.query_info
.plugin_paths
);
2721 printf_err("Cannot append plugin paths from BABELTRACE_PLUGIN_PATH\n");
2726 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
2727 query_info_long_options
, 0);
2729 printf_err("Cannot get popt context\n");
2733 poptReadDefaultConfig(pc
, 0);
2735 while ((opt
= poptGetNextOpt(pc
)) > 0) {
2736 arg
= poptGetOptArg(pc
);
2739 case OPT_PLUGIN_PATH
:
2740 if (bt_common_is_setuid_setgid()) {
2741 printf_debug("Skipping non-system plugin paths for setuid/setgid binary\n");
2743 if (bt_config_append_plugin_paths(
2744 cfg
->cmd_data
.query_info
.plugin_paths
,
2746 printf_err("Invalid --plugin-path option's argument:\n %s\n",
2752 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
2753 cfg
->cmd_data
.query_info
.omit_system_plugin_path
= true;
2755 case OPT_OMIT_HOME_PLUGIN_PATH
:
2756 cfg
->cmd_data
.query_info
.omit_home_plugin_path
= true;
2762 enum bt_component_class_type type
;
2764 if (cfg
->cmd_data
.query_info
.cfg_component
) {
2765 printf_err("Cannot specify more than one plugin and component class:\n %s\n",
2772 type
= BT_COMPONENT_CLASS_TYPE_SOURCE
;
2775 type
= BT_COMPONENT_CLASS_TYPE_FILTER
;
2778 type
= BT_COMPONENT_CLASS_TYPE_SINK
;
2784 cfg
->cmd_data
.query_info
.cfg_component
=
2785 bt_config_component_from_arg(type
, arg
);
2786 if (!cfg
->cmd_data
.query_info
.cfg_component
) {
2787 printf_err("Invalid format for --source/--filter/--sink option's argument:\n %s\n",
2792 /* Default parameters: null */
2793 bt_put(cfg
->cmd_data
.query_info
.cfg_component
->params
);
2794 cfg
->cmd_data
.query_info
.cfg_component
->params
=
2800 params
= bt_value_from_arg(arg
);
2802 printf_err("Invalid format for --params option's argument:\n %s\n",
2809 print_query_info_usage(stdout
);
2814 printf_err("Unknown command-line option specified (option code %d)\n",
2823 if (!cfg
->cmd_data
.query_info
.cfg_component
) {
2824 printf_err("No target component class specified with --source/--filter/--sink option\n");
2829 BT_MOVE(cfg
->cmd_data
.query_info
.cfg_component
->params
, params
);
2831 /* Check for option parsing error */
2833 printf_err("While parsing command-line options, at option %s: %s\n",
2834 poptBadOption(pc
, 0), poptStrerror(opt
));
2839 * We need exactly one leftover argument which is the
2842 leftover
= poptGetArg(pc
);
2844 if (strlen(leftover
) == 0) {
2845 printf_err("Invalid empty action\n");
2849 g_string_assign(cfg
->cmd_data
.query_info
.action
, leftover
);
2851 print_query_info_usage(stdout
);
2857 leftover
= poptGetArg(pc
);
2859 printf_err("Invalid argument: %s\n", leftover
);
2863 if (append_home_and_system_plugin_paths(
2864 cfg
->cmd_data
.query_info
.plugin_paths
,
2865 cfg
->cmd_data
.query_info
.omit_system_plugin_path
,
2866 cfg
->cmd_data
.query_info
.omit_home_plugin_path
)) {
2867 printf_err("Cannot append home and system plugin paths\n");
2879 poptFreeContext(pc
);
2888 * Prints the list-plugins command usage.
2891 void print_list_plugins_usage(FILE *fp
)
2893 fprintf(fp
, "Usage: babeltrace [GENERAL OPTIONS] list-plugins [OPTIONS]\n");
2895 fprintf(fp
, "Options:\n");
2897 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2898 fprintf(fp
, " (~/.local/lib/babeltrace/plugins)\n");
2899 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2900 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2901 fprintf(fp
, " dynamic plugins can be loaded\n");
2902 fprintf(fp
, " -h --help Show this help and quit\n");
2904 fprintf(fp
, "See `babeltrace --help` for the list of general options.\n");
2906 fprintf(fp
, "Use `babeltrace help` to get help for a specific plugin or component class.\n");
2909 static struct poptOption list_plugins_long_options
[] = {
2910 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
2911 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
2912 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
2913 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
2914 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
2915 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
2919 * Creates a Babeltrace config object from the arguments of a
2920 * list-plugins command.
2922 * *retcode is set to the appropriate exit code to use.
2924 struct bt_config
*bt_config_list_plugins_from_args(int argc
, const char *argv
[],
2925 int *retcode
, bool omit_system_plugin_path
,
2926 bool omit_home_plugin_path
,
2927 struct bt_value
*initial_plugin_paths
)
2929 poptContext pc
= NULL
;
2933 struct bt_config
*cfg
= NULL
;
2934 const char *leftover
;
2937 cfg
= bt_config_list_plugins_create(initial_plugin_paths
);
2943 cfg
->cmd_data
.list_plugins
.omit_system_plugin_path
= omit_system_plugin_path
;
2944 cfg
->cmd_data
.list_plugins
.omit_home_plugin_path
= omit_home_plugin_path
;
2945 ret
= append_env_var_plugin_paths(
2946 cfg
->cmd_data
.list_plugins
.plugin_paths
);
2948 printf_err("Cannot append plugin paths from BABELTRACE_PLUGIN_PATH\n");
2953 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
2954 list_plugins_long_options
, 0);
2956 printf_err("Cannot get popt context\n");
2960 poptReadDefaultConfig(pc
, 0);
2962 while ((opt
= poptGetNextOpt(pc
)) > 0) {
2963 arg
= poptGetOptArg(pc
);
2966 case OPT_PLUGIN_PATH
:
2967 if (bt_common_is_setuid_setgid()) {
2968 printf_debug("Skipping non-system plugin paths for setuid/setgid binary\n");
2970 if (bt_config_append_plugin_paths(
2971 cfg
->cmd_data
.list_plugins
.plugin_paths
,
2973 printf_err("Invalid --plugin-path option's argument:\n %s\n",
2979 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
2980 cfg
->cmd_data
.list_plugins
.omit_system_plugin_path
= true;
2982 case OPT_OMIT_HOME_PLUGIN_PATH
:
2983 cfg
->cmd_data
.list_plugins
.omit_home_plugin_path
= true;
2986 print_list_plugins_usage(stdout
);
2991 printf_err("Unknown command-line option specified (option code %d)\n",
3000 /* Check for option parsing error */
3002 printf_err("While parsing command-line options, at option %s: %s\n",
3003 poptBadOption(pc
, 0), poptStrerror(opt
));
3007 leftover
= poptGetArg(pc
);
3009 printf_err("Invalid argument: %s\n", leftover
);
3013 if (append_home_and_system_plugin_paths(
3014 cfg
->cmd_data
.list_plugins
.plugin_paths
,
3015 cfg
->cmd_data
.list_plugins
.omit_system_plugin_path
,
3016 cfg
->cmd_data
.list_plugins
.omit_home_plugin_path
)) {
3017 printf_err("Cannot append home and system plugin paths\n");
3029 poptFreeContext(pc
);
3037 * Prints the legacy, Babeltrace 1.x command usage. Those options are
3038 * still compatible in Babeltrace 2.x, but it is recommended to use
3039 * the more generic plugin/component parameters instead of those
3040 * hard-coded option names.
3043 void print_legacy_usage(FILE *fp
)
3045 fprintf(fp
, "Usage: babeltrace [OPTIONS] INPUT...\n");
3047 fprintf(fp
, "The following options are compatible with the Babeltrace 1.x options:\n");
3049 fprintf(fp
, " --clock-force-correlate Assume that clocks are inherently correlated\n");
3050 fprintf(fp
, " across traces\n");
3051 fprintf(fp
, " -d, --debug Enable debug mode\n");
3052 fprintf(fp
, " -i, --input-format=FORMAT Input trace format (default: ctf)\n");
3053 fprintf(fp
, " -l, --list List available formats\n");
3054 fprintf(fp
, " -o, --output-format=FORMAT Output trace format (default: text)\n");
3055 fprintf(fp
, " -v, --verbose Enable verbose output\n");
3056 fprintf(fp
, " --help-legacy Show this help and quit\n");
3057 fprintf(fp
, " -V, --version Show version and quit\n");
3059 fprintf(fp
, " Available input formats: ctf, lttng-live, ctf-metadata\n");
3060 fprintf(fp
, " Available output formats: text, dummy\n");
3062 fprintf(fp
, "Input formats specific options:\n");
3064 fprintf(fp
, " INPUT... Input trace file(s), directory(ies), or URLs\n");
3065 fprintf(fp
, " --clock-offset=SEC Set clock offset to SEC seconds\n");
3066 fprintf(fp
, " --clock-offset-ns=NS Set clock offset to NS nanoseconds\n");
3067 fprintf(fp
, " --stream-intersection Only process events when all streams are active\n");
3069 fprintf(fp
, "text output format specific options:\n");
3071 fprintf(fp
, " --clock-cycles Print timestamps in clock cycles\n");
3072 fprintf(fp
, " --clock-date Print timestamp dates\n");
3073 fprintf(fp
, " --clock-gmt Print and parse timestamps in GMT time zone\n");
3074 fprintf(fp
, " (default: local time zone)\n");
3075 fprintf(fp
, " --clock-seconds Print the timestamps as [SEC.NS]\n");
3076 fprintf(fp
, " (default format: [HH:MM:SS.NS])\n");
3077 fprintf(fp
, " --debug-info-dir=DIR Search for debug info in directory DIR\n");
3078 fprintf(fp
, " (default: `/usr/lib/debug`)\n");
3079 fprintf(fp
, " --debug-info-full-path Show full debug info source and binary paths\n");
3080 fprintf(fp
, " --debug-info-target-prefix=DIR Use directory DIR as a prefix when looking\n");
3081 fprintf(fp
, " up executables during debug info analysis\n");
3082 fprintf(fp
, " (default: `/usr/lib/debug`)\n");
3083 fprintf(fp
, " -f, --fields=NAME[,NAME]... Print additional fields:\n");
3084 fprintf(fp
, " all, trace, trace:hostname, trace:domain,\n");
3085 fprintf(fp
, " trace:procname, trace:vpid, loglevel, emf\n");
3086 fprintf(fp
, " (default: trace:hostname, trace:procname,\n");
3087 fprintf(fp
, " trace:vpid)\n");
3088 fprintf(fp
, " -n, --names=NAME[,NAME]... Print field names:\n");
3089 fprintf(fp
, " payload (or arg or args)\n");
3090 fprintf(fp
, " none, all, scope, header, context (or ctx)\n");
3091 fprintf(fp
, " (default: payload, context)\n");
3092 fprintf(fp
, " --no-delta Do not print time delta between consecutive\n");
3093 fprintf(fp
, " events\n");
3094 fprintf(fp
, " -w, --output=PATH Write output to PATH (default: standard output)\n");
3098 * Prints the convert command usage.
3101 void print_convert_usage(FILE *fp
)
3103 fprintf(fp
, "Usage: babeltrace [GENERAL OPTIONS] convert [OPTIONS]\n");
3105 fprintf(fp
, "Options:\n");
3107 fprintf(fp
, " -b, --base-params=PARAMS Set PARAMS as the current base parameters\n");
3108 fprintf(fp
, " for the following component instances\n");
3109 fprintf(fp
, " (see the expected format of PARAMS below)\n");
3110 fprintf(fp
, " --begin=BEGIN Set the `begin` parameter of the latest\n");
3111 fprintf(fp
, " source component instance to BEGIN\n");
3112 fprintf(fp
, " (see the suggested format of BEGIN below)\n");
3113 fprintf(fp
, " -c, --connect=CONNECTION Connect two component instances (see the\n");
3114 fprintf(fp
, " expected format of CONNECTION below)\n");
3115 fprintf(fp
, " -d, --debug Enable debug mode\n");
3116 fprintf(fp
, " --end=END Set the `end` parameter of the latest\n");
3117 fprintf(fp
, " source component instance to END\n");
3118 fprintf(fp
, " (see the suggested format of BEGIN below)\n");
3119 fprintf(fp
, " --name=NAME Set the name of the latest component\n");
3120 fprintf(fp
, " instance to NAME (must be unique amongst\n");
3121 fprintf(fp
, " all the names of the component instances)\n");
3122 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
3123 fprintf(fp
, " (~/.local/lib/babeltrace/plugins)\n");
3124 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
3125 fprintf(fp
, " -p, --params=PARAMS Set the parameters of the latest component\n");
3126 fprintf(fp
, " instance (in command-line order) to PARAMS\n");
3127 fprintf(fp
, " (see the expected format of PARAMS below)\n");
3128 fprintf(fp
, " -P, --path=PATH Set the `path` parameter of the latest\n");
3129 fprintf(fp
, " component instance to PATH\n");
3130 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
3131 fprintf(fp
, " dynamic plugins can be loaded\n");
3132 fprintf(fp
, " -r, --reset-base-params Reset the current base parameters of the\n");
3133 fprintf(fp
, " following source and sink component\n");
3134 fprintf(fp
, " instances to an empty map\n");
3135 fprintf(fp
, " -o, --sink=PLUGIN.COMPCLS Instantiate a sink component from plugin\n");
3136 fprintf(fp
, " PLUGIN and component class COMPCLS (may be\n");
3137 fprintf(fp
, " repeated)\n");
3138 fprintf(fp
, " -i, --source=PLUGIN.COMPCLS Instantiate a source component from plugin\n");
3139 fprintf(fp
, " PLUGIN and component class COMPCLS (may be\n");
3140 fprintf(fp
, " repeated)\n");
3141 fprintf(fp
, " --timerange=TIMERANGE Set time range to TIMERANGE: BEGIN,END or\n");
3142 fprintf(fp
, " [BEGIN,END] (literally `[` and `]`)\n");
3143 fprintf(fp
, " (suggested format of BEGIN/END below)\n");
3144 fprintf(fp
, " -v, --verbose Enable verbose output\n");
3145 fprintf(fp
, " -h --help Show this help and quit\n");
3147 fprintf(fp
, "See `babeltrace --help` for the list of general options.\n");
3148 fprintf(fp
, "\n\n");
3149 fprintf(fp
, "Suggested format of BEGIN and END\n");
3150 fprintf(fp
, "---------------------------------\n");
3152 fprintf(fp
, " [YYYY-MM-DD [hh:mm:]]ss[.nnnnnnnnn]\n");
3153 fprintf(fp
, "\n\n");
3154 fprintf(fp
, "Expected format of CONNECTION\n");
3155 fprintf(fp
, "-----------------------------\n");
3157 fprintf(fp
, " SRC[.SRCPORT]:DST[.DSTPORT]\n");
3159 fprintf(fp
, "SRC and DST are the names of the source and destination component\n");
3160 fprintf(fp
, "instances to connect together. You can set the name of a component\n");
3161 fprintf(fp
, "instance with the --name option.\n");
3163 fprintf(fp
, "SRCPORT and DSTPORT are the optional source and destination ports to use\n");
3164 fprintf(fp
, "for the connection. When the port is not specified, the default port is\n");
3165 fprintf(fp
, "used.\n");
3167 fprintf(fp
, "You can connect a source component to a filter or sink component. You\n");
3168 fprintf(fp
, "can connect a filter component to a sink component.\n");
3170 fprintf(fp
, "Example:\n");
3172 fprintf(fp
, " my-filter.top10:json-out\n");
3173 fprintf(fp
, "\n\n");
3174 print_expected_params_format(fp
);
3177 static struct poptOption convert_long_options
[] = {
3178 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
3179 { "base-params", 'b', POPT_ARG_STRING
, NULL
, OPT_BASE_PARAMS
, NULL
, NULL
},
3180 { "begin", '\0', POPT_ARG_STRING
, NULL
, OPT_BEGIN
, NULL
, NULL
},
3181 { "clock-cycles", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_CYCLES
, NULL
, NULL
},
3182 { "clock-date", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_DATE
, NULL
, NULL
},
3183 { "clock-force-correlate", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_FORCE_CORRELATE
, NULL
, NULL
},
3184 { "clock-gmt", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_GMT
, NULL
, NULL
},
3185 { "clock-offset", '\0', POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET
, NULL
, NULL
},
3186 { "clock-offset-ns", '\0', POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET_NS
, NULL
, NULL
},
3187 { "clock-seconds", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_SECONDS
, NULL
, NULL
},
3188 { "connect", 'c', POPT_ARG_STRING
, NULL
, OPT_CONNECT
, NULL
, NULL
},
3189 { "debug", 'd', POPT_ARG_NONE
, NULL
, OPT_DEBUG
, NULL
, NULL
},
3190 { "debug-info-dir", 0, POPT_ARG_STRING
, NULL
, OPT_DEBUG_INFO_DIR
, NULL
, NULL
},
3191 { "debug-info-full-path", 0, POPT_ARG_NONE
, NULL
, OPT_DEBUG_INFO_FULL_PATH
, NULL
, NULL
},
3192 { "debug-info-target-prefix", 0, POPT_ARG_STRING
, NULL
, OPT_DEBUG_INFO_TARGET_PREFIX
, NULL
, NULL
},
3193 { "end", '\0', POPT_ARG_STRING
, NULL
, OPT_END
, NULL
, NULL
},
3194 { "fields", 'f', POPT_ARG_STRING
, NULL
, OPT_FIELDS
, NULL
, NULL
},
3195 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
3196 { "input-format", 'i', POPT_ARG_STRING
, NULL
, OPT_INPUT_FORMAT
, NULL
, NULL
},
3197 { "name", '\0', POPT_ARG_STRING
, NULL
, OPT_NAME
, NULL
, NULL
},
3198 { "names", 'n', POPT_ARG_STRING
, NULL
, OPT_NAMES
, NULL
, NULL
},
3199 { "no-delta", '\0', POPT_ARG_NONE
, NULL
, OPT_NO_DELTA
, NULL
, NULL
},
3200 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
3201 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
3202 { "output", 'w', POPT_ARG_STRING
, NULL
, OPT_OUTPUT_PATH
, NULL
, NULL
},
3203 { "output-format", 'o', POPT_ARG_STRING
, NULL
, OPT_OUTPUT_FORMAT
, NULL
, NULL
},
3204 { "params", 'p', POPT_ARG_STRING
, NULL
, OPT_PARAMS
, NULL
, NULL
},
3205 { "path", 'P', POPT_ARG_STRING
, NULL
, OPT_PATH
, NULL
, NULL
},
3206 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
3207 { "reset-base-params", 'r', POPT_ARG_NONE
, NULL
, OPT_RESET_BASE_PARAMS
, NULL
, NULL
},
3208 { "sink", '\0', POPT_ARG_STRING
, NULL
, OPT_SINK
, NULL
, NULL
},
3209 { "source", '\0', POPT_ARG_STRING
, NULL
, OPT_SOURCE
, NULL
, NULL
},
3210 { "stream-intersection", '\0', POPT_ARG_NONE
, NULL
, OPT_STREAM_INTERSECTION
, NULL
, NULL
},
3211 { "timerange", '\0', POPT_ARG_STRING
, NULL
, OPT_TIMERANGE
, NULL
, NULL
},
3212 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
3213 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
3217 * Creates a Babeltrace config object from the arguments of a convert
3220 * *retcode is set to the appropriate exit code to use.
3222 struct bt_config
*bt_config_convert_from_args(int argc
, const char *argv
[],
3223 int *retcode
, bool omit_system_plugin_path
,
3224 bool omit_home_plugin_path
,
3225 struct bt_value
*initial_plugin_paths
)
3227 poptContext pc
= NULL
;
3229 struct ctf_legacy_opts ctf_legacy_opts
;
3230 struct text_legacy_opts text_legacy_opts
;
3231 enum legacy_input_format legacy_input_format
= LEGACY_INPUT_FORMAT_NONE
;
3232 enum legacy_output_format legacy_output_format
=
3233 LEGACY_OUTPUT_FORMAT_NONE
;
3234 struct bt_value
*legacy_input_paths
= NULL
;
3235 struct bt_config_component
*implicit_source_comp
= NULL
;
3236 struct bt_config_component
*cur_cfg_comp
= NULL
;
3237 bool cur_is_implicit_source
= false;
3238 bool use_implicit_source
= false;
3239 enum bt_config_component_dest cur_cfg_comp_dest
=
3240 BT_CONFIG_COMPONENT_DEST_SOURCE
;
3241 struct bt_value
*cur_base_params
= NULL
;
3243 struct bt_config
*cfg
= NULL
;
3244 struct bt_value
*instance_names
= NULL
;
3245 struct bt_value
*connection_args
= NULL
;
3246 char error_buf
[256] = { 0 };
3249 memset(&ctf_legacy_opts
, 0, sizeof(ctf_legacy_opts
));
3250 memset(&text_legacy_opts
, 0, sizeof(text_legacy_opts
));
3253 print_convert_usage(stdout
);
3258 cfg
= bt_config_convert_create(initial_plugin_paths
);
3264 cfg
->cmd_data
.convert
.omit_system_plugin_path
= omit_system_plugin_path
;
3265 cfg
->cmd_data
.convert
.omit_home_plugin_path
= omit_home_plugin_path
;
3266 text_legacy_opts
.output
= g_string_new(NULL
);
3267 if (!text_legacy_opts
.output
) {
3272 text_legacy_opts
.dbg_info_dir
= g_string_new(NULL
);
3273 if (!text_legacy_opts
.dbg_info_dir
) {
3278 text_legacy_opts
.dbg_info_target_prefix
= g_string_new(NULL
);
3279 if (!text_legacy_opts
.dbg_info_target_prefix
) {
3284 cur_base_params
= bt_value_map_create();
3285 if (!cur_base_params
) {
3290 legacy_input_paths
= bt_value_array_create();
3291 if (!legacy_input_paths
) {
3296 instance_names
= bt_value_map_create();
3297 if (!instance_names
) {
3302 connection_args
= bt_value_array_create();
3303 if (!connection_args
) {
3308 ret
= append_env_var_plugin_paths(cfg
->cmd_data
.convert
.plugin_paths
);
3310 printf_err("Cannot append plugin paths from BABELTRACE_PLUGIN_PATH\n");
3314 /* Note: implicit source never gets positional base params. */
3315 implicit_source_comp
= bt_config_component_from_arg(
3316 BT_COMPONENT_CLASS_TYPE_SOURCE
, DEFAULT_SOURCE_COMPONENT_NAME
);
3317 if (!implicit_source_comp
) {
3322 cur_cfg_comp
= implicit_source_comp
;
3323 cur_is_implicit_source
= true;
3324 use_implicit_source
= true;
3327 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
3328 convert_long_options
, 0);
3330 printf_err("Cannot get popt context\n");
3334 poptReadDefaultConfig(pc
, 0);
3336 while ((opt
= poptGetNextOpt(pc
)) > 0) {
3337 arg
= poptGetOptArg(pc
);
3340 case OPT_PLUGIN_PATH
:
3341 if (bt_common_is_setuid_setgid()) {
3342 printf_debug("Skipping non-system plugin paths for setuid/setgid binary\n");
3344 if (bt_config_append_plugin_paths(
3345 cfg
->cmd_data
.convert
.plugin_paths
,
3347 printf_err("Invalid --plugin-path option's argument:\n %s\n",
3353 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
3354 cfg
->cmd_data
.convert
.omit_system_plugin_path
= true;
3356 case OPT_OMIT_HOME_PLUGIN_PATH
:
3357 cfg
->cmd_data
.convert
.omit_home_plugin_path
= true;
3359 case OPT_OUTPUT_PATH
:
3360 if (text_legacy_opts
.output
->len
> 0) {
3361 printf_err("Duplicate --output option\n");
3365 g_string_assign(text_legacy_opts
.output
, arg
);
3367 case OPT_DEBUG_INFO_DIR
:
3368 if (text_legacy_opts
.dbg_info_dir
->len
> 0) {
3369 printf_err("Duplicate --debug-info-dir option\n");
3373 g_string_assign(text_legacy_opts
.dbg_info_dir
, arg
);
3375 case OPT_DEBUG_INFO_TARGET_PREFIX
:
3376 if (text_legacy_opts
.dbg_info_target_prefix
->len
> 0) {
3377 printf_err("Duplicate --debug-info-target-prefix option\n");
3381 g_string_assign(text_legacy_opts
.dbg_info_target_prefix
, arg
);
3383 case OPT_INPUT_FORMAT
:
3386 if (opt
== OPT_INPUT_FORMAT
) {
3387 if (!strcmp(arg
, "ctf")) {
3388 /* Legacy CTF input format */
3389 if (legacy_input_format
) {
3390 print_err_dup_legacy_input();
3394 legacy_input_format
=
3395 LEGACY_INPUT_FORMAT_CTF
;
3397 } else if (!strcmp(arg
, "lttng-live")) {
3398 /* Legacy LTTng-live input format */
3399 if (legacy_input_format
) {
3400 print_err_dup_legacy_input();
3404 legacy_input_format
=
3405 LEGACY_INPUT_FORMAT_LTTNG_LIVE
;
3410 use_implicit_source
= false;
3412 /* Non-legacy: try to create a component config */
3413 if (cur_cfg_comp
&& !cur_is_implicit_source
) {
3414 add_cfg_comp(cfg
, cur_cfg_comp
,
3418 cur_cfg_comp
= bt_config_component_from_arg(
3419 BT_COMPONENT_CLASS_TYPE_SOURCE
, arg
);
3420 if (!cur_cfg_comp
) {
3421 printf_err("Invalid format for --source option's argument:\n %s\n",
3425 cur_is_implicit_source
= false;
3427 assert(cur_base_params
);
3428 bt_put(cur_cfg_comp
->params
);
3429 cur_cfg_comp
->params
= bt_value_copy(cur_base_params
);
3430 if (!cur_cfg_comp
->params
) {
3435 cur_cfg_comp_dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
3438 case OPT_OUTPUT_FORMAT
:
3441 if (opt
== OPT_OUTPUT_FORMAT
) {
3442 if (!strcmp(arg
, "text")) {
3443 /* Legacy CTF-text output format */
3444 if (legacy_output_format
) {
3445 print_err_dup_legacy_output();
3449 legacy_output_format
=
3450 LEGACY_OUTPUT_FORMAT_TEXT
;
3452 } else if (!strcmp(arg
, "dummy")) {
3453 /* Legacy dummy output format */
3454 if (legacy_output_format
) {
3455 print_err_dup_legacy_output();
3459 legacy_output_format
=
3460 LEGACY_OUTPUT_FORMAT_DUMMY
;
3462 } else if (!strcmp(arg
, "ctf-metadata")) {
3463 /* Legacy CTF-metadata output format */
3464 if (legacy_output_format
) {
3465 print_err_dup_legacy_output();
3469 legacy_output_format
=
3470 LEGACY_OUTPUT_FORMAT_CTF_METADATA
;
3475 /* Non-legacy: try to create a component config */
3476 if (cur_cfg_comp
&& !cur_is_implicit_source
) {
3477 add_cfg_comp(cfg
, cur_cfg_comp
,
3481 cur_cfg_comp
= bt_config_component_from_arg(
3482 BT_COMPONENT_CLASS_TYPE_SINK
, arg
);
3483 if (!cur_cfg_comp
) {
3484 printf_err("Invalid format for --sink option's argument:\n %s\n",
3488 cur_is_implicit_source
= false;
3490 assert(cur_base_params
);
3491 bt_put(cur_cfg_comp
->params
);
3492 cur_cfg_comp
->params
= bt_value_copy(cur_base_params
);
3493 if (!cur_cfg_comp
->params
) {
3498 cur_cfg_comp_dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
3503 struct bt_value
*params
;
3504 struct bt_value
*params_to_set
;
3506 if (!cur_cfg_comp
) {
3507 printf_err("Cannot add parameters to unavailable default source component `%s`:\n %s\n",
3508 DEFAULT_SOURCE_COMPONENT_NAME
, arg
);
3512 params
= bt_value_from_arg(arg
);
3514 printf_err("Invalid format for --params option's argument:\n %s\n",
3519 params_to_set
= bt_value_map_extend(cur_cfg_comp
->params
,
3522 if (!params_to_set
) {
3523 printf_err("Cannot extend current component parameters with --params option's argument:\n %s\n",
3528 BT_MOVE(cur_cfg_comp
->params
, params_to_set
);
3532 if (!cur_cfg_comp
) {
3533 printf_err("Cannot add `path` parameter to unavailable default source component `%s`:\n %s\n",
3534 DEFAULT_SOURCE_COMPONENT_NAME
, arg
);
3538 assert(cur_cfg_comp
->params
);
3540 if (bt_value_map_insert_string(cur_cfg_comp
->params
,
3547 if (!cur_cfg_comp
) {
3548 printf_err("Cannot set the name of unavailable default source component `%s`:\n %s\n",
3549 DEFAULT_SOURCE_COMPONENT_NAME
, arg
);
3553 if (bt_value_map_has_key(instance_names
, arg
)) {
3554 printf_err("Duplicate component instance name:\n %s\n",
3559 g_string_assign(cur_cfg_comp
->instance_name
, arg
);
3561 if (bt_value_map_insert(instance_names
,
3562 arg
, bt_value_null
)) {
3567 case OPT_BASE_PARAMS
:
3569 struct bt_value
*params
= bt_value_from_arg(arg
);
3572 printf_err("Invalid format for --base-params option's argument:\n %s\n",
3577 BT_MOVE(cur_base_params
, params
);
3580 case OPT_RESET_BASE_PARAMS
:
3581 BT_PUT(cur_base_params
);
3582 cur_base_params
= bt_value_map_create();
3583 if (!cur_base_params
) {
3589 if (text_legacy_opts
.names
) {
3590 printf_err("Duplicate --names option\n");
3594 text_legacy_opts
.names
= names_from_arg(arg
);
3595 if (!text_legacy_opts
.names
) {
3596 printf_err("Invalid --names option's argument:\n %s\n",
3602 if (text_legacy_opts
.fields
) {
3603 printf_err("Duplicate --fields option\n");
3607 text_legacy_opts
.fields
= fields_from_arg(arg
);
3608 if (!text_legacy_opts
.fields
) {
3609 printf_err("Invalid --fields option's argument:\n %s\n",
3615 text_legacy_opts
.no_delta
= true;
3617 case OPT_CLOCK_CYCLES
:
3618 text_legacy_opts
.clock_cycles
= true;
3620 case OPT_CLOCK_SECONDS
:
3621 text_legacy_opts
.clock_seconds
= true;
3623 case OPT_CLOCK_DATE
:
3624 text_legacy_opts
.clock_date
= true;
3627 text_legacy_opts
.clock_gmt
= true;
3629 case OPT_DEBUG_INFO_FULL_PATH
:
3630 text_legacy_opts
.dbg_info_full_path
= true;
3632 case OPT_CLOCK_OFFSET
:
3636 if (ctf_legacy_opts
.offset_s
.is_set
) {
3637 printf_err("Duplicate --clock-offset option\n");
3641 if (parse_int64(arg
, &val
)) {
3642 printf_err("Invalid --clock-offset option's argument:\n %s\n",
3647 set_offset_value(&ctf_legacy_opts
.offset_s
, val
);
3650 case OPT_CLOCK_OFFSET_NS
:
3654 if (ctf_legacy_opts
.offset_ns
.is_set
) {
3655 printf_err("Duplicate --clock-offset-ns option\n");
3659 if (parse_int64(arg
, &val
)) {
3660 printf_err("Invalid --clock-offset-ns option's argument:\n %s\n",
3665 set_offset_value(&ctf_legacy_opts
.offset_ns
, val
);
3668 case OPT_STREAM_INTERSECTION
:
3669 ctf_legacy_opts
.stream_intersection
= true;
3671 case OPT_CLOCK_FORCE_CORRELATE
:
3672 cfg
->cmd_data
.convert
.force_correlate
= true;
3675 if (!cur_cfg_comp
) {
3676 printf_err("Cannot add `begin` parameter to unavailable default source component `%s`:\n %s\n",
3677 DEFAULT_SOURCE_COMPONENT_NAME
, arg
);
3680 if (cur_cfg_comp_dest
!= BT_CONFIG_COMPONENT_DEST_SOURCE
) {
3681 printf_err("--begin option must follow a --source option:\n %s\n",
3685 if (bt_value_map_insert_string(cur_cfg_comp
->params
,
3692 if (!cur_cfg_comp
) {
3693 printf_err("Cannot add `end` parameter to unavailable default source component `%s`:\n %s\n",
3694 DEFAULT_SOURCE_COMPONENT_NAME
, arg
);
3697 if (cur_cfg_comp_dest
!= BT_CONFIG_COMPONENT_DEST_SOURCE
) {
3698 printf_err("--end option must follow a --source option:\n %s\n",
3702 if (bt_value_map_insert_string(cur_cfg_comp
->params
,
3710 const char *begin
, *end
;
3712 if (!cur_cfg_comp
) {
3713 printf_err("Cannot add `begin` and `end` parameters to unavailable default source component `%s`:\n %s\n",
3714 DEFAULT_SOURCE_COMPONENT_NAME
, arg
);
3717 if (cur_cfg_comp_dest
!= BT_CONFIG_COMPONENT_DEST_SOURCE
) {
3718 printf_err("--timerange option must follow a --source option:\n %s\n",
3722 if (split_timerange(arg
, &begin
, &end
)) {
3723 printf_err("Invalid --timerange format: expecting BEGIN,END or [BEGIN,END]:\n %s\n",
3727 if (bt_value_map_insert_string(cur_cfg_comp
->params
,
3732 if (bt_value_map_insert_string(cur_cfg_comp
->params
,
3740 if (bt_value_array_append_string(connection_args
,
3747 print_convert_usage(stdout
);
3752 cfg
->verbose
= true;
3758 printf_err("Unknown command-line option specified (option code %d)\n",
3767 /* Check for option parsing error */
3769 printf_err("While parsing command-line options, at option %s: %s\n",
3770 poptBadOption(pc
, 0), poptStrerror(opt
));
3774 /* Consume leftover arguments as legacy input paths */
3776 const char *input_path
= poptGetArg(pc
);
3782 if (bt_value_array_append_string(legacy_input_paths
,
3789 if (append_home_and_system_plugin_paths(
3790 cfg
->cmd_data
.convert
.plugin_paths
,
3791 cfg
->cmd_data
.convert
.omit_system_plugin_path
,
3792 cfg
->cmd_data
.convert
.omit_home_plugin_path
)) {
3793 printf_err("Cannot append home and system plugin paths\n");
3797 /* Append current component configuration, if any */
3798 if (cur_cfg_comp
&& !cur_is_implicit_source
) {
3799 add_cfg_comp(cfg
, cur_cfg_comp
, cur_cfg_comp_dest
);
3801 cur_cfg_comp
= NULL
;
3803 /* Validate legacy/non-legacy options */
3804 if (!validate_cfg(cfg
, &legacy_input_format
, &legacy_output_format
,
3805 legacy_input_paths
, &ctf_legacy_opts
,
3806 &text_legacy_opts
)) {
3807 printf_err("Command-line options form an invalid configuration\n");
3812 * If there's a legacy input format, convert it to source
3813 * component configurations.
3815 if (legacy_input_format
) {
3816 if (append_sources_from_legacy_opts(
3817 cfg
->cmd_data
.convert
.sources
,
3818 legacy_input_format
, &ctf_legacy_opts
,
3819 legacy_input_paths
)) {
3820 printf_err("Cannot convert legacy input format options to source component instance(s)\n");
3823 if (append_sources_from_implicit_params(
3824 cfg
->cmd_data
.convert
.sources
,
3825 implicit_source_comp
)) {
3826 printf_err("Cannot initialize legacy component parameters\n");
3829 use_implicit_source
= false;
3831 if (use_implicit_source
) {
3832 add_cfg_comp(cfg
, implicit_source_comp
,
3833 BT_CONFIG_COMPONENT_DEST_SOURCE
);
3834 implicit_source_comp
= NULL
;
3836 if (implicit_source_comp
3837 && !bt_value_map_is_empty(implicit_source_comp
->params
)) {
3838 printf_err("Arguments specified for implicit source, but an explicit source has been specified, overriding it\n");
3845 * If there's a legacy output format, convert it to sink
3846 * component configurations.
3848 if (legacy_output_format
) {
3849 if (append_sinks_from_legacy_opts(cfg
->cmd_data
.convert
.sinks
,
3850 legacy_output_format
, &text_legacy_opts
)) {
3851 printf_err("Cannot convert legacy output format options to sink component instance(s)\n");
3856 if (cfg
->cmd_data
.convert
.sinks
->len
== 0) {
3857 /* Use implicit sink as default. */
3858 cur_cfg_comp
= bt_config_component_from_arg(
3859 BT_COMPONENT_CLASS_TYPE_SINK
,
3860 DEFAULT_SINK_COMPONENT_NAME
);
3861 if (!cur_cfg_comp
) {
3862 printf_error("Cannot find implicit sink plugin `%s`\n",
3863 DEFAULT_SINK_COMPONENT_NAME
);
3865 add_cfg_comp(cfg
, cur_cfg_comp
,
3866 BT_CONFIG_COMPONENT_DEST_SINK
);
3867 cur_cfg_comp
= NULL
;
3870 ret
= bt_config_create_connections(cfg
, connection_args
,
3873 printf_err("Cannot creation connections:\n%s", error_buf
);
3885 poptFreeContext(pc
);
3888 if (text_legacy_opts
.output
) {
3889 g_string_free(text_legacy_opts
.output
, TRUE
);
3892 if (text_legacy_opts
.dbg_info_dir
) {
3893 g_string_free(text_legacy_opts
.dbg_info_dir
, TRUE
);
3896 if (text_legacy_opts
.dbg_info_target_prefix
) {
3897 g_string_free(text_legacy_opts
.dbg_info_target_prefix
, TRUE
);
3901 BT_PUT(implicit_source_comp
);
3902 BT_PUT(cur_cfg_comp
);
3903 BT_PUT(cur_base_params
);
3904 BT_PUT(text_legacy_opts
.names
);
3905 BT_PUT(text_legacy_opts
.fields
);
3906 BT_PUT(legacy_input_paths
);
3907 BT_PUT(instance_names
);
3908 BT_PUT(connection_args
);
3913 * Prints the Babeltrace 2.x general usage.
3916 void print_gen_usage(FILE *fp
)
3918 fprintf(fp
, "Usage: babeltrace [GENERAL OPTIONS] [COMMAND] [COMMAND OPTIONS]\n");
3920 fprintf(fp
, "General options:\n");
3922 fprintf(fp
, " -d, --debug Enable debug mode\n");
3923 fprintf(fp
, " -h --help Show this help and quit\n");
3924 fprintf(fp
, " --help-legacy Show Babeltrace 1.x legacy help and quit\n");
3925 fprintf(fp
, " -v, --verbose Enable verbose output\n");
3926 fprintf(fp
, " -V, --version Show version and quit\n");
3928 fprintf(fp
, "Available commands:\n");
3930 fprintf(fp
, " convert Build a trace conversion graph and run it (default)\n");
3931 fprintf(fp
, " help Get help for a plugin or a component class\n");
3932 fprintf(fp
, " list-plugins List available plugins and their content\n");
3933 fprintf(fp
, " query-info Query information from a component class\n");
3935 fprintf(fp
, "Use `babeltrace COMMAND --help` to show the help of COMMAND.\n");
3938 struct bt_config
*bt_config_from_args(int argc
, const char *argv
[],
3939 int *retcode
, bool omit_system_plugin_path
,
3940 bool omit_home_plugin_path
,
3941 struct bt_value
*initial_plugin_paths
)
3943 struct bt_config
*config
= NULL
;
3944 bool verbose
= false;
3947 enum bt_config_command command
= -1;
3948 const char **command_argv
= NULL
;
3949 int command_argc
= -1;
3950 const char *command_name
= NULL
;
3955 print_gen_usage(stdout
);
3959 for (i
= 1; i
< argc
; i
++) {
3960 const char *cur_arg
= argv
[i
];
3962 if (strcmp(cur_arg
, "-d") == 0 ||
3963 strcmp(cur_arg
, "--debug") == 0) {
3965 } else if (strcmp(cur_arg
, "-v") == 0 ||
3966 strcmp(cur_arg
, "--verbose") == 0) {
3968 } else if (strcmp(cur_arg
, "-V") == 0 ||
3969 strcmp(cur_arg
, "--version") == 0) {
3972 } else if (strcmp(cur_arg
, "-h") == 0 ||
3973 strcmp(cur_arg
, "--help") == 0) {
3974 print_gen_usage(stdout
);
3976 } else if (strcmp(cur_arg
, "--help-legacy") == 0) {
3977 print_legacy_usage(stdout
);
3980 bool has_command
= true;
3983 * First unknown argument: is it a known command
3986 if (strcmp(cur_arg
, "convert") == 0) {
3987 command
= BT_CONFIG_COMMAND_CONVERT
;
3988 } else if (strcmp(cur_arg
, "list-plugins") == 0) {
3989 command
= BT_CONFIG_COMMAND_LIST_PLUGINS
;
3990 } else if (strcmp(cur_arg
, "help") == 0) {
3991 command
= BT_CONFIG_COMMAND_HELP
;
3992 } else if (strcmp(cur_arg
, "query-info") == 0) {
3993 command
= BT_CONFIG_COMMAND_QUERY_INFO
;
3996 * Unknown argument, but not a known
3997 * command name: assume the whole
3998 * arguments are for the default convert
4001 command
= BT_CONFIG_COMMAND_CONVERT
;
4002 command_argv
= argv
;
4003 command_argc
= argc
;
4004 has_command
= false;
4008 command_argv
= &argv
[i
];
4009 command_argc
= argc
- i
;
4010 command_name
= cur_arg
;
4016 if ((int) command
< 0) {
4018 * We only got non-help, non-version general options
4019 * like --verbose and --debug, without any other
4020 * arguments, so we can't do anything useful: print the
4023 print_gen_usage(stdout
);
4027 assert(command_argv
);
4028 assert(command_argc
>= 0);
4031 case BT_CONFIG_COMMAND_CONVERT
:
4032 config
= bt_config_convert_from_args(command_argc
, command_argv
,
4033 retcode
, omit_system_plugin_path
,
4034 omit_home_plugin_path
, initial_plugin_paths
);
4036 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
4037 config
= bt_config_list_plugins_from_args(command_argc
,
4038 command_argv
, retcode
, omit_system_plugin_path
,
4039 omit_home_plugin_path
, initial_plugin_paths
);
4041 case BT_CONFIG_COMMAND_HELP
:
4042 config
= bt_config_help_from_args(command_argc
,
4043 command_argv
, retcode
, omit_system_plugin_path
,
4044 omit_home_plugin_path
, initial_plugin_paths
);
4046 case BT_CONFIG_COMMAND_QUERY_INFO
:
4047 config
= bt_config_query_info_from_args(command_argc
,
4048 command_argv
, retcode
, omit_system_plugin_path
,
4049 omit_home_plugin_path
, initial_plugin_paths
);
4057 config
->verbose
= true;
4061 config
->debug
= true;
4064 config
->command_name
= command_name
;