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
;
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 * 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
|| opts
->verbose
||
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
:
859 BT_PUT(cfg
->cmd_data
.query
.plugin_paths
);
860 BT_PUT(cfg
->cmd_data
.query
.cfg_component
);
862 if (cfg
->cmd_data
.query
.object
) {
863 g_string_free(cfg
->cmd_data
.query
.object
, 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 (bt_value_map_insert_bool(params
, "verbose",
1330 text_legacy_opts
->verbose
)) {
1335 if (insert_flat_names_fields_from_array(params
,
1336 text_legacy_opts
->names
, "name")) {
1340 if (insert_flat_names_fields_from_array(params
,
1341 text_legacy_opts
->fields
, "field")) {
1355 int append_sinks_from_legacy_opts(GPtrArray
*sinks
,
1356 enum legacy_output_format legacy_output_format
,
1357 struct text_legacy_opts
*text_legacy_opts
)
1360 struct bt_value
*params
= NULL
;
1361 const char *plugin_name
;
1362 const char *component_name
;
1363 struct bt_config_component
*bt_config_component
= NULL
;
1365 switch (legacy_output_format
) {
1366 case LEGACY_OUTPUT_FORMAT_TEXT
:
1367 plugin_name
= "text";
1368 component_name
= "text";
1370 case LEGACY_OUTPUT_FORMAT_DUMMY
:
1371 plugin_name
= "utils";
1372 component_name
= "dummy";
1379 if (legacy_output_format
== LEGACY_OUTPUT_FORMAT_TEXT
) {
1380 /* Legacy "text" output format has parameters */
1381 params
= params_from_text_legacy_opts(text_legacy_opts
);
1387 * Legacy "dummy" and "ctf-metadata" output formats do
1388 * not have parameters.
1390 params
= bt_value_map_create();
1397 /* Create a component configuration */
1398 bt_config_component
= bt_config_component_create(
1399 BT_COMPONENT_CLASS_TYPE_SINK
, plugin_name
, component_name
);
1400 if (!bt_config_component
) {
1404 BT_MOVE(bt_config_component
->params
, params
);
1406 /* Move created component configuration to the array */
1407 g_ptr_array_add(sinks
, bt_config_component
);
1421 * Returns the parameters (map value object) corresponding to the
1422 * given legacy CTF format options.
1424 * Return value is owned by the caller.
1427 struct bt_value
*params_from_ctf_legacy_opts(
1428 struct ctf_legacy_opts
*ctf_legacy_opts
)
1430 struct bt_value
*params
;
1432 params
= bt_value_map_create();
1438 if (bt_value_map_insert_integer(params
, "offset-s",
1439 ctf_legacy_opts
->offset_s
.value
)) {
1444 if (bt_value_map_insert_integer(params
, "offset-ns",
1445 ctf_legacy_opts
->offset_ns
.value
)) {
1450 if (bt_value_map_insert_bool(params
, "stream-intersection",
1451 ctf_legacy_opts
->stream_intersection
)) {
1466 int append_sources_from_legacy_opts(GPtrArray
*sources
,
1467 enum legacy_input_format legacy_input_format
,
1468 struct ctf_legacy_opts
*ctf_legacy_opts
,
1469 struct bt_value
*legacy_input_paths
)
1473 struct bt_value
*base_params
;
1474 struct bt_value
*params
= NULL
;
1475 struct bt_value
*input_path
= NULL
;
1476 struct bt_value
*input_path_copy
= NULL
;
1477 const char *input_key
;
1478 const char *component_name
;
1480 switch (legacy_input_format
) {
1481 case LEGACY_INPUT_FORMAT_CTF
:
1483 component_name
= "fs";
1485 case LEGACY_INPUT_FORMAT_LTTNG_LIVE
:
1487 component_name
= "lttng-live";
1494 base_params
= params_from_ctf_legacy_opts(ctf_legacy_opts
);
1499 for (i
= 0; i
< bt_value_array_size(legacy_input_paths
); i
++) {
1500 struct bt_config_component
*bt_config_component
= NULL
;
1502 /* Copy base parameters as current parameters */
1503 params
= bt_value_copy(base_params
);
1508 /* Get current input path string value object */
1509 input_path
= bt_value_array_get(legacy_input_paths
, i
);
1514 /* Copy current input path value object */
1515 input_path_copy
= bt_value_copy(input_path
);
1516 if (!input_path_copy
) {
1520 /* Insert input path value object into current parameters */
1521 ret
= bt_value_map_insert(params
, input_key
, input_path_copy
);
1526 /* Create a component configuration */
1527 bt_config_component
= bt_config_component_create(
1528 BT_COMPONENT_CLASS_TYPE_SOURCE
, "ctf", component_name
);
1529 if (!bt_config_component
) {
1533 BT_MOVE(bt_config_component
->params
, params
);
1535 /* Move created component configuration to the array */
1536 g_ptr_array_add(sources
, bt_config_component
);
1538 /* Put current stuff */
1540 BT_PUT(input_path_copy
);
1549 BT_PUT(base_params
);
1552 BT_PUT(input_path_copy
);
1557 * Escapes a string for the shell. The string is escaped knowing that
1558 * it's a parameter string value (double-quoted), and that it will be
1559 * entered between single quotes in the shell.
1561 * Return value is owned by the caller.
1564 char *str_shell_escape(const char *input
)
1567 const char *at
= input
;
1568 GString
*str
= g_string_new(NULL
);
1574 while (*at
!= '\0') {
1577 g_string_append(str
, "\\\\");
1580 g_string_append(str
, "\\\"");
1583 g_string_append(str
, "'\"'\"'");
1586 g_string_append(str
, "\\n");
1589 g_string_append(str
, "\\t");
1592 g_string_append_c(str
, *at
);
1602 g_string_free(str
, FALSE
);
1609 int append_prefixed_flag_params(GString
*str
, struct bt_value
*flags
,
1619 for (i
= 0; i
< bt_value_array_size(flags
); i
++) {
1620 struct bt_value
*value
= bt_value_array_get(flags
, i
);
1628 if (bt_value_string_get(value
, &flag
)) {
1634 g_string_append_printf(str
, ",%s-%s=true", prefix
, flag
);
1643 * Appends a boolean parameter string.
1646 void g_string_append_bool_param(GString
*str
, const char *name
, bool value
)
1648 g_string_append_printf(str
, ",%s=%s", name
, value
? "true" : "false");
1652 * Appends a path parameter string, or null if it's empty.
1655 int g_string_append_string_path_param(GString
*str
, const char *name
,
1660 if (path
->len
> 0) {
1661 char *escaped_path
= str_shell_escape(path
->str
);
1663 if (!escaped_path
) {
1668 g_string_append_printf(str
, "%s=\"%s\"", name
, escaped_path
);
1671 g_string_append_printf(str
, "%s=null", name
);
1684 * Prints the non-legacy sink options equivalent to the specified
1685 * legacy output format options.
1688 void print_output_legacy_to_sinks(
1689 enum legacy_output_format legacy_output_format
,
1690 struct text_legacy_opts
*text_legacy_opts
)
1692 const char *output_format
;
1693 GString
*str
= NULL
;
1695 str
= g_string_new(" ");
1701 switch (legacy_output_format
) {
1702 case LEGACY_OUTPUT_FORMAT_TEXT
:
1703 output_format
= "text";
1705 case LEGACY_OUTPUT_FORMAT_DUMMY
:
1706 output_format
= "dummy";
1712 printf_err("Both `%s` legacy output format and non-legacy sink component\ninstances(s) specified.\n\n",
1714 printf_err("Specify the following non-legacy sink component instance instead of the\nlegacy `%s` output format options:\n\n",
1716 g_string_append(str
, "-o ");
1718 switch (legacy_output_format
) {
1719 case LEGACY_OUTPUT_FORMAT_TEXT
:
1720 g_string_append(str
, "text.text");
1722 case LEGACY_OUTPUT_FORMAT_DUMMY
:
1723 g_string_append(str
, "utils.dummy");
1729 if (legacy_output_format
== LEGACY_OUTPUT_FORMAT_TEXT
&&
1730 text_legacy_opts_is_any_set(text_legacy_opts
)) {
1733 g_string_append(str
, " -p '");
1735 if (g_string_append_string_path_param(str
, "output-path",
1736 text_legacy_opts
->output
)) {
1740 g_string_append(str
, ",");
1742 if (g_string_append_string_path_param(str
, "debug-info-dir",
1743 text_legacy_opts
->dbg_info_dir
)) {
1747 g_string_append(str
, ",");
1749 if (g_string_append_string_path_param(str
,
1750 "debug-info-target-prefix",
1751 text_legacy_opts
->dbg_info_target_prefix
)) {
1755 g_string_append_bool_param(str
, "no-delta",
1756 text_legacy_opts
->no_delta
);
1757 g_string_append_bool_param(str
, "clock-cycles",
1758 text_legacy_opts
->clock_cycles
);
1759 g_string_append_bool_param(str
, "clock-seconds",
1760 text_legacy_opts
->clock_seconds
);
1761 g_string_append_bool_param(str
, "clock-date",
1762 text_legacy_opts
->clock_date
);
1763 g_string_append_bool_param(str
, "clock-gmt",
1764 text_legacy_opts
->clock_gmt
);
1765 g_string_append_bool_param(str
, "verbose",
1766 text_legacy_opts
->verbose
);
1767 ret
= append_prefixed_flag_params(str
, text_legacy_opts
->names
,
1773 ret
= append_prefixed_flag_params(str
, text_legacy_opts
->fields
,
1779 /* Remove last comma and close single quote */
1780 g_string_append(str
, "'");
1783 printf_err("%s\n\n", str
->str
);
1787 g_string_free(str
, TRUE
);
1793 * Prints the non-legacy source options equivalent to the specified
1794 * legacy input format options.
1797 void print_input_legacy_to_sources(enum legacy_input_format legacy_input_format
,
1798 struct bt_value
*legacy_input_paths
,
1799 struct ctf_legacy_opts
*ctf_legacy_opts
)
1801 const char *input_format
;
1802 GString
*str
= NULL
;
1805 str
= g_string_new(" ");
1811 switch (legacy_input_format
) {
1812 case LEGACY_INPUT_FORMAT_CTF
:
1813 input_format
= "ctf";
1815 case LEGACY_INPUT_FORMAT_LTTNG_LIVE
:
1816 input_format
= "lttng-live";
1822 printf_err("Both `%s` legacy input format and non-legacy source component\ninstance(s) specified.\n\n",
1824 printf_err("Specify the following non-legacy source component instance(s) instead of the\nlegacy `%s` input format options and positional arguments:\n\n",
1827 for (i
= 0; i
< bt_value_array_size(legacy_input_paths
); i
++) {
1828 struct bt_value
*input_value
=
1829 bt_value_array_get(legacy_input_paths
, i
);
1830 const char *input
= NULL
;
1831 char *escaped_input
;
1834 assert(input_value
);
1835 ret
= bt_value_string_get(input_value
, &input
);
1836 BT_PUT(input_value
);
1837 assert(!ret
&& input
);
1838 escaped_input
= str_shell_escape(input
);
1839 if (!escaped_input
) {
1844 g_string_append(str
, "-i ctf.");
1846 switch (legacy_input_format
) {
1847 case LEGACY_INPUT_FORMAT_CTF
:
1848 g_string_append(str
, "fs -p 'path=\"");
1850 case LEGACY_INPUT_FORMAT_LTTNG_LIVE
:
1851 g_string_append(str
, "lttng-live -p 'url=\"");
1857 g_string_append(str
, escaped_input
);
1858 g_string_append(str
, "\"");
1859 g_string_append_printf(str
, ",offset-s=%" PRId64
,
1860 ctf_legacy_opts
->offset_s
.value
);
1861 g_string_append_printf(str
, ",offset-ns=%" PRId64
,
1862 ctf_legacy_opts
->offset_ns
.value
);
1863 g_string_append_bool_param(str
, "stream-intersection",
1864 ctf_legacy_opts
->stream_intersection
);
1865 g_string_append(str
, "' ");
1866 g_free(escaped_input
);
1869 printf_err("%s\n\n", str
->str
);
1873 g_string_free(str
, TRUE
);
1879 * Validates a given configuration, with optional legacy input and
1880 * output formats options. Prints useful error messages if anything
1883 * Returns true when the configuration is valid.
1886 bool validate_cfg(struct bt_config
*cfg
,
1887 enum legacy_input_format
*legacy_input_format
,
1888 enum legacy_output_format
*legacy_output_format
,
1889 struct bt_value
*legacy_input_paths
,
1890 struct ctf_legacy_opts
*ctf_legacy_opts
,
1891 struct text_legacy_opts
*text_legacy_opts
)
1893 bool legacy_input
= false;
1894 bool legacy_output
= false;
1896 /* Determine if the input and output should be legacy-style */
1897 if (cfg
->cmd_data
.convert
.print_ctf_metadata
||
1898 *legacy_input_format
!= LEGACY_INPUT_FORMAT_NONE
||
1899 !bt_value_array_is_empty(legacy_input_paths
) ||
1900 ctf_legacy_opts_is_any_set(ctf_legacy_opts
)) {
1901 legacy_input
= true;
1904 if (*legacy_output_format
!= LEGACY_OUTPUT_FORMAT_NONE
||
1905 text_legacy_opts_is_any_set(text_legacy_opts
)) {
1906 legacy_output
= true;
1910 /* If no legacy input format was specified, default to CTF */
1911 if (*legacy_input_format
== LEGACY_INPUT_FORMAT_NONE
) {
1912 *legacy_input_format
= LEGACY_INPUT_FORMAT_CTF
;
1915 /* Make sure at least one input path exists */
1916 if (bt_value_array_is_empty(legacy_input_paths
)) {
1917 switch (*legacy_input_format
) {
1918 case LEGACY_INPUT_FORMAT_CTF
:
1919 printf_err("No input path specified for legacy `ctf` input format\n");
1921 case LEGACY_INPUT_FORMAT_LTTNG_LIVE
:
1922 printf_err("No URL specified for legacy `lttng-live` input format\n");
1930 /* Make sure no non-legacy sources are specified */
1931 if (cfg
->cmd_data
.convert
.sources
->len
!= 0) {
1932 if (cfg
->cmd_data
.convert
.print_ctf_metadata
) {
1933 printf_err("You cannot instantiate a source component with the `ctf-metadata` output format\n");
1935 print_input_legacy_to_sources(
1936 *legacy_input_format
,
1937 legacy_input_paths
, ctf_legacy_opts
);
1945 * Strict rule: if we need to print the CTF metadata, the input
1946 * format must be legacy and CTF. Also there should be no
1947 * other sinks, and no legacy output format.
1949 if (cfg
->cmd_data
.convert
.print_ctf_metadata
) {
1950 if (*legacy_input_format
!= LEGACY_INPUT_FORMAT_CTF
) {
1951 printf_err("The `ctf-metadata` output format requires legacy `ctf` input format\n");
1955 if (bt_value_array_size(legacy_input_paths
) != 1) {
1956 printf_err("You need to specify exactly one path with the `ctf-metadata` output format\n");
1960 if (legacy_output
) {
1961 printf_err("You cannot use another legacy output format with the `ctf-metadata` output format\n");
1965 if (cfg
->cmd_data
.convert
.sinks
->len
!= 0) {
1966 printf_err("You cannot instantiate a sink component with the `ctf-metadata` output format\n");
1970 } else if (legacy_output
) {
1972 * If no legacy output format was specified, default to
1975 if (*legacy_output_format
== LEGACY_OUTPUT_FORMAT_NONE
) {
1976 *legacy_output_format
= LEGACY_OUTPUT_FORMAT_TEXT
;
1980 * If any "text" option was specified, the output must be
1983 if (text_legacy_opts_is_any_set(text_legacy_opts
) &&
1984 *legacy_output_format
!=
1985 LEGACY_OUTPUT_FORMAT_TEXT
) {
1986 printf_err("Options for legacy `text` output format specified with a different legacy output format\n");
1990 /* Make sure no non-legacy sinks are specified */
1991 if (cfg
->cmd_data
.convert
.sinks
->len
!= 0) {
1992 print_output_legacy_to_sinks(*legacy_output_format
,
2005 * Parses a 64-bit signed integer.
2007 * Returns a negative value if anything goes wrong.
2010 int parse_int64(const char *arg
, int64_t *val
)
2015 *val
= strtoll(arg
, &endptr
, 0);
2016 if (*endptr
!= '\0' || arg
== endptr
|| errno
!= 0) {
2030 OPT_CLOCK_FORCE_CORRELATE
,
2033 OPT_CLOCK_OFFSET_NS
,
2038 OPT_DEBUG_INFO_FULL_PATH
,
2039 OPT_DEBUG_INFO_TARGET_PREFIX
,
2049 OPT_OMIT_HOME_PLUGIN_PATH
,
2050 OPT_OMIT_SYSTEM_PLUGIN_PATH
,
2056 OPT_RESET_BASE_PARAMS
,
2059 OPT_STREAM_INTERSECTION
,
2065 * Sets the value of a given legacy offset option and marks it as set.
2067 static void set_offset_value(struct offset_opt
*offset_opt
, int64_t value
)
2069 offset_opt
->value
= value
;
2070 offset_opt
->is_set
= true;
2073 enum bt_config_component_dest
{
2074 BT_CONFIG_COMPONENT_DEST_SOURCE
,
2075 BT_CONFIG_COMPONENT_DEST_SINK
,
2079 * Adds a configuration component to the appropriate configuration
2080 * array depending on the destination.
2082 static void add_cfg_comp(struct bt_config
*cfg
,
2083 struct bt_config_component
*cfg_comp
,
2084 enum bt_config_component_dest dest
)
2086 if (dest
== BT_CONFIG_COMPONENT_DEST_SOURCE
) {
2087 g_ptr_array_add(cfg
->cmd_data
.convert
.sources
, cfg_comp
);
2089 g_ptr_array_add(cfg
->cmd_data
.convert
.sinks
, cfg_comp
);
2093 static int split_timerange(const char *arg
, const char **begin
, const char **end
)
2097 /* Try to match [begin,end] */
2098 c
= strchr(arg
, '[');
2112 /* Try to match begin,end */
2126 static int append_env_var_plugin_paths(struct bt_value
*plugin_paths
)
2131 if (bt_common_is_setuid_setgid()) {
2132 printf_debug("Skipping non-system plugin paths for setuid/setgid binary\n");
2136 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
2141 ret
= bt_config_append_plugin_paths(plugin_paths
, envvar
);
2147 static int append_home_and_system_plugin_paths(struct bt_value
*plugin_paths
,
2148 bool omit_system_plugin_path
, bool omit_home_plugin_path
)
2152 if (!omit_home_plugin_path
) {
2153 if (bt_common_is_setuid_setgid()) {
2154 printf_debug("Skipping non-system plugin paths for setuid/setgid binary\n");
2156 char *home_plugin_dir
=
2157 bt_common_get_home_plugin_path();
2159 if (home_plugin_dir
) {
2160 ret
= bt_config_append_plugin_paths(
2163 free(home_plugin_dir
);
2166 printf_err("Invalid home plugin path\n");
2173 if (!omit_system_plugin_path
) {
2174 if (bt_config_append_plugin_paths(plugin_paths
,
2175 bt_common_get_system_plugin_path())) {
2176 printf_err("Invalid system plugin path\n");
2185 static int append_sources_from_implicit_params(GPtrArray
*sources
,
2186 struct bt_config_component
*implicit_source_comp
)
2189 size_t len
= sources
->len
;
2191 for (i
= 0; i
< len
; i
++) {
2192 struct bt_config_component
*comp
;
2193 struct bt_value
*params_to_set
;
2195 comp
= g_ptr_array_index(sources
, i
);
2196 params_to_set
= bt_value_map_extend(comp
->params
,
2197 implicit_source_comp
->params
);
2198 if (!params_to_set
) {
2199 printf_err("Cannot extend legacy component parameters with non-legacy parameters\n");
2202 BT_MOVE(comp
->params
, params_to_set
);
2209 static struct bt_config
*bt_config_base_create(enum bt_config_command command
)
2211 struct bt_config
*cfg
;
2214 cfg
= g_new0(struct bt_config
, 1);
2220 bt_object_init(cfg
, bt_config_destroy
);
2221 cfg
->command
= command
;
2231 static struct bt_config
*bt_config_convert_create(
2232 struct bt_value
*initial_plugin_paths
)
2234 struct bt_config
*cfg
;
2237 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_CONVERT
);
2243 cfg
->cmd_data
.convert
.sources
= g_ptr_array_new_with_free_func(
2244 (GDestroyNotify
) bt_put
);
2245 if (!cfg
->cmd_data
.convert
.sources
) {
2250 cfg
->cmd_data
.convert
.filters
= g_ptr_array_new_with_free_func(
2251 (GDestroyNotify
) bt_put
);
2252 if (!cfg
->cmd_data
.convert
.filters
) {
2257 cfg
->cmd_data
.convert
.sinks
= g_ptr_array_new_with_free_func(
2258 (GDestroyNotify
) bt_put
);
2259 if (!cfg
->cmd_data
.convert
.sinks
) {
2264 cfg
->cmd_data
.convert
.connections
= g_ptr_array_new_with_free_func(
2265 (GDestroyNotify
) bt_config_connection_destroy
);
2266 if (!cfg
->cmd_data
.convert
.connections
) {
2271 if (initial_plugin_paths
) {
2272 cfg
->cmd_data
.convert
.plugin_paths
=
2273 bt_get(initial_plugin_paths
);
2275 cfg
->cmd_data
.convert
.plugin_paths
= bt_value_array_create();
2276 if (!cfg
->cmd_data
.convert
.plugin_paths
) {
2291 static struct bt_config
*bt_config_list_plugins_create(
2292 struct bt_value
*initial_plugin_paths
)
2294 struct bt_config
*cfg
;
2297 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_LIST_PLUGINS
);
2303 if (initial_plugin_paths
) {
2304 cfg
->cmd_data
.list_plugins
.plugin_paths
=
2305 bt_get(initial_plugin_paths
);
2307 cfg
->cmd_data
.list_plugins
.plugin_paths
=
2308 bt_value_array_create();
2309 if (!cfg
->cmd_data
.list_plugins
.plugin_paths
) {
2324 static struct bt_config
*bt_config_help_create(
2325 struct bt_value
*initial_plugin_paths
)
2327 struct bt_config
*cfg
;
2330 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_HELP
);
2336 if (initial_plugin_paths
) {
2337 cfg
->cmd_data
.help
.plugin_paths
=
2338 bt_get(initial_plugin_paths
);
2340 cfg
->cmd_data
.help
.plugin_paths
=
2341 bt_value_array_create();
2342 if (!cfg
->cmd_data
.help
.plugin_paths
) {
2348 cfg
->cmd_data
.help
.cfg_component
=
2349 bt_config_component_create(BT_COMPONENT_CLASS_TYPE_UNKNOWN
,
2351 if (!cfg
->cmd_data
.help
.cfg_component
) {
2365 static struct bt_config
*bt_config_query_create(
2366 struct bt_value
*initial_plugin_paths
)
2368 struct bt_config
*cfg
;
2371 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_QUERY
);
2377 if (initial_plugin_paths
) {
2378 cfg
->cmd_data
.query
.plugin_paths
=
2379 bt_get(initial_plugin_paths
);
2381 cfg
->cmd_data
.query
.plugin_paths
=
2382 bt_value_array_create();
2383 if (!cfg
->cmd_data
.query
.plugin_paths
) {
2389 cfg
->cmd_data
.query
.object
= g_string_new(NULL
);
2390 if (!cfg
->cmd_data
.query
.object
) {
2405 * Prints the expected format for a --params option.
2408 void print_expected_params_format(FILE *fp
)
2410 fprintf(fp
, "Expected format of PARAMS\n");
2411 fprintf(fp
, "-------------------------\n");
2413 fprintf(fp
, " PARAM=VALUE[,PARAM=VALUE]...\n");
2415 fprintf(fp
, "The parameter string is a comma-separated list of PARAM=VALUE assignments,\n");
2416 fprintf(fp
, "where PARAM is the parameter name (C identifier plus [:.-] characters), and\n");
2417 fprintf(fp
, "VALUE can be one of:\n");
2419 fprintf(fp
, "* `null`, `nul`, `NULL`: null value (no backticks).\n");
2420 fprintf(fp
, "* `true`, `TRUE`, `yes`, `YES`: true boolean value (no backticks).\n");
2421 fprintf(fp
, "* `false`, `FALSE`, `no`, `NO`: false boolean value (no backticks).\n");
2422 fprintf(fp
, "* Binary (`0b` prefix), octal (`0` prefix), decimal, or hexadecimal\n");
2423 fprintf(fp
, " (`0x` prefix) signed 64-bit integer.\n");
2424 fprintf(fp
, "* Double precision floating point number (scientific notation is accepted).\n");
2425 fprintf(fp
, "* Unquoted string with no special characters, and not matching any of\n");
2426 fprintf(fp
, " the null and boolean value symbols above.\n");
2427 fprintf(fp
, "* Double-quoted string (accepts escape characters).\n");
2429 fprintf(fp
, "Whitespaces are allowed around individual `=` and `,` tokens.\n");
2431 fprintf(fp
, "Example:\n");
2433 fprintf(fp
, " many=null, fresh=yes, condition=false, squirrel=-782329,\n");
2434 fprintf(fp
, " observe=3.14, simple=beef, needs-quotes=\"some string\",\n");
2435 fprintf(fp
, " escape.chars-are:allowed=\"this is a \\\" double quote\"\n");
2437 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run babeltrace\n");
2438 fprintf(fp
, "from a shell.\n");
2443 * Prints the help command usage.
2446 void print_help_usage(FILE *fp
)
2448 fprintf(fp
, "Usage: babeltrace [GENERAL OPTIONS] help [OPTIONS] PLUGIN\n");
2449 fprintf(fp
, " babeltrace [GENERAL OPTIONS] help [OPTIONS] --source=PLUGIN.COMPCLS\n");
2450 fprintf(fp
, " babeltrace [GENERAL OPTIONS] help [OPTIONS] --filter=PLUGIN.COMPCLS\n");
2451 fprintf(fp
, " babeltrace [GENERAL OPTIONS] help [OPTIONS] --sink=PLUGIN.COMPCLS\n");
2453 fprintf(fp
, "Options:\n");
2455 fprintf(fp
, " --filter=PLUGIN.COMPCLS Get help for the filter component class\n");
2456 fprintf(fp
, " COMPCLS found in the plugin PLUGIN\n");
2457 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2458 fprintf(fp
, " (~/.local/lib/babeltrace/plugins)\n");
2459 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2460 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2461 fprintf(fp
, " dynamic plugins can be loaded\n");
2462 fprintf(fp
, " --sink=PLUGIN.COMPCLS Get help for the sink component class\n");
2463 fprintf(fp
, " COMPCLS found in the plugin PLUGIN\n");
2464 fprintf(fp
, " --source=PLUGIN.COMPCLS Get help for the source component class\n");
2465 fprintf(fp
, " COMPCLS found in the plugin PLUGIN\n");
2466 fprintf(fp
, " -h --help Show this help and quit\n");
2468 fprintf(fp
, "See `babeltrace --help` for the list of general options.\n");
2470 fprintf(fp
, "Use `babeltrace list-plugins` to show the list of available plugins.\n");
2473 static struct poptOption help_long_options
[] = {
2474 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
2475 { "filter", '\0', POPT_ARG_STRING
, NULL
, OPT_FILTER
, NULL
, NULL
},
2476 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
2477 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
2478 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
2479 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
2480 { "sink", '\0', POPT_ARG_STRING
, NULL
, OPT_SINK
, NULL
, NULL
},
2481 { "source", '\0', POPT_ARG_STRING
, NULL
, OPT_SOURCE
, NULL
, NULL
},
2482 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
2486 * Creates a Babeltrace config object from the arguments of a help
2489 * *retcode is set to the appropriate exit code to use.
2491 struct bt_config
*bt_config_help_from_args(int argc
, const char *argv
[],
2492 int *retcode
, bool omit_system_plugin_path
,
2493 bool omit_home_plugin_path
,
2494 struct bt_value
*initial_plugin_paths
)
2496 poptContext pc
= NULL
;
2500 struct bt_config
*cfg
= NULL
;
2501 const char *leftover
;
2502 char *plugin_name
= NULL
, *component_name
= NULL
;
2503 char *plugin_comp_cls_names
= NULL
;
2506 cfg
= bt_config_help_create(initial_plugin_paths
);
2512 cfg
->cmd_data
.help
.omit_system_plugin_path
= omit_system_plugin_path
;
2513 cfg
->cmd_data
.help
.omit_home_plugin_path
= omit_home_plugin_path
;
2514 ret
= append_env_var_plugin_paths(cfg
->cmd_data
.help
.plugin_paths
);
2516 printf_err("Cannot append plugin paths from BABELTRACE_PLUGIN_PATH\n");
2521 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
2522 help_long_options
, 0);
2524 printf_err("Cannot get popt context\n");
2528 poptReadDefaultConfig(pc
, 0);
2530 while ((opt
= poptGetNextOpt(pc
)) > 0) {
2531 arg
= poptGetOptArg(pc
);
2534 case OPT_PLUGIN_PATH
:
2535 if (bt_common_is_setuid_setgid()) {
2536 printf_debug("Skipping non-system plugin paths for setuid/setgid binary\n");
2538 if (bt_config_append_plugin_paths(
2539 cfg
->cmd_data
.help
.plugin_paths
,
2541 printf_err("Invalid --plugin-path option's argument:\n %s\n",
2547 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
2548 cfg
->cmd_data
.help
.omit_system_plugin_path
= true;
2550 case OPT_OMIT_HOME_PLUGIN_PATH
:
2551 cfg
->cmd_data
.help
.omit_home_plugin_path
= true;
2556 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
2557 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
2558 printf_err("Cannot specify more than one plugin and component class:\n %s\n",
2565 cfg
->cmd_data
.help
.cfg_component
->type
=
2566 BT_COMPONENT_CLASS_TYPE_SOURCE
;
2569 cfg
->cmd_data
.help
.cfg_component
->type
=
2570 BT_COMPONENT_CLASS_TYPE_FILTER
;
2573 cfg
->cmd_data
.help
.cfg_component
->type
=
2574 BT_COMPONENT_CLASS_TYPE_SINK
;
2579 plugin_comp_cls_names
= strdup(arg
);
2580 if (!plugin_comp_cls_names
) {
2586 print_help_usage(stdout
);
2591 printf_err("Unknown command-line option specified (option code %d)\n",
2600 /* Check for option parsing error */
2602 printf_err("While parsing command-line options, at option %s: %s\n",
2603 poptBadOption(pc
, 0), poptStrerror(opt
));
2607 leftover
= poptGetArg(pc
);
2609 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
2610 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
2611 printf_err("Cannot specify plugin name and --source/--filter/--sink component class:\n %s\n",
2616 g_string_assign(cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
2619 if (cfg
->cmd_data
.help
.cfg_component
->type
==
2620 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
2621 print_help_usage(stdout
);
2627 plugin_component_names_from_arg(plugin_comp_cls_names
,
2628 &plugin_name
, &component_name
);
2629 if (plugin_name
&& component_name
) {
2630 g_string_assign(cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
2632 g_string_assign(cfg
->cmd_data
.help
.cfg_component
->component_name
,
2635 printf_err("Invalid --source/--filter/--sink option's argument:\n %s\n",
2636 plugin_comp_cls_names
);
2641 if (append_home_and_system_plugin_paths(
2642 cfg
->cmd_data
.help
.plugin_paths
,
2643 cfg
->cmd_data
.help
.omit_system_plugin_path
,
2644 cfg
->cmd_data
.help
.omit_home_plugin_path
)) {
2645 printf_err("Cannot append home and system plugin paths\n");
2656 free(plugin_comp_cls_names
);
2657 g_free(plugin_name
);
2658 g_free(component_name
);
2661 poptFreeContext(pc
);
2669 * Prints the help command usage.
2672 void print_query_usage(FILE *fp
)
2674 fprintf(fp
, "Usage: babeltrace [GEN OPTS] query [OPTS] OBJECT --source=PLUGIN.COMPCLS\n");
2675 fprintf(fp
, " babeltrace [GEN OPTS] query [OPTS] OBJECT --filter=PLUGIN.COMPCLS\n");
2676 fprintf(fp
, " babeltrace [GEN OPTS] query [OPTS] OBJECT --sink=PLUGIN.COMPCLS\n");
2678 fprintf(fp
, "Options:\n");
2680 fprintf(fp
, " --filter=PLUGIN.COMPCLS Query object from the filter component\n");
2681 fprintf(fp
, " class COMPCLS found in the plugin PLUGIN\n");
2682 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2683 fprintf(fp
, " (~/.local/lib/babeltrace/plugins)\n");
2684 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2685 fprintf(fp
, " -p, --params=PARAMS Set the query parameters to PARAMS\n");
2686 fprintf(fp
, " (see the expected format of PARAMS below)\n");
2687 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2688 fprintf(fp
, " dynamic plugins can be loaded\n");
2689 fprintf(fp
, " --sink=PLUGIN.COMPCLS Query object from the sink component class\n");
2690 fprintf(fp
, " COMPCLS found in the plugin PLUGIN\n");
2691 fprintf(fp
, " --source=PLUGIN.COMPCLS Query object from the source component\n");
2692 fprintf(fp
, " class COMPCLS found in the plugin PLUGIN\n");
2693 fprintf(fp
, " -h --help Show this help and quit\n");
2694 fprintf(fp
, "\n\n");
2695 print_expected_params_format(fp
);
2698 static struct poptOption query_long_options
[] = {
2699 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
2700 { "filter", '\0', POPT_ARG_STRING
, NULL
, OPT_FILTER
, NULL
, NULL
},
2701 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
2702 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
2703 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
2704 { "params", 'p', POPT_ARG_STRING
, NULL
, OPT_PARAMS
, NULL
, NULL
},
2705 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
2706 { "sink", '\0', POPT_ARG_STRING
, NULL
, OPT_SINK
, NULL
, NULL
},
2707 { "source", '\0', POPT_ARG_STRING
, NULL
, OPT_SOURCE
, NULL
, NULL
},
2708 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
2712 * Creates a Babeltrace config object from the arguments of a query
2715 * *retcode is set to the appropriate exit code to use.
2717 struct bt_config
*bt_config_query_from_args(int argc
, const char *argv
[],
2718 int *retcode
, bool omit_system_plugin_path
,
2719 bool omit_home_plugin_path
,
2720 struct bt_value
*initial_plugin_paths
)
2722 poptContext pc
= NULL
;
2726 struct bt_config
*cfg
= NULL
;
2727 const char *leftover
;
2728 struct bt_value
*params
= bt_value_null
;
2731 cfg
= bt_config_query_create(initial_plugin_paths
);
2737 cfg
->cmd_data
.query
.omit_system_plugin_path
=
2738 omit_system_plugin_path
;
2739 cfg
->cmd_data
.query
.omit_home_plugin_path
= omit_home_plugin_path
;
2740 ret
= append_env_var_plugin_paths(cfg
->cmd_data
.query
.plugin_paths
);
2742 printf_err("Cannot append plugin paths from BABELTRACE_PLUGIN_PATH\n");
2747 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
2748 query_long_options
, 0);
2750 printf_err("Cannot get popt context\n");
2754 poptReadDefaultConfig(pc
, 0);
2756 while ((opt
= poptGetNextOpt(pc
)) > 0) {
2757 arg
= poptGetOptArg(pc
);
2760 case OPT_PLUGIN_PATH
:
2761 if (bt_common_is_setuid_setgid()) {
2762 printf_debug("Skipping non-system plugin paths for setuid/setgid binary\n");
2764 if (bt_config_append_plugin_paths(
2765 cfg
->cmd_data
.query
.plugin_paths
,
2767 printf_err("Invalid --plugin-path option's argument:\n %s\n",
2773 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
2774 cfg
->cmd_data
.query
.omit_system_plugin_path
= true;
2776 case OPT_OMIT_HOME_PLUGIN_PATH
:
2777 cfg
->cmd_data
.query
.omit_home_plugin_path
= true;
2783 enum bt_component_class_type type
;
2785 if (cfg
->cmd_data
.query
.cfg_component
) {
2786 printf_err("Cannot specify more than one plugin and component class:\n %s\n",
2793 type
= BT_COMPONENT_CLASS_TYPE_SOURCE
;
2796 type
= BT_COMPONENT_CLASS_TYPE_FILTER
;
2799 type
= BT_COMPONENT_CLASS_TYPE_SINK
;
2805 cfg
->cmd_data
.query
.cfg_component
=
2806 bt_config_component_from_arg(type
, arg
);
2807 if (!cfg
->cmd_data
.query
.cfg_component
) {
2808 printf_err("Invalid format for --source/--filter/--sink option's argument:\n %s\n",
2813 /* Default parameters: null */
2814 bt_put(cfg
->cmd_data
.query
.cfg_component
->params
);
2815 cfg
->cmd_data
.query
.cfg_component
->params
=
2821 params
= bt_value_from_arg(arg
);
2823 printf_err("Invalid format for --params option's argument:\n %s\n",
2830 print_query_usage(stdout
);
2835 printf_err("Unknown command-line option specified (option code %d)\n",
2844 if (!cfg
->cmd_data
.query
.cfg_component
) {
2845 printf_err("No target component class specified with --source/--filter/--sink option\n");
2850 BT_MOVE(cfg
->cmd_data
.query
.cfg_component
->params
, params
);
2852 /* Check for option parsing error */
2854 printf_err("While parsing command-line options, at option %s: %s\n",
2855 poptBadOption(pc
, 0), poptStrerror(opt
));
2860 * We need exactly one leftover argument which is the
2863 leftover
= poptGetArg(pc
);
2865 if (strlen(leftover
) == 0) {
2866 printf_err("Invalid empty object\n");
2870 g_string_assign(cfg
->cmd_data
.query
.object
, leftover
);
2872 print_query_usage(stdout
);
2878 leftover
= poptGetArg(pc
);
2880 printf_err("Invalid argument: %s\n", leftover
);
2884 if (append_home_and_system_plugin_paths(
2885 cfg
->cmd_data
.query
.plugin_paths
,
2886 cfg
->cmd_data
.query
.omit_system_plugin_path
,
2887 cfg
->cmd_data
.query
.omit_home_plugin_path
)) {
2888 printf_err("Cannot append home and system plugin paths\n");
2900 poptFreeContext(pc
);
2909 * Prints the list-plugins command usage.
2912 void print_list_plugins_usage(FILE *fp
)
2914 fprintf(fp
, "Usage: babeltrace [GENERAL OPTIONS] list-plugins [OPTIONS]\n");
2916 fprintf(fp
, "Options:\n");
2918 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2919 fprintf(fp
, " (~/.local/lib/babeltrace/plugins)\n");
2920 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2921 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2922 fprintf(fp
, " dynamic plugins can be loaded\n");
2923 fprintf(fp
, " -h --help Show this help and quit\n");
2925 fprintf(fp
, "See `babeltrace --help` for the list of general options.\n");
2927 fprintf(fp
, "Use `babeltrace help` to get help for a specific plugin or component class.\n");
2930 static struct poptOption list_plugins_long_options
[] = {
2931 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
2932 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
2933 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
2934 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
2935 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
2936 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
2940 * Creates a Babeltrace config object from the arguments of a
2941 * list-plugins command.
2943 * *retcode is set to the appropriate exit code to use.
2945 struct bt_config
*bt_config_list_plugins_from_args(int argc
, const char *argv
[],
2946 int *retcode
, bool omit_system_plugin_path
,
2947 bool omit_home_plugin_path
,
2948 struct bt_value
*initial_plugin_paths
)
2950 poptContext pc
= NULL
;
2954 struct bt_config
*cfg
= NULL
;
2955 const char *leftover
;
2958 cfg
= bt_config_list_plugins_create(initial_plugin_paths
);
2964 cfg
->cmd_data
.list_plugins
.omit_system_plugin_path
= omit_system_plugin_path
;
2965 cfg
->cmd_data
.list_plugins
.omit_home_plugin_path
= omit_home_plugin_path
;
2966 ret
= append_env_var_plugin_paths(
2967 cfg
->cmd_data
.list_plugins
.plugin_paths
);
2969 printf_err("Cannot append plugin paths from BABELTRACE_PLUGIN_PATH\n");
2974 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
2975 list_plugins_long_options
, 0);
2977 printf_err("Cannot get popt context\n");
2981 poptReadDefaultConfig(pc
, 0);
2983 while ((opt
= poptGetNextOpt(pc
)) > 0) {
2984 arg
= poptGetOptArg(pc
);
2987 case OPT_PLUGIN_PATH
:
2988 if (bt_common_is_setuid_setgid()) {
2989 printf_debug("Skipping non-system plugin paths for setuid/setgid binary\n");
2991 if (bt_config_append_plugin_paths(
2992 cfg
->cmd_data
.list_plugins
.plugin_paths
,
2994 printf_err("Invalid --plugin-path option's argument:\n %s\n",
3000 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
3001 cfg
->cmd_data
.list_plugins
.omit_system_plugin_path
= true;
3003 case OPT_OMIT_HOME_PLUGIN_PATH
:
3004 cfg
->cmd_data
.list_plugins
.omit_home_plugin_path
= true;
3007 print_list_plugins_usage(stdout
);
3012 printf_err("Unknown command-line option specified (option code %d)\n",
3021 /* Check for option parsing error */
3023 printf_err("While parsing command-line options, at option %s: %s\n",
3024 poptBadOption(pc
, 0), poptStrerror(opt
));
3028 leftover
= poptGetArg(pc
);
3030 printf_err("Invalid argument: %s\n", leftover
);
3034 if (append_home_and_system_plugin_paths(
3035 cfg
->cmd_data
.list_plugins
.plugin_paths
,
3036 cfg
->cmd_data
.list_plugins
.omit_system_plugin_path
,
3037 cfg
->cmd_data
.list_plugins
.omit_home_plugin_path
)) {
3038 printf_err("Cannot append home and system plugin paths\n");
3050 poptFreeContext(pc
);
3058 * Prints the legacy, Babeltrace 1.x command usage. Those options are
3059 * still compatible in Babeltrace 2.x, but it is recommended to use
3060 * the more generic plugin/component parameters instead of those
3061 * hard-coded option names.
3064 void print_legacy_usage(FILE *fp
)
3066 fprintf(fp
, "Usage: babeltrace [OPTIONS] INPUT...\n");
3068 fprintf(fp
, "The following options are compatible with the Babeltrace 1.x options:\n");
3070 fprintf(fp
, " --clock-force-correlate Assume that clocks are inherently correlated\n");
3071 fprintf(fp
, " across traces\n");
3072 fprintf(fp
, " -d, --debug Enable debug mode\n");
3073 fprintf(fp
, " -i, --input-format=FORMAT Input trace format (default: ctf)\n");
3074 fprintf(fp
, " -l, --list List available formats\n");
3075 fprintf(fp
, " -o, --output-format=FORMAT Output trace format (default: text)\n");
3076 fprintf(fp
, " -v, --verbose Enable verbose output\n");
3077 fprintf(fp
, " --help-legacy Show this help and quit\n");
3078 fprintf(fp
, " -V, --version Show version and quit\n");
3080 fprintf(fp
, " Available input formats: ctf, lttng-live, ctf-metadata\n");
3081 fprintf(fp
, " Available output formats: text, dummy\n");
3083 fprintf(fp
, "Input formats specific options:\n");
3085 fprintf(fp
, " INPUT... Input trace file(s), directory(ies), or URLs\n");
3086 fprintf(fp
, " --clock-offset=SEC Set clock offset to SEC seconds\n");
3087 fprintf(fp
, " --clock-offset-ns=NS Set clock offset to NS nanoseconds\n");
3088 fprintf(fp
, " --stream-intersection Only process events when all streams are active\n");
3090 fprintf(fp
, "text output format specific options:\n");
3092 fprintf(fp
, " --clock-cycles Print timestamps in clock cycles\n");
3093 fprintf(fp
, " --clock-date Print timestamp dates\n");
3094 fprintf(fp
, " --clock-gmt Print and parse timestamps in GMT time zone\n");
3095 fprintf(fp
, " (default: local time zone)\n");
3096 fprintf(fp
, " --clock-seconds Print the timestamps as [SEC.NS]\n");
3097 fprintf(fp
, " (default format: [HH:MM:SS.NS])\n");
3098 fprintf(fp
, " --debug-info-dir=DIR Search for debug info in directory DIR\n");
3099 fprintf(fp
, " (default: `/usr/lib/debug`)\n");
3100 fprintf(fp
, " --debug-info-full-path Show full debug info source and binary paths\n");
3101 fprintf(fp
, " --debug-info-target-prefix=DIR Use directory DIR as a prefix when looking\n");
3102 fprintf(fp
, " up executables during debug info analysis\n");
3103 fprintf(fp
, " (default: `/usr/lib/debug`)\n");
3104 fprintf(fp
, " -f, --fields=NAME[,NAME]... Print additional fields:\n");
3105 fprintf(fp
, " all, trace, trace:hostname, trace:domain,\n");
3106 fprintf(fp
, " trace:procname, trace:vpid, loglevel, emf\n");
3107 fprintf(fp
, " (default: trace:hostname, trace:procname,\n");
3108 fprintf(fp
, " trace:vpid)\n");
3109 fprintf(fp
, " -n, --names=NAME[,NAME]... Print field names:\n");
3110 fprintf(fp
, " payload (or arg or args)\n");
3111 fprintf(fp
, " none, all, scope, header, context (or ctx)\n");
3112 fprintf(fp
, " (default: payload, context)\n");
3113 fprintf(fp
, " --no-delta Do not print time delta between consecutive\n");
3114 fprintf(fp
, " events\n");
3115 fprintf(fp
, " -w, --output=PATH Write output to PATH (default: standard output)\n");
3119 * Prints the convert command usage.
3122 void print_convert_usage(FILE *fp
)
3124 fprintf(fp
, "Usage: babeltrace [GENERAL OPTIONS] convert [OPTIONS]\n");
3126 fprintf(fp
, "Options:\n");
3128 fprintf(fp
, " -b, --base-params=PARAMS Set PARAMS as the current base parameters\n");
3129 fprintf(fp
, " for the following component instances\n");
3130 fprintf(fp
, " (see the expected format of PARAMS below)\n");
3131 fprintf(fp
, " --begin=BEGIN Set the `begin` parameter of the latest\n");
3132 fprintf(fp
, " source component instance to BEGIN\n");
3133 fprintf(fp
, " (see the suggested format of BEGIN below)\n");
3134 fprintf(fp
, " -c, --connect=CONNECTION Connect two component instances (see the\n");
3135 fprintf(fp
, " expected format of CONNECTION below)\n");
3136 fprintf(fp
, " -d, --debug Enable debug mode\n");
3137 fprintf(fp
, " --end=END Set the `end` parameter of the latest\n");
3138 fprintf(fp
, " source component instance to END\n");
3139 fprintf(fp
, " (see the suggested format of BEGIN below)\n");
3140 fprintf(fp
, " --name=NAME Set the name of the latest component\n");
3141 fprintf(fp
, " instance to NAME (must be unique amongst\n");
3142 fprintf(fp
, " all the names of the component instances)\n");
3143 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
3144 fprintf(fp
, " (~/.local/lib/babeltrace/plugins)\n");
3145 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
3146 fprintf(fp
, " -p, --params=PARAMS Set the parameters of the latest component\n");
3147 fprintf(fp
, " instance (in command-line order) to PARAMS\n");
3148 fprintf(fp
, " (see the expected format of PARAMS below)\n");
3149 fprintf(fp
, " -P, --path=PATH Set the `path` parameter of the latest\n");
3150 fprintf(fp
, " component instance to PATH\n");
3151 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
3152 fprintf(fp
, " dynamic plugins can be loaded\n");
3153 fprintf(fp
, " -r, --reset-base-params Reset the current base parameters of the\n");
3154 fprintf(fp
, " following source and sink component\n");
3155 fprintf(fp
, " instances to an empty map\n");
3156 fprintf(fp
, " -o, --sink=PLUGIN.COMPCLS Instantiate a sink component from plugin\n");
3157 fprintf(fp
, " PLUGIN and component class COMPCLS (may be\n");
3158 fprintf(fp
, " repeated)\n");
3159 fprintf(fp
, " -i, --source=PLUGIN.COMPCLS Instantiate a source component from plugin\n");
3160 fprintf(fp
, " PLUGIN and component class COMPCLS (may be\n");
3161 fprintf(fp
, " repeated)\n");
3162 fprintf(fp
, " --timerange=TIMERANGE Set time range to TIMERANGE: BEGIN,END or\n");
3163 fprintf(fp
, " [BEGIN,END] (literally `[` and `]`)\n");
3164 fprintf(fp
, " (suggested format of BEGIN/END below)\n");
3165 fprintf(fp
, " -v, --verbose Enable verbose output\n");
3166 fprintf(fp
, " -h --help Show this help and quit\n");
3168 fprintf(fp
, "See `babeltrace --help` for the list of general options.\n");
3169 fprintf(fp
, "\n\n");
3170 fprintf(fp
, "Suggested format of BEGIN and END\n");
3171 fprintf(fp
, "---------------------------------\n");
3173 fprintf(fp
, " [YYYY-MM-DD [hh:mm:]]ss[.nnnnnnnnn]\n");
3174 fprintf(fp
, "\n\n");
3175 fprintf(fp
, "Expected format of CONNECTION\n");
3176 fprintf(fp
, "-----------------------------\n");
3178 fprintf(fp
, " SRC[.SRCPORT]:DST[.DSTPORT]\n");
3180 fprintf(fp
, "SRC and DST are the names of the source and destination component\n");
3181 fprintf(fp
, "instances to connect together. You can set the name of a component\n");
3182 fprintf(fp
, "instance with the --name option.\n");
3184 fprintf(fp
, "SRCPORT and DSTPORT are the optional source and destination ports to use\n");
3185 fprintf(fp
, "for the connection. When the port is not specified, the default port is\n");
3186 fprintf(fp
, "used.\n");
3188 fprintf(fp
, "You can connect a source component to a filter or sink component. You\n");
3189 fprintf(fp
, "can connect a filter component to a sink component.\n");
3191 fprintf(fp
, "Example:\n");
3193 fprintf(fp
, " my-filter.top10:json-out\n");
3194 fprintf(fp
, "\n\n");
3195 print_expected_params_format(fp
);
3198 static struct poptOption convert_long_options
[] = {
3199 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
3200 { "base-params", 'b', POPT_ARG_STRING
, NULL
, OPT_BASE_PARAMS
, NULL
, NULL
},
3201 { "begin", '\0', POPT_ARG_STRING
, NULL
, OPT_BEGIN
, NULL
, NULL
},
3202 { "clock-cycles", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_CYCLES
, NULL
, NULL
},
3203 { "clock-date", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_DATE
, NULL
, NULL
},
3204 { "clock-force-correlate", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_FORCE_CORRELATE
, NULL
, NULL
},
3205 { "clock-gmt", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_GMT
, NULL
, NULL
},
3206 { "clock-offset", '\0', POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET
, NULL
, NULL
},
3207 { "clock-offset-ns", '\0', POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET_NS
, NULL
, NULL
},
3208 { "clock-seconds", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_SECONDS
, NULL
, NULL
},
3209 { "connect", 'c', POPT_ARG_STRING
, NULL
, OPT_CONNECT
, NULL
, NULL
},
3210 { "debug", 'd', POPT_ARG_NONE
, NULL
, OPT_DEBUG
, NULL
, NULL
},
3211 { "debug-info-dir", 0, POPT_ARG_STRING
, NULL
, OPT_DEBUG_INFO_DIR
, NULL
, NULL
},
3212 { "debug-info-full-path", 0, POPT_ARG_NONE
, NULL
, OPT_DEBUG_INFO_FULL_PATH
, NULL
, NULL
},
3213 { "debug-info-target-prefix", 0, POPT_ARG_STRING
, NULL
, OPT_DEBUG_INFO_TARGET_PREFIX
, NULL
, NULL
},
3214 { "end", '\0', POPT_ARG_STRING
, NULL
, OPT_END
, NULL
, NULL
},
3215 { "fields", 'f', POPT_ARG_STRING
, NULL
, OPT_FIELDS
, NULL
, NULL
},
3216 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
3217 { "input-format", 'i', POPT_ARG_STRING
, NULL
, OPT_INPUT_FORMAT
, NULL
, NULL
},
3218 { "name", '\0', POPT_ARG_STRING
, NULL
, OPT_NAME
, NULL
, NULL
},
3219 { "names", 'n', POPT_ARG_STRING
, NULL
, OPT_NAMES
, NULL
, NULL
},
3220 { "no-delta", '\0', POPT_ARG_NONE
, NULL
, OPT_NO_DELTA
, NULL
, NULL
},
3221 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
3222 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
3223 { "output", 'w', POPT_ARG_STRING
, NULL
, OPT_OUTPUT_PATH
, NULL
, NULL
},
3224 { "output-format", 'o', POPT_ARG_STRING
, NULL
, OPT_OUTPUT_FORMAT
, NULL
, NULL
},
3225 { "params", 'p', POPT_ARG_STRING
, NULL
, OPT_PARAMS
, NULL
, NULL
},
3226 { "path", 'P', POPT_ARG_STRING
, NULL
, OPT_PATH
, NULL
, NULL
},
3227 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
3228 { "reset-base-params", 'r', POPT_ARG_NONE
, NULL
, OPT_RESET_BASE_PARAMS
, NULL
, NULL
},
3229 { "sink", '\0', POPT_ARG_STRING
, NULL
, OPT_SINK
, NULL
, NULL
},
3230 { "source", '\0', POPT_ARG_STRING
, NULL
, OPT_SOURCE
, NULL
, NULL
},
3231 { "stream-intersection", '\0', POPT_ARG_NONE
, NULL
, OPT_STREAM_INTERSECTION
, NULL
, NULL
},
3232 { "timerange", '\0', POPT_ARG_STRING
, NULL
, OPT_TIMERANGE
, NULL
, NULL
},
3233 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
3234 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
3238 * Creates a Babeltrace config object from the arguments of a convert
3241 * *retcode is set to the appropriate exit code to use.
3243 struct bt_config
*bt_config_convert_from_args(int argc
, const char *argv
[],
3244 int *retcode
, bool omit_system_plugin_path
,
3245 bool omit_home_plugin_path
,
3246 struct bt_value
*initial_plugin_paths
)
3248 poptContext pc
= NULL
;
3250 struct ctf_legacy_opts ctf_legacy_opts
;
3251 struct text_legacy_opts text_legacy_opts
;
3252 enum legacy_input_format legacy_input_format
= LEGACY_INPUT_FORMAT_NONE
;
3253 enum legacy_output_format legacy_output_format
=
3254 LEGACY_OUTPUT_FORMAT_NONE
;
3255 struct bt_value
*legacy_input_paths
= NULL
;
3256 struct bt_config_component
*implicit_source_comp
= NULL
;
3257 struct bt_config_component
*cur_cfg_comp
= NULL
;
3258 bool cur_is_implicit_source
= false;
3259 bool use_implicit_source
= false;
3260 enum bt_config_component_dest cur_cfg_comp_dest
=
3261 BT_CONFIG_COMPONENT_DEST_SOURCE
;
3262 struct bt_value
*cur_base_params
= NULL
;
3264 struct bt_config
*cfg
= NULL
;
3265 struct bt_value
*instance_names
= NULL
;
3266 struct bt_value
*connection_args
= NULL
;
3267 char error_buf
[256] = { 0 };
3270 memset(&ctf_legacy_opts
, 0, sizeof(ctf_legacy_opts
));
3271 memset(&text_legacy_opts
, 0, sizeof(text_legacy_opts
));
3274 print_convert_usage(stdout
);
3279 cfg
= bt_config_convert_create(initial_plugin_paths
);
3285 cfg
->cmd_data
.convert
.omit_system_plugin_path
= omit_system_plugin_path
;
3286 cfg
->cmd_data
.convert
.omit_home_plugin_path
= omit_home_plugin_path
;
3287 text_legacy_opts
.output
= g_string_new(NULL
);
3288 if (!text_legacy_opts
.output
) {
3293 text_legacy_opts
.dbg_info_dir
= g_string_new(NULL
);
3294 if (!text_legacy_opts
.dbg_info_dir
) {
3299 text_legacy_opts
.dbg_info_target_prefix
= g_string_new(NULL
);
3300 if (!text_legacy_opts
.dbg_info_target_prefix
) {
3305 cur_base_params
= bt_value_map_create();
3306 if (!cur_base_params
) {
3311 legacy_input_paths
= bt_value_array_create();
3312 if (!legacy_input_paths
) {
3317 instance_names
= bt_value_map_create();
3318 if (!instance_names
) {
3323 connection_args
= bt_value_array_create();
3324 if (!connection_args
) {
3329 ret
= append_env_var_plugin_paths(cfg
->cmd_data
.convert
.plugin_paths
);
3331 printf_err("Cannot append plugin paths from BABELTRACE_PLUGIN_PATH\n");
3335 /* Note: implicit source never gets positional base params. */
3336 implicit_source_comp
= bt_config_component_from_arg(
3337 BT_COMPONENT_CLASS_TYPE_SOURCE
, DEFAULT_SOURCE_COMPONENT_NAME
);
3338 if (!implicit_source_comp
) {
3343 cur_cfg_comp
= implicit_source_comp
;
3344 cur_is_implicit_source
= true;
3345 use_implicit_source
= true;
3348 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
3349 convert_long_options
, 0);
3351 printf_err("Cannot get popt context\n");
3355 poptReadDefaultConfig(pc
, 0);
3357 while ((opt
= poptGetNextOpt(pc
)) > 0) {
3358 arg
= poptGetOptArg(pc
);
3361 case OPT_PLUGIN_PATH
:
3362 if (bt_common_is_setuid_setgid()) {
3363 printf_debug("Skipping non-system plugin paths for setuid/setgid binary\n");
3365 if (bt_config_append_plugin_paths(
3366 cfg
->cmd_data
.convert
.plugin_paths
,
3368 printf_err("Invalid --plugin-path option's argument:\n %s\n",
3374 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
3375 cfg
->cmd_data
.convert
.omit_system_plugin_path
= true;
3377 case OPT_OMIT_HOME_PLUGIN_PATH
:
3378 cfg
->cmd_data
.convert
.omit_home_plugin_path
= true;
3380 case OPT_OUTPUT_PATH
:
3381 if (text_legacy_opts
.output
->len
> 0) {
3382 printf_err("Duplicate --output option\n");
3386 g_string_assign(text_legacy_opts
.output
, arg
);
3388 case OPT_DEBUG_INFO_DIR
:
3389 if (text_legacy_opts
.dbg_info_dir
->len
> 0) {
3390 printf_err("Duplicate --debug-info-dir option\n");
3394 g_string_assign(text_legacy_opts
.dbg_info_dir
, arg
);
3396 case OPT_DEBUG_INFO_TARGET_PREFIX
:
3397 if (text_legacy_opts
.dbg_info_target_prefix
->len
> 0) {
3398 printf_err("Duplicate --debug-info-target-prefix option\n");
3402 g_string_assign(text_legacy_opts
.dbg_info_target_prefix
, arg
);
3404 case OPT_INPUT_FORMAT
:
3407 if (opt
== OPT_INPUT_FORMAT
) {
3408 if (!strcmp(arg
, "ctf")) {
3409 /* Legacy CTF input format */
3410 if (legacy_input_format
) {
3411 print_err_dup_legacy_input();
3415 legacy_input_format
=
3416 LEGACY_INPUT_FORMAT_CTF
;
3418 } else if (!strcmp(arg
, "lttng-live")) {
3419 /* Legacy LTTng-live input format */
3420 if (legacy_input_format
) {
3421 print_err_dup_legacy_input();
3425 legacy_input_format
=
3426 LEGACY_INPUT_FORMAT_LTTNG_LIVE
;
3431 use_implicit_source
= false;
3433 /* Non-legacy: try to create a component config */
3434 if (cur_cfg_comp
&& !cur_is_implicit_source
) {
3435 add_cfg_comp(cfg
, cur_cfg_comp
,
3439 cur_cfg_comp
= bt_config_component_from_arg(
3440 BT_COMPONENT_CLASS_TYPE_SOURCE
, arg
);
3441 if (!cur_cfg_comp
) {
3442 printf_err("Invalid format for --source option's argument:\n %s\n",
3446 cur_is_implicit_source
= false;
3448 assert(cur_base_params
);
3449 bt_put(cur_cfg_comp
->params
);
3450 cur_cfg_comp
->params
= bt_value_copy(cur_base_params
);
3451 if (!cur_cfg_comp
->params
) {
3456 cur_cfg_comp_dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
3459 case OPT_OUTPUT_FORMAT
:
3462 if (opt
== OPT_OUTPUT_FORMAT
) {
3463 if (!strcmp(arg
, "text")) {
3464 /* Legacy CTF-text output format */
3465 if (legacy_output_format
) {
3466 print_err_dup_legacy_output();
3470 legacy_output_format
=
3471 LEGACY_OUTPUT_FORMAT_TEXT
;
3473 } else if (!strcmp(arg
, "dummy")) {
3474 /* Legacy dummy output format */
3475 if (legacy_output_format
) {
3476 print_err_dup_legacy_output();
3480 legacy_output_format
=
3481 LEGACY_OUTPUT_FORMAT_DUMMY
;
3483 } else if (!strcmp(arg
, "ctf-metadata")) {
3484 cfg
->cmd_data
.convert
.print_ctf_metadata
= true;
3489 /* Non-legacy: try to create a component config */
3490 if (cur_cfg_comp
&& !cur_is_implicit_source
) {
3491 add_cfg_comp(cfg
, cur_cfg_comp
,
3495 cur_cfg_comp
= bt_config_component_from_arg(
3496 BT_COMPONENT_CLASS_TYPE_SINK
, arg
);
3497 if (!cur_cfg_comp
) {
3498 printf_err("Invalid format for --sink option's argument:\n %s\n",
3502 cur_is_implicit_source
= false;
3504 assert(cur_base_params
);
3505 bt_put(cur_cfg_comp
->params
);
3506 cur_cfg_comp
->params
= bt_value_copy(cur_base_params
);
3507 if (!cur_cfg_comp
->params
) {
3512 cur_cfg_comp_dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
3517 struct bt_value
*params
;
3518 struct bt_value
*params_to_set
;
3520 if (!cur_cfg_comp
) {
3521 printf_err("Cannot add parameters to unavailable default source component `%s`:\n %s\n",
3522 DEFAULT_SOURCE_COMPONENT_NAME
, arg
);
3526 params
= bt_value_from_arg(arg
);
3528 printf_err("Invalid format for --params option's argument:\n %s\n",
3533 params_to_set
= bt_value_map_extend(cur_cfg_comp
->params
,
3536 if (!params_to_set
) {
3537 printf_err("Cannot extend current component parameters with --params option's argument:\n %s\n",
3542 BT_MOVE(cur_cfg_comp
->params
, params_to_set
);
3546 if (!cur_cfg_comp
) {
3547 printf_err("Cannot add `path` parameter to unavailable default source component `%s`:\n %s\n",
3548 DEFAULT_SOURCE_COMPONENT_NAME
, arg
);
3552 assert(cur_cfg_comp
->params
);
3554 if (bt_value_map_insert_string(cur_cfg_comp
->params
,
3561 if (!cur_cfg_comp
) {
3562 printf_err("Cannot set the name of unavailable default source component `%s`:\n %s\n",
3563 DEFAULT_SOURCE_COMPONENT_NAME
, arg
);
3567 if (bt_value_map_has_key(instance_names
, arg
)) {
3568 printf_err("Duplicate component instance name:\n %s\n",
3573 g_string_assign(cur_cfg_comp
->instance_name
, arg
);
3575 if (bt_value_map_insert(instance_names
,
3576 arg
, bt_value_null
)) {
3581 case OPT_BASE_PARAMS
:
3583 struct bt_value
*params
= bt_value_from_arg(arg
);
3586 printf_err("Invalid format for --base-params option's argument:\n %s\n",
3591 BT_MOVE(cur_base_params
, params
);
3594 case OPT_RESET_BASE_PARAMS
:
3595 BT_PUT(cur_base_params
);
3596 cur_base_params
= bt_value_map_create();
3597 if (!cur_base_params
) {
3603 if (text_legacy_opts
.names
) {
3604 printf_err("Duplicate --names option\n");
3608 text_legacy_opts
.names
= names_from_arg(arg
);
3609 if (!text_legacy_opts
.names
) {
3610 printf_err("Invalid --names option's argument:\n %s\n",
3616 if (text_legacy_opts
.fields
) {
3617 printf_err("Duplicate --fields option\n");
3621 text_legacy_opts
.fields
= fields_from_arg(arg
);
3622 if (!text_legacy_opts
.fields
) {
3623 printf_err("Invalid --fields option's argument:\n %s\n",
3629 text_legacy_opts
.no_delta
= true;
3631 case OPT_CLOCK_CYCLES
:
3632 text_legacy_opts
.clock_cycles
= true;
3634 case OPT_CLOCK_SECONDS
:
3635 text_legacy_opts
.clock_seconds
= true;
3637 case OPT_CLOCK_DATE
:
3638 text_legacy_opts
.clock_date
= true;
3641 text_legacy_opts
.clock_gmt
= true;
3643 case OPT_DEBUG_INFO_FULL_PATH
:
3644 text_legacy_opts
.dbg_info_full_path
= true;
3646 case OPT_CLOCK_OFFSET
:
3650 if (ctf_legacy_opts
.offset_s
.is_set
) {
3651 printf_err("Duplicate --clock-offset option\n");
3655 if (parse_int64(arg
, &val
)) {
3656 printf_err("Invalid --clock-offset option's argument:\n %s\n",
3661 set_offset_value(&ctf_legacy_opts
.offset_s
, val
);
3664 case OPT_CLOCK_OFFSET_NS
:
3668 if (ctf_legacy_opts
.offset_ns
.is_set
) {
3669 printf_err("Duplicate --clock-offset-ns option\n");
3673 if (parse_int64(arg
, &val
)) {
3674 printf_err("Invalid --clock-offset-ns option's argument:\n %s\n",
3679 set_offset_value(&ctf_legacy_opts
.offset_ns
, val
);
3682 case OPT_STREAM_INTERSECTION
:
3683 ctf_legacy_opts
.stream_intersection
= true;
3685 case OPT_CLOCK_FORCE_CORRELATE
:
3686 cfg
->cmd_data
.convert
.force_correlate
= true;
3689 if (!cur_cfg_comp
) {
3690 printf_err("Cannot add `begin` parameter to unavailable default source component `%s`:\n %s\n",
3691 DEFAULT_SOURCE_COMPONENT_NAME
, arg
);
3694 if (cur_cfg_comp_dest
!= BT_CONFIG_COMPONENT_DEST_SOURCE
) {
3695 printf_err("--begin option must follow a --source option:\n %s\n",
3699 if (bt_value_map_insert_string(cur_cfg_comp
->params
,
3706 if (!cur_cfg_comp
) {
3707 printf_err("Cannot add `end` parameter to unavailable default source component `%s`:\n %s\n",
3708 DEFAULT_SOURCE_COMPONENT_NAME
, arg
);
3711 if (cur_cfg_comp_dest
!= BT_CONFIG_COMPONENT_DEST_SOURCE
) {
3712 printf_err("--end option must follow a --source option:\n %s\n",
3716 if (bt_value_map_insert_string(cur_cfg_comp
->params
,
3724 const char *begin
, *end
;
3726 if (!cur_cfg_comp
) {
3727 printf_err("Cannot add `begin` and `end` parameters to unavailable default source component `%s`:\n %s\n",
3728 DEFAULT_SOURCE_COMPONENT_NAME
, arg
);
3731 if (cur_cfg_comp_dest
!= BT_CONFIG_COMPONENT_DEST_SOURCE
) {
3732 printf_err("--timerange option must follow a --source option:\n %s\n",
3736 if (split_timerange(arg
, &begin
, &end
)) {
3737 printf_err("Invalid --timerange format: expecting BEGIN,END or [BEGIN,END]:\n %s\n",
3741 if (bt_value_map_insert_string(cur_cfg_comp
->params
,
3746 if (bt_value_map_insert_string(cur_cfg_comp
->params
,
3754 if (bt_value_array_append_string(connection_args
,
3761 print_convert_usage(stdout
);
3766 text_legacy_opts
.verbose
= true;
3767 cfg
->verbose
= true;
3773 printf_err("Unknown command-line option specified (option code %d)\n",
3782 /* Check for option parsing error */
3784 printf_err("While parsing command-line options, at option %s: %s\n",
3785 poptBadOption(pc
, 0), poptStrerror(opt
));
3789 /* Consume leftover arguments as legacy input paths */
3791 const char *input_path
= poptGetArg(pc
);
3797 if (bt_value_array_append_string(legacy_input_paths
,
3804 if (append_home_and_system_plugin_paths(
3805 cfg
->cmd_data
.convert
.plugin_paths
,
3806 cfg
->cmd_data
.convert
.omit_system_plugin_path
,
3807 cfg
->cmd_data
.convert
.omit_home_plugin_path
)) {
3808 printf_err("Cannot append home and system plugin paths\n");
3812 /* Append current component configuration, if any */
3813 if (cur_cfg_comp
&& !cur_is_implicit_source
) {
3814 add_cfg_comp(cfg
, cur_cfg_comp
, cur_cfg_comp_dest
);
3816 cur_cfg_comp
= NULL
;
3818 /* Validate legacy/non-legacy options */
3819 if (!validate_cfg(cfg
, &legacy_input_format
, &legacy_output_format
,
3820 legacy_input_paths
, &ctf_legacy_opts
,
3821 &text_legacy_opts
)) {
3822 printf_err("Command-line options form an invalid configuration\n");
3827 * If there's a legacy input format, convert it to source
3828 * component configurations.
3830 if (legacy_input_format
) {
3831 if (append_sources_from_legacy_opts(
3832 cfg
->cmd_data
.convert
.sources
,
3833 legacy_input_format
, &ctf_legacy_opts
,
3834 legacy_input_paths
)) {
3835 printf_err("Cannot convert legacy input format options to source component instance(s)\n");
3838 if (append_sources_from_implicit_params(
3839 cfg
->cmd_data
.convert
.sources
,
3840 implicit_source_comp
)) {
3841 printf_err("Cannot initialize legacy component parameters\n");
3844 use_implicit_source
= false;
3846 if (use_implicit_source
) {
3847 add_cfg_comp(cfg
, implicit_source_comp
,
3848 BT_CONFIG_COMPONENT_DEST_SOURCE
);
3849 implicit_source_comp
= NULL
;
3851 if (implicit_source_comp
3852 && !bt_value_map_is_empty(implicit_source_comp
->params
)) {
3853 printf_err("Arguments specified for implicit input format, but an explicit source component instance has been specified: overriding it\n");
3860 * At this point if we need to print the CTF metadata text, we
3861 * don't care about the legacy/implicit sinks and component
3864 if (cfg
->cmd_data
.convert
.print_ctf_metadata
) {
3869 * If there's a legacy output format, convert it to sink
3870 * component configurations.
3872 if (legacy_output_format
) {
3873 if (append_sinks_from_legacy_opts(cfg
->cmd_data
.convert
.sinks
,
3874 legacy_output_format
, &text_legacy_opts
)) {
3875 printf_err("Cannot convert legacy output format options to sink component instance(s)\n");
3880 if (cfg
->cmd_data
.convert
.sinks
->len
== 0) {
3881 /* Use implicit sink as default. */
3882 cur_cfg_comp
= bt_config_component_from_arg(
3883 BT_COMPONENT_CLASS_TYPE_SINK
,
3884 DEFAULT_SINK_COMPONENT_NAME
);
3885 if (!cur_cfg_comp
) {
3886 printf_error("Cannot find implicit sink plugin `%s`\n",
3887 DEFAULT_SINK_COMPONENT_NAME
);
3889 add_cfg_comp(cfg
, cur_cfg_comp
,
3890 BT_CONFIG_COMPONENT_DEST_SINK
);
3891 cur_cfg_comp
= NULL
;
3894 ret
= bt_config_create_connections(cfg
, connection_args
,
3897 printf_err("Cannot creation connections:\n%s", error_buf
);
3909 poptFreeContext(pc
);
3912 if (text_legacy_opts
.output
) {
3913 g_string_free(text_legacy_opts
.output
, TRUE
);
3916 if (text_legacy_opts
.dbg_info_dir
) {
3917 g_string_free(text_legacy_opts
.dbg_info_dir
, TRUE
);
3920 if (text_legacy_opts
.dbg_info_target_prefix
) {
3921 g_string_free(text_legacy_opts
.dbg_info_target_prefix
, TRUE
);
3925 BT_PUT(implicit_source_comp
);
3926 BT_PUT(cur_cfg_comp
);
3927 BT_PUT(cur_base_params
);
3928 BT_PUT(text_legacy_opts
.names
);
3929 BT_PUT(text_legacy_opts
.fields
);
3930 BT_PUT(legacy_input_paths
);
3931 BT_PUT(instance_names
);
3932 BT_PUT(connection_args
);
3937 * Prints the Babeltrace 2.x general usage.
3940 void print_gen_usage(FILE *fp
)
3942 fprintf(fp
, "Usage: babeltrace [GENERAL OPTIONS] [COMMAND] [COMMAND OPTIONS]\n");
3944 fprintf(fp
, "General options:\n");
3946 fprintf(fp
, " -d, --debug Enable debug mode\n");
3947 fprintf(fp
, " -h --help Show this help and quit\n");
3948 fprintf(fp
, " --help-legacy Show Babeltrace 1.x legacy help and quit\n");
3949 fprintf(fp
, " -v, --verbose Enable verbose output\n");
3950 fprintf(fp
, " -V, --version Show version and quit\n");
3952 fprintf(fp
, "Available commands:\n");
3954 fprintf(fp
, " convert Build a trace conversion graph and run it (default)\n");
3955 fprintf(fp
, " help Get help for a plugin or a component class\n");
3956 fprintf(fp
, " list-plugins List available plugins and their content\n");
3957 fprintf(fp
, " query Query objects from a component class\n");
3959 fprintf(fp
, "Use `babeltrace COMMAND --help` to show the help of COMMAND.\n");
3962 struct bt_config
*bt_config_from_args(int argc
, const char *argv
[],
3963 int *retcode
, bool omit_system_plugin_path
,
3964 bool omit_home_plugin_path
,
3965 struct bt_value
*initial_plugin_paths
)
3967 struct bt_config
*config
= NULL
;
3968 bool verbose
= false;
3971 enum bt_config_command command
= -1;
3972 const char **command_argv
= NULL
;
3973 int command_argc
= -1;
3974 const char *command_name
= NULL
;
3979 print_gen_usage(stdout
);
3983 for (i
= 1; i
< argc
; i
++) {
3984 const char *cur_arg
= argv
[i
];
3986 if (strcmp(cur_arg
, "-d") == 0 ||
3987 strcmp(cur_arg
, "--debug") == 0) {
3989 } else if (strcmp(cur_arg
, "-v") == 0 ||
3990 strcmp(cur_arg
, "--verbose") == 0) {
3992 } else if (strcmp(cur_arg
, "-V") == 0 ||
3993 strcmp(cur_arg
, "--version") == 0) {
3996 } else if (strcmp(cur_arg
, "-h") == 0 ||
3997 strcmp(cur_arg
, "--help") == 0) {
3998 print_gen_usage(stdout
);
4000 } else if (strcmp(cur_arg
, "--help-legacy") == 0) {
4001 print_legacy_usage(stdout
);
4004 bool has_command
= true;
4007 * First unknown argument: is it a known command
4010 if (strcmp(cur_arg
, "convert") == 0) {
4011 command
= BT_CONFIG_COMMAND_CONVERT
;
4012 } else if (strcmp(cur_arg
, "list-plugins") == 0) {
4013 command
= BT_CONFIG_COMMAND_LIST_PLUGINS
;
4014 } else if (strcmp(cur_arg
, "help") == 0) {
4015 command
= BT_CONFIG_COMMAND_HELP
;
4016 } else if (strcmp(cur_arg
, "query") == 0) {
4017 command
= BT_CONFIG_COMMAND_QUERY
;
4020 * Unknown argument, but not a known
4021 * command name: assume the whole
4022 * arguments are for the default convert
4025 command
= BT_CONFIG_COMMAND_CONVERT
;
4026 command_argv
= argv
;
4027 command_argc
= argc
;
4028 has_command
= false;
4032 command_argv
= &argv
[i
];
4033 command_argc
= argc
- i
;
4034 command_name
= cur_arg
;
4040 if ((int) command
< 0) {
4042 * We only got non-help, non-version general options
4043 * like --verbose and --debug, without any other
4044 * arguments, so we can't do anything useful: print the
4047 print_gen_usage(stdout
);
4051 assert(command_argv
);
4052 assert(command_argc
>= 0);
4055 case BT_CONFIG_COMMAND_CONVERT
:
4056 config
= bt_config_convert_from_args(command_argc
, command_argv
,
4057 retcode
, omit_system_plugin_path
,
4058 omit_home_plugin_path
, initial_plugin_paths
);
4060 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
4061 config
= bt_config_list_plugins_from_args(command_argc
,
4062 command_argv
, retcode
, omit_system_plugin_path
,
4063 omit_home_plugin_path
, initial_plugin_paths
);
4065 case BT_CONFIG_COMMAND_HELP
:
4066 config
= bt_config_help_from_args(command_argc
,
4067 command_argv
, retcode
, omit_system_plugin_path
,
4068 omit_home_plugin_path
, initial_plugin_paths
);
4070 case BT_CONFIG_COMMAND_QUERY
:
4071 config
= bt_config_query_from_args(command_argc
,
4072 command_argv
, retcode
, omit_system_plugin_path
,
4073 omit_home_plugin_path
, initial_plugin_paths
);
4081 config
->verbose
= true;
4085 config
->debug
= true;
4088 config
->command_name
= command_name
;