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 "common/assert.h"
35 #include <babeltrace2/babeltrace.h>
36 #include "common/common.h"
39 #include <sys/types.h>
40 #include "babeltrace2-cfg.h"
41 #include "babeltrace2-cfg-cli-args.h"
42 #include "babeltrace2-cfg-cli-args-connect.h"
43 #include "common/version.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;
61 static const int cli_default_log_level
= BT_LOG_WARN
;
63 /* INI-style parsing FSM states */
64 enum ini_parsing_fsm_state
{
65 /* Expect a map key (identifier) */
68 /* Expect an equal character ('=') */
74 /* Expect a comma character (',') */
78 /* INI-style parsing state variables */
79 struct ini_parsing_state
{
80 /* Lexical scanner (owned by this) */
83 /* Output map value object being filled (owned by this) */
86 /* Next expected FSM state */
87 enum ini_parsing_fsm_state expecting
;
89 /* Last decoded map key (owned by this) */
92 /* Complete INI-style string to parse (not owned by this) */
95 /* Error buffer (not owned by this) */
99 /* Offset option with "is set" boolean */
105 /* Legacy "ctf"/"lttng-live" format options */
106 struct ctf_legacy_opts
{
107 struct offset_opt offset_s
;
108 struct offset_opt offset_ns
;
109 bool stream_intersection
;
112 /* Legacy "text" format options */
113 struct text_legacy_opts
{
115 * output, dbg_info_dir, dbg_info_target_prefix, names,
116 * and fields are owned by this.
119 GString
*dbg_info_dir
;
120 GString
*dbg_info_target_prefix
;
121 const bt_value
*names
;
122 const bt_value
*fields
;
130 bool dbg_info_full_path
;
134 /* Legacy input format format */
135 enum legacy_input_format
{
136 LEGACY_INPUT_FORMAT_NONE
= 0,
137 LEGACY_INPUT_FORMAT_CTF
,
138 LEGACY_INPUT_FORMAT_LTTNG_LIVE
,
141 /* Legacy output format format */
142 enum legacy_output_format
{
143 LEGACY_OUTPUT_FORMAT_NONE
= 0,
144 LEGACY_OUTPUT_FORMAT_TEXT
,
145 LEGACY_OUTPUT_FORMAT_DUMMY
,
149 * Prints the "out of memory" error.
152 void print_err_oom(void)
154 printf_err("Out of memory\n");
158 * Appends an "expecting token" error to the INI-style parsing state's
162 void ini_append_error_expecting(struct ini_parsing_state
*state
,
163 GScanner
*scanner
, const char *expecting
)
168 g_string_append_printf(state
->ini_error
, "Expecting %s:\n", expecting
);
170 /* Only print error if there's one line */
171 if (strchr(state
->arg
, '\n') != NULL
|| strlen(state
->arg
) == 0) {
175 g_string_append_printf(state
->ini_error
, "\n %s\n", state
->arg
);
176 pos
= g_scanner_cur_position(scanner
) + 4;
178 if (!g_scanner_eof(scanner
)) {
182 for (i
= 0; i
< pos
; ++i
) {
183 g_string_append_printf(state
->ini_error
, " ");
186 g_string_append_printf(state
->ini_error
, "^\n\n");
189 /* Parse the next token as an unsigned integer. */
191 bt_value
*ini_parse_uint(struct ini_parsing_state
*state
)
193 bt_value
*value
= NULL
;
194 GTokenType token_type
= g_scanner_get_next_token(state
->scanner
);
196 if (token_type
!= G_TOKEN_INT
) {
197 ini_append_error_expecting(state
, state
->scanner
,
202 value
= bt_value_unsigned_integer_create_init(
203 state
->scanner
->value
.v_int64
);
209 /* Parse the next token as a number and return its negation. */
211 bt_value
*ini_parse_neg_number(struct ini_parsing_state
*state
)
213 bt_value
*value
= NULL
;
214 GTokenType token_type
= g_scanner_get_next_token(state
->scanner
);
216 switch (token_type
) {
219 /* Negative integer */
220 uint64_t int_val
= state
->scanner
->value
.v_int64
;
222 if (int_val
> (((uint64_t) INT64_MAX
) + 1)) {
223 g_string_append_printf(state
->ini_error
,
224 "Integer value -%" PRIu64
" is outside the range of a 64-bit signed integer\n",
227 value
= bt_value_signed_integer_create_init(
228 -((int64_t) int_val
));
234 /* Negative floating point number */
235 value
= bt_value_real_create_init(-state
->scanner
->value
.v_float
);
238 ini_append_error_expecting(state
, state
->scanner
, "value");
245 static bt_value
*ini_parse_value(struct ini_parsing_state
*state
);
248 * Parse the current and following tokens as an array. Arrays are formatted as
249 * an opening `[`, a list of comma-separated values and a closing `]`. For
250 * convenience we support an optional trailing comma, after the last value.
252 * The current token of the parser must be the opening square bracket of the
256 bt_value
*ini_parse_array(struct ini_parsing_state
*state
)
258 /* The [ character must have already been ingested. */
259 BT_ASSERT(g_scanner_cur_token(state
->scanner
) == G_TOKEN_CHAR
);
260 BT_ASSERT(g_scanner_cur_value(state
->scanner
).v_char
== '[');
262 bt_value
*array_value
;
263 GTokenType token_type
;
265 array_value
= bt_value_array_create ();
266 token_type
= g_scanner_get_next_token(state
->scanner
);
268 /* While the current token is not a ]... */
269 while (!(token_type
== G_TOKEN_CHAR
&& g_scanner_cur_value(state
->scanner
).v_char
== ']')) {
270 /* Parse the item... */
271 bt_value
*item_value
;
272 bt_value_status status
;
274 item_value
= ini_parse_value(state
);
279 /* ... and add it to the result array. */
280 status
= bt_value_array_append_element(array_value
, item_value
);
281 BT_VALUE_PUT_REF_AND_RESET(item_value
);
283 if (status
!= BT_VALUE_STATUS_OK
) {
288 * Ingest the token following the value, it should be either a
289 * comma or closing square brace.
291 token_type
= g_scanner_get_next_token(state
->scanner
);
293 if (token_type
== G_TOKEN_CHAR
&& g_scanner_cur_value(state
->scanner
).v_char
== ',') {
295 * Ingest the token following the comma. If it happens
296 * to be a closing square bracket, we'll exit the loop
297 * and we are done (we allow trailing commas).
298 * Otherwise, we are ready for the next ini_parse_value call.
300 token_type
= g_scanner_get_next_token(state
->scanner
);
301 } else if (token_type
!= G_TOKEN_CHAR
|| g_scanner_cur_value(state
->scanner
).v_char
!= ']') {
302 ini_append_error_expecting(state
, state
->scanner
, ", or ]");
310 BT_VALUE_PUT_REF_AND_RESET(array_value
);
317 * Parse the current token (and the following ones if needed) as a value, return
321 bt_value
*ini_parse_value(struct ini_parsing_state
*state
)
323 bt_value
*value
= NULL
;
324 GTokenType token_type
= state
->scanner
->token
;
326 switch (token_type
) {
328 if (state
->scanner
->value
.v_char
== '-') {
329 /* Negative number */
330 value
= ini_parse_neg_number(state
);
331 } else if (state
->scanner
->value
.v_char
== '+') {
332 /* Unsigned integer */
333 value
= ini_parse_uint(state
);
334 } else if (state
->scanner
->value
.v_char
== '[') {
336 value
= ini_parse_array(state
);
338 ini_append_error_expecting(state
, state
->scanner
, "value");
343 /* Positive, signed integer */
344 uint64_t int_val
= state
->scanner
->value
.v_int64
;
346 if (int_val
> INT64_MAX
) {
347 g_string_append_printf(state
->ini_error
,
348 "Integer value %" PRIu64
" is outside the range of a 64-bit signed integer\n",
351 value
= bt_value_signed_integer_create_init(
357 /* Positive floating point number */
358 value
= bt_value_real_create_init(state
->scanner
->value
.v_float
);
362 value
= bt_value_string_create_init(state
->scanner
->value
.v_string
);
364 case G_TOKEN_IDENTIFIER
:
367 * Using symbols would be appropriate here,
368 * but said symbols are allowed as map key,
369 * so it's easier to consider everything an
372 * If one of the known symbols is not
373 * recognized here, then fall back to creating
376 const char *id
= state
->scanner
->value
.v_identifier
;
378 if (!strcmp(id
, "null") || !strcmp(id
, "NULL") ||
379 !strcmp(id
, "nul")) {
380 value
= bt_value_null
;
381 } else if (!strcmp(id
, "true") || !strcmp(id
, "TRUE") ||
382 !strcmp(id
, "yes") ||
383 !strcmp(id
, "YES")) {
384 value
= bt_value_bool_create_init(true);
385 } else if (!strcmp(id
, "false") ||
386 !strcmp(id
, "FALSE") ||
389 value
= bt_value_bool_create_init(false);
391 value
= bt_value_string_create_init(id
);
396 /* Unset value variable will trigger the error */
397 ini_append_error_expecting(state
, state
->scanner
, "value");
405 int ini_handle_state(struct ini_parsing_state
*state
)
408 GTokenType token_type
;
409 bt_value
*value
= NULL
;
411 token_type
= g_scanner_get_next_token(state
->scanner
);
412 if (token_type
== G_TOKEN_EOF
) {
413 if (state
->expecting
!= INI_EXPECT_COMMA
) {
414 switch (state
->expecting
) {
415 case INI_EXPECT_EQUAL
:
416 ini_append_error_expecting(state
,
417 state
->scanner
, "'='");
419 case INI_EXPECT_VALUE
:
420 ini_append_error_expecting(state
,
421 state
->scanner
, "value");
423 case INI_EXPECT_MAP_KEY
:
424 ini_append_error_expecting(state
,
425 state
->scanner
, "unquoted map key");
438 switch (state
->expecting
) {
439 case INI_EXPECT_MAP_KEY
:
440 if (token_type
!= G_TOKEN_IDENTIFIER
) {
441 ini_append_error_expecting(state
, state
->scanner
,
446 free(state
->last_map_key
);
447 state
->last_map_key
=
448 strdup(state
->scanner
->value
.v_identifier
);
449 if (!state
->last_map_key
) {
450 g_string_append(state
->ini_error
,
455 if (bt_value_map_has_entry(state
->params
,
456 state
->last_map_key
)) {
457 g_string_append_printf(state
->ini_error
,
458 "Duplicate parameter key: `%s`\n",
459 state
->last_map_key
);
463 state
->expecting
= INI_EXPECT_EQUAL
;
465 case INI_EXPECT_EQUAL
:
466 if (token_type
!= G_TOKEN_CHAR
) {
467 ini_append_error_expecting(state
,
468 state
->scanner
, "'='");
472 if (state
->scanner
->value
.v_char
!= '=') {
473 ini_append_error_expecting(state
,
474 state
->scanner
, "'='");
478 state
->expecting
= INI_EXPECT_VALUE
;
480 case INI_EXPECT_VALUE
:
482 value
= ini_parse_value(state
);
487 state
->expecting
= INI_EXPECT_COMMA
;
490 case INI_EXPECT_COMMA
:
491 if (token_type
!= G_TOKEN_CHAR
) {
492 ini_append_error_expecting(state
,
493 state
->scanner
, "','");
497 if (state
->scanner
->value
.v_char
!= ',') {
498 ini_append_error_expecting(state
,
499 state
->scanner
, "','");
503 state
->expecting
= INI_EXPECT_MAP_KEY
;
515 if (bt_value_map_insert_entry(state
->params
,
516 state
->last_map_key
, value
)) {
517 /* Only override return value on error */
523 BT_VALUE_PUT_REF_AND_RESET(value
);
528 * Converts an INI-style argument to an equivalent map value object.
530 * Return value is owned by the caller.
533 bt_value
*bt_value_from_ini(const char *arg
,
536 /* Lexical scanner configuration */
537 GScannerConfig scanner_config
= {
538 /* Skip whitespaces */
539 .cset_skip_characters
= " \t\n",
541 /* Identifier syntax is: [a-zA-Z_][a-zA-Z0-9_.:-]* */
542 .cset_identifier_first
=
546 .cset_identifier_nth
=
551 /* "hello" and "Hello" two different keys */
552 .case_sensitive
= TRUE
,
555 .cpair_comment_single
= NULL
,
556 .skip_comment_multi
= TRUE
,
557 .skip_comment_single
= TRUE
,
558 .scan_comment_multi
= FALSE
,
561 * Do scan identifiers, including 1-char identifiers,
562 * but NULL is a normal identifier.
564 .scan_identifier
= TRUE
,
565 .scan_identifier_1char
= TRUE
,
566 .scan_identifier_NULL
= FALSE
,
569 * No specific symbols: null and boolean "symbols" are
570 * scanned as plain identifiers.
572 .scan_symbols
= FALSE
,
573 .symbol_2_token
= FALSE
,
574 .scope_0_fallback
= FALSE
,
577 * Scan "0b"-, "0"-, and "0x"-prefixed integers, but not
578 * integers prefixed with "$".
584 .scan_hex_dollar
= FALSE
,
586 /* Convert scanned numbers to integer tokens */
587 .numbers_2_int
= TRUE
,
589 /* Support both integers and floating-point numbers */
590 .int_2_float
= FALSE
,
592 /* Scan integers as 64-bit signed integers */
595 /* Only scan double-quoted strings */
596 .scan_string_sq
= FALSE
,
597 .scan_string_dq
= TRUE
,
599 /* Do not converter identifiers to string tokens */
600 .identifier_2_string
= FALSE
,
602 /* Scan characters as G_TOKEN_CHAR token */
603 .char_2_token
= FALSE
,
605 struct ini_parsing_state state
= {
608 .expecting
= INI_EXPECT_MAP_KEY
,
610 .ini_error
= ini_error
,
613 state
.params
= bt_value_map_create();
618 state
.scanner
= g_scanner_new(&scanner_config
);
619 if (!state
.scanner
) {
623 /* Let the scan begin */
624 g_scanner_input_text(state
.scanner
, arg
, strlen(arg
));
627 int ret
= ini_handle_state(&state
);
632 } else if (ret
> 0) {
641 BT_VALUE_PUT_REF_AND_RESET(state
.params
);
645 g_scanner_destroy(state
.scanner
);
648 free(state
.last_map_key
);
653 * Returns the parameters map value object from a command-line
654 * parameter option's argument.
656 * Return value is owned by the caller.
659 bt_value
*bt_value_from_arg(const char *arg
)
661 bt_value
*params
= NULL
;
662 GString
*ini_error
= NULL
;
664 ini_error
= g_string_new(NULL
);
670 /* Try INI-style parsing */
671 params
= bt_value_from_ini(arg
, ini_error
);
673 printf_err("%s", ini_error
->str
);
679 g_string_free(ini_error
, TRUE
);
686 * Returns the plugin name, component class name, component class type,
687 * and component name from a command-line --component option's argument.
688 * arg must have the following format:
690 * [NAME:]TYPE.PLUGIN.CLS
692 * where NAME is the optional component name, TYPE is either `source`,
693 * `filter`, or `sink`, PLUGIN is the plugin name, and CLS is the
694 * component class name.
696 * On success, both *plugin and *component are not NULL. *plugin
697 * and *comp_cls are owned by the caller. On success, *name can be NULL
698 * if no component class name was found, and *comp_cls_type is set.
701 void plugin_comp_cls_names(const char *arg
, char **name
, char **plugin
,
702 char **comp_cls
, bt_component_class_type
*comp_cls_type
)
704 const char *at
= arg
;
705 GString
*gs_name
= NULL
;
706 GString
*gs_comp_cls_type
= NULL
;
707 GString
*gs_plugin
= NULL
;
708 GString
*gs_comp_cls
= NULL
;
714 BT_ASSERT(comp_cls_type
);
716 if (!bt_common_string_is_printable(arg
)) {
717 printf_err("Argument contains a non-printable character\n");
721 /* Parse the component name */
722 gs_name
= bt_common_string_until(at
, ".:\\", ":", &end_pos
);
727 if (arg
[end_pos
] == ':') {
731 g_string_assign(gs_name
, "");
734 /* Parse the component class type */
735 gs_comp_cls_type
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
736 if (!gs_comp_cls_type
|| at
[end_pos
] == '\0') {
737 printf_err("Missing component class type (`source`, `filter`, or `sink`)\n");
741 if (strcmp(gs_comp_cls_type
->str
, "source") == 0 ||
742 strcmp(gs_comp_cls_type
->str
, "src") == 0) {
743 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_SOURCE
;
744 } else if (strcmp(gs_comp_cls_type
->str
, "filter") == 0 ||
745 strcmp(gs_comp_cls_type
->str
, "flt") == 0) {
746 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_FILTER
;
747 } else if (strcmp(gs_comp_cls_type
->str
, "sink") == 0) {
748 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_SINK
;
750 printf_err("Unknown component class type: `%s`\n",
751 gs_comp_cls_type
->str
);
757 /* Parse the plugin name */
758 gs_plugin
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
759 if (!gs_plugin
|| gs_plugin
->len
== 0 || at
[end_pos
] == '\0') {
760 printf_err("Missing plugin or component class name\n");
766 /* Parse the component class name */
767 gs_comp_cls
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
768 if (!gs_comp_cls
|| gs_comp_cls
->len
== 0) {
769 printf_err("Missing component class name\n");
773 if (at
[end_pos
] != '\0') {
774 /* Found a non-escaped `.` */
779 if (gs_name
->len
== 0) {
781 g_string_free(gs_name
, TRUE
);
783 *name
= gs_name
->str
;
784 g_string_free(gs_name
, FALSE
);
787 g_string_free(gs_name
, TRUE
);
790 *plugin
= gs_plugin
->str
;
791 *comp_cls
= gs_comp_cls
->str
;
792 g_string_free(gs_plugin
, FALSE
);
793 g_string_free(gs_comp_cls
, FALSE
);
809 g_string_free(gs_name
, TRUE
);
813 g_string_free(gs_plugin
, TRUE
);
817 g_string_free(gs_comp_cls
, TRUE
);
820 if (gs_comp_cls_type
) {
821 g_string_free(gs_comp_cls_type
, TRUE
);
828 * Prints the Babeltrace version.
831 void print_version(void)
833 if (GIT_VERSION
[0] == '\0') {
834 puts("Babeltrace " VERSION
);
836 puts("Babeltrace " VERSION
" - " GIT_VERSION
);
841 * Destroys a component configuration.
844 void bt_config_component_destroy(bt_object
*obj
)
846 struct bt_config_component
*bt_config_component
=
847 container_of(obj
, struct bt_config_component
, base
);
853 if (bt_config_component
->plugin_name
) {
854 g_string_free(bt_config_component
->plugin_name
, TRUE
);
857 if (bt_config_component
->comp_cls_name
) {
858 g_string_free(bt_config_component
->comp_cls_name
, TRUE
);
861 if (bt_config_component
->instance_name
) {
862 g_string_free(bt_config_component
->instance_name
, TRUE
);
865 BT_VALUE_PUT_REF_AND_RESET(bt_config_component
->params
);
866 g_free(bt_config_component
);
873 * Creates a component configuration using the given plugin name and
874 * component name. `plugin_name` and `comp_cls_name` are copied (belong
875 * to the return value).
877 * Return value is owned by the caller.
880 struct bt_config_component
*bt_config_component_create(
881 bt_component_class_type type
,
882 const char *plugin_name
, const char *comp_cls_name
,
885 struct bt_config_component
*cfg_component
= NULL
;
887 cfg_component
= g_new0(struct bt_config_component
, 1);
888 if (!cfg_component
) {
893 bt_object_init_shared(&cfg_component
->base
,
894 bt_config_component_destroy
);
895 cfg_component
->type
= type
;
896 cfg_component
->plugin_name
= g_string_new(plugin_name
);
897 if (!cfg_component
->plugin_name
) {
902 cfg_component
->comp_cls_name
= g_string_new(comp_cls_name
);
903 if (!cfg_component
->comp_cls_name
) {
908 cfg_component
->instance_name
= g_string_new(NULL
);
909 if (!cfg_component
->instance_name
) {
914 cfg_component
->log_level
= init_log_level
;
916 /* Start with empty parameters */
917 cfg_component
->params
= bt_value_map_create();
918 if (!cfg_component
->params
) {
926 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
929 return cfg_component
;
933 * Creates a component configuration from a command-line --component
937 struct bt_config_component
*bt_config_component_from_arg(const char *arg
,
940 struct bt_config_component
*cfg_comp
= NULL
;
942 char *plugin_name
= NULL
;
943 char *comp_cls_name
= NULL
;
944 bt_component_class_type type
;
946 plugin_comp_cls_names(arg
, &name
, &plugin_name
, &comp_cls_name
, &type
);
947 if (!plugin_name
|| !comp_cls_name
) {
951 cfg_comp
= bt_config_component_create(type
, plugin_name
, comp_cls_name
,
958 g_string_assign(cfg_comp
->instance_name
, name
);
964 BT_OBJECT_PUT_REF_AND_RESET(cfg_comp
);
969 g_free(comp_cls_name
);
974 * Destroys a configuration.
977 void bt_config_destroy(bt_object
*obj
)
979 struct bt_config
*cfg
=
980 container_of(obj
, struct bt_config
, base
);
986 BT_VALUE_PUT_REF_AND_RESET(cfg
->plugin_paths
);
988 switch (cfg
->command
) {
989 case BT_CONFIG_COMMAND_RUN
:
990 if (cfg
->cmd_data
.run
.sources
) {
991 g_ptr_array_free(cfg
->cmd_data
.run
.sources
, TRUE
);
994 if (cfg
->cmd_data
.run
.filters
) {
995 g_ptr_array_free(cfg
->cmd_data
.run
.filters
, TRUE
);
998 if (cfg
->cmd_data
.run
.sinks
) {
999 g_ptr_array_free(cfg
->cmd_data
.run
.sinks
, TRUE
);
1002 if (cfg
->cmd_data
.run
.connections
) {
1003 g_ptr_array_free(cfg
->cmd_data
.run
.connections
,
1007 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
1009 case BT_CONFIG_COMMAND_HELP
:
1010 BT_OBJECT_PUT_REF_AND_RESET(cfg
->cmd_data
.help
.cfg_component
);
1012 case BT_CONFIG_COMMAND_QUERY
:
1013 BT_OBJECT_PUT_REF_AND_RESET(cfg
->cmd_data
.query
.cfg_component
);
1015 if (cfg
->cmd_data
.query
.object
) {
1016 g_string_free(cfg
->cmd_data
.query
.object
, TRUE
);
1019 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
1020 if (cfg
->cmd_data
.print_ctf_metadata
.path
) {
1021 g_string_free(cfg
->cmd_data
.print_ctf_metadata
.path
,
1024 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
1028 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
1029 if (cfg
->cmd_data
.print_lttng_live_sessions
.url
) {
1031 cfg
->cmd_data
.print_lttng_live_sessions
.url
,
1034 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
1049 void destroy_glist_of_gstring(GList
*list
)
1057 for (at
= list
; at
!= NULL
; at
= g_list_next(at
)) {
1058 g_string_free(at
->data
, TRUE
);
1065 * Creates a simple lexical scanner for parsing comma-delimited names
1068 * Return value is owned by the caller.
1071 GScanner
*create_csv_identifiers_scanner(void)
1074 GScannerConfig scanner_config
= {
1075 .cset_skip_characters
= " \t\n",
1076 .cset_identifier_first
= G_CSET_a_2_z G_CSET_A_2_Z
"_",
1077 .cset_identifier_nth
= G_CSET_a_2_z G_CSET_A_2_Z
":_-",
1078 .case_sensitive
= TRUE
,
1079 .cpair_comment_single
= NULL
,
1080 .skip_comment_multi
= TRUE
,
1081 .skip_comment_single
= TRUE
,
1082 .scan_comment_multi
= FALSE
,
1083 .scan_identifier
= TRUE
,
1084 .scan_identifier_1char
= TRUE
,
1085 .scan_identifier_NULL
= FALSE
,
1086 .scan_symbols
= FALSE
,
1087 .symbol_2_token
= FALSE
,
1088 .scope_0_fallback
= FALSE
,
1089 .scan_binary
= FALSE
,
1090 .scan_octal
= FALSE
,
1091 .scan_float
= FALSE
,
1093 .scan_hex_dollar
= FALSE
,
1094 .numbers_2_int
= FALSE
,
1095 .int_2_float
= FALSE
,
1096 .store_int64
= FALSE
,
1097 .scan_string_sq
= FALSE
,
1098 .scan_string_dq
= FALSE
,
1099 .identifier_2_string
= FALSE
,
1100 .char_2_token
= TRUE
,
1103 scanner
= g_scanner_new(&scanner_config
);
1112 * Converts a comma-delimited list of known names (--names option) to
1113 * an array value object containing those names as string value objects.
1115 * Return value is owned by the caller.
1118 bt_value
*names_from_arg(const char *arg
)
1120 GScanner
*scanner
= NULL
;
1121 bt_value
*names
= NULL
;
1122 bool found_all
= false, found_none
= false, found_item
= false;
1124 names
= bt_value_array_create();
1130 scanner
= create_csv_identifiers_scanner();
1135 g_scanner_input_text(scanner
, arg
, strlen(arg
));
1138 GTokenType token_type
= g_scanner_get_next_token(scanner
);
1140 switch (token_type
) {
1141 case G_TOKEN_IDENTIFIER
:
1143 const char *identifier
= scanner
->value
.v_identifier
;
1145 if (!strcmp(identifier
, "payload") ||
1146 !strcmp(identifier
, "args") ||
1147 !strcmp(identifier
, "arg")) {
1149 if (bt_value_array_append_string_element(names
,
1153 } else if (!strcmp(identifier
, "context") ||
1154 !strcmp(identifier
, "ctx")) {
1156 if (bt_value_array_append_string_element(names
,
1160 } else if (!strcmp(identifier
, "scope") ||
1161 !strcmp(identifier
, "header")) {
1163 if (bt_value_array_append_string_element(names
,
1167 } else if (!strcmp(identifier
, "all")) {
1169 if (bt_value_array_append_string_element(names
,
1173 } else if (!strcmp(identifier
, "none")) {
1175 if (bt_value_array_append_string_element(names
,
1180 printf_err("Unknown name: `%s`\n",
1196 if (found_none
&& found_all
) {
1197 printf_err("Only either `all` or `none` can be specified in the list given to the --names option, but not both.\n");
1201 * Legacy behavior is to clear the defaults (show none) when at
1202 * least one item is specified.
1204 if (found_item
&& !found_none
&& !found_all
) {
1205 if (bt_value_array_append_string_element(names
, "none")) {
1210 g_scanner_destroy(scanner
);
1215 BT_VALUE_PUT_REF_AND_RESET(names
);
1217 g_scanner_destroy(scanner
);
1223 * Converts a comma-delimited list of known fields (--fields option) to
1224 * an array value object containing those fields as string
1227 * Return value is owned by the caller.
1230 bt_value
*fields_from_arg(const char *arg
)
1232 GScanner
*scanner
= NULL
;
1235 fields
= bt_value_array_create();
1241 scanner
= create_csv_identifiers_scanner();
1246 g_scanner_input_text(scanner
, arg
, strlen(arg
));
1249 GTokenType token_type
= g_scanner_get_next_token(scanner
);
1251 switch (token_type
) {
1252 case G_TOKEN_IDENTIFIER
:
1254 const char *identifier
= scanner
->value
.v_identifier
;
1256 if (!strcmp(identifier
, "trace") ||
1257 !strcmp(identifier
, "trace:hostname") ||
1258 !strcmp(identifier
, "trace:domain") ||
1259 !strcmp(identifier
, "trace:procname") ||
1260 !strcmp(identifier
, "trace:vpid") ||
1261 !strcmp(identifier
, "loglevel") ||
1262 !strcmp(identifier
, "emf") ||
1263 !strcmp(identifier
, "callsite") ||
1264 !strcmp(identifier
, "all")) {
1265 if (bt_value_array_append_string_element(fields
,
1270 printf_err("Unknown field: `%s`\n",
1288 BT_VALUE_PUT_REF_AND_RESET(fields
);
1292 g_scanner_destroy(scanner
);
1298 void append_param_arg(GString
*params_arg
, const char *key
, const char *value
)
1300 BT_ASSERT(params_arg
);
1304 if (params_arg
->len
!= 0) {
1305 g_string_append_c(params_arg
, ',');
1308 g_string_append(params_arg
, key
);
1309 g_string_append_c(params_arg
, '=');
1310 g_string_append(params_arg
, value
);
1314 * Inserts the equivalent "prefix-NAME=yes" strings into params_arg
1315 * where the names are in names_array.
1318 int insert_flat_params_from_array(GString
*params_arg
,
1319 const bt_value
*names_array
, const char *prefix
)
1323 GString
*tmpstr
= NULL
, *default_value
= NULL
;
1324 bool default_set
= false, non_default_set
= false;
1327 * names_array may be NULL if no CLI options were specified to
1328 * trigger its creation.
1334 tmpstr
= g_string_new(NULL
);
1341 default_value
= g_string_new(NULL
);
1342 if (!default_value
) {
1348 for (i
= 0; i
< bt_value_array_get_size(names_array
); i
++) {
1349 const bt_value
*str_obj
=
1350 bt_value_array_borrow_element_by_index_const(names_array
,
1353 bool is_default
= false;
1356 printf_err("Unexpected error\n");
1361 suffix
= bt_value_string_get(str_obj
);
1363 g_string_assign(tmpstr
, prefix
);
1364 g_string_append(tmpstr
, "-");
1366 /* Special-case for "all" and "none". */
1367 if (!strcmp(suffix
, "all")) {
1369 g_string_assign(default_value
, "show");
1370 } else if (!strcmp(suffix
, "none")) {
1372 g_string_assign(default_value
, "hide");
1376 g_string_append(tmpstr
, "default");
1377 append_param_arg(params_arg
, tmpstr
->str
,
1378 default_value
->str
);
1380 non_default_set
= true;
1381 g_string_append(tmpstr
, suffix
);
1382 append_param_arg(params_arg
, tmpstr
->str
, "yes");
1386 /* Implicit field-default=hide if any non-default option is set. */
1387 if (non_default_set
&& !default_set
) {
1388 g_string_assign(tmpstr
, prefix
);
1389 g_string_append(tmpstr
, "-default");
1390 g_string_assign(default_value
, "hide");
1391 append_param_arg(params_arg
, tmpstr
->str
, default_value
->str
);
1395 if (default_value
) {
1396 g_string_free(default_value
, TRUE
);
1400 g_string_free(tmpstr
, TRUE
);
1413 OPT_CLOCK_FORCE_CORRELATE
,
1416 OPT_CLOCK_OFFSET_NS
,
1424 OPT_DEBUG_INFO_FULL_PATH
,
1425 OPT_DEBUG_INFO_TARGET_PREFIX
,
1435 OPT_OMIT_HOME_PLUGIN_PATH
,
1436 OPT_OMIT_SYSTEM_PLUGIN_PATH
,
1442 OPT_RESET_BASE_PARAMS
,
1446 OPT_STREAM_INTERSECTION
,
1452 enum bt_config_component_dest
{
1453 BT_CONFIG_COMPONENT_DEST_UNKNOWN
= -1,
1454 BT_CONFIG_COMPONENT_DEST_SOURCE
,
1455 BT_CONFIG_COMPONENT_DEST_FILTER
,
1456 BT_CONFIG_COMPONENT_DEST_SINK
,
1460 * Adds a configuration component to the appropriate configuration
1461 * array depending on the destination.
1464 void add_run_cfg_comp(struct bt_config
*cfg
,
1465 struct bt_config_component
*cfg_comp
,
1466 enum bt_config_component_dest dest
)
1468 bt_object_get_ref(cfg_comp
);
1471 case BT_CONFIG_COMPONENT_DEST_SOURCE
:
1472 g_ptr_array_add(cfg
->cmd_data
.run
.sources
, cfg_comp
);
1474 case BT_CONFIG_COMPONENT_DEST_FILTER
:
1475 g_ptr_array_add(cfg
->cmd_data
.run
.filters
, cfg_comp
);
1477 case BT_CONFIG_COMPONENT_DEST_SINK
:
1478 g_ptr_array_add(cfg
->cmd_data
.run
.sinks
, cfg_comp
);
1486 int add_run_cfg_comp_check_name(struct bt_config
*cfg
,
1487 struct bt_config_component
*cfg_comp
,
1488 enum bt_config_component_dest dest
,
1489 bt_value
*instance_names
)
1493 if (cfg_comp
->instance_name
->len
== 0) {
1494 printf_err("Found an unnamed component\n");
1499 if (bt_value_map_has_entry(instance_names
,
1500 cfg_comp
->instance_name
->str
)) {
1501 printf_err("Duplicate component instance name:\n %s\n",
1502 cfg_comp
->instance_name
->str
);
1507 if (bt_value_map_insert_entry(instance_names
,
1508 cfg_comp
->instance_name
->str
, bt_value_null
)) {
1514 add_run_cfg_comp(cfg
, cfg_comp
, dest
);
1521 int append_env_var_plugin_paths(bt_value
*plugin_paths
)
1526 if (bt_common_is_setuid_setgid()) {
1527 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1531 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
1536 ret
= bt_config_append_plugin_paths(plugin_paths
, envvar
);
1540 printf_err("Cannot append plugin paths from BABELTRACE_PLUGIN_PATH\n");
1547 int append_home_and_system_plugin_paths(bt_value
*plugin_paths
,
1548 bool omit_system_plugin_path
, bool omit_home_plugin_path
)
1552 if (!omit_home_plugin_path
) {
1553 if (bt_common_is_setuid_setgid()) {
1554 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1556 char *home_plugin_dir
= bt_common_get_home_plugin_path(
1557 BT_LOG_OUTPUT_LEVEL
);
1559 if (home_plugin_dir
) {
1560 ret
= bt_config_append_plugin_paths(
1561 plugin_paths
, home_plugin_dir
);
1562 free(home_plugin_dir
);
1565 printf_err("Invalid home plugin path\n");
1572 if (!omit_system_plugin_path
) {
1573 if (bt_config_append_plugin_paths(plugin_paths
,
1574 bt_common_get_system_plugin_path())) {
1575 printf_err("Invalid system plugin path\n");
1581 printf_err("Cannot append home and system plugin paths\n");
1586 int append_home_and_system_plugin_paths_cfg(struct bt_config
*cfg
)
1588 return append_home_and_system_plugin_paths(cfg
->plugin_paths
,
1589 cfg
->omit_system_plugin_path
, cfg
->omit_home_plugin_path
);
1593 struct bt_config
*bt_config_base_create(enum bt_config_command command
,
1594 const bt_value
*initial_plugin_paths
,
1597 struct bt_config
*cfg
;
1600 cfg
= g_new0(struct bt_config
, 1);
1606 bt_object_init_shared(&cfg
->base
, bt_config_destroy
);
1607 cfg
->command
= command
;
1608 cfg
->command_needs_plugins
= needs_plugins
;
1610 if (initial_plugin_paths
) {
1611 bt_value
*initial_plugin_paths_copy
;
1613 (void) bt_value_copy(initial_plugin_paths
,
1614 &initial_plugin_paths_copy
);
1615 cfg
->plugin_paths
= initial_plugin_paths_copy
;
1617 cfg
->plugin_paths
= bt_value_array_create();
1618 if (!cfg
->plugin_paths
) {
1627 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1634 struct bt_config
*bt_config_run_create(
1635 const bt_value
*initial_plugin_paths
)
1637 struct bt_config
*cfg
;
1640 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_RUN
,
1641 initial_plugin_paths
, true);
1646 cfg
->cmd_data
.run
.sources
= g_ptr_array_new_with_free_func(
1647 (GDestroyNotify
) bt_object_put_ref
);
1648 if (!cfg
->cmd_data
.run
.sources
) {
1653 cfg
->cmd_data
.run
.filters
= g_ptr_array_new_with_free_func(
1654 (GDestroyNotify
) bt_object_put_ref
);
1655 if (!cfg
->cmd_data
.run
.filters
) {
1660 cfg
->cmd_data
.run
.sinks
= g_ptr_array_new_with_free_func(
1661 (GDestroyNotify
) bt_object_put_ref
);
1662 if (!cfg
->cmd_data
.run
.sinks
) {
1667 cfg
->cmd_data
.run
.connections
= g_ptr_array_new_with_free_func(
1668 (GDestroyNotify
) bt_config_connection_destroy
);
1669 if (!cfg
->cmd_data
.run
.connections
) {
1677 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1684 struct bt_config
*bt_config_list_plugins_create(
1685 const bt_value
*initial_plugin_paths
)
1687 struct bt_config
*cfg
;
1690 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_LIST_PLUGINS
,
1691 initial_plugin_paths
, true);
1699 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1706 struct bt_config
*bt_config_help_create(
1707 const bt_value
*initial_plugin_paths
,
1708 int default_log_level
)
1710 struct bt_config
*cfg
;
1713 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_HELP
,
1714 initial_plugin_paths
, true);
1719 cfg
->cmd_data
.help
.cfg_component
=
1720 bt_config_component_create(-1, NULL
, NULL
, default_log_level
);
1721 if (!cfg
->cmd_data
.help
.cfg_component
) {
1728 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1735 struct bt_config
*bt_config_query_create(
1736 const bt_value
*initial_plugin_paths
)
1738 struct bt_config
*cfg
;
1741 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_QUERY
,
1742 initial_plugin_paths
, true);
1747 cfg
->cmd_data
.query
.object
= g_string_new(NULL
);
1748 if (!cfg
->cmd_data
.query
.object
) {
1756 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1763 struct bt_config
*bt_config_print_ctf_metadata_create(
1764 const bt_value
*initial_plugin_paths
)
1766 struct bt_config
*cfg
;
1769 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_PRINT_CTF_METADATA
,
1770 initial_plugin_paths
, true);
1775 cfg
->cmd_data
.print_ctf_metadata
.path
= g_string_new(NULL
);
1776 if (!cfg
->cmd_data
.print_ctf_metadata
.path
) {
1781 cfg
->cmd_data
.print_ctf_metadata
.output_path
= g_string_new(NULL
);
1782 if (!cfg
->cmd_data
.print_ctf_metadata
.output_path
) {
1790 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1797 struct bt_config
*bt_config_print_lttng_live_sessions_create(
1798 const bt_value
*initial_plugin_paths
)
1800 struct bt_config
*cfg
;
1803 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
,
1804 initial_plugin_paths
, true);
1809 cfg
->cmd_data
.print_lttng_live_sessions
.url
= g_string_new(NULL
);
1810 if (!cfg
->cmd_data
.print_lttng_live_sessions
.url
) {
1815 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
=
1817 if (!cfg
->cmd_data
.print_lttng_live_sessions
.output_path
) {
1825 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1832 int bt_config_append_plugin_paths_check_setuid_setgid(
1833 bt_value
*plugin_paths
, const char *arg
)
1837 if (bt_common_is_setuid_setgid()) {
1838 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1842 if (bt_config_append_plugin_paths(plugin_paths
, arg
)) {
1843 printf_err("Invalid --plugin-path option's argument:\n %s\n",
1854 * Prints the expected format for a --params option.
1857 void print_expected_params_format(FILE *fp
)
1859 fprintf(fp
, "Expected format of PARAMS\n");
1860 fprintf(fp
, "-------------------------\n");
1862 fprintf(fp
, " PARAM=VALUE[,PARAM=VALUE]...\n");
1864 fprintf(fp
, "The parameter string is a comma-separated list of PARAM=VALUE assignments,\n");
1865 fprintf(fp
, "where PARAM is the parameter name (C identifier plus the [:.-] characters),\n");
1866 fprintf(fp
, "and VALUE can be one of:\n");
1868 fprintf(fp
, "* `null`, `nul`, `NULL`: null value (no backticks).\n");
1869 fprintf(fp
, "* `true`, `TRUE`, `yes`, `YES`: true boolean value (no backticks).\n");
1870 fprintf(fp
, "* `false`, `FALSE`, `no`, `NO`: false boolean value (no backticks).\n");
1871 fprintf(fp
, "* Binary (`0b` prefix), octal (`0` prefix), decimal, or hexadecimal\n");
1872 fprintf(fp
, " (`0x` prefix) unsigned (with `+` prefix) or signed 64-bit integer.\n");
1873 fprintf(fp
, "* Double precision floating point number (scientific notation is accepted).\n");
1874 fprintf(fp
, "* Unquoted string with no special characters, and not matching any of\n");
1875 fprintf(fp
, " the null and boolean value symbols above.\n");
1876 fprintf(fp
, "* Double-quoted string (accepts escape characters).\n");
1877 fprintf(fp
, "* Array, formatted as an opening `[`, a list of comma-separated values\n");
1878 fprintf(fp
, " (as described by the current list) and a closing `]`.\n");
1880 fprintf(fp
, "You can put whitespaces allowed around individual `=` and `,` symbols.\n");
1882 fprintf(fp
, "Example:\n");
1884 fprintf(fp
, " many=null, fresh=yes, condition=false, squirrel=-782329,\n");
1885 fprintf(fp
, " play=+23, observe=3.14, simple=beef, needs-quotes=\"some string\",\n");
1886 fprintf(fp
, " escape.chars-are:allowed=\"this is a \\\" double quote\",\n");
1887 fprintf(fp
, " things=[1, \"2\", 3]\n");
1889 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1890 fprintf(fp
, "babeltrace2 from a shell.\n");
1895 * Prints the help command usage.
1898 void print_help_usage(FILE *fp
)
1900 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] help [OPTIONS] PLUGIN\n");
1901 fprintf(fp
, " babeltrace2 [GENERAL OPTIONS] help [OPTIONS] TYPE.PLUGIN.CLS\n");
1903 fprintf(fp
, "Options:\n");
1905 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1906 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
1907 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1908 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1909 fprintf(fp
, " dynamic plugins can be loaded\n");
1910 fprintf(fp
, " -h, --help Show this help and quit\n");
1912 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1914 fprintf(fp
, "Use `babeltrace2 list-plugins` to show the list of available plugins.\n");
1918 struct poptOption help_long_options
[] = {
1919 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
1920 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
1921 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
1922 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
1923 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
1924 { NULL
, 0, '\0', NULL
, 0, NULL
, NULL
},
1928 * Creates a Babeltrace config object from the arguments of a help
1931 * *retcode is set to the appropriate exit code to use.
1934 struct bt_config
*bt_config_help_from_args(int argc
, const char *argv
[],
1935 int *retcode
, bool force_omit_system_plugin_path
,
1936 bool force_omit_home_plugin_path
,
1937 const bt_value
*initial_plugin_paths
, int default_log_level
)
1939 poptContext pc
= NULL
;
1943 struct bt_config
*cfg
= NULL
;
1944 const char *leftover
;
1945 char *plugin_name
= NULL
, *comp_cls_name
= NULL
;
1948 cfg
= bt_config_help_create(initial_plugin_paths
, default_log_level
);
1953 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
1954 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
1955 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
1961 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
1962 help_long_options
, 0);
1964 printf_err("Cannot get popt context\n");
1968 poptReadDefaultConfig(pc
, 0);
1970 while ((opt
= poptGetNextOpt(pc
)) > 0) {
1971 arg
= poptGetOptArg(pc
);
1974 case OPT_PLUGIN_PATH
:
1975 if (bt_config_append_plugin_paths_check_setuid_setgid(
1976 cfg
->plugin_paths
, arg
)) {
1980 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
1981 cfg
->omit_system_plugin_path
= true;
1983 case OPT_OMIT_HOME_PLUGIN_PATH
:
1984 cfg
->omit_home_plugin_path
= true;
1987 print_help_usage(stdout
);
1989 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1992 printf_err("Unknown command-line option specified (option code %d)\n",
2001 /* Check for option parsing error */
2003 printf_err("While parsing command-line options, at option %s: %s\n",
2004 poptBadOption(pc
, 0), poptStrerror(opt
));
2008 leftover
= poptGetArg(pc
);
2010 plugin_comp_cls_names(leftover
, NULL
,
2011 &plugin_name
, &comp_cls_name
,
2012 &cfg
->cmd_data
.help
.cfg_component
->type
);
2013 if (plugin_name
&& comp_cls_name
) {
2014 /* Component class help */
2016 cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
2019 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
,
2022 /* Fall back to plugin help */
2024 cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
2028 print_help_usage(stdout
);
2030 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2034 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
2042 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2045 g_free(plugin_name
);
2046 g_free(comp_cls_name
);
2049 poptFreeContext(pc
);
2057 * Prints the help command usage.
2060 void print_query_usage(FILE *fp
)
2062 fprintf(fp
, "Usage: babeltrace2 [GEN OPTS] query [OPTS] TYPE.PLUGIN.CLS OBJECT\n");
2064 fprintf(fp
, "Options:\n");
2066 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2067 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
2068 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2069 fprintf(fp
, " -p, --params=PARAMS Set the query parameters to PARAMS\n");
2070 fprintf(fp
, " (see the expected format of PARAMS below)\n");
2071 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2072 fprintf(fp
, " dynamic plugins can be loaded\n");
2073 fprintf(fp
, " -h, --help Show this help and quit\n");
2074 fprintf(fp
, "\n\n");
2075 print_expected_params_format(fp
);
2079 struct poptOption query_long_options
[] = {
2080 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
2081 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
2082 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
2083 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
2084 { "params", 'p', POPT_ARG_STRING
, NULL
, OPT_PARAMS
, NULL
, NULL
},
2085 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
2086 { NULL
, 0, '\0', NULL
, 0, NULL
, NULL
},
2090 * Creates a Babeltrace config object from the arguments of a query
2093 * *retcode is set to the appropriate exit code to use.
2096 struct bt_config
*bt_config_query_from_args(int argc
, const char *argv
[],
2097 int *retcode
, bool force_omit_system_plugin_path
,
2098 bool force_omit_home_plugin_path
,
2099 const bt_value
*initial_plugin_paths
,
2100 int default_log_level
)
2102 poptContext pc
= NULL
;
2106 struct bt_config
*cfg
= NULL
;
2107 const char *leftover
;
2110 params
= bt_value_null
;
2111 bt_value_get_ref(bt_value_null
);
2114 cfg
= bt_config_query_create(initial_plugin_paths
);
2119 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
2120 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
2121 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
2127 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
2128 query_long_options
, 0);
2130 printf_err("Cannot get popt context\n");
2134 poptReadDefaultConfig(pc
, 0);
2136 while ((opt
= poptGetNextOpt(pc
)) > 0) {
2137 arg
= poptGetOptArg(pc
);
2140 case OPT_PLUGIN_PATH
:
2141 if (bt_config_append_plugin_paths_check_setuid_setgid(
2142 cfg
->plugin_paths
, arg
)) {
2146 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
2147 cfg
->omit_system_plugin_path
= true;
2149 case OPT_OMIT_HOME_PLUGIN_PATH
:
2150 cfg
->omit_home_plugin_path
= true;
2154 bt_value_put_ref(params
);
2155 params
= bt_value_from_arg(arg
);
2157 printf_err("Invalid format for --params option's argument:\n %s\n",
2164 print_query_usage(stdout
);
2166 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2169 printf_err("Unknown command-line option specified (option code %d)\n",
2178 /* Check for option parsing error */
2180 printf_err("While parsing command-line options, at option %s: %s\n",
2181 poptBadOption(pc
, 0), poptStrerror(opt
));
2186 * We need exactly two leftover arguments which are the
2187 * mandatory component class specification and query object.
2189 leftover
= poptGetArg(pc
);
2191 cfg
->cmd_data
.query
.cfg_component
=
2192 bt_config_component_from_arg(leftover
,
2194 if (!cfg
->cmd_data
.query
.cfg_component
) {
2195 printf_err("Invalid format for component class specification:\n %s\n",
2201 BT_OBJECT_MOVE_REF(cfg
->cmd_data
.query
.cfg_component
->params
,
2204 print_query_usage(stdout
);
2206 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2210 leftover
= poptGetArg(pc
);
2212 if (strlen(leftover
) == 0) {
2213 printf_err("Invalid empty object\n");
2217 g_string_assign(cfg
->cmd_data
.query
.object
, leftover
);
2219 print_query_usage(stdout
);
2221 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2225 leftover
= poptGetArg(pc
);
2227 printf_err("Unexpected argument: %s\n", leftover
);
2231 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
2239 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2243 poptFreeContext(pc
);
2246 bt_value_put_ref(params
);
2252 * Prints the list-plugins command usage.
2255 void print_list_plugins_usage(FILE *fp
)
2257 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] list-plugins [OPTIONS]\n");
2259 fprintf(fp
, "Options:\n");
2261 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2262 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
2263 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2264 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2265 fprintf(fp
, " dynamic plugins can be loaded\n");
2266 fprintf(fp
, " -h, --help Show this help and quit\n");
2268 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
2270 fprintf(fp
, "Use `babeltrace2 help` to get help for a specific plugin or component class.\n");
2274 struct poptOption list_plugins_long_options
[] = {
2275 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
2276 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
2277 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
2278 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
2279 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
2280 { NULL
, 0, '\0', NULL
, 0, NULL
, NULL
},
2284 * Creates a Babeltrace config object from the arguments of a
2285 * list-plugins command.
2287 * *retcode is set to the appropriate exit code to use.
2290 struct bt_config
*bt_config_list_plugins_from_args(int argc
, const char *argv
[],
2291 int *retcode
, bool force_omit_system_plugin_path
,
2292 bool force_omit_home_plugin_path
,
2293 const bt_value
*initial_plugin_paths
)
2295 poptContext pc
= NULL
;
2299 struct bt_config
*cfg
= NULL
;
2300 const char *leftover
;
2303 cfg
= bt_config_list_plugins_create(initial_plugin_paths
);
2308 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
2309 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
2310 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
2316 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
2317 list_plugins_long_options
, 0);
2319 printf_err("Cannot get popt context\n");
2323 poptReadDefaultConfig(pc
, 0);
2325 while ((opt
= poptGetNextOpt(pc
)) > 0) {
2326 arg
= poptGetOptArg(pc
);
2329 case OPT_PLUGIN_PATH
:
2330 if (bt_config_append_plugin_paths_check_setuid_setgid(
2331 cfg
->plugin_paths
, arg
)) {
2335 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
2336 cfg
->omit_system_plugin_path
= true;
2338 case OPT_OMIT_HOME_PLUGIN_PATH
:
2339 cfg
->omit_home_plugin_path
= true;
2342 print_list_plugins_usage(stdout
);
2344 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2347 printf_err("Unknown command-line option specified (option code %d)\n",
2356 /* Check for option parsing error */
2358 printf_err("While parsing command-line options, at option %s: %s\n",
2359 poptBadOption(pc
, 0), poptStrerror(opt
));
2363 leftover
= poptGetArg(pc
);
2365 printf_err("Unexpected argument: %s\n", leftover
);
2369 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
2377 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2381 poptFreeContext(pc
);
2389 * Prints the run command usage.
2392 void print_run_usage(FILE *fp
)
2394 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] run [OPTIONS]\n");
2396 fprintf(fp
, "Options:\n");
2398 fprintf(fp
, " -b, --base-params=PARAMS Set PARAMS as the current base parameters\n");
2399 fprintf(fp
, " for all the following components until\n");
2400 fprintf(fp
, " --reset-base-params is encountered\n");
2401 fprintf(fp
, " (see the expected format of PARAMS below)\n");
2402 fprintf(fp
, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2403 fprintf(fp
, " Instantiate the component class CLS of type\n");
2404 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
2405 fprintf(fp
, " in the plugin PLUGIN, add it to the graph,\n");
2406 fprintf(fp
, " and optionally name it NAME (you can also\n");
2407 fprintf(fp
, " specify the name with --name)\n");
2408 fprintf(fp
, " -x, --connect=CONNECTION Connect two created components (see the\n");
2409 fprintf(fp
, " expected format of CONNECTION below)\n");
2410 fprintf(fp
, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
2411 fprintf(fp
, " (`N`, `V`, `D`, `I`, `W`, `E`, or `F`)\n");
2412 fprintf(fp
, " -n, --name=NAME Set the name of the current component\n");
2413 fprintf(fp
, " to NAME (must be unique amongst all the\n");
2414 fprintf(fp
, " names of the created components)\n");
2415 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2416 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
2417 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2418 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2419 fprintf(fp
, " current component (see the expected format\n");
2420 fprintf(fp
, " of PARAMS below)\n");
2421 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2422 fprintf(fp
, " dynamic plugins can be loaded\n");
2423 fprintf(fp
, " -r, --reset-base-params Reset the current base parameters to an\n");
2424 fprintf(fp
, " empty map\n");
2425 fprintf(fp
, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
2426 fprintf(fp
, " the graph later, retry in DUR µs\n");
2427 fprintf(fp
, " (default: 100000)\n");
2428 fprintf(fp
, " -h, --help Show this help and quit\n");
2430 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
2431 fprintf(fp
, "\n\n");
2432 fprintf(fp
, "Expected format of CONNECTION\n");
2433 fprintf(fp
, "-----------------------------\n");
2435 fprintf(fp
, " UPSTREAM[.UPSTREAM-PORT]:DOWNSTREAM[.DOWNSTREAM-PORT]\n");
2437 fprintf(fp
, "UPSTREAM and DOWNSTREAM are names of the upstream and downstream\n");
2438 fprintf(fp
, "components to connect together. You must escape the following characters\n\n");
2439 fprintf(fp
, "with `\\`: `\\`, `.`, and `:`. You can set the name of the current\n");
2440 fprintf(fp
, "component with the --name option.\n");
2442 fprintf(fp
, "UPSTREAM-PORT and DOWNSTREAM-PORT are optional globbing patterns to\n");
2443 fprintf(fp
, "identify the upstream and downstream ports to use for the connection.\n");
2444 fprintf(fp
, "When the port is not specified, `*` is used.\n");
2446 fprintf(fp
, "When a component named UPSTREAM has an available port which matches the\n");
2447 fprintf(fp
, "UPSTREAM-PORT globbing pattern, it is connected to the first port which\n");
2448 fprintf(fp
, "matches the DOWNSTREAM-PORT globbing pattern of the component named\n");
2449 fprintf(fp
, "DOWNSTREAM.\n");
2451 fprintf(fp
, "The only special character in UPSTREAM-PORT and DOWNSTREAM-PORT is `*`\n");
2452 fprintf(fp
, "which matches anything. You must escape the following characters\n");
2453 fprintf(fp
, "with `\\`: `\\`, `*`, `?`, `[`, `.`, and `:`.\n");
2455 fprintf(fp
, "You can connect a source component to a filter or sink component. You\n");
2456 fprintf(fp
, "can connect a filter component to a sink component.\n");
2458 fprintf(fp
, "Examples:\n");
2460 fprintf(fp
, " my-src:my-sink\n");
2461 fprintf(fp
, " ctf-fs.*stream*:utils-muxer:*\n");
2463 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
2464 fprintf(fp
, "babeltrace2 from a shell.\n");
2465 fprintf(fp
, "\n\n");
2466 print_expected_params_format(fp
);
2470 * Creates a Babeltrace config object from the arguments of a run
2473 * *retcode is set to the appropriate exit code to use.
2476 struct bt_config
*bt_config_run_from_args(int argc
, const char *argv
[],
2477 int *retcode
, bool force_omit_system_plugin_path
,
2478 bool force_omit_home_plugin_path
,
2479 const bt_value
*initial_plugin_paths
, int default_log_level
)
2481 poptContext pc
= NULL
;
2483 struct bt_config_component
*cur_cfg_comp
= NULL
;
2484 enum bt_config_component_dest cur_cfg_comp_dest
=
2485 BT_CONFIG_COMPONENT_DEST_UNKNOWN
;
2486 bt_value
*cur_base_params
= NULL
;
2488 struct bt_config
*cfg
= NULL
;
2489 bt_value
*instance_names
= NULL
;
2490 bt_value
*connection_args
= NULL
;
2491 char error_buf
[256] = { 0 };
2492 long retry_duration
= -1;
2493 bt_value_status status
;
2494 struct poptOption run_long_options
[] = {
2495 { "base-params", 'b', POPT_ARG_STRING
, NULL
, OPT_BASE_PARAMS
, NULL
, NULL
},
2496 { "component", 'c', POPT_ARG_STRING
, NULL
, OPT_COMPONENT
, NULL
, NULL
},
2497 { "connect", 'x', POPT_ARG_STRING
, NULL
, OPT_CONNECT
, NULL
, NULL
},
2498 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
2499 { "log-level", 'l', POPT_ARG_STRING
, NULL
, OPT_LOG_LEVEL
, NULL
, NULL
},
2500 { "name", 'n', POPT_ARG_STRING
, NULL
, OPT_NAME
, NULL
, NULL
},
2501 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
2502 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
2503 { "params", 'p', POPT_ARG_STRING
, NULL
, OPT_PARAMS
, NULL
, NULL
},
2504 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
2505 { "reset-base-params", 'r', POPT_ARG_NONE
, NULL
, OPT_RESET_BASE_PARAMS
, NULL
, NULL
},
2506 { "retry-duration", '\0', POPT_ARG_LONG
, &retry_duration
, OPT_RETRY_DURATION
, NULL
, NULL
},
2507 { NULL
, 0, '\0', NULL
, 0, NULL
, NULL
},
2513 print_run_usage(stdout
);
2518 cfg
= bt_config_run_create(initial_plugin_paths
);
2523 cfg
->cmd_data
.run
.retry_duration_us
= 100000;
2524 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
2525 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
2526 cur_base_params
= bt_value_map_create();
2527 if (!cur_base_params
) {
2532 instance_names
= bt_value_map_create();
2533 if (!instance_names
) {
2538 connection_args
= bt_value_array_create();
2539 if (!connection_args
) {
2544 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
2550 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
2551 run_long_options
, 0);
2553 printf_err("Cannot get popt context\n");
2557 poptReadDefaultConfig(pc
, 0);
2559 while ((opt
= poptGetNextOpt(pc
)) > 0) {
2560 arg
= poptGetOptArg(pc
);
2563 case OPT_PLUGIN_PATH
:
2564 if (bt_config_append_plugin_paths_check_setuid_setgid(
2565 cfg
->plugin_paths
, arg
)) {
2569 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
2570 cfg
->omit_system_plugin_path
= true;
2572 case OPT_OMIT_HOME_PLUGIN_PATH
:
2573 cfg
->omit_home_plugin_path
= true;
2577 enum bt_config_component_dest new_dest
;
2580 ret
= add_run_cfg_comp_check_name(cfg
,
2581 cur_cfg_comp
, cur_cfg_comp_dest
,
2583 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2589 cur_cfg_comp
= bt_config_component_from_arg(arg
,
2591 if (!cur_cfg_comp
) {
2592 printf_err("Invalid format for --component option's argument:\n %s\n",
2597 switch (cur_cfg_comp
->type
) {
2598 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2599 new_dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
2601 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2602 new_dest
= BT_CONFIG_COMPONENT_DEST_FILTER
;
2604 case BT_COMPONENT_CLASS_TYPE_SINK
:
2605 new_dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
2611 BT_ASSERT(cur_base_params
);
2612 bt_value_put_ref(cur_cfg_comp
->params
);
2613 status
= bt_value_copy(cur_base_params
,
2614 &cur_cfg_comp
->params
);
2615 if (status
!= BT_VALUE_STATUS_OK
) {
2620 cur_cfg_comp_dest
= new_dest
;
2626 bt_value
*params_to_set
;
2628 if (!cur_cfg_comp
) {
2629 printf_err("Cannot add parameters to unavailable component:\n %s\n",
2634 params
= bt_value_from_arg(arg
);
2636 printf_err("Invalid format for --params option's argument:\n %s\n",
2641 status
= bt_value_map_extend(cur_cfg_comp
->params
,
2642 params
, ¶ms_to_set
);
2643 BT_VALUE_PUT_REF_AND_RESET(params
);
2644 if (status
!= BT_VALUE_STATUS_OK
) {
2645 printf_err("Cannot extend current component parameters with --params option's argument:\n %s\n",
2650 BT_OBJECT_MOVE_REF(cur_cfg_comp
->params
, params_to_set
);
2654 if (!cur_cfg_comp
) {
2655 printf_err("Cannot set the name of unavailable component:\n %s\n",
2660 g_string_assign(cur_cfg_comp
->instance_name
, arg
);
2663 if (!cur_cfg_comp
) {
2664 printf_err("Cannot set the log level of unavailable component:\n %s\n",
2669 cur_cfg_comp
->log_level
=
2670 bt_log_get_level_from_string(arg
);
2671 if (cur_cfg_comp
->log_level
< 0) {
2672 printf_err("Invalid argument for --log-level option:\n %s\n",
2677 case OPT_BASE_PARAMS
:
2680 bt_value_from_arg(arg
);
2683 printf_err("Invalid format for --base-params option's argument:\n %s\n",
2688 BT_OBJECT_MOVE_REF(cur_base_params
, params
);
2691 case OPT_RESET_BASE_PARAMS
:
2692 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
2693 cur_base_params
= bt_value_map_create();
2694 if (!cur_base_params
) {
2700 if (bt_value_array_append_string_element(
2701 connection_args
, arg
)) {
2706 case OPT_RETRY_DURATION
:
2707 if (retry_duration
< 0) {
2708 printf_err("--retry-duration option's argument must be positive or 0: %ld\n",
2713 cfg
->cmd_data
.run
.retry_duration_us
=
2714 (uint64_t) retry_duration
;
2717 print_run_usage(stdout
);
2719 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2722 printf_err("Unknown command-line option specified (option code %d)\n",
2731 /* Check for option parsing error */
2733 printf_err("While parsing command-line options, at option %s: %s\n",
2734 poptBadOption(pc
, 0), poptStrerror(opt
));
2738 /* This command does not accept leftover arguments */
2739 if (poptPeekArg(pc
)) {
2740 printf_err("Unexpected argument: %s\n", poptPeekArg(pc
));
2744 /* Add current component */
2746 ret
= add_run_cfg_comp_check_name(cfg
, cur_cfg_comp
,
2747 cur_cfg_comp_dest
, instance_names
);
2748 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2754 if (cfg
->cmd_data
.run
.sources
->len
== 0) {
2755 printf_err("Incomplete graph: no source component\n");
2759 if (cfg
->cmd_data
.run
.sinks
->len
== 0) {
2760 printf_err("Incomplete graph: no sink component\n");
2764 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
2768 ret
= bt_config_cli_args_create_connections(cfg
,
2772 printf_err("Cannot creation connections:\n%s", error_buf
);
2780 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2784 poptFreeContext(pc
);
2788 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2789 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
2790 BT_VALUE_PUT_REF_AND_RESET(instance_names
);
2791 BT_VALUE_PUT_REF_AND_RESET(connection_args
);
2796 struct bt_config
*bt_config_run_from_args_array(const bt_value
*run_args
,
2797 int *retcode
, bool force_omit_system_plugin_path
,
2798 bool force_omit_home_plugin_path
,
2799 const bt_value
*initial_plugin_paths
, int default_log_level
)
2801 struct bt_config
*cfg
= NULL
;
2804 const size_t argc
= bt_value_array_get_size(run_args
) + 1;
2806 argv
= calloc(argc
, sizeof(*argv
));
2814 len
= bt_value_array_get_size(run_args
);
2816 printf_err("Invalid executable arguments\n");
2819 for (i
= 0; i
< len
; i
++) {
2820 const bt_value
*arg_value
=
2821 bt_value_array_borrow_element_by_index_const(run_args
,
2825 BT_ASSERT(arg_value
);
2826 arg
= bt_value_string_get(arg_value
);
2831 cfg
= bt_config_run_from_args(argc
, argv
, retcode
,
2832 force_omit_system_plugin_path
, force_omit_home_plugin_path
,
2833 initial_plugin_paths
, default_log_level
);
2841 * Prints the convert command usage.
2844 void print_convert_usage(FILE *fp
)
2846 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] [convert] [OPTIONS] [PATH/URL]\n");
2848 fprintf(fp
, "Options:\n");
2850 fprintf(fp
, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2851 fprintf(fp
, " Instantiate the component class CLS of type\n");
2852 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
2853 fprintf(fp
, " in the plugin PLUGIN, add it to the\n");
2854 fprintf(fp
, " conversion graph, and optionally name it\n");
2855 fprintf(fp
, " NAME (you can also specify the name with\n");
2856 fprintf(fp
, " --name)\n");
2857 fprintf(fp
, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
2858 fprintf(fp
, " (`N`, `V`, `D`, `I`, `W`, `E`, or `F`)\n");
2859 fprintf(fp
, " --name=NAME Set the name of the current component\n");
2860 fprintf(fp
, " to NAME (must be unique amongst all the\n");
2861 fprintf(fp
, " names of the created components)\n");
2862 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2863 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
2864 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2865 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2866 fprintf(fp
, " current component (see the expected format\n");
2867 fprintf(fp
, " of PARAMS below)\n");
2868 fprintf(fp
, " -P, --path=PATH Set the `path` string parameter of the\n");
2869 fprintf(fp
, " current component to PATH\n");
2870 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2871 fprintf(fp
, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
2872 fprintf(fp
, " the graph later, retry in DUR µs\n");
2873 fprintf(fp
, " (default: 100000)\n");
2874 fprintf(fp
, " dynamic plugins can be loaded\n");
2875 fprintf(fp
, " --run-args Print the equivalent arguments for the\n");
2876 fprintf(fp
, " `run` command to the standard output,\n");
2877 fprintf(fp
, " formatted for a shell, and quit\n");
2878 fprintf(fp
, " --run-args-0 Print the equivalent arguments for the\n");
2879 fprintf(fp
, " `run` command to the standard output,\n");
2880 fprintf(fp
, " formatted for `xargs -0`, and quit\n");
2881 fprintf(fp
, " --stream-intersection Only process events when all streams\n");
2882 fprintf(fp
, " are active\n");
2883 fprintf(fp
, " -u, --url=URL Set the `url` string parameter of the\n");
2884 fprintf(fp
, " current component to URL\n");
2885 fprintf(fp
, " -h, --help Show this help and quit\n");
2887 fprintf(fp
, "Implicit `source.ctf.fs` component options:\n");
2889 fprintf(fp
, " --clock-offset=SEC Set clock offset to SEC seconds\n");
2890 fprintf(fp
, " --clock-offset-ns=NS Set clock offset to NS ns\n");
2892 fprintf(fp
, "Implicit `sink.text.pretty` component options:\n");
2894 fprintf(fp
, " --clock-cycles Print timestamps in clock cycles\n");
2895 fprintf(fp
, " --clock-date Print timestamp dates\n");
2896 fprintf(fp
, " --clock-gmt Print and parse timestamps in the GMT\n");
2897 fprintf(fp
, " time zone instead of the local time zone\n");
2898 fprintf(fp
, " --clock-seconds Print the timestamps as `SEC.NS` instead\n");
2899 fprintf(fp
, " of `hh:mm:ss.nnnnnnnnn`\n");
2900 fprintf(fp
, " --color=(never | auto | always)\n");
2901 fprintf(fp
, " Never, automatically, or always emit\n");
2902 fprintf(fp
, " console color codes\n");
2903 fprintf(fp
, " -f, --fields=FIELD[,FIELD]... Print additional fields; FIELD can be:\n");
2904 fprintf(fp
, " `all`, `trace`, `trace:hostname`,\n");
2905 fprintf(fp
, " `trace:domain`, `trace:procname`,\n");
2906 fprintf(fp
, " `trace:vpid`, `loglevel`, `emf`\n");
2907 fprintf(fp
, " -n, --names=NAME[,NAME]... Print field names; NAME can be:\n");
2908 fprintf(fp
, " `payload` (or `arg` or `args`), `none`,\n");
2909 fprintf(fp
, " `all`, `scope`, `header`, `context`\n");
2910 fprintf(fp
, " (or `ctx`)\n");
2911 fprintf(fp
, " --no-delta Do not print time delta between\n");
2912 fprintf(fp
, " consecutive events\n");
2913 fprintf(fp
, " -w, --output=PATH Write output text to PATH instead of\n");
2914 fprintf(fp
, " the standard output\n");
2916 fprintf(fp
, "Implicit `filter.utils.muxer` component options:\n");
2918 fprintf(fp
, " --clock-force-correlate Assume that clocks are inherently\n");
2919 fprintf(fp
, " correlated across traces\n");
2921 fprintf(fp
, "Implicit `filter.utils.trimmer` component options:\n");
2923 fprintf(fp
, " -b, --begin=BEGIN Set the beginning time of the conversion\n");
2924 fprintf(fp
, " time range to BEGIN (see the format of\n");
2925 fprintf(fp
, " BEGIN below)\n");
2926 fprintf(fp
, " -e, --end=END Set the end time of the conversion time\n");
2927 fprintf(fp
, " range to END (see the format of END below)\n");
2928 fprintf(fp
, " -t, --timerange=TIMERANGE Set conversion time range to TIMERANGE:\n");
2929 fprintf(fp
, " BEGIN,END or [BEGIN,END] (literally `[` and\n");
2930 fprintf(fp
, " `]`) (see the format of BEGIN/END below)\n");
2932 fprintf(fp
, "Implicit `filter.lttng-utils.debug-info` component options:\n");
2934 fprintf(fp
, " --debug-info Create an implicit\n");
2935 fprintf(fp
, " `filter.lttng-utils.debug-info` component\n");
2936 fprintf(fp
, " --debug-info-dir=DIR Search for debug info in directory DIR\n");
2937 fprintf(fp
, " instead of `/usr/lib/debug`\n");
2938 fprintf(fp
, " --debug-info-full-path Show full debug info source and\n");
2939 fprintf(fp
, " binary paths instead of just names\n");
2940 fprintf(fp
, " --debug-info-target-prefix=DIR\n");
2941 fprintf(fp
, " Use directory DIR as a prefix when\n");
2942 fprintf(fp
, " looking up executables during debug\n");
2943 fprintf(fp
, " info analysis\n");
2945 fprintf(fp
, "Legacy options that still work:\n");
2947 fprintf(fp
, " -i, --input-format=(ctf | lttng-live)\n");
2948 fprintf(fp
, " `ctf`:\n");
2949 fprintf(fp
, " Create an implicit `source.ctf.fs`\n");
2950 fprintf(fp
, " component\n");
2951 fprintf(fp
, " `lttng-live`:\n");
2952 fprintf(fp
, " Create an implicit `source.ctf.lttng-live`\n");
2953 fprintf(fp
, " component\n");
2954 fprintf(fp
, " -o, --output-format=(text | ctf | dummy | ctf-metadata)\n");
2955 fprintf(fp
, " `text`:\n");
2956 fprintf(fp
, " Create an implicit `sink.text.pretty`\n");
2957 fprintf(fp
, " component\n");
2958 fprintf(fp
, " `ctf`:\n");
2959 fprintf(fp
, " Create an implicit `sink.ctf.fs`\n");
2960 fprintf(fp
, " component\n");
2961 fprintf(fp
, " `dummy`:\n");
2962 fprintf(fp
, " Create an implicit `sink.utils.dummy`\n");
2963 fprintf(fp
, " component\n");
2964 fprintf(fp
, " `ctf-metadata`:\n");
2965 fprintf(fp
, " Query the `source.ctf.fs` component class\n");
2966 fprintf(fp
, " for metadata text and quit\n");
2968 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
2969 fprintf(fp
, "\n\n");
2970 fprintf(fp
, "Format of BEGIN and END\n");
2971 fprintf(fp
, "-----------------------\n");
2973 fprintf(fp
, " [YYYY-MM-DD [hh:mm:]]ss[.nnnnnnnnn]\n");
2974 fprintf(fp
, "\n\n");
2975 print_expected_params_format(fp
);
2979 struct poptOption convert_long_options
[] = {
2980 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
2981 { "begin", 'b', POPT_ARG_STRING
, NULL
, OPT_BEGIN
, NULL
, NULL
},
2982 { "clock-cycles", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_CYCLES
, NULL
, NULL
},
2983 { "clock-date", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_DATE
, NULL
, NULL
},
2984 { "clock-force-correlate", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_FORCE_CORRELATE
, NULL
, NULL
},
2985 { "clock-gmt", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_GMT
, NULL
, NULL
},
2986 { "clock-offset", '\0', POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET
, NULL
, NULL
},
2987 { "clock-offset-ns", '\0', POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET_NS
, NULL
, NULL
},
2988 { "clock-seconds", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_SECONDS
, NULL
, NULL
},
2989 { "color", '\0', POPT_ARG_STRING
, NULL
, OPT_COLOR
, NULL
, NULL
},
2990 { "component", 'c', POPT_ARG_STRING
, NULL
, OPT_COMPONENT
, NULL
, NULL
},
2991 { "debug", 'd', POPT_ARG_NONE
, NULL
, OPT_DEBUG
, NULL
, NULL
},
2992 { "debug-info-dir", 0, POPT_ARG_STRING
, NULL
, OPT_DEBUG_INFO_DIR
, NULL
, NULL
},
2993 { "debug-info-full-path", 0, POPT_ARG_NONE
, NULL
, OPT_DEBUG_INFO_FULL_PATH
, NULL
, NULL
},
2994 { "debug-info-target-prefix", 0, POPT_ARG_STRING
, NULL
, OPT_DEBUG_INFO_TARGET_PREFIX
, NULL
, NULL
},
2995 { "end", 'e', POPT_ARG_STRING
, NULL
, OPT_END
, NULL
, NULL
},
2996 { "fields", 'f', POPT_ARG_STRING
, NULL
, OPT_FIELDS
, NULL
, NULL
},
2997 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
2998 { "input-format", 'i', POPT_ARG_STRING
, NULL
, OPT_INPUT_FORMAT
, NULL
, NULL
},
2999 { "log-level", 'l', POPT_ARG_STRING
, NULL
, OPT_LOG_LEVEL
, NULL
, NULL
},
3000 { "name", '\0', POPT_ARG_STRING
, NULL
, OPT_NAME
, NULL
, NULL
},
3001 { "names", 'n', POPT_ARG_STRING
, NULL
, OPT_NAMES
, NULL
, NULL
},
3002 { "debug-info", '\0', POPT_ARG_NONE
, NULL
, OPT_DEBUG_INFO
, NULL
, NULL
},
3003 { "no-delta", '\0', POPT_ARG_NONE
, NULL
, OPT_NO_DELTA
, NULL
, NULL
},
3004 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
3005 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
3006 { "output", 'w', POPT_ARG_STRING
, NULL
, OPT_OUTPUT
, NULL
, NULL
},
3007 { "output-format", 'o', POPT_ARG_STRING
, NULL
, OPT_OUTPUT_FORMAT
, NULL
, NULL
},
3008 { "params", 'p', POPT_ARG_STRING
, NULL
, OPT_PARAMS
, NULL
, NULL
},
3009 { "path", 'P', POPT_ARG_STRING
, NULL
, OPT_PATH
, NULL
, NULL
},
3010 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
3011 { "retry-duration", '\0', POPT_ARG_STRING
, NULL
, OPT_RETRY_DURATION
, NULL
, NULL
},
3012 { "run-args", '\0', POPT_ARG_NONE
, NULL
, OPT_RUN_ARGS
, NULL
, NULL
},
3013 { "run-args-0", '\0', POPT_ARG_NONE
, NULL
, OPT_RUN_ARGS_0
, NULL
, NULL
},
3014 { "stream-intersection", '\0', POPT_ARG_NONE
, NULL
, OPT_STREAM_INTERSECTION
, NULL
, NULL
},
3015 { "timerange", '\0', POPT_ARG_STRING
, NULL
, OPT_TIMERANGE
, NULL
, NULL
},
3016 { "url", 'u', POPT_ARG_STRING
, NULL
, OPT_URL
, NULL
, NULL
},
3017 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
3018 { NULL
, 0, '\0', NULL
, 0, NULL
, NULL
},
3022 GString
*get_component_auto_name(const char *prefix
,
3023 const bt_value
*existing_names
)
3026 GString
*auto_name
= g_string_new(NULL
);
3033 if (!bt_value_map_has_entry(existing_names
, prefix
)) {
3034 g_string_assign(auto_name
, prefix
);
3039 g_string_printf(auto_name
, "%s-%d", prefix
, i
);
3041 } while (bt_value_map_has_entry(existing_names
, auto_name
->str
));
3047 struct implicit_component_args
{
3051 GString
*params_arg
;
3052 bt_value
*extra_params
;
3056 int assign_name_to_implicit_component(struct implicit_component_args
*args
,
3057 const char *prefix
, bt_value
*existing_names
,
3058 GList
**comp_names
, bool append_to_comp_names
)
3061 GString
*name
= NULL
;
3063 if (!args
->exists
) {
3067 name
= get_component_auto_name(prefix
,
3075 g_string_assign(args
->name_arg
, name
->str
);
3077 if (bt_value_map_insert_entry(existing_names
, name
->str
,
3084 if (append_to_comp_names
) {
3085 *comp_names
= g_list_append(*comp_names
, name
);
3091 g_string_free(name
, TRUE
);
3098 int append_run_args_for_implicit_component(
3099 struct implicit_component_args
*impl_args
,
3105 if (!impl_args
->exists
) {
3109 if (bt_value_array_append_string_element(run_args
, "--component")) {
3114 if (bt_value_array_append_string_element(run_args
, impl_args
->comp_arg
->str
)) {
3119 if (bt_value_array_append_string_element(run_args
, "--name")) {
3124 if (bt_value_array_append_string_element(run_args
, impl_args
->name_arg
->str
)) {
3129 if (impl_args
->params_arg
->len
> 0) {
3130 if (bt_value_array_append_string_element(run_args
, "--params")) {
3135 if (bt_value_array_append_string_element(run_args
,
3136 impl_args
->params_arg
->str
)) {
3142 for (i
= 0; i
< bt_value_array_get_size(impl_args
->extra_params
);
3144 const bt_value
*elem
;
3147 elem
= bt_value_array_borrow_element_by_index(impl_args
->extra_params
,
3153 BT_ASSERT(bt_value_is_string(elem
));
3154 arg
= bt_value_string_get(elem
);
3155 ret
= bt_value_array_append_string_element(run_args
, arg
);
3172 void finalize_implicit_component_args(struct implicit_component_args
*args
)
3176 if (args
->comp_arg
) {
3177 g_string_free(args
->comp_arg
, TRUE
);
3180 if (args
->name_arg
) {
3181 g_string_free(args
->name_arg
, TRUE
);
3184 if (args
->params_arg
) {
3185 g_string_free(args
->params_arg
, TRUE
);
3188 bt_value_put_ref(args
->extra_params
);
3192 int init_implicit_component_args(struct implicit_component_args
*args
,
3193 const char *comp_arg
, bool exists
)
3197 args
->exists
= exists
;
3198 args
->comp_arg
= g_string_new(comp_arg
);
3199 args
->name_arg
= g_string_new(NULL
);
3200 args
->params_arg
= g_string_new(NULL
);
3201 args
->extra_params
= bt_value_array_create();
3203 if (!args
->comp_arg
|| !args
->name_arg
||
3204 !args
->params_arg
|| !args
->extra_params
) {
3206 finalize_implicit_component_args(args
);
3216 void append_implicit_component_param(struct implicit_component_args
*args
,
3217 const char *key
, const char *value
)
3222 append_param_arg(args
->params_arg
, key
, value
);
3225 /* Escape value to make it suitable to use as a string parameter value. */
3227 gchar
*escape_string_value(const char *value
)
3232 ret
= g_string_new(NULL
);
3243 g_string_append_c(ret
, '\\');
3247 g_string_append_c(ret
, *in
);
3253 return g_string_free(ret
, FALSE
);
3257 int bt_value_to_cli_param_value_append(const bt_value
*value
, GString
*buf
)
3263 switch (bt_value_get_type(value
)) {
3264 case BT_VALUE_TYPE_STRING
:
3266 const char *str_value
= bt_value_string_get(value
);
3267 gchar
*escaped_str_value
;
3269 escaped_str_value
= escape_string_value(str_value
);
3270 if (!escaped_str_value
) {
3274 g_string_append_printf(buf
, "\"%s\"", escaped_str_value
);
3276 g_free(escaped_str_value
);
3279 case BT_VALUE_TYPE_ARRAY
: {
3280 g_string_append_c(buf
, '[');
3281 uint64_t sz
= bt_value_array_get_size(value
);
3282 for (uint64_t i
= 0; i
< sz
; i
++) {
3283 const bt_value
*item
;
3287 g_string_append(buf
, ", ");
3290 item
= bt_value_array_borrow_element_by_index_const(
3292 ret
= bt_value_to_cli_param_value_append(item
, buf
);
3298 g_string_append_c(buf
, ']');
3312 * Convert `value` to its equivalent representation as a command line parameter
3317 gchar
*bt_value_to_cli_param_value(bt_value
*value
)
3320 gchar
*result
= NULL
;
3322 buf
= g_string_new(NULL
);
3328 if (bt_value_to_cli_param_value_append(value
, buf
)) {
3332 result
= g_string_free(buf
, FALSE
);
3339 g_string_free(buf
, TRUE
);
3347 int append_parameter_to_args(bt_value
*args
, const char *key
, bt_value
*value
)
3350 BT_ASSERT(bt_value_get_type(args
) == BT_VALUE_TYPE_ARRAY
);
3355 gchar
*str_value
= NULL
;
3356 GString
*parameter
= NULL
;
3358 if (bt_value_array_append_string_element(args
, "--params")) {
3364 str_value
= bt_value_to_cli_param_value(value
);
3370 parameter
= g_string_new(NULL
);
3377 g_string_printf(parameter
, "%s=%s", key
, str_value
);
3379 if (bt_value_array_append_string_element(args
, parameter
->str
)) {
3387 g_string_free(parameter
, TRUE
);
3400 int append_string_parameter_to_args(bt_value
*args
, const char *key
, const char *value
)
3402 bt_value
*str_value
;
3405 str_value
= bt_value_string_create_init(value
);
3413 ret
= append_parameter_to_args(args
, key
, str_value
);
3416 BT_VALUE_PUT_REF_AND_RESET(str_value
);
3421 int append_implicit_component_extra_param(struct implicit_component_args
*args
,
3422 const char *key
, const char *value
)
3424 return append_string_parameter_to_args(args
->extra_params
, key
, value
);
3428 int convert_append_name_param(enum bt_config_component_dest dest
,
3429 GString
*cur_name
, GString
*cur_name_prefix
,
3431 bt_value
*all_names
,
3432 GList
**source_names
, GList
**filter_names
,
3437 if (cur_name_prefix
->len
> 0) {
3438 /* We're after a --component option */
3439 GString
*name
= NULL
;
3440 bool append_name_opt
= false;
3442 if (cur_name
->len
== 0) {
3444 * No explicit name was provided for the user
3447 name
= get_component_auto_name(cur_name_prefix
->str
,
3449 append_name_opt
= true;
3452 * An explicit name was provided for the user
3455 if (bt_value_map_has_entry(all_names
,
3457 printf_err("Duplicate component instance name:\n %s\n",
3462 name
= g_string_new(cur_name
->str
);
3471 * Remember this name globally, for the uniqueness of
3472 * all component names.
3474 if (bt_value_map_insert_entry(all_names
, name
->str
, bt_value_null
)) {
3480 * Append the --name option if necessary.
3482 if (append_name_opt
) {
3483 if (bt_value_array_append_string_element(run_args
, "--name")) {
3488 if (bt_value_array_append_string_element(run_args
, name
->str
)) {
3495 * Remember this name specifically for the type of the
3496 * component. This is to create connection arguments.
3499 case BT_CONFIG_COMPONENT_DEST_SOURCE
:
3500 *source_names
= g_list_append(*source_names
, name
);
3502 case BT_CONFIG_COMPONENT_DEST_FILTER
:
3503 *filter_names
= g_list_append(*filter_names
, name
);
3505 case BT_CONFIG_COMPONENT_DEST_SINK
:
3506 *sink_names
= g_list_append(*sink_names
, name
);
3512 g_string_assign(cur_name_prefix
, "");
3525 * Escapes `.`, `:`, and `\` of `input` with `\`.
3528 GString
*escape_dot_colon(const char *input
)
3530 GString
*output
= g_string_new(NULL
);
3538 for (ch
= input
; *ch
!= '\0'; ch
++) {
3539 if (*ch
== '\\' || *ch
== '.' || *ch
== ':') {
3540 g_string_append_c(output
, '\\');
3543 g_string_append_c(output
, *ch
);
3551 * Appends a --connect option to a list of arguments. `upstream_name`
3552 * and `downstream_name` are escaped with escape_dot_colon() in this
3556 int append_connect_arg(bt_value
*run_args
,
3557 const char *upstream_name
, const char *downstream_name
)
3560 GString
*e_upstream_name
= escape_dot_colon(upstream_name
);
3561 GString
*e_downstream_name
= escape_dot_colon(downstream_name
);
3562 GString
*arg
= g_string_new(NULL
);
3564 if (!e_upstream_name
|| !e_downstream_name
|| !arg
) {
3570 ret
= bt_value_array_append_string_element(run_args
, "--connect");
3577 g_string_append(arg
, e_upstream_name
->str
);
3578 g_string_append_c(arg
, ':');
3579 g_string_append(arg
, e_downstream_name
->str
);
3580 ret
= bt_value_array_append_string_element(run_args
, arg
->str
);
3589 g_string_free(arg
, TRUE
);
3592 if (e_upstream_name
) {
3593 g_string_free(e_upstream_name
, TRUE
);
3596 if (e_downstream_name
) {
3597 g_string_free(e_downstream_name
, TRUE
);
3604 * Appends the run command's --connect options for the convert command.
3607 int convert_auto_connect(bt_value
*run_args
,
3608 GList
*source_names
, GList
*filter_names
,
3612 GList
*source_at
= source_names
;
3613 GList
*filter_at
= filter_names
;
3615 GList
*sink_at
= sink_names
;
3617 BT_ASSERT(source_names
);
3618 BT_ASSERT(filter_names
);
3619 BT_ASSERT(sink_names
);
3621 /* Connect all sources to the first filter */
3622 for (source_at
= source_names
; source_at
!= NULL
; source_at
= g_list_next(source_at
)) {
3623 GString
*source_name
= source_at
->data
;
3624 GString
*filter_name
= filter_at
->data
;
3626 ret
= append_connect_arg(run_args
, source_name
->str
,
3633 filter_prev
= filter_at
;
3634 filter_at
= g_list_next(filter_at
);
3636 /* Connect remaining filters */
3637 for (; filter_at
!= NULL
; filter_prev
= filter_at
, filter_at
= g_list_next(filter_at
)) {
3638 GString
*filter_name
= filter_at
->data
;
3639 GString
*filter_prev_name
= filter_prev
->data
;
3641 ret
= append_connect_arg(run_args
, filter_prev_name
->str
,
3648 /* Connect last filter to all sinks */
3649 for (sink_at
= sink_names
; sink_at
!= NULL
; sink_at
= g_list_next(sink_at
)) {
3650 GString
*filter_name
= filter_prev
->data
;
3651 GString
*sink_name
= sink_at
->data
;
3653 ret
= append_connect_arg(run_args
, filter_name
->str
,
3670 int split_timerange(const char *arg
, char **begin
, char **end
)
3673 const char *ch
= arg
;
3675 GString
*g_begin
= NULL
;
3676 GString
*g_end
= NULL
;
3684 g_begin
= bt_common_string_until(ch
, "", ",", &end_pos
);
3685 if (!g_begin
|| ch
[end_pos
] != ',' || g_begin
->len
== 0) {
3691 g_end
= bt_common_string_until(ch
, "", "]", &end_pos
);
3692 if (!g_end
|| g_end
->len
== 0) {
3698 *begin
= g_begin
->str
;
3700 g_string_free(g_begin
, FALSE
);
3701 g_string_free(g_end
, FALSE
);
3711 g_string_free(g_begin
, TRUE
);
3715 g_string_free(g_end
, TRUE
);
3722 int g_list_prepend_gstring(GList
**list
, const char *string
)
3725 GString
*gs
= g_string_new(string
);
3734 *list
= g_list_prepend(*list
, gs
);
3741 * Creates a Babeltrace config object from the arguments of a convert
3744 * *retcode is set to the appropriate exit code to use.
3747 struct bt_config
*bt_config_convert_from_args(int argc
, const char *argv
[],
3748 int *retcode
, bool force_omit_system_plugin_path
,
3749 bool force_omit_home_plugin_path
,
3750 const bt_value
*initial_plugin_paths
, int *default_log_level
)
3752 poptContext pc
= NULL
;
3754 enum bt_config_component_dest cur_comp_dest
=
3755 BT_CONFIG_COMPONENT_DEST_UNKNOWN
;
3757 struct bt_config
*cfg
= NULL
;
3758 bool got_input_format_opt
= false;
3759 bool got_output_format_opt
= false;
3760 bool trimmer_has_begin
= false;
3761 bool trimmer_has_end
= false;
3762 bool stream_intersection_mode
= false;
3763 GString
*cur_name
= NULL
;
3764 GString
*cur_name_prefix
= NULL
;
3765 const char *leftover
= NULL
;
3766 bool print_run_args
= false;
3767 bool print_run_args_0
= false;
3768 bool print_ctf_metadata
= false;
3769 bt_value
*run_args
= NULL
;
3770 bt_value
*all_names
= NULL
;
3771 GList
*source_names
= NULL
;
3772 GList
*filter_names
= NULL
;
3773 GList
*sink_names
= NULL
;
3774 bt_value
*leftovers
= NULL
;
3775 struct implicit_component_args implicit_ctf_input_args
= { 0 };
3776 struct implicit_component_args implicit_ctf_output_args
= { 0 };
3777 struct implicit_component_args implicit_lttng_live_args
= { 0 };
3778 struct implicit_component_args implicit_dummy_args
= { 0 };
3779 struct implicit_component_args implicit_text_args
= { 0 };
3780 struct implicit_component_args implicit_debug_info_args
= { 0 };
3781 struct implicit_component_args implicit_muxer_args
= { 0 };
3782 struct implicit_component_args implicit_trimmer_args
= { 0 };
3783 bt_value
*plugin_paths
;
3784 char error_buf
[256] = { 0 };
3786 struct bt_common_lttng_live_url_parts lttng_live_url_parts
= { 0 };
3787 char *output
= NULL
;
3789 (void) bt_value_copy(initial_plugin_paths
, &plugin_paths
);
3794 print_convert_usage(stdout
);
3799 if (init_implicit_component_args(&implicit_ctf_input_args
,
3800 "source.ctf.fs", false)) {
3804 if (init_implicit_component_args(&implicit_ctf_output_args
,
3805 "sink.ctf.fs", false)) {
3809 if (init_implicit_component_args(&implicit_lttng_live_args
,
3810 "source.ctf.lttng-live", false)) {
3814 if (init_implicit_component_args(&implicit_text_args
,
3815 "sink.text.pretty", false)) {
3819 if (init_implicit_component_args(&implicit_dummy_args
,
3820 "sink.utils.dummy", false)) {
3824 if (init_implicit_component_args(&implicit_debug_info_args
,
3825 "filter.lttng-utils.debug-info", false)) {
3829 if (init_implicit_component_args(&implicit_muxer_args
,
3830 "filter.utils.muxer", true)) {
3834 if (init_implicit_component_args(&implicit_trimmer_args
,
3835 "filter.utils.trimmer", false)) {
3839 all_names
= bt_value_map_create();
3845 run_args
= bt_value_array_create();
3851 cur_name
= g_string_new(NULL
);
3857 cur_name_prefix
= g_string_new(NULL
);
3858 if (!cur_name_prefix
) {
3863 ret
= append_env_var_plugin_paths(plugin_paths
);
3868 leftovers
= bt_value_array_create();
3875 * First pass: collect all arguments which need to be passed
3876 * as is to the run command. This pass can also add --name
3877 * arguments if needed to automatically name unnamed component
3878 * instances. Also it does the following transformations:
3880 * --path=PATH -> --params=path="PATH"
3881 * --url=URL -> --params=url="URL"
3883 * Also it appends the plugin paths of --plugin-path to
3886 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
3887 convert_long_options
, 0);
3889 printf_err("Cannot get popt context\n");
3893 poptReadDefaultConfig(pc
, 0);
3895 while ((opt
= poptGetNextOpt(pc
)) > 0) {
3897 char *plugin_name
= NULL
;
3898 char *comp_cls_name
= NULL
;
3900 arg
= poptGetOptArg(pc
);
3905 bt_component_class_type type
;
3906 const char *type_prefix
;
3908 /* Append current component's name if needed */
3909 ret
= convert_append_name_param(cur_comp_dest
, cur_name
,
3910 cur_name_prefix
, run_args
, all_names
,
3911 &source_names
, &filter_names
, &sink_names
);
3916 /* Parse the argument */
3917 plugin_comp_cls_names(arg
, &name
, &plugin_name
,
3918 &comp_cls_name
, &type
);
3919 if (!plugin_name
|| !comp_cls_name
) {
3920 printf_err("Invalid format for --component option's argument:\n %s\n",
3926 g_string_assign(cur_name
, name
);
3928 g_string_assign(cur_name
, "");
3932 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
3933 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
3934 type_prefix
= "source";
3936 case BT_COMPONENT_CLASS_TYPE_FILTER
:
3937 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_FILTER
;
3938 type_prefix
= "filter";
3940 case BT_COMPONENT_CLASS_TYPE_SINK
:
3941 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
3942 type_prefix
= "sink";
3948 if (bt_value_array_append_string_element(run_args
,
3954 if (bt_value_array_append_string_element(run_args
, arg
)) {
3959 g_string_assign(cur_name_prefix
, "");
3960 g_string_append_printf(cur_name_prefix
, "%s.%s.%s",
3961 type_prefix
, plugin_name
, comp_cls_name
);
3964 free(comp_cls_name
);
3967 comp_cls_name
= NULL
;
3971 if (cur_name_prefix
->len
== 0) {
3972 printf_err("No current component of which to set parameters:\n %s\n",
3977 if (bt_value_array_append_string_element(run_args
,
3983 if (bt_value_array_append_string_element(run_args
, arg
)) {
3989 if (cur_name_prefix
->len
== 0) {
3990 printf_err("No current component of which to set `path` parameter:\n %s\n",
3995 if (append_string_parameter_to_args(run_args
, "path", arg
)) {
4000 if (cur_name_prefix
->len
== 0) {
4001 printf_err("No current component of which to set `url` parameter:\n %s\n",
4007 if (append_string_parameter_to_args(run_args
, "url", arg
)) {
4012 if (cur_name_prefix
->len
== 0) {
4013 printf_err("No current component to name:\n %s\n",
4018 if (bt_value_array_append_string_element(run_args
, "--name")) {
4023 if (bt_value_array_append_string_element(run_args
, arg
)) {
4028 g_string_assign(cur_name
, arg
);
4031 if (cur_name_prefix
->len
== 0) {
4032 printf_err("No current component to assign a log level to:\n %s\n",
4037 if (bt_value_array_append_string_element(run_args
, "--log-level")) {
4042 if (bt_value_array_append_string_element(run_args
, arg
)) {
4048 case OPT_OMIT_HOME_PLUGIN_PATH
:
4049 force_omit_home_plugin_path
= true;
4051 if (bt_value_array_append_string_element(run_args
,
4052 "--omit-home-plugin-path")) {
4057 case OPT_RETRY_DURATION
:
4058 if (bt_value_array_append_string_element(run_args
,
4059 "--retry-duration")) {
4064 if (bt_value_array_append_string_element(run_args
, arg
)) {
4069 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
4070 force_omit_system_plugin_path
= true;
4072 if (bt_value_array_append_string_element(run_args
,
4073 "--omit-system-plugin-path")) {
4078 case OPT_PLUGIN_PATH
:
4079 if (bt_config_append_plugin_paths_check_setuid_setgid(
4080 plugin_paths
, arg
)) {
4084 if (bt_value_array_append_string_element(run_args
,
4090 if (bt_value_array_append_string_element(run_args
, arg
)) {
4096 print_convert_usage(stdout
);
4098 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4101 case OPT_CLOCK_CYCLES
:
4102 case OPT_CLOCK_DATE
:
4103 case OPT_CLOCK_FORCE_CORRELATE
:
4105 case OPT_CLOCK_OFFSET
:
4106 case OPT_CLOCK_OFFSET_NS
:
4107 case OPT_CLOCK_SECONDS
:
4110 case OPT_DEBUG_INFO
:
4111 case OPT_DEBUG_INFO_DIR
:
4112 case OPT_DEBUG_INFO_FULL_PATH
:
4113 case OPT_DEBUG_INFO_TARGET_PREFIX
:
4116 case OPT_INPUT_FORMAT
:
4119 case OPT_OUTPUT_FORMAT
:
4122 case OPT_RUN_ARGS_0
:
4123 case OPT_STREAM_INTERSECTION
:
4126 /* Ignore in this pass */
4129 printf_err("Unknown command-line option specified (option code %d)\n",
4138 /* Append current component's name if needed */
4139 ret
= convert_append_name_param(cur_comp_dest
, cur_name
,
4140 cur_name_prefix
, run_args
, all_names
, &source_names
,
4141 &filter_names
, &sink_names
);
4146 /* Check for option parsing error */
4148 printf_err("While parsing command-line options, at option %s: %s\n",
4149 poptBadOption(pc
, 0), poptStrerror(opt
));
4153 poptFreeContext(pc
);
4158 * Second pass: transform the convert-specific options and
4159 * arguments into implicit component instances for the run
4162 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
4163 convert_long_options
, 0);
4165 printf_err("Cannot get popt context\n");
4169 poptReadDefaultConfig(pc
, 0);
4171 while ((opt
= poptGetNextOpt(pc
)) > 0) {
4172 arg
= poptGetOptArg(pc
);
4176 if (trimmer_has_begin
) {
4177 printf("At --begin option: --begin or --timerange option already specified\n %s\n",
4182 trimmer_has_begin
= true;
4183 ret
= append_implicit_component_extra_param(
4184 &implicit_trimmer_args
, "begin", arg
);
4185 implicit_trimmer_args
.exists
= true;
4191 if (trimmer_has_end
) {
4192 printf("At --end option: --end or --timerange option already specified\n %s\n",
4197 trimmer_has_end
= true;
4198 ret
= append_implicit_component_extra_param(
4199 &implicit_trimmer_args
, "end", arg
);
4200 implicit_trimmer_args
.exists
= true;
4210 if (trimmer_has_begin
|| trimmer_has_end
) {
4211 printf("At --timerange option: --begin, --end, or --timerange option already specified\n %s\n",
4216 ret
= split_timerange(arg
, &begin
, &end
);
4218 printf_err("Invalid --timerange option's argument: expecting BEGIN,END or [BEGIN,END]:\n %s\n",
4223 ret
= append_implicit_component_extra_param(
4224 &implicit_trimmer_args
, "begin", begin
);
4225 ret
|= append_implicit_component_extra_param(
4226 &implicit_trimmer_args
, "end", end
);
4227 implicit_trimmer_args
.exists
= true;
4235 case OPT_CLOCK_CYCLES
:
4236 append_implicit_component_param(
4237 &implicit_text_args
, "clock-cycles", "yes");
4238 implicit_text_args
.exists
= true;
4240 case OPT_CLOCK_DATE
:
4241 append_implicit_component_param(
4242 &implicit_text_args
, "clock-date", "yes");
4243 implicit_text_args
.exists
= true;
4245 case OPT_CLOCK_FORCE_CORRELATE
:
4246 append_implicit_component_param(
4247 &implicit_muxer_args
,
4248 "assume-absolute-clock-classes", "yes");
4251 append_implicit_component_param(
4252 &implicit_text_args
, "clock-gmt", "yes");
4253 append_implicit_component_param(
4254 &implicit_trimmer_args
, "gmt", "yes");
4255 implicit_text_args
.exists
= true;
4257 case OPT_CLOCK_OFFSET
:
4258 implicit_ctf_input_args
.exists
= true;
4259 append_implicit_component_param(
4260 &implicit_ctf_input_args
,
4261 "clock-class-offset-s", arg
);
4263 case OPT_CLOCK_OFFSET_NS
:
4264 implicit_ctf_input_args
.exists
= true;
4265 append_implicit_component_param(
4266 &implicit_ctf_input_args
,
4267 "clock-class-offset-ns", arg
);
4269 case OPT_CLOCK_SECONDS
:
4270 append_implicit_component_param(
4271 &implicit_text_args
, "clock-seconds", "yes");
4272 implicit_text_args
.exists
= true;
4275 implicit_text_args
.exists
= true;
4276 ret
= append_implicit_component_extra_param(
4277 &implicit_text_args
, "color", arg
);
4282 case OPT_DEBUG_INFO
:
4283 implicit_debug_info_args
.exists
= true;
4285 case OPT_DEBUG_INFO_DIR
:
4286 implicit_debug_info_args
.exists
= true;
4287 ret
= append_implicit_component_extra_param(
4288 &implicit_debug_info_args
, "debug-info-dir", arg
);
4293 case OPT_DEBUG_INFO_FULL_PATH
:
4294 implicit_debug_info_args
.exists
= true;
4295 append_implicit_component_param(
4296 &implicit_debug_info_args
, "full-path", "yes");
4298 case OPT_DEBUG_INFO_TARGET_PREFIX
:
4299 implicit_debug_info_args
.exists
= true;
4300 ret
= append_implicit_component_extra_param(
4301 &implicit_debug_info_args
,
4302 "target-prefix", arg
);
4309 bt_value
*fields
= fields_from_arg(arg
);
4315 implicit_text_args
.exists
= true;
4316 ret
= insert_flat_params_from_array(
4317 implicit_text_args
.params_arg
,
4319 bt_value_put_ref(fields
);
4327 bt_value
*names
= names_from_arg(arg
);
4333 implicit_text_args
.exists
= true;
4334 ret
= insert_flat_params_from_array(
4335 implicit_text_args
.params_arg
,
4337 bt_value_put_ref(names
);
4344 append_implicit_component_param(
4345 &implicit_text_args
, "no-delta", "yes");
4346 implicit_text_args
.exists
= true;
4348 case OPT_INPUT_FORMAT
:
4349 if (got_input_format_opt
) {
4350 printf_err("Duplicate --input-format option\n");
4354 got_input_format_opt
= true;
4356 if (strcmp(arg
, "ctf") == 0) {
4357 implicit_ctf_input_args
.exists
= true;
4358 } else if (strcmp(arg
, "lttng-live") == 0) {
4359 implicit_lttng_live_args
.exists
= true;
4361 printf_err("Unknown legacy input format:\n %s\n",
4366 case OPT_OUTPUT_FORMAT
:
4367 if (got_output_format_opt
) {
4368 printf_err("Duplicate --output-format option\n");
4372 got_output_format_opt
= true;
4374 if (strcmp(arg
, "text") == 0) {
4375 implicit_text_args
.exists
= true;
4376 } else if (strcmp(arg
, "ctf") == 0) {
4377 implicit_ctf_output_args
.exists
= true;
4378 } else if (strcmp(arg
, "dummy") == 0) {
4379 implicit_dummy_args
.exists
= true;
4380 } else if (strcmp(arg
, "ctf-metadata") == 0) {
4381 print_ctf_metadata
= true;
4383 printf_err("Unknown legacy output format:\n %s\n",
4390 printf_err("Duplicate --output option\n");
4394 output
= strdup(arg
);
4401 if (print_run_args_0
) {
4402 printf_err("Cannot specify --run-args and --run-args-0\n");
4406 print_run_args
= true;
4408 case OPT_RUN_ARGS_0
:
4409 if (print_run_args
) {
4410 printf_err("Cannot specify --run-args and --run-args-0\n");
4414 print_run_args_0
= true;
4416 case OPT_STREAM_INTERSECTION
:
4418 * Applies to all traces implementing the trace-info
4421 stream_intersection_mode
= true;
4424 if (*default_log_level
!= BT_LOG_TRACE
&&
4425 *default_log_level
!= BT_LOG_DEBUG
) {
4426 *default_log_level
= BT_LOG_INFO
;
4430 *default_log_level
= BT_LOG_TRACE
;
4440 /* Check for option parsing error */
4442 printf_err("While parsing command-line options, at option %s: %s\n",
4443 poptBadOption(pc
, 0), poptStrerror(opt
));
4448 * Legacy behaviour: --verbose used to make the `text` output
4449 * format print more information. --verbose is now equivalent to
4450 * the INFO log level, which is why we compare to `BT_LOG_INFO`
4453 if (*default_log_level
== BT_LOG_INFO
) {
4454 append_implicit_component_param(&implicit_text_args
,
4459 * Append home and system plugin paths now that we possibly got
4462 if (append_home_and_system_plugin_paths(plugin_paths
,
4463 force_omit_system_plugin_path
,
4464 force_omit_home_plugin_path
)) {
4468 /* Consume and keep leftover arguments */
4469 while ((leftover
= poptGetArg(pc
))) {
4470 bt_value_status status
= bt_value_array_append_string_element(leftovers
, leftover
);
4471 if (status
!= BT_VALUE_STATUS_OK
) {
4477 /* Print CTF metadata or print LTTng live sessions */
4478 if (print_ctf_metadata
) {
4479 const bt_value
*bt_val_leftover
;
4481 if (bt_value_array_is_empty(leftovers
)) {
4482 printf_err("--output-format=ctf-metadata specified without a path\n");
4486 if (bt_value_array_get_size(leftovers
) > 1) {
4487 printf_err("Too many paths specified for --output-format=ctf-metadata\n");
4491 cfg
= bt_config_print_ctf_metadata_create(plugin_paths
);
4496 bt_val_leftover
= bt_value_array_borrow_element_by_index_const(leftovers
, 0);
4497 g_string_assign(cfg
->cmd_data
.print_ctf_metadata
.path
,
4498 bt_value_string_get(bt_val_leftover
));
4502 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
4510 * If -o ctf was specified, make sure an output path (--output)
4511 * was also specified. --output does not imply -o ctf because
4512 * it's also used for the default, implicit -o text if -o ctf
4515 if (implicit_ctf_output_args
.exists
) {
4517 printf_err("--output-format=ctf specified without --output (trace output path)\n");
4522 * At this point we know that -o ctf AND --output were
4523 * specified. Make sure that no options were specified
4524 * which would imply -o text because --output would be
4525 * ambiguous in this case. For example, this is wrong:
4527 * babeltrace2 --names=all -o ctf --output=/tmp/path my-trace
4529 * because --names=all implies -o text, and --output
4530 * could apply to both the sink.text.pretty and
4531 * sink.ctf.fs implicit components.
4533 if (implicit_text_args
.exists
) {
4534 printf_err("Ambiguous --output option: --output-format=ctf specified but another option implies --output-format=text\n");
4540 * If -o dummy and -o ctf were not specified, and if there are
4541 * no explicit sink components, then use an implicit
4542 * `sink.text.pretty` component.
4544 if (!implicit_dummy_args
.exists
&& !implicit_ctf_output_args
.exists
&&
4546 implicit_text_args
.exists
= true;
4550 * Set implicit `sink.text.pretty` or `sink.ctf.fs` component's
4551 * `path` parameter if --output was specified.
4554 if (implicit_text_args
.exists
) {
4555 append_implicit_component_extra_param(&implicit_text_args
,
4557 } else if (implicit_ctf_output_args
.exists
) {
4558 append_implicit_component_extra_param(&implicit_ctf_output_args
,
4563 /* Decide where the leftover argument(s) go */
4564 if (bt_value_array_get_size(leftovers
) > 0) {
4565 if (implicit_lttng_live_args
.exists
) {
4566 const bt_value
*bt_val_leftover
;
4568 if (bt_value_array_get_size(leftovers
) > 1) {
4569 printf_err("Too many URLs specified for --output-format=lttng-live\n");
4573 bt_val_leftover
= bt_value_array_borrow_element_by_index_const(leftovers
, 0);
4574 lttng_live_url_parts
=
4575 bt_common_parse_lttng_live_url(bt_value_string_get(bt_val_leftover
),
4576 error_buf
, sizeof(error_buf
));
4577 if (!lttng_live_url_parts
.proto
) {
4578 printf_err("Invalid LTTng live URL format: %s\n",
4583 if (!lttng_live_url_parts
.session_name
) {
4584 /* Print LTTng live sessions */
4585 cfg
= bt_config_print_lttng_live_sessions_create(
4591 g_string_assign(cfg
->cmd_data
.print_lttng_live_sessions
.url
,
4592 bt_value_string_get(bt_val_leftover
));
4596 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
4603 ret
= append_implicit_component_extra_param(
4604 &implicit_lttng_live_args
, "url",
4605 bt_value_string_get(bt_val_leftover
));
4610 ret
= append_implicit_component_extra_param(
4611 &implicit_lttng_live_args
,
4612 "session-not-found-action", "end");
4618 * Create one source.ctf.fs component, pass it an array
4619 * with the leftovers.
4620 * Note that it still has to be named later.
4622 implicit_ctf_input_args
.exists
= true;
4623 ret
= append_parameter_to_args(implicit_ctf_input_args
.extra_params
,
4624 "paths", leftovers
);
4632 * Ensure mutual exclusion between implicit `source.ctf.fs` and
4633 * `source.ctf.lttng-live` components.
4635 if (implicit_ctf_input_args
.exists
&& implicit_lttng_live_args
.exists
) {
4636 printf_err("Cannot create both implicit `%s` and `%s` components\n",
4637 implicit_ctf_input_args
.comp_arg
->str
,
4638 implicit_lttng_live_args
.comp_arg
->str
);
4643 * If the implicit `source.ctf.fs` or `source.ctf.lttng-live`
4644 * components exists, make sure there's at least one leftover
4645 * (which is the path or URL).
4647 if (implicit_ctf_input_args
.exists
&& bt_value_array_is_empty(leftovers
)) {
4648 printf_err("Missing path for implicit `%s` component\n",
4649 implicit_ctf_input_args
.comp_arg
->str
);
4653 if (implicit_lttng_live_args
.exists
&& bt_value_array_is_empty(leftovers
)) {
4654 printf_err("Missing URL for implicit `%s` component\n",
4655 implicit_lttng_live_args
.comp_arg
->str
);
4659 /* Assign names to implicit components */
4660 ret
= assign_name_to_implicit_component(&implicit_ctf_input_args
,
4661 "source-ctf-fs", all_names
, &source_names
, true);
4666 ret
= assign_name_to_implicit_component(&implicit_lttng_live_args
,
4667 "lttng-live", all_names
, &source_names
, true);
4672 ret
= assign_name_to_implicit_component(&implicit_text_args
,
4673 "pretty", all_names
, &sink_names
, true);
4678 ret
= assign_name_to_implicit_component(&implicit_ctf_output_args
,
4679 "sink-ctf-fs", all_names
, &sink_names
, true);
4684 ret
= assign_name_to_implicit_component(&implicit_dummy_args
,
4685 "dummy", all_names
, &sink_names
, true);
4690 ret
= assign_name_to_implicit_component(&implicit_muxer_args
,
4691 "muxer", all_names
, NULL
, false);
4696 ret
= assign_name_to_implicit_component(&implicit_trimmer_args
,
4697 "trimmer", all_names
, NULL
, false);
4702 ret
= assign_name_to_implicit_component(&implicit_debug_info_args
,
4703 "debug-info", all_names
, NULL
, false);
4708 /* Make sure there's at least one source and one sink */
4709 if (!source_names
) {
4710 printf_err("No source component\n");
4715 printf_err("No sink component\n");
4720 * Prepend the muxer, the trimmer, and the debug info to the
4721 * filter chain so that we have:
4723 * sources -> muxer -> [trimmer] -> [debug info] ->
4724 * [user filters] -> sinks
4726 if (implicit_debug_info_args
.exists
) {
4727 if (g_list_prepend_gstring(&filter_names
,
4728 implicit_debug_info_args
.name_arg
->str
)) {
4733 if (implicit_trimmer_args
.exists
) {
4734 if (g_list_prepend_gstring(&filter_names
,
4735 implicit_trimmer_args
.name_arg
->str
)) {
4740 if (g_list_prepend_gstring(&filter_names
,
4741 implicit_muxer_args
.name_arg
->str
)) {
4746 * Append the equivalent run arguments for the implicit
4749 ret
= append_run_args_for_implicit_component(&implicit_ctf_input_args
, run_args
);
4754 ret
= append_run_args_for_implicit_component(&implicit_lttng_live_args
,
4760 ret
= append_run_args_for_implicit_component(&implicit_text_args
,
4766 ret
= append_run_args_for_implicit_component(&implicit_ctf_output_args
,
4772 ret
= append_run_args_for_implicit_component(&implicit_dummy_args
,
4778 ret
= append_run_args_for_implicit_component(&implicit_muxer_args
,
4784 ret
= append_run_args_for_implicit_component(&implicit_trimmer_args
,
4790 ret
= append_run_args_for_implicit_component(&implicit_debug_info_args
,
4796 /* Auto-connect components */
4797 ret
= convert_auto_connect(run_args
, source_names
, filter_names
,
4800 printf_err("Cannot auto-connect components\n");
4805 * We have all the run command arguments now. Depending on
4806 * --run-args, we pass this to the run command or print them
4809 if (print_run_args
|| print_run_args_0
) {
4810 if (stream_intersection_mode
) {
4811 printf_err("Cannot specify --stream-intersection with --run-args or --run-args-0\n");
4815 for (i
= 0; i
< bt_value_array_get_size(run_args
); i
++) {
4816 const bt_value
*arg_value
=
4817 bt_value_array_borrow_element_by_index(run_args
,
4820 GString
*quoted
= NULL
;
4821 const char *arg_to_print
;
4823 BT_ASSERT(arg_value
);
4824 arg
= bt_value_string_get(arg_value
);
4826 if (print_run_args
) {
4827 quoted
= bt_common_shell_quote(arg
, true);
4832 arg_to_print
= quoted
->str
;
4837 printf("%s", arg_to_print
);
4840 g_string_free(quoted
, TRUE
);
4843 if (i
< bt_value_array_get_size(run_args
) - 1) {
4844 if (print_run_args
) {
4853 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4858 * If the log level is still unset at this point, set it to
4859 * the program's default.
4861 if (*default_log_level
< 0) {
4862 *default_log_level
= cli_default_log_level
;
4865 cfg
= bt_config_run_from_args_array(run_args
, retcode
,
4866 force_omit_system_plugin_path
,
4867 force_omit_home_plugin_path
,
4868 initial_plugin_paths
, *default_log_level
);
4873 cfg
->cmd_data
.run
.stream_intersection_mode
= stream_intersection_mode
;
4878 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4882 * If the log level is still unset at this point, set it to
4883 * the program's default.
4885 if (*default_log_level
< 0) {
4886 *default_log_level
= cli_default_log_level
;
4890 poptFreeContext(pc
);
4897 g_string_free(cur_name
, TRUE
);
4900 if (cur_name_prefix
) {
4901 g_string_free(cur_name_prefix
, TRUE
);
4904 bt_value_put_ref(run_args
);
4905 bt_value_put_ref(all_names
);
4906 destroy_glist_of_gstring(source_names
);
4907 destroy_glist_of_gstring(filter_names
);
4908 destroy_glist_of_gstring(sink_names
);
4909 bt_value_put_ref(leftovers
);
4910 finalize_implicit_component_args(&implicit_ctf_input_args
);
4911 finalize_implicit_component_args(&implicit_ctf_output_args
);
4912 finalize_implicit_component_args(&implicit_lttng_live_args
);
4913 finalize_implicit_component_args(&implicit_dummy_args
);
4914 finalize_implicit_component_args(&implicit_text_args
);
4915 finalize_implicit_component_args(&implicit_debug_info_args
);
4916 finalize_implicit_component_args(&implicit_muxer_args
);
4917 finalize_implicit_component_args(&implicit_trimmer_args
);
4918 bt_value_put_ref(plugin_paths
);
4919 bt_common_destroy_lttng_live_url_parts(<tng_live_url_parts
);
4924 * Prints the Babeltrace 2.x general usage.
4927 void print_gen_usage(FILE *fp
)
4929 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] [COMMAND] [COMMAND ARGUMENTS]\n");
4931 fprintf(fp
, "General options:\n");
4933 fprintf(fp
, " -d, --debug Enable debug mode (same as --log-level=V)\n");
4934 fprintf(fp
, " -h, --help Show this help and quit\n");
4935 fprintf(fp
, " -l, --log-level=LVL Set the default log level to LVL (`N`, `V`, `D`,\n");
4936 fprintf(fp
, " `I`, `W` (default), `E`, or `F`)\n");
4937 fprintf(fp
, " -v, --verbose Enable verbose mode (same as --log-level=I)\n");
4938 fprintf(fp
, " -V, --version Show version and quit\n");
4940 fprintf(fp
, "Available commands:\n");
4942 fprintf(fp
, " convert Convert and trim traces (default)\n");
4943 fprintf(fp
, " help Get help for a plugin or a component class\n");
4944 fprintf(fp
, " list-plugins List available plugins and their content\n");
4945 fprintf(fp
, " query Query objects from a component class\n");
4946 fprintf(fp
, " run Build a processing graph and run it\n");
4948 fprintf(fp
, "Use `babeltrace2 COMMAND --help` to show the help of COMMAND.\n");
4951 struct bt_config
*bt_config_cli_args_create(int argc
, const char *argv
[],
4952 int *retcode
, bool force_omit_system_plugin_path
,
4953 bool force_omit_home_plugin_path
,
4954 const bt_value
*initial_plugin_paths
)
4956 struct bt_config
*config
= NULL
;
4958 const char **command_argv
= NULL
;
4959 int command_argc
= -1;
4960 const char *command_name
= NULL
;
4961 int default_log_level
= -1;
4964 COMMAND_TYPE_NONE
= -1,
4965 COMMAND_TYPE_RUN
= 0,
4966 COMMAND_TYPE_CONVERT
,
4967 COMMAND_TYPE_LIST_PLUGINS
,
4970 } command_type
= COMMAND_TYPE_NONE
;
4974 if (!initial_plugin_paths
) {
4975 initial_plugin_paths
= bt_value_array_create();
4976 if (!initial_plugin_paths
) {
4981 bt_value_get_ref(initial_plugin_paths
);
4987 print_gen_usage(stdout
);
4991 for (i
= 1; i
< argc
; i
++) {
4992 const char *cur_arg
= argv
[i
];
4993 const char *next_arg
= i
== (argc
- 1) ? NULL
: argv
[i
+ 1];
4995 if (strcmp(cur_arg
, "-d") == 0 ||
4996 strcmp(cur_arg
, "--debug") == 0) {
4997 default_log_level
= BT_LOG_TRACE
;
4998 } else if (strcmp(cur_arg
, "-v") == 0 ||
4999 strcmp(cur_arg
, "--verbose") == 0) {
5000 if (default_log_level
!= BT_LOG_TRACE
&&
5001 default_log_level
!= BT_LOG_DEBUG
) {
5003 * Legacy: do not override a previous
5004 * --debug because --verbose and --debug
5005 * can be specified together (in this
5006 * case we want the lowest log level to
5009 default_log_level
= BT_LOG_INFO
;
5011 } else if (strcmp(cur_arg
, "--log-level") == 0 ||
5012 strcmp(cur_arg
, "-l") == 0) {
5014 printf_err("Missing log level value for --log-level option\n");
5020 bt_log_get_level_from_string(next_arg
);
5021 if (default_log_level
< 0) {
5022 printf_err("Invalid argument for --log-level option:\n %s\n",
5029 } else if (strncmp(cur_arg
, "--log-level=", 12) == 0) {
5030 const char *arg
= &cur_arg
[12];
5032 default_log_level
= bt_log_get_level_from_string(arg
);
5033 if (default_log_level
< 0) {
5034 printf_err("Invalid argument for --log-level option:\n %s\n",
5039 } else if (strncmp(cur_arg
, "-l", 2) == 0) {
5040 const char *arg
= &cur_arg
[2];
5042 default_log_level
= bt_log_get_level_from_string(arg
);
5043 if (default_log_level
< 0) {
5044 printf_err("Invalid argument for --log-level option:\n %s\n",
5049 } else if (strcmp(cur_arg
, "-V") == 0 ||
5050 strcmp(cur_arg
, "--version") == 0) {
5053 } else if (strcmp(cur_arg
, "-h") == 0 ||
5054 strcmp(cur_arg
, "--help") == 0) {
5055 print_gen_usage(stdout
);
5059 * First unknown argument: is it a known command
5062 command_argv
= &argv
[i
];
5063 command_argc
= argc
- i
;
5065 if (strcmp(cur_arg
, "convert") == 0) {
5066 command_type
= COMMAND_TYPE_CONVERT
;
5067 } else if (strcmp(cur_arg
, "list-plugins") == 0) {
5068 command_type
= COMMAND_TYPE_LIST_PLUGINS
;
5069 } else if (strcmp(cur_arg
, "help") == 0) {
5070 command_type
= COMMAND_TYPE_HELP
;
5071 } else if (strcmp(cur_arg
, "query") == 0) {
5072 command_type
= COMMAND_TYPE_QUERY
;
5073 } else if (strcmp(cur_arg
, "run") == 0) {
5074 command_type
= COMMAND_TYPE_RUN
;
5077 * Unknown argument, but not a known
5078 * command name: assume the default
5079 * `convert` command.
5081 command_type
= COMMAND_TYPE_CONVERT
;
5082 command_name
= "convert";
5083 command_argv
= &argv
[i
- 1];
5084 command_argc
= argc
- i
+ 1;
5090 if (command_type
== COMMAND_TYPE_NONE
) {
5092 * We only got non-help, non-version general options
5093 * like --verbose and --debug, without any other
5094 * arguments, so we can't do anything useful: print the
5097 print_gen_usage(stdout
);
5101 BT_ASSERT(command_argv
);
5102 BT_ASSERT(command_argc
>= 0);
5105 * The convert command can set its own default log level for
5106 * backward compatibility reasons. It only does so if there's no
5107 * log level yet, so do not force one for this command.
5109 if (command_type
!= COMMAND_TYPE_CONVERT
&& default_log_level
< 0) {
5110 /* Default log level */
5111 default_log_level
= cli_default_log_level
;
5114 switch (command_type
) {
5115 case COMMAND_TYPE_RUN
:
5116 config
= bt_config_run_from_args(command_argc
, command_argv
,
5117 retcode
, force_omit_system_plugin_path
,
5118 force_omit_home_plugin_path
, initial_plugin_paths
,
5121 case COMMAND_TYPE_CONVERT
:
5122 config
= bt_config_convert_from_args(command_argc
, command_argv
,
5123 retcode
, force_omit_system_plugin_path
,
5124 force_omit_home_plugin_path
,
5125 initial_plugin_paths
, &default_log_level
);
5127 case COMMAND_TYPE_LIST_PLUGINS
:
5128 config
= bt_config_list_plugins_from_args(command_argc
,
5129 command_argv
, retcode
, force_omit_system_plugin_path
,
5130 force_omit_home_plugin_path
, initial_plugin_paths
);
5132 case COMMAND_TYPE_HELP
:
5133 config
= bt_config_help_from_args(command_argc
,
5134 command_argv
, retcode
, force_omit_system_plugin_path
,
5135 force_omit_home_plugin_path
, initial_plugin_paths
,
5138 case COMMAND_TYPE_QUERY
:
5139 config
= bt_config_query_from_args(command_argc
,
5140 command_argv
, retcode
, force_omit_system_plugin_path
,
5141 force_omit_home_plugin_path
, initial_plugin_paths
,
5149 BT_ASSERT(default_log_level
>= BT_LOG_TRACE
);
5150 config
->log_level
= default_log_level
;
5151 config
->command_name
= command_name
;
5155 bt_value_put_ref(initial_plugin_paths
);