2 * Babeltrace trace converter - parameter parsing
4 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #define BT_LOG_TAG "CLI-CFG-CLI-ARGS"
31 #include <babeltrace/assert-internal.h>
35 #include <babeltrace/babeltrace.h>
36 #include <babeltrace/common-internal.h>
39 #include <sys/types.h>
40 #include "babeltrace-cfg.h"
41 #include "babeltrace-cfg-cli-args.h"
42 #include "babeltrace-cfg-cli-args-connect.h"
46 * Error printf() macro which prepends "Error: " the first time it's
47 * called. This gives a nicer feel than having a bunch of error prefixes
48 * (since the following lines usually describe the error and possible
49 * solutions), or the error prefix just at the end.
51 #define printf_err(fmt, args...) \
53 if (is_first_error) { \
54 fprintf(stderr, "Command line error: "); \
55 is_first_error = false; \
57 fprintf(stderr, fmt, ##args); \
60 static bool is_first_error
= true;
62 /* INI-style parsing FSM states */
63 enum ini_parsing_fsm_state
{
64 /* Expect a map key (identifier) */
67 /* Expect an equal character ('=') */
73 /* Expect a comma character (',') */
77 /* INI-style parsing state variables */
78 struct ini_parsing_state
{
79 /* Lexical scanner (owned by this) */
82 /* Output map value object being filled (owned by this) */
85 /* Next expected FSM state */
86 enum ini_parsing_fsm_state expecting
;
88 /* Last decoded map key (owned by this) */
91 /* Complete INI-style string to parse (not owned by this) */
94 /* Error buffer (not owned by this) */
98 /* Offset option with "is set" boolean */
104 /* Legacy "ctf"/"lttng-live" format options */
105 struct ctf_legacy_opts
{
106 struct offset_opt offset_s
;
107 struct offset_opt offset_ns
;
108 bool stream_intersection
;
111 /* Legacy "text" format options */
112 struct text_legacy_opts
{
114 * output, dbg_info_dir, dbg_info_target_prefix, names,
115 * and fields are owned by this.
118 GString
*dbg_info_dir
;
119 GString
*dbg_info_target_prefix
;
120 const bt_value
*names
;
121 const bt_value
*fields
;
129 bool dbg_info_full_path
;
133 /* Legacy input format format */
134 enum legacy_input_format
{
135 LEGACY_INPUT_FORMAT_NONE
= 0,
136 LEGACY_INPUT_FORMAT_CTF
,
137 LEGACY_INPUT_FORMAT_LTTNG_LIVE
,
140 /* Legacy output format format */
141 enum legacy_output_format
{
142 LEGACY_OUTPUT_FORMAT_NONE
= 0,
143 LEGACY_OUTPUT_FORMAT_TEXT
,
144 LEGACY_OUTPUT_FORMAT_DUMMY
,
148 * Prints the "out of memory" error.
151 void print_err_oom(void)
153 printf_err("Out of memory\n");
157 * Appends an "expecting token" error to the INI-style parsing state's
161 void ini_append_error_expecting(struct ini_parsing_state
*state
,
162 GScanner
*scanner
, const char *expecting
)
167 g_string_append_printf(state
->ini_error
, "Expecting %s:\n", expecting
);
169 /* Only print error if there's one line */
170 if (strchr(state
->arg
, '\n') != NULL
|| strlen(state
->arg
) == 0) {
174 g_string_append_printf(state
->ini_error
, "\n %s\n", state
->arg
);
175 pos
= g_scanner_cur_position(scanner
) + 4;
177 if (!g_scanner_eof(scanner
)) {
181 for (i
= 0; i
< pos
; ++i
) {
182 g_string_append_printf(state
->ini_error
, " ");
185 g_string_append_printf(state
->ini_error
, "^\n\n");
188 /* Parse the next token as a number and return its negation. */
190 bt_value
*ini_parse_neg_number(struct ini_parsing_state
*state
)
192 bt_value
*value
= NULL
;
193 GTokenType token_type
= g_scanner_get_next_token(state
->scanner
);
195 switch (token_type
) {
198 /* Negative integer */
199 uint64_t int_val
= state
->scanner
->value
.v_int64
;
201 if (int_val
> (((uint64_t) INT64_MAX
) + 1)) {
202 g_string_append_printf(state
->ini_error
,
203 "Integer value -%" PRIu64
" is outside the range of a 64-bit signed integer\n",
206 value
= bt_value_integer_create_init(-((int64_t) int_val
));
212 /* Negative floating point number */
213 value
= bt_value_real_create_init(-state
->scanner
->value
.v_float
);
216 ini_append_error_expecting(state
, state
->scanner
, "value");
223 static bt_value
*ini_parse_value(struct ini_parsing_state
*state
);
226 * Parse the current and following tokens as an array. Arrays are formatted as
227 * an opening `[`, a list of comma-separated values and a closing `]`. For
228 * convenience we support an optional trailing comma, after the last value.
230 * The current token of the parser must be the opening square bracket of the
234 bt_value
*ini_parse_array(struct ini_parsing_state
*state
)
236 /* The [ character must have already been ingested. */
237 BT_ASSERT(g_scanner_cur_token(state
->scanner
) == G_TOKEN_CHAR
);
238 BT_ASSERT(g_scanner_cur_value(state
->scanner
).v_char
== '[');
240 bt_value
*array_value
;
241 GTokenType token_type
;
243 array_value
= bt_value_array_create ();
244 token_type
= g_scanner_get_next_token(state
->scanner
);
246 /* While the current token is not a ]... */
247 while (!(token_type
== G_TOKEN_CHAR
&& g_scanner_cur_value(state
->scanner
).v_char
== ']')) {
248 /* Parse the item... */
249 bt_value
*item_value
;
250 bt_value_status status
;
252 item_value
= ini_parse_value(state
);
257 /* ... and add it to the result array. */
258 status
= bt_value_array_append_element(array_value
, item_value
);
259 BT_VALUE_PUT_REF_AND_RESET(item_value
);
261 if (status
!= BT_VALUE_STATUS_OK
) {
266 * Ingest the token following the value, it should be either a
267 * comma or closing square brace.
269 token_type
= g_scanner_get_next_token(state
->scanner
);
271 if (token_type
== G_TOKEN_CHAR
&& g_scanner_cur_value(state
->scanner
).v_char
== ',') {
273 * Ingest the token following the comma. If it happens
274 * to be a closing square bracket, we'll exit the loop
275 * and we are done (we allow trailing commas).
276 * Otherwise, we are ready for the next ini_parse_value call.
278 token_type
= g_scanner_get_next_token(state
->scanner
);
279 } else if (token_type
!= G_TOKEN_CHAR
|| g_scanner_cur_value(state
->scanner
).v_char
!= ']') {
280 ini_append_error_expecting(state
, state
->scanner
, ", or ]");
288 BT_VALUE_PUT_REF_AND_RESET(array_value
);
295 * Parse the current token (and the following ones if needed) as a value, return
299 bt_value
*ini_parse_value(struct ini_parsing_state
*state
)
301 bt_value
*value
= NULL
;
302 GTokenType token_type
= state
->scanner
->token
;
304 switch (token_type
) {
306 if (state
->scanner
->value
.v_char
== '-') {
307 /* Negative number */
308 value
= ini_parse_neg_number(state
);
309 } else if (state
->scanner
->value
.v_char
== '[') {
311 value
= ini_parse_array(state
);
313 ini_append_error_expecting(state
, state
->scanner
, "value");
318 /* Positive integer */
319 uint64_t int_val
= state
->scanner
->value
.v_int64
;
321 if (int_val
> INT64_MAX
) {
322 g_string_append_printf(state
->ini_error
,
323 "Integer value %" PRIu64
" is outside the range of a 64-bit signed integer\n",
326 value
= bt_value_integer_create_init((int64_t)int_val
);
331 /* Positive floating point number */
332 value
= bt_value_real_create_init(state
->scanner
->value
.v_float
);
336 value
= bt_value_string_create_init(state
->scanner
->value
.v_string
);
338 case G_TOKEN_IDENTIFIER
:
341 * Using symbols would be appropriate here,
342 * but said symbols are allowed as map key,
343 * so it's easier to consider everything an
346 * If one of the known symbols is not
347 * recognized here, then fall back to creating
350 const char *id
= state
->scanner
->value
.v_identifier
;
352 if (!strcmp(id
, "null") || !strcmp(id
, "NULL") ||
353 !strcmp(id
, "nul")) {
354 value
= bt_value_null
;
355 } else if (!strcmp(id
, "true") || !strcmp(id
, "TRUE") ||
356 !strcmp(id
, "yes") ||
357 !strcmp(id
, "YES")) {
358 value
= bt_value_bool_create_init(true);
359 } else if (!strcmp(id
, "false") ||
360 !strcmp(id
, "FALSE") ||
363 value
= bt_value_bool_create_init(false);
365 value
= bt_value_string_create_init(id
);
370 /* Unset value variable will trigger the error */
371 ini_append_error_expecting(state
, state
->scanner
, "value");
379 int ini_handle_state(struct ini_parsing_state
*state
)
382 GTokenType token_type
;
383 bt_value
*value
= NULL
;
385 token_type
= g_scanner_get_next_token(state
->scanner
);
386 if (token_type
== G_TOKEN_EOF
) {
387 if (state
->expecting
!= INI_EXPECT_COMMA
) {
388 switch (state
->expecting
) {
389 case INI_EXPECT_EQUAL
:
390 ini_append_error_expecting(state
,
391 state
->scanner
, "'='");
393 case INI_EXPECT_VALUE
:
394 ini_append_error_expecting(state
,
395 state
->scanner
, "value");
397 case INI_EXPECT_MAP_KEY
:
398 ini_append_error_expecting(state
,
399 state
->scanner
, "unquoted map key");
412 switch (state
->expecting
) {
413 case INI_EXPECT_MAP_KEY
:
414 if (token_type
!= G_TOKEN_IDENTIFIER
) {
415 ini_append_error_expecting(state
, state
->scanner
,
420 free(state
->last_map_key
);
421 state
->last_map_key
=
422 strdup(state
->scanner
->value
.v_identifier
);
423 if (!state
->last_map_key
) {
424 g_string_append(state
->ini_error
,
429 if (bt_value_map_has_entry(state
->params
,
430 state
->last_map_key
)) {
431 g_string_append_printf(state
->ini_error
,
432 "Duplicate parameter key: `%s`\n",
433 state
->last_map_key
);
437 state
->expecting
= INI_EXPECT_EQUAL
;
439 case INI_EXPECT_EQUAL
:
440 if (token_type
!= G_TOKEN_CHAR
) {
441 ini_append_error_expecting(state
,
442 state
->scanner
, "'='");
446 if (state
->scanner
->value
.v_char
!= '=') {
447 ini_append_error_expecting(state
,
448 state
->scanner
, "'='");
452 state
->expecting
= INI_EXPECT_VALUE
;
454 case INI_EXPECT_VALUE
:
456 value
= ini_parse_value(state
);
461 state
->expecting
= INI_EXPECT_COMMA
;
464 case INI_EXPECT_COMMA
:
465 if (token_type
!= G_TOKEN_CHAR
) {
466 ini_append_error_expecting(state
,
467 state
->scanner
, "','");
471 if (state
->scanner
->value
.v_char
!= ',') {
472 ini_append_error_expecting(state
,
473 state
->scanner
, "','");
477 state
->expecting
= INI_EXPECT_MAP_KEY
;
489 if (bt_value_map_insert_entry(state
->params
,
490 state
->last_map_key
, value
)) {
491 /* Only override return value on error */
497 BT_VALUE_PUT_REF_AND_RESET(value
);
502 * Converts an INI-style argument to an equivalent map value object.
504 * Return value is owned by the caller.
507 bt_value
*bt_value_from_ini(const char *arg
,
510 /* Lexical scanner configuration */
511 GScannerConfig scanner_config
= {
512 /* Skip whitespaces */
513 .cset_skip_characters
= " \t\n",
515 /* Identifier syntax is: [a-zA-Z_][a-zA-Z0-9_.:-]* */
516 .cset_identifier_first
=
520 .cset_identifier_nth
=
525 /* "hello" and "Hello" two different keys */
526 .case_sensitive
= TRUE
,
529 .cpair_comment_single
= NULL
,
530 .skip_comment_multi
= TRUE
,
531 .skip_comment_single
= TRUE
,
532 .scan_comment_multi
= FALSE
,
535 * Do scan identifiers, including 1-char identifiers,
536 * but NULL is a normal identifier.
538 .scan_identifier
= TRUE
,
539 .scan_identifier_1char
= TRUE
,
540 .scan_identifier_NULL
= FALSE
,
543 * No specific symbols: null and boolean "symbols" are
544 * scanned as plain identifiers.
546 .scan_symbols
= FALSE
,
547 .symbol_2_token
= FALSE
,
548 .scope_0_fallback
= FALSE
,
551 * Scan "0b"-, "0"-, and "0x"-prefixed integers, but not
552 * integers prefixed with "$".
558 .scan_hex_dollar
= FALSE
,
560 /* Convert scanned numbers to integer tokens */
561 .numbers_2_int
= TRUE
,
563 /* Support both integers and floating-point numbers */
564 .int_2_float
= FALSE
,
566 /* Scan integers as 64-bit signed integers */
569 /* Only scan double-quoted strings */
570 .scan_string_sq
= FALSE
,
571 .scan_string_dq
= TRUE
,
573 /* Do not converter identifiers to string tokens */
574 .identifier_2_string
= FALSE
,
576 /* Scan characters as G_TOKEN_CHAR token */
577 .char_2_token
= FALSE
,
579 struct ini_parsing_state state
= {
582 .expecting
= INI_EXPECT_MAP_KEY
,
584 .ini_error
= ini_error
,
587 state
.params
= bt_value_map_create();
592 state
.scanner
= g_scanner_new(&scanner_config
);
593 if (!state
.scanner
) {
597 /* Let the scan begin */
598 g_scanner_input_text(state
.scanner
, arg
, strlen(arg
));
601 int ret
= ini_handle_state(&state
);
606 } else if (ret
> 0) {
615 BT_VALUE_PUT_REF_AND_RESET(state
.params
);
619 g_scanner_destroy(state
.scanner
);
622 free(state
.last_map_key
);
627 * Returns the parameters map value object from a command-line
628 * parameter option's argument.
630 * Return value is owned by the caller.
633 bt_value
*bt_value_from_arg(const char *arg
)
635 bt_value
*params
= NULL
;
636 GString
*ini_error
= NULL
;
638 ini_error
= g_string_new(NULL
);
644 /* Try INI-style parsing */
645 params
= bt_value_from_ini(arg
, ini_error
);
647 printf_err("%s", ini_error
->str
);
653 g_string_free(ini_error
, TRUE
);
660 * Returns the plugin name, component class name, component class type,
661 * and component name from a command-line --component option's argument.
662 * arg must have the following format:
664 * [NAME:]TYPE.PLUGIN.CLS
666 * where NAME is the optional component name, TYPE is either `source`,
667 * `filter`, or `sink`, PLUGIN is the plugin name, and CLS is the
668 * component class name.
670 * On success, both *plugin and *component are not NULL. *plugin
671 * and *comp_cls are owned by the caller. On success, *name can be NULL
672 * if no component class name was found, and *comp_cls_type is set.
675 void plugin_comp_cls_names(const char *arg
, char **name
, char **plugin
,
676 char **comp_cls
, bt_component_class_type
*comp_cls_type
)
678 const char *at
= arg
;
679 GString
*gs_name
= NULL
;
680 GString
*gs_comp_cls_type
= NULL
;
681 GString
*gs_plugin
= NULL
;
682 GString
*gs_comp_cls
= NULL
;
688 BT_ASSERT(comp_cls_type
);
690 if (!bt_common_string_is_printable(arg
)) {
691 printf_err("Argument contains a non-printable character\n");
695 /* Parse the component name */
696 gs_name
= bt_common_string_until(at
, ".:\\", ":", &end_pos
);
701 if (arg
[end_pos
] == ':') {
705 g_string_assign(gs_name
, "");
708 /* Parse the component class type */
709 gs_comp_cls_type
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
710 if (!gs_comp_cls_type
|| at
[end_pos
] == '\0') {
711 printf_err("Missing component class type (`source`, `filter`, or `sink`)\n");
715 if (strcmp(gs_comp_cls_type
->str
, "source") == 0 ||
716 strcmp(gs_comp_cls_type
->str
, "src") == 0) {
717 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_SOURCE
;
718 } else if (strcmp(gs_comp_cls_type
->str
, "filter") == 0 ||
719 strcmp(gs_comp_cls_type
->str
, "flt") == 0) {
720 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_FILTER
;
721 } else if (strcmp(gs_comp_cls_type
->str
, "sink") == 0) {
722 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_SINK
;
724 printf_err("Unknown component class type: `%s`\n",
725 gs_comp_cls_type
->str
);
731 /* Parse the plugin name */
732 gs_plugin
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
733 if (!gs_plugin
|| gs_plugin
->len
== 0 || at
[end_pos
] == '\0') {
734 printf_err("Missing plugin or component class name\n");
740 /* Parse the component class name */
741 gs_comp_cls
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
742 if (!gs_comp_cls
|| gs_comp_cls
->len
== 0) {
743 printf_err("Missing component class name\n");
747 if (at
[end_pos
] != '\0') {
748 /* Found a non-escaped `.` */
753 if (gs_name
->len
== 0) {
755 g_string_free(gs_name
, TRUE
);
757 *name
= gs_name
->str
;
758 g_string_free(gs_name
, FALSE
);
761 g_string_free(gs_name
, TRUE
);
764 *plugin
= gs_plugin
->str
;
765 *comp_cls
= gs_comp_cls
->str
;
766 g_string_free(gs_plugin
, FALSE
);
767 g_string_free(gs_comp_cls
, FALSE
);
783 g_string_free(gs_name
, TRUE
);
787 g_string_free(gs_plugin
, TRUE
);
791 g_string_free(gs_comp_cls
, TRUE
);
794 if (gs_comp_cls_type
) {
795 g_string_free(gs_comp_cls_type
, TRUE
);
802 * Prints the Babeltrace version.
805 void print_version(void)
807 if (GIT_VERSION
[0] == '\0') {
808 puts("Babeltrace " VERSION
);
810 puts("Babeltrace " VERSION
" - " GIT_VERSION
);
815 * Destroys a component configuration.
818 void bt_config_component_destroy(bt_object
*obj
)
820 struct bt_config_component
*bt_config_component
=
821 container_of(obj
, struct bt_config_component
, base
);
827 if (bt_config_component
->plugin_name
) {
828 g_string_free(bt_config_component
->plugin_name
, TRUE
);
831 if (bt_config_component
->comp_cls_name
) {
832 g_string_free(bt_config_component
->comp_cls_name
, TRUE
);
835 if (bt_config_component
->instance_name
) {
836 g_string_free(bt_config_component
->instance_name
, TRUE
);
839 BT_VALUE_PUT_REF_AND_RESET(bt_config_component
->params
);
840 g_free(bt_config_component
);
847 * Creates a component configuration using the given plugin name and
848 * component name. `plugin_name` and `comp_cls_name` are copied (belong
849 * to the return value).
851 * Return value is owned by the caller.
854 struct bt_config_component
*bt_config_component_create(
855 bt_component_class_type type
,
856 const char *plugin_name
, const char *comp_cls_name
)
858 struct bt_config_component
*cfg_component
= NULL
;
860 cfg_component
= g_new0(struct bt_config_component
, 1);
861 if (!cfg_component
) {
866 bt_object_init_shared(&cfg_component
->base
,
867 bt_config_component_destroy
);
868 cfg_component
->type
= type
;
869 cfg_component
->plugin_name
= g_string_new(plugin_name
);
870 if (!cfg_component
->plugin_name
) {
875 cfg_component
->comp_cls_name
= g_string_new(comp_cls_name
);
876 if (!cfg_component
->comp_cls_name
) {
881 cfg_component
->instance_name
= g_string_new(NULL
);
882 if (!cfg_component
->instance_name
) {
887 /* Start with empty parameters */
888 cfg_component
->params
= bt_value_map_create();
889 if (!cfg_component
->params
) {
897 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
900 return cfg_component
;
904 * Creates a component configuration from a command-line --component
908 struct bt_config_component
*bt_config_component_from_arg(const char *arg
)
910 struct bt_config_component
*cfg_comp
= NULL
;
912 char *plugin_name
= NULL
;
913 char *comp_cls_name
= NULL
;
914 bt_component_class_type type
;
916 plugin_comp_cls_names(arg
, &name
, &plugin_name
, &comp_cls_name
, &type
);
917 if (!plugin_name
|| !comp_cls_name
) {
921 cfg_comp
= bt_config_component_create(type
, plugin_name
, comp_cls_name
);
927 g_string_assign(cfg_comp
->instance_name
, name
);
933 BT_OBJECT_PUT_REF_AND_RESET(cfg_comp
);
938 g_free(comp_cls_name
);
943 * Destroys a configuration.
946 void bt_config_destroy(bt_object
*obj
)
948 struct bt_config
*cfg
=
949 container_of(obj
, struct bt_config
, base
);
955 BT_VALUE_PUT_REF_AND_RESET(cfg
->plugin_paths
);
957 switch (cfg
->command
) {
958 case BT_CONFIG_COMMAND_RUN
:
959 if (cfg
->cmd_data
.run
.sources
) {
960 g_ptr_array_free(cfg
->cmd_data
.run
.sources
, TRUE
);
963 if (cfg
->cmd_data
.run
.filters
) {
964 g_ptr_array_free(cfg
->cmd_data
.run
.filters
, TRUE
);
967 if (cfg
->cmd_data
.run
.sinks
) {
968 g_ptr_array_free(cfg
->cmd_data
.run
.sinks
, TRUE
);
971 if (cfg
->cmd_data
.run
.connections
) {
972 g_ptr_array_free(cfg
->cmd_data
.run
.connections
,
976 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
978 case BT_CONFIG_COMMAND_HELP
:
979 BT_OBJECT_PUT_REF_AND_RESET(cfg
->cmd_data
.help
.cfg_component
);
981 case BT_CONFIG_COMMAND_QUERY
:
982 BT_OBJECT_PUT_REF_AND_RESET(cfg
->cmd_data
.query
.cfg_component
);
984 if (cfg
->cmd_data
.query
.object
) {
985 g_string_free(cfg
->cmd_data
.query
.object
, TRUE
);
988 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
989 if (cfg
->cmd_data
.print_ctf_metadata
.path
) {
990 g_string_free(cfg
->cmd_data
.print_ctf_metadata
.path
,
993 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
997 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
998 if (cfg
->cmd_data
.print_lttng_live_sessions
.url
) {
1000 cfg
->cmd_data
.print_lttng_live_sessions
.url
,
1003 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
1018 void destroy_glist_of_gstring(GList
*list
)
1026 for (at
= list
; at
!= NULL
; at
= g_list_next(at
)) {
1027 g_string_free(at
->data
, TRUE
);
1034 * Creates a simple lexical scanner for parsing comma-delimited names
1037 * Return value is owned by the caller.
1040 GScanner
*create_csv_identifiers_scanner(void)
1043 GScannerConfig scanner_config
= {
1044 .cset_skip_characters
= " \t\n",
1045 .cset_identifier_first
= G_CSET_a_2_z G_CSET_A_2_Z
"_",
1046 .cset_identifier_nth
= G_CSET_a_2_z G_CSET_A_2_Z
":_-",
1047 .case_sensitive
= TRUE
,
1048 .cpair_comment_single
= NULL
,
1049 .skip_comment_multi
= TRUE
,
1050 .skip_comment_single
= TRUE
,
1051 .scan_comment_multi
= FALSE
,
1052 .scan_identifier
= TRUE
,
1053 .scan_identifier_1char
= TRUE
,
1054 .scan_identifier_NULL
= FALSE
,
1055 .scan_symbols
= FALSE
,
1056 .symbol_2_token
= FALSE
,
1057 .scope_0_fallback
= FALSE
,
1058 .scan_binary
= FALSE
,
1059 .scan_octal
= FALSE
,
1060 .scan_float
= FALSE
,
1062 .scan_hex_dollar
= FALSE
,
1063 .numbers_2_int
= FALSE
,
1064 .int_2_float
= FALSE
,
1065 .store_int64
= FALSE
,
1066 .scan_string_sq
= FALSE
,
1067 .scan_string_dq
= FALSE
,
1068 .identifier_2_string
= FALSE
,
1069 .char_2_token
= TRUE
,
1072 scanner
= g_scanner_new(&scanner_config
);
1081 * Converts a comma-delimited list of known names (--names option) to
1082 * an array value object containing those names as string value objects.
1084 * Return value is owned by the caller.
1087 bt_value
*names_from_arg(const char *arg
)
1089 GScanner
*scanner
= NULL
;
1090 bt_value
*names
= NULL
;
1091 bool found_all
= false, found_none
= false, found_item
= false;
1093 names
= bt_value_array_create();
1099 scanner
= create_csv_identifiers_scanner();
1104 g_scanner_input_text(scanner
, arg
, strlen(arg
));
1107 GTokenType token_type
= g_scanner_get_next_token(scanner
);
1109 switch (token_type
) {
1110 case G_TOKEN_IDENTIFIER
:
1112 const char *identifier
= scanner
->value
.v_identifier
;
1114 if (!strcmp(identifier
, "payload") ||
1115 !strcmp(identifier
, "args") ||
1116 !strcmp(identifier
, "arg")) {
1118 if (bt_value_array_append_string_element(names
,
1122 } else if (!strcmp(identifier
, "context") ||
1123 !strcmp(identifier
, "ctx")) {
1125 if (bt_value_array_append_string_element(names
,
1129 } else if (!strcmp(identifier
, "scope") ||
1130 !strcmp(identifier
, "header")) {
1132 if (bt_value_array_append_string_element(names
,
1136 } else if (!strcmp(identifier
, "all")) {
1138 if (bt_value_array_append_string_element(names
,
1142 } else if (!strcmp(identifier
, "none")) {
1144 if (bt_value_array_append_string_element(names
,
1149 printf_err("Unknown name: `%s`\n",
1165 if (found_none
&& found_all
) {
1166 printf_err("Only either `all` or `none` can be specified in the list given to the --names option, but not both.\n");
1170 * Legacy behavior is to clear the defaults (show none) when at
1171 * least one item is specified.
1173 if (found_item
&& !found_none
&& !found_all
) {
1174 if (bt_value_array_append_string_element(names
, "none")) {
1179 g_scanner_destroy(scanner
);
1184 BT_VALUE_PUT_REF_AND_RESET(names
);
1186 g_scanner_destroy(scanner
);
1192 * Converts a comma-delimited list of known fields (--fields option) to
1193 * an array value object containing those fields as string
1196 * Return value is owned by the caller.
1199 bt_value
*fields_from_arg(const char *arg
)
1201 GScanner
*scanner
= NULL
;
1204 fields
= bt_value_array_create();
1210 scanner
= create_csv_identifiers_scanner();
1215 g_scanner_input_text(scanner
, arg
, strlen(arg
));
1218 GTokenType token_type
= g_scanner_get_next_token(scanner
);
1220 switch (token_type
) {
1221 case G_TOKEN_IDENTIFIER
:
1223 const char *identifier
= scanner
->value
.v_identifier
;
1225 if (!strcmp(identifier
, "trace") ||
1226 !strcmp(identifier
, "trace:hostname") ||
1227 !strcmp(identifier
, "trace:domain") ||
1228 !strcmp(identifier
, "trace:procname") ||
1229 !strcmp(identifier
, "trace:vpid") ||
1230 !strcmp(identifier
, "loglevel") ||
1231 !strcmp(identifier
, "emf") ||
1232 !strcmp(identifier
, "callsite") ||
1233 !strcmp(identifier
, "all")) {
1234 if (bt_value_array_append_string_element(fields
,
1239 printf_err("Unknown field: `%s`\n",
1257 BT_VALUE_PUT_REF_AND_RESET(fields
);
1261 g_scanner_destroy(scanner
);
1267 void append_param_arg(GString
*params_arg
, const char *key
, const char *value
)
1269 BT_ASSERT(params_arg
);
1273 if (params_arg
->len
!= 0) {
1274 g_string_append_c(params_arg
, ',');
1277 g_string_append(params_arg
, key
);
1278 g_string_append_c(params_arg
, '=');
1279 g_string_append(params_arg
, value
);
1283 * Inserts the equivalent "prefix-NAME=yes" strings into params_arg
1284 * where the names are in names_array.
1287 int insert_flat_params_from_array(GString
*params_arg
,
1288 const bt_value
*names_array
, const char *prefix
)
1292 GString
*tmpstr
= NULL
, *default_value
= NULL
;
1293 bool default_set
= false, non_default_set
= false;
1296 * names_array may be NULL if no CLI options were specified to
1297 * trigger its creation.
1303 tmpstr
= g_string_new(NULL
);
1310 default_value
= g_string_new(NULL
);
1311 if (!default_value
) {
1317 for (i
= 0; i
< bt_value_array_get_size(names_array
); i
++) {
1318 const bt_value
*str_obj
=
1319 bt_value_array_borrow_element_by_index_const(names_array
,
1322 bool is_default
= false;
1325 printf_err("Unexpected error\n");
1330 suffix
= bt_value_string_get(str_obj
);
1332 g_string_assign(tmpstr
, prefix
);
1333 g_string_append(tmpstr
, "-");
1335 /* Special-case for "all" and "none". */
1336 if (!strcmp(suffix
, "all")) {
1338 g_string_assign(default_value
, "show");
1339 } else if (!strcmp(suffix
, "none")) {
1341 g_string_assign(default_value
, "hide");
1345 g_string_append(tmpstr
, "default");
1346 append_param_arg(params_arg
, tmpstr
->str
,
1347 default_value
->str
);
1349 non_default_set
= true;
1350 g_string_append(tmpstr
, suffix
);
1351 append_param_arg(params_arg
, tmpstr
->str
, "yes");
1355 /* Implicit field-default=hide if any non-default option is set. */
1356 if (non_default_set
&& !default_set
) {
1357 g_string_assign(tmpstr
, prefix
);
1358 g_string_append(tmpstr
, "-default");
1359 g_string_assign(default_value
, "hide");
1360 append_param_arg(params_arg
, tmpstr
->str
, default_value
->str
);
1364 if (default_value
) {
1365 g_string_free(default_value
, TRUE
);
1369 g_string_free(tmpstr
, TRUE
);
1382 OPT_CLOCK_FORCE_CORRELATE
,
1385 OPT_CLOCK_OFFSET_NS
,
1393 OPT_DEBUG_INFO_FULL_PATH
,
1394 OPT_DEBUG_INFO_TARGET_PREFIX
,
1403 OPT_OMIT_HOME_PLUGIN_PATH
,
1404 OPT_OMIT_SYSTEM_PLUGIN_PATH
,
1410 OPT_RESET_BASE_PARAMS
,
1414 OPT_STREAM_INTERSECTION
,
1420 enum bt_config_component_dest
{
1421 BT_CONFIG_COMPONENT_DEST_UNKNOWN
= -1,
1422 BT_CONFIG_COMPONENT_DEST_SOURCE
,
1423 BT_CONFIG_COMPONENT_DEST_FILTER
,
1424 BT_CONFIG_COMPONENT_DEST_SINK
,
1428 * Adds a configuration component to the appropriate configuration
1429 * array depending on the destination.
1432 void add_run_cfg_comp(struct bt_config
*cfg
,
1433 struct bt_config_component
*cfg_comp
,
1434 enum bt_config_component_dest dest
)
1436 bt_object_get_ref(cfg_comp
);
1439 case BT_CONFIG_COMPONENT_DEST_SOURCE
:
1440 g_ptr_array_add(cfg
->cmd_data
.run
.sources
, cfg_comp
);
1442 case BT_CONFIG_COMPONENT_DEST_FILTER
:
1443 g_ptr_array_add(cfg
->cmd_data
.run
.filters
, cfg_comp
);
1445 case BT_CONFIG_COMPONENT_DEST_SINK
:
1446 g_ptr_array_add(cfg
->cmd_data
.run
.sinks
, cfg_comp
);
1454 int add_run_cfg_comp_check_name(struct bt_config
*cfg
,
1455 struct bt_config_component
*cfg_comp
,
1456 enum bt_config_component_dest dest
,
1457 bt_value
*instance_names
)
1461 if (cfg_comp
->instance_name
->len
== 0) {
1462 printf_err("Found an unnamed component\n");
1467 if (bt_value_map_has_entry(instance_names
,
1468 cfg_comp
->instance_name
->str
)) {
1469 printf_err("Duplicate component instance name:\n %s\n",
1470 cfg_comp
->instance_name
->str
);
1475 if (bt_value_map_insert_entry(instance_names
,
1476 cfg_comp
->instance_name
->str
, bt_value_null
)) {
1482 add_run_cfg_comp(cfg
, cfg_comp
, dest
);
1489 int append_env_var_plugin_paths(bt_value
*plugin_paths
)
1494 if (bt_common_is_setuid_setgid()) {
1495 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1499 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
1504 ret
= bt_config_append_plugin_paths(plugin_paths
, envvar
);
1508 printf_err("Cannot append plugin paths from BABELTRACE_PLUGIN_PATH\n");
1515 int append_home_and_system_plugin_paths(bt_value
*plugin_paths
,
1516 bool omit_system_plugin_path
, bool omit_home_plugin_path
)
1520 if (!omit_home_plugin_path
) {
1521 if (bt_common_is_setuid_setgid()) {
1522 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1524 char *home_plugin_dir
=
1525 bt_common_get_home_plugin_path();
1527 if (home_plugin_dir
) {
1528 ret
= bt_config_append_plugin_paths(
1529 plugin_paths
, home_plugin_dir
);
1530 free(home_plugin_dir
);
1533 printf_err("Invalid home plugin path\n");
1540 if (!omit_system_plugin_path
) {
1541 if (bt_config_append_plugin_paths(plugin_paths
,
1542 bt_common_get_system_plugin_path())) {
1543 printf_err("Invalid system plugin path\n");
1549 printf_err("Cannot append home and system plugin paths\n");
1554 int append_home_and_system_plugin_paths_cfg(struct bt_config
*cfg
)
1556 return append_home_and_system_plugin_paths(cfg
->plugin_paths
,
1557 cfg
->omit_system_plugin_path
, cfg
->omit_home_plugin_path
);
1561 struct bt_config
*bt_config_base_create(enum bt_config_command command
,
1562 const bt_value
*initial_plugin_paths
,
1565 struct bt_config
*cfg
;
1568 cfg
= g_new0(struct bt_config
, 1);
1574 bt_object_init_shared(&cfg
->base
, bt_config_destroy
);
1575 cfg
->command
= command
;
1576 cfg
->command_needs_plugins
= needs_plugins
;
1578 if (initial_plugin_paths
) {
1579 bt_value
*initial_plugin_paths_copy
;
1581 (void) bt_value_copy(initial_plugin_paths
,
1582 &initial_plugin_paths_copy
);
1583 cfg
->plugin_paths
= initial_plugin_paths_copy
;
1585 cfg
->plugin_paths
= bt_value_array_create();
1586 if (!cfg
->plugin_paths
) {
1595 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1602 struct bt_config
*bt_config_run_create(
1603 const bt_value
*initial_plugin_paths
)
1605 struct bt_config
*cfg
;
1608 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_RUN
,
1609 initial_plugin_paths
, true);
1614 cfg
->cmd_data
.run
.sources
= g_ptr_array_new_with_free_func(
1615 (GDestroyNotify
) bt_object_put_ref
);
1616 if (!cfg
->cmd_data
.run
.sources
) {
1621 cfg
->cmd_data
.run
.filters
= g_ptr_array_new_with_free_func(
1622 (GDestroyNotify
) bt_object_put_ref
);
1623 if (!cfg
->cmd_data
.run
.filters
) {
1628 cfg
->cmd_data
.run
.sinks
= g_ptr_array_new_with_free_func(
1629 (GDestroyNotify
) bt_object_put_ref
);
1630 if (!cfg
->cmd_data
.run
.sinks
) {
1635 cfg
->cmd_data
.run
.connections
= g_ptr_array_new_with_free_func(
1636 (GDestroyNotify
) bt_config_connection_destroy
);
1637 if (!cfg
->cmd_data
.run
.connections
) {
1645 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1652 struct bt_config
*bt_config_list_plugins_create(
1653 const bt_value
*initial_plugin_paths
)
1655 struct bt_config
*cfg
;
1658 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_LIST_PLUGINS
,
1659 initial_plugin_paths
, true);
1667 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1674 struct bt_config
*bt_config_help_create(
1675 const bt_value
*initial_plugin_paths
)
1677 struct bt_config
*cfg
;
1680 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_HELP
,
1681 initial_plugin_paths
, true);
1686 cfg
->cmd_data
.help
.cfg_component
=
1687 bt_config_component_create(-1, NULL
, NULL
);
1688 if (!cfg
->cmd_data
.help
.cfg_component
) {
1695 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1702 struct bt_config
*bt_config_query_create(
1703 const bt_value
*initial_plugin_paths
)
1705 struct bt_config
*cfg
;
1708 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_QUERY
,
1709 initial_plugin_paths
, true);
1714 cfg
->cmd_data
.query
.object
= g_string_new(NULL
);
1715 if (!cfg
->cmd_data
.query
.object
) {
1723 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1730 struct bt_config
*bt_config_print_ctf_metadata_create(
1731 const bt_value
*initial_plugin_paths
)
1733 struct bt_config
*cfg
;
1736 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_PRINT_CTF_METADATA
,
1737 initial_plugin_paths
, true);
1742 cfg
->cmd_data
.print_ctf_metadata
.path
= g_string_new(NULL
);
1743 if (!cfg
->cmd_data
.print_ctf_metadata
.path
) {
1748 cfg
->cmd_data
.print_ctf_metadata
.output_path
= g_string_new(NULL
);
1749 if (!cfg
->cmd_data
.print_ctf_metadata
.output_path
) {
1757 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1764 struct bt_config
*bt_config_print_lttng_live_sessions_create(
1765 const bt_value
*initial_plugin_paths
)
1767 struct bt_config
*cfg
;
1770 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
,
1771 initial_plugin_paths
, true);
1776 cfg
->cmd_data
.print_lttng_live_sessions
.url
= g_string_new(NULL
);
1777 if (!cfg
->cmd_data
.print_lttng_live_sessions
.url
) {
1782 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
=
1784 if (!cfg
->cmd_data
.print_lttng_live_sessions
.output_path
) {
1792 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1799 int bt_config_append_plugin_paths_check_setuid_setgid(
1800 bt_value
*plugin_paths
, const char *arg
)
1804 if (bt_common_is_setuid_setgid()) {
1805 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1809 if (bt_config_append_plugin_paths(plugin_paths
, arg
)) {
1810 printf_err("Invalid --plugin-path option's argument:\n %s\n",
1821 * Prints the expected format for a --params option.
1824 void print_expected_params_format(FILE *fp
)
1826 fprintf(fp
, "Expected format of PARAMS\n");
1827 fprintf(fp
, "-------------------------\n");
1829 fprintf(fp
, " PARAM=VALUE[,PARAM=VALUE]...\n");
1831 fprintf(fp
, "The parameter string is a comma-separated list of PARAM=VALUE assignments,\n");
1832 fprintf(fp
, "where PARAM is the parameter name (C identifier plus the [:.-] characters),\n");
1833 fprintf(fp
, "and VALUE can be one of:\n");
1835 fprintf(fp
, "* `null`, `nul`, `NULL`: null value (no backticks).\n");
1836 fprintf(fp
, "* `true`, `TRUE`, `yes`, `YES`: true boolean value (no backticks).\n");
1837 fprintf(fp
, "* `false`, `FALSE`, `no`, `NO`: false boolean value (no backticks).\n");
1838 fprintf(fp
, "* Binary (`0b` prefix), octal (`0` prefix), decimal, or hexadecimal\n");
1839 fprintf(fp
, " (`0x` prefix) signed 64-bit integer.\n");
1840 fprintf(fp
, "* Double precision floating point number (scientific notation is accepted).\n");
1841 fprintf(fp
, "* Unquoted string with no special characters, and not matching any of\n");
1842 fprintf(fp
, " the null and boolean value symbols above.\n");
1843 fprintf(fp
, "* Double-quoted string (accepts escape characters).\n");
1844 fprintf(fp
, "* Array, formatted as an opening `[`, a list of comma-separated values\n");
1845 fprintf(fp
, " (as described by the current list) and a closing `]`.\n");
1847 fprintf(fp
, "You can put whitespaces allowed around individual `=` and `,` symbols.\n");
1849 fprintf(fp
, "Example:\n");
1851 fprintf(fp
, " many=null, fresh=yes, condition=false, squirrel=-782329,\n");
1852 fprintf(fp
, " observe=3.14, simple=beef, needs-quotes=\"some string\",\n");
1853 fprintf(fp
, " escape.chars-are:allowed=\"this is a \\\" double quote\",\n");
1854 fprintf(fp
, " things=[1, \"2\", 3]\n");
1856 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1857 fprintf(fp
, "babeltrace from a shell.\n");
1862 * Prints the help command usage.
1865 void print_help_usage(FILE *fp
)
1867 fprintf(fp
, "Usage: babeltrace [GENERAL OPTIONS] help [OPTIONS] PLUGIN\n");
1868 fprintf(fp
, " babeltrace [GENERAL OPTIONS] help [OPTIONS] TYPE.PLUGIN.CLS\n");
1870 fprintf(fp
, "Options:\n");
1872 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1873 fprintf(fp
, " (~/.local/lib/babeltrace/plugins)\n");
1874 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1875 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1876 fprintf(fp
, " dynamic plugins can be loaded\n");
1877 fprintf(fp
, " -h, --help Show this help and quit\n");
1879 fprintf(fp
, "See `babeltrace --help` for the list of general options.\n");
1881 fprintf(fp
, "Use `babeltrace list-plugins` to show the list of available plugins.\n");
1885 struct poptOption help_long_options
[] = {
1886 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
1887 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
1888 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
1889 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
1890 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
1891 { NULL
, 0, '\0', NULL
, 0, NULL
, NULL
},
1895 * Creates a Babeltrace config object from the arguments of a help
1898 * *retcode is set to the appropriate exit code to use.
1901 struct bt_config
*bt_config_help_from_args(int argc
, const char *argv
[],
1902 int *retcode
, bool force_omit_system_plugin_path
,
1903 bool force_omit_home_plugin_path
,
1904 const bt_value
*initial_plugin_paths
)
1906 poptContext pc
= NULL
;
1910 struct bt_config
*cfg
= NULL
;
1911 const char *leftover
;
1912 char *plugin_name
= NULL
, *comp_cls_name
= NULL
;
1915 cfg
= bt_config_help_create(initial_plugin_paths
);
1920 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
1921 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
1922 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
1928 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
1929 help_long_options
, 0);
1931 printf_err("Cannot get popt context\n");
1935 poptReadDefaultConfig(pc
, 0);
1937 while ((opt
= poptGetNextOpt(pc
)) > 0) {
1938 arg
= poptGetOptArg(pc
);
1941 case OPT_PLUGIN_PATH
:
1942 if (bt_config_append_plugin_paths_check_setuid_setgid(
1943 cfg
->plugin_paths
, arg
)) {
1947 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
1948 cfg
->omit_system_plugin_path
= true;
1950 case OPT_OMIT_HOME_PLUGIN_PATH
:
1951 cfg
->omit_home_plugin_path
= true;
1954 print_help_usage(stdout
);
1956 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1959 printf_err("Unknown command-line option specified (option code %d)\n",
1968 /* Check for option parsing error */
1970 printf_err("While parsing command-line options, at option %s: %s\n",
1971 poptBadOption(pc
, 0), poptStrerror(opt
));
1975 leftover
= poptGetArg(pc
);
1977 plugin_comp_cls_names(leftover
, NULL
,
1978 &plugin_name
, &comp_cls_name
,
1979 &cfg
->cmd_data
.help
.cfg_component
->type
);
1980 if (plugin_name
&& comp_cls_name
) {
1981 /* Component class help */
1983 cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
1986 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
,
1989 /* Fall back to plugin help */
1991 cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
1995 print_help_usage(stdout
);
1997 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2001 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
2009 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2012 g_free(plugin_name
);
2013 g_free(comp_cls_name
);
2016 poptFreeContext(pc
);
2024 * Prints the help command usage.
2027 void print_query_usage(FILE *fp
)
2029 fprintf(fp
, "Usage: babeltrace [GEN OPTS] query [OPTS] TYPE.PLUGIN.CLS OBJECT\n");
2031 fprintf(fp
, "Options:\n");
2033 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2034 fprintf(fp
, " (~/.local/lib/babeltrace/plugins)\n");
2035 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2036 fprintf(fp
, " -p, --params=PARAMS Set the query parameters to PARAMS\n");
2037 fprintf(fp
, " (see the expected format of PARAMS below)\n");
2038 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2039 fprintf(fp
, " dynamic plugins can be loaded\n");
2040 fprintf(fp
, " -h, --help Show this help and quit\n");
2041 fprintf(fp
, "\n\n");
2042 print_expected_params_format(fp
);
2046 struct poptOption query_long_options
[] = {
2047 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
2048 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
2049 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
2050 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
2051 { "params", 'p', POPT_ARG_STRING
, NULL
, OPT_PARAMS
, NULL
, NULL
},
2052 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
2053 { NULL
, 0, '\0', NULL
, 0, NULL
, NULL
},
2057 * Creates a Babeltrace config object from the arguments of a query
2060 * *retcode is set to the appropriate exit code to use.
2063 struct bt_config
*bt_config_query_from_args(int argc
, const char *argv
[],
2064 int *retcode
, bool force_omit_system_plugin_path
,
2065 bool force_omit_home_plugin_path
,
2066 const bt_value
*initial_plugin_paths
)
2068 poptContext pc
= NULL
;
2072 struct bt_config
*cfg
= NULL
;
2073 const char *leftover
;
2076 params
= bt_value_null
;
2077 bt_value_get_ref(bt_value_null
);
2080 cfg
= bt_config_query_create(initial_plugin_paths
);
2085 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
2086 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
2087 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
2093 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
2094 query_long_options
, 0);
2096 printf_err("Cannot get popt context\n");
2100 poptReadDefaultConfig(pc
, 0);
2102 while ((opt
= poptGetNextOpt(pc
)) > 0) {
2103 arg
= poptGetOptArg(pc
);
2106 case OPT_PLUGIN_PATH
:
2107 if (bt_config_append_plugin_paths_check_setuid_setgid(
2108 cfg
->plugin_paths
, arg
)) {
2112 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
2113 cfg
->omit_system_plugin_path
= true;
2115 case OPT_OMIT_HOME_PLUGIN_PATH
:
2116 cfg
->omit_home_plugin_path
= true;
2120 bt_value_put_ref(params
);
2121 params
= bt_value_from_arg(arg
);
2123 printf_err("Invalid format for --params option's argument:\n %s\n",
2130 print_query_usage(stdout
);
2132 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2135 printf_err("Unknown command-line option specified (option code %d)\n",
2144 /* Check for option parsing error */
2146 printf_err("While parsing command-line options, at option %s: %s\n",
2147 poptBadOption(pc
, 0), poptStrerror(opt
));
2152 * We need exactly two leftover arguments which are the
2153 * mandatory component class specification and query object.
2155 leftover
= poptGetArg(pc
);
2157 cfg
->cmd_data
.query
.cfg_component
=
2158 bt_config_component_from_arg(leftover
);
2159 if (!cfg
->cmd_data
.query
.cfg_component
) {
2160 printf_err("Invalid format for component class specification:\n %s\n",
2166 BT_OBJECT_MOVE_REF(cfg
->cmd_data
.query
.cfg_component
->params
,
2169 print_query_usage(stdout
);
2171 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2175 leftover
= poptGetArg(pc
);
2177 if (strlen(leftover
) == 0) {
2178 printf_err("Invalid empty object\n");
2182 g_string_assign(cfg
->cmd_data
.query
.object
, leftover
);
2184 print_query_usage(stdout
);
2186 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2190 leftover
= poptGetArg(pc
);
2192 printf_err("Unexpected argument: %s\n", leftover
);
2196 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
2204 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2208 poptFreeContext(pc
);
2211 bt_value_put_ref(params
);
2217 * Prints the list-plugins command usage.
2220 void print_list_plugins_usage(FILE *fp
)
2222 fprintf(fp
, "Usage: babeltrace [GENERAL OPTIONS] list-plugins [OPTIONS]\n");
2224 fprintf(fp
, "Options:\n");
2226 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2227 fprintf(fp
, " (~/.local/lib/babeltrace/plugins)\n");
2228 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2229 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2230 fprintf(fp
, " dynamic plugins can be loaded\n");
2231 fprintf(fp
, " -h, --help Show this help and quit\n");
2233 fprintf(fp
, "See `babeltrace --help` for the list of general options.\n");
2235 fprintf(fp
, "Use `babeltrace help` to get help for a specific plugin or component class.\n");
2239 struct poptOption list_plugins_long_options
[] = {
2240 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
2241 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
2242 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
2243 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
2244 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
2245 { NULL
, 0, '\0', NULL
, 0, NULL
, NULL
},
2249 * Creates a Babeltrace config object from the arguments of a
2250 * list-plugins command.
2252 * *retcode is set to the appropriate exit code to use.
2255 struct bt_config
*bt_config_list_plugins_from_args(int argc
, const char *argv
[],
2256 int *retcode
, bool force_omit_system_plugin_path
,
2257 bool force_omit_home_plugin_path
,
2258 const bt_value
*initial_plugin_paths
)
2260 poptContext pc
= NULL
;
2264 struct bt_config
*cfg
= NULL
;
2265 const char *leftover
;
2268 cfg
= bt_config_list_plugins_create(initial_plugin_paths
);
2273 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
2274 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
2275 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
2281 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
2282 list_plugins_long_options
, 0);
2284 printf_err("Cannot get popt context\n");
2288 poptReadDefaultConfig(pc
, 0);
2290 while ((opt
= poptGetNextOpt(pc
)) > 0) {
2291 arg
= poptGetOptArg(pc
);
2294 case OPT_PLUGIN_PATH
:
2295 if (bt_config_append_plugin_paths_check_setuid_setgid(
2296 cfg
->plugin_paths
, arg
)) {
2300 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
2301 cfg
->omit_system_plugin_path
= true;
2303 case OPT_OMIT_HOME_PLUGIN_PATH
:
2304 cfg
->omit_home_plugin_path
= true;
2307 print_list_plugins_usage(stdout
);
2309 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2312 printf_err("Unknown command-line option specified (option code %d)\n",
2321 /* Check for option parsing error */
2323 printf_err("While parsing command-line options, at option %s: %s\n",
2324 poptBadOption(pc
, 0), poptStrerror(opt
));
2328 leftover
= poptGetArg(pc
);
2330 printf_err("Unexpected argument: %s\n", leftover
);
2334 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
2342 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2346 poptFreeContext(pc
);
2354 * Prints the run command usage.
2357 void print_run_usage(FILE *fp
)
2359 fprintf(fp
, "Usage: babeltrace [GENERAL OPTIONS] run [OPTIONS]\n");
2361 fprintf(fp
, "Options:\n");
2363 fprintf(fp
, " -b, --base-params=PARAMS Set PARAMS as the current base parameters\n");
2364 fprintf(fp
, " for all the following components until\n");
2365 fprintf(fp
, " --reset-base-params is encountered\n");
2366 fprintf(fp
, " (see the expected format of PARAMS below)\n");
2367 fprintf(fp
, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2368 fprintf(fp
, " Instantiate the component class CLS of type\n");
2369 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
2370 fprintf(fp
, " in the plugin PLUGIN, add it to the graph,\n");
2371 fprintf(fp
, " and optionally name it NAME (you can also\n");
2372 fprintf(fp
, " specify the name with --name)\n");
2373 fprintf(fp
, " -x, --connect=CONNECTION Connect two created components (see the\n");
2374 fprintf(fp
, " expected format of CONNECTION below)\n");
2375 fprintf(fp
, " -n, --name=NAME Set the name of the current component\n");
2376 fprintf(fp
, " to NAME (must be unique amongst all the\n");
2377 fprintf(fp
, " names of the created components)\n");
2378 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2379 fprintf(fp
, " (~/.local/lib/babeltrace/plugins)\n");
2380 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2381 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2382 fprintf(fp
, " current component (see the expected format\n");
2383 fprintf(fp
, " of PARAMS below)\n");
2384 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2385 fprintf(fp
, " dynamic plugins can be loaded\n");
2386 fprintf(fp
, " -r, --reset-base-params Reset the current base parameters to an\n");
2387 fprintf(fp
, " empty map\n");
2388 fprintf(fp
, " --retry-duration=DUR When babeltrace(1) needs to retry to run\n");
2389 fprintf(fp
, " the graph later, retry in DUR µs\n");
2390 fprintf(fp
, " (default: 100000)\n");
2391 fprintf(fp
, " -h, --help Show this help and quit\n");
2393 fprintf(fp
, "See `babeltrace --help` for the list of general options.\n");
2394 fprintf(fp
, "\n\n");
2395 fprintf(fp
, "Expected format of CONNECTION\n");
2396 fprintf(fp
, "-----------------------------\n");
2398 fprintf(fp
, " UPSTREAM[.UPSTREAM-PORT]:DOWNSTREAM[.DOWNSTREAM-PORT]\n");
2400 fprintf(fp
, "UPSTREAM and DOWNSTREAM are names of the upstream and downstream\n");
2401 fprintf(fp
, "components to connect together. You must escape the following characters\n\n");
2402 fprintf(fp
, "with `\\`: `\\`, `.`, and `:`. You can set the name of the current\n");
2403 fprintf(fp
, "component with the --name option.\n");
2405 fprintf(fp
, "UPSTREAM-PORT and DOWNSTREAM-PORT are optional globbing patterns to\n");
2406 fprintf(fp
, "identify the upstream and downstream ports to use for the connection.\n");
2407 fprintf(fp
, "When the port is not specified, `*` is used.\n");
2409 fprintf(fp
, "When a component named UPSTREAM has an available port which matches the\n");
2410 fprintf(fp
, "UPSTREAM-PORT globbing pattern, it is connected to the first port which\n");
2411 fprintf(fp
, "matches the DOWNSTREAM-PORT globbing pattern of the component named\n");
2412 fprintf(fp
, "DOWNSTREAM.\n");
2414 fprintf(fp
, "The only special character in UPSTREAM-PORT and DOWNSTREAM-PORT is `*`\n");
2415 fprintf(fp
, "which matches anything. You must escape the following characters\n");
2416 fprintf(fp
, "with `\\`: `\\`, `*`, `?`, `[`, `.`, and `:`.\n");
2418 fprintf(fp
, "You can connect a source component to a filter or sink component. You\n");
2419 fprintf(fp
, "can connect a filter component to a sink component.\n");
2421 fprintf(fp
, "Examples:\n");
2423 fprintf(fp
, " my-src:my-sink\n");
2424 fprintf(fp
, " ctf-fs.*stream*:utils-muxer:*\n");
2426 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
2427 fprintf(fp
, "babeltrace from a shell.\n");
2428 fprintf(fp
, "\n\n");
2429 print_expected_params_format(fp
);
2433 * Creates a Babeltrace config object from the arguments of a run
2436 * *retcode is set to the appropriate exit code to use.
2439 struct bt_config
*bt_config_run_from_args(int argc
, const char *argv
[],
2440 int *retcode
, bool force_omit_system_plugin_path
,
2441 bool force_omit_home_plugin_path
,
2442 const bt_value
*initial_plugin_paths
)
2444 poptContext pc
= NULL
;
2446 struct bt_config_component
*cur_cfg_comp
= NULL
;
2447 enum bt_config_component_dest cur_cfg_comp_dest
=
2448 BT_CONFIG_COMPONENT_DEST_UNKNOWN
;
2449 bt_value
*cur_base_params
= NULL
;
2451 struct bt_config
*cfg
= NULL
;
2452 bt_value
*instance_names
= NULL
;
2453 bt_value
*connection_args
= NULL
;
2454 char error_buf
[256] = { 0 };
2455 long retry_duration
= -1;
2456 bt_value_status status
;
2457 struct poptOption run_long_options
[] = {
2458 { "base-params", 'b', POPT_ARG_STRING
, NULL
, OPT_BASE_PARAMS
, NULL
, NULL
},
2459 { "component", 'c', POPT_ARG_STRING
, NULL
, OPT_COMPONENT
, NULL
, NULL
},
2460 { "connect", 'x', POPT_ARG_STRING
, NULL
, OPT_CONNECT
, NULL
, NULL
},
2461 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
2462 { "name", 'n', POPT_ARG_STRING
, NULL
, OPT_NAME
, NULL
, NULL
},
2463 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
2464 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
2465 { "params", 'p', POPT_ARG_STRING
, NULL
, OPT_PARAMS
, NULL
, NULL
},
2466 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
2467 { "reset-base-params", 'r', POPT_ARG_NONE
, NULL
, OPT_RESET_BASE_PARAMS
, NULL
, NULL
},
2468 { "retry-duration", '\0', POPT_ARG_LONG
, &retry_duration
, OPT_RETRY_DURATION
, NULL
, NULL
},
2469 { NULL
, 0, '\0', NULL
, 0, NULL
, NULL
},
2475 print_run_usage(stdout
);
2480 cfg
= bt_config_run_create(initial_plugin_paths
);
2485 cfg
->cmd_data
.run
.retry_duration_us
= 100000;
2486 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
2487 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
2488 cur_base_params
= bt_value_map_create();
2489 if (!cur_base_params
) {
2494 instance_names
= bt_value_map_create();
2495 if (!instance_names
) {
2500 connection_args
= bt_value_array_create();
2501 if (!connection_args
) {
2506 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
2512 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
2513 run_long_options
, 0);
2515 printf_err("Cannot get popt context\n");
2519 poptReadDefaultConfig(pc
, 0);
2521 while ((opt
= poptGetNextOpt(pc
)) > 0) {
2522 arg
= poptGetOptArg(pc
);
2525 case OPT_PLUGIN_PATH
:
2526 if (bt_config_append_plugin_paths_check_setuid_setgid(
2527 cfg
->plugin_paths
, arg
)) {
2531 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
2532 cfg
->omit_system_plugin_path
= true;
2534 case OPT_OMIT_HOME_PLUGIN_PATH
:
2535 cfg
->omit_home_plugin_path
= true;
2539 enum bt_config_component_dest new_dest
;
2542 ret
= add_run_cfg_comp_check_name(cfg
,
2543 cur_cfg_comp
, cur_cfg_comp_dest
,
2545 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2551 cur_cfg_comp
= bt_config_component_from_arg(arg
);
2552 if (!cur_cfg_comp
) {
2553 printf_err("Invalid format for --component option's argument:\n %s\n",
2558 switch (cur_cfg_comp
->type
) {
2559 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2560 new_dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
2562 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2563 new_dest
= BT_CONFIG_COMPONENT_DEST_FILTER
;
2565 case BT_COMPONENT_CLASS_TYPE_SINK
:
2566 new_dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
2572 BT_ASSERT(cur_base_params
);
2573 bt_value_put_ref(cur_cfg_comp
->params
);
2574 status
= bt_value_copy(cur_base_params
,
2575 &cur_cfg_comp
->params
);
2576 if (status
!= BT_VALUE_STATUS_OK
) {
2581 cur_cfg_comp_dest
= new_dest
;
2587 bt_value
*params_to_set
;
2589 if (!cur_cfg_comp
) {
2590 printf_err("Cannot add parameters to unavailable component:\n %s\n",
2595 params
= bt_value_from_arg(arg
);
2597 printf_err("Invalid format for --params option's argument:\n %s\n",
2602 status
= bt_value_map_extend(cur_cfg_comp
->params
,
2603 params
, ¶ms_to_set
);
2604 BT_VALUE_PUT_REF_AND_RESET(params
);
2605 if (status
!= BT_VALUE_STATUS_OK
) {
2606 printf_err("Cannot extend current component parameters with --params option's argument:\n %s\n",
2611 BT_OBJECT_MOVE_REF(cur_cfg_comp
->params
, params_to_set
);
2615 if (!cur_cfg_comp
) {
2616 printf_err("Cannot set the name of unavailable component:\n %s\n",
2621 g_string_assign(cur_cfg_comp
->instance_name
, arg
);
2623 case OPT_BASE_PARAMS
:
2626 bt_value_from_arg(arg
);
2629 printf_err("Invalid format for --base-params option's argument:\n %s\n",
2634 BT_OBJECT_MOVE_REF(cur_base_params
, params
);
2637 case OPT_RESET_BASE_PARAMS
:
2638 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
2639 cur_base_params
= bt_value_map_create();
2640 if (!cur_base_params
) {
2646 if (bt_value_array_append_string_element(
2647 connection_args
, arg
)) {
2652 case OPT_RETRY_DURATION
:
2653 if (retry_duration
< 0) {
2654 printf_err("--retry-duration option's argument must be positive or 0: %ld\n",
2659 cfg
->cmd_data
.run
.retry_duration_us
=
2660 (uint64_t) retry_duration
;
2663 print_run_usage(stdout
);
2665 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2668 printf_err("Unknown command-line option specified (option code %d)\n",
2677 /* Check for option parsing error */
2679 printf_err("While parsing command-line options, at option %s: %s\n",
2680 poptBadOption(pc
, 0), poptStrerror(opt
));
2684 /* This command does not accept leftover arguments */
2685 if (poptPeekArg(pc
)) {
2686 printf_err("Unexpected argument: %s\n", poptPeekArg(pc
));
2690 /* Add current component */
2692 ret
= add_run_cfg_comp_check_name(cfg
, cur_cfg_comp
,
2693 cur_cfg_comp_dest
, instance_names
);
2694 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2700 if (cfg
->cmd_data
.run
.sources
->len
== 0) {
2701 printf_err("Incomplete graph: no source component\n");
2705 if (cfg
->cmd_data
.run
.sinks
->len
== 0) {
2706 printf_err("Incomplete graph: no sink component\n");
2710 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
2714 ret
= bt_config_cli_args_create_connections(cfg
,
2718 printf_err("Cannot creation connections:\n%s", error_buf
);
2726 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2730 poptFreeContext(pc
);
2734 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2735 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
2736 BT_VALUE_PUT_REF_AND_RESET(instance_names
);
2737 BT_VALUE_PUT_REF_AND_RESET(connection_args
);
2742 struct bt_config
*bt_config_run_from_args_array(const bt_value
*run_args
,
2743 int *retcode
, bool force_omit_system_plugin_path
,
2744 bool force_omit_home_plugin_path
,
2745 const bt_value
*initial_plugin_paths
)
2747 struct bt_config
*cfg
= NULL
;
2750 const size_t argc
= bt_value_array_get_size(run_args
) + 1;
2752 argv
= calloc(argc
, sizeof(*argv
));
2760 len
= bt_value_array_get_size(run_args
);
2762 printf_err("Invalid executable arguments\n");
2765 for (i
= 0; i
< len
; i
++) {
2766 const bt_value
*arg_value
=
2767 bt_value_array_borrow_element_by_index_const(run_args
,
2771 BT_ASSERT(arg_value
);
2772 arg
= bt_value_string_get(arg_value
);
2777 cfg
= bt_config_run_from_args(argc
, argv
, retcode
,
2778 force_omit_system_plugin_path
, force_omit_home_plugin_path
,
2779 initial_plugin_paths
);
2787 * Prints the convert command usage.
2790 void print_convert_usage(FILE *fp
)
2792 fprintf(fp
, "Usage: babeltrace [GENERAL OPTIONS] [convert] [OPTIONS] [PATH/URL]\n");
2794 fprintf(fp
, "Options:\n");
2796 fprintf(fp
, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2797 fprintf(fp
, " Instantiate the component class CLS of type\n");
2798 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
2799 fprintf(fp
, " in the plugin PLUGIN, add it to the\n");
2800 fprintf(fp
, " conversion graph, and optionally name it\n");
2801 fprintf(fp
, " NAME (you can also specify the name with\n");
2802 fprintf(fp
, " --name)\n");
2803 fprintf(fp
, " --name=NAME Set the name of the current component\n");
2804 fprintf(fp
, " to NAME (must be unique amongst all the\n");
2805 fprintf(fp
, " names of the created components)\n");
2806 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2807 fprintf(fp
, " (~/.local/lib/babeltrace/plugins)\n");
2808 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2809 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2810 fprintf(fp
, " current component (see the expected format\n");
2811 fprintf(fp
, " of PARAMS below)\n");
2812 fprintf(fp
, " -P, --path=PATH Set the `path` string parameter of the\n");
2813 fprintf(fp
, " current component to PATH\n");
2814 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2815 fprintf(fp
, " --retry-duration=DUR When babeltrace(1) needs to retry to run\n");
2816 fprintf(fp
, " the graph later, retry in DUR µs\n");
2817 fprintf(fp
, " (default: 100000)\n");
2818 fprintf(fp
, " dynamic plugins can be loaded\n");
2819 fprintf(fp
, " --run-args Print the equivalent arguments for the\n");
2820 fprintf(fp
, " `run` command to the standard output,\n");
2821 fprintf(fp
, " formatted for a shell, and quit\n");
2822 fprintf(fp
, " --run-args-0 Print the equivalent arguments for the\n");
2823 fprintf(fp
, " `run` command to the standard output,\n");
2824 fprintf(fp
, " formatted for `xargs -0`, and quit\n");
2825 fprintf(fp
, " --stream-intersection Only process events when all streams\n");
2826 fprintf(fp
, " are active\n");
2827 fprintf(fp
, " -u, --url=URL Set the `url` string parameter of the\n");
2828 fprintf(fp
, " current component to URL\n");
2829 fprintf(fp
, " -h, --help Show this help and quit\n");
2831 fprintf(fp
, "Implicit `source.ctf.fs` component options:\n");
2833 fprintf(fp
, " --clock-offset=SEC Set clock offset to SEC seconds\n");
2834 fprintf(fp
, " --clock-offset-ns=NS Set clock offset to NS ns\n");
2836 fprintf(fp
, "Implicit `sink.text.pretty` component options:\n");
2838 fprintf(fp
, " --clock-cycles Print timestamps in clock cycles\n");
2839 fprintf(fp
, " --clock-date Print timestamp dates\n");
2840 fprintf(fp
, " --clock-gmt Print and parse timestamps in the GMT\n");
2841 fprintf(fp
, " time zone instead of the local time zone\n");
2842 fprintf(fp
, " --clock-seconds Print the timestamps as `SEC.NS` instead\n");
2843 fprintf(fp
, " of `hh:mm:ss.nnnnnnnnn`\n");
2844 fprintf(fp
, " --color=(never | auto | always)\n");
2845 fprintf(fp
, " Never, automatically, or always emit\n");
2846 fprintf(fp
, " console color codes\n");
2847 fprintf(fp
, " -f, --fields=FIELD[,FIELD]... Print additional fields; FIELD can be:\n");
2848 fprintf(fp
, " `all`, `trace`, `trace:hostname`,\n");
2849 fprintf(fp
, " `trace:domain`, `trace:procname`,\n");
2850 fprintf(fp
, " `trace:vpid`, `loglevel`, `emf`\n");
2851 fprintf(fp
, " -n, --names=NAME[,NAME]... Print field names; NAME can be:\n");
2852 fprintf(fp
, " `payload` (or `arg` or `args`), `none`,\n");
2853 fprintf(fp
, " `all`, `scope`, `header`, `context`\n");
2854 fprintf(fp
, " (or `ctx`)\n");
2855 fprintf(fp
, " --no-delta Do not print time delta between\n");
2856 fprintf(fp
, " consecutive events\n");
2857 fprintf(fp
, " -w, --output=PATH Write output text to PATH instead of\n");
2858 fprintf(fp
, " the standard output\n");
2860 fprintf(fp
, "Implicit `filter.utils.muxer` component options:\n");
2862 fprintf(fp
, " --clock-force-correlate Assume that clocks are inherently\n");
2863 fprintf(fp
, " correlated across traces\n");
2865 fprintf(fp
, "Implicit `filter.utils.trimmer` component options:\n");
2867 fprintf(fp
, " -b, --begin=BEGIN Set the beginning time of the conversion\n");
2868 fprintf(fp
, " time range to BEGIN (see the format of\n");
2869 fprintf(fp
, " BEGIN below)\n");
2870 fprintf(fp
, " -e, --end=END Set the end time of the conversion time\n");
2871 fprintf(fp
, " range to END (see the format of END below)\n");
2872 fprintf(fp
, " -t, --timerange=TIMERANGE Set conversion time range to TIMERANGE:\n");
2873 fprintf(fp
, " BEGIN,END or [BEGIN,END] (literally `[` and\n");
2874 fprintf(fp
, " `]`) (see the format of BEGIN/END below)\n");
2876 fprintf(fp
, "Implicit `filter.lttng-utils.debug-info` component options:\n");
2878 fprintf(fp
, " --debug-info Create an implicit\n");
2879 fprintf(fp
, " `filter.lttng-utils.debug-info` component\n");
2880 fprintf(fp
, " --debug-info-dir=DIR Search for debug info in directory DIR\n");
2881 fprintf(fp
, " instead of `/usr/lib/debug`\n");
2882 fprintf(fp
, " --debug-info-full-path Show full debug info source and\n");
2883 fprintf(fp
, " binary paths instead of just names\n");
2884 fprintf(fp
, " --debug-info-target-prefix=DIR\n");
2885 fprintf(fp
, " Use directory DIR as a prefix when\n");
2886 fprintf(fp
, " looking up executables during debug\n");
2887 fprintf(fp
, " info analysis\n");
2889 fprintf(fp
, "Legacy options that still work:\n");
2891 fprintf(fp
, " -i, --input-format=(ctf | lttng-live)\n");
2892 fprintf(fp
, " `ctf`:\n");
2893 fprintf(fp
, " Create an implicit `source.ctf.fs`\n");
2894 fprintf(fp
, " component\n");
2895 fprintf(fp
, " `lttng-live`:\n");
2896 fprintf(fp
, " Create an implicit `source.ctf.lttng-live`\n");
2897 fprintf(fp
, " component\n");
2898 fprintf(fp
, " -o, --output-format=(text | ctf | dummy | ctf-metadata)\n");
2899 fprintf(fp
, " `text`:\n");
2900 fprintf(fp
, " Create an implicit `sink.text.pretty`\n");
2901 fprintf(fp
, " component\n");
2902 fprintf(fp
, " `ctf`:\n");
2903 fprintf(fp
, " Create an implicit `sink.ctf.fs`\n");
2904 fprintf(fp
, " component\n");
2905 fprintf(fp
, " `dummy`:\n");
2906 fprintf(fp
, " Create an implicit `sink.utils.dummy`\n");
2907 fprintf(fp
, " component\n");
2908 fprintf(fp
, " `ctf-metadata`:\n");
2909 fprintf(fp
, " Query the `source.ctf.fs` component class\n");
2910 fprintf(fp
, " for metadata text and quit\n");
2912 fprintf(fp
, "See `babeltrace --help` for the list of general options.\n");
2913 fprintf(fp
, "\n\n");
2914 fprintf(fp
, "Format of BEGIN and END\n");
2915 fprintf(fp
, "-----------------------\n");
2917 fprintf(fp
, " [YYYY-MM-DD [hh:mm:]]ss[.nnnnnnnnn]\n");
2918 fprintf(fp
, "\n\n");
2919 print_expected_params_format(fp
);
2923 struct poptOption convert_long_options
[] = {
2924 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
2925 { "begin", 'b', POPT_ARG_STRING
, NULL
, OPT_BEGIN
, NULL
, NULL
},
2926 { "clock-cycles", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_CYCLES
, NULL
, NULL
},
2927 { "clock-date", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_DATE
, NULL
, NULL
},
2928 { "clock-force-correlate", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_FORCE_CORRELATE
, NULL
, NULL
},
2929 { "clock-gmt", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_GMT
, NULL
, NULL
},
2930 { "clock-offset", '\0', POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET
, NULL
, NULL
},
2931 { "clock-offset-ns", '\0', POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET_NS
, NULL
, NULL
},
2932 { "clock-seconds", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_SECONDS
, NULL
, NULL
},
2933 { "color", '\0', POPT_ARG_STRING
, NULL
, OPT_COLOR
, NULL
, NULL
},
2934 { "component", 'c', POPT_ARG_STRING
, NULL
, OPT_COMPONENT
, NULL
, NULL
},
2935 { "debug", 'd', POPT_ARG_NONE
, NULL
, OPT_DEBUG
, NULL
, NULL
},
2936 { "debug-info-dir", 0, POPT_ARG_STRING
, NULL
, OPT_DEBUG_INFO_DIR
, NULL
, NULL
},
2937 { "debug-info-full-path", 0, POPT_ARG_NONE
, NULL
, OPT_DEBUG_INFO_FULL_PATH
, NULL
, NULL
},
2938 { "debug-info-target-prefix", 0, POPT_ARG_STRING
, NULL
, OPT_DEBUG_INFO_TARGET_PREFIX
, NULL
, NULL
},
2939 { "end", 'e', POPT_ARG_STRING
, NULL
, OPT_END
, NULL
, NULL
},
2940 { "fields", 'f', POPT_ARG_STRING
, NULL
, OPT_FIELDS
, NULL
, NULL
},
2941 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
2942 { "input-format", 'i', POPT_ARG_STRING
, NULL
, OPT_INPUT_FORMAT
, NULL
, NULL
},
2943 { "name", '\0', POPT_ARG_STRING
, NULL
, OPT_NAME
, NULL
, NULL
},
2944 { "names", 'n', POPT_ARG_STRING
, NULL
, OPT_NAMES
, NULL
, NULL
},
2945 { "debug-info", '\0', POPT_ARG_NONE
, NULL
, OPT_DEBUG_INFO
, NULL
, NULL
},
2946 { "no-delta", '\0', POPT_ARG_NONE
, NULL
, OPT_NO_DELTA
, NULL
, NULL
},
2947 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
2948 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
2949 { "output", 'w', POPT_ARG_STRING
, NULL
, OPT_OUTPUT
, NULL
, NULL
},
2950 { "output-format", 'o', POPT_ARG_STRING
, NULL
, OPT_OUTPUT_FORMAT
, NULL
, NULL
},
2951 { "params", 'p', POPT_ARG_STRING
, NULL
, OPT_PARAMS
, NULL
, NULL
},
2952 { "path", 'P', POPT_ARG_STRING
, NULL
, OPT_PATH
, NULL
, NULL
},
2953 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
2954 { "retry-duration", '\0', POPT_ARG_STRING
, NULL
, OPT_RETRY_DURATION
, NULL
, NULL
},
2955 { "run-args", '\0', POPT_ARG_NONE
, NULL
, OPT_RUN_ARGS
, NULL
, NULL
},
2956 { "run-args-0", '\0', POPT_ARG_NONE
, NULL
, OPT_RUN_ARGS_0
, NULL
, NULL
},
2957 { "stream-intersection", '\0', POPT_ARG_NONE
, NULL
, OPT_STREAM_INTERSECTION
, NULL
, NULL
},
2958 { "timerange", '\0', POPT_ARG_STRING
, NULL
, OPT_TIMERANGE
, NULL
, NULL
},
2959 { "url", 'u', POPT_ARG_STRING
, NULL
, OPT_URL
, NULL
, NULL
},
2960 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
2961 { NULL
, 0, '\0', NULL
, 0, NULL
, NULL
},
2965 GString
*get_component_auto_name(const char *prefix
,
2966 const bt_value
*existing_names
)
2969 GString
*auto_name
= g_string_new(NULL
);
2976 if (!bt_value_map_has_entry(existing_names
, prefix
)) {
2977 g_string_assign(auto_name
, prefix
);
2982 g_string_printf(auto_name
, "%s-%d", prefix
, i
);
2984 } while (bt_value_map_has_entry(existing_names
, auto_name
->str
));
2990 struct implicit_component_args
{
2994 GString
*params_arg
;
2995 bt_value
*extra_params
;
2999 int assign_name_to_implicit_component(struct implicit_component_args
*args
,
3000 const char *prefix
, bt_value
*existing_names
,
3001 GList
**comp_names
, bool append_to_comp_names
)
3004 GString
*name
= NULL
;
3006 if (!args
->exists
) {
3010 name
= get_component_auto_name(prefix
,
3018 g_string_assign(args
->name_arg
, name
->str
);
3020 if (bt_value_map_insert_entry(existing_names
, name
->str
,
3027 if (append_to_comp_names
) {
3028 *comp_names
= g_list_append(*comp_names
, name
);
3034 g_string_free(name
, TRUE
);
3041 int append_run_args_for_implicit_component(
3042 struct implicit_component_args
*impl_args
,
3048 if (!impl_args
->exists
) {
3052 if (bt_value_array_append_string_element(run_args
, "--component")) {
3057 if (bt_value_array_append_string_element(run_args
, impl_args
->comp_arg
->str
)) {
3062 if (bt_value_array_append_string_element(run_args
, "--name")) {
3067 if (bt_value_array_append_string_element(run_args
, impl_args
->name_arg
->str
)) {
3072 if (impl_args
->params_arg
->len
> 0) {
3073 if (bt_value_array_append_string_element(run_args
, "--params")) {
3078 if (bt_value_array_append_string_element(run_args
,
3079 impl_args
->params_arg
->str
)) {
3085 for (i
= 0; i
< bt_value_array_get_size(impl_args
->extra_params
);
3087 const bt_value
*elem
;
3090 elem
= bt_value_array_borrow_element_by_index(impl_args
->extra_params
,
3096 BT_ASSERT(bt_value_is_string(elem
));
3097 arg
= bt_value_string_get(elem
);
3098 ret
= bt_value_array_append_string_element(run_args
, arg
);
3115 void finalize_implicit_component_args(struct implicit_component_args
*args
)
3119 if (args
->comp_arg
) {
3120 g_string_free(args
->comp_arg
, TRUE
);
3123 if (args
->name_arg
) {
3124 g_string_free(args
->name_arg
, TRUE
);
3127 if (args
->params_arg
) {
3128 g_string_free(args
->params_arg
, TRUE
);
3131 bt_value_put_ref(args
->extra_params
);
3135 void destroy_implicit_component_args(void *args
)
3141 finalize_implicit_component_args(args
);
3146 int init_implicit_component_args(struct implicit_component_args
*args
,
3147 const char *comp_arg
, bool exists
)
3151 args
->exists
= exists
;
3152 args
->comp_arg
= g_string_new(comp_arg
);
3153 args
->name_arg
= g_string_new(NULL
);
3154 args
->params_arg
= g_string_new(NULL
);
3155 args
->extra_params
= bt_value_array_create();
3157 if (!args
->comp_arg
|| !args
->name_arg
||
3158 !args
->params_arg
|| !args
->extra_params
) {
3160 finalize_implicit_component_args(args
);
3170 void append_implicit_component_param(struct implicit_component_args
*args
,
3171 const char *key
, const char *value
)
3176 append_param_arg(args
->params_arg
, key
, value
);
3179 /* Escape value to make it suitable to use as a string parameter value. */
3181 gchar
*escape_string_value(const char *value
)
3186 ret
= g_string_new(NULL
);
3197 g_string_append_c(ret
, '\\');
3201 g_string_append_c(ret
, *in
);
3207 return g_string_free(ret
, FALSE
);
3211 * Convert `value` to its equivalent representation as a command line parameter
3216 gchar
*bt_value_to_cli_param_value(bt_value
*value
)
3219 gchar
*result
= NULL
;
3221 buf
= g_string_new(NULL
);
3227 switch (bt_value_get_type(value
)) {
3228 case BT_VALUE_TYPE_STRING
:
3230 const char *str_value
= bt_value_string_get(value
);
3231 gchar
*escaped_str_value
;
3233 escaped_str_value
= escape_string_value(str_value
);
3234 if (!escaped_str_value
) {
3238 g_string_printf(buf
, "\"%s\"", escaped_str_value
);
3240 g_free(escaped_str_value
);
3247 result
= g_string_free(buf
, FALSE
);
3254 g_string_free(buf
, TRUE
);
3262 int append_parameter_to_args(bt_value
*args
, const char *key
, bt_value
*value
)
3265 BT_ASSERT(bt_value_get_type(args
) == BT_VALUE_TYPE_ARRAY
);
3270 gchar
*str_value
= NULL
;
3271 GString
*parameter
= NULL
;
3273 if (bt_value_array_append_string_element(args
, "--params")) {
3279 str_value
= bt_value_to_cli_param_value(value
);
3285 parameter
= g_string_new(NULL
);
3292 g_string_printf(parameter
, "%s=%s", key
, str_value
);
3294 if (bt_value_array_append_string_element(args
, parameter
->str
)) {
3302 g_string_free(parameter
, TRUE
);
3315 int append_string_parameter_to_args(bt_value
*args
, const char *key
, const char *value
)
3317 bt_value
*str_value
;
3320 str_value
= bt_value_string_create_init(value
);
3328 ret
= append_parameter_to_args(args
, key
, str_value
);
3331 BT_VALUE_PUT_REF_AND_RESET(str_value
);
3336 int append_implicit_component_extra_param(struct implicit_component_args
*args
,
3337 const char *key
, const char *value
)
3339 return append_string_parameter_to_args(args
->extra_params
, key
, value
);
3343 int convert_append_name_param(enum bt_config_component_dest dest
,
3344 GString
*cur_name
, GString
*cur_name_prefix
,
3346 bt_value
*all_names
,
3347 GList
**source_names
, GList
**filter_names
,
3352 if (cur_name_prefix
->len
> 0) {
3353 /* We're after a --component option */
3354 GString
*name
= NULL
;
3355 bool append_name_opt
= false;
3357 if (cur_name
->len
== 0) {
3359 * No explicit name was provided for the user
3362 name
= get_component_auto_name(cur_name_prefix
->str
,
3364 append_name_opt
= true;
3367 * An explicit name was provided for the user
3370 if (bt_value_map_has_entry(all_names
,
3372 printf_err("Duplicate component instance name:\n %s\n",
3377 name
= g_string_new(cur_name
->str
);
3386 * Remember this name globally, for the uniqueness of
3387 * all component names.
3389 if (bt_value_map_insert_entry(all_names
, name
->str
, bt_value_null
)) {
3395 * Append the --name option if necessary.
3397 if (append_name_opt
) {
3398 if (bt_value_array_append_string_element(run_args
, "--name")) {
3403 if (bt_value_array_append_string_element(run_args
, name
->str
)) {
3410 * Remember this name specifically for the type of the
3411 * component. This is to create connection arguments.
3414 case BT_CONFIG_COMPONENT_DEST_SOURCE
:
3415 *source_names
= g_list_append(*source_names
, name
);
3417 case BT_CONFIG_COMPONENT_DEST_FILTER
:
3418 *filter_names
= g_list_append(*filter_names
, name
);
3420 case BT_CONFIG_COMPONENT_DEST_SINK
:
3421 *sink_names
= g_list_append(*sink_names
, name
);
3427 g_string_assign(cur_name_prefix
, "");
3440 * Escapes `.`, `:`, and `\` of `input` with `\`.
3443 GString
*escape_dot_colon(const char *input
)
3445 GString
*output
= g_string_new(NULL
);
3453 for (ch
= input
; *ch
!= '\0'; ch
++) {
3454 if (*ch
== '\\' || *ch
== '.' || *ch
== ':') {
3455 g_string_append_c(output
, '\\');
3458 g_string_append_c(output
, *ch
);
3466 * Appends a --connect option to a list of arguments. `upstream_name`
3467 * and `downstream_name` are escaped with escape_dot_colon() in this
3471 int append_connect_arg(bt_value
*run_args
,
3472 const char *upstream_name
, const char *downstream_name
)
3475 GString
*e_upstream_name
= escape_dot_colon(upstream_name
);
3476 GString
*e_downstream_name
= escape_dot_colon(downstream_name
);
3477 GString
*arg
= g_string_new(NULL
);
3479 if (!e_upstream_name
|| !e_downstream_name
|| !arg
) {
3485 ret
= bt_value_array_append_string_element(run_args
, "--connect");
3492 g_string_append(arg
, e_upstream_name
->str
);
3493 g_string_append_c(arg
, ':');
3494 g_string_append(arg
, e_downstream_name
->str
);
3495 ret
= bt_value_array_append_string_element(run_args
, arg
->str
);
3504 g_string_free(arg
, TRUE
);
3507 if (e_upstream_name
) {
3508 g_string_free(e_upstream_name
, TRUE
);
3511 if (e_downstream_name
) {
3512 g_string_free(e_downstream_name
, TRUE
);
3519 * Appends the run command's --connect options for the convert command.
3522 int convert_auto_connect(bt_value
*run_args
,
3523 GList
*source_names
, GList
*filter_names
,
3527 GList
*source_at
= source_names
;
3528 GList
*filter_at
= filter_names
;
3530 GList
*sink_at
= sink_names
;
3532 BT_ASSERT(source_names
);
3533 BT_ASSERT(filter_names
);
3534 BT_ASSERT(sink_names
);
3536 /* Connect all sources to the first filter */
3537 for (source_at
= source_names
; source_at
!= NULL
; source_at
= g_list_next(source_at
)) {
3538 GString
*source_name
= source_at
->data
;
3539 GString
*filter_name
= filter_at
->data
;
3541 ret
= append_connect_arg(run_args
, source_name
->str
,
3548 filter_prev
= filter_at
;
3549 filter_at
= g_list_next(filter_at
);
3551 /* Connect remaining filters */
3552 for (; filter_at
!= NULL
; filter_prev
= filter_at
, filter_at
= g_list_next(filter_at
)) {
3553 GString
*filter_name
= filter_at
->data
;
3554 GString
*filter_prev_name
= filter_prev
->data
;
3556 ret
= append_connect_arg(run_args
, filter_prev_name
->str
,
3563 /* Connect last filter to all sinks */
3564 for (sink_at
= sink_names
; sink_at
!= NULL
; sink_at
= g_list_next(sink_at
)) {
3565 GString
*filter_name
= filter_prev
->data
;
3566 GString
*sink_name
= sink_at
->data
;
3568 ret
= append_connect_arg(run_args
, filter_name
->str
,
3585 int split_timerange(const char *arg
, char **begin
, char **end
)
3588 const char *ch
= arg
;
3590 GString
*g_begin
= NULL
;
3591 GString
*g_end
= NULL
;
3599 g_begin
= bt_common_string_until(ch
, "", ",", &end_pos
);
3600 if (!g_begin
|| ch
[end_pos
] != ',' || g_begin
->len
== 0) {
3606 g_end
= bt_common_string_until(ch
, "", "]", &end_pos
);
3607 if (!g_end
|| g_end
->len
== 0) {
3613 *begin
= g_begin
->str
;
3615 g_string_free(g_begin
, FALSE
);
3616 g_string_free(g_end
, FALSE
);
3626 g_string_free(g_begin
, TRUE
);
3630 g_string_free(g_end
, TRUE
);
3637 int g_list_prepend_gstring(GList
**list
, const char *string
)
3640 GString
*gs
= g_string_new(string
);
3649 *list
= g_list_prepend(*list
, gs
);
3656 struct implicit_component_args
*create_implicit_component_args(void)
3658 struct implicit_component_args
*impl_args
=
3659 g_new0(struct implicit_component_args
, 1);
3665 if (init_implicit_component_args(impl_args
, NULL
, true)) {
3666 destroy_implicit_component_args(impl_args
);
3676 int fill_implicit_ctf_inputs_args(GPtrArray
*implicit_ctf_inputs_args
,
3677 struct implicit_component_args
*base_implicit_ctf_input_args
,
3682 bt_value_status status
;
3684 for (leftover
= leftovers
; leftover
!= NULL
;
3685 leftover
= g_list_next(leftover
)) {
3686 GString
*gs_leftover
= leftover
->data
;
3687 struct implicit_component_args
*impl_args
=
3688 create_implicit_component_args();
3695 impl_args
->exists
= true;
3696 g_string_assign(impl_args
->comp_arg
,
3697 base_implicit_ctf_input_args
->comp_arg
->str
);
3698 g_string_assign(impl_args
->params_arg
,
3699 base_implicit_ctf_input_args
->params_arg
->str
);
3702 * We need our own copy of the extra parameters because
3703 * this is where the unique path goes.
3705 BT_VALUE_PUT_REF_AND_RESET(impl_args
->extra_params
);
3706 status
= bt_value_copy(base_implicit_ctf_input_args
->extra_params
,
3707 &impl_args
->extra_params
);
3708 if (status
!= BT_VALUE_STATUS_OK
) {
3710 destroy_implicit_component_args(impl_args
);
3714 /* Append unique path parameter */
3715 ret
= append_implicit_component_extra_param(impl_args
,
3716 "path", gs_leftover
->str
);
3718 destroy_implicit_component_args(impl_args
);
3722 g_ptr_array_add(implicit_ctf_inputs_args
, impl_args
);
3735 * Creates a Babeltrace config object from the arguments of a convert
3738 * *retcode is set to the appropriate exit code to use.
3741 struct bt_config
*bt_config_convert_from_args(int argc
, const char *argv
[],
3742 int *retcode
, bool force_omit_system_plugin_path
,
3743 bool force_omit_home_plugin_path
,
3744 const bt_value
*initial_plugin_paths
, char *log_level
)
3746 poptContext pc
= NULL
;
3748 enum bt_config_component_dest cur_comp_dest
=
3749 BT_CONFIG_COMPONENT_DEST_UNKNOWN
;
3751 struct bt_config
*cfg
= NULL
;
3752 bool got_input_format_opt
= false;
3753 bool got_output_format_opt
= false;
3754 bool trimmer_has_begin
= false;
3755 bool trimmer_has_end
= false;
3756 bool stream_intersection_mode
= false;
3757 GString
*cur_name
= NULL
;
3758 GString
*cur_name_prefix
= NULL
;
3759 const char *leftover
= NULL
;
3760 bool print_run_args
= false;
3761 bool print_run_args_0
= false;
3762 bool print_ctf_metadata
= false;
3763 bt_value
*run_args
= NULL
;
3764 bt_value
*all_names
= NULL
;
3765 GList
*source_names
= NULL
;
3766 GList
*filter_names
= NULL
;
3767 GList
*sink_names
= NULL
;
3768 GList
*leftovers
= NULL
;
3769 GPtrArray
*implicit_ctf_inputs_args
= NULL
;
3770 struct implicit_component_args base_implicit_ctf_input_args
= { 0 };
3771 struct implicit_component_args implicit_ctf_output_args
= { 0 };
3772 struct implicit_component_args implicit_lttng_live_args
= { 0 };
3773 struct implicit_component_args implicit_dummy_args
= { 0 };
3774 struct implicit_component_args implicit_text_args
= { 0 };
3775 struct implicit_component_args implicit_debug_info_args
= { 0 };
3776 struct implicit_component_args implicit_muxer_args
= { 0 };
3777 struct implicit_component_args implicit_trimmer_args
= { 0 };
3778 bt_value
*plugin_paths
;
3779 char error_buf
[256] = { 0 };
3781 struct bt_common_lttng_live_url_parts lttng_live_url_parts
= { 0 };
3782 char *output
= NULL
;
3784 (void) bt_value_copy(initial_plugin_paths
, &plugin_paths
);
3789 print_convert_usage(stdout
);
3794 if (init_implicit_component_args(&base_implicit_ctf_input_args
,
3795 "source.ctf.fs", false)) {
3799 if (init_implicit_component_args(&implicit_ctf_output_args
,
3800 "sink.ctf.fs", false)) {
3804 if (init_implicit_component_args(&implicit_lttng_live_args
,
3805 "source.ctf.lttng-live", false)) {
3809 if (init_implicit_component_args(&implicit_text_args
,
3810 "sink.text.pretty", false)) {
3814 if (init_implicit_component_args(&implicit_dummy_args
,
3815 "sink.utils.dummy", false)) {
3819 if (init_implicit_component_args(&implicit_debug_info_args
,
3820 "filter.lttng-utils.debug-info", false)) {
3824 if (init_implicit_component_args(&implicit_muxer_args
,
3825 "filter.utils.muxer", true)) {
3829 if (init_implicit_component_args(&implicit_trimmer_args
,
3830 "filter.utils.trimmer", false)) {
3834 implicit_ctf_inputs_args
= g_ptr_array_new_with_free_func(
3835 (GDestroyNotify
) destroy_implicit_component_args
);
3836 if (!implicit_ctf_inputs_args
) {
3841 all_names
= bt_value_map_create();
3847 run_args
= bt_value_array_create();
3853 cur_name
= g_string_new(NULL
);
3859 cur_name_prefix
= g_string_new(NULL
);
3860 if (!cur_name_prefix
) {
3865 ret
= append_env_var_plugin_paths(plugin_paths
);
3871 * First pass: collect all arguments which need to be passed
3872 * as is to the run command. This pass can also add --name
3873 * arguments if needed to automatically name unnamed component
3874 * instances. Also it does the following transformations:
3876 * --path=PATH -> --params=path="PATH"
3877 * --url=URL -> --params=url="URL"
3879 * Also it appends the plugin paths of --plugin-path to
3882 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
3883 convert_long_options
, 0);
3885 printf_err("Cannot get popt context\n");
3889 poptReadDefaultConfig(pc
, 0);
3891 while ((opt
= poptGetNextOpt(pc
)) > 0) {
3893 char *plugin_name
= NULL
;
3894 char *comp_cls_name
= NULL
;
3896 arg
= poptGetOptArg(pc
);
3901 bt_component_class_type type
;
3902 const char *type_prefix
;
3904 /* Append current component's name if needed */
3905 ret
= convert_append_name_param(cur_comp_dest
, cur_name
,
3906 cur_name_prefix
, run_args
, all_names
,
3907 &source_names
, &filter_names
, &sink_names
);
3912 /* Parse the argument */
3913 plugin_comp_cls_names(arg
, &name
, &plugin_name
,
3914 &comp_cls_name
, &type
);
3915 if (!plugin_name
|| !comp_cls_name
) {
3916 printf_err("Invalid format for --component option's argument:\n %s\n",
3922 g_string_assign(cur_name
, name
);
3924 g_string_assign(cur_name
, "");
3928 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
3929 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
3930 type_prefix
= "source";
3932 case BT_COMPONENT_CLASS_TYPE_FILTER
:
3933 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_FILTER
;
3934 type_prefix
= "filter";
3936 case BT_COMPONENT_CLASS_TYPE_SINK
:
3937 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
3938 type_prefix
= "sink";
3944 if (bt_value_array_append_string_element(run_args
,
3950 if (bt_value_array_append_string_element(run_args
, arg
)) {
3955 g_string_assign(cur_name_prefix
, "");
3956 g_string_append_printf(cur_name_prefix
, "%s.%s.%s",
3957 type_prefix
, plugin_name
, comp_cls_name
);
3960 free(comp_cls_name
);
3963 comp_cls_name
= NULL
;
3967 if (cur_name_prefix
->len
== 0) {
3968 printf_err("No current component of which to set parameters:\n %s\n",
3973 if (bt_value_array_append_string_element(run_args
,
3979 if (bt_value_array_append_string_element(run_args
, arg
)) {
3985 if (cur_name_prefix
->len
== 0) {
3986 printf_err("No current component of which to set `path` parameter:\n %s\n",
3991 if (append_string_parameter_to_args(run_args
, "path", arg
)) {
3996 if (cur_name_prefix
->len
== 0) {
3997 printf_err("No current component of which to set `url` parameter:\n %s\n",
4003 if (append_string_parameter_to_args(run_args
, "url", arg
)) {
4008 if (cur_name_prefix
->len
== 0) {
4009 printf_err("No current component to name:\n %s\n",
4014 if (bt_value_array_append_string_element(run_args
, "--name")) {
4019 if (bt_value_array_append_string_element(run_args
, arg
)) {
4024 g_string_assign(cur_name
, arg
);
4026 case OPT_OMIT_HOME_PLUGIN_PATH
:
4027 force_omit_home_plugin_path
= true;
4029 if (bt_value_array_append_string_element(run_args
,
4030 "--omit-home-plugin-path")) {
4035 case OPT_RETRY_DURATION
:
4036 if (bt_value_array_append_string_element(run_args
,
4037 "--retry-duration")) {
4042 if (bt_value_array_append_string_element(run_args
, arg
)) {
4047 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
4048 force_omit_system_plugin_path
= true;
4050 if (bt_value_array_append_string_element(run_args
,
4051 "--omit-system-plugin-path")) {
4056 case OPT_PLUGIN_PATH
:
4057 if (bt_config_append_plugin_paths_check_setuid_setgid(
4058 plugin_paths
, arg
)) {
4062 if (bt_value_array_append_string_element(run_args
,
4068 if (bt_value_array_append_string_element(run_args
, arg
)) {
4074 print_convert_usage(stdout
);
4076 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4079 case OPT_CLOCK_CYCLES
:
4080 case OPT_CLOCK_DATE
:
4081 case OPT_CLOCK_FORCE_CORRELATE
:
4083 case OPT_CLOCK_OFFSET
:
4084 case OPT_CLOCK_OFFSET_NS
:
4085 case OPT_CLOCK_SECONDS
:
4088 case OPT_DEBUG_INFO
:
4089 case OPT_DEBUG_INFO_DIR
:
4090 case OPT_DEBUG_INFO_FULL_PATH
:
4091 case OPT_DEBUG_INFO_TARGET_PREFIX
:
4094 case OPT_INPUT_FORMAT
:
4097 case OPT_OUTPUT_FORMAT
:
4100 case OPT_RUN_ARGS_0
:
4101 case OPT_STREAM_INTERSECTION
:
4104 /* Ignore in this pass */
4107 printf_err("Unknown command-line option specified (option code %d)\n",
4116 /* Append current component's name if needed */
4117 ret
= convert_append_name_param(cur_comp_dest
, cur_name
,
4118 cur_name_prefix
, run_args
, all_names
, &source_names
,
4119 &filter_names
, &sink_names
);
4124 /* Check for option parsing error */
4126 printf_err("While parsing command-line options, at option %s: %s\n",
4127 poptBadOption(pc
, 0), poptStrerror(opt
));
4131 poptFreeContext(pc
);
4136 * Second pass: transform the convert-specific options and
4137 * arguments into implicit component instances for the run
4140 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
4141 convert_long_options
, 0);
4143 printf_err("Cannot get popt context\n");
4147 poptReadDefaultConfig(pc
, 0);
4149 while ((opt
= poptGetNextOpt(pc
)) > 0) {
4150 arg
= poptGetOptArg(pc
);
4154 if (trimmer_has_begin
) {
4155 printf("At --begin option: --begin or --timerange option already specified\n %s\n",
4160 trimmer_has_begin
= true;
4161 ret
= append_implicit_component_extra_param(
4162 &implicit_trimmer_args
, "begin", arg
);
4163 implicit_trimmer_args
.exists
= true;
4169 if (trimmer_has_end
) {
4170 printf("At --end option: --end or --timerange option already specified\n %s\n",
4175 trimmer_has_end
= true;
4176 ret
= append_implicit_component_extra_param(
4177 &implicit_trimmer_args
, "end", arg
);
4178 implicit_trimmer_args
.exists
= true;
4188 if (trimmer_has_begin
|| trimmer_has_end
) {
4189 printf("At --timerange option: --begin, --end, or --timerange option already specified\n %s\n",
4194 ret
= split_timerange(arg
, &begin
, &end
);
4196 printf_err("Invalid --timerange option's argument: expecting BEGIN,END or [BEGIN,END]:\n %s\n",
4201 ret
= append_implicit_component_extra_param(
4202 &implicit_trimmer_args
, "begin", begin
);
4203 ret
|= append_implicit_component_extra_param(
4204 &implicit_trimmer_args
, "end", end
);
4205 implicit_trimmer_args
.exists
= true;
4213 case OPT_CLOCK_CYCLES
:
4214 append_implicit_component_param(
4215 &implicit_text_args
, "clock-cycles", "yes");
4216 implicit_text_args
.exists
= true;
4218 case OPT_CLOCK_DATE
:
4219 append_implicit_component_param(
4220 &implicit_text_args
, "clock-date", "yes");
4221 implicit_text_args
.exists
= true;
4223 case OPT_CLOCK_FORCE_CORRELATE
:
4224 append_implicit_component_param(
4225 &implicit_muxer_args
,
4226 "assume-absolute-clock-classes", "yes");
4229 append_implicit_component_param(
4230 &implicit_text_args
, "clock-gmt", "yes");
4231 append_implicit_component_param(
4232 &implicit_trimmer_args
, "gmt", "yes");
4233 implicit_text_args
.exists
= true;
4235 case OPT_CLOCK_OFFSET
:
4236 base_implicit_ctf_input_args
.exists
= true;
4237 append_implicit_component_param(
4238 &base_implicit_ctf_input_args
,
4239 "clock-class-offset-s", arg
);
4241 case OPT_CLOCK_OFFSET_NS
:
4242 base_implicit_ctf_input_args
.exists
= true;
4243 append_implicit_component_param(
4244 &base_implicit_ctf_input_args
,
4245 "clock-class-offset-ns", arg
);
4247 case OPT_CLOCK_SECONDS
:
4248 append_implicit_component_param(
4249 &implicit_text_args
, "clock-seconds", "yes");
4250 implicit_text_args
.exists
= true;
4253 implicit_text_args
.exists
= true;
4254 ret
= append_implicit_component_extra_param(
4255 &implicit_text_args
, "color", arg
);
4260 case OPT_DEBUG_INFO
:
4261 implicit_debug_info_args
.exists
= true;
4263 case OPT_DEBUG_INFO_DIR
:
4264 implicit_debug_info_args
.exists
= true;
4265 ret
= append_implicit_component_extra_param(
4266 &implicit_debug_info_args
, "debug-info-dir", arg
);
4271 case OPT_DEBUG_INFO_FULL_PATH
:
4272 implicit_debug_info_args
.exists
= true;
4273 append_implicit_component_param(
4274 &implicit_debug_info_args
, "full-path", "yes");
4276 case OPT_DEBUG_INFO_TARGET_PREFIX
:
4277 implicit_debug_info_args
.exists
= true;
4278 ret
= append_implicit_component_extra_param(
4279 &implicit_debug_info_args
,
4280 "target-prefix", arg
);
4287 bt_value
*fields
= fields_from_arg(arg
);
4293 implicit_text_args
.exists
= true;
4294 ret
= insert_flat_params_from_array(
4295 implicit_text_args
.params_arg
,
4297 bt_value_put_ref(fields
);
4305 bt_value
*names
= names_from_arg(arg
);
4311 implicit_text_args
.exists
= true;
4312 ret
= insert_flat_params_from_array(
4313 implicit_text_args
.params_arg
,
4315 bt_value_put_ref(names
);
4322 append_implicit_component_param(
4323 &implicit_text_args
, "no-delta", "yes");
4324 implicit_text_args
.exists
= true;
4326 case OPT_INPUT_FORMAT
:
4327 if (got_input_format_opt
) {
4328 printf_err("Duplicate --input-format option\n");
4332 got_input_format_opt
= true;
4334 if (strcmp(arg
, "ctf") == 0) {
4335 base_implicit_ctf_input_args
.exists
= true;
4336 } else if (strcmp(arg
, "lttng-live") == 0) {
4337 implicit_lttng_live_args
.exists
= true;
4339 printf_err("Unknown legacy input format:\n %s\n",
4344 case OPT_OUTPUT_FORMAT
:
4345 if (got_output_format_opt
) {
4346 printf_err("Duplicate --output-format option\n");
4350 got_output_format_opt
= true;
4352 if (strcmp(arg
, "text") == 0) {
4353 implicit_text_args
.exists
= true;
4354 } else if (strcmp(arg
, "ctf") == 0) {
4355 implicit_ctf_output_args
.exists
= true;
4356 } else if (strcmp(arg
, "dummy") == 0) {
4357 implicit_dummy_args
.exists
= true;
4358 } else if (strcmp(arg
, "ctf-metadata") == 0) {
4359 print_ctf_metadata
= true;
4361 printf_err("Unknown legacy output format:\n %s\n",
4368 printf_err("Duplicate --output option\n");
4372 output
= strdup(arg
);
4379 if (print_run_args_0
) {
4380 printf_err("Cannot specify --run-args and --run-args-0\n");
4384 print_run_args
= true;
4386 case OPT_RUN_ARGS_0
:
4387 if (print_run_args
) {
4388 printf_err("Cannot specify --run-args and --run-args-0\n");
4392 print_run_args_0
= true;
4394 case OPT_STREAM_INTERSECTION
:
4396 * Applies to all traces implementing the trace-info
4399 stream_intersection_mode
= true;
4402 if (*log_level
!= 'V' && *log_level
!= 'D') {
4415 /* Check for option parsing error */
4417 printf_err("While parsing command-line options, at option %s: %s\n",
4418 poptBadOption(pc
, 0), poptStrerror(opt
));
4423 * Legacy behaviour: --verbose used to make the `text` output
4424 * format print more information. --verbose is now equivalent to
4425 * the INFO log level, which is why we compare to 'I' here.
4427 if (*log_level
== 'I') {
4428 append_implicit_component_param(&implicit_text_args
,
4433 * Append home and system plugin paths now that we possibly got
4436 if (append_home_and_system_plugin_paths(plugin_paths
,
4437 force_omit_system_plugin_path
,
4438 force_omit_home_plugin_path
)) {
4442 /* Consume and keep leftover arguments */
4443 while ((leftover
= poptGetArg(pc
))) {
4444 GString
*gs_leftover
= g_string_new(leftover
);
4451 leftovers
= g_list_append(leftovers
, gs_leftover
);
4453 g_string_free(gs_leftover
, TRUE
);
4459 /* Print CTF metadata or print LTTng live sessions */
4460 if (print_ctf_metadata
) {
4461 GString
*gs_leftover
;
4463 if (g_list_length(leftovers
) == 0) {
4464 printf_err("--output-format=ctf-metadata specified without a path\n");
4468 if (g_list_length(leftovers
) > 1) {
4469 printf_err("Too many paths specified for --output-format=ctf-metadata\n");
4473 cfg
= bt_config_print_ctf_metadata_create(plugin_paths
);
4478 gs_leftover
= leftovers
->data
;
4479 g_string_assign(cfg
->cmd_data
.print_ctf_metadata
.path
,
4484 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
4492 * If -o ctf was specified, make sure an output path (--output)
4493 * was also specified. --output does not imply -o ctf because
4494 * it's also used for the default, implicit -o text if -o ctf
4497 if (implicit_ctf_output_args
.exists
) {
4499 printf_err("--output-format=ctf specified without --output (trace output path)\n");
4504 * At this point we know that -o ctf AND --output were
4505 * specified. Make sure that no options were specified
4506 * which would imply -o text because --output would be
4507 * ambiguous in this case. For example, this is wrong:
4509 * babeltrace --names=all -o ctf --output=/tmp/path my-trace
4511 * because --names=all implies -o text, and --output
4512 * could apply to both the sink.text.pretty and
4513 * sink.ctf.fs implicit components.
4515 if (implicit_text_args
.exists
) {
4516 printf_err("Ambiguous --output option: --output-format=ctf specified but another option implies --output-format=text\n");
4522 * If -o dummy and -o ctf were not specified, and if there are
4523 * no explicit sink components, then use an implicit
4524 * `sink.text.pretty` component.
4526 if (!implicit_dummy_args
.exists
&& !implicit_ctf_output_args
.exists
&&
4528 implicit_text_args
.exists
= true;
4532 * Set implicit `sink.text.pretty` or `sink.ctf.fs` component's
4533 * `path` parameter if --output was specified.
4536 if (implicit_text_args
.exists
) {
4537 append_implicit_component_extra_param(&implicit_text_args
,
4539 } else if (implicit_ctf_output_args
.exists
) {
4540 append_implicit_component_extra_param(&implicit_ctf_output_args
,
4545 /* Decide where the leftover argument(s) go */
4546 if (g_list_length(leftovers
) > 0) {
4547 if (implicit_lttng_live_args
.exists
) {
4548 GString
*gs_leftover
;
4550 if (g_list_length(leftovers
) > 1) {
4551 printf_err("Too many URLs specified for --output-format=lttng-live\n");
4555 gs_leftover
= leftovers
->data
;
4556 lttng_live_url_parts
=
4557 bt_common_parse_lttng_live_url(gs_leftover
->str
,
4558 error_buf
, sizeof(error_buf
));
4559 if (!lttng_live_url_parts
.proto
) {
4560 printf_err("Invalid LTTng live URL format: %s\n",
4565 if (!lttng_live_url_parts
.session_name
) {
4566 /* Print LTTng live sessions */
4567 cfg
= bt_config_print_lttng_live_sessions_create(
4573 g_string_assign(cfg
->cmd_data
.print_lttng_live_sessions
.url
,
4578 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
4585 ret
= append_implicit_component_extra_param(
4586 &implicit_lttng_live_args
, "url",
4593 * Append one implicit component argument set
4594 * for each leftover (souce.ctf.fs paths). Copy
4595 * the base implicit component arguments.
4596 * Note that they still have to be named later.
4598 ret
= fill_implicit_ctf_inputs_args(
4599 implicit_ctf_inputs_args
,
4600 &base_implicit_ctf_input_args
, leftovers
);
4608 * Ensure mutual exclusion between implicit `source.ctf.fs` and
4609 * `source.ctf.lttng-live` components.
4611 if (base_implicit_ctf_input_args
.exists
&&
4612 implicit_lttng_live_args
.exists
) {
4613 printf_err("Cannot create both implicit `%s` and `%s` components\n",
4614 base_implicit_ctf_input_args
.comp_arg
->str
,
4615 implicit_lttng_live_args
.comp_arg
->str
);
4620 * If the implicit `source.ctf.fs` or `source.ctf.lttng-live`
4621 * components exists, make sure there's at least one leftover
4622 * (which is the path or URL).
4624 if (base_implicit_ctf_input_args
.exists
&&
4625 g_list_length(leftovers
) == 0) {
4626 printf_err("Missing path for implicit `%s` component\n",
4627 base_implicit_ctf_input_args
.comp_arg
->str
);
4631 if (implicit_lttng_live_args
.exists
&& g_list_length(leftovers
) == 0) {
4632 printf_err("Missing URL for implicit `%s` component\n",
4633 implicit_lttng_live_args
.comp_arg
->str
);
4637 /* Assign names to implicit components */
4638 for (i
= 0; i
< implicit_ctf_inputs_args
->len
; i
++) {
4639 struct implicit_component_args
*impl_args
=
4640 g_ptr_array_index(implicit_ctf_inputs_args
, i
);
4642 ret
= assign_name_to_implicit_component(impl_args
,
4643 "source-ctf-fs", all_names
, &source_names
, true);
4649 ret
= assign_name_to_implicit_component(&implicit_lttng_live_args
,
4650 "lttng-live", all_names
, &source_names
, true);
4655 ret
= assign_name_to_implicit_component(&implicit_text_args
,
4656 "pretty", all_names
, &sink_names
, true);
4661 ret
= assign_name_to_implicit_component(&implicit_ctf_output_args
,
4662 "sink-ctf-fs", all_names
, &sink_names
, true);
4667 ret
= assign_name_to_implicit_component(&implicit_dummy_args
,
4668 "dummy", all_names
, &sink_names
, true);
4673 ret
= assign_name_to_implicit_component(&implicit_muxer_args
,
4674 "muxer", all_names
, NULL
, false);
4679 ret
= assign_name_to_implicit_component(&implicit_trimmer_args
,
4680 "trimmer", all_names
, NULL
, false);
4685 ret
= assign_name_to_implicit_component(&implicit_debug_info_args
,
4686 "debug-info", all_names
, NULL
, false);
4691 /* Make sure there's at least one source and one sink */
4692 if (!source_names
) {
4693 printf_err("No source component\n");
4698 printf_err("No sink component\n");
4703 * Prepend the muxer, the trimmer, and the debug info to the
4704 * filter chain so that we have:
4706 * sources -> muxer -> [trimmer] -> [debug info] ->
4707 * [user filters] -> sinks
4709 if (implicit_debug_info_args
.exists
) {
4710 if (g_list_prepend_gstring(&filter_names
,
4711 implicit_debug_info_args
.name_arg
->str
)) {
4716 if (implicit_trimmer_args
.exists
) {
4717 if (g_list_prepend_gstring(&filter_names
,
4718 implicit_trimmer_args
.name_arg
->str
)) {
4723 if (g_list_prepend_gstring(&filter_names
,
4724 implicit_muxer_args
.name_arg
->str
)) {
4729 * Append the equivalent run arguments for the implicit
4732 for (i
= 0; i
< implicit_ctf_inputs_args
->len
; i
++) {
4733 struct implicit_component_args
*impl_args
=
4734 g_ptr_array_index(implicit_ctf_inputs_args
, i
);
4736 ret
= append_run_args_for_implicit_component(impl_args
,
4743 ret
= append_run_args_for_implicit_component(&implicit_lttng_live_args
,
4749 ret
= append_run_args_for_implicit_component(&implicit_text_args
,
4755 ret
= append_run_args_for_implicit_component(&implicit_ctf_output_args
,
4761 ret
= append_run_args_for_implicit_component(&implicit_dummy_args
,
4767 ret
= append_run_args_for_implicit_component(&implicit_muxer_args
,
4773 ret
= append_run_args_for_implicit_component(&implicit_trimmer_args
,
4779 ret
= append_run_args_for_implicit_component(&implicit_debug_info_args
,
4785 /* Auto-connect components */
4786 ret
= convert_auto_connect(run_args
, source_names
, filter_names
,
4789 printf_err("Cannot auto-connect components\n");
4794 * We have all the run command arguments now. Depending on
4795 * --run-args, we pass this to the run command or print them
4798 if (print_run_args
|| print_run_args_0
) {
4799 if (stream_intersection_mode
) {
4800 printf_err("Cannot specify --stream-intersection with --run-args or --run-args-0\n");
4804 for (i
= 0; i
< bt_value_array_get_size(run_args
); i
++) {
4805 const bt_value
*arg_value
=
4806 bt_value_array_borrow_element_by_index(run_args
,
4809 GString
*quoted
= NULL
;
4810 const char *arg_to_print
;
4812 BT_ASSERT(arg_value
);
4813 arg
= bt_value_string_get(arg_value
);
4815 if (print_run_args
) {
4816 quoted
= bt_common_shell_quote(arg
, true);
4821 arg_to_print
= quoted
->str
;
4826 printf("%s", arg_to_print
);
4829 g_string_free(quoted
, TRUE
);
4832 if (i
< bt_value_array_get_size(run_args
) - 1) {
4833 if (print_run_args
) {
4842 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4846 cfg
= bt_config_run_from_args_array(run_args
, retcode
,
4847 force_omit_system_plugin_path
,
4848 force_omit_home_plugin_path
,
4849 initial_plugin_paths
);
4854 cfg
->cmd_data
.run
.stream_intersection_mode
= stream_intersection_mode
;
4859 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4863 poptFreeContext(pc
);
4870 g_string_free(cur_name
, TRUE
);
4873 if (cur_name_prefix
) {
4874 g_string_free(cur_name_prefix
, TRUE
);
4877 if (implicit_ctf_inputs_args
) {
4878 g_ptr_array_free(implicit_ctf_inputs_args
, TRUE
);
4881 bt_value_put_ref(run_args
);
4882 bt_value_put_ref(all_names
);
4883 destroy_glist_of_gstring(source_names
);
4884 destroy_glist_of_gstring(filter_names
);
4885 destroy_glist_of_gstring(sink_names
);
4886 destroy_glist_of_gstring(leftovers
);
4887 finalize_implicit_component_args(&base_implicit_ctf_input_args
);
4888 finalize_implicit_component_args(&implicit_ctf_output_args
);
4889 finalize_implicit_component_args(&implicit_lttng_live_args
);
4890 finalize_implicit_component_args(&implicit_dummy_args
);
4891 finalize_implicit_component_args(&implicit_text_args
);
4892 finalize_implicit_component_args(&implicit_debug_info_args
);
4893 finalize_implicit_component_args(&implicit_muxer_args
);
4894 finalize_implicit_component_args(&implicit_trimmer_args
);
4895 bt_value_put_ref(plugin_paths
);
4896 bt_common_destroy_lttng_live_url_parts(<tng_live_url_parts
);
4901 * Prints the Babeltrace 2.x general usage.
4904 void print_gen_usage(FILE *fp
)
4906 fprintf(fp
, "Usage: babeltrace [GENERAL OPTIONS] [COMMAND] [COMMAND ARGUMENTS]\n");
4908 fprintf(fp
, "General options:\n");
4910 fprintf(fp
, " -d, --debug Enable debug mode (same as --log-level=V)\n");
4911 fprintf(fp
, " -h, --help Show this help and quit\n");
4912 fprintf(fp
, " -l, --log-level=LVL Set all log levels to LVL (`N`, `V`, `D`,\n");
4913 fprintf(fp
, " `I`, `W` (default), `E`, or `F`)\n");
4914 fprintf(fp
, " -v, --verbose Enable verbose mode (same as --log-level=I)\n");
4915 fprintf(fp
, " -V, --version Show version and quit\n");
4917 fprintf(fp
, "Available commands:\n");
4919 fprintf(fp
, " convert Convert and trim traces (default)\n");
4920 fprintf(fp
, " help Get help for a plugin or a component class\n");
4921 fprintf(fp
, " list-plugins List available plugins and their content\n");
4922 fprintf(fp
, " query Query objects from a component class\n");
4923 fprintf(fp
, " run Build a processing graph and run it\n");
4925 fprintf(fp
, "Use `babeltrace COMMAND --help` to show the help of COMMAND.\n");
4929 char log_level_from_arg(const char *arg
)
4933 if (strcmp(arg
, "VERBOSE") == 0 ||
4934 strcmp(arg
, "V") == 0) {
4936 } else if (strcmp(arg
, "DEBUG") == 0 ||
4937 strcmp(arg
, "D") == 0) {
4939 } else if (strcmp(arg
, "INFO") == 0 ||
4940 strcmp(arg
, "I") == 0) {
4942 } else if (strcmp(arg
, "WARN") == 0 ||
4943 strcmp(arg
, "WARNING") == 0 ||
4944 strcmp(arg
, "W") == 0) {
4946 } else if (strcmp(arg
, "ERROR") == 0 ||
4947 strcmp(arg
, "E") == 0) {
4949 } else if (strcmp(arg
, "FATAL") == 0 ||
4950 strcmp(arg
, "F") == 0) {
4952 } else if (strcmp(arg
, "NONE") == 0 ||
4953 strcmp(arg
, "N") == 0) {
4960 struct bt_config
*bt_config_cli_args_create(int argc
, const char *argv
[],
4961 int *retcode
, bool force_omit_system_plugin_path
,
4962 bool force_omit_home_plugin_path
,
4963 const bt_value
*initial_plugin_paths
)
4965 struct bt_config
*config
= NULL
;
4967 const char **command_argv
= NULL
;
4968 int command_argc
= -1;
4969 const char *command_name
= NULL
;
4970 char log_level
= 'U';
4973 COMMAND_TYPE_NONE
= -1,
4974 COMMAND_TYPE_RUN
= 0,
4975 COMMAND_TYPE_CONVERT
,
4976 COMMAND_TYPE_LIST_PLUGINS
,
4979 } command_type
= COMMAND_TYPE_NONE
;
4983 if (!initial_plugin_paths
) {
4984 initial_plugin_paths
= bt_value_array_create();
4985 if (!initial_plugin_paths
) {
4990 bt_value_get_ref(initial_plugin_paths
);
4996 print_gen_usage(stdout
);
5000 for (i
= 1; i
< argc
; i
++) {
5001 const char *cur_arg
= argv
[i
];
5002 const char *next_arg
= i
== (argc
- 1) ? NULL
: argv
[i
+ 1];
5004 if (strcmp(cur_arg
, "-d") == 0 ||
5005 strcmp(cur_arg
, "--debug") == 0) {
5007 } else if (strcmp(cur_arg
, "-v") == 0 ||
5008 strcmp(cur_arg
, "--verbose") == 0) {
5009 if (log_level
!= 'V' && log_level
!= 'D') {
5011 * Legacy: do not override a previous
5012 * --debug because --verbose and --debug
5013 * can be specified together (in this
5014 * case we want the lowest log level to
5019 } else if (strcmp(cur_arg
, "--log-level") == 0 ||
5020 strcmp(cur_arg
, "-l") == 0) {
5022 printf_err("Missing log level value for --log-level option\n");
5027 log_level
= log_level_from_arg(next_arg
);
5028 if (log_level
== 'U') {
5029 printf_err("Invalid argument for --log-level option:\n %s\n",
5036 } else if (strncmp(cur_arg
, "--log-level=", 12) == 0) {
5037 const char *arg
= &cur_arg
[12];
5039 log_level
= log_level_from_arg(arg
);
5040 if (log_level
== 'U') {
5041 printf_err("Invalid argument for --log-level option:\n %s\n",
5046 } else if (strncmp(cur_arg
, "-l", 2) == 0) {
5047 const char *arg
= &cur_arg
[2];
5049 log_level
= log_level_from_arg(arg
);
5050 if (log_level
== 'U') {
5051 printf_err("Invalid argument for --log-level option:\n %s\n",
5056 } else if (strcmp(cur_arg
, "-V") == 0 ||
5057 strcmp(cur_arg
, "--version") == 0) {
5060 } else if (strcmp(cur_arg
, "-h") == 0 ||
5061 strcmp(cur_arg
, "--help") == 0) {
5062 print_gen_usage(stdout
);
5066 * First unknown argument: is it a known command
5069 command_argv
= &argv
[i
];
5070 command_argc
= argc
- i
;
5072 if (strcmp(cur_arg
, "convert") == 0) {
5073 command_type
= COMMAND_TYPE_CONVERT
;
5074 } else if (strcmp(cur_arg
, "list-plugins") == 0) {
5075 command_type
= COMMAND_TYPE_LIST_PLUGINS
;
5076 } else if (strcmp(cur_arg
, "help") == 0) {
5077 command_type
= COMMAND_TYPE_HELP
;
5078 } else if (strcmp(cur_arg
, "query") == 0) {
5079 command_type
= COMMAND_TYPE_QUERY
;
5080 } else if (strcmp(cur_arg
, "run") == 0) {
5081 command_type
= COMMAND_TYPE_RUN
;
5084 * Unknown argument, but not a known
5085 * command name: assume the default
5086 * `convert` command.
5088 command_type
= COMMAND_TYPE_CONVERT
;
5089 command_name
= "convert";
5090 command_argv
= &argv
[i
- 1];
5091 command_argc
= argc
- i
+ 1;
5097 if (command_type
== COMMAND_TYPE_NONE
) {
5099 * We only got non-help, non-version general options
5100 * like --verbose and --debug, without any other
5101 * arguments, so we can't do anything useful: print the
5104 print_gen_usage(stdout
);
5108 BT_ASSERT(command_argv
);
5109 BT_ASSERT(command_argc
>= 0);
5111 switch (command_type
) {
5112 case COMMAND_TYPE_RUN
:
5113 config
= bt_config_run_from_args(command_argc
, command_argv
,
5114 retcode
, force_omit_system_plugin_path
,
5115 force_omit_home_plugin_path
, initial_plugin_paths
);
5117 case COMMAND_TYPE_CONVERT
:
5118 config
= bt_config_convert_from_args(command_argc
, command_argv
,
5119 retcode
, force_omit_system_plugin_path
,
5120 force_omit_home_plugin_path
,
5121 initial_plugin_paths
, &log_level
);
5123 case COMMAND_TYPE_LIST_PLUGINS
:
5124 config
= bt_config_list_plugins_from_args(command_argc
,
5125 command_argv
, retcode
, force_omit_system_plugin_path
,
5126 force_omit_home_plugin_path
, initial_plugin_paths
);
5128 case COMMAND_TYPE_HELP
:
5129 config
= bt_config_help_from_args(command_argc
,
5130 command_argv
, retcode
, force_omit_system_plugin_path
,
5131 force_omit_home_plugin_path
, initial_plugin_paths
);
5133 case COMMAND_TYPE_QUERY
:
5134 config
= bt_config_query_from_args(command_argc
,
5135 command_argv
, retcode
, force_omit_system_plugin_path
,
5136 force_omit_home_plugin_path
, initial_plugin_paths
);
5143 if (log_level
== 'U') {
5147 config
->log_level
= log_level
;
5148 config
->command_name
= command_name
;
5152 bt_value_put_ref(initial_plugin_paths
);