2 * Babeltrace trace converter - parameter parsing
4 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 #include <babeltrace/babeltrace.h>
33 #include <babeltrace/values.h>
36 #include "babeltrace-cfg.h"
39 * Error printf() macro which prepends "Error: " the first time it's
40 * called. This gives a nicer feel than having a bunch of error prefixes
41 * (since the following lines usually describe the error and possible
42 * solutions), or the error prefix just at the end.
44 #define printf_err(fmt, args...) \
46 if (is_first_error) { \
47 fprintf(stderr, "Error: "); \
48 is_first_error = false; \
50 fprintf(stderr, fmt, ##args); \
53 static bool is_first_error
= true;
55 /* INI-style parsing FSM states */
56 enum ini_parsing_fsm_state
{
57 /* Expect a map key (identifier) */
60 /* Expect an equal character ('=') */
66 /* Expect a negative number value */
67 INI_EXPECT_VALUE_NUMBER_NEG
,
69 /* Expect a comma character (',') */
73 /* INI-style parsing state variables */
74 struct ini_parsing_state
{
75 /* Lexical scanner (owned by this) */
78 /* Output map value object being filled (owned by this) */
79 struct bt_value
*params
;
81 /* Next expected FSM state */
82 enum ini_parsing_fsm_state expecting
;
84 /* Last decoded map key (owned by this) */
87 /* Complete INI-style string to parse (not owned by this) */
90 /* Error buffer (not owned by this) */
94 /* Offset option with "is set" boolean */
100 /* Legacy "ctf"/"lttng-live" format options */
101 struct ctf_legacy_opts
{
102 struct offset_opt offset_s
;
103 struct offset_opt offset_ns
;
104 bool stream_intersection
;
107 /* Legacy "text" format options */
108 struct text_legacy_opts
{
110 * output, dbg_info_dir, dbg_info_target_prefix, names,
111 * and fields are owned by this.
114 GString
*dbg_info_dir
;
115 GString
*dbg_info_target_prefix
;
116 struct bt_value
*names
;
117 struct bt_value
*fields
;
125 bool dbg_info_full_path
;
128 /* Legacy input format format */
129 enum legacy_input_format
{
130 LEGACY_INPUT_FORMAT_NONE
= 0,
131 LEGACY_INPUT_FORMAT_CTF
,
132 LEGACY_INPUT_FORMAT_LTTNG_LIVE
,
135 /* Legacy output format format */
136 enum legacy_output_format
{
137 LEGACY_OUTPUT_FORMAT_NONE
= 0,
138 LEGACY_OUTPUT_FORMAT_TEXT
,
139 LEGACY_OUTPUT_FORMAT_CTF_METADATA
,
140 LEGACY_OUTPUT_FORMAT_DUMMY
,
144 * Prints the "out of memory" error.
147 void print_err_oom(void)
149 printf_err("Out of memory\n");
153 * Prints duplicate legacy output format error.
156 void print_err_dup_legacy_output(void)
158 printf_err("More than one legacy output format specified\n");
162 * Prints duplicate legacy input format error.
165 void print_err_dup_legacy_input(void)
167 printf_err("More than one legacy input format specified\n");
171 * Checks if any of the "text" legacy options is set.
174 bool text_legacy_opts_is_any_set(struct text_legacy_opts
*opts
)
176 return (opts
->output
&& opts
->output
->len
> 0) ||
177 (opts
->dbg_info_dir
&& opts
->dbg_info_dir
->len
> 0) ||
178 (opts
->dbg_info_target_prefix
&&
179 opts
->dbg_info_target_prefix
->len
> 0) ||
180 bt_value_array_size(opts
->names
) > 0 ||
181 bt_value_array_size(opts
->fields
) > 0 ||
182 opts
->no_delta
|| opts
->clock_cycles
|| opts
->clock_seconds
||
183 opts
->clock_date
|| opts
->clock_gmt
||
184 opts
->dbg_info_full_path
;
188 * Checks if any of the "ctf" legacy options is set.
191 bool ctf_legacy_opts_is_any_set(struct ctf_legacy_opts
*opts
)
193 return opts
->offset_s
.is_set
|| opts
->offset_ns
.is_set
||
194 opts
->stream_intersection
;
198 * Appends an "expecting token" error to the INI-style parsing state's
202 void ini_append_error_expecting(struct ini_parsing_state
*state
,
203 GScanner
*scanner
, const char *expecting
)
208 g_string_append_printf(state
->ini_error
, "Expecting %s:\n", expecting
);
210 /* Only print error if there's one line */
211 if (strchr(state
->arg
, '\n') != NULL
|| strlen(state
->arg
) == 0) {
215 g_string_append_printf(state
->ini_error
, "\n %s\n", state
->arg
);
216 pos
= g_scanner_cur_position(scanner
) + 4;
218 if (!g_scanner_eof(scanner
)) {
222 for (i
= 0; i
< pos
; ++i
) {
223 g_string_append_printf(state
->ini_error
, " ");
226 g_string_append_printf(state
->ini_error
, "^\n\n");
230 int ini_handle_state(struct ini_parsing_state
*state
)
233 GTokenType token_type
;
234 struct bt_value
*value
= NULL
;
236 token_type
= g_scanner_get_next_token(state
->scanner
);
237 if (token_type
== G_TOKEN_EOF
) {
238 if (state
->expecting
!= INI_EXPECT_COMMA
) {
239 switch (state
->expecting
) {
240 case INI_EXPECT_EQUAL
:
241 ini_append_error_expecting(state
,
242 state
->scanner
, "'='");
244 case INI_EXPECT_VALUE
:
245 case INI_EXPECT_VALUE_NUMBER_NEG
:
246 ini_append_error_expecting(state
,
247 state
->scanner
, "value");
249 case INI_EXPECT_MAP_KEY
:
250 ini_append_error_expecting(state
,
251 state
->scanner
, "unquoted map key");
264 switch (state
->expecting
) {
265 case INI_EXPECT_MAP_KEY
:
266 if (token_type
!= G_TOKEN_IDENTIFIER
) {
267 ini_append_error_expecting(state
, state
->scanner
,
272 free(state
->last_map_key
);
273 state
->last_map_key
=
274 strdup(state
->scanner
->value
.v_identifier
);
275 if (!state
->last_map_key
) {
276 g_string_append(state
->ini_error
,
281 if (bt_value_map_has_key(state
->params
, state
->last_map_key
)) {
282 g_string_append_printf(state
->ini_error
,
283 "Duplicate parameter key: \"%s\"\n",
284 state
->last_map_key
);
288 state
->expecting
= INI_EXPECT_EQUAL
;
290 case INI_EXPECT_EQUAL
:
291 if (token_type
!= G_TOKEN_CHAR
) {
292 ini_append_error_expecting(state
,
293 state
->scanner
, "'='");
297 if (state
->scanner
->value
.v_char
!= '=') {
298 ini_append_error_expecting(state
,
299 state
->scanner
, "'='");
303 state
->expecting
= INI_EXPECT_VALUE
;
305 case INI_EXPECT_VALUE
:
307 switch (token_type
) {
309 if (state
->scanner
->value
.v_char
== '-') {
310 /* Negative number */
312 INI_EXPECT_VALUE_NUMBER_NEG
;
315 ini_append_error_expecting(state
,
316 state
->scanner
, "value");
322 /* Positive integer */
323 uint64_t int_val
= state
->scanner
->value
.v_int64
;
325 if (int_val
> (1ULL << 63) - 1) {
326 g_string_append_printf(state
->ini_error
,
327 "Integer value %" PRIu64
" is outside the range of a 64-bit signed integer\n",
332 value
= bt_value_integer_create_init(
337 /* Positive floating point number */
338 value
= bt_value_float_create_init(
339 state
->scanner
->value
.v_float
);
343 value
= bt_value_string_create_init(
344 state
->scanner
->value
.v_string
);
346 case G_TOKEN_IDENTIFIER
:
349 * Using symbols would be appropriate here,
350 * but said symbols are allowed as map key,
351 * so it's easier to consider everything an
354 * If one of the known symbols is not
355 * recognized here, then fall back to creating
358 const char *id
= state
->scanner
->value
.v_identifier
;
360 if (!strcmp(id
, "null") || !strcmp(id
, "NULL") ||
361 !strcmp(id
, "nul")) {
362 value
= bt_value_null
;
363 } else if (!strcmp(id
, "true") || !strcmp(id
, "TRUE") ||
364 !strcmp(id
, "yes") ||
365 !strcmp(id
, "YES")) {
366 value
= bt_value_bool_create_init(true);
367 } else if (!strcmp(id
, "false") ||
368 !strcmp(id
, "FALSE") ||
371 value
= bt_value_bool_create_init(false);
373 value
= bt_value_string_create_init(id
);
378 /* Unset value variable will trigger the error */
383 ini_append_error_expecting(state
,
384 state
->scanner
, "value");
388 state
->expecting
= INI_EXPECT_COMMA
;
391 case INI_EXPECT_VALUE_NUMBER_NEG
:
393 switch (token_type
) {
396 /* Negative integer */
397 uint64_t int_val
= state
->scanner
->value
.v_int64
;
399 if (int_val
> (1ULL << 63) - 1) {
400 g_string_append_printf(state
->ini_error
,
401 "Integer value -%" PRIu64
" is outside the range of a 64-bit signed integer\n",
406 value
= bt_value_integer_create_init(
407 -((int64_t) int_val
));
411 /* Negative floating point number */
412 value
= bt_value_float_create_init(
413 -state
->scanner
->value
.v_float
);
416 /* Unset value variable will trigger the error */
421 ini_append_error_expecting(state
,
422 state
->scanner
, "value");
426 state
->expecting
= INI_EXPECT_COMMA
;
429 case INI_EXPECT_COMMA
:
430 if (token_type
!= G_TOKEN_CHAR
) {
431 ini_append_error_expecting(state
,
432 state
->scanner
, "','");
436 if (state
->scanner
->value
.v_char
!= ',') {
437 ini_append_error_expecting(state
,
438 state
->scanner
, "','");
442 state
->expecting
= INI_EXPECT_MAP_KEY
;
454 if (bt_value_map_insert(state
->params
,
455 state
->last_map_key
, value
)) {
456 /* Only override return value on error */
467 * Converts an INI-style argument to an equivalent map value object.
469 * Return value is owned by the caller.
472 struct bt_value
*bt_value_from_ini(const char *arg
, GString
*ini_error
)
474 /* Lexical scanner configuration */
475 GScannerConfig scanner_config
= {
476 /* Skip whitespaces */
477 .cset_skip_characters
= " \t\n",
479 /* Identifier syntax is: [a-zA-Z_][a-zA-Z0-9_.:-]* */
480 .cset_identifier_first
=
484 .cset_identifier_nth
=
489 /* "hello" and "Hello" two different keys */
490 .case_sensitive
= TRUE
,
493 .cpair_comment_single
= NULL
,
494 .skip_comment_multi
= TRUE
,
495 .skip_comment_single
= TRUE
,
496 .scan_comment_multi
= FALSE
,
499 * Do scan identifiers, including 1-char identifiers,
500 * but NULL is a normal identifier.
502 .scan_identifier
= TRUE
,
503 .scan_identifier_1char
= TRUE
,
504 .scan_identifier_NULL
= FALSE
,
507 * No specific symbols: null and boolean "symbols" are
508 * scanned as plain identifiers.
510 .scan_symbols
= FALSE
,
511 .symbol_2_token
= FALSE
,
512 .scope_0_fallback
= FALSE
,
515 * Scan "0b"-, "0"-, and "0x"-prefixed integers, but not
516 * integers prefixed with "$".
522 .scan_hex_dollar
= FALSE
,
524 /* Convert scanned numbers to integer tokens */
525 .numbers_2_int
= TRUE
,
527 /* Support both integers and floating-point numbers */
528 .int_2_float
= FALSE
,
530 /* Scan integers as 64-bit signed integers */
533 /* Only scan double-quoted strings */
534 .scan_string_sq
= FALSE
,
535 .scan_string_dq
= TRUE
,
537 /* Do not converter identifiers to string tokens */
538 .identifier_2_string
= FALSE
,
540 /* Scan characters as G_TOKEN_CHAR token */
541 .char_2_token
= FALSE
,
543 struct ini_parsing_state state
= {
546 .expecting
= INI_EXPECT_MAP_KEY
,
548 .ini_error
= ini_error
,
551 state
.params
= bt_value_map_create();
556 state
.scanner
= g_scanner_new(&scanner_config
);
557 if (!state
.scanner
) {
561 /* Let the scan begin */
562 g_scanner_input_text(state
.scanner
, arg
, strlen(arg
));
565 int ret
= ini_handle_state(&state
);
570 } else if (ret
> 0) {
579 BT_PUT(state
.params
);
583 g_scanner_destroy(state
.scanner
);
586 free(state
.last_map_key
);
591 * Returns the parameters map value object from a command-line
592 * source/sink option's argument. arg is the full argument, including
593 * the plugin/component names, the optional colon, and the optional
596 * Return value is owned by the caller.
599 struct bt_value
*bt_value_from_arg(const char *arg
)
601 struct bt_value
*params
= NULL
;
603 const char *params_string
;
604 GString
*ini_error
= NULL
;
606 /* Isolate parameters */
607 colon
= strchr(arg
, ':');
609 /* No colon: empty parameters */
610 params
= bt_value_map_create();
614 params_string
= colon
+ 1;
615 ini_error
= g_string_new(NULL
);
621 /* Try INI-style parsing */
622 params
= bt_value_from_ini(params_string
, ini_error
);
624 printf_err("%s", ini_error
->str
);
630 g_string_free(ini_error
, TRUE
);
636 * Returns the plugin and component names from a command-line
637 * source/sink option's argument. arg is the full argument, including
638 * the plugin/component names, the optional colon, and the optional
641 * On success, both *plugin and *component are not NULL. *plugin
642 * and *component are owned by the caller.
645 void plugin_component_names_from_arg(const char *arg
, char **plugin
,
651 size_t component_len
;
656 dot
= strchr(arg
, '.');
657 if (!dot
|| dot
== arg
) {
661 colon
= strchr(dot
, ':');
666 colon
= arg
+ strlen(arg
);
669 plugin_len
= dot
- arg
;
670 component_len
= colon
- dot
- 1;
671 if (plugin_len
== 0 || component_len
== 0) {
675 *plugin
= malloc(plugin_len
+ 1);
681 (*plugin
)[plugin_len
] = '\0';
682 memcpy(*plugin
, arg
, plugin_len
);
683 *component
= malloc(component_len
+ 1);
689 (*component
)[component_len
] = '\0';
690 memcpy(*component
, dot
+ 1, component_len
);
697 * Prints the Babeltrace version.
700 void print_version(void)
702 puts("Babeltrace " VERSION
);
706 * Prints the legacy, Babeltrace 1.x command usage. Those options are
707 * still compatible in Babeltrace 2.x, but it is recommended to use
708 * the more generic plugin/component parameters instead of those
709 * hard-coded option names.
712 void print_legacy_usage(FILE *fp
)
714 fprintf(fp
, "Usage: babeltrace [OPTIONS] INPUT...\n");
716 fprintf(fp
, "The following options are compatible with the Babeltrace 1.x options:\n");
718 fprintf(fp
, " --help-legacy Show this help\n");
719 fprintf(fp
, " -V, --version Show version\n");
720 fprintf(fp
, " --clock-force-correlate Assume that clocks are inherently correlated\n");
721 fprintf(fp
, " across traces\n");
722 fprintf(fp
, " -d, --debug Enable debug mode\n");
723 fprintf(fp
, " -i, --input-format=FORMAT Input trace format (default: ctf)\n");
724 fprintf(fp
, " -l, --list List available formats\n");
725 fprintf(fp
, " -o, --output-format=FORMAT Output trace format (default: text)\n");
726 fprintf(fp
, " -v, --verbose Enable verbose output\n");
728 fprintf(fp
, " Available input formats: ctf, lttng-live, ctf-metadata\n");
729 fprintf(fp
, " Available output formats: text, dummy\n");
731 fprintf(fp
, "Input formats specific options:\n");
733 fprintf(fp
, " INPUT... Input trace file(s), directory(ies), or URLs\n");
734 fprintf(fp
, " --clock-offset=SEC Set clock offset to SEC seconds\n");
735 fprintf(fp
, " --clock-offset-ns=NS Set clock offset to NS nanoseconds\n");
736 fprintf(fp
, " --stream-intersection Only process events when all streams are active\n");
738 fprintf(fp
, "text output format specific options:\n");
740 fprintf(fp
, " --clock-cycles Print timestamps in clock cycles\n");
741 fprintf(fp
, " --clock-date Print timestamp dates\n");
742 fprintf(fp
, " --clock-gmt Print timestamps in GMT time zone\n");
743 fprintf(fp
, " (default: local time zone)\n");
744 fprintf(fp
, " --clock-seconds Print the timestamps as [SEC.NS]\n");
745 fprintf(fp
, " (default format: [HH:MM:SS.NS])\n");
746 fprintf(fp
, " --debug-info-dir=DIR Search for debug info in directory DIR\n");
747 fprintf(fp
, " (default: \"/usr/lib/debug\")\n");
748 fprintf(fp
, " --debug-info-full-path Show full debug info source and binary paths\n");
749 fprintf(fp
, " --debug-info-target-prefix=DIR Use directory DIR as a prefix when looking\n");
750 fprintf(fp
, " up executables during debug info analysis\n");
751 fprintf(fp
, " (default: \"/usr/lib/debug\")\n");
752 fprintf(fp
, " -f, --fields=NAME[,NAME]... Print additional fields:\n");
753 fprintf(fp
, " all, trace, trace:hostname, trace:domain,\n");
754 fprintf(fp
, " trace:procname, trace:vpid, loglevel, emf,\n");
755 fprintf(fp
, " callsite\n");
756 fprintf(fp
, " (default: trace:hostname, trace:procname,\n");
757 fprintf(fp
, " trace:vpid)\n");
758 fprintf(fp
, " -n, --names=NAME[,NAME]... Print field names:\n");
759 fprintf(fp
, " payload (or arg or args)\n");
760 fprintf(fp
, " none, all, scope, header, context (or ctx)\n");
761 fprintf(fp
, " (default: payload, context)\n");
762 fprintf(fp
, " --no-delta Do not print time delta between consecutive\n");
763 fprintf(fp
, " events\n");
764 fprintf(fp
, " -w, --output=PATH Write output to PATH (default: standard output)\n");
768 * Prints the Babeltrace 2.x usage.
771 void print_usage(FILE *fp
)
773 fprintf(fp
, "Usage: babeltrace [OPTIONS]\n");
775 fprintf(fp
, " -h --help Show this help\n");
776 fprintf(fp
, " --help-legacy Show Babeltrace 1.x legacy options\n");
777 fprintf(fp
, " -d, --debug Enable debug mode\n");
778 fprintf(fp
, " -l, --list List available plugins and their components\n");
779 fprintf(fp
, " -p, --plugin-path=PATH[:PATH]... Set paths from which dynamic plugins can be\n");
780 fprintf(fp
, " loaded to PATH\n");
781 fprintf(fp
, " -i, --source=SOURCE Add source plugin/component SOURCE and its\n");
782 fprintf(fp
, " parameters to the active sources (may be\n");
783 fprintf(fp
, " repeated; see the exact format below)\n");
784 fprintf(fp
, " -o, --sink=SINK Add sink plugin/component SINK and its\n");
785 fprintf(fp
, " parameters to the active sinks (may be\n");
786 fprintf(fp
, " repeated; see the exact format below)\n");
787 fprintf(fp
, " -v, --verbose Enable verbose output\n");
788 fprintf(fp
, " -V, --version Show version\n");
790 fprintf(fp
, "SOURCE/SINK argument format:\n");
792 fprintf(fp
, " One of:\n");
794 fprintf(fp
, " PLUGIN.COMPONENT\n");
795 fprintf(fp
, " Load component COMPONENT from plugin PLUGIN with its default parameters.\n");
797 fprintf(fp
, " PLUGIN.COMPONENT:PARAM=VALUE[,PARAM=VALUE]...\n");
798 fprintf(fp
, " Load component COMPONENT from plugin PLUGIN with the specified parameters.\n");
800 fprintf(fp
, " The parameter string is a comma-separated list of PARAM=VALUE assignments,\n");
801 fprintf(fp
, " where PARAM is the parameter name (C identifier plus [:.-] characters), and\n");
802 fprintf(fp
, " VALUE can be one of:\n");
804 fprintf(fp
, " * \"null\", \"nul\", \"NULL\": null value (no double quotes)\n");
805 fprintf(fp
, " * \"true\", \"TRUE\", \"yes\", \"YES\": true boolean value (no double quotes)\n");
806 fprintf(fp
, " * \"false\", \"FALSE\", \"no\", \"NO\": false boolean value (no double quotes)\n");
807 fprintf(fp
, " * Binary (\"0b\" prefix), octal (\"0\" prefix), decimal, or\n");
808 fprintf(fp
, " hexadecimal (\"0x\" prefix) signed 64-bit integer\n");
809 fprintf(fp
, " * Double precision floating point number (scientific notation is accepted)\n");
810 fprintf(fp
, " * Unquoted string with no special characters, and not matching any of\n");
811 fprintf(fp
, " the null and boolean value symbols above\n");
812 fprintf(fp
, " * Double-quoted string (accepts escape characters)\n");
814 fprintf(fp
, " Example:\n");
816 fprintf(fp
, " plugin.comp:many=null, fresh=yes, condition=false, squirrel=-782329,\n");
817 fprintf(fp
, " observe=3.14, simple=beef, needs-quotes=\"some string\",\n");
818 fprintf(fp
, " escape.chars-are:allowed=\"this is a \\\" double quote\"\n");
822 * Destroys a component configuration.
825 void bt_config_component_destroy(struct bt_object
*obj
)
827 struct bt_config_component
*bt_config_component
=
828 container_of(obj
, struct bt_config_component
, base
);
834 if (bt_config_component
->plugin_name
) {
835 g_string_free(bt_config_component
->plugin_name
, TRUE
);
838 if (bt_config_component
->component_name
) {
839 g_string_free(bt_config_component
->component_name
, TRUE
);
842 BT_PUT(bt_config_component
->params
);
843 g_free(bt_config_component
);
850 * Creates a component configuration using the given plugin name,
851 * component name, and parameters. plugin_name and component_name
852 * are copied (belong to the return value), and a reference to
853 * params is acquired.
855 * Return value is owned by the caller.
858 struct bt_config_component
*bt_config_component_create(const char *plugin_name
,
859 const char *component_name
, struct bt_value
*params
)
861 struct bt_config_component
*cfg_component
= NULL
;
863 cfg_component
= g_new0(struct bt_config_component
, 1);
864 if (!cfg_component
) {
869 bt_object_init(cfg_component
, bt_config_component_destroy
);
870 cfg_component
->plugin_name
= g_string_new(plugin_name
);
871 if (!cfg_component
->plugin_name
) {
876 cfg_component
->component_name
= g_string_new(component_name
);
877 if (!cfg_component
->component_name
) {
882 cfg_component
->params
= bt_get(params
);
886 BT_PUT(cfg_component
);
889 return cfg_component
;
893 * Creates a component configuration from a command-line source/sink
894 * option's argument. arg is the full argument, including
895 * the plugin/component names, the optional colon, and the optional
899 struct bt_config_component
*bt_config_component_from_arg(const char *arg
)
901 struct bt_config_component
*bt_config_component
= NULL
;
903 char *component_name
;
904 struct bt_value
*params
= NULL
;
906 plugin_component_names_from_arg(arg
, &plugin_name
, &component_name
);
907 if (!plugin_name
|| !component_name
) {
908 printf_err("Cannot get plugin or component name\n");
912 params
= bt_value_from_arg(arg
);
914 printf_err("Cannot parse parameters\n");
918 bt_config_component
= bt_config_component_create(plugin_name
,
919 component_name
, params
);
920 if (!bt_config_component
) {
927 BT_PUT(bt_config_component
);
931 free(component_name
);
933 return bt_config_component
;
937 * Destroys a configuration.
940 void bt_config_destroy(struct bt_object
*obj
)
942 struct bt_config
*bt_config
=
943 container_of(obj
, struct bt_config
, base
);
949 if (bt_config
->sources
) {
950 g_ptr_array_free(bt_config
->sources
, TRUE
);
953 if (bt_config
->sinks
) {
954 g_ptr_array_free(bt_config
->sinks
, TRUE
);
957 BT_PUT(bt_config
->plugin_paths
);
965 * Extracts the various paths from the string arg, delimited by ':',
966 * and converts them to an array value object.
968 * Returned array value object is empty if arg is empty.
970 * Return value is owned by the caller.
973 struct bt_value
*plugin_paths_from_arg(const char *arg
)
975 struct bt_value
*plugin_paths
;
976 const char *at
= arg
;
977 const char *end
= arg
+ strlen(arg
);
979 plugin_paths
= bt_value_array_create();
988 const char *next_colon
;
990 next_colon
= strchr(at
, ':');
991 if (next_colon
== at
) {
993 * Empty path: try next character (supported
994 * to conform to the typical parsing of $PATH).
998 } else if (!next_colon
) {
999 /* No more colon: use the remaining */
1000 next_colon
= arg
+ strlen(arg
);
1003 path
= g_string_new(NULL
);
1009 g_string_append_len(path
, at
, next_colon
- at
);
1010 at
= next_colon
+ 1;
1011 ret
= bt_value_array_append_string(plugin_paths
, path
->str
);
1012 g_string_free(path
, TRUE
);
1022 BT_PUT(plugin_paths
);
1025 return plugin_paths
;
1029 * Creates a simple lexical scanner for parsing comma-delimited names
1032 * Return value is owned by the caller.
1035 GScanner
*create_csv_identifiers_scanner(void)
1037 GScannerConfig scanner_config
= {
1038 .cset_skip_characters
= " \t\n",
1039 .cset_identifier_first
= G_CSET_a_2_z G_CSET_A_2_Z
"_",
1040 .cset_identifier_nth
= G_CSET_a_2_z G_CSET_A_2_Z
":_-",
1041 .case_sensitive
= TRUE
,
1042 .cpair_comment_single
= NULL
,
1043 .skip_comment_multi
= TRUE
,
1044 .skip_comment_single
= TRUE
,
1045 .scan_comment_multi
= FALSE
,
1046 .scan_identifier
= TRUE
,
1047 .scan_identifier_1char
= TRUE
,
1048 .scan_identifier_NULL
= FALSE
,
1049 .scan_symbols
= FALSE
,
1050 .symbol_2_token
= FALSE
,
1051 .scope_0_fallback
= FALSE
,
1052 .scan_binary
= FALSE
,
1053 .scan_octal
= FALSE
,
1054 .scan_float
= FALSE
,
1056 .scan_hex_dollar
= FALSE
,
1057 .numbers_2_int
= FALSE
,
1058 .int_2_float
= FALSE
,
1059 .store_int64
= FALSE
,
1060 .scan_string_sq
= FALSE
,
1061 .scan_string_dq
= FALSE
,
1062 .identifier_2_string
= FALSE
,
1063 .char_2_token
= TRUE
,
1066 return g_scanner_new(&scanner_config
);
1070 * Converts a comma-delimited list of known names (--names option) to
1071 * an array value object containing those names as string value objects.
1073 * Return value is owned by the caller.
1076 struct bt_value
*names_from_arg(const char *arg
)
1078 GScanner
*scanner
= NULL
;
1079 struct bt_value
*names
= NULL
;
1081 names
= bt_value_array_create();
1087 scanner
= create_csv_identifiers_scanner();
1093 g_scanner_input_text(scanner
, arg
, strlen(arg
));
1096 GTokenType token_type
= g_scanner_get_next_token(scanner
);
1098 switch (token_type
) {
1099 case G_TOKEN_IDENTIFIER
:
1101 const char *identifier
= scanner
->value
.v_identifier
;
1103 if (!strcmp(identifier
, "payload") ||
1104 !strcmp(identifier
, "args") ||
1105 !strcmp(identifier
, "arg")) {
1106 if (bt_value_array_append_string(names
,
1110 } else if (!strcmp(identifier
, "context") ||
1111 !strcmp(identifier
, "ctx")) {
1112 if (bt_value_array_append_string(names
,
1116 } else if (!strcmp(identifier
, "scope") ||
1117 !strcmp(identifier
, "header")) {
1118 if (bt_value_array_append_string(names
,
1122 } else if (!strcmp(identifier
, "all") ||
1123 !strcmp(identifier
, "none")) {
1125 * "all" and "none" override all the
1129 names
= bt_value_array_create();
1135 if (bt_value_array_append_string(names
,
1141 printf_err("Unknown field name: \"%s\"\n",
1163 g_scanner_destroy(scanner
);
1170 * Converts a comma-delimited list of known fields (--fields option) to
1171 * an array value object containing those fields as string
1174 * Return value is owned by the caller.
1177 struct bt_value
*fields_from_arg(const char *arg
)
1179 GScanner
*scanner
= NULL
;
1180 struct bt_value
*fields
;
1182 fields
= bt_value_array_create();
1188 scanner
= create_csv_identifiers_scanner();
1194 g_scanner_input_text(scanner
, arg
, strlen(arg
));
1197 GTokenType token_type
= g_scanner_get_next_token(scanner
);
1199 switch (token_type
) {
1200 case G_TOKEN_IDENTIFIER
:
1202 const char *identifier
= scanner
->value
.v_identifier
;
1204 if (!strcmp(identifier
, "trace") ||
1205 !strcmp(identifier
, "trace:hostname") ||
1206 !strcmp(identifier
, "trace:domain") ||
1207 !strcmp(identifier
, "trace:procname") ||
1208 !strcmp(identifier
, "trace:vpid") ||
1209 !strcmp(identifier
, "loglevel") ||
1210 !strcmp(identifier
, "emf") ||
1211 !strcmp(identifier
, "callsite")) {
1212 if (bt_value_array_append_string(fields
,
1216 } else if (!strcmp(identifier
, "all")) {
1217 /* "all" override all the specific fields */
1219 fields
= bt_value_array_create();
1225 if (bt_value_array_append_string(fields
,
1231 printf_err("Unknown field name: \"%s\"\n",
1253 g_scanner_destroy(scanner
);
1259 * Inserts the equivalent "prefix-name" true boolean value objects into
1260 * map_obj where the names are in array_obj.
1263 int insert_flat_names_fields_from_array(struct bt_value
*map_obj
,
1264 struct bt_value
*array_obj
, const char *prefix
)
1268 GString
*tmpstr
= NULL
;
1271 * array_obj may be NULL if no CLI options were specified to
1272 * trigger its creation.
1278 tmpstr
= g_string_new(NULL
);
1285 for (i
= 0; i
< bt_value_array_size(array_obj
); i
++) {
1286 struct bt_value
*str_obj
= bt_value_array_get(array_obj
, i
);
1290 printf_err("Unexpected error\n");
1295 ret
= bt_value_string_get(str_obj
, &suffix
);
1298 printf_err("Unexpected error\n");
1302 g_string_assign(tmpstr
, prefix
);
1303 g_string_append(tmpstr
, "-");
1304 g_string_append(tmpstr
, suffix
);
1305 ret
= bt_value_map_insert_bool(map_obj
, tmpstr
->str
, true);
1314 g_string_free(tmpstr
, TRUE
);
1321 * Inserts a string (if exists and not empty) or null to a map value
1325 enum bt_value_status
map_insert_string_or_null(struct bt_value
*map
,
1326 const char *key
, GString
*string
)
1328 enum bt_value_status ret
;
1330 if (string
&& string
->len
> 0) {
1331 ret
= bt_value_map_insert_string(map
, key
, string
->str
);
1333 ret
= bt_value_map_insert(map
, key
, bt_value_null
);
1339 * Returns the parameters (map value object) corresponding to the
1340 * legacy text format options.
1342 * Return value is owned by the caller.
1345 struct bt_value
*params_from_text_legacy_opts(
1346 struct text_legacy_opts
*text_legacy_opts
)
1348 struct bt_value
*params
;
1350 params
= bt_value_map_create();
1356 if (map_insert_string_or_null(params
, "output-path",
1357 text_legacy_opts
->output
)) {
1362 if (map_insert_string_or_null(params
, "debug-info-dir",
1363 text_legacy_opts
->dbg_info_dir
)) {
1368 if (map_insert_string_or_null(params
, "debug-info-target-prefix",
1369 text_legacy_opts
->dbg_info_target_prefix
)) {
1374 if (bt_value_map_insert_bool(params
, "debug-info-full-path",
1375 text_legacy_opts
->dbg_info_full_path
)) {
1380 if (bt_value_map_insert_bool(params
, "no-delta",
1381 text_legacy_opts
->no_delta
)) {
1386 if (bt_value_map_insert_bool(params
, "clock-cycles",
1387 text_legacy_opts
->clock_cycles
)) {
1392 if (bt_value_map_insert_bool(params
, "clock-seconds",
1393 text_legacy_opts
->clock_seconds
)) {
1398 if (bt_value_map_insert_bool(params
, "clock-date",
1399 text_legacy_opts
->clock_date
)) {
1404 if (bt_value_map_insert_bool(params
, "clock-gmt",
1405 text_legacy_opts
->clock_gmt
)) {
1410 if (insert_flat_names_fields_from_array(params
,
1411 text_legacy_opts
->names
, "name")) {
1415 if (insert_flat_names_fields_from_array(params
,
1416 text_legacy_opts
->fields
, "field")) {
1430 int append_sinks_from_legacy_opts(GPtrArray
*sinks
,
1431 enum legacy_output_format legacy_output_format
,
1432 struct text_legacy_opts
*text_legacy_opts
)
1435 struct bt_value
*params
= NULL
;
1436 const char *plugin_name
;
1437 const char *component_name
;
1438 struct bt_config_component
*bt_config_component
= NULL
;
1440 switch (legacy_output_format
) {
1441 case LEGACY_OUTPUT_FORMAT_TEXT
:
1442 plugin_name
= "text";
1443 component_name
= "text";
1445 case LEGACY_OUTPUT_FORMAT_CTF_METADATA
:
1446 plugin_name
= "ctf";
1447 component_name
= "metadata-text";
1449 case LEGACY_OUTPUT_FORMAT_DUMMY
:
1450 plugin_name
= "dummy";
1451 component_name
= "dummy";
1458 if (legacy_output_format
== LEGACY_OUTPUT_FORMAT_TEXT
) {
1459 /* Legacy "text" output format has parameters */
1460 params
= params_from_text_legacy_opts(text_legacy_opts
);
1466 * Legacy "dummy" and "ctf-metadata" output formats do
1467 * not have parameters.
1469 params
= bt_value_map_create();
1476 /* Create a component configuration */
1477 bt_config_component
= bt_config_component_create(plugin_name
,
1478 component_name
, params
);
1479 if (!bt_config_component
) {
1483 /* Move created component configuration to the array */
1484 g_ptr_array_add(sinks
, bt_config_component
);
1498 * Returns the parameters (map value object) corresponding to the
1499 * given legacy CTF format options.
1501 * Return value is owned by the caller.
1504 struct bt_value
*params_from_ctf_legacy_opts(
1505 struct ctf_legacy_opts
*ctf_legacy_opts
)
1507 struct bt_value
*params
;
1509 params
= bt_value_map_create();
1515 if (bt_value_map_insert_integer(params
, "offset-s",
1516 ctf_legacy_opts
->offset_s
.value
)) {
1521 if (bt_value_map_insert_integer(params
, "offset-ns",
1522 ctf_legacy_opts
->offset_ns
.value
)) {
1527 if (bt_value_map_insert_bool(params
, "stream-intersection",
1528 ctf_legacy_opts
->stream_intersection
)) {
1543 int append_sources_from_legacy_opts(GPtrArray
*sources
,
1544 enum legacy_input_format legacy_input_format
,
1545 struct ctf_legacy_opts
*ctf_legacy_opts
,
1546 struct bt_value
*legacy_input_paths
)
1550 struct bt_value
*base_params
;
1551 struct bt_value
*params
= NULL
;
1552 struct bt_value
*input_path
= NULL
;
1553 struct bt_value
*input_path_copy
= NULL
;
1554 const char *input_key
;
1555 const char *component_name
;
1557 switch (legacy_input_format
) {
1558 case LEGACY_INPUT_FORMAT_CTF
:
1560 component_name
= "fs";
1562 case LEGACY_INPUT_FORMAT_LTTNG_LIVE
:
1564 component_name
= "lttng-live";
1571 base_params
= params_from_ctf_legacy_opts(ctf_legacy_opts
);
1576 for (i
= 0; i
< bt_value_array_size(legacy_input_paths
); i
++) {
1577 struct bt_config_component
*bt_config_component
= NULL
;
1579 /* Copy base parameters as current parameters */
1580 params
= bt_value_copy(base_params
);
1585 /* Get current input path string value object */
1586 input_path
= bt_value_array_get(legacy_input_paths
, i
);
1591 /* Copy current input path value object */
1592 input_path_copy
= bt_value_copy(input_path
);
1593 if (!input_path_copy
) {
1597 /* Insert input path value object into current parameters */
1598 ret
= bt_value_map_insert(params
, input_key
, input_path_copy
);
1603 /* Create a component configuration */
1604 bt_config_component
= bt_config_component_create("ctf",
1605 component_name
, params
);
1606 if (!bt_config_component
) {
1610 /* Move created component configuration to the array */
1611 g_ptr_array_add(sources
, bt_config_component
);
1613 /* Put current stuff */
1615 BT_PUT(input_path_copy
);
1625 BT_PUT(base_params
);
1628 BT_PUT(input_path_copy
);
1633 * Escapes a string for the shell. The string is escaped knowing that
1634 * it's a parameter string value (double-quoted), and that it will be
1635 * entered between single quotes in the shell.
1637 * Return value is owned by the caller.
1640 char *str_shell_escape(const char *input
)
1643 const char *at
= input
;
1644 GString
*str
= g_string_new(NULL
);
1650 while (*at
!= '\0') {
1653 g_string_append(str
, "\\\\");
1656 g_string_append(str
, "\\\"");
1659 g_string_append(str
, "'\"'\"'");
1662 g_string_append(str
, "\\n");
1665 g_string_append(str
, "\\t");
1668 g_string_append_c(str
, *at
);
1678 g_string_free(str
, FALSE
);
1685 int append_prefixed_flag_params(GString
*str
, struct bt_value
*flags
,
1695 for (i
= 0; i
< bt_value_array_size(flags
); i
++) {
1696 struct bt_value
*value
= bt_value_array_get(flags
, i
);
1704 if (bt_value_string_get(value
, &flag
)) {
1710 g_string_append_printf(str
, ",%s-%s=true", prefix
, flag
);
1719 * Appends a boolean parameter string.
1722 void g_string_append_bool_param(GString
*str
, const char *name
, bool value
)
1724 g_string_append_printf(str
, ",%s=%s", name
, value
? "true" : "false");
1728 * Appends a path parameter string, or null if it's empty.
1731 int g_string_append_string_path_param(GString
*str
, const char *name
,
1736 if (path
->len
> 0) {
1737 char *escaped_path
= str_shell_escape(path
->str
);
1739 if (!escaped_path
) {
1744 g_string_append_printf(str
, "%s=\"%s\"", name
, escaped_path
);
1747 g_string_append_printf(str
, "%s=null", name
);
1760 * Prints the non-legacy sink options equivalent to the specified
1761 * legacy output format options.
1764 void print_output_legacy_to_sinks(
1765 enum legacy_output_format legacy_output_format
,
1766 struct text_legacy_opts
*text_legacy_opts
)
1768 const char *input_format
;
1769 GString
*str
= NULL
;
1771 str
= g_string_new(" ");
1777 switch (legacy_output_format
) {
1778 case LEGACY_OUTPUT_FORMAT_TEXT
:
1779 input_format
= "text";
1781 case LEGACY_OUTPUT_FORMAT_CTF_METADATA
:
1782 input_format
= "ctf-metadata";
1784 case LEGACY_OUTPUT_FORMAT_DUMMY
:
1785 input_format
= "dummy";
1791 printf_err("Both \"%s\" legacy output format and non-legacy sink(s) specified.\n\n",
1793 printf_err("Specify the following non-legacy sink instead of the legacy \"%s\"\noutput format options:\n\n",
1795 g_string_append(str
, "-o ");
1797 switch (legacy_output_format
) {
1798 case LEGACY_OUTPUT_FORMAT_TEXT
:
1799 g_string_append(str
, "text.text");
1801 case LEGACY_OUTPUT_FORMAT_CTF_METADATA
:
1802 g_string_append(str
, "ctf.metadata-text");
1804 case LEGACY_OUTPUT_FORMAT_DUMMY
:
1805 g_string_append(str
, "dummy.dummy");
1811 if (legacy_output_format
== LEGACY_OUTPUT_FORMAT_TEXT
&&
1812 text_legacy_opts_is_any_set(text_legacy_opts
)) {
1815 g_string_append(str
, ":'");
1817 if (g_string_append_string_path_param(str
, "output-path",
1818 text_legacy_opts
->output
)) {
1822 g_string_append(str
, ",");
1824 if (g_string_append_string_path_param(str
, "debug-info-dir",
1825 text_legacy_opts
->dbg_info_dir
)) {
1829 g_string_append(str
, ",");
1831 if (g_string_append_string_path_param(str
,
1832 "debug-info-target-prefix",
1833 text_legacy_opts
->dbg_info_target_prefix
)) {
1837 g_string_append_bool_param(str
, "no-delta",
1838 text_legacy_opts
->no_delta
);
1839 g_string_append_bool_param(str
, "clock-cycles",
1840 text_legacy_opts
->clock_cycles
);
1841 g_string_append_bool_param(str
, "clock-seconds",
1842 text_legacy_opts
->clock_seconds
);
1843 g_string_append_bool_param(str
, "clock-date",
1844 text_legacy_opts
->clock_date
);
1845 g_string_append_bool_param(str
, "clock-gmt",
1846 text_legacy_opts
->clock_gmt
);
1847 ret
= append_prefixed_flag_params(str
, text_legacy_opts
->names
,
1853 ret
= append_prefixed_flag_params(str
, text_legacy_opts
->fields
,
1859 /* Remove last comma and close single quote */
1860 g_string_append(str
, "'");
1863 printf_err("%s\n\n", str
->str
);
1867 g_string_free(str
, TRUE
);
1873 * Prints the non-legacy source options equivalent to the specified
1874 * legacy input format options.
1877 void print_input_legacy_to_sources(enum legacy_input_format legacy_input_format
,
1878 struct bt_value
*legacy_input_paths
,
1879 struct ctf_legacy_opts
*ctf_legacy_opts
)
1881 const char *input_format
;
1882 GString
*str
= NULL
;
1885 str
= g_string_new(" ");
1891 switch (legacy_input_format
) {
1892 case LEGACY_INPUT_FORMAT_CTF
:
1893 input_format
= "ctf";
1895 case LEGACY_INPUT_FORMAT_LTTNG_LIVE
:
1896 input_format
= "lttng-live";
1902 printf_err("Both \"%s\" legacy input format and non-legacy source(s) specified.\n\n",
1904 printf_err("Specify the following non-legacy source(s) instead of the legacy \"%s\"\ninput format options and positional arguments:\n\n",
1907 for (i
= 0; i
< bt_value_array_size(legacy_input_paths
); i
++) {
1908 struct bt_value
*input_value
=
1909 bt_value_array_get(legacy_input_paths
, i
);
1910 const char *input
= NULL
;
1911 char *escaped_input
;
1914 assert(input_value
);
1915 ret
= bt_value_string_get(input_value
, &input
);
1916 BT_PUT(input_value
);
1917 assert(!ret
&& input
);
1918 escaped_input
= str_shell_escape(input
);
1919 if (!escaped_input
) {
1924 g_string_append(str
, "-i ctf.");
1926 switch (legacy_input_format
) {
1927 case LEGACY_INPUT_FORMAT_CTF
:
1928 g_string_append(str
, "fs:'path=\"");
1930 case LEGACY_INPUT_FORMAT_LTTNG_LIVE
:
1931 g_string_append(str
, "lttng-live:'url=\"");
1937 g_string_append(str
, escaped_input
);
1938 g_string_append(str
, "\"");
1939 g_string_append_printf(str
, ",offset-s=%" PRId64
,
1940 ctf_legacy_opts
->offset_s
.value
);
1941 g_string_append_printf(str
, ",offset-ns=%" PRId64
,
1942 ctf_legacy_opts
->offset_ns
.value
);
1943 g_string_append_bool_param(str
, "stream-intersection",
1944 ctf_legacy_opts
->stream_intersection
);
1945 g_string_append(str
, "' ");
1946 g_free(escaped_input
);
1949 printf_err("%s\n\n", str
->str
);
1953 g_string_free(str
, TRUE
);
1959 * Validates a given configuration, with optional legacy input and
1960 * output formats options. Prints useful error messages if anything
1963 * Returns true when the configuration is valid.
1966 bool validate_cfg(struct bt_config
*cfg
,
1967 enum legacy_input_format
*legacy_input_format
,
1968 enum legacy_output_format
*legacy_output_format
,
1969 struct bt_value
*legacy_input_paths
,
1970 struct ctf_legacy_opts
*ctf_legacy_opts
,
1971 struct text_legacy_opts
*text_legacy_opts
)
1973 bool legacy_input
= false;
1974 bool legacy_output
= false;
1976 /* Determine if the input and output should be legacy-style */
1977 if (*legacy_input_format
!= LEGACY_INPUT_FORMAT_NONE
||
1978 cfg
->sources
->len
== 0 ||
1979 !bt_value_array_is_empty(legacy_input_paths
) ||
1980 ctf_legacy_opts_is_any_set(ctf_legacy_opts
)) {
1981 legacy_input
= true;
1984 if (*legacy_output_format
!= LEGACY_OUTPUT_FORMAT_NONE
||
1985 cfg
->sinks
->len
== 0 ||
1986 text_legacy_opts_is_any_set(text_legacy_opts
)) {
1987 legacy_output
= true;
1991 /* If no legacy input format was specified, default to CTF */
1992 if (*legacy_input_format
== LEGACY_INPUT_FORMAT_NONE
) {
1993 *legacy_input_format
= LEGACY_INPUT_FORMAT_CTF
;
1996 /* Make sure at least one input path exists */
1997 if (bt_value_array_is_empty(legacy_input_paths
)) {
1998 switch (*legacy_input_format
) {
1999 case LEGACY_INPUT_FORMAT_CTF
:
2000 printf_err("No input path specified for legacy \"ctf\" input format\n");
2002 case LEGACY_INPUT_FORMAT_LTTNG_LIVE
:
2003 printf_err("No URL specified for legacy \"lttng-live\" input format\n");
2011 /* Make sure no non-legacy sources are specified */
2012 if (cfg
->sources
->len
!= 0) {
2013 print_input_legacy_to_sources(*legacy_input_format
,
2014 legacy_input_paths
, ctf_legacy_opts
);
2019 if (legacy_output
) {
2021 * If no legacy output format was specified, default to
2024 if (*legacy_output_format
== LEGACY_OUTPUT_FORMAT_NONE
) {
2025 *legacy_output_format
= LEGACY_OUTPUT_FORMAT_TEXT
;
2029 * If any "text" option was specified, the output must be
2032 if (text_legacy_opts_is_any_set(text_legacy_opts
) &&
2033 *legacy_output_format
!=
2034 LEGACY_OUTPUT_FORMAT_TEXT
) {
2035 printf_err("Options for legacy \"text\" output format specified with a different legacy output format\n");
2039 /* Make sure no non-legacy sinks are specified */
2040 if (cfg
->sinks
->len
!= 0) {
2041 print_output_legacy_to_sinks(*legacy_output_format
,
2048 * If the output is the legacy "ctf-metadata" format, then the
2049 * input should be the legacy "ctf" input format.
2051 if (*legacy_output_format
== LEGACY_OUTPUT_FORMAT_CTF_METADATA
&&
2052 *legacy_input_format
!= LEGACY_INPUT_FORMAT_CTF
) {
2053 printf_err("Legacy \"ctf-metadata\" output format requires using legacy \"ctf\" input format\n");
2064 * Parses a 64-bit signed integer.
2066 * Returns a negative value if anything goes wrong.
2069 int parse_int64(const char *arg
, int64_t *val
)
2074 *val
= strtoll(arg
, &endptr
, 0);
2075 if (*endptr
!= '\0' || arg
== endptr
|| errno
!= 0) {
2087 OPT_CLOCK_FORCE_CORRELATE
,
2090 OPT_CLOCK_OFFSET_NS
,
2094 OPT_DEBUG_INFO_FULL_PATH
,
2095 OPT_DEBUG_INFO_TARGET_PREFIX
,
2108 OPT_STREAM_INTERSECTION
,
2113 /* popt long option descriptions */
2114 static struct poptOption long_options
[] = {
2115 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
2116 { "clock-cycles", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_CYCLES
, NULL
, NULL
},
2117 { "clock-date", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_DATE
, NULL
, NULL
},
2118 { "clock-force-correlate", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_FORCE_CORRELATE
, NULL
, NULL
},
2119 { "clock-gmt", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_GMT
, NULL
, NULL
},
2120 { "clock-offset", '\0', POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET
, NULL
, NULL
},
2121 { "clock-offset-ns", '\0', POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET_NS
, NULL
, NULL
},
2122 { "clock-seconds", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_SECONDS
, NULL
, NULL
},
2123 { "debug", 'd', POPT_ARG_NONE
, NULL
, OPT_DEBUG
, NULL
, NULL
},
2124 { "debug-info-dir", 0, POPT_ARG_STRING
, NULL
, OPT_DEBUG_INFO_DIR
, NULL
, NULL
},
2125 { "debug-info-full-path", 0, POPT_ARG_NONE
, NULL
, OPT_DEBUG_INFO_FULL_PATH
, NULL
, NULL
},
2126 { "debug-info-target-prefix", 0, POPT_ARG_STRING
, NULL
, OPT_DEBUG_INFO_TARGET_PREFIX
, NULL
, NULL
},
2127 { "fields", 'f', POPT_ARG_STRING
, NULL
, OPT_FIELDS
, NULL
, NULL
},
2128 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
2129 { "help-legacy", '\0', POPT_ARG_NONE
, NULL
, OPT_HELP_LEGACY
, NULL
, NULL
},
2130 { "input-format", 'i', POPT_ARG_STRING
, NULL
, OPT_INPUT_FORMAT
, NULL
, NULL
},
2131 { "list", 'l', POPT_ARG_NONE
, NULL
, OPT_LIST
, NULL
, NULL
},
2132 { "names", 'n', POPT_ARG_STRING
, NULL
, OPT_NAMES
, NULL
, NULL
},
2133 { "no-delta", '\0', POPT_ARG_NONE
, NULL
, OPT_NO_DELTA
, NULL
, NULL
},
2134 { "output", 'w', POPT_ARG_STRING
, NULL
, OPT_OUTPUT_PATH
, NULL
, NULL
},
2135 { "output-format", 'o', POPT_ARG_STRING
, NULL
, OPT_OUTPUT_FORMAT
, NULL
, NULL
},
2136 { "plugin-path", 'p', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
2137 { "sink", '\0', POPT_ARG_STRING
, NULL
, OPT_SINK
, NULL
, NULL
},
2138 { "source", '\0', POPT_ARG_STRING
, NULL
, OPT_SOURCE
, NULL
, NULL
},
2139 { "stream-intersection", '\0', POPT_ARG_NONE
, NULL
, OPT_STREAM_INTERSECTION
, NULL
, NULL
},
2140 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
2141 { "version", 'V', POPT_ARG_NONE
, NULL
, OPT_VERSION
, NULL
, NULL
},
2142 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
2146 * Sets the value of a given legacy offset option and marks it as set.
2148 static void set_offset_value(struct offset_opt
*offset_opt
, int64_t value
)
2150 offset_opt
->value
= value
;
2151 offset_opt
->is_set
= true;
2155 * Returns a Babeltrace configuration, out of command-line arguments,
2156 * containing everything that is needed to instanciate specific
2157 * components with given parameters.
2159 * *exit_code is set to the appropriate exit code to use as far as this
2162 * Return value is NULL on error, otherwise it's owned by the caller.
2164 struct bt_config
*bt_config_from_args(int argc
, char *argv
[], int *exit_code
)
2166 struct bt_config
*cfg
= NULL
;
2167 poptContext pc
= NULL
;
2169 struct ctf_legacy_opts ctf_legacy_opts
= { 0 };
2170 struct text_legacy_opts text_legacy_opts
= { 0 };
2171 enum legacy_input_format legacy_input_format
= LEGACY_INPUT_FORMAT_NONE
;
2172 enum legacy_output_format legacy_output_format
=
2173 LEGACY_OUTPUT_FORMAT_NONE
;
2174 struct bt_value
*legacy_input_paths
= NULL
;
2180 print_usage(stdout
);
2184 text_legacy_opts
.output
= g_string_new(NULL
);
2185 if (!text_legacy_opts
.output
) {
2190 text_legacy_opts
.dbg_info_dir
= g_string_new(NULL
);
2191 if (!text_legacy_opts
.dbg_info_dir
) {
2196 text_legacy_opts
.dbg_info_target_prefix
= g_string_new(NULL
);
2197 if (!text_legacy_opts
.dbg_info_target_prefix
) {
2203 cfg
= g_new0(struct bt_config
, 1);
2209 bt_object_init(cfg
, bt_config_destroy
);
2210 cfg
->sources
= g_ptr_array_new_with_free_func((GDestroyNotify
) bt_put
);
2211 if (!cfg
->sources
) {
2216 cfg
->sinks
= g_ptr_array_new_with_free_func((GDestroyNotify
) bt_put
);
2222 legacy_input_paths
= bt_value_array_create();
2223 if (!legacy_input_paths
) {
2229 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
2231 printf_err("Cannot get popt context\n");
2235 poptReadDefaultConfig(pc
, 0);
2237 while ((opt
= poptGetNextOpt(pc
)) > 0) {
2238 arg
= poptGetOptArg(pc
);
2241 case OPT_PLUGIN_PATH
:
2242 if (cfg
->plugin_paths
) {
2243 printf_err("Duplicate --plugin-path option\n");
2247 cfg
->plugin_paths
= plugin_paths_from_arg(arg
);
2248 if (!cfg
->plugin_paths
) {
2249 printf_err("Invalid --plugin-path option's argument\n");
2253 case OPT_OUTPUT_PATH
:
2254 if (text_legacy_opts
.output
->len
> 0) {
2255 printf_err("Duplicate --output option\n");
2259 g_string_assign(text_legacy_opts
.output
, arg
);
2261 case OPT_DEBUG_INFO_DIR
:
2262 if (text_legacy_opts
.dbg_info_dir
->len
> 0) {
2263 printf_err("Duplicate --debug-info-dir option\n");
2267 g_string_assign(text_legacy_opts
.dbg_info_dir
, arg
);
2269 case OPT_DEBUG_INFO_TARGET_PREFIX
:
2270 if (text_legacy_opts
.dbg_info_target_prefix
->len
> 0) {
2271 printf_err("Duplicate --debug-info-target-prefix option\n");
2275 g_string_assign(text_legacy_opts
.dbg_info_target_prefix
, arg
);
2277 case OPT_INPUT_FORMAT
:
2280 struct bt_config_component
*bt_config_component
;
2282 if (opt
== OPT_INPUT_FORMAT
) {
2283 if (!strcmp(arg
, "ctf")) {
2284 /* Legacy CTF input format */
2285 if (legacy_input_format
) {
2286 print_err_dup_legacy_input();
2290 legacy_input_format
=
2291 LEGACY_INPUT_FORMAT_CTF
;
2293 } else if (!strcmp(arg
, "lttng-live")) {
2294 /* Legacy LTTng-live input format */
2295 if (legacy_input_format
) {
2296 print_err_dup_legacy_input();
2300 legacy_input_format
=
2301 LEGACY_INPUT_FORMAT_LTTNG_LIVE
;
2306 /* Non-legacy: try to create a component config */
2307 bt_config_component
= bt_config_component_from_arg(arg
);
2308 if (!bt_config_component
) {
2309 printf_err("Invalid source component format:\n %s\n",
2314 g_ptr_array_add(cfg
->sources
, bt_config_component
);
2317 case OPT_OUTPUT_FORMAT
:
2320 struct bt_config_component
*bt_config_component
;
2322 if (opt
== OPT_OUTPUT_FORMAT
) {
2323 if (!strcmp(arg
, "text")) {
2324 /* Legacy CTF-text output format */
2325 if (legacy_output_format
) {
2326 print_err_dup_legacy_output();
2330 legacy_output_format
=
2331 LEGACY_OUTPUT_FORMAT_TEXT
;
2333 } else if (!strcmp(arg
, "dummy")) {
2334 /* Legacy dummy output format */
2335 if (legacy_output_format
) {
2336 print_err_dup_legacy_output();
2340 legacy_output_format
=
2341 LEGACY_OUTPUT_FORMAT_DUMMY
;
2343 } else if (!strcmp(arg
, "ctf-metadata")) {
2344 /* Legacy CTF-metadata output format */
2345 if (legacy_output_format
) {
2346 print_err_dup_legacy_output();
2350 legacy_output_format
=
2351 LEGACY_OUTPUT_FORMAT_CTF_METADATA
;
2356 /* Non-legacy: try to create a component config */
2357 bt_config_component
= bt_config_component_from_arg(arg
);
2358 if (!bt_config_component
) {
2359 printf_err("Invalid sink component format:\n %s\n",
2364 g_ptr_array_add(cfg
->sinks
, bt_config_component
);
2368 if (text_legacy_opts
.names
) {
2369 printf_err("Duplicate --names option\n");
2373 text_legacy_opts
.names
= names_from_arg(arg
);
2374 if (!text_legacy_opts
.names
) {
2375 printf_err("Invalid --names option's argument\n");
2380 if (text_legacy_opts
.fields
) {
2381 printf_err("Duplicate --fields option\n");
2385 text_legacy_opts
.fields
= fields_from_arg(arg
);
2386 if (!text_legacy_opts
.fields
) {
2387 printf_err("Invalid --fields option's argument\n");
2392 text_legacy_opts
.no_delta
= true;
2394 case OPT_CLOCK_CYCLES
:
2395 text_legacy_opts
.clock_cycles
= true;
2397 case OPT_CLOCK_SECONDS
:
2398 text_legacy_opts
.clock_seconds
= true;
2400 case OPT_CLOCK_DATE
:
2401 text_legacy_opts
.clock_date
= true;
2404 text_legacy_opts
.clock_gmt
= true;
2406 case OPT_DEBUG_INFO_FULL_PATH
:
2407 text_legacy_opts
.dbg_info_full_path
= true;
2409 case OPT_CLOCK_OFFSET
:
2413 if (ctf_legacy_opts
.offset_s
.is_set
) {
2414 printf_err("Duplicate --clock-offset option\n");
2418 if (parse_int64(arg
, &val
)) {
2419 printf_err("Invalid --clock-offset option's argument\n");
2423 set_offset_value(&ctf_legacy_opts
.offset_s
, val
);
2426 case OPT_CLOCK_OFFSET_NS
:
2430 if (ctf_legacy_opts
.offset_ns
.is_set
) {
2431 printf_err("Duplicate --clock-offset-ns option\n");
2435 if (parse_int64(arg
, &val
)) {
2436 printf_err("Invalid --clock-offset-ns option's argument\n");
2440 set_offset_value(&ctf_legacy_opts
.offset_ns
, val
);
2443 case OPT_STREAM_INTERSECTION
:
2444 ctf_legacy_opts
.stream_intersection
= true;
2446 case OPT_CLOCK_FORCE_CORRELATE
:
2447 cfg
->force_correlate
= true;
2451 print_usage(stdout
);
2453 case OPT_HELP_LEGACY
:
2455 print_legacy_usage(stdout
);
2462 cfg
->do_list
= true;
2465 cfg
->verbose
= true;
2471 printf_err("Unknown command-line option specified (option code %d)\n",
2480 /* Check for option parsing error */
2482 printf_err("While parsing command-line options, at option %s: %s\n",
2483 poptBadOption(pc
, 0), poptStrerror(opt
));
2487 /* Consume leftover arguments as legacy input paths */
2489 const char *input_path
= poptGetArg(pc
);
2495 if (bt_value_array_append_string(legacy_input_paths
,
2502 /* Validate legacy/non-legacy options */
2503 if (!validate_cfg(cfg
, &legacy_input_format
, &legacy_output_format
,
2504 legacy_input_paths
, &ctf_legacy_opts
,
2505 &text_legacy_opts
)) {
2506 printf_err("Command-line options form an invalid configuration\n");
2511 * If there's a legacy input format, convert it to source
2512 * component configurations.
2514 if (legacy_input_format
) {
2515 if (append_sources_from_legacy_opts(cfg
->sources
,
2516 legacy_input_format
, &ctf_legacy_opts
,
2517 legacy_input_paths
)) {
2518 printf_err("Cannot convert legacy input format options to source(s)\n");
2524 * If there's a legacy output format, convert it to sink
2525 * component configurations.
2527 if (legacy_output_format
) {
2528 if (append_sinks_from_legacy_opts(cfg
->sinks
,
2529 legacy_output_format
, &text_legacy_opts
)) {
2530 printf_err("Cannot convert legacy output format options to sink(s)\n");
2544 poptFreeContext(pc
);
2547 if (text_legacy_opts
.output
) {
2548 g_string_free(text_legacy_opts
.output
, TRUE
);
2551 if (text_legacy_opts
.dbg_info_dir
) {
2552 g_string_free(text_legacy_opts
.dbg_info_dir
, TRUE
);
2555 if (text_legacy_opts
.dbg_info_target_prefix
) {
2556 g_string_free(text_legacy_opts
.dbg_info_target_prefix
, TRUE
);
2560 BT_PUT(text_legacy_opts
.names
);
2561 BT_PUT(text_legacy_opts
.fields
);
2562 BT_PUT(legacy_input_paths
);