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 int append_parameter_to_args(bt_value
*args
, const char *key
, const char *value
)
3214 BT_ASSERT(bt_value_get_type(args
) == BT_VALUE_TYPE_ARRAY
);
3219 gchar
*escaped_value
;
3220 GString
*parameter
= NULL
;
3222 if (bt_value_array_append_string_element(args
, "--params")) {
3228 escaped_value
= escape_string_value(value
);
3229 if (!escaped_value
) {
3234 parameter
= g_string_new(NULL
);
3241 g_string_printf(parameter
, "%s=\"%s\"", key
, escaped_value
);
3243 if (bt_value_array_append_string_element(args
, parameter
->str
)) {
3250 if (escaped_value
) {
3251 g_free(escaped_value
);
3252 escaped_value
= NULL
;
3256 g_string_free(parameter
, TRUE
);
3264 int append_implicit_component_extra_param(struct implicit_component_args
*args
,
3265 const char *key
, const char *value
)
3267 return append_parameter_to_args(args
->extra_params
, key
, value
);
3271 int convert_append_name_param(enum bt_config_component_dest dest
,
3272 GString
*cur_name
, GString
*cur_name_prefix
,
3274 bt_value
*all_names
,
3275 GList
**source_names
, GList
**filter_names
,
3280 if (cur_name_prefix
->len
> 0) {
3281 /* We're after a --component option */
3282 GString
*name
= NULL
;
3283 bool append_name_opt
= false;
3285 if (cur_name
->len
== 0) {
3287 * No explicit name was provided for the user
3290 name
= get_component_auto_name(cur_name_prefix
->str
,
3292 append_name_opt
= true;
3295 * An explicit name was provided for the user
3298 if (bt_value_map_has_entry(all_names
,
3300 printf_err("Duplicate component instance name:\n %s\n",
3305 name
= g_string_new(cur_name
->str
);
3314 * Remember this name globally, for the uniqueness of
3315 * all component names.
3317 if (bt_value_map_insert_entry(all_names
, name
->str
, bt_value_null
)) {
3323 * Append the --name option if necessary.
3325 if (append_name_opt
) {
3326 if (bt_value_array_append_string_element(run_args
, "--name")) {
3331 if (bt_value_array_append_string_element(run_args
, name
->str
)) {
3338 * Remember this name specifically for the type of the
3339 * component. This is to create connection arguments.
3342 case BT_CONFIG_COMPONENT_DEST_SOURCE
:
3343 *source_names
= g_list_append(*source_names
, name
);
3345 case BT_CONFIG_COMPONENT_DEST_FILTER
:
3346 *filter_names
= g_list_append(*filter_names
, name
);
3348 case BT_CONFIG_COMPONENT_DEST_SINK
:
3349 *sink_names
= g_list_append(*sink_names
, name
);
3355 g_string_assign(cur_name_prefix
, "");
3368 * Escapes `.`, `:`, and `\` of `input` with `\`.
3371 GString
*escape_dot_colon(const char *input
)
3373 GString
*output
= g_string_new(NULL
);
3381 for (ch
= input
; *ch
!= '\0'; ch
++) {
3382 if (*ch
== '\\' || *ch
== '.' || *ch
== ':') {
3383 g_string_append_c(output
, '\\');
3386 g_string_append_c(output
, *ch
);
3394 * Appends a --connect option to a list of arguments. `upstream_name`
3395 * and `downstream_name` are escaped with escape_dot_colon() in this
3399 int append_connect_arg(bt_value
*run_args
,
3400 const char *upstream_name
, const char *downstream_name
)
3403 GString
*e_upstream_name
= escape_dot_colon(upstream_name
);
3404 GString
*e_downstream_name
= escape_dot_colon(downstream_name
);
3405 GString
*arg
= g_string_new(NULL
);
3407 if (!e_upstream_name
|| !e_downstream_name
|| !arg
) {
3413 ret
= bt_value_array_append_string_element(run_args
, "--connect");
3420 g_string_append(arg
, e_upstream_name
->str
);
3421 g_string_append_c(arg
, ':');
3422 g_string_append(arg
, e_downstream_name
->str
);
3423 ret
= bt_value_array_append_string_element(run_args
, arg
->str
);
3432 g_string_free(arg
, TRUE
);
3435 if (e_upstream_name
) {
3436 g_string_free(e_upstream_name
, TRUE
);
3439 if (e_downstream_name
) {
3440 g_string_free(e_downstream_name
, TRUE
);
3447 * Appends the run command's --connect options for the convert command.
3450 int convert_auto_connect(bt_value
*run_args
,
3451 GList
*source_names
, GList
*filter_names
,
3455 GList
*source_at
= source_names
;
3456 GList
*filter_at
= filter_names
;
3458 GList
*sink_at
= sink_names
;
3460 BT_ASSERT(source_names
);
3461 BT_ASSERT(filter_names
);
3462 BT_ASSERT(sink_names
);
3464 /* Connect all sources to the first filter */
3465 for (source_at
= source_names
; source_at
!= NULL
; source_at
= g_list_next(source_at
)) {
3466 GString
*source_name
= source_at
->data
;
3467 GString
*filter_name
= filter_at
->data
;
3469 ret
= append_connect_arg(run_args
, source_name
->str
,
3476 filter_prev
= filter_at
;
3477 filter_at
= g_list_next(filter_at
);
3479 /* Connect remaining filters */
3480 for (; filter_at
!= NULL
; filter_prev
= filter_at
, filter_at
= g_list_next(filter_at
)) {
3481 GString
*filter_name
= filter_at
->data
;
3482 GString
*filter_prev_name
= filter_prev
->data
;
3484 ret
= append_connect_arg(run_args
, filter_prev_name
->str
,
3491 /* Connect last filter to all sinks */
3492 for (sink_at
= sink_names
; sink_at
!= NULL
; sink_at
= g_list_next(sink_at
)) {
3493 GString
*filter_name
= filter_prev
->data
;
3494 GString
*sink_name
= sink_at
->data
;
3496 ret
= append_connect_arg(run_args
, filter_name
->str
,
3513 int split_timerange(const char *arg
, char **begin
, char **end
)
3516 const char *ch
= arg
;
3518 GString
*g_begin
= NULL
;
3519 GString
*g_end
= NULL
;
3527 g_begin
= bt_common_string_until(ch
, "", ",", &end_pos
);
3528 if (!g_begin
|| ch
[end_pos
] != ',' || g_begin
->len
== 0) {
3534 g_end
= bt_common_string_until(ch
, "", "]", &end_pos
);
3535 if (!g_end
|| g_end
->len
== 0) {
3541 *begin
= g_begin
->str
;
3543 g_string_free(g_begin
, FALSE
);
3544 g_string_free(g_end
, FALSE
);
3554 g_string_free(g_begin
, TRUE
);
3558 g_string_free(g_end
, TRUE
);
3565 int g_list_prepend_gstring(GList
**list
, const char *string
)
3568 GString
*gs
= g_string_new(string
);
3577 *list
= g_list_prepend(*list
, gs
);
3584 struct implicit_component_args
*create_implicit_component_args(void)
3586 struct implicit_component_args
*impl_args
=
3587 g_new0(struct implicit_component_args
, 1);
3593 if (init_implicit_component_args(impl_args
, NULL
, true)) {
3594 destroy_implicit_component_args(impl_args
);
3604 int fill_implicit_ctf_inputs_args(GPtrArray
*implicit_ctf_inputs_args
,
3605 struct implicit_component_args
*base_implicit_ctf_input_args
,
3610 bt_value_status status
;
3612 for (leftover
= leftovers
; leftover
!= NULL
;
3613 leftover
= g_list_next(leftover
)) {
3614 GString
*gs_leftover
= leftover
->data
;
3615 struct implicit_component_args
*impl_args
=
3616 create_implicit_component_args();
3623 impl_args
->exists
= true;
3624 g_string_assign(impl_args
->comp_arg
,
3625 base_implicit_ctf_input_args
->comp_arg
->str
);
3626 g_string_assign(impl_args
->params_arg
,
3627 base_implicit_ctf_input_args
->params_arg
->str
);
3630 * We need our own copy of the extra parameters because
3631 * this is where the unique path goes.
3633 BT_VALUE_PUT_REF_AND_RESET(impl_args
->extra_params
);
3634 status
= bt_value_copy(base_implicit_ctf_input_args
->extra_params
,
3635 &impl_args
->extra_params
);
3636 if (status
!= BT_VALUE_STATUS_OK
) {
3638 destroy_implicit_component_args(impl_args
);
3642 /* Append unique path parameter */
3643 ret
= append_implicit_component_extra_param(impl_args
,
3644 "path", gs_leftover
->str
);
3646 destroy_implicit_component_args(impl_args
);
3650 g_ptr_array_add(implicit_ctf_inputs_args
, impl_args
);
3663 * Creates a Babeltrace config object from the arguments of a convert
3666 * *retcode is set to the appropriate exit code to use.
3669 struct bt_config
*bt_config_convert_from_args(int argc
, const char *argv
[],
3670 int *retcode
, bool force_omit_system_plugin_path
,
3671 bool force_omit_home_plugin_path
,
3672 const bt_value
*initial_plugin_paths
, char *log_level
)
3674 poptContext pc
= NULL
;
3676 enum bt_config_component_dest cur_comp_dest
=
3677 BT_CONFIG_COMPONENT_DEST_UNKNOWN
;
3679 struct bt_config
*cfg
= NULL
;
3680 bool got_input_format_opt
= false;
3681 bool got_output_format_opt
= false;
3682 bool trimmer_has_begin
= false;
3683 bool trimmer_has_end
= false;
3684 bool stream_intersection_mode
= false;
3685 GString
*cur_name
= NULL
;
3686 GString
*cur_name_prefix
= NULL
;
3687 const char *leftover
= NULL
;
3688 bool print_run_args
= false;
3689 bool print_run_args_0
= false;
3690 bool print_ctf_metadata
= false;
3691 bt_value
*run_args
= NULL
;
3692 bt_value
*all_names
= NULL
;
3693 GList
*source_names
= NULL
;
3694 GList
*filter_names
= NULL
;
3695 GList
*sink_names
= NULL
;
3696 GList
*leftovers
= NULL
;
3697 GPtrArray
*implicit_ctf_inputs_args
= NULL
;
3698 struct implicit_component_args base_implicit_ctf_input_args
= { 0 };
3699 struct implicit_component_args implicit_ctf_output_args
= { 0 };
3700 struct implicit_component_args implicit_lttng_live_args
= { 0 };
3701 struct implicit_component_args implicit_dummy_args
= { 0 };
3702 struct implicit_component_args implicit_text_args
= { 0 };
3703 struct implicit_component_args implicit_debug_info_args
= { 0 };
3704 struct implicit_component_args implicit_muxer_args
= { 0 };
3705 struct implicit_component_args implicit_trimmer_args
= { 0 };
3706 bt_value
*plugin_paths
;
3707 char error_buf
[256] = { 0 };
3709 struct bt_common_lttng_live_url_parts lttng_live_url_parts
= { 0 };
3710 char *output
= NULL
;
3712 (void) bt_value_copy(initial_plugin_paths
, &plugin_paths
);
3717 print_convert_usage(stdout
);
3722 if (init_implicit_component_args(&base_implicit_ctf_input_args
,
3723 "source.ctf.fs", false)) {
3727 if (init_implicit_component_args(&implicit_ctf_output_args
,
3728 "sink.ctf.fs", false)) {
3732 if (init_implicit_component_args(&implicit_lttng_live_args
,
3733 "source.ctf.lttng-live", false)) {
3737 if (init_implicit_component_args(&implicit_text_args
,
3738 "sink.text.pretty", false)) {
3742 if (init_implicit_component_args(&implicit_dummy_args
,
3743 "sink.utils.dummy", false)) {
3747 if (init_implicit_component_args(&implicit_debug_info_args
,
3748 "filter.lttng-utils.debug-info", false)) {
3752 if (init_implicit_component_args(&implicit_muxer_args
,
3753 "filter.utils.muxer", true)) {
3757 if (init_implicit_component_args(&implicit_trimmer_args
,
3758 "filter.utils.trimmer", false)) {
3762 implicit_ctf_inputs_args
= g_ptr_array_new_with_free_func(
3763 (GDestroyNotify
) destroy_implicit_component_args
);
3764 if (!implicit_ctf_inputs_args
) {
3769 all_names
= bt_value_map_create();
3775 run_args
= bt_value_array_create();
3781 cur_name
= g_string_new(NULL
);
3787 cur_name_prefix
= g_string_new(NULL
);
3788 if (!cur_name_prefix
) {
3793 ret
= append_env_var_plugin_paths(plugin_paths
);
3799 * First pass: collect all arguments which need to be passed
3800 * as is to the run command. This pass can also add --name
3801 * arguments if needed to automatically name unnamed component
3802 * instances. Also it does the following transformations:
3804 * --path=PATH -> --params=path="PATH"
3805 * --url=URL -> --params=url="URL"
3807 * Also it appends the plugin paths of --plugin-path to
3810 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
3811 convert_long_options
, 0);
3813 printf_err("Cannot get popt context\n");
3817 poptReadDefaultConfig(pc
, 0);
3819 while ((opt
= poptGetNextOpt(pc
)) > 0) {
3821 char *plugin_name
= NULL
;
3822 char *comp_cls_name
= NULL
;
3824 arg
= poptGetOptArg(pc
);
3829 bt_component_class_type type
;
3830 const char *type_prefix
;
3832 /* Append current component's name if needed */
3833 ret
= convert_append_name_param(cur_comp_dest
, cur_name
,
3834 cur_name_prefix
, run_args
, all_names
,
3835 &source_names
, &filter_names
, &sink_names
);
3840 /* Parse the argument */
3841 plugin_comp_cls_names(arg
, &name
, &plugin_name
,
3842 &comp_cls_name
, &type
);
3843 if (!plugin_name
|| !comp_cls_name
) {
3844 printf_err("Invalid format for --component option's argument:\n %s\n",
3850 g_string_assign(cur_name
, name
);
3852 g_string_assign(cur_name
, "");
3856 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
3857 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
3858 type_prefix
= "source";
3860 case BT_COMPONENT_CLASS_TYPE_FILTER
:
3861 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_FILTER
;
3862 type_prefix
= "filter";
3864 case BT_COMPONENT_CLASS_TYPE_SINK
:
3865 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
3866 type_prefix
= "sink";
3872 if (bt_value_array_append_string_element(run_args
,
3878 if (bt_value_array_append_string_element(run_args
, arg
)) {
3883 g_string_assign(cur_name_prefix
, "");
3884 g_string_append_printf(cur_name_prefix
, "%s.%s.%s",
3885 type_prefix
, plugin_name
, comp_cls_name
);
3888 free(comp_cls_name
);
3891 comp_cls_name
= NULL
;
3895 if (cur_name_prefix
->len
== 0) {
3896 printf_err("No current component of which to set parameters:\n %s\n",
3901 if (bt_value_array_append_string_element(run_args
,
3907 if (bt_value_array_append_string_element(run_args
, arg
)) {
3913 if (cur_name_prefix
->len
== 0) {
3914 printf_err("No current component of which to set `path` parameter:\n %s\n",
3919 if (append_parameter_to_args(run_args
, "path", arg
)) {
3924 if (cur_name_prefix
->len
== 0) {
3925 printf_err("No current component of which to set `url` parameter:\n %s\n",
3931 if (append_parameter_to_args(run_args
, "url", arg
)) {
3936 if (cur_name_prefix
->len
== 0) {
3937 printf_err("No current component to name:\n %s\n",
3942 if (bt_value_array_append_string_element(run_args
, "--name")) {
3947 if (bt_value_array_append_string_element(run_args
, arg
)) {
3952 g_string_assign(cur_name
, arg
);
3954 case OPT_OMIT_HOME_PLUGIN_PATH
:
3955 force_omit_home_plugin_path
= true;
3957 if (bt_value_array_append_string_element(run_args
,
3958 "--omit-home-plugin-path")) {
3963 case OPT_RETRY_DURATION
:
3964 if (bt_value_array_append_string_element(run_args
,
3965 "--retry-duration")) {
3970 if (bt_value_array_append_string_element(run_args
, arg
)) {
3975 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
3976 force_omit_system_plugin_path
= true;
3978 if (bt_value_array_append_string_element(run_args
,
3979 "--omit-system-plugin-path")) {
3984 case OPT_PLUGIN_PATH
:
3985 if (bt_config_append_plugin_paths_check_setuid_setgid(
3986 plugin_paths
, arg
)) {
3990 if (bt_value_array_append_string_element(run_args
,
3996 if (bt_value_array_append_string_element(run_args
, arg
)) {
4002 print_convert_usage(stdout
);
4004 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4007 case OPT_CLOCK_CYCLES
:
4008 case OPT_CLOCK_DATE
:
4009 case OPT_CLOCK_FORCE_CORRELATE
:
4011 case OPT_CLOCK_OFFSET
:
4012 case OPT_CLOCK_OFFSET_NS
:
4013 case OPT_CLOCK_SECONDS
:
4016 case OPT_DEBUG_INFO
:
4017 case OPT_DEBUG_INFO_DIR
:
4018 case OPT_DEBUG_INFO_FULL_PATH
:
4019 case OPT_DEBUG_INFO_TARGET_PREFIX
:
4022 case OPT_INPUT_FORMAT
:
4025 case OPT_OUTPUT_FORMAT
:
4028 case OPT_RUN_ARGS_0
:
4029 case OPT_STREAM_INTERSECTION
:
4032 /* Ignore in this pass */
4035 printf_err("Unknown command-line option specified (option code %d)\n",
4044 /* Append current component's name if needed */
4045 ret
= convert_append_name_param(cur_comp_dest
, cur_name
,
4046 cur_name_prefix
, run_args
, all_names
, &source_names
,
4047 &filter_names
, &sink_names
);
4052 /* Check for option parsing error */
4054 printf_err("While parsing command-line options, at option %s: %s\n",
4055 poptBadOption(pc
, 0), poptStrerror(opt
));
4059 poptFreeContext(pc
);
4064 * Second pass: transform the convert-specific options and
4065 * arguments into implicit component instances for the run
4068 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
4069 convert_long_options
, 0);
4071 printf_err("Cannot get popt context\n");
4075 poptReadDefaultConfig(pc
, 0);
4077 while ((opt
= poptGetNextOpt(pc
)) > 0) {
4078 arg
= poptGetOptArg(pc
);
4082 if (trimmer_has_begin
) {
4083 printf("At --begin option: --begin or --timerange option already specified\n %s\n",
4088 trimmer_has_begin
= true;
4089 ret
= append_implicit_component_extra_param(
4090 &implicit_trimmer_args
, "begin", arg
);
4091 implicit_trimmer_args
.exists
= true;
4097 if (trimmer_has_end
) {
4098 printf("At --end option: --end or --timerange option already specified\n %s\n",
4103 trimmer_has_end
= true;
4104 ret
= append_implicit_component_extra_param(
4105 &implicit_trimmer_args
, "end", arg
);
4106 implicit_trimmer_args
.exists
= true;
4116 if (trimmer_has_begin
|| trimmer_has_end
) {
4117 printf("At --timerange option: --begin, --end, or --timerange option already specified\n %s\n",
4122 ret
= split_timerange(arg
, &begin
, &end
);
4124 printf_err("Invalid --timerange option's argument: expecting BEGIN,END or [BEGIN,END]:\n %s\n",
4129 ret
= append_implicit_component_extra_param(
4130 &implicit_trimmer_args
, "begin", begin
);
4131 ret
|= append_implicit_component_extra_param(
4132 &implicit_trimmer_args
, "end", end
);
4133 implicit_trimmer_args
.exists
= true;
4141 case OPT_CLOCK_CYCLES
:
4142 append_implicit_component_param(
4143 &implicit_text_args
, "clock-cycles", "yes");
4144 implicit_text_args
.exists
= true;
4146 case OPT_CLOCK_DATE
:
4147 append_implicit_component_param(
4148 &implicit_text_args
, "clock-date", "yes");
4149 implicit_text_args
.exists
= true;
4151 case OPT_CLOCK_FORCE_CORRELATE
:
4152 append_implicit_component_param(
4153 &implicit_muxer_args
,
4154 "assume-absolute-clock-classes", "yes");
4157 append_implicit_component_param(
4158 &implicit_text_args
, "clock-gmt", "yes");
4159 append_implicit_component_param(
4160 &implicit_trimmer_args
, "gmt", "yes");
4161 implicit_text_args
.exists
= true;
4163 case OPT_CLOCK_OFFSET
:
4164 base_implicit_ctf_input_args
.exists
= true;
4165 append_implicit_component_param(
4166 &base_implicit_ctf_input_args
,
4167 "clock-class-offset-s", arg
);
4169 case OPT_CLOCK_OFFSET_NS
:
4170 base_implicit_ctf_input_args
.exists
= true;
4171 append_implicit_component_param(
4172 &base_implicit_ctf_input_args
,
4173 "clock-class-offset-ns", arg
);
4175 case OPT_CLOCK_SECONDS
:
4176 append_implicit_component_param(
4177 &implicit_text_args
, "clock-seconds", "yes");
4178 implicit_text_args
.exists
= true;
4181 implicit_text_args
.exists
= true;
4182 ret
= append_implicit_component_extra_param(
4183 &implicit_text_args
, "color", arg
);
4188 case OPT_DEBUG_INFO
:
4189 implicit_debug_info_args
.exists
= true;
4191 case OPT_DEBUG_INFO_DIR
:
4192 implicit_debug_info_args
.exists
= true;
4193 ret
= append_implicit_component_extra_param(
4194 &implicit_debug_info_args
, "debug-info-dir", arg
);
4199 case OPT_DEBUG_INFO_FULL_PATH
:
4200 implicit_debug_info_args
.exists
= true;
4201 append_implicit_component_param(
4202 &implicit_debug_info_args
, "full-path", "yes");
4204 case OPT_DEBUG_INFO_TARGET_PREFIX
:
4205 implicit_debug_info_args
.exists
= true;
4206 ret
= append_implicit_component_extra_param(
4207 &implicit_debug_info_args
,
4208 "target-prefix", arg
);
4215 bt_value
*fields
= fields_from_arg(arg
);
4221 implicit_text_args
.exists
= true;
4222 ret
= insert_flat_params_from_array(
4223 implicit_text_args
.params_arg
,
4225 bt_value_put_ref(fields
);
4233 bt_value
*names
= names_from_arg(arg
);
4239 implicit_text_args
.exists
= true;
4240 ret
= insert_flat_params_from_array(
4241 implicit_text_args
.params_arg
,
4243 bt_value_put_ref(names
);
4250 append_implicit_component_param(
4251 &implicit_text_args
, "no-delta", "yes");
4252 implicit_text_args
.exists
= true;
4254 case OPT_INPUT_FORMAT
:
4255 if (got_input_format_opt
) {
4256 printf_err("Duplicate --input-format option\n");
4260 got_input_format_opt
= true;
4262 if (strcmp(arg
, "ctf") == 0) {
4263 base_implicit_ctf_input_args
.exists
= true;
4264 } else if (strcmp(arg
, "lttng-live") == 0) {
4265 implicit_lttng_live_args
.exists
= true;
4267 printf_err("Unknown legacy input format:\n %s\n",
4272 case OPT_OUTPUT_FORMAT
:
4273 if (got_output_format_opt
) {
4274 printf_err("Duplicate --output-format option\n");
4278 got_output_format_opt
= true;
4280 if (strcmp(arg
, "text") == 0) {
4281 implicit_text_args
.exists
= true;
4282 } else if (strcmp(arg
, "ctf") == 0) {
4283 implicit_ctf_output_args
.exists
= true;
4284 } else if (strcmp(arg
, "dummy") == 0) {
4285 implicit_dummy_args
.exists
= true;
4286 } else if (strcmp(arg
, "ctf-metadata") == 0) {
4287 print_ctf_metadata
= true;
4289 printf_err("Unknown legacy output format:\n %s\n",
4296 printf_err("Duplicate --output option\n");
4300 output
= strdup(arg
);
4307 if (print_run_args_0
) {
4308 printf_err("Cannot specify --run-args and --run-args-0\n");
4312 print_run_args
= true;
4314 case OPT_RUN_ARGS_0
:
4315 if (print_run_args
) {
4316 printf_err("Cannot specify --run-args and --run-args-0\n");
4320 print_run_args_0
= true;
4322 case OPT_STREAM_INTERSECTION
:
4324 * Applies to all traces implementing the trace-info
4327 stream_intersection_mode
= true;
4330 if (*log_level
!= 'V' && *log_level
!= 'D') {
4343 /* Check for option parsing error */
4345 printf_err("While parsing command-line options, at option %s: %s\n",
4346 poptBadOption(pc
, 0), poptStrerror(opt
));
4351 * Legacy behaviour: --verbose used to make the `text` output
4352 * format print more information. --verbose is now equivalent to
4353 * the INFO log level, which is why we compare to 'I' here.
4355 if (*log_level
== 'I') {
4356 append_implicit_component_param(&implicit_text_args
,
4361 * Append home and system plugin paths now that we possibly got
4364 if (append_home_and_system_plugin_paths(plugin_paths
,
4365 force_omit_system_plugin_path
,
4366 force_omit_home_plugin_path
)) {
4370 /* Consume and keep leftover arguments */
4371 while ((leftover
= poptGetArg(pc
))) {
4372 GString
*gs_leftover
= g_string_new(leftover
);
4379 leftovers
= g_list_append(leftovers
, gs_leftover
);
4381 g_string_free(gs_leftover
, TRUE
);
4387 /* Print CTF metadata or print LTTng live sessions */
4388 if (print_ctf_metadata
) {
4389 GString
*gs_leftover
;
4391 if (g_list_length(leftovers
) == 0) {
4392 printf_err("--output-format=ctf-metadata specified without a path\n");
4396 if (g_list_length(leftovers
) > 1) {
4397 printf_err("Too many paths specified for --output-format=ctf-metadata\n");
4401 cfg
= bt_config_print_ctf_metadata_create(plugin_paths
);
4406 gs_leftover
= leftovers
->data
;
4407 g_string_assign(cfg
->cmd_data
.print_ctf_metadata
.path
,
4412 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
4420 * If -o ctf was specified, make sure an output path (--output)
4421 * was also specified. --output does not imply -o ctf because
4422 * it's also used for the default, implicit -o text if -o ctf
4425 if (implicit_ctf_output_args
.exists
) {
4427 printf_err("--output-format=ctf specified without --output (trace output path)\n");
4432 * At this point we know that -o ctf AND --output were
4433 * specified. Make sure that no options were specified
4434 * which would imply -o text because --output would be
4435 * ambiguous in this case. For example, this is wrong:
4437 * babeltrace --names=all -o ctf --output=/tmp/path my-trace
4439 * because --names=all implies -o text, and --output
4440 * could apply to both the sink.text.pretty and
4441 * sink.ctf.fs implicit components.
4443 if (implicit_text_args
.exists
) {
4444 printf_err("Ambiguous --output option: --output-format=ctf specified but another option implies --output-format=text\n");
4450 * If -o dummy and -o ctf were not specified, and if there are
4451 * no explicit sink components, then use an implicit
4452 * `sink.text.pretty` component.
4454 if (!implicit_dummy_args
.exists
&& !implicit_ctf_output_args
.exists
&&
4456 implicit_text_args
.exists
= true;
4460 * Set implicit `sink.text.pretty` or `sink.ctf.fs` component's
4461 * `path` parameter if --output was specified.
4464 if (implicit_text_args
.exists
) {
4465 append_implicit_component_extra_param(&implicit_text_args
,
4467 } else if (implicit_ctf_output_args
.exists
) {
4468 append_implicit_component_extra_param(&implicit_ctf_output_args
,
4473 /* Decide where the leftover argument(s) go */
4474 if (g_list_length(leftovers
) > 0) {
4475 if (implicit_lttng_live_args
.exists
) {
4476 GString
*gs_leftover
;
4478 if (g_list_length(leftovers
) > 1) {
4479 printf_err("Too many URLs specified for --output-format=lttng-live\n");
4483 gs_leftover
= leftovers
->data
;
4484 lttng_live_url_parts
=
4485 bt_common_parse_lttng_live_url(gs_leftover
->str
,
4486 error_buf
, sizeof(error_buf
));
4487 if (!lttng_live_url_parts
.proto
) {
4488 printf_err("Invalid LTTng live URL format: %s\n",
4493 if (!lttng_live_url_parts
.session_name
) {
4494 /* Print LTTng live sessions */
4495 cfg
= bt_config_print_lttng_live_sessions_create(
4501 g_string_assign(cfg
->cmd_data
.print_lttng_live_sessions
.url
,
4506 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
4513 ret
= append_implicit_component_extra_param(
4514 &implicit_lttng_live_args
, "url",
4521 * Append one implicit component argument set
4522 * for each leftover (souce.ctf.fs paths). Copy
4523 * the base implicit component arguments.
4524 * Note that they still have to be named later.
4526 ret
= fill_implicit_ctf_inputs_args(
4527 implicit_ctf_inputs_args
,
4528 &base_implicit_ctf_input_args
, leftovers
);
4536 * Ensure mutual exclusion between implicit `source.ctf.fs` and
4537 * `source.ctf.lttng-live` components.
4539 if (base_implicit_ctf_input_args
.exists
&&
4540 implicit_lttng_live_args
.exists
) {
4541 printf_err("Cannot create both implicit `%s` and `%s` components\n",
4542 base_implicit_ctf_input_args
.comp_arg
->str
,
4543 implicit_lttng_live_args
.comp_arg
->str
);
4548 * If the implicit `source.ctf.fs` or `source.ctf.lttng-live`
4549 * components exists, make sure there's at least one leftover
4550 * (which is the path or URL).
4552 if (base_implicit_ctf_input_args
.exists
&&
4553 g_list_length(leftovers
) == 0) {
4554 printf_err("Missing path for implicit `%s` component\n",
4555 base_implicit_ctf_input_args
.comp_arg
->str
);
4559 if (implicit_lttng_live_args
.exists
&& g_list_length(leftovers
) == 0) {
4560 printf_err("Missing URL for implicit `%s` component\n",
4561 implicit_lttng_live_args
.comp_arg
->str
);
4565 /* Assign names to implicit components */
4566 for (i
= 0; i
< implicit_ctf_inputs_args
->len
; i
++) {
4567 struct implicit_component_args
*impl_args
=
4568 g_ptr_array_index(implicit_ctf_inputs_args
, i
);
4570 ret
= assign_name_to_implicit_component(impl_args
,
4571 "source-ctf-fs", all_names
, &source_names
, true);
4577 ret
= assign_name_to_implicit_component(&implicit_lttng_live_args
,
4578 "lttng-live", all_names
, &source_names
, true);
4583 ret
= assign_name_to_implicit_component(&implicit_text_args
,
4584 "pretty", all_names
, &sink_names
, true);
4589 ret
= assign_name_to_implicit_component(&implicit_ctf_output_args
,
4590 "sink-ctf-fs", all_names
, &sink_names
, true);
4595 ret
= assign_name_to_implicit_component(&implicit_dummy_args
,
4596 "dummy", all_names
, &sink_names
, true);
4601 ret
= assign_name_to_implicit_component(&implicit_muxer_args
,
4602 "muxer", all_names
, NULL
, false);
4607 ret
= assign_name_to_implicit_component(&implicit_trimmer_args
,
4608 "trimmer", all_names
, NULL
, false);
4613 ret
= assign_name_to_implicit_component(&implicit_debug_info_args
,
4614 "debug-info", all_names
, NULL
, false);
4619 /* Make sure there's at least one source and one sink */
4620 if (!source_names
) {
4621 printf_err("No source component\n");
4626 printf_err("No sink component\n");
4631 * Prepend the muxer, the trimmer, and the debug info to the
4632 * filter chain so that we have:
4634 * sources -> muxer -> [trimmer] -> [debug info] ->
4635 * [user filters] -> sinks
4637 if (implicit_debug_info_args
.exists
) {
4638 if (g_list_prepend_gstring(&filter_names
,
4639 implicit_debug_info_args
.name_arg
->str
)) {
4644 if (implicit_trimmer_args
.exists
) {
4645 if (g_list_prepend_gstring(&filter_names
,
4646 implicit_trimmer_args
.name_arg
->str
)) {
4651 if (g_list_prepend_gstring(&filter_names
,
4652 implicit_muxer_args
.name_arg
->str
)) {
4657 * Append the equivalent run arguments for the implicit
4660 for (i
= 0; i
< implicit_ctf_inputs_args
->len
; i
++) {
4661 struct implicit_component_args
*impl_args
=
4662 g_ptr_array_index(implicit_ctf_inputs_args
, i
);
4664 ret
= append_run_args_for_implicit_component(impl_args
,
4671 ret
= append_run_args_for_implicit_component(&implicit_lttng_live_args
,
4677 ret
= append_run_args_for_implicit_component(&implicit_text_args
,
4683 ret
= append_run_args_for_implicit_component(&implicit_ctf_output_args
,
4689 ret
= append_run_args_for_implicit_component(&implicit_dummy_args
,
4695 ret
= append_run_args_for_implicit_component(&implicit_muxer_args
,
4701 ret
= append_run_args_for_implicit_component(&implicit_trimmer_args
,
4707 ret
= append_run_args_for_implicit_component(&implicit_debug_info_args
,
4713 /* Auto-connect components */
4714 ret
= convert_auto_connect(run_args
, source_names
, filter_names
,
4717 printf_err("Cannot auto-connect components\n");
4722 * We have all the run command arguments now. Depending on
4723 * --run-args, we pass this to the run command or print them
4726 if (print_run_args
|| print_run_args_0
) {
4727 if (stream_intersection_mode
) {
4728 printf_err("Cannot specify --stream-intersection with --run-args or --run-args-0\n");
4732 for (i
= 0; i
< bt_value_array_get_size(run_args
); i
++) {
4733 const bt_value
*arg_value
=
4734 bt_value_array_borrow_element_by_index(run_args
,
4737 GString
*quoted
= NULL
;
4738 const char *arg_to_print
;
4740 BT_ASSERT(arg_value
);
4741 arg
= bt_value_string_get(arg_value
);
4743 if (print_run_args
) {
4744 quoted
= bt_common_shell_quote(arg
, true);
4749 arg_to_print
= quoted
->str
;
4754 printf("%s", arg_to_print
);
4757 g_string_free(quoted
, TRUE
);
4760 if (i
< bt_value_array_get_size(run_args
) - 1) {
4761 if (print_run_args
) {
4770 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4774 cfg
= bt_config_run_from_args_array(run_args
, retcode
,
4775 force_omit_system_plugin_path
,
4776 force_omit_home_plugin_path
,
4777 initial_plugin_paths
);
4782 cfg
->cmd_data
.run
.stream_intersection_mode
= stream_intersection_mode
;
4787 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4791 poptFreeContext(pc
);
4798 g_string_free(cur_name
, TRUE
);
4801 if (cur_name_prefix
) {
4802 g_string_free(cur_name_prefix
, TRUE
);
4805 if (implicit_ctf_inputs_args
) {
4806 g_ptr_array_free(implicit_ctf_inputs_args
, TRUE
);
4809 bt_value_put_ref(run_args
);
4810 bt_value_put_ref(all_names
);
4811 destroy_glist_of_gstring(source_names
);
4812 destroy_glist_of_gstring(filter_names
);
4813 destroy_glist_of_gstring(sink_names
);
4814 destroy_glist_of_gstring(leftovers
);
4815 finalize_implicit_component_args(&base_implicit_ctf_input_args
);
4816 finalize_implicit_component_args(&implicit_ctf_output_args
);
4817 finalize_implicit_component_args(&implicit_lttng_live_args
);
4818 finalize_implicit_component_args(&implicit_dummy_args
);
4819 finalize_implicit_component_args(&implicit_text_args
);
4820 finalize_implicit_component_args(&implicit_debug_info_args
);
4821 finalize_implicit_component_args(&implicit_muxer_args
);
4822 finalize_implicit_component_args(&implicit_trimmer_args
);
4823 bt_value_put_ref(plugin_paths
);
4824 bt_common_destroy_lttng_live_url_parts(<tng_live_url_parts
);
4829 * Prints the Babeltrace 2.x general usage.
4832 void print_gen_usage(FILE *fp
)
4834 fprintf(fp
, "Usage: babeltrace [GENERAL OPTIONS] [COMMAND] [COMMAND ARGUMENTS]\n");
4836 fprintf(fp
, "General options:\n");
4838 fprintf(fp
, " -d, --debug Enable debug mode (same as --log-level=V)\n");
4839 fprintf(fp
, " -h, --help Show this help and quit\n");
4840 fprintf(fp
, " -l, --log-level=LVL Set all log levels to LVL (`N`, `V`, `D`,\n");
4841 fprintf(fp
, " `I`, `W` (default), `E`, or `F`)\n");
4842 fprintf(fp
, " -v, --verbose Enable verbose mode (same as --log-level=I)\n");
4843 fprintf(fp
, " -V, --version Show version and quit\n");
4845 fprintf(fp
, "Available commands:\n");
4847 fprintf(fp
, " convert Convert and trim traces (default)\n");
4848 fprintf(fp
, " help Get help for a plugin or a component class\n");
4849 fprintf(fp
, " list-plugins List available plugins and their content\n");
4850 fprintf(fp
, " query Query objects from a component class\n");
4851 fprintf(fp
, " run Build a processing graph and run it\n");
4853 fprintf(fp
, "Use `babeltrace COMMAND --help` to show the help of COMMAND.\n");
4857 char log_level_from_arg(const char *arg
)
4861 if (strcmp(arg
, "VERBOSE") == 0 ||
4862 strcmp(arg
, "V") == 0) {
4864 } else if (strcmp(arg
, "DEBUG") == 0 ||
4865 strcmp(arg
, "D") == 0) {
4867 } else if (strcmp(arg
, "INFO") == 0 ||
4868 strcmp(arg
, "I") == 0) {
4870 } else if (strcmp(arg
, "WARN") == 0 ||
4871 strcmp(arg
, "WARNING") == 0 ||
4872 strcmp(arg
, "W") == 0) {
4874 } else if (strcmp(arg
, "ERROR") == 0 ||
4875 strcmp(arg
, "E") == 0) {
4877 } else if (strcmp(arg
, "FATAL") == 0 ||
4878 strcmp(arg
, "F") == 0) {
4880 } else if (strcmp(arg
, "NONE") == 0 ||
4881 strcmp(arg
, "N") == 0) {
4888 struct bt_config
*bt_config_cli_args_create(int argc
, const char *argv
[],
4889 int *retcode
, bool force_omit_system_plugin_path
,
4890 bool force_omit_home_plugin_path
,
4891 const bt_value
*initial_plugin_paths
)
4893 struct bt_config
*config
= NULL
;
4895 const char **command_argv
= NULL
;
4896 int command_argc
= -1;
4897 const char *command_name
= NULL
;
4898 char log_level
= 'U';
4901 COMMAND_TYPE_NONE
= -1,
4902 COMMAND_TYPE_RUN
= 0,
4903 COMMAND_TYPE_CONVERT
,
4904 COMMAND_TYPE_LIST_PLUGINS
,
4907 } command_type
= COMMAND_TYPE_NONE
;
4911 if (!initial_plugin_paths
) {
4912 initial_plugin_paths
= bt_value_array_create();
4913 if (!initial_plugin_paths
) {
4918 bt_value_get_ref(initial_plugin_paths
);
4924 print_gen_usage(stdout
);
4928 for (i
= 1; i
< argc
; i
++) {
4929 const char *cur_arg
= argv
[i
];
4930 const char *next_arg
= i
== (argc
- 1) ? NULL
: argv
[i
+ 1];
4932 if (strcmp(cur_arg
, "-d") == 0 ||
4933 strcmp(cur_arg
, "--debug") == 0) {
4935 } else if (strcmp(cur_arg
, "-v") == 0 ||
4936 strcmp(cur_arg
, "--verbose") == 0) {
4937 if (log_level
!= 'V' && log_level
!= 'D') {
4939 * Legacy: do not override a previous
4940 * --debug because --verbose and --debug
4941 * can be specified together (in this
4942 * case we want the lowest log level to
4947 } else if (strcmp(cur_arg
, "--log-level") == 0 ||
4948 strcmp(cur_arg
, "-l") == 0) {
4950 printf_err("Missing log level value for --log-level option\n");
4955 log_level
= log_level_from_arg(next_arg
);
4956 if (log_level
== 'U') {
4957 printf_err("Invalid argument for --log-level option:\n %s\n",
4964 } else if (strncmp(cur_arg
, "--log-level=", 12) == 0) {
4965 const char *arg
= &cur_arg
[12];
4967 log_level
= log_level_from_arg(arg
);
4968 if (log_level
== 'U') {
4969 printf_err("Invalid argument for --log-level option:\n %s\n",
4974 } else if (strncmp(cur_arg
, "-l", 2) == 0) {
4975 const char *arg
= &cur_arg
[2];
4977 log_level
= log_level_from_arg(arg
);
4978 if (log_level
== 'U') {
4979 printf_err("Invalid argument for --log-level option:\n %s\n",
4984 } else if (strcmp(cur_arg
, "-V") == 0 ||
4985 strcmp(cur_arg
, "--version") == 0) {
4988 } else if (strcmp(cur_arg
, "-h") == 0 ||
4989 strcmp(cur_arg
, "--help") == 0) {
4990 print_gen_usage(stdout
);
4994 * First unknown argument: is it a known command
4997 command_argv
= &argv
[i
];
4998 command_argc
= argc
- i
;
5000 if (strcmp(cur_arg
, "convert") == 0) {
5001 command_type
= COMMAND_TYPE_CONVERT
;
5002 } else if (strcmp(cur_arg
, "list-plugins") == 0) {
5003 command_type
= COMMAND_TYPE_LIST_PLUGINS
;
5004 } else if (strcmp(cur_arg
, "help") == 0) {
5005 command_type
= COMMAND_TYPE_HELP
;
5006 } else if (strcmp(cur_arg
, "query") == 0) {
5007 command_type
= COMMAND_TYPE_QUERY
;
5008 } else if (strcmp(cur_arg
, "run") == 0) {
5009 command_type
= COMMAND_TYPE_RUN
;
5012 * Unknown argument, but not a known
5013 * command name: assume the default
5014 * `convert` command.
5016 command_type
= COMMAND_TYPE_CONVERT
;
5017 command_name
= "convert";
5018 command_argv
= &argv
[i
- 1];
5019 command_argc
= argc
- i
+ 1;
5025 if (command_type
== COMMAND_TYPE_NONE
) {
5027 * We only got non-help, non-version general options
5028 * like --verbose and --debug, without any other
5029 * arguments, so we can't do anything useful: print the
5032 print_gen_usage(stdout
);
5036 BT_ASSERT(command_argv
);
5037 BT_ASSERT(command_argc
>= 0);
5039 switch (command_type
) {
5040 case COMMAND_TYPE_RUN
:
5041 config
= bt_config_run_from_args(command_argc
, command_argv
,
5042 retcode
, force_omit_system_plugin_path
,
5043 force_omit_home_plugin_path
, initial_plugin_paths
);
5045 case COMMAND_TYPE_CONVERT
:
5046 config
= bt_config_convert_from_args(command_argc
, command_argv
,
5047 retcode
, force_omit_system_plugin_path
,
5048 force_omit_home_plugin_path
,
5049 initial_plugin_paths
, &log_level
);
5051 case COMMAND_TYPE_LIST_PLUGINS
:
5052 config
= bt_config_list_plugins_from_args(command_argc
,
5053 command_argv
, retcode
, force_omit_system_plugin_path
,
5054 force_omit_home_plugin_path
, initial_plugin_paths
);
5056 case COMMAND_TYPE_HELP
:
5057 config
= bt_config_help_from_args(command_argc
,
5058 command_argv
, retcode
, force_omit_system_plugin_path
,
5059 force_omit_home_plugin_path
, initial_plugin_paths
);
5061 case COMMAND_TYPE_QUERY
:
5062 config
= bt_config_query_from_args(command_argc
,
5063 command_argv
, retcode
, force_omit_system_plugin_path
,
5064 force_omit_home_plugin_path
, initial_plugin_paths
);
5071 if (log_level
== 'U') {
5075 config
->log_level
= log_level
;
5076 config
->command_name
= command_name
;
5080 bt_value_put_ref(initial_plugin_paths
);