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"
41 #define DEFAULT_SOURCE_COMPONENT_NAME "ctf.fs"
42 #define DEFAULT_SINK_COMPONENT_NAME "text.text"
45 * Error printf() macro which prepends "Error: " the first time it's
46 * called. This gives a nicer feel than having a bunch of error prefixes
47 * (since the following lines usually describe the error and possible
48 * solutions), or the error prefix just at the end.
50 #define printf_err(fmt, args...) \
52 if (is_first_error) { \
53 fprintf(stderr, "Error: "); \
54 is_first_error = false; \
56 fprintf(stderr, fmt, ##args); \
59 static bool is_first_error
= true;
61 /* INI-style parsing FSM states */
62 enum ini_parsing_fsm_state
{
63 /* Expect a map key (identifier) */
66 /* Expect an equal character ('=') */
72 /* Expect a negative number value */
73 INI_EXPECT_VALUE_NUMBER_NEG
,
75 /* Expect a comma character (',') */
79 /* INI-style parsing state variables */
80 struct ini_parsing_state
{
81 /* Lexical scanner (owned by this) */
84 /* Output map value object being filled (owned by this) */
85 struct bt_value
*params
;
87 /* Next expected FSM state */
88 enum ini_parsing_fsm_state expecting
;
90 /* Last decoded map key (owned by this) */
93 /* Complete INI-style string to parse (not owned by this) */
96 /* Error buffer (not owned by this) */
100 /* Offset option with "is set" boolean */
106 /* Legacy "ctf"/"lttng-live" format options */
107 struct ctf_legacy_opts
{
108 struct offset_opt offset_s
;
109 struct offset_opt offset_ns
;
110 bool stream_intersection
;
113 /* Legacy "text" format options */
114 struct text_legacy_opts
{
116 * output, dbg_info_dir, dbg_info_target_prefix, names,
117 * and fields are owned by this.
120 GString
*dbg_info_dir
;
121 GString
*dbg_info_target_prefix
;
122 struct bt_value
*names
;
123 struct bt_value
*fields
;
131 bool dbg_info_full_path
;
134 /* Legacy input format format */
135 enum legacy_input_format
{
136 LEGACY_INPUT_FORMAT_NONE
= 0,
137 LEGACY_INPUT_FORMAT_CTF
,
138 LEGACY_INPUT_FORMAT_LTTNG_LIVE
,
141 /* Legacy output format format */
142 enum legacy_output_format
{
143 LEGACY_OUTPUT_FORMAT_NONE
= 0,
144 LEGACY_OUTPUT_FORMAT_TEXT
,
145 LEGACY_OUTPUT_FORMAT_CTF_METADATA
,
146 LEGACY_OUTPUT_FORMAT_DUMMY
,
150 * Prints the "out of memory" error.
153 void print_err_oom(void)
155 printf_err("Out of memory\n");
159 * Prints duplicate legacy output format error.
162 void print_err_dup_legacy_output(void)
164 printf_err("More than one legacy output format specified\n");
168 * Prints duplicate legacy input format error.
171 void print_err_dup_legacy_input(void)
173 printf_err("More than one legacy input format specified\n");
177 * Checks if any of the "text" legacy options is set.
180 bool text_legacy_opts_is_any_set(struct text_legacy_opts
*opts
)
182 return (opts
->output
&& opts
->output
->len
> 0) ||
183 (opts
->dbg_info_dir
&& opts
->dbg_info_dir
->len
> 0) ||
184 (opts
->dbg_info_target_prefix
&&
185 opts
->dbg_info_target_prefix
->len
> 0) ||
186 bt_value_array_size(opts
->names
) > 0 ||
187 bt_value_array_size(opts
->fields
) > 0 ||
188 opts
->no_delta
|| opts
->clock_cycles
|| opts
->clock_seconds
||
189 opts
->clock_date
|| opts
->clock_gmt
||
190 opts
->dbg_info_full_path
;
194 * Checks if any of the "ctf" legacy options is set.
197 bool ctf_legacy_opts_is_any_set(struct ctf_legacy_opts
*opts
)
199 return opts
->offset_s
.is_set
|| opts
->offset_ns
.is_set
||
200 opts
->stream_intersection
;
204 * Appends an "expecting token" error to the INI-style parsing state's
208 void ini_append_error_expecting(struct ini_parsing_state
*state
,
209 GScanner
*scanner
, const char *expecting
)
214 g_string_append_printf(state
->ini_error
, "Expecting %s:\n", expecting
);
216 /* Only print error if there's one line */
217 if (strchr(state
->arg
, '\n') != NULL
|| strlen(state
->arg
) == 0) {
221 g_string_append_printf(state
->ini_error
, "\n %s\n", state
->arg
);
222 pos
= g_scanner_cur_position(scanner
) + 4;
224 if (!g_scanner_eof(scanner
)) {
228 for (i
= 0; i
< pos
; ++i
) {
229 g_string_append_printf(state
->ini_error
, " ");
232 g_string_append_printf(state
->ini_error
, "^\n\n");
236 int ini_handle_state(struct ini_parsing_state
*state
)
239 GTokenType token_type
;
240 struct bt_value
*value
= NULL
;
242 token_type
= g_scanner_get_next_token(state
->scanner
);
243 if (token_type
== G_TOKEN_EOF
) {
244 if (state
->expecting
!= INI_EXPECT_COMMA
) {
245 switch (state
->expecting
) {
246 case INI_EXPECT_EQUAL
:
247 ini_append_error_expecting(state
,
248 state
->scanner
, "'='");
250 case INI_EXPECT_VALUE
:
251 case INI_EXPECT_VALUE_NUMBER_NEG
:
252 ini_append_error_expecting(state
,
253 state
->scanner
, "value");
255 case INI_EXPECT_MAP_KEY
:
256 ini_append_error_expecting(state
,
257 state
->scanner
, "unquoted map key");
270 switch (state
->expecting
) {
271 case INI_EXPECT_MAP_KEY
:
272 if (token_type
!= G_TOKEN_IDENTIFIER
) {
273 ini_append_error_expecting(state
, state
->scanner
,
278 free(state
->last_map_key
);
279 state
->last_map_key
=
280 strdup(state
->scanner
->value
.v_identifier
);
281 if (!state
->last_map_key
) {
282 g_string_append(state
->ini_error
,
287 if (bt_value_map_has_key(state
->params
, state
->last_map_key
)) {
288 g_string_append_printf(state
->ini_error
,
289 "Duplicate parameter key: `%s`\n",
290 state
->last_map_key
);
294 state
->expecting
= INI_EXPECT_EQUAL
;
296 case INI_EXPECT_EQUAL
:
297 if (token_type
!= G_TOKEN_CHAR
) {
298 ini_append_error_expecting(state
,
299 state
->scanner
, "'='");
303 if (state
->scanner
->value
.v_char
!= '=') {
304 ini_append_error_expecting(state
,
305 state
->scanner
, "'='");
309 state
->expecting
= INI_EXPECT_VALUE
;
311 case INI_EXPECT_VALUE
:
313 switch (token_type
) {
315 if (state
->scanner
->value
.v_char
== '-') {
316 /* Negative number */
318 INI_EXPECT_VALUE_NUMBER_NEG
;
321 ini_append_error_expecting(state
,
322 state
->scanner
, "value");
328 /* Positive integer */
329 uint64_t int_val
= state
->scanner
->value
.v_int64
;
331 if (int_val
> (1ULL << 63) - 1) {
332 g_string_append_printf(state
->ini_error
,
333 "Integer value %" PRIu64
" is outside the range of a 64-bit signed integer\n",
338 value
= bt_value_integer_create_init(
343 /* Positive floating point number */
344 value
= bt_value_float_create_init(
345 state
->scanner
->value
.v_float
);
349 value
= bt_value_string_create_init(
350 state
->scanner
->value
.v_string
);
352 case G_TOKEN_IDENTIFIER
:
355 * Using symbols would be appropriate here,
356 * but said symbols are allowed as map key,
357 * so it's easier to consider everything an
360 * If one of the known symbols is not
361 * recognized here, then fall back to creating
364 const char *id
= state
->scanner
->value
.v_identifier
;
366 if (!strcmp(id
, "null") || !strcmp(id
, "NULL") ||
367 !strcmp(id
, "nul")) {
368 value
= bt_value_null
;
369 } else if (!strcmp(id
, "true") || !strcmp(id
, "TRUE") ||
370 !strcmp(id
, "yes") ||
371 !strcmp(id
, "YES")) {
372 value
= bt_value_bool_create_init(true);
373 } else if (!strcmp(id
, "false") ||
374 !strcmp(id
, "FALSE") ||
377 value
= bt_value_bool_create_init(false);
379 value
= bt_value_string_create_init(id
);
384 /* Unset value variable will trigger the error */
389 ini_append_error_expecting(state
,
390 state
->scanner
, "value");
394 state
->expecting
= INI_EXPECT_COMMA
;
397 case INI_EXPECT_VALUE_NUMBER_NEG
:
399 switch (token_type
) {
402 /* Negative integer */
403 uint64_t int_val
= state
->scanner
->value
.v_int64
;
405 if (int_val
> (1ULL << 63) - 1) {
406 g_string_append_printf(state
->ini_error
,
407 "Integer value -%" PRIu64
" is outside the range of a 64-bit signed integer\n",
412 value
= bt_value_integer_create_init(
413 -((int64_t) int_val
));
417 /* Negative floating point number */
418 value
= bt_value_float_create_init(
419 -state
->scanner
->value
.v_float
);
422 /* Unset value variable will trigger the error */
427 ini_append_error_expecting(state
,
428 state
->scanner
, "value");
432 state
->expecting
= INI_EXPECT_COMMA
;
435 case INI_EXPECT_COMMA
:
436 if (token_type
!= G_TOKEN_CHAR
) {
437 ini_append_error_expecting(state
,
438 state
->scanner
, "','");
442 if (state
->scanner
->value
.v_char
!= ',') {
443 ini_append_error_expecting(state
,
444 state
->scanner
, "','");
448 state
->expecting
= INI_EXPECT_MAP_KEY
;
460 if (bt_value_map_insert(state
->params
,
461 state
->last_map_key
, value
)) {
462 /* Only override return value on error */
473 * Converts an INI-style argument to an equivalent map value object.
475 * Return value is owned by the caller.
478 struct bt_value
*bt_value_from_ini(const char *arg
, GString
*ini_error
)
480 /* Lexical scanner configuration */
481 GScannerConfig scanner_config
= {
482 /* Skip whitespaces */
483 .cset_skip_characters
= " \t\n",
485 /* Identifier syntax is: [a-zA-Z_][a-zA-Z0-9_.:-]* */
486 .cset_identifier_first
=
490 .cset_identifier_nth
=
495 /* "hello" and "Hello" two different keys */
496 .case_sensitive
= TRUE
,
499 .cpair_comment_single
= NULL
,
500 .skip_comment_multi
= TRUE
,
501 .skip_comment_single
= TRUE
,
502 .scan_comment_multi
= FALSE
,
505 * Do scan identifiers, including 1-char identifiers,
506 * but NULL is a normal identifier.
508 .scan_identifier
= TRUE
,
509 .scan_identifier_1char
= TRUE
,
510 .scan_identifier_NULL
= FALSE
,
513 * No specific symbols: null and boolean "symbols" are
514 * scanned as plain identifiers.
516 .scan_symbols
= FALSE
,
517 .symbol_2_token
= FALSE
,
518 .scope_0_fallback
= FALSE
,
521 * Scan "0b"-, "0"-, and "0x"-prefixed integers, but not
522 * integers prefixed with "$".
528 .scan_hex_dollar
= FALSE
,
530 /* Convert scanned numbers to integer tokens */
531 .numbers_2_int
= TRUE
,
533 /* Support both integers and floating-point numbers */
534 .int_2_float
= FALSE
,
536 /* Scan integers as 64-bit signed integers */
539 /* Only scan double-quoted strings */
540 .scan_string_sq
= FALSE
,
541 .scan_string_dq
= TRUE
,
543 /* Do not converter identifiers to string tokens */
544 .identifier_2_string
= FALSE
,
546 /* Scan characters as G_TOKEN_CHAR token */
547 .char_2_token
= FALSE
,
549 struct ini_parsing_state state
= {
552 .expecting
= INI_EXPECT_MAP_KEY
,
554 .ini_error
= ini_error
,
557 state
.params
= bt_value_map_create();
562 state
.scanner
= g_scanner_new(&scanner_config
);
563 if (!state
.scanner
) {
567 /* Let the scan begin */
568 g_scanner_input_text(state
.scanner
, arg
, strlen(arg
));
571 int ret
= ini_handle_state(&state
);
576 } else if (ret
> 0) {
585 BT_PUT(state
.params
);
589 g_scanner_destroy(state
.scanner
);
592 free(state
.last_map_key
);
597 * Returns the parameters map value object from a command-line
598 * parameter option's argument.
600 * Return value is owned by the caller.
603 struct bt_value
*bt_value_from_arg(const char *arg
)
605 struct bt_value
*params
= NULL
;
606 GString
*ini_error
= NULL
;
608 ini_error
= g_string_new(NULL
);
614 /* Try INI-style parsing */
615 params
= bt_value_from_ini(arg
, ini_error
);
617 printf_err("%s", ini_error
->str
);
623 g_string_free(ini_error
, TRUE
);
629 * Returns the plugin and component names from a command-line
630 * source/sink option's argument. arg must have the following format:
634 * where PLUGIN is the plugin name, and COMPONENT is the component
637 * On success, both *plugin and *component are not NULL. *plugin
638 * and *component are owned by the caller.
641 void plugin_component_names_from_arg(const char *arg
, char **plugin
,
647 size_t component_len
;
649 /* Initialize both return values to NULL: not found */
653 dot
= strchr(arg
, '.');
659 end
= arg
+ strlen(arg
);
660 plugin_len
= dot
- arg
;
661 component_len
= end
- dot
- 1;
662 if (plugin_len
== 0 || component_len
== 0) {
666 *plugin
= g_malloc0(plugin_len
+ 1);
672 g_strlcpy(*plugin
, arg
, plugin_len
+ 1);
673 *component
= g_malloc0(component_len
+ 1);
679 g_strlcpy(*component
, dot
+ 1, component_len
+ 1);
686 * Prints the Babeltrace version.
689 void print_version(void)
691 puts("Babeltrace " VERSION
);
695 * Destroys a component configuration.
698 void bt_config_component_destroy(struct bt_object
*obj
)
700 struct bt_config_component
*bt_config_component
=
701 container_of(obj
, struct bt_config_component
, base
);
707 if (bt_config_component
->plugin_name
) {
708 g_string_free(bt_config_component
->plugin_name
, TRUE
);
711 if (bt_config_component
->component_name
) {
712 g_string_free(bt_config_component
->component_name
, TRUE
);
715 BT_PUT(bt_config_component
->params
);
716 g_free(bt_config_component
);
723 * Creates a component configuration using the given plugin name and
724 * component name. plugin_name and component_name are copied (belong to
727 * Return value is owned by the caller.
730 struct bt_config_component
*bt_config_component_create(const char *plugin_name
,
731 const char *component_name
)
733 struct bt_config_component
*cfg_component
= NULL
;
735 cfg_component
= g_new0(struct bt_config_component
, 1);
736 if (!cfg_component
) {
741 bt_object_init(cfg_component
, bt_config_component_destroy
);
742 cfg_component
->plugin_name
= g_string_new(plugin_name
);
743 if (!cfg_component
->plugin_name
) {
748 cfg_component
->component_name
= g_string_new(component_name
);
749 if (!cfg_component
->component_name
) {
754 /* Start with empty parameters */
755 cfg_component
->params
= bt_value_map_create();
756 if (!cfg_component
->params
) {
764 BT_PUT(cfg_component
);
767 return cfg_component
;
771 * Creates a component configuration from a command-line source/sink
775 struct bt_config_component
*bt_config_component_from_arg(const char *arg
)
777 struct bt_config_component
*bt_config_component
= NULL
;
779 char *component_name
;
781 plugin_component_names_from_arg(arg
, &plugin_name
, &component_name
);
782 if (!plugin_name
|| !component_name
) {
783 printf_err("Cannot get plugin or component class name\n");
787 bt_config_component
= bt_config_component_create(plugin_name
,
789 if (!bt_config_component
) {
796 BT_PUT(bt_config_component
);
800 g_free(component_name
);
801 return bt_config_component
;
805 * Destroys a configuration.
808 void bt_config_destroy(struct bt_object
*obj
)
810 struct bt_config
*cfg
=
811 container_of(obj
, struct bt_config
, base
);
817 switch (cfg
->command
) {
818 case BT_CONFIG_COMMAND_CONVERT
:
819 if (cfg
->cmd_data
.convert
.sources
) {
820 g_ptr_array_free(cfg
->cmd_data
.convert
.sources
, TRUE
);
823 if (cfg
->cmd_data
.convert
.sinks
) {
824 g_ptr_array_free(cfg
->cmd_data
.convert
.sinks
, TRUE
);
827 BT_PUT(cfg
->cmd_data
.convert
.plugin_paths
);
830 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
831 BT_PUT(cfg
->cmd_data
.list_plugins
.plugin_paths
);
844 static void destroy_gstring(void *data
)
846 g_string_free(data
, TRUE
);
850 * Extracts the various paths from the string arg, delimited by ':',
851 * and appends them to the array value object plugin_paths.
853 enum bt_value_status
bt_config_append_plugin_paths(
854 struct bt_value
*plugin_paths
, const char *arg
)
856 enum bt_value_status status
= BT_VALUE_STATUS_OK
;
857 GPtrArray
*dirs
= g_ptr_array_new_with_free_func(destroy_gstring
);
862 status
= BT_VALUE_STATUS_ERROR
;
866 ret
= bt_common_append_plugin_path_dirs(arg
, dirs
);
868 status
= BT_VALUE_STATUS_ERROR
;
872 for (i
= 0; i
< dirs
->len
; i
++) {
873 GString
*dir
= g_ptr_array_index(dirs
, i
);
875 bt_value_array_append_string(plugin_paths
, dir
->str
);
879 g_ptr_array_free(dirs
, TRUE
);
884 * Creates a simple lexical scanner for parsing comma-delimited names
887 * Return value is owned by the caller.
890 GScanner
*create_csv_identifiers_scanner(void)
892 GScannerConfig scanner_config
= {
893 .cset_skip_characters
= " \t\n",
894 .cset_identifier_first
= G_CSET_a_2_z G_CSET_A_2_Z
"_",
895 .cset_identifier_nth
= G_CSET_a_2_z G_CSET_A_2_Z
":_-",
896 .case_sensitive
= TRUE
,
897 .cpair_comment_single
= NULL
,
898 .skip_comment_multi
= TRUE
,
899 .skip_comment_single
= TRUE
,
900 .scan_comment_multi
= FALSE
,
901 .scan_identifier
= TRUE
,
902 .scan_identifier_1char
= TRUE
,
903 .scan_identifier_NULL
= FALSE
,
904 .scan_symbols
= FALSE
,
905 .symbol_2_token
= FALSE
,
906 .scope_0_fallback
= FALSE
,
907 .scan_binary
= FALSE
,
911 .scan_hex_dollar
= FALSE
,
912 .numbers_2_int
= FALSE
,
913 .int_2_float
= FALSE
,
914 .store_int64
= FALSE
,
915 .scan_string_sq
= FALSE
,
916 .scan_string_dq
= FALSE
,
917 .identifier_2_string
= FALSE
,
918 .char_2_token
= TRUE
,
921 return g_scanner_new(&scanner_config
);
925 * Inserts a string (if exists and not empty) or null to a map value
929 enum bt_value_status
map_insert_string_or_null(struct bt_value
*map
,
930 const char *key
, GString
*string
)
932 enum bt_value_status ret
;
934 if (string
&& string
->len
> 0) {
935 ret
= bt_value_map_insert_string(map
, key
, string
->str
);
937 ret
= bt_value_map_insert(map
, key
, bt_value_null
);
943 * Converts a comma-delimited list of known names (--names option) to
944 * an array value object containing those names as string value objects.
946 * Return value is owned by the caller.
949 struct bt_value
*names_from_arg(const char *arg
)
951 GScanner
*scanner
= NULL
;
952 struct bt_value
*names
= NULL
;
953 bool found_all
= false, found_none
= false, found_item
= false;
955 names
= bt_value_array_create();
961 scanner
= create_csv_identifiers_scanner();
967 g_scanner_input_text(scanner
, arg
, strlen(arg
));
970 GTokenType token_type
= g_scanner_get_next_token(scanner
);
972 switch (token_type
) {
973 case G_TOKEN_IDENTIFIER
:
975 const char *identifier
= scanner
->value
.v_identifier
;
977 if (!strcmp(identifier
, "payload") ||
978 !strcmp(identifier
, "args") ||
979 !strcmp(identifier
, "arg")) {
981 if (bt_value_array_append_string(names
,
985 } else if (!strcmp(identifier
, "context") ||
986 !strcmp(identifier
, "ctx")) {
988 if (bt_value_array_append_string(names
,
992 } else if (!strcmp(identifier
, "scope") ||
993 !strcmp(identifier
, "header")) {
995 if (bt_value_array_append_string(names
,
999 } else if (!strcmp(identifier
, "all")) {
1001 if (bt_value_array_append_string(names
,
1005 } else if (!strcmp(identifier
, "none")) {
1007 if (bt_value_array_append_string(names
,
1012 printf_err("Unknown field name: `%s`\n",
1028 if (found_none
&& found_all
) {
1029 printf_err("Only either `all` or `none` can be specified in the list given to the --names option, but not both.\n");
1033 * Legacy behavior is to clear the defaults (show none) when at
1034 * least one item is specified.
1036 if (found_item
&& !found_none
&& !found_all
) {
1037 if (bt_value_array_append_string(names
, "none")) {
1042 g_scanner_destroy(scanner
);
1049 g_scanner_destroy(scanner
);
1056 * Converts a comma-delimited list of known fields (--fields option) to
1057 * an array value object containing those fields as string
1060 * Return value is owned by the caller.
1063 struct bt_value
*fields_from_arg(const char *arg
)
1065 GScanner
*scanner
= NULL
;
1066 struct bt_value
*fields
;
1068 fields
= bt_value_array_create();
1074 scanner
= create_csv_identifiers_scanner();
1080 g_scanner_input_text(scanner
, arg
, strlen(arg
));
1083 GTokenType token_type
= g_scanner_get_next_token(scanner
);
1085 switch (token_type
) {
1086 case G_TOKEN_IDENTIFIER
:
1088 const char *identifier
= scanner
->value
.v_identifier
;
1090 if (!strcmp(identifier
, "trace") ||
1091 !strcmp(identifier
, "trace:hostname") ||
1092 !strcmp(identifier
, "trace:domain") ||
1093 !strcmp(identifier
, "trace:procname") ||
1094 !strcmp(identifier
, "trace:vpid") ||
1095 !strcmp(identifier
, "loglevel") ||
1096 !strcmp(identifier
, "emf") ||
1097 !strcmp(identifier
, "callsite") ||
1098 !strcmp(identifier
, "all")) {
1099 if (bt_value_array_append_string(fields
,
1104 printf_err("Unknown field name: `%s`\n",
1126 g_scanner_destroy(scanner
);
1132 * Inserts the equivalent "prefix-name" true boolean value objects into
1133 * map_obj where the names are in array_obj.
1136 int insert_flat_names_fields_from_array(struct bt_value
*map_obj
,
1137 struct bt_value
*array_obj
, const char *prefix
)
1141 GString
*tmpstr
= NULL
, *default_value
= NULL
;
1144 * array_obj may be NULL if no CLI options were specified to
1145 * trigger its creation.
1151 tmpstr
= g_string_new(NULL
);
1158 default_value
= g_string_new(NULL
);
1159 if (!default_value
) {
1165 for (i
= 0; i
< bt_value_array_size(array_obj
); i
++) {
1166 struct bt_value
*str_obj
= bt_value_array_get(array_obj
, i
);
1168 bool is_default
= false;
1171 printf_err("Unexpected error\n");
1176 ret
= bt_value_string_get(str_obj
, &suffix
);
1179 printf_err("Unexpected error\n");
1183 g_string_assign(tmpstr
, prefix
);
1184 g_string_append(tmpstr
, "-");
1186 /* Special-case for "all" and "none". */
1187 if (!strcmp(suffix
, "all")) {
1189 g_string_assign(default_value
, "show");
1190 } else if (!strcmp(suffix
, "none")) {
1192 g_string_assign(default_value
, "hide");
1195 g_string_append(tmpstr
, "default");
1196 ret
= map_insert_string_or_null(map_obj
,
1204 g_string_append(tmpstr
, suffix
);
1205 ret
= bt_value_map_insert_bool(map_obj
, tmpstr
->str
,
1215 if (default_value
) {
1216 g_string_free(default_value
, TRUE
);
1219 g_string_free(tmpstr
, TRUE
);
1226 * Returns the parameters (map value object) corresponding to the
1227 * legacy text format options.
1229 * Return value is owned by the caller.
1232 struct bt_value
*params_from_text_legacy_opts(
1233 struct text_legacy_opts
*text_legacy_opts
)
1235 struct bt_value
*params
;
1237 params
= bt_value_map_create();
1243 if (map_insert_string_or_null(params
, "output-path",
1244 text_legacy_opts
->output
)) {
1249 if (map_insert_string_or_null(params
, "debug-info-dir",
1250 text_legacy_opts
->dbg_info_dir
)) {
1255 if (map_insert_string_or_null(params
, "debug-info-target-prefix",
1256 text_legacy_opts
->dbg_info_target_prefix
)) {
1261 if (bt_value_map_insert_bool(params
, "debug-info-full-path",
1262 text_legacy_opts
->dbg_info_full_path
)) {
1267 if (bt_value_map_insert_bool(params
, "no-delta",
1268 text_legacy_opts
->no_delta
)) {
1273 if (bt_value_map_insert_bool(params
, "clock-cycles",
1274 text_legacy_opts
->clock_cycles
)) {
1279 if (bt_value_map_insert_bool(params
, "clock-seconds",
1280 text_legacy_opts
->clock_seconds
)) {
1285 if (bt_value_map_insert_bool(params
, "clock-date",
1286 text_legacy_opts
->clock_date
)) {
1291 if (bt_value_map_insert_bool(params
, "clock-gmt",
1292 text_legacy_opts
->clock_gmt
)) {
1297 if (insert_flat_names_fields_from_array(params
,
1298 text_legacy_opts
->names
, "name")) {
1302 if (insert_flat_names_fields_from_array(params
,
1303 text_legacy_opts
->fields
, "field")) {
1317 int append_sinks_from_legacy_opts(GPtrArray
*sinks
,
1318 enum legacy_output_format legacy_output_format
,
1319 struct text_legacy_opts
*text_legacy_opts
)
1322 struct bt_value
*params
= NULL
;
1323 const char *plugin_name
;
1324 const char *component_name
;
1325 struct bt_config_component
*bt_config_component
= NULL
;
1327 switch (legacy_output_format
) {
1328 case LEGACY_OUTPUT_FORMAT_TEXT
:
1329 plugin_name
= "text";
1330 component_name
= "text";
1332 case LEGACY_OUTPUT_FORMAT_CTF_METADATA
:
1333 plugin_name
= "ctf";
1334 component_name
= "metadata-text";
1336 case LEGACY_OUTPUT_FORMAT_DUMMY
:
1337 plugin_name
= "dummy";
1338 component_name
= "dummy";
1345 if (legacy_output_format
== LEGACY_OUTPUT_FORMAT_TEXT
) {
1346 /* Legacy "text" output format has parameters */
1347 params
= params_from_text_legacy_opts(text_legacy_opts
);
1353 * Legacy "dummy" and "ctf-metadata" output formats do
1354 * not have parameters.
1356 params
= bt_value_map_create();
1363 /* Create a component configuration */
1364 bt_config_component
= bt_config_component_create(plugin_name
,
1366 if (!bt_config_component
) {
1370 BT_MOVE(bt_config_component
->params
, params
);
1372 /* Move created component configuration to the array */
1373 g_ptr_array_add(sinks
, bt_config_component
);
1387 * Returns the parameters (map value object) corresponding to the
1388 * given legacy CTF format options.
1390 * Return value is owned by the caller.
1393 struct bt_value
*params_from_ctf_legacy_opts(
1394 struct ctf_legacy_opts
*ctf_legacy_opts
)
1396 struct bt_value
*params
;
1398 params
= bt_value_map_create();
1404 if (bt_value_map_insert_integer(params
, "offset-s",
1405 ctf_legacy_opts
->offset_s
.value
)) {
1410 if (bt_value_map_insert_integer(params
, "offset-ns",
1411 ctf_legacy_opts
->offset_ns
.value
)) {
1416 if (bt_value_map_insert_bool(params
, "stream-intersection",
1417 ctf_legacy_opts
->stream_intersection
)) {
1432 int append_sources_from_legacy_opts(GPtrArray
*sources
,
1433 enum legacy_input_format legacy_input_format
,
1434 struct ctf_legacy_opts
*ctf_legacy_opts
,
1435 struct bt_value
*legacy_input_paths
)
1439 struct bt_value
*base_params
;
1440 struct bt_value
*params
= NULL
;
1441 struct bt_value
*input_path
= NULL
;
1442 struct bt_value
*input_path_copy
= NULL
;
1443 const char *input_key
;
1444 const char *component_name
;
1446 switch (legacy_input_format
) {
1447 case LEGACY_INPUT_FORMAT_CTF
:
1449 component_name
= "fs";
1451 case LEGACY_INPUT_FORMAT_LTTNG_LIVE
:
1453 component_name
= "lttng-live";
1460 base_params
= params_from_ctf_legacy_opts(ctf_legacy_opts
);
1465 for (i
= 0; i
< bt_value_array_size(legacy_input_paths
); i
++) {
1466 struct bt_config_component
*bt_config_component
= NULL
;
1468 /* Copy base parameters as current parameters */
1469 params
= bt_value_copy(base_params
);
1474 /* Get current input path string value object */
1475 input_path
= bt_value_array_get(legacy_input_paths
, i
);
1480 /* Copy current input path value object */
1481 input_path_copy
= bt_value_copy(input_path
);
1482 if (!input_path_copy
) {
1486 /* Insert input path value object into current parameters */
1487 ret
= bt_value_map_insert(params
, input_key
, input_path_copy
);
1492 /* Create a component configuration */
1493 bt_config_component
= bt_config_component_create("ctf",
1495 if (!bt_config_component
) {
1499 BT_MOVE(bt_config_component
->params
, params
);
1501 /* Move created component configuration to the array */
1502 g_ptr_array_add(sources
, bt_config_component
);
1504 /* Put current stuff */
1506 BT_PUT(input_path_copy
);
1515 BT_PUT(base_params
);
1518 BT_PUT(input_path_copy
);
1523 * Escapes a string for the shell. The string is escaped knowing that
1524 * it's a parameter string value (double-quoted), and that it will be
1525 * entered between single quotes in the shell.
1527 * Return value is owned by the caller.
1530 char *str_shell_escape(const char *input
)
1533 const char *at
= input
;
1534 GString
*str
= g_string_new(NULL
);
1540 while (*at
!= '\0') {
1543 g_string_append(str
, "\\\\");
1546 g_string_append(str
, "\\\"");
1549 g_string_append(str
, "'\"'\"'");
1552 g_string_append(str
, "\\n");
1555 g_string_append(str
, "\\t");
1558 g_string_append_c(str
, *at
);
1568 g_string_free(str
, FALSE
);
1575 int append_prefixed_flag_params(GString
*str
, struct bt_value
*flags
,
1585 for (i
= 0; i
< bt_value_array_size(flags
); i
++) {
1586 struct bt_value
*value
= bt_value_array_get(flags
, i
);
1594 if (bt_value_string_get(value
, &flag
)) {
1600 g_string_append_printf(str
, ",%s-%s=true", prefix
, flag
);
1609 * Appends a boolean parameter string.
1612 void g_string_append_bool_param(GString
*str
, const char *name
, bool value
)
1614 g_string_append_printf(str
, ",%s=%s", name
, value
? "true" : "false");
1618 * Appends a path parameter string, or null if it's empty.
1621 int g_string_append_string_path_param(GString
*str
, const char *name
,
1626 if (path
->len
> 0) {
1627 char *escaped_path
= str_shell_escape(path
->str
);
1629 if (!escaped_path
) {
1634 g_string_append_printf(str
, "%s=\"%s\"", name
, escaped_path
);
1637 g_string_append_printf(str
, "%s=null", name
);
1650 * Prints the non-legacy sink options equivalent to the specified
1651 * legacy output format options.
1654 void print_output_legacy_to_sinks(
1655 enum legacy_output_format legacy_output_format
,
1656 struct text_legacy_opts
*text_legacy_opts
)
1658 const char *input_format
;
1659 GString
*str
= NULL
;
1661 str
= g_string_new(" ");
1667 switch (legacy_output_format
) {
1668 case LEGACY_OUTPUT_FORMAT_TEXT
:
1669 input_format
= "text";
1671 case LEGACY_OUTPUT_FORMAT_CTF_METADATA
:
1672 input_format
= "ctf-metadata";
1674 case LEGACY_OUTPUT_FORMAT_DUMMY
:
1675 input_format
= "dummy";
1681 printf_err("Both `%s` legacy output format and non-legacy sink component\ninstances(s) specified.\n\n",
1683 printf_err("Specify the following non-legacy sink component instance instead of the\nlegacy `%s` output format options:\n\n",
1685 g_string_append(str
, "-o ");
1687 switch (legacy_output_format
) {
1688 case LEGACY_OUTPUT_FORMAT_TEXT
:
1689 g_string_append(str
, "text.text");
1691 case LEGACY_OUTPUT_FORMAT_CTF_METADATA
:
1692 g_string_append(str
, "ctf.metadata-text");
1694 case LEGACY_OUTPUT_FORMAT_DUMMY
:
1695 g_string_append(str
, "dummy.dummy");
1701 if (legacy_output_format
== LEGACY_OUTPUT_FORMAT_TEXT
&&
1702 text_legacy_opts_is_any_set(text_legacy_opts
)) {
1705 g_string_append(str
, " -p '");
1707 if (g_string_append_string_path_param(str
, "output-path",
1708 text_legacy_opts
->output
)) {
1712 g_string_append(str
, ",");
1714 if (g_string_append_string_path_param(str
, "debug-info-dir",
1715 text_legacy_opts
->dbg_info_dir
)) {
1719 g_string_append(str
, ",");
1721 if (g_string_append_string_path_param(str
,
1722 "debug-info-target-prefix",
1723 text_legacy_opts
->dbg_info_target_prefix
)) {
1727 g_string_append_bool_param(str
, "no-delta",
1728 text_legacy_opts
->no_delta
);
1729 g_string_append_bool_param(str
, "clock-cycles",
1730 text_legacy_opts
->clock_cycles
);
1731 g_string_append_bool_param(str
, "clock-seconds",
1732 text_legacy_opts
->clock_seconds
);
1733 g_string_append_bool_param(str
, "clock-date",
1734 text_legacy_opts
->clock_date
);
1735 g_string_append_bool_param(str
, "clock-gmt",
1736 text_legacy_opts
->clock_gmt
);
1737 ret
= append_prefixed_flag_params(str
, text_legacy_opts
->names
,
1743 ret
= append_prefixed_flag_params(str
, text_legacy_opts
->fields
,
1749 /* Remove last comma and close single quote */
1750 g_string_append(str
, "'");
1753 printf_err("%s\n\n", str
->str
);
1757 g_string_free(str
, TRUE
);
1763 * Prints the non-legacy source options equivalent to the specified
1764 * legacy input format options.
1767 void print_input_legacy_to_sources(enum legacy_input_format legacy_input_format
,
1768 struct bt_value
*legacy_input_paths
,
1769 struct ctf_legacy_opts
*ctf_legacy_opts
)
1771 const char *input_format
;
1772 GString
*str
= NULL
;
1775 str
= g_string_new(" ");
1781 switch (legacy_input_format
) {
1782 case LEGACY_INPUT_FORMAT_CTF
:
1783 input_format
= "ctf";
1785 case LEGACY_INPUT_FORMAT_LTTNG_LIVE
:
1786 input_format
= "lttng-live";
1792 printf_err("Both `%s` legacy input format and non-legacy source component\ninstance(s) specified.\n\n",
1794 printf_err("Specify the following non-legacy source component instance(s) instead of the\nlegacy `%s` input format options and positional arguments:\n\n",
1797 for (i
= 0; i
< bt_value_array_size(legacy_input_paths
); i
++) {
1798 struct bt_value
*input_value
=
1799 bt_value_array_get(legacy_input_paths
, i
);
1800 const char *input
= NULL
;
1801 char *escaped_input
;
1804 assert(input_value
);
1805 ret
= bt_value_string_get(input_value
, &input
);
1806 BT_PUT(input_value
);
1807 assert(!ret
&& input
);
1808 escaped_input
= str_shell_escape(input
);
1809 if (!escaped_input
) {
1814 g_string_append(str
, "-i ctf.");
1816 switch (legacy_input_format
) {
1817 case LEGACY_INPUT_FORMAT_CTF
:
1818 g_string_append(str
, "fs -p 'path=\"");
1820 case LEGACY_INPUT_FORMAT_LTTNG_LIVE
:
1821 g_string_append(str
, "lttng-live -p 'url=\"");
1827 g_string_append(str
, escaped_input
);
1828 g_string_append(str
, "\"");
1829 g_string_append_printf(str
, ",offset-s=%" PRId64
,
1830 ctf_legacy_opts
->offset_s
.value
);
1831 g_string_append_printf(str
, ",offset-ns=%" PRId64
,
1832 ctf_legacy_opts
->offset_ns
.value
);
1833 g_string_append_bool_param(str
, "stream-intersection",
1834 ctf_legacy_opts
->stream_intersection
);
1835 g_string_append(str
, "' ");
1836 g_free(escaped_input
);
1839 printf_err("%s\n\n", str
->str
);
1843 g_string_free(str
, TRUE
);
1849 * Validates a given configuration, with optional legacy input and
1850 * output formats options. Prints useful error messages if anything
1853 * Returns true when the configuration is valid.
1856 bool validate_cfg(struct bt_config
*cfg
,
1857 enum legacy_input_format
*legacy_input_format
,
1858 enum legacy_output_format
*legacy_output_format
,
1859 struct bt_value
*legacy_input_paths
,
1860 struct ctf_legacy_opts
*ctf_legacy_opts
,
1861 struct text_legacy_opts
*text_legacy_opts
)
1863 bool legacy_input
= false;
1864 bool legacy_output
= false;
1866 /* Determine if the input and output should be legacy-style */
1867 if (*legacy_input_format
!= LEGACY_INPUT_FORMAT_NONE
||
1868 !bt_value_array_is_empty(legacy_input_paths
) ||
1869 ctf_legacy_opts_is_any_set(ctf_legacy_opts
)) {
1870 legacy_input
= true;
1873 if (*legacy_output_format
!= LEGACY_OUTPUT_FORMAT_NONE
||
1874 text_legacy_opts_is_any_set(text_legacy_opts
)) {
1875 legacy_output
= true;
1879 /* If no legacy input format was specified, default to CTF */
1880 if (*legacy_input_format
== LEGACY_INPUT_FORMAT_NONE
) {
1881 *legacy_input_format
= LEGACY_INPUT_FORMAT_CTF
;
1884 /* Make sure at least one input path exists */
1885 if (bt_value_array_is_empty(legacy_input_paths
)) {
1886 switch (*legacy_input_format
) {
1887 case LEGACY_INPUT_FORMAT_CTF
:
1888 printf_err("No input path specified for legacy `ctf` input format\n");
1890 case LEGACY_INPUT_FORMAT_LTTNG_LIVE
:
1891 printf_err("No URL specified for legacy `lttng-live` input format\n");
1899 /* Make sure no non-legacy sources are specified */
1900 if (cfg
->cmd_data
.convert
.sources
->len
!= 0) {
1901 print_input_legacy_to_sources(*legacy_input_format
,
1902 legacy_input_paths
, ctf_legacy_opts
);
1907 if (legacy_output
) {
1909 * If no legacy output format was specified, default to
1912 if (*legacy_output_format
== LEGACY_OUTPUT_FORMAT_NONE
) {
1913 *legacy_output_format
= LEGACY_OUTPUT_FORMAT_TEXT
;
1917 * If any "text" option was specified, the output must be
1920 if (text_legacy_opts_is_any_set(text_legacy_opts
) &&
1921 *legacy_output_format
!=
1922 LEGACY_OUTPUT_FORMAT_TEXT
) {
1923 printf_err("Options for legacy `text` output format specified with a different legacy output format\n");
1927 /* Make sure no non-legacy sinks are specified */
1928 if (cfg
->cmd_data
.convert
.sinks
->len
!= 0) {
1929 print_output_legacy_to_sinks(*legacy_output_format
,
1936 * If the output is the legacy "ctf-metadata" format, then the
1937 * input should be the legacy "ctf" input format.
1939 if (*legacy_output_format
== LEGACY_OUTPUT_FORMAT_CTF_METADATA
&&
1940 *legacy_input_format
!= LEGACY_INPUT_FORMAT_CTF
) {
1941 printf_err("Legacy `ctf-metadata` output format requires using legacy `ctf` input format\n");
1952 * Parses a 64-bit signed integer.
1954 * Returns a negative value if anything goes wrong.
1957 int parse_int64(const char *arg
, int64_t *val
)
1962 *val
= strtoll(arg
, &endptr
, 0);
1963 if (*endptr
!= '\0' || arg
== endptr
|| errno
!= 0) {
1977 OPT_CLOCK_FORCE_CORRELATE
,
1980 OPT_CLOCK_OFFSET_NS
,
1984 OPT_DEBUG_INFO_FULL_PATH
,
1985 OPT_DEBUG_INFO_TARGET_PREFIX
,
1993 OPT_OMIT_HOME_PLUGIN_PATH
,
1994 OPT_OMIT_SYSTEM_PLUGIN_PATH
,
2000 OPT_RESET_BASE_PARAMS
,
2003 OPT_STREAM_INTERSECTION
,
2009 * Sets the value of a given legacy offset option and marks it as set.
2011 static void set_offset_value(struct offset_opt
*offset_opt
, int64_t value
)
2013 offset_opt
->value
= value
;
2014 offset_opt
->is_set
= true;
2017 enum bt_config_component_dest
{
2018 BT_CONFIG_COMPONENT_DEST_SOURCE
,
2019 BT_CONFIG_COMPONENT_DEST_SINK
,
2023 * Adds a configuration component to the appropriate configuration
2024 * array depending on the destination.
2026 static void add_cfg_comp(struct bt_config
*cfg
,
2027 struct bt_config_component
*cfg_comp
,
2028 enum bt_config_component_dest dest
)
2030 if (dest
== BT_CONFIG_COMPONENT_DEST_SOURCE
) {
2031 g_ptr_array_add(cfg
->cmd_data
.convert
.sources
, cfg_comp
);
2033 g_ptr_array_add(cfg
->cmd_data
.convert
.sinks
, cfg_comp
);
2037 static int split_timerange(const char *arg
, const char **begin
, const char **end
)
2041 /* Try to match [begin,end] */
2042 c
= strchr(arg
, '[');
2056 /* Try to match begin,end */
2070 static int append_env_var_plugin_paths(struct bt_value
*plugin_paths
)
2075 if (bt_common_is_setuid_setgid()) {
2076 printf_debug("Skipping non-system plugin paths for setuid/setgid binary\n");
2080 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
2085 ret
= bt_config_append_plugin_paths(plugin_paths
, envvar
);
2091 static int append_home_and_system_plugin_paths(struct bt_value
*plugin_paths
,
2092 bool omit_system_plugin_path
, bool omit_home_plugin_path
)
2096 if (!omit_home_plugin_path
) {
2097 if (bt_common_is_setuid_setgid()) {
2098 printf_debug("Skipping non-system plugin paths for setuid/setgid binary\n");
2100 char *home_plugin_dir
=
2101 bt_common_get_home_plugin_path();
2103 if (home_plugin_dir
) {
2104 ret
= bt_config_append_plugin_paths(
2107 free(home_plugin_dir
);
2110 printf_err("Invalid home plugin path\n");
2117 if (!omit_system_plugin_path
) {
2118 if (bt_config_append_plugin_paths(plugin_paths
,
2119 bt_common_get_system_plugin_path())) {
2120 printf_err("Invalid system plugin path\n");
2129 static int append_sources_from_implicit_params(GPtrArray
*sources
,
2130 struct bt_config_component
*implicit_source_comp
)
2133 size_t len
= sources
->len
;
2135 for (i
= 0; i
< len
; i
++) {
2136 struct bt_config_component
*comp
;
2137 struct bt_value
*params_to_set
;
2139 comp
= g_ptr_array_index(sources
, i
);
2140 params_to_set
= bt_value_map_extend(comp
->params
,
2141 implicit_source_comp
->params
);
2142 if (!params_to_set
) {
2143 printf_err("Cannot extend legacy component parameters with non-legacy parameters\n");
2146 BT_MOVE(comp
->params
, params_to_set
);
2153 static struct bt_config
*bt_config_base_create(enum bt_config_command command
)
2155 struct bt_config
*cfg
;
2158 cfg
= g_new0(struct bt_config
, 1);
2164 bt_object_init(cfg
, bt_config_destroy
);
2165 cfg
->command
= command
;
2175 static struct bt_config
*bt_config_convert_create(
2176 struct bt_value
*initial_plugin_paths
)
2178 struct bt_config
*cfg
;
2181 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_CONVERT
);
2187 cfg
->cmd_data
.convert
.sources
= g_ptr_array_new_with_free_func(
2188 (GDestroyNotify
) bt_put
);
2189 if (!cfg
->cmd_data
.convert
.sources
) {
2194 cfg
->cmd_data
.convert
.sinks
= g_ptr_array_new_with_free_func(
2195 (GDestroyNotify
) bt_put
);
2196 if (!cfg
->cmd_data
.convert
.sinks
) {
2201 if (initial_plugin_paths
) {
2202 cfg
->cmd_data
.convert
.plugin_paths
=
2203 bt_get(initial_plugin_paths
);
2205 cfg
->cmd_data
.convert
.plugin_paths
= bt_value_array_create();
2206 if (!cfg
->cmd_data
.convert
.plugin_paths
) {
2221 static struct bt_config
*bt_config_list_plugins_create(
2222 struct bt_value
*initial_plugin_paths
)
2224 struct bt_config
*cfg
;
2227 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_LIST_PLUGINS
);
2233 if (initial_plugin_paths
) {
2234 cfg
->cmd_data
.list_plugins
.plugin_paths
=
2235 bt_get(initial_plugin_paths
);
2237 cfg
->cmd_data
.list_plugins
.plugin_paths
=
2238 bt_value_array_create();
2239 if (!cfg
->cmd_data
.list_plugins
.plugin_paths
) {
2255 * Prints the list-plugins command usage.
2258 void print_list_plugins_usage(FILE *fp
)
2260 fprintf(fp
, "Usage: babeltrace [GENERAL OPTIONS] list-plugins [OPTIONS]\n");
2262 fprintf(fp
, "Options:\n");
2264 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2265 fprintf(fp
, " (~/.local/lib/babeltrace/plugins)\n");
2266 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2267 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2268 fprintf(fp
, " dynamic plugins can be loaded\n");
2269 fprintf(fp
, " -h --help Show this help and quit\n");
2271 fprintf(fp
, "See `babeltrace --help` for the list of general options.\n");
2274 static struct poptOption list_plugins_long_options
[] = {
2275 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
2276 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
2277 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
2278 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
2279 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
2280 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
2284 * Creates a Babeltrace config object from the arguments of a
2285 * list-plugins command.
2287 * *retcode is set to the appropriate exit code to use.
2289 struct bt_config
*bt_config_list_plugins_from_args(int argc
, const char *argv
[],
2290 int *retcode
, bool omit_system_plugin_path
,
2291 bool omit_home_plugin_path
,
2292 struct bt_value
*initial_plugin_paths
)
2294 poptContext pc
= NULL
;
2298 struct bt_config
*cfg
= NULL
;
2299 const char *leftover
;
2302 cfg
= bt_config_list_plugins_create(initial_plugin_paths
);
2308 cfg
->cmd_data
.list_plugins
.omit_system_plugin_path
= omit_system_plugin_path
;
2309 cfg
->cmd_data
.list_plugins
.omit_home_plugin_path
= omit_home_plugin_path
;
2310 ret
= append_env_var_plugin_paths(
2311 cfg
->cmd_data
.list_plugins
.plugin_paths
);
2313 printf_err("Cannot append plugin paths from BABELTRACE_PLUGIN_PATH\n");
2318 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
2319 list_plugins_long_options
, 0);
2321 printf_err("Cannot get popt context\n");
2325 poptReadDefaultConfig(pc
, 0);
2327 while ((opt
= poptGetNextOpt(pc
)) > 0) {
2328 arg
= poptGetOptArg(pc
);
2331 case OPT_PLUGIN_PATH
:
2332 if (bt_common_is_setuid_setgid()) {
2333 printf_debug("Skipping non-system plugin paths for setuid/setgid binary\n");
2335 if (bt_config_append_plugin_paths(
2336 cfg
->cmd_data
.list_plugins
.plugin_paths
,
2338 printf_err("Invalid --plugin-path option's argument:\n %s\n",
2344 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
2345 cfg
->cmd_data
.list_plugins
.omit_system_plugin_path
= true;
2347 case OPT_OMIT_HOME_PLUGIN_PATH
:
2348 cfg
->cmd_data
.list_plugins
.omit_home_plugin_path
= true;
2351 print_list_plugins_usage(stdout
);
2356 printf_err("Unknown command-line option specified (option code %d)\n",
2365 /* Check for option parsing error */
2367 printf_err("While parsing command-line options, at option %s: %s\n",
2368 poptBadOption(pc
, 0), poptStrerror(opt
));
2372 leftover
= poptGetArg(pc
);
2374 printf_err("Invalid argument: %s\n", leftover
);
2378 if (append_home_and_system_plugin_paths(
2379 cfg
->cmd_data
.list_plugins
.plugin_paths
,
2380 cfg
->cmd_data
.list_plugins
.omit_system_plugin_path
,
2381 cfg
->cmd_data
.list_plugins
.omit_home_plugin_path
)) {
2382 printf_err("Cannot append home and system plugin paths\n");
2394 poptFreeContext(pc
);
2403 * Prints the legacy, Babeltrace 1.x command usage. Those options are
2404 * still compatible in Babeltrace 2.x, but it is recommended to use
2405 * the more generic plugin/component parameters instead of those
2406 * hard-coded option names.
2409 void print_legacy_usage(FILE *fp
)
2411 fprintf(fp
, "Usage: babeltrace [OPTIONS] INPUT...\n");
2413 fprintf(fp
, "The following options are compatible with the Babeltrace 1.x options:\n");
2415 fprintf(fp
, " --clock-force-correlate Assume that clocks are inherently correlated\n");
2416 fprintf(fp
, " across traces\n");
2417 fprintf(fp
, " -d, --debug Enable debug mode\n");
2418 fprintf(fp
, " -i, --input-format=FORMAT Input trace format (default: ctf)\n");
2419 fprintf(fp
, " -l, --list List available formats\n");
2420 fprintf(fp
, " -o, --output-format=FORMAT Output trace format (default: text)\n");
2421 fprintf(fp
, " -v, --verbose Enable verbose output\n");
2422 fprintf(fp
, " --help-legacy Show this help and quit\n");
2423 fprintf(fp
, " -V, --version Show version and quit\n");
2425 fprintf(fp
, " Available input formats: ctf, lttng-live, ctf-metadata\n");
2426 fprintf(fp
, " Available output formats: text, dummy\n");
2428 fprintf(fp
, "Input formats specific options:\n");
2430 fprintf(fp
, " INPUT... Input trace file(s), directory(ies), or URLs\n");
2431 fprintf(fp
, " --clock-offset=SEC Set clock offset to SEC seconds\n");
2432 fprintf(fp
, " --clock-offset-ns=NS Set clock offset to NS nanoseconds\n");
2433 fprintf(fp
, " --stream-intersection Only process events when all streams are active\n");
2435 fprintf(fp
, "text output format specific options:\n");
2437 fprintf(fp
, " --clock-cycles Print timestamps in clock cycles\n");
2438 fprintf(fp
, " --clock-date Print timestamp dates\n");
2439 fprintf(fp
, " --clock-gmt Print and parse timestamps in GMT time zone\n");
2440 fprintf(fp
, " (default: local time zone)\n");
2441 fprintf(fp
, " --clock-seconds Print the timestamps as [SEC.NS]\n");
2442 fprintf(fp
, " (default format: [HH:MM:SS.NS])\n");
2443 fprintf(fp
, " --debug-info-dir=DIR Search for debug info in directory DIR\n");
2444 fprintf(fp
, " (default: `/usr/lib/debug`)\n");
2445 fprintf(fp
, " --debug-info-full-path Show full debug info source and binary paths\n");
2446 fprintf(fp
, " --debug-info-target-prefix=DIR Use directory DIR as a prefix when looking\n");
2447 fprintf(fp
, " up executables during debug info analysis\n");
2448 fprintf(fp
, " (default: `/usr/lib/debug`)\n");
2449 fprintf(fp
, " -f, --fields=NAME[,NAME]... Print additional fields:\n");
2450 fprintf(fp
, " all, trace, trace:hostname, trace:domain,\n");
2451 fprintf(fp
, " trace:procname, trace:vpid, loglevel, emf\n");
2452 fprintf(fp
, " (default: trace:hostname, trace:procname,\n");
2453 fprintf(fp
, " trace:vpid)\n");
2454 fprintf(fp
, " -n, --names=NAME[,NAME]... Print field names:\n");
2455 fprintf(fp
, " payload (or arg or args)\n");
2456 fprintf(fp
, " none, all, scope, header, context (or ctx)\n");
2457 fprintf(fp
, " (default: payload, context)\n");
2458 fprintf(fp
, " --no-delta Do not print time delta between consecutive\n");
2459 fprintf(fp
, " events\n");
2460 fprintf(fp
, " -w, --output=PATH Write output to PATH (default: standard output)\n");
2464 * Prints the convert command usage.
2467 void print_convert_usage(FILE *fp
)
2469 fprintf(fp
, "Usage: babeltrace [GENERAL OPTIONS] convert [OPTIONS]\n");
2471 fprintf(fp
, "Options:\n");
2473 fprintf(fp
, " -b, --base-params=PARAMS Set PARAMS as the current base parameters\n");
2474 fprintf(fp
, " for the following component instances\n");
2475 fprintf(fp
, " (see the expected format of PARAMS below)\n");
2476 fprintf(fp
, " --begin=BEGIN Set the `begin` parameter of the latest\n");
2477 fprintf(fp
, " source component instance to BEGIN\n");
2478 fprintf(fp
, " (see the suggested format of BEGIN below)\n");
2479 fprintf(fp
, " -d, --debug Enable debug mode\n");
2480 fprintf(fp
, " --end=END Set the `end` parameter of the latest\n");
2481 fprintf(fp
, " source component instance to END\n");
2482 fprintf(fp
, " (see the suggested format of BEGIN below)\n");
2483 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2484 fprintf(fp
, " (~/.local/lib/babeltrace/plugins)\n");
2485 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2486 fprintf(fp
, " -p, --params=PARAMS Set the parameters of the latest component\n");
2487 fprintf(fp
, " instance (in command-line order) to PARAMS\n");
2488 fprintf(fp
, " (see the expected format of PARAMS below)\n");
2489 fprintf(fp
, " -P, --path=PATH Set the `path` parameter of the latest\n");
2490 fprintf(fp
, " component instance to PATH\n");
2491 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2492 fprintf(fp
, " dynamic plugins can be loaded\n");
2493 fprintf(fp
, " -r, --reset-base-params Reset the current base parameters of the\n");
2494 fprintf(fp
, " following source and sink component\n");
2495 fprintf(fp
, " instances to an empty map\n");
2496 fprintf(fp
, " -o, --sink=PLUGIN.COMPCLS Instantiate a sink component from plugin\n");
2497 fprintf(fp
, " PLUGIN and component class COMPCLS (may be\n");
2498 fprintf(fp
, " repeated)\n");
2499 fprintf(fp
, " -i, --source=PLUGIN.COMPCLS Instantiate a source component from plugin\n");
2500 fprintf(fp
, " PLUGIN and component class COMPCLS (may be\n");
2501 fprintf(fp
, " repeated)\n");
2502 fprintf(fp
, " --timerange=TIMERANGE Set time range to TIMERANGE: BEGIN,END or\n");
2503 fprintf(fp
, " [BEGIN,END] (literally `[` and `]`)\n");
2504 fprintf(fp
, " (suggested format of BEGIN/END below)\n");
2505 fprintf(fp
, " -v, --verbose Enable verbose output\n");
2506 fprintf(fp
, " -h --help Show this help and quit\n");
2508 fprintf(fp
, "See `babeltrace --help` for the list of general options.\n");
2509 fprintf(fp
, "\n\n");
2510 fprintf(fp
, "Suggested format of BEGIN and END\n");
2511 fprintf(fp
, "---------------------------------\n");
2513 fprintf(fp
, " [YYYY-MM-DD [hh:mm:]]ss[.nnnnnnnnn]\n");
2514 fprintf(fp
, "\n\n");
2515 fprintf(fp
, "Expected format of PARAMS\n");
2516 fprintf(fp
, "-------------------------\n");
2518 fprintf(fp
, " PARAM=VALUE[,PARAM=VALUE]...\n");
2520 fprintf(fp
, "The parameter string is a comma-separated list of PARAM=VALUE assignments,\n");
2521 fprintf(fp
, "where PARAM is the parameter name (C identifier plus [:.-] characters), and\n");
2522 fprintf(fp
, "VALUE can be one of:\n");
2524 fprintf(fp
, "* `null`, `nul`, `NULL`: null value (no backticks).\n");
2525 fprintf(fp
, "* `true`, `TRUE`, `yes`, `YES`: true boolean value (no backticks).\n");
2526 fprintf(fp
, "* `false`, `FALSE`, `no`, `NO`: false boolean value (no backticks).\n");
2527 fprintf(fp
, "* Binary (`0b` prefix), octal (`0` prefix), decimal, or hexadecimal\n");
2528 fprintf(fp
, " (`0x` prefix) signed 64-bit integer.\n");
2529 fprintf(fp
, "* Double precision floating point number (scientific notation is accepted).\n");
2530 fprintf(fp
, "* Unquoted string with no special characters, and not matching any of\n");
2531 fprintf(fp
, " the null and boolean value symbols above.\n");
2532 fprintf(fp
, "* Double-quoted string (accepts escape characters).\n");
2534 fprintf(fp
, "Whitespaces are allowed around individual `=` and `,` tokens.\n");
2536 fprintf(fp
, "Example:\n");
2538 fprintf(fp
, " many=null, fresh=yes, condition=false, squirrel=-782329,\n");
2539 fprintf(fp
, " observe=3.14, simple=beef, needs-quotes=\"some string\",\n");
2540 fprintf(fp
, " escape.chars-are:allowed=\"this is a \\\" double quote\"\n");
2542 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run babeltrace\n");
2543 fprintf(fp
, "from a shell.\n");
2546 static struct poptOption convert_long_options
[] = {
2547 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
2548 { "base-params", 'b', POPT_ARG_STRING
, NULL
, OPT_BASE_PARAMS
, NULL
, NULL
},
2549 { "begin", '\0', POPT_ARG_STRING
, NULL
, OPT_BEGIN
, NULL
, NULL
},
2550 { "clock-cycles", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_CYCLES
, NULL
, NULL
},
2551 { "clock-date", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_DATE
, NULL
, NULL
},
2552 { "clock-force-correlate", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_FORCE_CORRELATE
, NULL
, NULL
},
2553 { "clock-gmt", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_GMT
, NULL
, NULL
},
2554 { "clock-offset", '\0', POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET
, NULL
, NULL
},
2555 { "clock-offset-ns", '\0', POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET_NS
, NULL
, NULL
},
2556 { "clock-seconds", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_SECONDS
, NULL
, NULL
},
2557 { "debug", 'd', POPT_ARG_NONE
, NULL
, OPT_DEBUG
, NULL
, NULL
},
2558 { "debug-info-dir", 0, POPT_ARG_STRING
, NULL
, OPT_DEBUG_INFO_DIR
, NULL
, NULL
},
2559 { "debug-info-full-path", 0, POPT_ARG_NONE
, NULL
, OPT_DEBUG_INFO_FULL_PATH
, NULL
, NULL
},
2560 { "debug-info-target-prefix", 0, POPT_ARG_STRING
, NULL
, OPT_DEBUG_INFO_TARGET_PREFIX
, NULL
, NULL
},
2561 { "end", '\0', POPT_ARG_STRING
, NULL
, OPT_END
, NULL
, NULL
},
2562 { "fields", 'f', POPT_ARG_STRING
, NULL
, OPT_FIELDS
, NULL
, NULL
},
2563 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
2564 { "input-format", 'i', POPT_ARG_STRING
, NULL
, OPT_INPUT_FORMAT
, NULL
, NULL
},
2565 { "names", 'n', POPT_ARG_STRING
, NULL
, OPT_NAMES
, NULL
, NULL
},
2566 { "no-delta", '\0', POPT_ARG_NONE
, NULL
, OPT_NO_DELTA
, NULL
, NULL
},
2567 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
2568 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
2569 { "output", 'w', POPT_ARG_STRING
, NULL
, OPT_OUTPUT_PATH
, NULL
, NULL
},
2570 { "output-format", 'o', POPT_ARG_STRING
, NULL
, OPT_OUTPUT_FORMAT
, NULL
, NULL
},
2571 { "params", 'p', POPT_ARG_STRING
, NULL
, OPT_PARAMS
, NULL
, NULL
},
2572 { "path", 'P', POPT_ARG_STRING
, NULL
, OPT_PATH
, NULL
, NULL
},
2573 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
2574 { "reset-base-params", 'r', POPT_ARG_NONE
, NULL
, OPT_RESET_BASE_PARAMS
, NULL
, NULL
},
2575 { "sink", '\0', POPT_ARG_STRING
, NULL
, OPT_SINK
, NULL
, NULL
},
2576 { "source", '\0', POPT_ARG_STRING
, NULL
, OPT_SOURCE
, NULL
, NULL
},
2577 { "stream-intersection", '\0', POPT_ARG_NONE
, NULL
, OPT_STREAM_INTERSECTION
, NULL
, NULL
},
2578 { "timerange", '\0', POPT_ARG_STRING
, NULL
, OPT_TIMERANGE
, NULL
, NULL
},
2579 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
2580 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
2584 * Creates a Babeltrace config object from the arguments of a convert
2587 * *retcode is set to the appropriate exit code to use.
2589 struct bt_config
*bt_config_convert_from_args(int argc
, const char *argv
[],
2590 int *retcode
, bool omit_system_plugin_path
,
2591 bool omit_home_plugin_path
,
2592 struct bt_value
*initial_plugin_paths
)
2594 poptContext pc
= NULL
;
2596 struct ctf_legacy_opts ctf_legacy_opts
;
2597 struct text_legacy_opts text_legacy_opts
;
2598 enum legacy_input_format legacy_input_format
= LEGACY_INPUT_FORMAT_NONE
;
2599 enum legacy_output_format legacy_output_format
=
2600 LEGACY_OUTPUT_FORMAT_NONE
;
2601 struct bt_value
*legacy_input_paths
= NULL
;
2602 struct bt_config_component
*implicit_source_comp
= NULL
;
2603 struct bt_config_component
*cur_cfg_comp
= NULL
;
2604 bool cur_is_implicit_source
= false;
2605 bool use_implicit_source
= false;
2606 enum bt_config_component_dest cur_cfg_comp_dest
=
2607 BT_CONFIG_COMPONENT_DEST_SOURCE
;
2608 struct bt_value
*cur_base_params
= NULL
;
2610 struct bt_config
*cfg
= NULL
;
2613 memset(&ctf_legacy_opts
, 0, sizeof(ctf_legacy_opts
));
2614 memset(&text_legacy_opts
, 0, sizeof(text_legacy_opts
));
2617 print_convert_usage(stdout
);
2622 cfg
= bt_config_convert_create(initial_plugin_paths
);
2628 cfg
->cmd_data
.convert
.omit_system_plugin_path
= omit_system_plugin_path
;
2629 cfg
->cmd_data
.convert
.omit_home_plugin_path
= omit_home_plugin_path
;
2630 text_legacy_opts
.output
= g_string_new(NULL
);
2631 if (!text_legacy_opts
.output
) {
2636 text_legacy_opts
.dbg_info_dir
= g_string_new(NULL
);
2637 if (!text_legacy_opts
.dbg_info_dir
) {
2642 text_legacy_opts
.dbg_info_target_prefix
= g_string_new(NULL
);
2643 if (!text_legacy_opts
.dbg_info_target_prefix
) {
2648 cur_base_params
= bt_value_map_create();
2649 if (!cur_base_params
) {
2654 legacy_input_paths
= bt_value_array_create();
2655 if (!legacy_input_paths
) {
2660 ret
= append_env_var_plugin_paths(cfg
->cmd_data
.convert
.plugin_paths
);
2662 printf_err("Cannot append plugin paths from BABELTRACE_PLUGIN_PATH\n");
2666 /* Note: implicit source never gets positional base params. */
2667 implicit_source_comp
= bt_config_component_from_arg(DEFAULT_SOURCE_COMPONENT_NAME
);
2668 if (implicit_source_comp
) {
2669 cur_cfg_comp
= implicit_source_comp
;
2670 cur_is_implicit_source
= true;
2671 use_implicit_source
= true;
2673 printf_debug("Cannot find implicit source plugin `%s`",
2674 DEFAULT_SOURCE_COMPONENT_NAME
);
2678 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
2679 convert_long_options
, 0);
2681 printf_err("Cannot get popt context\n");
2685 poptReadDefaultConfig(pc
, 0);
2687 while ((opt
= poptGetNextOpt(pc
)) > 0) {
2688 arg
= poptGetOptArg(pc
);
2691 case OPT_PLUGIN_PATH
:
2692 if (bt_common_is_setuid_setgid()) {
2693 printf_debug("Skipping non-system plugin paths for setuid/setgid binary\n");
2695 if (bt_config_append_plugin_paths(
2696 cfg
->cmd_data
.convert
.plugin_paths
,
2698 printf_err("Invalid --plugin-path option's argument:\n %s\n",
2704 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
2705 cfg
->cmd_data
.convert
.omit_system_plugin_path
= true;
2707 case OPT_OMIT_HOME_PLUGIN_PATH
:
2708 cfg
->cmd_data
.convert
.omit_home_plugin_path
= true;
2710 case OPT_OUTPUT_PATH
:
2711 if (text_legacy_opts
.output
->len
> 0) {
2712 printf_err("Duplicate --output option\n");
2716 g_string_assign(text_legacy_opts
.output
, arg
);
2718 case OPT_DEBUG_INFO_DIR
:
2719 if (text_legacy_opts
.dbg_info_dir
->len
> 0) {
2720 printf_err("Duplicate --debug-info-dir option\n");
2724 g_string_assign(text_legacy_opts
.dbg_info_dir
, arg
);
2726 case OPT_DEBUG_INFO_TARGET_PREFIX
:
2727 if (text_legacy_opts
.dbg_info_target_prefix
->len
> 0) {
2728 printf_err("Duplicate --debug-info-target-prefix option\n");
2732 g_string_assign(text_legacy_opts
.dbg_info_target_prefix
, arg
);
2734 case OPT_INPUT_FORMAT
:
2737 if (opt
== OPT_INPUT_FORMAT
) {
2738 if (!strcmp(arg
, "ctf")) {
2739 /* Legacy CTF input format */
2740 if (legacy_input_format
) {
2741 print_err_dup_legacy_input();
2745 legacy_input_format
=
2746 LEGACY_INPUT_FORMAT_CTF
;
2748 } else if (!strcmp(arg
, "lttng-live")) {
2749 /* Legacy LTTng-live input format */
2750 if (legacy_input_format
) {
2751 print_err_dup_legacy_input();
2755 legacy_input_format
=
2756 LEGACY_INPUT_FORMAT_LTTNG_LIVE
;
2761 use_implicit_source
= false;
2763 /* Non-legacy: try to create a component config */
2764 if (cur_cfg_comp
&& !cur_is_implicit_source
) {
2765 add_cfg_comp(cfg
, cur_cfg_comp
,
2769 cur_cfg_comp
= bt_config_component_from_arg(arg
);
2770 if (!cur_cfg_comp
) {
2771 printf_err("Invalid format for --source option's argument:\n %s\n",
2775 cur_is_implicit_source
= false;
2777 assert(cur_base_params
);
2778 bt_put(cur_cfg_comp
->params
);
2779 cur_cfg_comp
->params
= bt_value_copy(cur_base_params
);
2780 if (!cur_cfg_comp
->params
) {
2785 cur_cfg_comp_dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
2788 case OPT_OUTPUT_FORMAT
:
2791 if (opt
== OPT_OUTPUT_FORMAT
) {
2792 if (!strcmp(arg
, "text")) {
2793 /* Legacy CTF-text output format */
2794 if (legacy_output_format
) {
2795 print_err_dup_legacy_output();
2799 legacy_output_format
=
2800 LEGACY_OUTPUT_FORMAT_TEXT
;
2802 } else if (!strcmp(arg
, "dummy")) {
2803 /* Legacy dummy output format */
2804 if (legacy_output_format
) {
2805 print_err_dup_legacy_output();
2809 legacy_output_format
=
2810 LEGACY_OUTPUT_FORMAT_DUMMY
;
2812 } else if (!strcmp(arg
, "ctf-metadata")) {
2813 /* Legacy CTF-metadata output format */
2814 if (legacy_output_format
) {
2815 print_err_dup_legacy_output();
2819 legacy_output_format
=
2820 LEGACY_OUTPUT_FORMAT_CTF_METADATA
;
2825 /* Non-legacy: try to create a component config */
2826 if (cur_cfg_comp
&& !cur_is_implicit_source
) {
2827 add_cfg_comp(cfg
, cur_cfg_comp
,
2831 cur_cfg_comp
= bt_config_component_from_arg(arg
);
2832 if (!cur_cfg_comp
) {
2833 printf_err("Invalid format for --sink option's argument:\n %s\n",
2837 cur_is_implicit_source
= false;
2839 assert(cur_base_params
);
2840 bt_put(cur_cfg_comp
->params
);
2841 cur_cfg_comp
->params
= bt_value_copy(cur_base_params
);
2842 if (!cur_cfg_comp
->params
) {
2847 cur_cfg_comp_dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
2852 struct bt_value
*params
;
2853 struct bt_value
*params_to_set
;
2855 if (!cur_cfg_comp
) {
2856 printf_err("Cannot add parameters to unavailable default source component `%s`:\n %s\n",
2857 DEFAULT_SOURCE_COMPONENT_NAME
, arg
);
2861 params
= bt_value_from_arg(arg
);
2863 printf_err("Invalid format for --params option's argument:\n %s\n",
2868 params_to_set
= bt_value_map_extend(cur_cfg_comp
->params
,
2871 if (!params_to_set
) {
2872 printf_err("Cannot extend current component parameters with --params option's argument:\n %s\n",
2877 BT_MOVE(cur_cfg_comp
->params
, params_to_set
);
2881 if (!cur_cfg_comp
) {
2882 printf_err("Cannot add `path` parameter to unavailable default source component `%s`:\n %s\n",
2883 DEFAULT_SOURCE_COMPONENT_NAME
, arg
);
2887 assert(cur_cfg_comp
->params
);
2889 if (bt_value_map_insert_string(cur_cfg_comp
->params
,
2895 case OPT_BASE_PARAMS
:
2897 struct bt_value
*params
= bt_value_from_arg(arg
);
2900 printf_err("Invalid format for --base-params option's argument:\n %s\n",
2905 BT_MOVE(cur_base_params
, params
);
2908 case OPT_RESET_BASE_PARAMS
:
2909 BT_PUT(cur_base_params
);
2910 cur_base_params
= bt_value_map_create();
2911 if (!cur_base_params
) {
2917 if (text_legacy_opts
.names
) {
2918 printf_err("Duplicate --names option\n");
2922 text_legacy_opts
.names
= names_from_arg(arg
);
2923 if (!text_legacy_opts
.names
) {
2924 printf_err("Invalid --names option's argument:\n %s\n",
2930 if (text_legacy_opts
.fields
) {
2931 printf_err("Duplicate --fields option\n");
2935 text_legacy_opts
.fields
= fields_from_arg(arg
);
2936 if (!text_legacy_opts
.fields
) {
2937 printf_err("Invalid --fields option's argument:\n %s\n",
2943 text_legacy_opts
.no_delta
= true;
2945 case OPT_CLOCK_CYCLES
:
2946 text_legacy_opts
.clock_cycles
= true;
2948 case OPT_CLOCK_SECONDS
:
2949 text_legacy_opts
.clock_seconds
= true;
2951 case OPT_CLOCK_DATE
:
2952 text_legacy_opts
.clock_date
= true;
2955 text_legacy_opts
.clock_gmt
= true;
2957 case OPT_DEBUG_INFO_FULL_PATH
:
2958 text_legacy_opts
.dbg_info_full_path
= true;
2960 case OPT_CLOCK_OFFSET
:
2964 if (ctf_legacy_opts
.offset_s
.is_set
) {
2965 printf_err("Duplicate --clock-offset option\n");
2969 if (parse_int64(arg
, &val
)) {
2970 printf_err("Invalid --clock-offset option's argument:\n %s\n",
2975 set_offset_value(&ctf_legacy_opts
.offset_s
, val
);
2978 case OPT_CLOCK_OFFSET_NS
:
2982 if (ctf_legacy_opts
.offset_ns
.is_set
) {
2983 printf_err("Duplicate --clock-offset-ns option\n");
2987 if (parse_int64(arg
, &val
)) {
2988 printf_err("Invalid --clock-offset-ns option's argument:\n %s\n",
2993 set_offset_value(&ctf_legacy_opts
.offset_ns
, val
);
2996 case OPT_STREAM_INTERSECTION
:
2997 ctf_legacy_opts
.stream_intersection
= true;
2999 case OPT_CLOCK_FORCE_CORRELATE
:
3000 cfg
->cmd_data
.convert
.force_correlate
= true;
3003 if (!cur_cfg_comp
) {
3004 printf_err("Cannot add `begin` parameter to unavailable default source component `%s`:\n %s\n",
3005 DEFAULT_SOURCE_COMPONENT_NAME
, arg
);
3008 if (cur_cfg_comp_dest
!= BT_CONFIG_COMPONENT_DEST_SOURCE
) {
3009 printf_err("--begin option must follow a --source option:\n %s\n",
3013 if (bt_value_map_insert_string(cur_cfg_comp
->params
,
3020 if (!cur_cfg_comp
) {
3021 printf_err("Cannot add `end` parameter to unavailable default source component `%s`:\n %s\n",
3022 DEFAULT_SOURCE_COMPONENT_NAME
, arg
);
3025 if (cur_cfg_comp_dest
!= BT_CONFIG_COMPONENT_DEST_SOURCE
) {
3026 printf_err("--end option must follow a --source option:\n %s\n",
3030 if (bt_value_map_insert_string(cur_cfg_comp
->params
,
3038 const char *begin
, *end
;
3040 if (!cur_cfg_comp
) {
3041 printf_err("Cannot add `begin` and `end` parameters to unavailable default source component `%s`:\n %s\n",
3042 DEFAULT_SOURCE_COMPONENT_NAME
, arg
);
3045 if (cur_cfg_comp_dest
!= BT_CONFIG_COMPONENT_DEST_SOURCE
) {
3046 printf_err("--timerange option must follow a --source option:\n %s\n",
3050 if (split_timerange(arg
, &begin
, &end
)) {
3051 printf_err("Invalid --timerange format: expecting BEGIN,END or [BEGIN,END]:\n %s\n",
3055 if (bt_value_map_insert_string(cur_cfg_comp
->params
,
3060 if (bt_value_map_insert_string(cur_cfg_comp
->params
,
3068 print_convert_usage(stdout
);
3073 cfg
->verbose
= true;
3079 printf_err("Unknown command-line option specified (option code %d)\n",
3088 /* Check for option parsing error */
3090 printf_err("While parsing command-line options, at option %s: %s\n",
3091 poptBadOption(pc
, 0), poptStrerror(opt
));
3095 /* Consume leftover arguments as legacy input paths */
3097 const char *input_path
= poptGetArg(pc
);
3103 if (bt_value_array_append_string(legacy_input_paths
,
3110 if (append_home_and_system_plugin_paths(
3111 cfg
->cmd_data
.convert
.plugin_paths
,
3112 cfg
->cmd_data
.convert
.omit_system_plugin_path
,
3113 cfg
->cmd_data
.convert
.omit_home_plugin_path
)) {
3114 printf_err("Cannot append home and system plugin paths\n");
3118 /* Append current component configuration, if any */
3119 if (cur_cfg_comp
&& !cur_is_implicit_source
) {
3120 add_cfg_comp(cfg
, cur_cfg_comp
, cur_cfg_comp_dest
);
3122 cur_cfg_comp
= NULL
;
3124 /* Validate legacy/non-legacy options */
3125 if (!validate_cfg(cfg
, &legacy_input_format
, &legacy_output_format
,
3126 legacy_input_paths
, &ctf_legacy_opts
,
3127 &text_legacy_opts
)) {
3128 printf_err("Command-line options form an invalid configuration\n");
3133 * If there's a legacy input format, convert it to source
3134 * component configurations.
3136 if (legacy_input_format
) {
3137 if (append_sources_from_legacy_opts(
3138 cfg
->cmd_data
.convert
.sources
,
3139 legacy_input_format
, &ctf_legacy_opts
,
3140 legacy_input_paths
)) {
3141 printf_err("Cannot convert legacy input format options to source component instance(s)\n");
3144 if (append_sources_from_implicit_params(
3145 cfg
->cmd_data
.convert
.sources
,
3146 implicit_source_comp
)) {
3147 printf_err("Cannot initialize legacy component parameters\n");
3150 use_implicit_source
= false;
3152 if (use_implicit_source
) {
3153 add_cfg_comp(cfg
, implicit_source_comp
,
3154 BT_CONFIG_COMPONENT_DEST_SOURCE
);
3155 implicit_source_comp
= NULL
;
3157 if (implicit_source_comp
3158 && !bt_value_map_is_empty(implicit_source_comp
->params
)) {
3159 printf_err("Arguments specified for implicit source, but an explicit source has been specified, overriding it\n");
3166 * If there's a legacy output format, convert it to sink
3167 * component configurations.
3169 if (legacy_output_format
) {
3170 if (append_sinks_from_legacy_opts(cfg
->cmd_data
.convert
.sinks
,
3171 legacy_output_format
, &text_legacy_opts
)) {
3172 printf_err("Cannot convert legacy output format options to sink component instance(s)\n");
3177 if (cfg
->cmd_data
.convert
.sinks
->len
== 0) {
3178 /* Use implicit sink as default. */
3179 cur_cfg_comp
= bt_config_component_from_arg(DEFAULT_SINK_COMPONENT_NAME
);
3180 if (!cur_cfg_comp
) {
3181 printf_error("Cannot find implicit sink plugin `%s`\n",
3182 DEFAULT_SINK_COMPONENT_NAME
);
3184 add_cfg_comp(cfg
, cur_cfg_comp
,
3185 BT_CONFIG_COMPONENT_DEST_SINK
);
3186 cur_cfg_comp
= NULL
;
3197 poptFreeContext(pc
);
3200 if (text_legacy_opts
.output
) {
3201 g_string_free(text_legacy_opts
.output
, TRUE
);
3204 if (text_legacy_opts
.dbg_info_dir
) {
3205 g_string_free(text_legacy_opts
.dbg_info_dir
, TRUE
);
3208 if (text_legacy_opts
.dbg_info_target_prefix
) {
3209 g_string_free(text_legacy_opts
.dbg_info_target_prefix
, TRUE
);
3213 BT_PUT(implicit_source_comp
);
3214 BT_PUT(cur_cfg_comp
);
3215 BT_PUT(cur_base_params
);
3216 BT_PUT(text_legacy_opts
.names
);
3217 BT_PUT(text_legacy_opts
.fields
);
3218 BT_PUT(legacy_input_paths
);
3223 * Prints the Babeltrace 2.x general usage.
3226 void print_gen_usage(FILE *fp
)
3228 fprintf(fp
, "Usage: babeltrace [GENERAL OPTIONS] [COMMAND] [COMMAND OPTIONS]\n");
3230 fprintf(fp
, "General options:\n");
3232 fprintf(fp
, " -d, --debug Enable debug mode\n");
3233 fprintf(fp
, " -h --help Show this help and quit\n");
3234 fprintf(fp
, " --help-legacy Show Babeltrace 1.x legacy help and quit\n");
3235 fprintf(fp
, " -v, --verbose Enable verbose output\n");
3236 fprintf(fp
, " -V, --version Show version and quit\n");
3238 fprintf(fp
, "Available commands:\n");
3240 fprintf(fp
, " convert Build a trace conversion graph and run it (default)\n");
3241 fprintf(fp
, " list-plugins List available plugins and their content\n");
3243 fprintf(fp
, "Use `babeltrace COMMAND --help` to show the help of COMMAND.\n");
3246 struct bt_config
*bt_config_from_args(int argc
, const char *argv
[],
3247 int *retcode
, bool omit_system_plugin_path
,
3248 bool omit_home_plugin_path
,
3249 struct bt_value
*initial_plugin_paths
)
3251 struct bt_config
*config
= NULL
;
3252 bool verbose
= false;
3255 enum bt_config_command command
= -1;
3256 const char **command_argv
= NULL
;
3257 int command_argc
= -1;
3258 const char *command_name
= NULL
;
3263 print_gen_usage(stdout
);
3267 for (i
= 1; i
< argc
; i
++) {
3268 const char *cur_arg
= argv
[i
];
3270 if (strcmp(cur_arg
, "-d") == 0 ||
3271 strcmp(cur_arg
, "--debug") == 0) {
3273 } else if (strcmp(cur_arg
, "-v") == 0 ||
3274 strcmp(cur_arg
, "--verbose") == 0) {
3276 } else if (strcmp(cur_arg
, "-V") == 0 ||
3277 strcmp(cur_arg
, "--version") == 0) {
3280 } else if (strcmp(cur_arg
, "-h") == 0 ||
3281 strcmp(cur_arg
, "--help") == 0) {
3282 print_gen_usage(stdout
);
3284 } else if (strcmp(cur_arg
, "--help-legacy") == 0) {
3285 print_legacy_usage(stdout
);
3289 * First unknown argument: is it a known command
3292 if (strcmp(cur_arg
, "convert") == 0) {
3293 command
= BT_CONFIG_COMMAND_CONVERT
;
3294 command_argv
= &argv
[i
];
3295 command_argc
= argc
- i
;
3296 command_name
= cur_arg
;
3297 } else if (strcmp(cur_arg
, "list-plugins") == 0) {
3298 command
= BT_CONFIG_COMMAND_LIST_PLUGINS
;
3299 command_argv
= &argv
[i
];
3300 command_argc
= argc
- i
;
3301 command_name
= cur_arg
;
3304 * Unknown argument, but not a known
3305 * command name: assume the whole
3306 * arguments are for the default convert
3309 command
= BT_CONFIG_COMMAND_CONVERT
;
3310 command_argv
= argv
;
3311 command_argc
= argc
;
3317 if ((int) command
< 0) {
3319 * We only got non-help, non-version general options
3320 * like --verbose and --debug, without any other
3321 * arguments, so we can't do anything useful: print the
3324 print_gen_usage(stdout
);
3328 assert(command_argv
);
3329 assert(command_argc
>= 0);
3332 case BT_CONFIG_COMMAND_CONVERT
:
3333 config
= bt_config_convert_from_args(command_argc
, command_argv
,
3334 retcode
, omit_system_plugin_path
,
3335 omit_home_plugin_path
, initial_plugin_paths
);
3337 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
3338 config
= bt_config_list_plugins_from_args(command_argc
,
3339 command_argv
, retcode
, omit_system_plugin_path
,
3340 omit_home_plugin_path
, initial_plugin_paths
);
3348 config
->verbose
= true;
3352 config
->debug
= true;
3355 config
->command_name
= command_name
;