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
,
1404 OPT_OMIT_HOME_PLUGIN_PATH
,
1405 OPT_OMIT_SYSTEM_PLUGIN_PATH
,
1411 OPT_RESET_BASE_PARAMS
,
1415 OPT_STREAM_INTERSECTION
,
1422 enum bt_config_component_dest
{
1423 BT_CONFIG_COMPONENT_DEST_UNKNOWN
= -1,
1424 BT_CONFIG_COMPONENT_DEST_SOURCE
,
1425 BT_CONFIG_COMPONENT_DEST_FILTER
,
1426 BT_CONFIG_COMPONENT_DEST_SINK
,
1430 * Adds a configuration component to the appropriate configuration
1431 * array depending on the destination.
1434 void add_run_cfg_comp(struct bt_config
*cfg
,
1435 struct bt_config_component
*cfg_comp
,
1436 enum bt_config_component_dest dest
)
1438 bt_object_get_ref(cfg_comp
);
1441 case BT_CONFIG_COMPONENT_DEST_SOURCE
:
1442 g_ptr_array_add(cfg
->cmd_data
.run
.sources
, cfg_comp
);
1444 case BT_CONFIG_COMPONENT_DEST_FILTER
:
1445 g_ptr_array_add(cfg
->cmd_data
.run
.filters
, cfg_comp
);
1447 case BT_CONFIG_COMPONENT_DEST_SINK
:
1448 g_ptr_array_add(cfg
->cmd_data
.run
.sinks
, cfg_comp
);
1456 int add_run_cfg_comp_check_name(struct bt_config
*cfg
,
1457 struct bt_config_component
*cfg_comp
,
1458 enum bt_config_component_dest dest
,
1459 bt_value
*instance_names
)
1463 if (cfg_comp
->instance_name
->len
== 0) {
1464 printf_err("Found an unnamed component\n");
1469 if (bt_value_map_has_entry(instance_names
,
1470 cfg_comp
->instance_name
->str
)) {
1471 printf_err("Duplicate component instance name:\n %s\n",
1472 cfg_comp
->instance_name
->str
);
1477 if (bt_value_map_insert_entry(instance_names
,
1478 cfg_comp
->instance_name
->str
, bt_value_null
)) {
1484 add_run_cfg_comp(cfg
, cfg_comp
, dest
);
1491 int append_env_var_plugin_paths(bt_value
*plugin_paths
)
1496 if (bt_common_is_setuid_setgid()) {
1497 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1501 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
1506 ret
= bt_config_append_plugin_paths(plugin_paths
, envvar
);
1510 printf_err("Cannot append plugin paths from BABELTRACE_PLUGIN_PATH\n");
1517 int append_home_and_system_plugin_paths(bt_value
*plugin_paths
,
1518 bool omit_system_plugin_path
, bool omit_home_plugin_path
)
1522 if (!omit_home_plugin_path
) {
1523 if (bt_common_is_setuid_setgid()) {
1524 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1526 char *home_plugin_dir
=
1527 bt_common_get_home_plugin_path();
1529 if (home_plugin_dir
) {
1530 ret
= bt_config_append_plugin_paths(
1531 plugin_paths
, home_plugin_dir
);
1532 free(home_plugin_dir
);
1535 printf_err("Invalid home plugin path\n");
1542 if (!omit_system_plugin_path
) {
1543 if (bt_config_append_plugin_paths(plugin_paths
,
1544 bt_common_get_system_plugin_path())) {
1545 printf_err("Invalid system plugin path\n");
1551 printf_err("Cannot append home and system plugin paths\n");
1556 int append_home_and_system_plugin_paths_cfg(struct bt_config
*cfg
)
1558 return append_home_and_system_plugin_paths(cfg
->plugin_paths
,
1559 cfg
->omit_system_plugin_path
, cfg
->omit_home_plugin_path
);
1563 struct bt_config
*bt_config_base_create(enum bt_config_command command
,
1564 const bt_value
*initial_plugin_paths
,
1567 struct bt_config
*cfg
;
1570 cfg
= g_new0(struct bt_config
, 1);
1576 bt_object_init_shared(&cfg
->base
, bt_config_destroy
);
1577 cfg
->command
= command
;
1578 cfg
->command_needs_plugins
= needs_plugins
;
1580 if (initial_plugin_paths
) {
1581 bt_value
*initial_plugin_paths_copy
;
1583 (void) bt_value_copy(initial_plugin_paths
,
1584 &initial_plugin_paths_copy
);
1585 cfg
->plugin_paths
= initial_plugin_paths_copy
;
1587 cfg
->plugin_paths
= bt_value_array_create();
1588 if (!cfg
->plugin_paths
) {
1597 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1604 struct bt_config
*bt_config_run_create(
1605 const bt_value
*initial_plugin_paths
)
1607 struct bt_config
*cfg
;
1610 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_RUN
,
1611 initial_plugin_paths
, true);
1616 cfg
->cmd_data
.run
.sources
= g_ptr_array_new_with_free_func(
1617 (GDestroyNotify
) bt_object_put_ref
);
1618 if (!cfg
->cmd_data
.run
.sources
) {
1623 cfg
->cmd_data
.run
.filters
= g_ptr_array_new_with_free_func(
1624 (GDestroyNotify
) bt_object_put_ref
);
1625 if (!cfg
->cmd_data
.run
.filters
) {
1630 cfg
->cmd_data
.run
.sinks
= g_ptr_array_new_with_free_func(
1631 (GDestroyNotify
) bt_object_put_ref
);
1632 if (!cfg
->cmd_data
.run
.sinks
) {
1637 cfg
->cmd_data
.run
.connections
= g_ptr_array_new_with_free_func(
1638 (GDestroyNotify
) bt_config_connection_destroy
);
1639 if (!cfg
->cmd_data
.run
.connections
) {
1647 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1654 struct bt_config
*bt_config_list_plugins_create(
1655 const bt_value
*initial_plugin_paths
)
1657 struct bt_config
*cfg
;
1660 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_LIST_PLUGINS
,
1661 initial_plugin_paths
, true);
1669 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1676 struct bt_config
*bt_config_help_create(
1677 const bt_value
*initial_plugin_paths
)
1679 struct bt_config
*cfg
;
1682 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_HELP
,
1683 initial_plugin_paths
, true);
1688 cfg
->cmd_data
.help
.cfg_component
=
1689 bt_config_component_create(-1, NULL
, NULL
);
1690 if (!cfg
->cmd_data
.help
.cfg_component
) {
1697 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1704 struct bt_config
*bt_config_query_create(
1705 const bt_value
*initial_plugin_paths
)
1707 struct bt_config
*cfg
;
1710 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_QUERY
,
1711 initial_plugin_paths
, true);
1716 cfg
->cmd_data
.query
.object
= g_string_new(NULL
);
1717 if (!cfg
->cmd_data
.query
.object
) {
1725 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1732 struct bt_config
*bt_config_print_ctf_metadata_create(
1733 const bt_value
*initial_plugin_paths
)
1735 struct bt_config
*cfg
;
1738 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_PRINT_CTF_METADATA
,
1739 initial_plugin_paths
, true);
1744 cfg
->cmd_data
.print_ctf_metadata
.path
= g_string_new(NULL
);
1745 if (!cfg
->cmd_data
.print_ctf_metadata
.path
) {
1750 cfg
->cmd_data
.print_ctf_metadata
.output_path
= g_string_new(NULL
);
1751 if (!cfg
->cmd_data
.print_ctf_metadata
.output_path
) {
1759 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1766 struct bt_config
*bt_config_print_lttng_live_sessions_create(
1767 const bt_value
*initial_plugin_paths
)
1769 struct bt_config
*cfg
;
1772 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
,
1773 initial_plugin_paths
, true);
1778 cfg
->cmd_data
.print_lttng_live_sessions
.url
= g_string_new(NULL
);
1779 if (!cfg
->cmd_data
.print_lttng_live_sessions
.url
) {
1784 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
=
1786 if (!cfg
->cmd_data
.print_lttng_live_sessions
.output_path
) {
1794 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1801 int bt_config_append_plugin_paths_check_setuid_setgid(
1802 bt_value
*plugin_paths
, const char *arg
)
1806 if (bt_common_is_setuid_setgid()) {
1807 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1811 if (bt_config_append_plugin_paths(plugin_paths
, arg
)) {
1812 printf_err("Invalid --plugin-path option's argument:\n %s\n",
1823 * Prints the expected format for a --params option.
1826 void print_expected_params_format(FILE *fp
)
1828 fprintf(fp
, "Expected format of PARAMS\n");
1829 fprintf(fp
, "-------------------------\n");
1831 fprintf(fp
, " PARAM=VALUE[,PARAM=VALUE]...\n");
1833 fprintf(fp
, "The parameter string is a comma-separated list of PARAM=VALUE assignments,\n");
1834 fprintf(fp
, "where PARAM is the parameter name (C identifier plus the [:.-] characters),\n");
1835 fprintf(fp
, "and VALUE can be one of:\n");
1837 fprintf(fp
, "* `null`, `nul`, `NULL`: null value (no backticks).\n");
1838 fprintf(fp
, "* `true`, `TRUE`, `yes`, `YES`: true boolean value (no backticks).\n");
1839 fprintf(fp
, "* `false`, `FALSE`, `no`, `NO`: false boolean value (no backticks).\n");
1840 fprintf(fp
, "* Binary (`0b` prefix), octal (`0` prefix), decimal, or hexadecimal\n");
1841 fprintf(fp
, " (`0x` prefix) signed 64-bit integer.\n");
1842 fprintf(fp
, "* Double precision floating point number (scientific notation is accepted).\n");
1843 fprintf(fp
, "* Unquoted string with no special characters, and not matching any of\n");
1844 fprintf(fp
, " the null and boolean value symbols above.\n");
1845 fprintf(fp
, "* Double-quoted string (accepts escape characters).\n");
1846 fprintf(fp
, "* Array, formatted as an opening `[`, a list of comma-separated values\n");
1847 fprintf(fp
, " (as described by the current list) and a closing `]`.\n");
1849 fprintf(fp
, "You can put whitespaces allowed around individual `=` and `,` symbols.\n");
1851 fprintf(fp
, "Example:\n");
1853 fprintf(fp
, " many=null, fresh=yes, condition=false, squirrel=-782329,\n");
1854 fprintf(fp
, " observe=3.14, simple=beef, needs-quotes=\"some string\",\n");
1855 fprintf(fp
, " escape.chars-are:allowed=\"this is a \\\" double quote\",\n");
1856 fprintf(fp
, " things=[1, \"2\", 3]\n");
1858 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1859 fprintf(fp
, "babeltrace from a shell.\n");
1864 * Prints the help command usage.
1867 void print_help_usage(FILE *fp
)
1869 fprintf(fp
, "Usage: babeltrace [GENERAL OPTIONS] help [OPTIONS] PLUGIN\n");
1870 fprintf(fp
, " babeltrace [GENERAL OPTIONS] help [OPTIONS] TYPE.PLUGIN.CLS\n");
1872 fprintf(fp
, "Options:\n");
1874 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1875 fprintf(fp
, " (~/.local/lib/babeltrace/plugins)\n");
1876 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1877 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1878 fprintf(fp
, " dynamic plugins can be loaded\n");
1879 fprintf(fp
, " -h, --help Show this help and quit\n");
1881 fprintf(fp
, "See `babeltrace --help` for the list of general options.\n");
1883 fprintf(fp
, "Use `babeltrace list-plugins` to show the list of available plugins.\n");
1887 struct poptOption help_long_options
[] = {
1888 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
1889 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
1890 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
1891 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
1892 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
1893 { NULL
, 0, '\0', NULL
, 0, NULL
, NULL
},
1897 * Creates a Babeltrace config object from the arguments of a help
1900 * *retcode is set to the appropriate exit code to use.
1903 struct bt_config
*bt_config_help_from_args(int argc
, const char *argv
[],
1904 int *retcode
, bool force_omit_system_plugin_path
,
1905 bool force_omit_home_plugin_path
,
1906 const bt_value
*initial_plugin_paths
)
1908 poptContext pc
= NULL
;
1912 struct bt_config
*cfg
= NULL
;
1913 const char *leftover
;
1914 char *plugin_name
= NULL
, *comp_cls_name
= NULL
;
1917 cfg
= bt_config_help_create(initial_plugin_paths
);
1922 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
1923 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
1924 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
1930 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
1931 help_long_options
, 0);
1933 printf_err("Cannot get popt context\n");
1937 poptReadDefaultConfig(pc
, 0);
1939 while ((opt
= poptGetNextOpt(pc
)) > 0) {
1940 arg
= poptGetOptArg(pc
);
1943 case OPT_PLUGIN_PATH
:
1944 if (bt_config_append_plugin_paths_check_setuid_setgid(
1945 cfg
->plugin_paths
, arg
)) {
1949 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
1950 cfg
->omit_system_plugin_path
= true;
1952 case OPT_OMIT_HOME_PLUGIN_PATH
:
1953 cfg
->omit_home_plugin_path
= true;
1956 print_help_usage(stdout
);
1958 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1961 printf_err("Unknown command-line option specified (option code %d)\n",
1970 /* Check for option parsing error */
1972 printf_err("While parsing command-line options, at option %s: %s\n",
1973 poptBadOption(pc
, 0), poptStrerror(opt
));
1977 leftover
= poptGetArg(pc
);
1979 plugin_comp_cls_names(leftover
, NULL
,
1980 &plugin_name
, &comp_cls_name
,
1981 &cfg
->cmd_data
.help
.cfg_component
->type
);
1982 if (plugin_name
&& comp_cls_name
) {
1983 /* Component class help */
1985 cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
1988 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
,
1991 /* Fall back to plugin help */
1993 cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
1997 print_help_usage(stdout
);
1999 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2003 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
2011 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2014 g_free(plugin_name
);
2015 g_free(comp_cls_name
);
2018 poptFreeContext(pc
);
2026 * Prints the help command usage.
2029 void print_query_usage(FILE *fp
)
2031 fprintf(fp
, "Usage: babeltrace [GEN OPTS] query [OPTS] TYPE.PLUGIN.CLS OBJECT\n");
2033 fprintf(fp
, "Options:\n");
2035 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2036 fprintf(fp
, " (~/.local/lib/babeltrace/plugins)\n");
2037 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2038 fprintf(fp
, " -p, --params=PARAMS Set the query parameters to PARAMS\n");
2039 fprintf(fp
, " (see the expected format of PARAMS below)\n");
2040 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2041 fprintf(fp
, " dynamic plugins can be loaded\n");
2042 fprintf(fp
, " -h, --help Show this help and quit\n");
2043 fprintf(fp
, "\n\n");
2044 print_expected_params_format(fp
);
2048 struct poptOption query_long_options
[] = {
2049 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
2050 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
2051 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
2052 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
2053 { "params", 'p', POPT_ARG_STRING
, NULL
, OPT_PARAMS
, NULL
, NULL
},
2054 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
2055 { NULL
, 0, '\0', NULL
, 0, NULL
, NULL
},
2059 * Creates a Babeltrace config object from the arguments of a query
2062 * *retcode is set to the appropriate exit code to use.
2065 struct bt_config
*bt_config_query_from_args(int argc
, const char *argv
[],
2066 int *retcode
, bool force_omit_system_plugin_path
,
2067 bool force_omit_home_plugin_path
,
2068 const bt_value
*initial_plugin_paths
)
2070 poptContext pc
= NULL
;
2074 struct bt_config
*cfg
= NULL
;
2075 const char *leftover
;
2078 params
= bt_value_null
;
2079 bt_value_get_ref(bt_value_null
);
2082 cfg
= bt_config_query_create(initial_plugin_paths
);
2087 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
2088 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
2089 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
2095 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
2096 query_long_options
, 0);
2098 printf_err("Cannot get popt context\n");
2102 poptReadDefaultConfig(pc
, 0);
2104 while ((opt
= poptGetNextOpt(pc
)) > 0) {
2105 arg
= poptGetOptArg(pc
);
2108 case OPT_PLUGIN_PATH
:
2109 if (bt_config_append_plugin_paths_check_setuid_setgid(
2110 cfg
->plugin_paths
, arg
)) {
2114 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
2115 cfg
->omit_system_plugin_path
= true;
2117 case OPT_OMIT_HOME_PLUGIN_PATH
:
2118 cfg
->omit_home_plugin_path
= true;
2122 bt_value_put_ref(params
);
2123 params
= bt_value_from_arg(arg
);
2125 printf_err("Invalid format for --params option's argument:\n %s\n",
2132 print_query_usage(stdout
);
2134 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2137 printf_err("Unknown command-line option specified (option code %d)\n",
2146 /* Check for option parsing error */
2148 printf_err("While parsing command-line options, at option %s: %s\n",
2149 poptBadOption(pc
, 0), poptStrerror(opt
));
2154 * We need exactly two leftover arguments which are the
2155 * mandatory component class specification and query object.
2157 leftover
= poptGetArg(pc
);
2159 cfg
->cmd_data
.query
.cfg_component
=
2160 bt_config_component_from_arg(leftover
);
2161 if (!cfg
->cmd_data
.query
.cfg_component
) {
2162 printf_err("Invalid format for component class specification:\n %s\n",
2168 BT_OBJECT_MOVE_REF(cfg
->cmd_data
.query
.cfg_component
->params
,
2171 print_query_usage(stdout
);
2173 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2177 leftover
= poptGetArg(pc
);
2179 if (strlen(leftover
) == 0) {
2180 printf_err("Invalid empty object\n");
2184 g_string_assign(cfg
->cmd_data
.query
.object
, leftover
);
2186 print_query_usage(stdout
);
2188 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2192 leftover
= poptGetArg(pc
);
2194 printf_err("Unexpected argument: %s\n", leftover
);
2198 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
2206 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2210 poptFreeContext(pc
);
2213 bt_value_put_ref(params
);
2219 * Prints the list-plugins command usage.
2222 void print_list_plugins_usage(FILE *fp
)
2224 fprintf(fp
, "Usage: babeltrace [GENERAL OPTIONS] list-plugins [OPTIONS]\n");
2226 fprintf(fp
, "Options:\n");
2228 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2229 fprintf(fp
, " (~/.local/lib/babeltrace/plugins)\n");
2230 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2231 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2232 fprintf(fp
, " dynamic plugins can be loaded\n");
2233 fprintf(fp
, " -h, --help Show this help and quit\n");
2235 fprintf(fp
, "See `babeltrace --help` for the list of general options.\n");
2237 fprintf(fp
, "Use `babeltrace help` to get help for a specific plugin or component class.\n");
2241 struct poptOption list_plugins_long_options
[] = {
2242 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
2243 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
2244 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
2245 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
2246 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
2247 { NULL
, 0, '\0', NULL
, 0, NULL
, NULL
},
2251 * Creates a Babeltrace config object from the arguments of a
2252 * list-plugins command.
2254 * *retcode is set to the appropriate exit code to use.
2257 struct bt_config
*bt_config_list_plugins_from_args(int argc
, const char *argv
[],
2258 int *retcode
, bool force_omit_system_plugin_path
,
2259 bool force_omit_home_plugin_path
,
2260 const bt_value
*initial_plugin_paths
)
2262 poptContext pc
= NULL
;
2266 struct bt_config
*cfg
= NULL
;
2267 const char *leftover
;
2270 cfg
= bt_config_list_plugins_create(initial_plugin_paths
);
2275 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
2276 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
2277 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
2283 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
2284 list_plugins_long_options
, 0);
2286 printf_err("Cannot get popt context\n");
2290 poptReadDefaultConfig(pc
, 0);
2292 while ((opt
= poptGetNextOpt(pc
)) > 0) {
2293 arg
= poptGetOptArg(pc
);
2296 case OPT_PLUGIN_PATH
:
2297 if (bt_config_append_plugin_paths_check_setuid_setgid(
2298 cfg
->plugin_paths
, arg
)) {
2302 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
2303 cfg
->omit_system_plugin_path
= true;
2305 case OPT_OMIT_HOME_PLUGIN_PATH
:
2306 cfg
->omit_home_plugin_path
= true;
2309 print_list_plugins_usage(stdout
);
2311 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2314 printf_err("Unknown command-line option specified (option code %d)\n",
2323 /* Check for option parsing error */
2325 printf_err("While parsing command-line options, at option %s: %s\n",
2326 poptBadOption(pc
, 0), poptStrerror(opt
));
2330 leftover
= poptGetArg(pc
);
2332 printf_err("Unexpected argument: %s\n", leftover
);
2336 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
2344 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2348 poptFreeContext(pc
);
2356 * Prints the run command usage.
2359 void print_run_usage(FILE *fp
)
2361 fprintf(fp
, "Usage: babeltrace [GENERAL OPTIONS] run [OPTIONS]\n");
2363 fprintf(fp
, "Options:\n");
2365 fprintf(fp
, " -b, --base-params=PARAMS Set PARAMS as the current base parameters\n");
2366 fprintf(fp
, " for all the following components until\n");
2367 fprintf(fp
, " --reset-base-params is encountered\n");
2368 fprintf(fp
, " (see the expected format of PARAMS below)\n");
2369 fprintf(fp
, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2370 fprintf(fp
, " Instantiate the component class CLS of type\n");
2371 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
2372 fprintf(fp
, " in the plugin PLUGIN, add it to the graph,\n");
2373 fprintf(fp
, " and optionally name it NAME (you can also\n");
2374 fprintf(fp
, " specify the name with --name)\n");
2375 fprintf(fp
, " -x, --connect=CONNECTION Connect two created components (see the\n");
2376 fprintf(fp
, " expected format of CONNECTION below)\n");
2377 fprintf(fp
, " --key=KEY Set the current initialization string\n");
2378 fprintf(fp
, " parameter key to KEY (see --value)\n");
2379 fprintf(fp
, " -n, --name=NAME Set the name of the current component\n");
2380 fprintf(fp
, " to NAME (must be unique amongst all the\n");
2381 fprintf(fp
, " names of the created components)\n");
2382 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2383 fprintf(fp
, " (~/.local/lib/babeltrace/plugins)\n");
2384 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2385 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2386 fprintf(fp
, " current component (see the expected format\n");
2387 fprintf(fp
, " of PARAMS below)\n");
2388 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2389 fprintf(fp
, " dynamic plugins can be loaded\n");
2390 fprintf(fp
, " -r, --reset-base-params Reset the current base parameters to an\n");
2391 fprintf(fp
, " empty map\n");
2392 fprintf(fp
, " --retry-duration=DUR When babeltrace(1) needs to retry to run\n");
2393 fprintf(fp
, " the graph later, retry in DUR µs\n");
2394 fprintf(fp
, " (default: 100000)\n");
2395 fprintf(fp
, " --value=VAL Add a string initialization parameter to\n");
2396 fprintf(fp
, " the current component with a name given by\n");
2397 fprintf(fp
, " the last argument of the --key option and a\n");
2398 fprintf(fp
, " value set to VAL\n");
2399 fprintf(fp
, " -h, --help Show this help and quit\n");
2401 fprintf(fp
, "See `babeltrace --help` for the list of general options.\n");
2402 fprintf(fp
, "\n\n");
2403 fprintf(fp
, "Expected format of CONNECTION\n");
2404 fprintf(fp
, "-----------------------------\n");
2406 fprintf(fp
, " UPSTREAM[.UPSTREAM-PORT]:DOWNSTREAM[.DOWNSTREAM-PORT]\n");
2408 fprintf(fp
, "UPSTREAM and DOWNSTREAM are names of the upstream and downstream\n");
2409 fprintf(fp
, "components to connect together. You must escape the following characters\n\n");
2410 fprintf(fp
, "with `\\`: `\\`, `.`, and `:`. You can set the name of the current\n");
2411 fprintf(fp
, "component with the --name option.\n");
2413 fprintf(fp
, "UPSTREAM-PORT and DOWNSTREAM-PORT are optional globbing patterns to\n");
2414 fprintf(fp
, "identify the upstream and downstream ports to use for the connection.\n");
2415 fprintf(fp
, "When the port is not specified, `*` is used.\n");
2417 fprintf(fp
, "When a component named UPSTREAM has an available port which matches the\n");
2418 fprintf(fp
, "UPSTREAM-PORT globbing pattern, it is connected to the first port which\n");
2419 fprintf(fp
, "matches the DOWNSTREAM-PORT globbing pattern of the component named\n");
2420 fprintf(fp
, "DOWNSTREAM.\n");
2422 fprintf(fp
, "The only special character in UPSTREAM-PORT and DOWNSTREAM-PORT is `*`\n");
2423 fprintf(fp
, "which matches anything. You must escape the following characters\n");
2424 fprintf(fp
, "with `\\`: `\\`, `*`, `?`, `[`, `.`, and `:`.\n");
2426 fprintf(fp
, "You can connect a source component to a filter or sink component. You\n");
2427 fprintf(fp
, "can connect a filter component to a sink component.\n");
2429 fprintf(fp
, "Examples:\n");
2431 fprintf(fp
, " my-src:my-sink\n");
2432 fprintf(fp
, " ctf-fs.*stream*:utils-muxer:*\n");
2434 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
2435 fprintf(fp
, "babeltrace from a shell.\n");
2436 fprintf(fp
, "\n\n");
2437 print_expected_params_format(fp
);
2441 * Creates a Babeltrace config object from the arguments of a run
2444 * *retcode is set to the appropriate exit code to use.
2447 struct bt_config
*bt_config_run_from_args(int argc
, const char *argv
[],
2448 int *retcode
, bool force_omit_system_plugin_path
,
2449 bool force_omit_home_plugin_path
,
2450 const bt_value
*initial_plugin_paths
)
2452 poptContext pc
= NULL
;
2454 struct bt_config_component
*cur_cfg_comp
= NULL
;
2455 enum bt_config_component_dest cur_cfg_comp_dest
=
2456 BT_CONFIG_COMPONENT_DEST_UNKNOWN
;
2457 bt_value
*cur_base_params
= NULL
;
2459 struct bt_config
*cfg
= NULL
;
2460 bt_value
*instance_names
= NULL
;
2461 bt_value
*connection_args
= NULL
;
2462 GString
*cur_param_key
= NULL
;
2463 char error_buf
[256] = { 0 };
2464 long retry_duration
= -1;
2465 bt_value_status status
;
2466 struct poptOption run_long_options
[] = {
2467 { "base-params", 'b', POPT_ARG_STRING
, NULL
, OPT_BASE_PARAMS
, NULL
, NULL
},
2468 { "component", 'c', POPT_ARG_STRING
, NULL
, OPT_COMPONENT
, NULL
, NULL
},
2469 { "connect", 'x', POPT_ARG_STRING
, NULL
, OPT_CONNECT
, NULL
, NULL
},
2470 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
2471 { "key", '\0', POPT_ARG_STRING
, NULL
, OPT_KEY
, NULL
, NULL
},
2472 { "name", 'n', POPT_ARG_STRING
, NULL
, OPT_NAME
, NULL
, NULL
},
2473 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
2474 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
2475 { "params", 'p', POPT_ARG_STRING
, NULL
, OPT_PARAMS
, NULL
, NULL
},
2476 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
2477 { "reset-base-params", 'r', POPT_ARG_NONE
, NULL
, OPT_RESET_BASE_PARAMS
, NULL
, NULL
},
2478 { "retry-duration", '\0', POPT_ARG_LONG
, &retry_duration
, OPT_RETRY_DURATION
, NULL
, NULL
},
2479 { "value", '\0', POPT_ARG_STRING
, NULL
, OPT_VALUE
, NULL
, NULL
},
2480 { NULL
, 0, '\0', NULL
, 0, NULL
, NULL
},
2484 cur_param_key
= g_string_new(NULL
);
2485 if (!cur_param_key
) {
2491 print_run_usage(stdout
);
2496 cfg
= bt_config_run_create(initial_plugin_paths
);
2501 cfg
->cmd_data
.run
.retry_duration_us
= 100000;
2502 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
2503 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
2504 cur_base_params
= bt_value_map_create();
2505 if (!cur_base_params
) {
2510 instance_names
= bt_value_map_create();
2511 if (!instance_names
) {
2516 connection_args
= bt_value_array_create();
2517 if (!connection_args
) {
2522 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
2528 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
2529 run_long_options
, 0);
2531 printf_err("Cannot get popt context\n");
2535 poptReadDefaultConfig(pc
, 0);
2537 while ((opt
= poptGetNextOpt(pc
)) > 0) {
2538 arg
= poptGetOptArg(pc
);
2541 case OPT_PLUGIN_PATH
:
2542 if (bt_config_append_plugin_paths_check_setuid_setgid(
2543 cfg
->plugin_paths
, arg
)) {
2547 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
2548 cfg
->omit_system_plugin_path
= true;
2550 case OPT_OMIT_HOME_PLUGIN_PATH
:
2551 cfg
->omit_home_plugin_path
= true;
2555 enum bt_config_component_dest new_dest
;
2558 ret
= add_run_cfg_comp_check_name(cfg
,
2559 cur_cfg_comp
, cur_cfg_comp_dest
,
2561 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2567 cur_cfg_comp
= bt_config_component_from_arg(arg
);
2568 if (!cur_cfg_comp
) {
2569 printf_err("Invalid format for --component option's argument:\n %s\n",
2574 switch (cur_cfg_comp
->type
) {
2575 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2576 new_dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
2578 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2579 new_dest
= BT_CONFIG_COMPONENT_DEST_FILTER
;
2581 case BT_COMPONENT_CLASS_TYPE_SINK
:
2582 new_dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
2588 BT_ASSERT(cur_base_params
);
2589 bt_value_put_ref(cur_cfg_comp
->params
);
2590 status
= bt_value_copy(cur_base_params
,
2591 &cur_cfg_comp
->params
);
2592 if (status
!= BT_VALUE_STATUS_OK
) {
2597 cur_cfg_comp_dest
= new_dest
;
2603 bt_value
*params_to_set
;
2605 if (!cur_cfg_comp
) {
2606 printf_err("Cannot add parameters to unavailable component:\n %s\n",
2611 params
= bt_value_from_arg(arg
);
2613 printf_err("Invalid format for --params option's argument:\n %s\n",
2618 status
= bt_value_map_extend(cur_cfg_comp
->params
,
2619 params
, ¶ms_to_set
);
2620 BT_VALUE_PUT_REF_AND_RESET(params
);
2621 if (status
!= BT_VALUE_STATUS_OK
) {
2622 printf_err("Cannot extend current component parameters with --params option's argument:\n %s\n",
2627 BT_OBJECT_MOVE_REF(cur_cfg_comp
->params
, params_to_set
);
2631 if (strlen(arg
) == 0) {
2632 printf_err("Cannot set an empty string as the current parameter key\n");
2636 g_string_assign(cur_param_key
, arg
);
2639 if (!cur_cfg_comp
) {
2640 printf_err("Cannot set a parameter's value of unavailable component:\n %s\n",
2645 if (cur_param_key
->len
== 0) {
2646 printf_err("--value option specified without preceding --key option:\n %s\n",
2651 if (bt_value_map_insert_string_entry(cur_cfg_comp
->params
,
2652 cur_param_key
->str
, arg
)) {
2658 if (!cur_cfg_comp
) {
2659 printf_err("Cannot set the name of unavailable component:\n %s\n",
2664 g_string_assign(cur_cfg_comp
->instance_name
, arg
);
2666 case OPT_BASE_PARAMS
:
2669 bt_value_from_arg(arg
);
2672 printf_err("Invalid format for --base-params option's argument:\n %s\n",
2677 BT_OBJECT_MOVE_REF(cur_base_params
, params
);
2680 case OPT_RESET_BASE_PARAMS
:
2681 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
2682 cur_base_params
= bt_value_map_create();
2683 if (!cur_base_params
) {
2689 if (bt_value_array_append_string_element(
2690 connection_args
, arg
)) {
2695 case OPT_RETRY_DURATION
:
2696 if (retry_duration
< 0) {
2697 printf_err("--retry-duration option's argument must be positive or 0: %ld\n",
2702 cfg
->cmd_data
.run
.retry_duration_us
=
2703 (uint64_t) retry_duration
;
2706 print_run_usage(stdout
);
2708 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2711 printf_err("Unknown command-line option specified (option code %d)\n",
2720 /* Check for option parsing error */
2722 printf_err("While parsing command-line options, at option %s: %s\n",
2723 poptBadOption(pc
, 0), poptStrerror(opt
));
2727 /* This command does not accept leftover arguments */
2728 if (poptPeekArg(pc
)) {
2729 printf_err("Unexpected argument: %s\n", poptPeekArg(pc
));
2733 /* Add current component */
2735 ret
= add_run_cfg_comp_check_name(cfg
, cur_cfg_comp
,
2736 cur_cfg_comp_dest
, instance_names
);
2737 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2743 if (cfg
->cmd_data
.run
.sources
->len
== 0) {
2744 printf_err("Incomplete graph: no source component\n");
2748 if (cfg
->cmd_data
.run
.sinks
->len
== 0) {
2749 printf_err("Incomplete graph: no sink component\n");
2753 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
2757 ret
= bt_config_cli_args_create_connections(cfg
,
2761 printf_err("Cannot creation connections:\n%s", error_buf
);
2769 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2773 poptFreeContext(pc
);
2776 if (cur_param_key
) {
2777 g_string_free(cur_param_key
, TRUE
);
2781 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2782 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
2783 BT_VALUE_PUT_REF_AND_RESET(instance_names
);
2784 BT_VALUE_PUT_REF_AND_RESET(connection_args
);
2789 struct bt_config
*bt_config_run_from_args_array(const bt_value
*run_args
,
2790 int *retcode
, bool force_omit_system_plugin_path
,
2791 bool force_omit_home_plugin_path
,
2792 const bt_value
*initial_plugin_paths
)
2794 struct bt_config
*cfg
= NULL
;
2797 const size_t argc
= bt_value_array_get_size(run_args
) + 1;
2799 argv
= calloc(argc
, sizeof(*argv
));
2807 len
= bt_value_array_get_size(run_args
);
2809 printf_err("Invalid executable arguments\n");
2812 for (i
= 0; i
< len
; i
++) {
2813 const bt_value
*arg_value
=
2814 bt_value_array_borrow_element_by_index_const(run_args
,
2818 BT_ASSERT(arg_value
);
2819 arg
= bt_value_string_get(arg_value
);
2824 cfg
= bt_config_run_from_args(argc
, argv
, retcode
,
2825 force_omit_system_plugin_path
, force_omit_home_plugin_path
,
2826 initial_plugin_paths
);
2834 * Prints the convert command usage.
2837 void print_convert_usage(FILE *fp
)
2839 fprintf(fp
, "Usage: babeltrace [GENERAL OPTIONS] [convert] [OPTIONS] [PATH/URL]\n");
2841 fprintf(fp
, "Options:\n");
2843 fprintf(fp
, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2844 fprintf(fp
, " Instantiate the component class CLS of type\n");
2845 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
2846 fprintf(fp
, " in the plugin PLUGIN, add it to the\n");
2847 fprintf(fp
, " conversion graph, and optionally name it\n");
2848 fprintf(fp
, " NAME (you can also specify the name with\n");
2849 fprintf(fp
, " --name)\n");
2850 fprintf(fp
, " --name=NAME Set the name of the current component\n");
2851 fprintf(fp
, " to NAME (must be unique amongst all the\n");
2852 fprintf(fp
, " names of the created components)\n");
2853 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2854 fprintf(fp
, " (~/.local/lib/babeltrace/plugins)\n");
2855 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2856 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2857 fprintf(fp
, " current component (see the expected format\n");
2858 fprintf(fp
, " of PARAMS below)\n");
2859 fprintf(fp
, " -P, --path=PATH Set the `path` string parameter of the\n");
2860 fprintf(fp
, " current component to PATH\n");
2861 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2862 fprintf(fp
, " --retry-duration=DUR When babeltrace(1) needs to retry to run\n");
2863 fprintf(fp
, " the graph later, retry in DUR µs\n");
2864 fprintf(fp
, " (default: 100000)\n");
2865 fprintf(fp
, " dynamic plugins can be loaded\n");
2866 fprintf(fp
, " --run-args Print the equivalent arguments for the\n");
2867 fprintf(fp
, " `run` command to the standard output,\n");
2868 fprintf(fp
, " formatted for a shell, and quit\n");
2869 fprintf(fp
, " --run-args-0 Print the equivalent arguments for the\n");
2870 fprintf(fp
, " `run` command to the standard output,\n");
2871 fprintf(fp
, " formatted for `xargs -0`, and quit\n");
2872 fprintf(fp
, " --stream-intersection Only process events when all streams\n");
2873 fprintf(fp
, " are active\n");
2874 fprintf(fp
, " -u, --url=URL Set the `url` string parameter of the\n");
2875 fprintf(fp
, " current component to URL\n");
2876 fprintf(fp
, " -h, --help Show this help and quit\n");
2878 fprintf(fp
, "Implicit `source.ctf.fs` component options:\n");
2880 fprintf(fp
, " --clock-offset=SEC Set clock offset to SEC seconds\n");
2881 fprintf(fp
, " --clock-offset-ns=NS Set clock offset to NS ns\n");
2883 fprintf(fp
, "Implicit `sink.text.pretty` component options:\n");
2885 fprintf(fp
, " --clock-cycles Print timestamps in clock cycles\n");
2886 fprintf(fp
, " --clock-date Print timestamp dates\n");
2887 fprintf(fp
, " --clock-gmt Print and parse timestamps in the GMT\n");
2888 fprintf(fp
, " time zone instead of the local time zone\n");
2889 fprintf(fp
, " --clock-seconds Print the timestamps as `SEC.NS` instead\n");
2890 fprintf(fp
, " of `hh:mm:ss.nnnnnnnnn`\n");
2891 fprintf(fp
, " --color=(never | auto | always)\n");
2892 fprintf(fp
, " Never, automatically, or always emit\n");
2893 fprintf(fp
, " console color codes\n");
2894 fprintf(fp
, " -f, --fields=FIELD[,FIELD]... Print additional fields; FIELD can be:\n");
2895 fprintf(fp
, " `all`, `trace`, `trace:hostname`,\n");
2896 fprintf(fp
, " `trace:domain`, `trace:procname`,\n");
2897 fprintf(fp
, " `trace:vpid`, `loglevel`, `emf`\n");
2898 fprintf(fp
, " -n, --names=NAME[,NAME]... Print field names; NAME can be:\n");
2899 fprintf(fp
, " `payload` (or `arg` or `args`), `none`,\n");
2900 fprintf(fp
, " `all`, `scope`, `header`, `context`\n");
2901 fprintf(fp
, " (or `ctx`)\n");
2902 fprintf(fp
, " --no-delta Do not print time delta between\n");
2903 fprintf(fp
, " consecutive events\n");
2904 fprintf(fp
, " -w, --output=PATH Write output text to PATH instead of\n");
2905 fprintf(fp
, " the standard output\n");
2907 fprintf(fp
, "Implicit `filter.utils.muxer` component options:\n");
2909 fprintf(fp
, " --clock-force-correlate Assume that clocks are inherently\n");
2910 fprintf(fp
, " correlated across traces\n");
2912 fprintf(fp
, "Implicit `filter.utils.trimmer` component options:\n");
2914 fprintf(fp
, " -b, --begin=BEGIN Set the beginning time of the conversion\n");
2915 fprintf(fp
, " time range to BEGIN (see the format of\n");
2916 fprintf(fp
, " BEGIN below)\n");
2917 fprintf(fp
, " -e, --end=END Set the end time of the conversion time\n");
2918 fprintf(fp
, " range to END (see the format of END below)\n");
2919 fprintf(fp
, " -t, --timerange=TIMERANGE Set conversion time range to TIMERANGE:\n");
2920 fprintf(fp
, " BEGIN,END or [BEGIN,END] (literally `[` and\n");
2921 fprintf(fp
, " `]`) (see the format of BEGIN/END below)\n");
2923 fprintf(fp
, "Implicit `filter.lttng-utils.debug-info` component options:\n");
2925 fprintf(fp
, " --debug-info Create an implicit\n");
2926 fprintf(fp
, " `filter.lttng-utils.debug-info` component\n");
2927 fprintf(fp
, " --debug-info-dir=DIR Search for debug info in directory DIR\n");
2928 fprintf(fp
, " instead of `/usr/lib/debug`\n");
2929 fprintf(fp
, " --debug-info-full-path Show full debug info source and\n");
2930 fprintf(fp
, " binary paths instead of just names\n");
2931 fprintf(fp
, " --debug-info-target-prefix=DIR\n");
2932 fprintf(fp
, " Use directory DIR as a prefix when\n");
2933 fprintf(fp
, " looking up executables during debug\n");
2934 fprintf(fp
, " info analysis\n");
2936 fprintf(fp
, "Legacy options that still work:\n");
2938 fprintf(fp
, " -i, --input-format=(ctf | lttng-live)\n");
2939 fprintf(fp
, " `ctf`:\n");
2940 fprintf(fp
, " Create an implicit `source.ctf.fs`\n");
2941 fprintf(fp
, " component\n");
2942 fprintf(fp
, " `lttng-live`:\n");
2943 fprintf(fp
, " Create an implicit `source.ctf.lttng-live`\n");
2944 fprintf(fp
, " component\n");
2945 fprintf(fp
, " -o, --output-format=(text | ctf | dummy | ctf-metadata)\n");
2946 fprintf(fp
, " `text`:\n");
2947 fprintf(fp
, " Create an implicit `sink.text.pretty`\n");
2948 fprintf(fp
, " component\n");
2949 fprintf(fp
, " `ctf`:\n");
2950 fprintf(fp
, " Create an implicit `sink.ctf.fs`\n");
2951 fprintf(fp
, " component\n");
2952 fprintf(fp
, " `dummy`:\n");
2953 fprintf(fp
, " Create an implicit `sink.utils.dummy`\n");
2954 fprintf(fp
, " component\n");
2955 fprintf(fp
, " `ctf-metadata`:\n");
2956 fprintf(fp
, " Query the `source.ctf.fs` component class\n");
2957 fprintf(fp
, " for metadata text and quit\n");
2959 fprintf(fp
, "See `babeltrace --help` for the list of general options.\n");
2960 fprintf(fp
, "\n\n");
2961 fprintf(fp
, "Format of BEGIN and END\n");
2962 fprintf(fp
, "-----------------------\n");
2964 fprintf(fp
, " [YYYY-MM-DD [hh:mm:]]ss[.nnnnnnnnn]\n");
2965 fprintf(fp
, "\n\n");
2966 print_expected_params_format(fp
);
2970 struct poptOption convert_long_options
[] = {
2971 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
2972 { "begin", 'b', POPT_ARG_STRING
, NULL
, OPT_BEGIN
, NULL
, NULL
},
2973 { "clock-cycles", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_CYCLES
, NULL
, NULL
},
2974 { "clock-date", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_DATE
, NULL
, NULL
},
2975 { "clock-force-correlate", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_FORCE_CORRELATE
, NULL
, NULL
},
2976 { "clock-gmt", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_GMT
, NULL
, NULL
},
2977 { "clock-offset", '\0', POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET
, NULL
, NULL
},
2978 { "clock-offset-ns", '\0', POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET_NS
, NULL
, NULL
},
2979 { "clock-seconds", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_SECONDS
, NULL
, NULL
},
2980 { "color", '\0', POPT_ARG_STRING
, NULL
, OPT_COLOR
, NULL
, NULL
},
2981 { "component", 'c', POPT_ARG_STRING
, NULL
, OPT_COMPONENT
, NULL
, NULL
},
2982 { "debug", 'd', POPT_ARG_NONE
, NULL
, OPT_DEBUG
, NULL
, NULL
},
2983 { "debug-info-dir", 0, POPT_ARG_STRING
, NULL
, OPT_DEBUG_INFO_DIR
, NULL
, NULL
},
2984 { "debug-info-full-path", 0, POPT_ARG_NONE
, NULL
, OPT_DEBUG_INFO_FULL_PATH
, NULL
, NULL
},
2985 { "debug-info-target-prefix", 0, POPT_ARG_STRING
, NULL
, OPT_DEBUG_INFO_TARGET_PREFIX
, NULL
, NULL
},
2986 { "end", 'e', POPT_ARG_STRING
, NULL
, OPT_END
, NULL
, NULL
},
2987 { "fields", 'f', POPT_ARG_STRING
, NULL
, OPT_FIELDS
, NULL
, NULL
},
2988 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
2989 { "input-format", 'i', POPT_ARG_STRING
, NULL
, OPT_INPUT_FORMAT
, NULL
, NULL
},
2990 { "name", '\0', POPT_ARG_STRING
, NULL
, OPT_NAME
, NULL
, NULL
},
2991 { "names", 'n', POPT_ARG_STRING
, NULL
, OPT_NAMES
, NULL
, NULL
},
2992 { "debug-info", '\0', POPT_ARG_NONE
, NULL
, OPT_DEBUG_INFO
, NULL
, NULL
},
2993 { "no-delta", '\0', POPT_ARG_NONE
, NULL
, OPT_NO_DELTA
, NULL
, NULL
},
2994 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
2995 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
2996 { "output", 'w', POPT_ARG_STRING
, NULL
, OPT_OUTPUT
, NULL
, NULL
},
2997 { "output-format", 'o', POPT_ARG_STRING
, NULL
, OPT_OUTPUT_FORMAT
, NULL
, NULL
},
2998 { "params", 'p', POPT_ARG_STRING
, NULL
, OPT_PARAMS
, NULL
, NULL
},
2999 { "path", 'P', POPT_ARG_STRING
, NULL
, OPT_PATH
, NULL
, NULL
},
3000 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
3001 { "retry-duration", '\0', POPT_ARG_STRING
, NULL
, OPT_RETRY_DURATION
, NULL
, NULL
},
3002 { "run-args", '\0', POPT_ARG_NONE
, NULL
, OPT_RUN_ARGS
, NULL
, NULL
},
3003 { "run-args-0", '\0', POPT_ARG_NONE
, NULL
, OPT_RUN_ARGS_0
, NULL
, NULL
},
3004 { "stream-intersection", '\0', POPT_ARG_NONE
, NULL
, OPT_STREAM_INTERSECTION
, NULL
, NULL
},
3005 { "timerange", '\0', POPT_ARG_STRING
, NULL
, OPT_TIMERANGE
, NULL
, NULL
},
3006 { "url", 'u', POPT_ARG_STRING
, NULL
, OPT_URL
, NULL
, NULL
},
3007 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
3008 { NULL
, 0, '\0', NULL
, 0, NULL
, NULL
},
3012 GString
*get_component_auto_name(const char *prefix
,
3013 const bt_value
*existing_names
)
3016 GString
*auto_name
= g_string_new(NULL
);
3023 if (!bt_value_map_has_entry(existing_names
, prefix
)) {
3024 g_string_assign(auto_name
, prefix
);
3029 g_string_printf(auto_name
, "%s-%d", prefix
, i
);
3031 } while (bt_value_map_has_entry(existing_names
, auto_name
->str
));
3037 struct implicit_component_args
{
3041 GString
*params_arg
;
3042 bt_value
*extra_params
;
3046 int assign_name_to_implicit_component(struct implicit_component_args
*args
,
3047 const char *prefix
, bt_value
*existing_names
,
3048 GList
**comp_names
, bool append_to_comp_names
)
3051 GString
*name
= NULL
;
3053 if (!args
->exists
) {
3057 name
= get_component_auto_name(prefix
,
3065 g_string_assign(args
->name_arg
, name
->str
);
3067 if (bt_value_map_insert_entry(existing_names
, name
->str
,
3074 if (append_to_comp_names
) {
3075 *comp_names
= g_list_append(*comp_names
, name
);
3081 g_string_free(name
, TRUE
);
3088 int append_run_args_for_implicit_component(
3089 struct implicit_component_args
*impl_args
,
3095 if (!impl_args
->exists
) {
3099 if (bt_value_array_append_string_element(run_args
, "--component")) {
3104 if (bt_value_array_append_string_element(run_args
, impl_args
->comp_arg
->str
)) {
3109 if (bt_value_array_append_string_element(run_args
, "--name")) {
3114 if (bt_value_array_append_string_element(run_args
, impl_args
->name_arg
->str
)) {
3119 if (impl_args
->params_arg
->len
> 0) {
3120 if (bt_value_array_append_string_element(run_args
, "--params")) {
3125 if (bt_value_array_append_string_element(run_args
,
3126 impl_args
->params_arg
->str
)) {
3132 for (i
= 0; i
< bt_value_array_get_size(impl_args
->extra_params
);
3134 const bt_value
*elem
;
3137 elem
= bt_value_array_borrow_element_by_index(impl_args
->extra_params
,
3143 BT_ASSERT(bt_value_is_string(elem
));
3144 arg
= bt_value_string_get(elem
);
3145 ret
= bt_value_array_append_string_element(run_args
, arg
);
3162 void finalize_implicit_component_args(struct implicit_component_args
*args
)
3166 if (args
->comp_arg
) {
3167 g_string_free(args
->comp_arg
, TRUE
);
3170 if (args
->name_arg
) {
3171 g_string_free(args
->name_arg
, TRUE
);
3174 if (args
->params_arg
) {
3175 g_string_free(args
->params_arg
, TRUE
);
3178 bt_value_put_ref(args
->extra_params
);
3182 void destroy_implicit_component_args(void *args
)
3188 finalize_implicit_component_args(args
);
3193 int init_implicit_component_args(struct implicit_component_args
*args
,
3194 const char *comp_arg
, bool exists
)
3198 args
->exists
= exists
;
3199 args
->comp_arg
= g_string_new(comp_arg
);
3200 args
->name_arg
= g_string_new(NULL
);
3201 args
->params_arg
= g_string_new(NULL
);
3202 args
->extra_params
= bt_value_array_create();
3204 if (!args
->comp_arg
|| !args
->name_arg
||
3205 !args
->params_arg
|| !args
->extra_params
) {
3207 finalize_implicit_component_args(args
);
3217 void append_implicit_component_param(struct implicit_component_args
*args
,
3218 const char *key
, const char *value
)
3223 append_param_arg(args
->params_arg
, key
, value
);
3227 int append_implicit_component_extra_param(struct implicit_component_args
*args
,
3228 const char *key
, const char *value
)
3236 if (bt_value_array_append_string_element(args
->extra_params
, "--key")) {
3242 if (bt_value_array_append_string_element(args
->extra_params
, key
)) {
3248 if (bt_value_array_append_string_element(args
->extra_params
, "--value")) {
3254 if (bt_value_array_append_string_element(args
->extra_params
, value
)) {
3265 int convert_append_name_param(enum bt_config_component_dest dest
,
3266 GString
*cur_name
, GString
*cur_name_prefix
,
3268 bt_value
*all_names
,
3269 GList
**source_names
, GList
**filter_names
,
3274 if (cur_name_prefix
->len
> 0) {
3275 /* We're after a --component option */
3276 GString
*name
= NULL
;
3277 bool append_name_opt
= false;
3279 if (cur_name
->len
== 0) {
3281 * No explicit name was provided for the user
3284 name
= get_component_auto_name(cur_name_prefix
->str
,
3286 append_name_opt
= true;
3289 * An explicit name was provided for the user
3292 if (bt_value_map_has_entry(all_names
,
3294 printf_err("Duplicate component instance name:\n %s\n",
3299 name
= g_string_new(cur_name
->str
);
3308 * Remember this name globally, for the uniqueness of
3309 * all component names.
3311 if (bt_value_map_insert_entry(all_names
, name
->str
, bt_value_null
)) {
3317 * Append the --name option if necessary.
3319 if (append_name_opt
) {
3320 if (bt_value_array_append_string_element(run_args
, "--name")) {
3325 if (bt_value_array_append_string_element(run_args
, name
->str
)) {
3332 * Remember this name specifically for the type of the
3333 * component. This is to create connection arguments.
3336 case BT_CONFIG_COMPONENT_DEST_SOURCE
:
3337 *source_names
= g_list_append(*source_names
, name
);
3339 case BT_CONFIG_COMPONENT_DEST_FILTER
:
3340 *filter_names
= g_list_append(*filter_names
, name
);
3342 case BT_CONFIG_COMPONENT_DEST_SINK
:
3343 *sink_names
= g_list_append(*sink_names
, name
);
3349 g_string_assign(cur_name_prefix
, "");
3362 * Escapes `.`, `:`, and `\` of `input` with `\`.
3365 GString
*escape_dot_colon(const char *input
)
3367 GString
*output
= g_string_new(NULL
);
3375 for (ch
= input
; *ch
!= '\0'; ch
++) {
3376 if (*ch
== '\\' || *ch
== '.' || *ch
== ':') {
3377 g_string_append_c(output
, '\\');
3380 g_string_append_c(output
, *ch
);
3388 * Appends a --connect option to a list of arguments. `upstream_name`
3389 * and `downstream_name` are escaped with escape_dot_colon() in this
3393 int append_connect_arg(bt_value
*run_args
,
3394 const char *upstream_name
, const char *downstream_name
)
3397 GString
*e_upstream_name
= escape_dot_colon(upstream_name
);
3398 GString
*e_downstream_name
= escape_dot_colon(downstream_name
);
3399 GString
*arg
= g_string_new(NULL
);
3401 if (!e_upstream_name
|| !e_downstream_name
|| !arg
) {
3407 ret
= bt_value_array_append_string_element(run_args
, "--connect");
3414 g_string_append(arg
, e_upstream_name
->str
);
3415 g_string_append_c(arg
, ':');
3416 g_string_append(arg
, e_downstream_name
->str
);
3417 ret
= bt_value_array_append_string_element(run_args
, arg
->str
);
3426 g_string_free(arg
, TRUE
);
3429 if (e_upstream_name
) {
3430 g_string_free(e_upstream_name
, TRUE
);
3433 if (e_downstream_name
) {
3434 g_string_free(e_downstream_name
, TRUE
);
3441 * Appends the run command's --connect options for the convert command.
3444 int convert_auto_connect(bt_value
*run_args
,
3445 GList
*source_names
, GList
*filter_names
,
3449 GList
*source_at
= source_names
;
3450 GList
*filter_at
= filter_names
;
3452 GList
*sink_at
= sink_names
;
3454 BT_ASSERT(source_names
);
3455 BT_ASSERT(filter_names
);
3456 BT_ASSERT(sink_names
);
3458 /* Connect all sources to the first filter */
3459 for (source_at
= source_names
; source_at
!= NULL
; source_at
= g_list_next(source_at
)) {
3460 GString
*source_name
= source_at
->data
;
3461 GString
*filter_name
= filter_at
->data
;
3463 ret
= append_connect_arg(run_args
, source_name
->str
,
3470 filter_prev
= filter_at
;
3471 filter_at
= g_list_next(filter_at
);
3473 /* Connect remaining filters */
3474 for (; filter_at
!= NULL
; filter_prev
= filter_at
, filter_at
= g_list_next(filter_at
)) {
3475 GString
*filter_name
= filter_at
->data
;
3476 GString
*filter_prev_name
= filter_prev
->data
;
3478 ret
= append_connect_arg(run_args
, filter_prev_name
->str
,
3485 /* Connect last filter to all sinks */
3486 for (sink_at
= sink_names
; sink_at
!= NULL
; sink_at
= g_list_next(sink_at
)) {
3487 GString
*filter_name
= filter_prev
->data
;
3488 GString
*sink_name
= sink_at
->data
;
3490 ret
= append_connect_arg(run_args
, filter_name
->str
,
3507 int split_timerange(const char *arg
, char **begin
, char **end
)
3510 const char *ch
= arg
;
3512 GString
*g_begin
= NULL
;
3513 GString
*g_end
= NULL
;
3521 g_begin
= bt_common_string_until(ch
, "", ",", &end_pos
);
3522 if (!g_begin
|| ch
[end_pos
] != ',' || g_begin
->len
== 0) {
3528 g_end
= bt_common_string_until(ch
, "", "]", &end_pos
);
3529 if (!g_end
|| g_end
->len
== 0) {
3535 *begin
= g_begin
->str
;
3537 g_string_free(g_begin
, FALSE
);
3538 g_string_free(g_end
, FALSE
);
3548 g_string_free(g_begin
, TRUE
);
3552 g_string_free(g_end
, TRUE
);
3559 int g_list_prepend_gstring(GList
**list
, const char *string
)
3562 GString
*gs
= g_string_new(string
);
3571 *list
= g_list_prepend(*list
, gs
);
3578 struct implicit_component_args
*create_implicit_component_args(void)
3580 struct implicit_component_args
*impl_args
=
3581 g_new0(struct implicit_component_args
, 1);
3587 if (init_implicit_component_args(impl_args
, NULL
, true)) {
3588 destroy_implicit_component_args(impl_args
);
3598 int fill_implicit_ctf_inputs_args(GPtrArray
*implicit_ctf_inputs_args
,
3599 struct implicit_component_args
*base_implicit_ctf_input_args
,
3604 bt_value_status status
;
3606 for (leftover
= leftovers
; leftover
!= NULL
;
3607 leftover
= g_list_next(leftover
)) {
3608 GString
*gs_leftover
= leftover
->data
;
3609 struct implicit_component_args
*impl_args
=
3610 create_implicit_component_args();
3617 impl_args
->exists
= true;
3618 g_string_assign(impl_args
->comp_arg
,
3619 base_implicit_ctf_input_args
->comp_arg
->str
);
3620 g_string_assign(impl_args
->params_arg
,
3621 base_implicit_ctf_input_args
->params_arg
->str
);
3624 * We need our own copy of the extra parameters because
3625 * this is where the unique path goes.
3627 BT_VALUE_PUT_REF_AND_RESET(impl_args
->extra_params
);
3628 status
= bt_value_copy(base_implicit_ctf_input_args
->extra_params
,
3629 &impl_args
->extra_params
);
3630 if (status
!= BT_VALUE_STATUS_OK
) {
3632 destroy_implicit_component_args(impl_args
);
3636 /* Append unique path parameter */
3637 ret
= append_implicit_component_extra_param(impl_args
,
3638 "path", gs_leftover
->str
);
3640 destroy_implicit_component_args(impl_args
);
3644 g_ptr_array_add(implicit_ctf_inputs_args
, impl_args
);
3657 * Creates a Babeltrace config object from the arguments of a convert
3660 * *retcode is set to the appropriate exit code to use.
3663 struct bt_config
*bt_config_convert_from_args(int argc
, const char *argv
[],
3664 int *retcode
, bool force_omit_system_plugin_path
,
3665 bool force_omit_home_plugin_path
,
3666 const bt_value
*initial_plugin_paths
, char *log_level
)
3668 poptContext pc
= NULL
;
3670 enum bt_config_component_dest cur_comp_dest
=
3671 BT_CONFIG_COMPONENT_DEST_UNKNOWN
;
3673 struct bt_config
*cfg
= NULL
;
3674 bool got_input_format_opt
= false;
3675 bool got_output_format_opt
= false;
3676 bool trimmer_has_begin
= false;
3677 bool trimmer_has_end
= false;
3678 bool stream_intersection_mode
= false;
3679 GString
*cur_name
= NULL
;
3680 GString
*cur_name_prefix
= NULL
;
3681 const char *leftover
= NULL
;
3682 bool print_run_args
= false;
3683 bool print_run_args_0
= false;
3684 bool print_ctf_metadata
= false;
3685 bt_value
*run_args
= NULL
;
3686 bt_value
*all_names
= NULL
;
3687 GList
*source_names
= NULL
;
3688 GList
*filter_names
= NULL
;
3689 GList
*sink_names
= NULL
;
3690 GList
*leftovers
= NULL
;
3691 GPtrArray
*implicit_ctf_inputs_args
= NULL
;
3692 struct implicit_component_args base_implicit_ctf_input_args
= { 0 };
3693 struct implicit_component_args implicit_ctf_output_args
= { 0 };
3694 struct implicit_component_args implicit_lttng_live_args
= { 0 };
3695 struct implicit_component_args implicit_dummy_args
= { 0 };
3696 struct implicit_component_args implicit_text_args
= { 0 };
3697 struct implicit_component_args implicit_debug_info_args
= { 0 };
3698 struct implicit_component_args implicit_muxer_args
= { 0 };
3699 struct implicit_component_args implicit_trimmer_args
= { 0 };
3700 bt_value
*plugin_paths
;
3701 char error_buf
[256] = { 0 };
3703 struct bt_common_lttng_live_url_parts lttng_live_url_parts
= { 0 };
3704 char *output
= NULL
;
3706 (void) bt_value_copy(initial_plugin_paths
, &plugin_paths
);
3711 print_convert_usage(stdout
);
3716 if (init_implicit_component_args(&base_implicit_ctf_input_args
,
3717 "source.ctf.fs", false)) {
3721 if (init_implicit_component_args(&implicit_ctf_output_args
,
3722 "sink.ctf.fs", false)) {
3726 if (init_implicit_component_args(&implicit_lttng_live_args
,
3727 "source.ctf.lttng-live", false)) {
3731 if (init_implicit_component_args(&implicit_text_args
,
3732 "sink.text.pretty", false)) {
3736 if (init_implicit_component_args(&implicit_dummy_args
,
3737 "sink.utils.dummy", false)) {
3741 if (init_implicit_component_args(&implicit_debug_info_args
,
3742 "filter.lttng-utils.debug-info", false)) {
3746 if (init_implicit_component_args(&implicit_muxer_args
,
3747 "filter.utils.muxer", true)) {
3751 if (init_implicit_component_args(&implicit_trimmer_args
,
3752 "filter.utils.trimmer", false)) {
3756 implicit_ctf_inputs_args
= g_ptr_array_new_with_free_func(
3757 (GDestroyNotify
) destroy_implicit_component_args
);
3758 if (!implicit_ctf_inputs_args
) {
3763 all_names
= bt_value_map_create();
3769 run_args
= bt_value_array_create();
3775 cur_name
= g_string_new(NULL
);
3781 cur_name_prefix
= g_string_new(NULL
);
3782 if (!cur_name_prefix
) {
3787 ret
= append_env_var_plugin_paths(plugin_paths
);
3793 * First pass: collect all arguments which need to be passed
3794 * as is to the run command. This pass can also add --name
3795 * arguments if needed to automatically name unnamed component
3796 * instances. Also it does the following transformations:
3798 * --path=PATH -> --key path --value PATH
3799 * --url=URL -> --key url --value URL
3801 * Also it appends the plugin paths of --plugin-path to
3804 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
3805 convert_long_options
, 0);
3807 printf_err("Cannot get popt context\n");
3811 poptReadDefaultConfig(pc
, 0);
3813 while ((opt
= poptGetNextOpt(pc
)) > 0) {
3815 char *plugin_name
= NULL
;
3816 char *comp_cls_name
= NULL
;
3818 arg
= poptGetOptArg(pc
);
3823 bt_component_class_type type
;
3824 const char *type_prefix
;
3826 /* Append current component's name if needed */
3827 ret
= convert_append_name_param(cur_comp_dest
, cur_name
,
3828 cur_name_prefix
, run_args
, all_names
,
3829 &source_names
, &filter_names
, &sink_names
);
3834 /* Parse the argument */
3835 plugin_comp_cls_names(arg
, &name
, &plugin_name
,
3836 &comp_cls_name
, &type
);
3837 if (!plugin_name
|| !comp_cls_name
) {
3838 printf_err("Invalid format for --component option's argument:\n %s\n",
3844 g_string_assign(cur_name
, name
);
3846 g_string_assign(cur_name
, "");
3850 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
3851 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
3852 type_prefix
= "source";
3854 case BT_COMPONENT_CLASS_TYPE_FILTER
:
3855 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_FILTER
;
3856 type_prefix
= "filter";
3858 case BT_COMPONENT_CLASS_TYPE_SINK
:
3859 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
3860 type_prefix
= "sink";
3866 if (bt_value_array_append_string_element(run_args
,
3872 if (bt_value_array_append_string_element(run_args
, arg
)) {
3877 g_string_assign(cur_name_prefix
, "");
3878 g_string_append_printf(cur_name_prefix
, "%s.%s.%s",
3879 type_prefix
, plugin_name
, comp_cls_name
);
3882 free(comp_cls_name
);
3885 comp_cls_name
= NULL
;
3889 if (cur_name_prefix
->len
== 0) {
3890 printf_err("No current component of which to set parameters:\n %s\n",
3895 if (bt_value_array_append_string_element(run_args
,
3901 if (bt_value_array_append_string_element(run_args
, arg
)) {
3907 if (cur_name_prefix
->len
== 0) {
3908 printf_err("No current component of which to set `path` parameter:\n %s\n",
3913 if (bt_value_array_append_string_element(run_args
, "--key")) {
3918 if (bt_value_array_append_string_element(run_args
, "path")) {
3923 if (bt_value_array_append_string_element(run_args
, "--value")) {
3928 if (bt_value_array_append_string_element(run_args
, arg
)) {
3934 if (cur_name_prefix
->len
== 0) {
3935 printf_err("No current component of which to set `url` parameter:\n %s\n",
3940 if (bt_value_array_append_string_element(run_args
, "--key")) {
3945 if (bt_value_array_append_string_element(run_args
, "url")) {
3950 if (bt_value_array_append_string_element(run_args
, "--value")) {
3955 if (bt_value_array_append_string_element(run_args
, arg
)) {
3961 if (cur_name_prefix
->len
== 0) {
3962 printf_err("No current component to name:\n %s\n",
3967 if (bt_value_array_append_string_element(run_args
, "--name")) {
3972 if (bt_value_array_append_string_element(run_args
, arg
)) {
3977 g_string_assign(cur_name
, arg
);
3979 case OPT_OMIT_HOME_PLUGIN_PATH
:
3980 force_omit_home_plugin_path
= true;
3982 if (bt_value_array_append_string_element(run_args
,
3983 "--omit-home-plugin-path")) {
3988 case OPT_RETRY_DURATION
:
3989 if (bt_value_array_append_string_element(run_args
,
3990 "--retry-duration")) {
3995 if (bt_value_array_append_string_element(run_args
, arg
)) {
4000 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
4001 force_omit_system_plugin_path
= true;
4003 if (bt_value_array_append_string_element(run_args
,
4004 "--omit-system-plugin-path")) {
4009 case OPT_PLUGIN_PATH
:
4010 if (bt_config_append_plugin_paths_check_setuid_setgid(
4011 plugin_paths
, arg
)) {
4015 if (bt_value_array_append_string_element(run_args
,
4021 if (bt_value_array_append_string_element(run_args
, arg
)) {
4027 print_convert_usage(stdout
);
4029 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4032 case OPT_CLOCK_CYCLES
:
4033 case OPT_CLOCK_DATE
:
4034 case OPT_CLOCK_FORCE_CORRELATE
:
4036 case OPT_CLOCK_OFFSET
:
4037 case OPT_CLOCK_OFFSET_NS
:
4038 case OPT_CLOCK_SECONDS
:
4041 case OPT_DEBUG_INFO
:
4042 case OPT_DEBUG_INFO_DIR
:
4043 case OPT_DEBUG_INFO_FULL_PATH
:
4044 case OPT_DEBUG_INFO_TARGET_PREFIX
:
4047 case OPT_INPUT_FORMAT
:
4050 case OPT_OUTPUT_FORMAT
:
4053 case OPT_RUN_ARGS_0
:
4054 case OPT_STREAM_INTERSECTION
:
4057 /* Ignore in this pass */
4060 printf_err("Unknown command-line option specified (option code %d)\n",
4069 /* Append current component's name if needed */
4070 ret
= convert_append_name_param(cur_comp_dest
, cur_name
,
4071 cur_name_prefix
, run_args
, all_names
, &source_names
,
4072 &filter_names
, &sink_names
);
4077 /* Check for option parsing error */
4079 printf_err("While parsing command-line options, at option %s: %s\n",
4080 poptBadOption(pc
, 0), poptStrerror(opt
));
4084 poptFreeContext(pc
);
4089 * Second pass: transform the convert-specific options and
4090 * arguments into implicit component instances for the run
4093 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
4094 convert_long_options
, 0);
4096 printf_err("Cannot get popt context\n");
4100 poptReadDefaultConfig(pc
, 0);
4102 while ((opt
= poptGetNextOpt(pc
)) > 0) {
4103 arg
= poptGetOptArg(pc
);
4107 if (trimmer_has_begin
) {
4108 printf("At --begin option: --begin or --timerange option already specified\n %s\n",
4113 trimmer_has_begin
= true;
4114 ret
= append_implicit_component_extra_param(
4115 &implicit_trimmer_args
, "begin", arg
);
4116 implicit_trimmer_args
.exists
= true;
4122 if (trimmer_has_end
) {
4123 printf("At --end option: --end or --timerange option already specified\n %s\n",
4128 trimmer_has_end
= true;
4129 ret
= append_implicit_component_extra_param(
4130 &implicit_trimmer_args
, "end", arg
);
4131 implicit_trimmer_args
.exists
= true;
4141 if (trimmer_has_begin
|| trimmer_has_end
) {
4142 printf("At --timerange option: --begin, --end, or --timerange option already specified\n %s\n",
4147 ret
= split_timerange(arg
, &begin
, &end
);
4149 printf_err("Invalid --timerange option's argument: expecting BEGIN,END or [BEGIN,END]:\n %s\n",
4154 ret
= append_implicit_component_extra_param(
4155 &implicit_trimmer_args
, "begin", begin
);
4156 ret
|= append_implicit_component_extra_param(
4157 &implicit_trimmer_args
, "end", end
);
4158 implicit_trimmer_args
.exists
= true;
4166 case OPT_CLOCK_CYCLES
:
4167 append_implicit_component_param(
4168 &implicit_text_args
, "clock-cycles", "yes");
4169 implicit_text_args
.exists
= true;
4171 case OPT_CLOCK_DATE
:
4172 append_implicit_component_param(
4173 &implicit_text_args
, "clock-date", "yes");
4174 implicit_text_args
.exists
= true;
4176 case OPT_CLOCK_FORCE_CORRELATE
:
4177 append_implicit_component_param(
4178 &implicit_muxer_args
,
4179 "assume-absolute-clock-classes", "yes");
4182 append_implicit_component_param(
4183 &implicit_text_args
, "clock-gmt", "yes");
4184 append_implicit_component_param(
4185 &implicit_trimmer_args
, "gmt", "yes");
4186 implicit_text_args
.exists
= true;
4188 case OPT_CLOCK_OFFSET
:
4189 base_implicit_ctf_input_args
.exists
= true;
4190 append_implicit_component_param(
4191 &base_implicit_ctf_input_args
,
4192 "clock-class-offset-s", arg
);
4194 case OPT_CLOCK_OFFSET_NS
:
4195 base_implicit_ctf_input_args
.exists
= true;
4196 append_implicit_component_param(
4197 &base_implicit_ctf_input_args
,
4198 "clock-class-offset-ns", arg
);
4200 case OPT_CLOCK_SECONDS
:
4201 append_implicit_component_param(
4202 &implicit_text_args
, "clock-seconds", "yes");
4203 implicit_text_args
.exists
= true;
4206 implicit_text_args
.exists
= true;
4207 ret
= append_implicit_component_extra_param(
4208 &implicit_text_args
, "color", arg
);
4213 case OPT_DEBUG_INFO
:
4214 implicit_debug_info_args
.exists
= true;
4216 case OPT_DEBUG_INFO_DIR
:
4217 implicit_debug_info_args
.exists
= true;
4218 ret
= append_implicit_component_extra_param(
4219 &implicit_debug_info_args
, "debug-info-dir", arg
);
4224 case OPT_DEBUG_INFO_FULL_PATH
:
4225 implicit_debug_info_args
.exists
= true;
4226 append_implicit_component_param(
4227 &implicit_debug_info_args
, "full-path", "yes");
4229 case OPT_DEBUG_INFO_TARGET_PREFIX
:
4230 implicit_debug_info_args
.exists
= true;
4231 ret
= append_implicit_component_extra_param(
4232 &implicit_debug_info_args
,
4233 "target-prefix", arg
);
4240 bt_value
*fields
= fields_from_arg(arg
);
4246 implicit_text_args
.exists
= true;
4247 ret
= insert_flat_params_from_array(
4248 implicit_text_args
.params_arg
,
4250 bt_value_put_ref(fields
);
4258 bt_value
*names
= names_from_arg(arg
);
4264 implicit_text_args
.exists
= true;
4265 ret
= insert_flat_params_from_array(
4266 implicit_text_args
.params_arg
,
4268 bt_value_put_ref(names
);
4275 append_implicit_component_param(
4276 &implicit_text_args
, "no-delta", "yes");
4277 implicit_text_args
.exists
= true;
4279 case OPT_INPUT_FORMAT
:
4280 if (got_input_format_opt
) {
4281 printf_err("Duplicate --input-format option\n");
4285 got_input_format_opt
= true;
4287 if (strcmp(arg
, "ctf") == 0) {
4288 base_implicit_ctf_input_args
.exists
= true;
4289 } else if (strcmp(arg
, "lttng-live") == 0) {
4290 implicit_lttng_live_args
.exists
= true;
4292 printf_err("Unknown legacy input format:\n %s\n",
4297 case OPT_OUTPUT_FORMAT
:
4298 if (got_output_format_opt
) {
4299 printf_err("Duplicate --output-format option\n");
4303 got_output_format_opt
= true;
4305 if (strcmp(arg
, "text") == 0) {
4306 implicit_text_args
.exists
= true;
4307 } else if (strcmp(arg
, "ctf") == 0) {
4308 implicit_ctf_output_args
.exists
= true;
4309 } else if (strcmp(arg
, "dummy") == 0) {
4310 implicit_dummy_args
.exists
= true;
4311 } else if (strcmp(arg
, "ctf-metadata") == 0) {
4312 print_ctf_metadata
= true;
4314 printf_err("Unknown legacy output format:\n %s\n",
4321 printf_err("Duplicate --output option\n");
4325 output
= strdup(arg
);
4332 if (print_run_args_0
) {
4333 printf_err("Cannot specify --run-args and --run-args-0\n");
4337 print_run_args
= true;
4339 case OPT_RUN_ARGS_0
:
4340 if (print_run_args
) {
4341 printf_err("Cannot specify --run-args and --run-args-0\n");
4345 print_run_args_0
= true;
4347 case OPT_STREAM_INTERSECTION
:
4349 * Applies to all traces implementing the trace-info
4352 stream_intersection_mode
= true;
4355 if (*log_level
!= 'V' && *log_level
!= 'D') {
4368 /* Check for option parsing error */
4370 printf_err("While parsing command-line options, at option %s: %s\n",
4371 poptBadOption(pc
, 0), poptStrerror(opt
));
4376 * Legacy behaviour: --verbose used to make the `text` output
4377 * format print more information. --verbose is now equivalent to
4378 * the INFO log level, which is why we compare to 'I' here.
4380 if (*log_level
== 'I') {
4381 append_implicit_component_param(&implicit_text_args
,
4386 * Append home and system plugin paths now that we possibly got
4389 if (append_home_and_system_plugin_paths(plugin_paths
,
4390 force_omit_system_plugin_path
,
4391 force_omit_home_plugin_path
)) {
4395 /* Consume and keep leftover arguments */
4396 while ((leftover
= poptGetArg(pc
))) {
4397 GString
*gs_leftover
= g_string_new(leftover
);
4404 leftovers
= g_list_append(leftovers
, gs_leftover
);
4406 g_string_free(gs_leftover
, TRUE
);
4412 /* Print CTF metadata or print LTTng live sessions */
4413 if (print_ctf_metadata
) {
4414 GString
*gs_leftover
;
4416 if (g_list_length(leftovers
) == 0) {
4417 printf_err("--output-format=ctf-metadata specified without a path\n");
4421 if (g_list_length(leftovers
) > 1) {
4422 printf_err("Too many paths specified for --output-format=ctf-metadata\n");
4426 cfg
= bt_config_print_ctf_metadata_create(plugin_paths
);
4431 gs_leftover
= leftovers
->data
;
4432 g_string_assign(cfg
->cmd_data
.print_ctf_metadata
.path
,
4437 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
4445 * If -o ctf was specified, make sure an output path (--output)
4446 * was also specified. --output does not imply -o ctf because
4447 * it's also used for the default, implicit -o text if -o ctf
4450 if (implicit_ctf_output_args
.exists
) {
4452 printf_err("--output-format=ctf specified without --output (trace output path)\n");
4457 * At this point we know that -o ctf AND --output were
4458 * specified. Make sure that no options were specified
4459 * which would imply -o text because --output would be
4460 * ambiguous in this case. For example, this is wrong:
4462 * babeltrace --names=all -o ctf --output=/tmp/path my-trace
4464 * because --names=all implies -o text, and --output
4465 * could apply to both the sink.text.pretty and
4466 * sink.ctf.fs implicit components.
4468 if (implicit_text_args
.exists
) {
4469 printf_err("Ambiguous --output option: --output-format=ctf specified but another option implies --output-format=text\n");
4475 * If -o dummy and -o ctf were not specified, and if there are
4476 * no explicit sink components, then use an implicit
4477 * `sink.text.pretty` component.
4479 if (!implicit_dummy_args
.exists
&& !implicit_ctf_output_args
.exists
&&
4481 implicit_text_args
.exists
= true;
4485 * Set implicit `sink.text.pretty` or `sink.ctf.fs` component's
4486 * `path` parameter if --output was specified.
4489 if (implicit_text_args
.exists
) {
4490 append_implicit_component_extra_param(&implicit_text_args
,
4492 } else if (implicit_ctf_output_args
.exists
) {
4493 append_implicit_component_extra_param(&implicit_ctf_output_args
,
4498 /* Decide where the leftover argument(s) go */
4499 if (g_list_length(leftovers
) > 0) {
4500 if (implicit_lttng_live_args
.exists
) {
4501 GString
*gs_leftover
;
4503 if (g_list_length(leftovers
) > 1) {
4504 printf_err("Too many URLs specified for --output-format=lttng-live\n");
4508 gs_leftover
= leftovers
->data
;
4509 lttng_live_url_parts
=
4510 bt_common_parse_lttng_live_url(gs_leftover
->str
,
4511 error_buf
, sizeof(error_buf
));
4512 if (!lttng_live_url_parts
.proto
) {
4513 printf_err("Invalid LTTng live URL format: %s\n",
4518 if (!lttng_live_url_parts
.session_name
) {
4519 /* Print LTTng live sessions */
4520 cfg
= bt_config_print_lttng_live_sessions_create(
4526 g_string_assign(cfg
->cmd_data
.print_lttng_live_sessions
.url
,
4531 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
4538 ret
= append_implicit_component_extra_param(
4539 &implicit_lttng_live_args
, "url",
4546 * Append one implicit component argument set
4547 * for each leftover (souce.ctf.fs paths). Copy
4548 * the base implicit component arguments.
4549 * Note that they still have to be named later.
4551 ret
= fill_implicit_ctf_inputs_args(
4552 implicit_ctf_inputs_args
,
4553 &base_implicit_ctf_input_args
, leftovers
);
4561 * Ensure mutual exclusion between implicit `source.ctf.fs` and
4562 * `source.ctf.lttng-live` components.
4564 if (base_implicit_ctf_input_args
.exists
&&
4565 implicit_lttng_live_args
.exists
) {
4566 printf_err("Cannot create both implicit `%s` and `%s` components\n",
4567 base_implicit_ctf_input_args
.comp_arg
->str
,
4568 implicit_lttng_live_args
.comp_arg
->str
);
4573 * If the implicit `source.ctf.fs` or `source.ctf.lttng-live`
4574 * components exists, make sure there's at least one leftover
4575 * (which is the path or URL).
4577 if (base_implicit_ctf_input_args
.exists
&&
4578 g_list_length(leftovers
) == 0) {
4579 printf_err("Missing path for implicit `%s` component\n",
4580 base_implicit_ctf_input_args
.comp_arg
->str
);
4584 if (implicit_lttng_live_args
.exists
&& g_list_length(leftovers
) == 0) {
4585 printf_err("Missing URL for implicit `%s` component\n",
4586 implicit_lttng_live_args
.comp_arg
->str
);
4590 /* Assign names to implicit components */
4591 for (i
= 0; i
< implicit_ctf_inputs_args
->len
; i
++) {
4592 struct implicit_component_args
*impl_args
=
4593 g_ptr_array_index(implicit_ctf_inputs_args
, i
);
4595 ret
= assign_name_to_implicit_component(impl_args
,
4596 "source-ctf-fs", all_names
, &source_names
, true);
4602 ret
= assign_name_to_implicit_component(&implicit_lttng_live_args
,
4603 "lttng-live", all_names
, &source_names
, true);
4608 ret
= assign_name_to_implicit_component(&implicit_text_args
,
4609 "pretty", all_names
, &sink_names
, true);
4614 ret
= assign_name_to_implicit_component(&implicit_ctf_output_args
,
4615 "sink-ctf-fs", all_names
, &sink_names
, true);
4620 ret
= assign_name_to_implicit_component(&implicit_dummy_args
,
4621 "dummy", all_names
, &sink_names
, true);
4626 ret
= assign_name_to_implicit_component(&implicit_muxer_args
,
4627 "muxer", all_names
, NULL
, false);
4632 ret
= assign_name_to_implicit_component(&implicit_trimmer_args
,
4633 "trimmer", all_names
, NULL
, false);
4638 ret
= assign_name_to_implicit_component(&implicit_debug_info_args
,
4639 "debug-info", all_names
, NULL
, false);
4644 /* Make sure there's at least one source and one sink */
4645 if (!source_names
) {
4646 printf_err("No source component\n");
4651 printf_err("No sink component\n");
4656 * Prepend the muxer, the trimmer, and the debug info to the
4657 * filter chain so that we have:
4659 * sources -> muxer -> [trimmer] -> [debug info] ->
4660 * [user filters] -> sinks
4662 if (implicit_debug_info_args
.exists
) {
4663 if (g_list_prepend_gstring(&filter_names
,
4664 implicit_debug_info_args
.name_arg
->str
)) {
4669 if (implicit_trimmer_args
.exists
) {
4670 if (g_list_prepend_gstring(&filter_names
,
4671 implicit_trimmer_args
.name_arg
->str
)) {
4676 if (g_list_prepend_gstring(&filter_names
,
4677 implicit_muxer_args
.name_arg
->str
)) {
4682 * Append the equivalent run arguments for the implicit
4685 for (i
= 0; i
< implicit_ctf_inputs_args
->len
; i
++) {
4686 struct implicit_component_args
*impl_args
=
4687 g_ptr_array_index(implicit_ctf_inputs_args
, i
);
4689 ret
= append_run_args_for_implicit_component(impl_args
,
4696 ret
= append_run_args_for_implicit_component(&implicit_lttng_live_args
,
4702 ret
= append_run_args_for_implicit_component(&implicit_text_args
,
4708 ret
= append_run_args_for_implicit_component(&implicit_ctf_output_args
,
4714 ret
= append_run_args_for_implicit_component(&implicit_dummy_args
,
4720 ret
= append_run_args_for_implicit_component(&implicit_muxer_args
,
4726 ret
= append_run_args_for_implicit_component(&implicit_trimmer_args
,
4732 ret
= append_run_args_for_implicit_component(&implicit_debug_info_args
,
4738 /* Auto-connect components */
4739 ret
= convert_auto_connect(run_args
, source_names
, filter_names
,
4742 printf_err("Cannot auto-connect components\n");
4747 * We have all the run command arguments now. Depending on
4748 * --run-args, we pass this to the run command or print them
4751 if (print_run_args
|| print_run_args_0
) {
4752 if (stream_intersection_mode
) {
4753 printf_err("Cannot specify --stream-intersection with --run-args or --run-args-0\n");
4757 for (i
= 0; i
< bt_value_array_get_size(run_args
); i
++) {
4758 const bt_value
*arg_value
=
4759 bt_value_array_borrow_element_by_index(run_args
,
4762 GString
*quoted
= NULL
;
4763 const char *arg_to_print
;
4765 BT_ASSERT(arg_value
);
4766 arg
= bt_value_string_get(arg_value
);
4768 if (print_run_args
) {
4769 quoted
= bt_common_shell_quote(arg
, true);
4774 arg_to_print
= quoted
->str
;
4779 printf("%s", arg_to_print
);
4782 g_string_free(quoted
, TRUE
);
4785 if (i
< bt_value_array_get_size(run_args
) - 1) {
4786 if (print_run_args
) {
4795 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4799 cfg
= bt_config_run_from_args_array(run_args
, retcode
,
4800 force_omit_system_plugin_path
,
4801 force_omit_home_plugin_path
,
4802 initial_plugin_paths
);
4807 cfg
->cmd_data
.run
.stream_intersection_mode
= stream_intersection_mode
;
4812 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4816 poptFreeContext(pc
);
4823 g_string_free(cur_name
, TRUE
);
4826 if (cur_name_prefix
) {
4827 g_string_free(cur_name_prefix
, TRUE
);
4830 if (implicit_ctf_inputs_args
) {
4831 g_ptr_array_free(implicit_ctf_inputs_args
, TRUE
);
4834 bt_value_put_ref(run_args
);
4835 bt_value_put_ref(all_names
);
4836 destroy_glist_of_gstring(source_names
);
4837 destroy_glist_of_gstring(filter_names
);
4838 destroy_glist_of_gstring(sink_names
);
4839 destroy_glist_of_gstring(leftovers
);
4840 finalize_implicit_component_args(&base_implicit_ctf_input_args
);
4841 finalize_implicit_component_args(&implicit_ctf_output_args
);
4842 finalize_implicit_component_args(&implicit_lttng_live_args
);
4843 finalize_implicit_component_args(&implicit_dummy_args
);
4844 finalize_implicit_component_args(&implicit_text_args
);
4845 finalize_implicit_component_args(&implicit_debug_info_args
);
4846 finalize_implicit_component_args(&implicit_muxer_args
);
4847 finalize_implicit_component_args(&implicit_trimmer_args
);
4848 bt_value_put_ref(plugin_paths
);
4849 bt_common_destroy_lttng_live_url_parts(<tng_live_url_parts
);
4854 * Prints the Babeltrace 2.x general usage.
4857 void print_gen_usage(FILE *fp
)
4859 fprintf(fp
, "Usage: babeltrace [GENERAL OPTIONS] [COMMAND] [COMMAND ARGUMENTS]\n");
4861 fprintf(fp
, "General options:\n");
4863 fprintf(fp
, " -d, --debug Enable debug mode (same as --log-level=V)\n");
4864 fprintf(fp
, " -h, --help Show this help and quit\n");
4865 fprintf(fp
, " -l, --log-level=LVL Set all log levels to LVL (`N`, `V`, `D`,\n");
4866 fprintf(fp
, " `I`, `W` (default), `E`, or `F`)\n");
4867 fprintf(fp
, " -v, --verbose Enable verbose mode (same as --log-level=I)\n");
4868 fprintf(fp
, " -V, --version Show version and quit\n");
4870 fprintf(fp
, "Available commands:\n");
4872 fprintf(fp
, " convert Convert and trim traces (default)\n");
4873 fprintf(fp
, " help Get help for a plugin or a component class\n");
4874 fprintf(fp
, " list-plugins List available plugins and their content\n");
4875 fprintf(fp
, " query Query objects from a component class\n");
4876 fprintf(fp
, " run Build a processing graph and run it\n");
4878 fprintf(fp
, "Use `babeltrace COMMAND --help` to show the help of COMMAND.\n");
4882 char log_level_from_arg(const char *arg
)
4886 if (strcmp(arg
, "VERBOSE") == 0 ||
4887 strcmp(arg
, "V") == 0) {
4889 } else if (strcmp(arg
, "DEBUG") == 0 ||
4890 strcmp(arg
, "D") == 0) {
4892 } else if (strcmp(arg
, "INFO") == 0 ||
4893 strcmp(arg
, "I") == 0) {
4895 } else if (strcmp(arg
, "WARN") == 0 ||
4896 strcmp(arg
, "WARNING") == 0 ||
4897 strcmp(arg
, "W") == 0) {
4899 } else if (strcmp(arg
, "ERROR") == 0 ||
4900 strcmp(arg
, "E") == 0) {
4902 } else if (strcmp(arg
, "FATAL") == 0 ||
4903 strcmp(arg
, "F") == 0) {
4905 } else if (strcmp(arg
, "NONE") == 0 ||
4906 strcmp(arg
, "N") == 0) {
4913 struct bt_config
*bt_config_cli_args_create(int argc
, const char *argv
[],
4914 int *retcode
, bool force_omit_system_plugin_path
,
4915 bool force_omit_home_plugin_path
,
4916 const bt_value
*initial_plugin_paths
)
4918 struct bt_config
*config
= NULL
;
4920 const char **command_argv
= NULL
;
4921 int command_argc
= -1;
4922 const char *command_name
= NULL
;
4923 char log_level
= 'U';
4926 COMMAND_TYPE_NONE
= -1,
4927 COMMAND_TYPE_RUN
= 0,
4928 COMMAND_TYPE_CONVERT
,
4929 COMMAND_TYPE_LIST_PLUGINS
,
4932 } command_type
= COMMAND_TYPE_NONE
;
4936 if (!initial_plugin_paths
) {
4937 initial_plugin_paths
= bt_value_array_create();
4938 if (!initial_plugin_paths
) {
4943 bt_value_get_ref(initial_plugin_paths
);
4949 print_gen_usage(stdout
);
4953 for (i
= 1; i
< argc
; i
++) {
4954 const char *cur_arg
= argv
[i
];
4955 const char *next_arg
= i
== (argc
- 1) ? NULL
: argv
[i
+ 1];
4957 if (strcmp(cur_arg
, "-d") == 0 ||
4958 strcmp(cur_arg
, "--debug") == 0) {
4960 } else if (strcmp(cur_arg
, "-v") == 0 ||
4961 strcmp(cur_arg
, "--verbose") == 0) {
4962 if (log_level
!= 'V' && log_level
!= 'D') {
4964 * Legacy: do not override a previous
4965 * --debug because --verbose and --debug
4966 * can be specified together (in this
4967 * case we want the lowest log level to
4972 } else if (strcmp(cur_arg
, "--log-level") == 0 ||
4973 strcmp(cur_arg
, "-l") == 0) {
4975 printf_err("Missing log level value for --log-level option\n");
4980 log_level
= log_level_from_arg(next_arg
);
4981 if (log_level
== 'U') {
4982 printf_err("Invalid argument for --log-level option:\n %s\n",
4989 } else if (strncmp(cur_arg
, "--log-level=", 12) == 0) {
4990 const char *arg
= &cur_arg
[12];
4992 log_level
= log_level_from_arg(arg
);
4993 if (log_level
== 'U') {
4994 printf_err("Invalid argument for --log-level option:\n %s\n",
4999 } else if (strncmp(cur_arg
, "-l", 2) == 0) {
5000 const char *arg
= &cur_arg
[2];
5002 log_level
= log_level_from_arg(arg
);
5003 if (log_level
== 'U') {
5004 printf_err("Invalid argument for --log-level option:\n %s\n",
5009 } else if (strcmp(cur_arg
, "-V") == 0 ||
5010 strcmp(cur_arg
, "--version") == 0) {
5013 } else if (strcmp(cur_arg
, "-h") == 0 ||
5014 strcmp(cur_arg
, "--help") == 0) {
5015 print_gen_usage(stdout
);
5019 * First unknown argument: is it a known command
5022 command_argv
= &argv
[i
];
5023 command_argc
= argc
- i
;
5025 if (strcmp(cur_arg
, "convert") == 0) {
5026 command_type
= COMMAND_TYPE_CONVERT
;
5027 } else if (strcmp(cur_arg
, "list-plugins") == 0) {
5028 command_type
= COMMAND_TYPE_LIST_PLUGINS
;
5029 } else if (strcmp(cur_arg
, "help") == 0) {
5030 command_type
= COMMAND_TYPE_HELP
;
5031 } else if (strcmp(cur_arg
, "query") == 0) {
5032 command_type
= COMMAND_TYPE_QUERY
;
5033 } else if (strcmp(cur_arg
, "run") == 0) {
5034 command_type
= COMMAND_TYPE_RUN
;
5037 * Unknown argument, but not a known
5038 * command name: assume the default
5039 * `convert` command.
5041 command_type
= COMMAND_TYPE_CONVERT
;
5042 command_name
= "convert";
5043 command_argv
= &argv
[i
- 1];
5044 command_argc
= argc
- i
+ 1;
5050 if (command_type
== COMMAND_TYPE_NONE
) {
5052 * We only got non-help, non-version general options
5053 * like --verbose and --debug, without any other
5054 * arguments, so we can't do anything useful: print the
5057 print_gen_usage(stdout
);
5061 BT_ASSERT(command_argv
);
5062 BT_ASSERT(command_argc
>= 0);
5064 switch (command_type
) {
5065 case COMMAND_TYPE_RUN
:
5066 config
= bt_config_run_from_args(command_argc
, command_argv
,
5067 retcode
, force_omit_system_plugin_path
,
5068 force_omit_home_plugin_path
, initial_plugin_paths
);
5070 case COMMAND_TYPE_CONVERT
:
5071 config
= bt_config_convert_from_args(command_argc
, command_argv
,
5072 retcode
, force_omit_system_plugin_path
,
5073 force_omit_home_plugin_path
,
5074 initial_plugin_paths
, &log_level
);
5076 case COMMAND_TYPE_LIST_PLUGINS
:
5077 config
= bt_config_list_plugins_from_args(command_argc
,
5078 command_argv
, retcode
, force_omit_system_plugin_path
,
5079 force_omit_home_plugin_path
, initial_plugin_paths
);
5081 case COMMAND_TYPE_HELP
:
5082 config
= bt_config_help_from_args(command_argc
,
5083 command_argv
, retcode
, force_omit_system_plugin_path
,
5084 force_omit_home_plugin_path
, initial_plugin_paths
);
5086 case COMMAND_TYPE_QUERY
:
5087 config
= bt_config_query_from_args(command_argc
,
5088 command_argv
, retcode
, force_omit_system_plugin_path
,
5089 force_omit_home_plugin_path
, initial_plugin_paths
);
5096 if (log_level
== 'U') {
5100 config
->log_level
= log_level
;
5101 config
->command_name
= command_name
;
5105 bt_value_put_ref(initial_plugin_paths
);