bt2: Rename _set_default_clock_snapshot_ptr callback to _set_default_clock_snapshot
[babeltrace.git] / src / cli / babeltrace2-cfg-cli-args.c
CommitLineData
9009cc24
PP
1/*
2 * Babeltrace trace converter - parameter parsing
3 *
4 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * SOFTWARE.
23 */
24
f4f9e43b
PP
25#define BT_LOG_TAG "CLI-CFG-CLI-ARGS"
26#include "logging.h"
27
9009cc24
PP
28#include <errno.h>
29#include <stdlib.h>
30#include <string.h>
57952005 31#include "common/assert.h"
9009cc24
PP
32#include <stdio.h>
33#include <stdbool.h>
34#include <inttypes.h>
71c5da58 35#include <babeltrace2/babeltrace.h>
57952005 36#include "common/common.h"
9009cc24
PP
37#include <popt.h>
38#include <glib.h>
39#include <sys/types.h>
142ac9b0
MJ
40#include "babeltrace2-cfg.h"
41#include "babeltrace2-cfg-cli-args.h"
42#include "babeltrace2-cfg-cli-args-connect.h"
57952005 43#include "common/version.h"
9009cc24
PP
44
45/*
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.
50 */
51#define printf_err(fmt, args...) \
52 do { \
53 if (is_first_error) { \
54 fprintf(stderr, "Command line error: "); \
55 is_first_error = false; \
56 } \
57 fprintf(stderr, fmt, ##args); \
58 } while (0)
59
60static bool is_first_error = true;
c4f81dc9 61static const int cli_default_log_level = BT_LOG_WARN;
9009cc24
PP
62
63/* INI-style parsing FSM states */
64enum ini_parsing_fsm_state {
65 /* Expect a map key (identifier) */
66 INI_EXPECT_MAP_KEY,
67
68 /* Expect an equal character ('=') */
69 INI_EXPECT_EQUAL,
70
71 /* Expect a value */
72 INI_EXPECT_VALUE,
73
9009cc24
PP
74 /* Expect a comma character (',') */
75 INI_EXPECT_COMMA,
76};
77
78/* INI-style parsing state variables */
79struct ini_parsing_state {
80 /* Lexical scanner (owned by this) */
81 GScanner *scanner;
82
83 /* Output map value object being filled (owned by this) */
8eee8ea2 84 bt_value *params;
9009cc24
PP
85
86 /* Next expected FSM state */
87 enum ini_parsing_fsm_state expecting;
88
89 /* Last decoded map key (owned by this) */
90 char *last_map_key;
91
92 /* Complete INI-style string to parse (not owned by this) */
93 const char *arg;
94
95 /* Error buffer (not owned by this) */
96 GString *ini_error;
97};
98
99/* Offset option with "is set" boolean */
100struct offset_opt {
101 int64_t value;
102 bool is_set;
103};
104
105/* Legacy "ctf"/"lttng-live" format options */
106struct ctf_legacy_opts {
107 struct offset_opt offset_s;
108 struct offset_opt offset_ns;
109 bool stream_intersection;
110};
111
112/* Legacy "text" format options */
113struct text_legacy_opts {
114 /*
115 * output, dbg_info_dir, dbg_info_target_prefix, names,
116 * and fields are owned by this.
117 */
118 GString *output;
119 GString *dbg_info_dir;
120 GString *dbg_info_target_prefix;
8eee8ea2
PP
121 const bt_value *names;
122 const bt_value *fields;
9009cc24
PP
123
124 /* Flags */
125 bool no_delta;
126 bool clock_cycles;
127 bool clock_seconds;
128 bool clock_date;
129 bool clock_gmt;
130 bool dbg_info_full_path;
131 bool verbose;
132};
133
134/* Legacy input format format */
135enum legacy_input_format {
136 LEGACY_INPUT_FORMAT_NONE = 0,
137 LEGACY_INPUT_FORMAT_CTF,
138 LEGACY_INPUT_FORMAT_LTTNG_LIVE,
139};
140
141/* Legacy output format format */
142enum legacy_output_format {
143 LEGACY_OUTPUT_FORMAT_NONE = 0,
144 LEGACY_OUTPUT_FORMAT_TEXT,
145 LEGACY_OUTPUT_FORMAT_DUMMY,
146};
147
148/*
149 * Prints the "out of memory" error.
150 */
151static
152void print_err_oom(void)
153{
154 printf_err("Out of memory\n");
155}
156
157/*
158 * Appends an "expecting token" error to the INI-style parsing state's
159 * error buffer.
160 */
161static
162void ini_append_error_expecting(struct ini_parsing_state *state,
163 GScanner *scanner, const char *expecting)
164{
165 size_t i;
166 size_t pos;
167
168 g_string_append_printf(state->ini_error, "Expecting %s:\n", expecting);
169
170 /* Only print error if there's one line */
171 if (strchr(state->arg, '\n') != NULL || strlen(state->arg) == 0) {
172 return;
173 }
174
175 g_string_append_printf(state->ini_error, "\n %s\n", state->arg);
176 pos = g_scanner_cur_position(scanner) + 4;
177
178 if (!g_scanner_eof(scanner)) {
179 pos--;
180 }
181
182 for (i = 0; i < pos; ++i) {
183 g_string_append_printf(state->ini_error, " ");
184 }
185
186 g_string_append_printf(state->ini_error, "^\n\n");
187}
188
68d9d039
PP
189/* Parse the next token as an unsigned integer. */
190static
191bt_value *ini_parse_uint(struct ini_parsing_state *state)
192{
193 bt_value *value = NULL;
194 GTokenType token_type = g_scanner_get_next_token(state->scanner);
195
196 if (token_type != G_TOKEN_INT) {
197 ini_append_error_expecting(state, state->scanner,
198 "integer value");
199 goto end;
200 }
201
202 value = bt_value_unsigned_integer_create_init(
203 state->scanner->value.v_int64);
204
205end:
206 return value;
207}
208
663d0879
SM
209/* Parse the next token as a number and return its negation. */
210static
211bt_value *ini_parse_neg_number(struct ini_parsing_state *state)
212{
213 bt_value *value = NULL;
214 GTokenType token_type = g_scanner_get_next_token(state->scanner);
215
216 switch (token_type) {
217 case G_TOKEN_INT:
218 {
219 /* Negative integer */
220 uint64_t int_val = state->scanner->value.v_int64;
221
4b25c966 222 if (int_val > (((uint64_t) INT64_MAX) + 1)) {
663d0879
SM
223 g_string_append_printf(state->ini_error,
224 "Integer value -%" PRIu64 " is outside the range of a 64-bit signed integer\n",
225 int_val);
226 } else {
68d9d039
PP
227 value = bt_value_signed_integer_create_init(
228 -((int64_t) int_val));
663d0879
SM
229 }
230
231 break;
232 }
233 case G_TOKEN_FLOAT:
234 /* Negative floating point number */
235 value = bt_value_real_create_init(-state->scanner->value.v_float);
236 break;
237 default:
238 ini_append_error_expecting(state, state->scanner, "value");
239 break;
240 }
241
242 return value;
243}
244
7b6a2143
SM
245static bt_value *ini_parse_value(struct ini_parsing_state *state);
246
247/*
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.
251 *
252 * The current token of the parser must be the opening square bracket of the
253 * array.
254 */
255static
256bt_value *ini_parse_array(struct ini_parsing_state *state)
257{
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 == '[');
261
262 bt_value *array_value;
263 GTokenType token_type;
264
265 array_value = bt_value_array_create ();
266 token_type = g_scanner_get_next_token(state->scanner);
267
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;
273
274 item_value = ini_parse_value(state);
275 if (!item_value) {
276 goto error;
277 }
278
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);
282
283 if (status != BT_VALUE_STATUS_OK) {
284 goto error;
285 }
286
287 /*
288 * Ingest the token following the value, it should be either a
289 * comma or closing square brace.
290 */
291 token_type = g_scanner_get_next_token(state->scanner);
292
293 if (token_type == G_TOKEN_CHAR && g_scanner_cur_value(state->scanner).v_char == ',') {
294 /*
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.
299 */
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 ]");
303 goto error;
304 }
305 }
306
307 goto end;
308
309error:
310 BT_VALUE_PUT_REF_AND_RESET(array_value);
311
312end:
313 return array_value;
314}
315
e24a0c1b
SM
316/*
317 * Parse the current token (and the following ones if needed) as a value, return
318 * it as a bt_value.
319 */
320static
321bt_value *ini_parse_value(struct ini_parsing_state *state)
322{
323 bt_value *value = NULL;
324 GTokenType token_type = state->scanner->token;
325
326 switch (token_type) {
327 case G_TOKEN_CHAR:
328 if (state->scanner->value.v_char == '-') {
329 /* Negative number */
330 value = ini_parse_neg_number(state);
68d9d039
PP
331 } else if (state->scanner->value.v_char == '+') {
332 /* Unsigned integer */
333 value = ini_parse_uint(state);
7b6a2143
SM
334 } else if (state->scanner->value.v_char == '[') {
335 /* Array */
336 value = ini_parse_array(state);
e24a0c1b
SM
337 } else {
338 ini_append_error_expecting(state, state->scanner, "value");
339 }
340 break;
341 case G_TOKEN_INT:
342 {
68d9d039 343 /* Positive, signed integer */
e24a0c1b
SM
344 uint64_t int_val = state->scanner->value.v_int64;
345
4b25c966 346 if (int_val > INT64_MAX) {
e24a0c1b
SM
347 g_string_append_printf(state->ini_error,
348 "Integer value %" PRIu64 " is outside the range of a 64-bit signed integer\n",
349 int_val);
350 } else {
68d9d039
PP
351 value = bt_value_signed_integer_create_init(
352 (int64_t) int_val);
e24a0c1b
SM
353 }
354 break;
355 }
356 case G_TOKEN_FLOAT:
357 /* Positive floating point number */
358 value = bt_value_real_create_init(state->scanner->value.v_float);
359 break;
360 case G_TOKEN_STRING:
361 /* Quoted string */
362 value = bt_value_string_create_init(state->scanner->value.v_string);
363 break;
364 case G_TOKEN_IDENTIFIER:
365 {
366 /*
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
370 * identifier.
371 *
372 * If one of the known symbols is not
373 * recognized here, then fall back to creating
374 * a string value.
375 */
376 const char *id = state->scanner->value.v_identifier;
377
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") ||
387 !strcmp(id, "no") ||
388 !strcmp(id, "NO")) {
389 value = bt_value_bool_create_init(false);
390 } else {
391 value = bt_value_string_create_init(id);
392 }
393 break;
394 }
395 default:
396 /* Unset value variable will trigger the error */
397 ini_append_error_expecting(state, state->scanner, "value");
398 break;
399 }
400
401 return value;
402}
403
9009cc24
PP
404static
405int ini_handle_state(struct ini_parsing_state *state)
406{
407 int ret = 0;
408 GTokenType token_type;
8eee8ea2 409 bt_value *value = NULL;
9009cc24
PP
410
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, "'='");
418 break;
419 case INI_EXPECT_VALUE:
9009cc24
PP
420 ini_append_error_expecting(state,
421 state->scanner, "value");
422 break;
423 case INI_EXPECT_MAP_KEY:
424 ini_append_error_expecting(state,
425 state->scanner, "unquoted map key");
426 break;
427 default:
428 break;
429 }
430 goto error;
431 }
432
433 /* We're done! */
434 ret = 1;
435 goto success;
436 }
437
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,
442 "unquoted map key");
443 goto error;
444 }
445
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,
451 "Out of memory\n");
452 goto error;
453 }
454
ce141536
PP
455 if (bt_value_map_has_entry(state->params,
456 state->last_map_key)) {
9009cc24
PP
457 g_string_append_printf(state->ini_error,
458 "Duplicate parameter key: `%s`\n",
459 state->last_map_key);
460 goto error;
461 }
462
463 state->expecting = INI_EXPECT_EQUAL;
464 goto success;
465 case INI_EXPECT_EQUAL:
466 if (token_type != G_TOKEN_CHAR) {
467 ini_append_error_expecting(state,
468 state->scanner, "'='");
469 goto error;
470 }
471
472 if (state->scanner->value.v_char != '=') {
473 ini_append_error_expecting(state,
474 state->scanner, "'='");
475 goto error;
476 }
477
478 state->expecting = INI_EXPECT_VALUE;
479 goto success;
480 case INI_EXPECT_VALUE:
481 {
e24a0c1b 482 value = ini_parse_value(state);
9009cc24 483 if (!value) {
9009cc24
PP
484 goto error;
485 }
486
487 state->expecting = INI_EXPECT_COMMA;
488 goto success;
489 }
9009cc24
PP
490 case INI_EXPECT_COMMA:
491 if (token_type != G_TOKEN_CHAR) {
492 ini_append_error_expecting(state,
493 state->scanner, "','");
494 goto error;
495 }
496
497 if (state->scanner->value.v_char != ',') {
498 ini_append_error_expecting(state,
499 state->scanner, "','");
500 goto error;
501 }
502
503 state->expecting = INI_EXPECT_MAP_KEY;
504 goto success;
505 default:
0fbb9a9f 506 abort();
9009cc24
PP
507 }
508
509error:
510 ret = -1;
511 goto end;
512
513success:
514 if (value) {
ce141536 515 if (bt_value_map_insert_entry(state->params,
9009cc24
PP
516 state->last_map_key, value)) {
517 /* Only override return value on error */
518 ret = -1;
519 }
520 }
521
522end:
8c6884d9 523 BT_VALUE_PUT_REF_AND_RESET(value);
9009cc24
PP
524 return ret;
525}
526
527/*
528 * Converts an INI-style argument to an equivalent map value object.
529 *
530 * Return value is owned by the caller.
531 */
532static
8eee8ea2 533bt_value *bt_value_from_ini(const char *arg,
17582c6d 534 GString *ini_error)
9009cc24
PP
535{
536 /* Lexical scanner configuration */
537 GScannerConfig scanner_config = {
538 /* Skip whitespaces */
539 .cset_skip_characters = " \t\n",
540
541 /* Identifier syntax is: [a-zA-Z_][a-zA-Z0-9_.:-]* */
542 .cset_identifier_first =
543 G_CSET_a_2_z
544 "_"
545 G_CSET_A_2_Z,
546 .cset_identifier_nth =
547 G_CSET_a_2_z
548 "_0123456789-.:"
549 G_CSET_A_2_Z,
550
551 /* "hello" and "Hello" two different keys */
552 .case_sensitive = TRUE,
553
554 /* No comments */
555 .cpair_comment_single = NULL,
556 .skip_comment_multi = TRUE,
557 .skip_comment_single = TRUE,
558 .scan_comment_multi = FALSE,
559
560 /*
561 * Do scan identifiers, including 1-char identifiers,
562 * but NULL is a normal identifier.
563 */
564 .scan_identifier = TRUE,
565 .scan_identifier_1char = TRUE,
566 .scan_identifier_NULL = FALSE,
567
568 /*
569 * No specific symbols: null and boolean "symbols" are
570 * scanned as plain identifiers.
571 */
572 .scan_symbols = FALSE,
573 .symbol_2_token = FALSE,
574 .scope_0_fallback = FALSE,
575
576 /*
577 * Scan "0b"-, "0"-, and "0x"-prefixed integers, but not
578 * integers prefixed with "$".
579 */
580 .scan_binary = TRUE,
581 .scan_octal = TRUE,
582 .scan_float = TRUE,
583 .scan_hex = TRUE,
584 .scan_hex_dollar = FALSE,
585
586 /* Convert scanned numbers to integer tokens */
587 .numbers_2_int = TRUE,
588
589 /* Support both integers and floating-point numbers */
590 .int_2_float = FALSE,
591
592 /* Scan integers as 64-bit signed integers */
593 .store_int64 = TRUE,
594
595 /* Only scan double-quoted strings */
596 .scan_string_sq = FALSE,
597 .scan_string_dq = TRUE,
598
599 /* Do not converter identifiers to string tokens */
600 .identifier_2_string = FALSE,
601
602 /* Scan characters as G_TOKEN_CHAR token */
603 .char_2_token = FALSE,
604 };
605 struct ini_parsing_state state = {
606 .scanner = NULL,
607 .params = NULL,
608 .expecting = INI_EXPECT_MAP_KEY,
609 .arg = arg,
610 .ini_error = ini_error,
611 };
612
ce141536 613 state.params = bt_value_map_create();
9009cc24
PP
614 if (!state.params) {
615 goto error;
616 }
617
618 state.scanner = g_scanner_new(&scanner_config);
619 if (!state.scanner) {
620 goto error;
621 }
622
623 /* Let the scan begin */
624 g_scanner_input_text(state.scanner, arg, strlen(arg));
625
626 while (true) {
627 int ret = ini_handle_state(&state);
628
629 if (ret < 0) {
630 /* Error */
631 goto error;
632 } else if (ret > 0) {
633 /* Done */
634 break;
635 }
636 }
637
638 goto end;
639
640error:
8c6884d9 641 BT_VALUE_PUT_REF_AND_RESET(state.params);
9009cc24
PP
642
643end:
644 if (state.scanner) {
645 g_scanner_destroy(state.scanner);
646 }
647
648 free(state.last_map_key);
649 return state.params;
650}
651
652/*
653 * Returns the parameters map value object from a command-line
654 * parameter option's argument.
655 *
656 * Return value is owned by the caller.
657 */
658static
8eee8ea2 659bt_value *bt_value_from_arg(const char *arg)
9009cc24 660{
8eee8ea2 661 bt_value *params = NULL;
9009cc24
PP
662 GString *ini_error = NULL;
663
664 ini_error = g_string_new(NULL);
665 if (!ini_error) {
666 print_err_oom();
667 goto end;
668 }
669
670 /* Try INI-style parsing */
ce141536 671 params = bt_value_from_ini(arg, ini_error);
9009cc24
PP
672 if (!params) {
673 printf_err("%s", ini_error->str);
674 goto end;
675 }
676
677end:
678 if (ini_error) {
679 g_string_free(ini_error, TRUE);
680 }
17582c6d 681
9009cc24
PP
682 return params;
683}
684
685/*
fd5f8053
PP
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:
9009cc24 689 *
fd5f8053 690 * [NAME:]TYPE.PLUGIN.CLS
9009cc24 691 *
fd5f8053
PP
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.
9009cc24
PP
695 *
696 * On success, both *plugin and *component are not NULL. *plugin
75a1a799
PP
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.
9009cc24
PP
699 */
700static
701void plugin_comp_cls_names(const char *arg, char **name, char **plugin,
ee78f405 702 char **comp_cls, bt_component_class_type *comp_cls_type)
9009cc24
PP
703{
704 const char *at = arg;
705 GString *gs_name = NULL;
fd5f8053 706 GString *gs_comp_cls_type = NULL;
9009cc24
PP
707 GString *gs_plugin = NULL;
708 GString *gs_comp_cls = NULL;
709 size_t end_pos;
710
8b45963b
PP
711 BT_ASSERT(arg);
712 BT_ASSERT(plugin);
713 BT_ASSERT(comp_cls);
714 BT_ASSERT(comp_cls_type);
9009cc24
PP
715
716 if (!bt_common_string_is_printable(arg)) {
717 printf_err("Argument contains a non-printable character\n");
718 goto error;
719 }
720
721 /* Parse the component name */
722 gs_name = bt_common_string_until(at, ".:\\", ":", &end_pos);
723 if (!gs_name) {
724 goto error;
725 }
726
727 if (arg[end_pos] == ':') {
728 at += end_pos + 1;
729 } else {
730 /* No name */
731 g_string_assign(gs_name, "");
732 }
733
fd5f8053
PP
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");
738 goto error;
739 }
740
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;
749 } else {
750 printf_err("Unknown component class type: `%s`\n",
751 gs_comp_cls_type->str);
752 goto error;
753 }
754
755 at += end_pos + 1;
756
9009cc24
PP
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') {
93c1364b 760 printf_err("Missing plugin or component class name\n");
9009cc24
PP
761 goto error;
762 }
763
764 at += end_pos + 1;
765
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) {
fd5f8053 769 printf_err("Missing component class name\n");
9009cc24
PP
770 goto error;
771 }
772
773 if (at[end_pos] != '\0') {
774 /* Found a non-escaped `.` */
775 goto error;
776 }
777
778 if (name) {
779 if (gs_name->len == 0) {
780 *name = NULL;
781 g_string_free(gs_name, TRUE);
782 } else {
783 *name = gs_name->str;
784 g_string_free(gs_name, FALSE);
785 }
786 } else {
787 g_string_free(gs_name, TRUE);
788 }
789
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);
794 gs_name = NULL;
795 gs_plugin = NULL;
796 gs_comp_cls = NULL;
797 goto end;
798
799error:
282c8cd0
PP
800 if (name) {
801 *name = NULL;
802 }
803
804 *plugin = NULL;
805 *comp_cls = NULL;
806
807end:
9009cc24
PP
808 if (gs_name) {
809 g_string_free(gs_name, TRUE);
810 }
811
812 if (gs_plugin) {
813 g_string_free(gs_plugin, TRUE);
814 }
815
816 if (gs_comp_cls) {
817 g_string_free(gs_comp_cls, TRUE);
818 }
819
282c8cd0
PP
820 if (gs_comp_cls_type) {
821 g_string_free(gs_comp_cls_type, TRUE);
9009cc24
PP
822 }
823
9009cc24
PP
824 return;
825}
826
827/*
828 * Prints the Babeltrace version.
829 */
830static
831void print_version(void)
832{
b6a0d2d9
MJ
833 if (GIT_VERSION[0] == '\0') {
834 puts("Babeltrace " VERSION);
835 } else {
836 puts("Babeltrace " VERSION " - " GIT_VERSION);
837 }
9009cc24
PP
838}
839
840/*
841 * Destroys a component configuration.
842 */
843static
8eee8ea2 844void bt_config_component_destroy(bt_object *obj)
9009cc24
PP
845{
846 struct bt_config_component *bt_config_component =
847 container_of(obj, struct bt_config_component, base);
848
849 if (!obj) {
850 goto end;
851 }
852
853 if (bt_config_component->plugin_name) {
854 g_string_free(bt_config_component->plugin_name, TRUE);
855 }
856
857 if (bt_config_component->comp_cls_name) {
858 g_string_free(bt_config_component->comp_cls_name, TRUE);
859 }
860
861 if (bt_config_component->instance_name) {
862 g_string_free(bt_config_component->instance_name, TRUE);
863 }
864
8c6884d9 865 BT_VALUE_PUT_REF_AND_RESET(bt_config_component->params);
9009cc24
PP
866 g_free(bt_config_component);
867
868end:
869 return;
870}
871
872/*
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).
876 *
877 * Return value is owned by the caller.
878 */
879static
880struct bt_config_component *bt_config_component_create(
ee78f405 881 bt_component_class_type type,
c4f81dc9
PP
882 const char *plugin_name, const char *comp_cls_name,
883 int init_log_level)
9009cc24
PP
884{
885 struct bt_config_component *cfg_component = NULL;
886
887 cfg_component = g_new0(struct bt_config_component, 1);
888 if (!cfg_component) {
889 print_err_oom();
890 goto error;
891 }
892
1d7bf349
PP
893 bt_object_init_shared(&cfg_component->base,
894 bt_config_component_destroy);
9009cc24
PP
895 cfg_component->type = type;
896 cfg_component->plugin_name = g_string_new(plugin_name);
897 if (!cfg_component->plugin_name) {
898 print_err_oom();
899 goto error;
900 }
901
902 cfg_component->comp_cls_name = g_string_new(comp_cls_name);
903 if (!cfg_component->comp_cls_name) {
904 print_err_oom();
905 goto error;
906 }
907
908 cfg_component->instance_name = g_string_new(NULL);
909 if (!cfg_component->instance_name) {
910 print_err_oom();
911 goto error;
912 }
913
c4f81dc9
PP
914 cfg_component->log_level = init_log_level;
915
9009cc24 916 /* Start with empty parameters */
ce141536 917 cfg_component->params = bt_value_map_create();
9009cc24
PP
918 if (!cfg_component->params) {
919 print_err_oom();
920 goto error;
921 }
922
923 goto end;
924
925error:
8138bfe1 926 BT_OBJECT_PUT_REF_AND_RESET(cfg_component);
9009cc24
PP
927
928end:
929 return cfg_component;
930}
931
932/*
fd5f8053 933 * Creates a component configuration from a command-line --component
9009cc24
PP
934 * option's argument.
935 */
936static
c4f81dc9
PP
937struct bt_config_component *bt_config_component_from_arg(const char *arg,
938 int init_log_level)
9009cc24
PP
939{
940 struct bt_config_component *cfg_comp = NULL;
941 char *name = NULL;
942 char *plugin_name = NULL;
943 char *comp_cls_name = NULL;
ee78f405 944 bt_component_class_type type;
9009cc24 945
fd5f8053 946 plugin_comp_cls_names(arg, &name, &plugin_name, &comp_cls_name, &type);
9009cc24 947 if (!plugin_name || !comp_cls_name) {
9009cc24
PP
948 goto error;
949 }
950
c4f81dc9
PP
951 cfg_comp = bt_config_component_create(type, plugin_name, comp_cls_name,
952 init_log_level);
9009cc24
PP
953 if (!cfg_comp) {
954 goto error;
955 }
956
957 if (name) {
958 g_string_assign(cfg_comp->instance_name, name);
959 }
960
961 goto end;
962
963error:
8138bfe1 964 BT_OBJECT_PUT_REF_AND_RESET(cfg_comp);
9009cc24
PP
965
966end:
967 g_free(name);
968 g_free(plugin_name);
969 g_free(comp_cls_name);
970 return cfg_comp;
971}
972
973/*
974 * Destroys a configuration.
975 */
976static
8eee8ea2 977void bt_config_destroy(bt_object *obj)
9009cc24
PP
978{
979 struct bt_config *cfg =
980 container_of(obj, struct bt_config, base);
981
982 if (!obj) {
983 goto end;
984 }
985
8c6884d9 986 BT_VALUE_PUT_REF_AND_RESET(cfg->plugin_paths);
9009cc24
PP
987
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);
992 }
993
994 if (cfg->cmd_data.run.filters) {
995 g_ptr_array_free(cfg->cmd_data.run.filters, TRUE);
996 }
997
998 if (cfg->cmd_data.run.sinks) {
999 g_ptr_array_free(cfg->cmd_data.run.sinks, TRUE);
1000 }
1001
1002 if (cfg->cmd_data.run.connections) {
1003 g_ptr_array_free(cfg->cmd_data.run.connections,
1004 TRUE);
1005 }
1006 break;
1007 case BT_CONFIG_COMMAND_LIST_PLUGINS:
1008 break;
1009 case BT_CONFIG_COMMAND_HELP:
8138bfe1 1010 BT_OBJECT_PUT_REF_AND_RESET(cfg->cmd_data.help.cfg_component);
9009cc24
PP
1011 break;
1012 case BT_CONFIG_COMMAND_QUERY:
8138bfe1 1013 BT_OBJECT_PUT_REF_AND_RESET(cfg->cmd_data.query.cfg_component);
9009cc24
PP
1014
1015 if (cfg->cmd_data.query.object) {
1016 g_string_free(cfg->cmd_data.query.object, TRUE);
1017 }
1018 break;
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,
1022 TRUE);
a107deea
PP
1023 g_string_free(
1024 cfg->cmd_data.print_ctf_metadata.output_path,
1025 TRUE);
9009cc24
PP
1026 }
1027 break;
1028 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS:
1029 if (cfg->cmd_data.print_lttng_live_sessions.url) {
1030 g_string_free(
1031 cfg->cmd_data.print_lttng_live_sessions.url,
1032 TRUE);
a107deea
PP
1033 g_string_free(
1034 cfg->cmd_data.print_lttng_live_sessions.output_path,
1035 TRUE);
9009cc24
PP
1036 }
1037 break;
1038 default:
0fbb9a9f 1039 abort();
9009cc24
PP
1040 }
1041
1042 g_free(cfg);
1043
1044end:
1045 return;
1046}
1047
1048static
1049void destroy_glist_of_gstring(GList *list)
1050{
94023a1c
PP
1051 GList *at;
1052
9009cc24
PP
1053 if (!list) {
1054 return;
1055 }
1056
9009cc24
PP
1057 for (at = list; at != NULL; at = g_list_next(at)) {
1058 g_string_free(at->data, TRUE);
1059 }
1060
1061 g_list_free(list);
1062}
1063
1064/*
1065 * Creates a simple lexical scanner for parsing comma-delimited names
1066 * and fields.
1067 *
1068 * Return value is owned by the caller.
1069 */
1070static
1071GScanner *create_csv_identifiers_scanner(void)
1072{
1073 GScanner *scanner;
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,
1092 .scan_hex = 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,
1101 };
1102
1103 scanner = g_scanner_new(&scanner_config);
1104 if (!scanner) {
1105 print_err_oom();
1106 }
1107
1108 return scanner;
1109}
1110
1111/*
1112 * Converts a comma-delimited list of known names (--names option) to
1113 * an array value object containing those names as string value objects.
1114 *
1115 * Return value is owned by the caller.
1116 */
1117static
8eee8ea2 1118bt_value *names_from_arg(const char *arg)
9009cc24
PP
1119{
1120 GScanner *scanner = NULL;
8eee8ea2 1121 bt_value *names = NULL;
9009cc24
PP
1122 bool found_all = false, found_none = false, found_item = false;
1123
ce141536 1124 names = bt_value_array_create();
9009cc24
PP
1125 if (!names) {
1126 print_err_oom();
1127 goto error;
1128 }
1129
1130 scanner = create_csv_identifiers_scanner();
1131 if (!scanner) {
1132 goto error;
1133 }
1134
1135 g_scanner_input_text(scanner, arg, strlen(arg));
1136
1137 while (true) {
1138 GTokenType token_type = g_scanner_get_next_token(scanner);
1139
1140 switch (token_type) {
1141 case G_TOKEN_IDENTIFIER:
1142 {
1143 const char *identifier = scanner->value.v_identifier;
1144
1145 if (!strcmp(identifier, "payload") ||
1146 !strcmp(identifier, "args") ||
1147 !strcmp(identifier, "arg")) {
1148 found_item = true;
ce141536 1149 if (bt_value_array_append_string_element(names,
9009cc24
PP
1150 "payload")) {
1151 goto error;
1152 }
1153 } else if (!strcmp(identifier, "context") ||
1154 !strcmp(identifier, "ctx")) {
1155 found_item = true;
ce141536 1156 if (bt_value_array_append_string_element(names,
9009cc24
PP
1157 "context")) {
1158 goto error;
1159 }
1160 } else if (!strcmp(identifier, "scope") ||
1161 !strcmp(identifier, "header")) {
1162 found_item = true;
ce141536 1163 if (bt_value_array_append_string_element(names,
9009cc24
PP
1164 identifier)) {
1165 goto error;
1166 }
1167 } else if (!strcmp(identifier, "all")) {
1168 found_all = true;
ce141536 1169 if (bt_value_array_append_string_element(names,
9009cc24
PP
1170 identifier)) {
1171 goto error;
1172 }
1173 } else if (!strcmp(identifier, "none")) {
1174 found_none = true;
ce141536 1175 if (bt_value_array_append_string_element(names,
9009cc24
PP
1176 identifier)) {
1177 goto error;
1178 }
1179 } else {
1180 printf_err("Unknown name: `%s`\n",
1181 identifier);
1182 goto error;
1183 }
1184 break;
1185 }
1186 case G_TOKEN_COMMA:
1187 continue;
1188 case G_TOKEN_EOF:
1189 goto end;
1190 default:
1191 goto error;
1192 }
1193 }
1194
1195end:
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");
1198 goto error;
1199 }
1200 /*
1201 * Legacy behavior is to clear the defaults (show none) when at
1202 * least one item is specified.
1203 */
1204 if (found_item && !found_none && !found_all) {
ce141536 1205 if (bt_value_array_append_string_element(names, "none")) {
9009cc24
PP
1206 goto error;
1207 }
1208 }
1209 if (scanner) {
1210 g_scanner_destroy(scanner);
1211 }
1212 return names;
1213
1214error:
8c6884d9 1215 BT_VALUE_PUT_REF_AND_RESET(names);
9009cc24
PP
1216 if (scanner) {
1217 g_scanner_destroy(scanner);
1218 }
1219 return names;
1220}
1221
1222/*
1223 * Converts a comma-delimited list of known fields (--fields option) to
1224 * an array value object containing those fields as string
1225 * value objects.
1226 *
1227 * Return value is owned by the caller.
1228 */
1229static
8eee8ea2 1230bt_value *fields_from_arg(const char *arg)
9009cc24
PP
1231{
1232 GScanner *scanner = NULL;
8eee8ea2 1233 bt_value *fields;
9009cc24 1234
ce141536 1235 fields = bt_value_array_create();
9009cc24
PP
1236 if (!fields) {
1237 print_err_oom();
1238 goto error;
1239 }
1240
1241 scanner = create_csv_identifiers_scanner();
1242 if (!scanner) {
1243 goto error;
1244 }
1245
1246 g_scanner_input_text(scanner, arg, strlen(arg));
1247
1248 while (true) {
1249 GTokenType token_type = g_scanner_get_next_token(scanner);
1250
1251 switch (token_type) {
1252 case G_TOKEN_IDENTIFIER:
1253 {
1254 const char *identifier = scanner->value.v_identifier;
1255
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")) {
ce141536 1265 if (bt_value_array_append_string_element(fields,
9009cc24
PP
1266 identifier)) {
1267 goto error;
1268 }
1269 } else {
1270 printf_err("Unknown field: `%s`\n",
1271 identifier);
1272 goto error;
1273 }
1274 break;
1275 }
1276 case G_TOKEN_COMMA:
1277 continue;
1278 case G_TOKEN_EOF:
1279 goto end;
1280 default:
1281 goto error;
1282 }
1283 }
1284
1285 goto end;
1286
1287error:
8c6884d9 1288 BT_VALUE_PUT_REF_AND_RESET(fields);
9009cc24
PP
1289
1290end:
1291 if (scanner) {
1292 g_scanner_destroy(scanner);
1293 }
1294 return fields;
1295}
1296
1297static
1298void append_param_arg(GString *params_arg, const char *key, const char *value)
1299{
8b45963b
PP
1300 BT_ASSERT(params_arg);
1301 BT_ASSERT(key);
1302 BT_ASSERT(value);
9009cc24
PP
1303
1304 if (params_arg->len != 0) {
1305 g_string_append_c(params_arg, ',');
1306 }
1307
1308 g_string_append(params_arg, key);
1309 g_string_append_c(params_arg, '=');
1310 g_string_append(params_arg, value);
1311}
1312
1313/*
1314 * Inserts the equivalent "prefix-NAME=yes" strings into params_arg
1315 * where the names are in names_array.
1316 */
1317static
1318int insert_flat_params_from_array(GString *params_arg,
8eee8ea2 1319 const bt_value *names_array, const char *prefix)
9009cc24
PP
1320{
1321 int ret = 0;
1322 int i;
1323 GString *tmpstr = NULL, *default_value = NULL;
1324 bool default_set = false, non_default_set = false;
1325
1326 /*
1327 * names_array may be NULL if no CLI options were specified to
1328 * trigger its creation.
1329 */
1330 if (!names_array) {
1331 goto end;
1332 }
1333
1334 tmpstr = g_string_new(NULL);
1335 if (!tmpstr) {
1336 print_err_oom();
1337 ret = -1;
1338 goto end;
1339 }
1340
1341 default_value = g_string_new(NULL);
1342 if (!default_value) {
1343 print_err_oom();
1344 ret = -1;
1345 goto end;
1346 }
1347
44514773 1348 for (i = 0; i < bt_value_array_get_size(names_array); i++) {
8eee8ea2 1349 const bt_value *str_obj =
ce141536
PP
1350 bt_value_array_borrow_element_by_index_const(names_array,
1351 i);
9009cc24
PP
1352 const char *suffix;
1353 bool is_default = false;
1354
1355 if (!str_obj) {
1356 printf_err("Unexpected error\n");
1357 ret = -1;
1358 goto end;
1359 }
1360
b5cdc106 1361 suffix = bt_value_string_get(str_obj);
9009cc24
PP
1362
1363 g_string_assign(tmpstr, prefix);
1364 g_string_append(tmpstr, "-");
1365
1366 /* Special-case for "all" and "none". */
1367 if (!strcmp(suffix, "all")) {
1368 is_default = true;
1369 g_string_assign(default_value, "show");
1370 } else if (!strcmp(suffix, "none")) {
1371 is_default = true;
1372 g_string_assign(default_value, "hide");
1373 }
1374 if (is_default) {
1375 default_set = true;
1376 g_string_append(tmpstr, "default");
1377 append_param_arg(params_arg, tmpstr->str,
1378 default_value->str);
1379 } else {
1380 non_default_set = true;
1381 g_string_append(tmpstr, suffix);
1382 append_param_arg(params_arg, tmpstr->str, "yes");
1383 }
1384 }
1385
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);
1392 }
1393
1394end:
1395 if (default_value) {
1396 g_string_free(default_value, TRUE);
1397 }
1398
1399 if (tmpstr) {
1400 g_string_free(tmpstr, TRUE);
1401 }
1402
1403 return ret;
1404}
1405
1406/* popt options */
1407enum {
1408 OPT_NONE = 0,
1409 OPT_BASE_PARAMS,
1410 OPT_BEGIN,
1411 OPT_CLOCK_CYCLES,
1412 OPT_CLOCK_DATE,
1413 OPT_CLOCK_FORCE_CORRELATE,
1414 OPT_CLOCK_GMT,
1415 OPT_CLOCK_OFFSET,
1416 OPT_CLOCK_OFFSET_NS,
1417 OPT_CLOCK_SECONDS,
1418 OPT_COLOR,
fd5f8053 1419 OPT_COMPONENT,
9009cc24
PP
1420 OPT_CONNECT,
1421 OPT_DEBUG,
8e765000 1422 OPT_DEBUG_INFO,
9009cc24
PP
1423 OPT_DEBUG_INFO_DIR,
1424 OPT_DEBUG_INFO_FULL_PATH,
1425 OPT_DEBUG_INFO_TARGET_PREFIX,
1426 OPT_END,
1427 OPT_FIELDS,
9009cc24
PP
1428 OPT_HELP,
1429 OPT_INPUT_FORMAT,
9009cc24 1430 OPT_LIST,
c4f81dc9 1431 OPT_LOG_LEVEL,
9009cc24
PP
1432 OPT_NAME,
1433 OPT_NAMES,
9009cc24
PP
1434 OPT_NO_DELTA,
1435 OPT_OMIT_HOME_PLUGIN_PATH,
1436 OPT_OMIT_SYSTEM_PLUGIN_PATH,
9009cc24 1437 OPT_OUTPUT,
fd5f8053 1438 OPT_OUTPUT_FORMAT,
9009cc24
PP
1439 OPT_PARAMS,
1440 OPT_PATH,
1441 OPT_PLUGIN_PATH,
1442 OPT_RESET_BASE_PARAMS,
1443 OPT_RETRY_DURATION,
1444 OPT_RUN_ARGS,
1445 OPT_RUN_ARGS_0,
9009cc24
PP
1446 OPT_STREAM_INTERSECTION,
1447 OPT_TIMERANGE,
1448 OPT_URL,
9009cc24
PP
1449 OPT_VERBOSE,
1450};
1451
1452enum bt_config_component_dest {
0a011c88 1453 BT_CONFIG_COMPONENT_DEST_UNKNOWN = -1,
9009cc24
PP
1454 BT_CONFIG_COMPONENT_DEST_SOURCE,
1455 BT_CONFIG_COMPONENT_DEST_FILTER,
1456 BT_CONFIG_COMPONENT_DEST_SINK,
1457};
1458
1459/*
1460 * Adds a configuration component to the appropriate configuration
1461 * array depending on the destination.
1462 */
1463static
1464void add_run_cfg_comp(struct bt_config *cfg,
1465 struct bt_config_component *cfg_comp,
1466 enum bt_config_component_dest dest)
1467{
8138bfe1 1468 bt_object_get_ref(cfg_comp);
9009cc24
PP
1469
1470 switch (dest) {
1471 case BT_CONFIG_COMPONENT_DEST_SOURCE:
1472 g_ptr_array_add(cfg->cmd_data.run.sources, cfg_comp);
1473 break;
1474 case BT_CONFIG_COMPONENT_DEST_FILTER:
1475 g_ptr_array_add(cfg->cmd_data.run.filters, cfg_comp);
1476 break;
1477 case BT_CONFIG_COMPONENT_DEST_SINK:
1478 g_ptr_array_add(cfg->cmd_data.run.sinks, cfg_comp);
1479 break;
1480 default:
0fbb9a9f 1481 abort();
9009cc24
PP
1482 }
1483}
1484
1485static
1486int add_run_cfg_comp_check_name(struct bt_config *cfg,
1487 struct bt_config_component *cfg_comp,
1488 enum bt_config_component_dest dest,
8eee8ea2 1489 bt_value *instance_names)
9009cc24
PP
1490{
1491 int ret = 0;
1492
1493 if (cfg_comp->instance_name->len == 0) {
1494 printf_err("Found an unnamed component\n");
1495 ret = -1;
1496 goto end;
1497 }
1498
ce141536
PP
1499 if (bt_value_map_has_entry(instance_names,
1500 cfg_comp->instance_name->str)) {
9009cc24
PP
1501 printf_err("Duplicate component instance name:\n %s\n",
1502 cfg_comp->instance_name->str);
1503 ret = -1;
1504 goto end;
1505 }
1506
ce141536 1507 if (bt_value_map_insert_entry(instance_names,
9009cc24
PP
1508 cfg_comp->instance_name->str, bt_value_null)) {
1509 print_err_oom();
1510 ret = -1;
1511 goto end;
1512 }
1513
1514 add_run_cfg_comp(cfg, cfg_comp, dest);
1515
1516end:
1517 return ret;
1518}
1519
1520static
8eee8ea2 1521int append_env_var_plugin_paths(bt_value *plugin_paths)
9009cc24
PP
1522{
1523 int ret = 0;
1524 const char *envvar;
1525
1526 if (bt_common_is_setuid_setgid()) {
f4f9e43b 1527 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
9009cc24
PP
1528 goto end;
1529 }
1530
1531 envvar = getenv("BABELTRACE_PLUGIN_PATH");
1532 if (!envvar) {
1533 goto end;
1534 }
1535
1536 ret = bt_config_append_plugin_paths(plugin_paths, envvar);
1537
1538end:
1539 if (ret) {
1540 printf_err("Cannot append plugin paths from BABELTRACE_PLUGIN_PATH\n");
1541 }
1542
1543 return ret;
1544}
1545
1546static
8eee8ea2 1547int append_home_and_system_plugin_paths(bt_value *plugin_paths,
9009cc24
PP
1548 bool omit_system_plugin_path, bool omit_home_plugin_path)
1549{
1550 int ret;
1551
1552 if (!omit_home_plugin_path) {
1553 if (bt_common_is_setuid_setgid()) {
f4f9e43b 1554 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
9009cc24 1555 } else {
c864e29a
PP
1556 char *home_plugin_dir = bt_common_get_home_plugin_path(
1557 BT_LOG_OUTPUT_LEVEL);
9009cc24
PP
1558
1559 if (home_plugin_dir) {
1560 ret = bt_config_append_plugin_paths(
1561 plugin_paths, home_plugin_dir);
1562 free(home_plugin_dir);
1563
1564 if (ret) {
1565 printf_err("Invalid home plugin path\n");
1566 goto error;
1567 }
1568 }
1569 }
1570 }
1571
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");
1576 goto error;
1577 }
1578 }
1579 return 0;
1580error:
1581 printf_err("Cannot append home and system plugin paths\n");
1582 return -1;
1583}
1584
1585static
1586int append_home_and_system_plugin_paths_cfg(struct bt_config *cfg)
1587{
1588 return append_home_and_system_plugin_paths(cfg->plugin_paths,
1589 cfg->omit_system_plugin_path, cfg->omit_home_plugin_path);
1590}
1591
1592static
1593struct bt_config *bt_config_base_create(enum bt_config_command command,
8eee8ea2 1594 const bt_value *initial_plugin_paths,
17582c6d 1595 bool needs_plugins)
9009cc24
PP
1596{
1597 struct bt_config *cfg;
1598
1599 /* Create config */
1600 cfg = g_new0(struct bt_config, 1);
1601 if (!cfg) {
1602 print_err_oom();
1603 goto error;
1604 }
1605
1d7bf349 1606 bt_object_init_shared(&cfg->base, bt_config_destroy);
9009cc24
PP
1607 cfg->command = command;
1608 cfg->command_needs_plugins = needs_plugins;
1609
1610 if (initial_plugin_paths) {
8eee8ea2 1611 bt_value *initial_plugin_paths_copy;
ce141536 1612
6284461f
PP
1613 (void) bt_value_copy(initial_plugin_paths,
1614 &initial_plugin_paths_copy);
ce141536 1615 cfg->plugin_paths = initial_plugin_paths_copy;
9009cc24 1616 } else {
ce141536 1617 cfg->plugin_paths = bt_value_array_create();
9009cc24
PP
1618 if (!cfg->plugin_paths) {
1619 print_err_oom();
1620 goto error;
1621 }
1622 }
1623
1624 goto end;
1625
1626error:
8138bfe1 1627 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
1628
1629end:
1630 return cfg;
1631}
1632
1633static
1634struct bt_config *bt_config_run_create(
8eee8ea2 1635 const bt_value *initial_plugin_paths)
9009cc24
PP
1636{
1637 struct bt_config *cfg;
1638
1639 /* Create config */
1640 cfg = bt_config_base_create(BT_CONFIG_COMMAND_RUN,
1641 initial_plugin_paths, true);
1642 if (!cfg) {
1643 goto error;
1644 }
1645
1646 cfg->cmd_data.run.sources = g_ptr_array_new_with_free_func(
8138bfe1 1647 (GDestroyNotify) bt_object_put_ref);
9009cc24
PP
1648 if (!cfg->cmd_data.run.sources) {
1649 print_err_oom();
1650 goto error;
1651 }
1652
1653 cfg->cmd_data.run.filters = g_ptr_array_new_with_free_func(
8138bfe1 1654 (GDestroyNotify) bt_object_put_ref);
9009cc24
PP
1655 if (!cfg->cmd_data.run.filters) {
1656 print_err_oom();
1657 goto error;
1658 }
1659
1660 cfg->cmd_data.run.sinks = g_ptr_array_new_with_free_func(
8138bfe1 1661 (GDestroyNotify) bt_object_put_ref);
9009cc24
PP
1662 if (!cfg->cmd_data.run.sinks) {
1663 print_err_oom();
1664 goto error;
1665 }
1666
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) {
1670 print_err_oom();
1671 goto error;
1672 }
1673
1674 goto end;
1675
1676error:
8138bfe1 1677 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
1678
1679end:
1680 return cfg;
1681}
1682
1683static
1684struct bt_config *bt_config_list_plugins_create(
8eee8ea2 1685 const bt_value *initial_plugin_paths)
9009cc24
PP
1686{
1687 struct bt_config *cfg;
1688
1689 /* Create config */
1690 cfg = bt_config_base_create(BT_CONFIG_COMMAND_LIST_PLUGINS,
1691 initial_plugin_paths, true);
1692 if (!cfg) {
1693 goto error;
1694 }
1695
1696 goto end;
1697
1698error:
8138bfe1 1699 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
1700
1701end:
1702 return cfg;
1703}
1704
1705static
1706struct bt_config *bt_config_help_create(
c4f81dc9
PP
1707 const bt_value *initial_plugin_paths,
1708 int default_log_level)
9009cc24
PP
1709{
1710 struct bt_config *cfg;
1711
1712 /* Create config */
1713 cfg = bt_config_base_create(BT_CONFIG_COMMAND_HELP,
1714 initial_plugin_paths, true);
1715 if (!cfg) {
1716 goto error;
1717 }
1718
1719 cfg->cmd_data.help.cfg_component =
c4f81dc9 1720 bt_config_component_create(-1, NULL, NULL, default_log_level);
9009cc24
PP
1721 if (!cfg->cmd_data.help.cfg_component) {
1722 goto error;
1723 }
1724
1725 goto end;
1726
1727error:
8138bfe1 1728 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
1729
1730end:
1731 return cfg;
1732}
1733
1734static
1735struct bt_config *bt_config_query_create(
8eee8ea2 1736 const bt_value *initial_plugin_paths)
9009cc24
PP
1737{
1738 struct bt_config *cfg;
1739
1740 /* Create config */
1741 cfg = bt_config_base_create(BT_CONFIG_COMMAND_QUERY,
1742 initial_plugin_paths, true);
1743 if (!cfg) {
1744 goto error;
1745 }
1746
1747 cfg->cmd_data.query.object = g_string_new(NULL);
1748 if (!cfg->cmd_data.query.object) {
1749 print_err_oom();
1750 goto error;
1751 }
1752
1753 goto end;
1754
1755error:
8138bfe1 1756 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
1757
1758end:
1759 return cfg;
1760}
1761
1762static
1763struct bt_config *bt_config_print_ctf_metadata_create(
8eee8ea2 1764 const bt_value *initial_plugin_paths)
9009cc24
PP
1765{
1766 struct bt_config *cfg;
1767
1768 /* Create config */
1769 cfg = bt_config_base_create(BT_CONFIG_COMMAND_PRINT_CTF_METADATA,
1770 initial_plugin_paths, true);
1771 if (!cfg) {
1772 goto error;
1773 }
1774
1775 cfg->cmd_data.print_ctf_metadata.path = g_string_new(NULL);
1776 if (!cfg->cmd_data.print_ctf_metadata.path) {
1777 print_err_oom();
1778 goto error;
1779 }
1780
a107deea
PP
1781 cfg->cmd_data.print_ctf_metadata.output_path = g_string_new(NULL);
1782 if (!cfg->cmd_data.print_ctf_metadata.output_path) {
1783 print_err_oom();
1784 goto error;
1785 }
1786
9009cc24
PP
1787 goto end;
1788
1789error:
8138bfe1 1790 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
1791
1792end:
1793 return cfg;
1794}
1795
1796static
1797struct bt_config *bt_config_print_lttng_live_sessions_create(
8eee8ea2 1798 const bt_value *initial_plugin_paths)
9009cc24
PP
1799{
1800 struct bt_config *cfg;
1801
1802 /* Create config */
1803 cfg = bt_config_base_create(BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS,
1804 initial_plugin_paths, true);
1805 if (!cfg) {
1806 goto error;
1807 }
1808
1809 cfg->cmd_data.print_lttng_live_sessions.url = g_string_new(NULL);
1810 if (!cfg->cmd_data.print_lttng_live_sessions.url) {
1811 print_err_oom();
1812 goto error;
1813 }
1814
a107deea
PP
1815 cfg->cmd_data.print_lttng_live_sessions.output_path =
1816 g_string_new(NULL);
1817 if (!cfg->cmd_data.print_lttng_live_sessions.output_path) {
1818 print_err_oom();
1819 goto error;
1820 }
1821
9009cc24
PP
1822 goto end;
1823
1824error:
8138bfe1 1825 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
1826
1827end:
1828 return cfg;
1829}
1830
1831static
1832int bt_config_append_plugin_paths_check_setuid_setgid(
8eee8ea2 1833 bt_value *plugin_paths, const char *arg)
9009cc24
PP
1834{
1835 int ret = 0;
1836
1837 if (bt_common_is_setuid_setgid()) {
f4f9e43b 1838 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
9009cc24
PP
1839 goto end;
1840 }
1841
1842 if (bt_config_append_plugin_paths(plugin_paths, arg)) {
1843 printf_err("Invalid --plugin-path option's argument:\n %s\n",
1844 arg);
1845 ret = -1;
1846 goto end;
1847 }
1848
1849end:
1850 return ret;
1851}
1852
1853/*
1854 * Prints the expected format for a --params option.
1855 */
1856static
1857void print_expected_params_format(FILE *fp)
1858{
1859 fprintf(fp, "Expected format of PARAMS\n");
1860 fprintf(fp, "-------------------------\n");
1861 fprintf(fp, "\n");
1862 fprintf(fp, " PARAM=VALUE[,PARAM=VALUE]...\n");
1863 fprintf(fp, "\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");
1867 fprintf(fp, "\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");
68d9d039 1872 fprintf(fp, " (`0x` prefix) unsigned (with `+` prefix) or signed 64-bit integer.\n");
9009cc24
PP
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");
7b6a2143
SM
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");
9009cc24
PP
1879 fprintf(fp, "\n");
1880 fprintf(fp, "You can put whitespaces allowed around individual `=` and `,` symbols.\n");
1881 fprintf(fp, "\n");
1882 fprintf(fp, "Example:\n");
1883 fprintf(fp, "\n");
1884 fprintf(fp, " many=null, fresh=yes, condition=false, squirrel=-782329,\n");
68d9d039 1885 fprintf(fp, " play=+23, observe=3.14, simple=beef, needs-quotes=\"some string\",\n");
7b6a2143
SM
1886 fprintf(fp, " escape.chars-are:allowed=\"this is a \\\" double quote\",\n");
1887 fprintf(fp, " things=[1, \"2\", 3]\n");
9009cc24
PP
1888 fprintf(fp, "\n");
1889 fprintf(fp, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
142ac9b0 1890 fprintf(fp, "babeltrace2 from a shell.\n");
9009cc24
PP
1891}
1892
1893
1894/*
1895 * Prints the help command usage.
1896 */
1897static
1898void print_help_usage(FILE *fp)
1899{
142ac9b0
MJ
1900 fprintf(fp, "Usage: babeltrace2 [GENERAL OPTIONS] help [OPTIONS] PLUGIN\n");
1901 fprintf(fp, " babeltrace2 [GENERAL OPTIONS] help [OPTIONS] TYPE.PLUGIN.CLS\n");
9009cc24
PP
1902 fprintf(fp, "\n");
1903 fprintf(fp, "Options:\n");
1904 fprintf(fp, "\n");
9009cc24 1905 fprintf(fp, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
fed88866 1906 fprintf(fp, " (~/.local/lib/babeltrace2/plugins)\n");
9009cc24
PP
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");
9e503aa9 1910 fprintf(fp, " -h, --help Show this help and quit\n");
9009cc24 1911 fprintf(fp, "\n");
142ac9b0 1912 fprintf(fp, "See `babeltrace2 --help` for the list of general options.\n");
9009cc24 1913 fprintf(fp, "\n");
142ac9b0 1914 fprintf(fp, "Use `babeltrace2 list-plugins` to show the list of available plugins.\n");
9009cc24
PP
1915}
1916
1917static
1918struct poptOption help_long_options[] = {
1919 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
9009cc24
PP
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 },
9009cc24
PP
1924 { NULL, 0, '\0', NULL, 0, NULL, NULL },
1925};
1926
1927/*
1928 * Creates a Babeltrace config object from the arguments of a help
1929 * command.
1930 *
1931 * *retcode is set to the appropriate exit code to use.
1932 */
1933static
1934struct 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,
c4f81dc9 1937 const bt_value *initial_plugin_paths, int default_log_level)
9009cc24
PP
1938{
1939 poptContext pc = NULL;
1940 char *arg = NULL;
1941 int opt;
1942 int ret;
1943 struct bt_config *cfg = NULL;
1944 const char *leftover;
1945 char *plugin_name = NULL, *comp_cls_name = NULL;
9009cc24
PP
1946
1947 *retcode = 0;
c4f81dc9 1948 cfg = bt_config_help_create(initial_plugin_paths, default_log_level);
9009cc24
PP
1949 if (!cfg) {
1950 goto error;
1951 }
1952
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);
1956 if (ret) {
1957 goto error;
1958 }
1959
1960 /* Parse options */
1961 pc = poptGetContext(NULL, argc, (const char **) argv,
1962 help_long_options, 0);
1963 if (!pc) {
1964 printf_err("Cannot get popt context\n");
1965 goto error;
1966 }
1967
1968 poptReadDefaultConfig(pc, 0);
1969
1970 while ((opt = poptGetNextOpt(pc)) > 0) {
1971 arg = poptGetOptArg(pc);
1972
1973 switch (opt) {
1974 case OPT_PLUGIN_PATH:
1975 if (bt_config_append_plugin_paths_check_setuid_setgid(
1976 cfg->plugin_paths, arg)) {
1977 goto error;
1978 }
1979 break;
1980 case OPT_OMIT_SYSTEM_PLUGIN_PATH:
1981 cfg->omit_system_plugin_path = true;
1982 break;
1983 case OPT_OMIT_HOME_PLUGIN_PATH:
1984 cfg->omit_home_plugin_path = true;
1985 break;
9009cc24
PP
1986 case OPT_HELP:
1987 print_help_usage(stdout);
1988 *retcode = -1;
8138bfe1 1989 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
1990 goto end;
1991 default:
1992 printf_err("Unknown command-line option specified (option code %d)\n",
1993 opt);
1994 goto error;
1995 }
1996
1997 free(arg);
1998 arg = NULL;
1999 }
2000
2001 /* Check for option parsing error */
2002 if (opt < -1) {
2003 printf_err("While parsing command-line options, at option %s: %s\n",
2004 poptBadOption(pc, 0), poptStrerror(opt));
2005 goto error;
2006 }
2007
2008 leftover = poptGetArg(pc);
2009 if (leftover) {
75a1a799 2010 plugin_comp_cls_names(leftover, NULL,
fd5f8053
PP
2011 &plugin_name, &comp_cls_name,
2012 &cfg->cmd_data.help.cfg_component->type);
9009cc24 2013 if (plugin_name && comp_cls_name) {
75a1a799
PP
2014 /* Component class help */
2015 g_string_assign(
2016 cfg->cmd_data.help.cfg_component->plugin_name,
9009cc24 2017 plugin_name);
75a1a799
PP
2018 g_string_assign(
2019 cfg->cmd_data.help.cfg_component->comp_cls_name,
9009cc24
PP
2020 comp_cls_name);
2021 } else {
75a1a799 2022 /* Fall back to plugin help */
75a1a799
PP
2023 g_string_assign(
2024 cfg->cmd_data.help.cfg_component->plugin_name,
2025 leftover);
9009cc24 2026 }
75a1a799
PP
2027 } else {
2028 print_help_usage(stdout);
2029 *retcode = -1;
8138bfe1 2030 BT_OBJECT_PUT_REF_AND_RESET(cfg);
75a1a799 2031 goto end;
9009cc24
PP
2032 }
2033
2034 if (append_home_and_system_plugin_paths_cfg(cfg)) {
2035 goto error;
2036 }
2037
2038 goto end;
2039
2040error:
2041 *retcode = 1;
8138bfe1 2042 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
2043
2044end:
9009cc24
PP
2045 g_free(plugin_name);
2046 g_free(comp_cls_name);
2047
2048 if (pc) {
2049 poptFreeContext(pc);
2050 }
2051
2052 free(arg);
2053 return cfg;
2054}
2055
2056/*
2057 * Prints the help command usage.
2058 */
2059static
2060void print_query_usage(FILE *fp)
2061{
142ac9b0 2062 fprintf(fp, "Usage: babeltrace2 [GEN OPTS] query [OPTS] TYPE.PLUGIN.CLS OBJECT\n");
9009cc24
PP
2063 fprintf(fp, "\n");
2064 fprintf(fp, "Options:\n");
2065 fprintf(fp, "\n");
9009cc24 2066 fprintf(fp, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
fed88866 2067 fprintf(fp, " (~/.local/lib/babeltrace2/plugins)\n");
9009cc24
PP
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");
9e503aa9 2073 fprintf(fp, " -h, --help Show this help and quit\n");
9009cc24
PP
2074 fprintf(fp, "\n\n");
2075 print_expected_params_format(fp);
2076}
2077
2078static
2079struct poptOption query_long_options[] = {
2080 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
9009cc24
PP
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 },
9009cc24
PP
2086 { NULL, 0, '\0', NULL, 0, NULL, NULL },
2087};
2088
2089/*
2090 * Creates a Babeltrace config object from the arguments of a query
2091 * command.
2092 *
2093 * *retcode is set to the appropriate exit code to use.
2094 */
2095static
2096struct 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,
c4f81dc9
PP
2099 const bt_value *initial_plugin_paths,
2100 int default_log_level)
9009cc24
PP
2101{
2102 poptContext pc = NULL;
2103 char *arg = NULL;
2104 int opt;
2105 int ret;
2106 struct bt_config *cfg = NULL;
2107 const char *leftover;
13041794
SM
2108 bt_value *params;
2109
2110 params = bt_value_null;
2111 bt_value_get_ref(bt_value_null);
9009cc24
PP
2112
2113 *retcode = 0;
2114 cfg = bt_config_query_create(initial_plugin_paths);
2115 if (!cfg) {
2116 goto error;
2117 }
2118
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);
2122 if (ret) {
2123 goto error;
2124 }
2125
2126 /* Parse options */
2127 pc = poptGetContext(NULL, argc, (const char **) argv,
2128 query_long_options, 0);
2129 if (!pc) {
2130 printf_err("Cannot get popt context\n");
2131 goto error;
2132 }
2133
2134 poptReadDefaultConfig(pc, 0);
2135
2136 while ((opt = poptGetNextOpt(pc)) > 0) {
2137 arg = poptGetOptArg(pc);
2138
2139 switch (opt) {
2140 case OPT_PLUGIN_PATH:
2141 if (bt_config_append_plugin_paths_check_setuid_setgid(
2142 cfg->plugin_paths, arg)) {
2143 goto error;
2144 }
2145 break;
2146 case OPT_OMIT_SYSTEM_PLUGIN_PATH:
2147 cfg->omit_system_plugin_path = true;
2148 break;
2149 case OPT_OMIT_HOME_PLUGIN_PATH:
2150 cfg->omit_home_plugin_path = true;
2151 break;
9009cc24
PP
2152 case OPT_PARAMS:
2153 {
8c6884d9 2154 bt_value_put_ref(params);
ce141536 2155 params = bt_value_from_arg(arg);
9009cc24
PP
2156 if (!params) {
2157 printf_err("Invalid format for --params option's argument:\n %s\n",
2158 arg);
2159 goto error;
2160 }
2161 break;
2162 }
2163 case OPT_HELP:
2164 print_query_usage(stdout);
2165 *retcode = -1;
8138bfe1 2166 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
2167 goto end;
2168 default:
2169 printf_err("Unknown command-line option specified (option code %d)\n",
2170 opt);
2171 goto error;
2172 }
2173
2174 free(arg);
2175 arg = NULL;
2176 }
2177
9009cc24
PP
2178 /* Check for option parsing error */
2179 if (opt < -1) {
2180 printf_err("While parsing command-line options, at option %s: %s\n",
2181 poptBadOption(pc, 0), poptStrerror(opt));
2182 goto error;
2183 }
2184
2185 /*
93c1364b
PP
2186 * We need exactly two leftover arguments which are the
2187 * mandatory component class specification and query object.
9009cc24 2188 */
93c1364b
PP
2189 leftover = poptGetArg(pc);
2190 if (leftover) {
2191 cfg->cmd_data.query.cfg_component =
c4f81dc9
PP
2192 bt_config_component_from_arg(leftover,
2193 default_log_level);
93c1364b
PP
2194 if (!cfg->cmd_data.query.cfg_component) {
2195 printf_err("Invalid format for component class specification:\n %s\n",
2196 leftover);
2197 goto error;
2198 }
2199
8b45963b 2200 BT_ASSERT(params);
17582c6d
PP
2201 BT_OBJECT_MOVE_REF(cfg->cmd_data.query.cfg_component->params,
2202 params);
93c1364b
PP
2203 } else {
2204 print_query_usage(stdout);
2205 *retcode = -1;
8138bfe1 2206 BT_OBJECT_PUT_REF_AND_RESET(cfg);
93c1364b
PP
2207 goto end;
2208 }
2209
9009cc24
PP
2210 leftover = poptGetArg(pc);
2211 if (leftover) {
2212 if (strlen(leftover) == 0) {
2213 printf_err("Invalid empty object\n");
2214 goto error;
2215 }
2216
2217 g_string_assign(cfg->cmd_data.query.object, leftover);
2218 } else {
2219 print_query_usage(stdout);
2220 *retcode = -1;
8138bfe1 2221 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
2222 goto end;
2223 }
2224
2225 leftover = poptGetArg(pc);
2226 if (leftover) {
2227 printf_err("Unexpected argument: %s\n", leftover);
2228 goto error;
2229 }
2230
2231 if (append_home_and_system_plugin_paths_cfg(cfg)) {
2232 goto error;
2233 }
2234
2235 goto end;
2236
2237error:
2238 *retcode = 1;
8138bfe1 2239 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
2240
2241end:
2242 if (pc) {
2243 poptFreeContext(pc);
2244 }
2245
8c6884d9 2246 bt_value_put_ref(params);
9009cc24
PP
2247 free(arg);
2248 return cfg;
2249}
2250
2251/*
2252 * Prints the list-plugins command usage.
2253 */
2254static
2255void print_list_plugins_usage(FILE *fp)
2256{
142ac9b0 2257 fprintf(fp, "Usage: babeltrace2 [GENERAL OPTIONS] list-plugins [OPTIONS]\n");
9009cc24
PP
2258 fprintf(fp, "\n");
2259 fprintf(fp, "Options:\n");
2260 fprintf(fp, "\n");
2261 fprintf(fp, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
fed88866 2262 fprintf(fp, " (~/.local/lib/babeltrace2/plugins)\n");
9009cc24
PP
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");
9e503aa9 2266 fprintf(fp, " -h, --help Show this help and quit\n");
9009cc24 2267 fprintf(fp, "\n");
142ac9b0 2268 fprintf(fp, "See `babeltrace2 --help` for the list of general options.\n");
9009cc24 2269 fprintf(fp, "\n");
142ac9b0 2270 fprintf(fp, "Use `babeltrace2 help` to get help for a specific plugin or component class.\n");
9009cc24
PP
2271}
2272
2273static
2274struct 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 },
2281};
2282
2283/*
2284 * Creates a Babeltrace config object from the arguments of a
2285 * list-plugins command.
2286 *
2287 * *retcode is set to the appropriate exit code to use.
2288 */
2289static
2290struct 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,
8eee8ea2 2293 const bt_value *initial_plugin_paths)
9009cc24
PP
2294{
2295 poptContext pc = NULL;
2296 char *arg = NULL;
2297 int opt;
2298 int ret;
2299 struct bt_config *cfg = NULL;
2300 const char *leftover;
2301
2302 *retcode = 0;
2303 cfg = bt_config_list_plugins_create(initial_plugin_paths);
2304 if (!cfg) {
2305 goto error;
2306 }
2307
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);
2311 if (ret) {
2312 goto error;
2313 }
2314
2315 /* Parse options */
2316 pc = poptGetContext(NULL, argc, (const char **) argv,
2317 list_plugins_long_options, 0);
2318 if (!pc) {
2319 printf_err("Cannot get popt context\n");
2320 goto error;
2321 }
2322
2323 poptReadDefaultConfig(pc, 0);
2324
2325 while ((opt = poptGetNextOpt(pc)) > 0) {
2326 arg = poptGetOptArg(pc);
2327
2328 switch (opt) {
2329 case OPT_PLUGIN_PATH:
2330 if (bt_config_append_plugin_paths_check_setuid_setgid(
2331 cfg->plugin_paths, arg)) {
2332 goto error;
2333 }
2334 break;
2335 case OPT_OMIT_SYSTEM_PLUGIN_PATH:
2336 cfg->omit_system_plugin_path = true;
2337 break;
2338 case OPT_OMIT_HOME_PLUGIN_PATH:
2339 cfg->omit_home_plugin_path = true;
2340 break;
2341 case OPT_HELP:
2342 print_list_plugins_usage(stdout);
2343 *retcode = -1;
8138bfe1 2344 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
2345 goto end;
2346 default:
2347 printf_err("Unknown command-line option specified (option code %d)\n",
2348 opt);
2349 goto error;
2350 }
2351
2352 free(arg);
2353 arg = NULL;
2354 }
2355
2356 /* Check for option parsing error */
2357 if (opt < -1) {
2358 printf_err("While parsing command-line options, at option %s: %s\n",
2359 poptBadOption(pc, 0), poptStrerror(opt));
2360 goto error;
2361 }
2362
2363 leftover = poptGetArg(pc);
2364 if (leftover) {
2365 printf_err("Unexpected argument: %s\n", leftover);
2366 goto error;
2367 }
2368
2369 if (append_home_and_system_plugin_paths_cfg(cfg)) {
2370 goto error;
2371 }
2372
2373 goto end;
2374
2375error:
2376 *retcode = 1;
8138bfe1 2377 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
2378
2379end:
2380 if (pc) {
2381 poptFreeContext(pc);
2382 }
2383
2384 free(arg);
2385 return cfg;
2386}
2387
2388/*
2389 * Prints the run command usage.
2390 */
2391static
2392void print_run_usage(FILE *fp)
2393{
142ac9b0 2394 fprintf(fp, "Usage: babeltrace2 [GENERAL OPTIONS] run [OPTIONS]\n");
9009cc24
PP
2395 fprintf(fp, "\n");
2396 fprintf(fp, "Options:\n");
2397 fprintf(fp, "\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");
fd5f8053
PP
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");
d5128b09 2408 fprintf(fp, " -x, --connect=CONNECTION Connect two created components (see the\n");
9009cc24 2409 fprintf(fp, " expected format of CONNECTION below)\n");
c4f81dc9
PP
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");
9009cc24
PP
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");
fed88866 2416 fprintf(fp, " (~/.local/lib/babeltrace2/plugins)\n");
9009cc24
PP
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");
142ac9b0 2425 fprintf(fp, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
9009cc24
PP
2426 fprintf(fp, " the graph later, retry in DUR µs\n");
2427 fprintf(fp, " (default: 100000)\n");
9e503aa9 2428 fprintf(fp, " -h, --help Show this help and quit\n");
9009cc24 2429 fprintf(fp, "\n");
142ac9b0 2430 fprintf(fp, "See `babeltrace2 --help` for the list of general options.\n");
9009cc24
PP
2431 fprintf(fp, "\n\n");
2432 fprintf(fp, "Expected format of CONNECTION\n");
2433 fprintf(fp, "-----------------------------\n");
2434 fprintf(fp, "\n");
2435 fprintf(fp, " UPSTREAM[.UPSTREAM-PORT]:DOWNSTREAM[.DOWNSTREAM-PORT]\n");
2436 fprintf(fp, "\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");
2441 fprintf(fp, "\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");
2445 fprintf(fp, "\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");
2450 fprintf(fp, "\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");
2454 fprintf(fp, "\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");
2457 fprintf(fp, "\n");
2458 fprintf(fp, "Examples:\n");
2459 fprintf(fp, "\n");
2460 fprintf(fp, " my-src:my-sink\n");
2461 fprintf(fp, " ctf-fs.*stream*:utils-muxer:*\n");
2462 fprintf(fp, "\n");
2463 fprintf(fp, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
142ac9b0 2464 fprintf(fp, "babeltrace2 from a shell.\n");
9009cc24
PP
2465 fprintf(fp, "\n\n");
2466 print_expected_params_format(fp);
2467}
2468
2469/*
2470 * Creates a Babeltrace config object from the arguments of a run
2471 * command.
2472 *
2473 * *retcode is set to the appropriate exit code to use.
2474 */
2475static
2476struct 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,
c4f81dc9 2479 const bt_value *initial_plugin_paths, int default_log_level)
9009cc24
PP
2480{
2481 poptContext pc = NULL;
2482 char *arg = NULL;
2483 struct bt_config_component *cur_cfg_comp = NULL;
0a011c88
JG
2484 enum bt_config_component_dest cur_cfg_comp_dest =
2485 BT_CONFIG_COMPONENT_DEST_UNKNOWN;
8eee8ea2 2486 bt_value *cur_base_params = NULL;
9009cc24
PP
2487 int opt, ret = 0;
2488 struct bt_config *cfg = NULL;
8eee8ea2
PP
2489 bt_value *instance_names = NULL;
2490 bt_value *connection_args = NULL;
9009cc24 2491 char error_buf[256] = { 0 };
738302b8 2492 long retry_duration = -1;
ee78f405 2493 bt_value_status status;
9009cc24
PP
2494 struct poptOption run_long_options[] = {
2495 { "base-params", 'b', POPT_ARG_STRING, NULL, OPT_BASE_PARAMS, NULL, NULL },
fd5f8053 2496 { "component", 'c', POPT_ARG_STRING, NULL, OPT_COMPONENT, NULL, NULL },
d5128b09 2497 { "connect", 'x', POPT_ARG_STRING, NULL, OPT_CONNECT, NULL, NULL },
9009cc24 2498 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
c4f81dc9 2499 { "log-level", 'l', POPT_ARG_STRING, NULL, OPT_LOG_LEVEL, NULL, NULL },
9009cc24
PP
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 },
738302b8 2506 { "retry-duration", '\0', POPT_ARG_LONG, &retry_duration, OPT_RETRY_DURATION, NULL, NULL },
9009cc24
PP
2507 { NULL, 0, '\0', NULL, 0, NULL, NULL },
2508 };
2509
2510 *retcode = 0;
9009cc24
PP
2511
2512 if (argc <= 1) {
2513 print_run_usage(stdout);
2514 *retcode = -1;
2515 goto end;
2516 }
2517
2518 cfg = bt_config_run_create(initial_plugin_paths);
2519 if (!cfg) {
2520 goto error;
2521 }
2522
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;
ce141536 2526 cur_base_params = bt_value_map_create();
9009cc24
PP
2527 if (!cur_base_params) {
2528 print_err_oom();
2529 goto error;
2530 }
2531
ce141536 2532 instance_names = bt_value_map_create();
9009cc24
PP
2533 if (!instance_names) {
2534 print_err_oom();
2535 goto error;
2536 }
2537
ce141536 2538 connection_args = bt_value_array_create();
9009cc24
PP
2539 if (!connection_args) {
2540 print_err_oom();
2541 goto error;
2542 }
2543
2544 ret = append_env_var_plugin_paths(cfg->plugin_paths);
2545 if (ret) {
2546 goto error;
2547 }
2548
2549 /* Parse options */
2550 pc = poptGetContext(NULL, argc, (const char **) argv,
2551 run_long_options, 0);
2552 if (!pc) {
2553 printf_err("Cannot get popt context\n");
2554 goto error;
2555 }
2556
2557 poptReadDefaultConfig(pc, 0);
2558
2559 while ((opt = poptGetNextOpt(pc)) > 0) {
2560 arg = poptGetOptArg(pc);
2561
2562 switch (opt) {
2563 case OPT_PLUGIN_PATH:
2564 if (bt_config_append_plugin_paths_check_setuid_setgid(
2565 cfg->plugin_paths, arg)) {
2566 goto error;
2567 }
2568 break;
2569 case OPT_OMIT_SYSTEM_PLUGIN_PATH:
2570 cfg->omit_system_plugin_path = true;
2571 break;
2572 case OPT_OMIT_HOME_PLUGIN_PATH:
2573 cfg->omit_home_plugin_path = true;
2574 break;
fd5f8053 2575 case OPT_COMPONENT:
9009cc24 2576 {
9009cc24 2577 enum bt_config_component_dest new_dest;
9009cc24
PP
2578
2579 if (cur_cfg_comp) {
2580 ret = add_run_cfg_comp_check_name(cfg,
2581 cur_cfg_comp, cur_cfg_comp_dest,
2582 instance_names);
8138bfe1 2583 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp);
9009cc24
PP
2584 if (ret) {
2585 goto error;
2586 }
2587 }
2588
c4f81dc9
PP
2589 cur_cfg_comp = bt_config_component_from_arg(arg,
2590 default_log_level);
9009cc24 2591 if (!cur_cfg_comp) {
fd5f8053
PP
2592 printf_err("Invalid format for --component option's argument:\n %s\n",
2593 arg);
9009cc24
PP
2594 goto error;
2595 }
2596
fd5f8053
PP
2597 switch (cur_cfg_comp->type) {
2598 case BT_COMPONENT_CLASS_TYPE_SOURCE:
2599 new_dest = BT_CONFIG_COMPONENT_DEST_SOURCE;
2600 break;
2601 case BT_COMPONENT_CLASS_TYPE_FILTER:
2602 new_dest = BT_CONFIG_COMPONENT_DEST_FILTER;
2603 break;
2604 case BT_COMPONENT_CLASS_TYPE_SINK:
2605 new_dest = BT_CONFIG_COMPONENT_DEST_SINK;
2606 break;
2607 default:
2608 abort();
2609 }
2610
8b45963b 2611 BT_ASSERT(cur_base_params);
8c6884d9 2612 bt_value_put_ref(cur_cfg_comp->params);
6284461f
PP
2613 status = bt_value_copy(cur_base_params,
2614 &cur_cfg_comp->params);
b5cdc106 2615 if (status != BT_VALUE_STATUS_OK) {
9009cc24
PP
2616 print_err_oom();
2617 goto error;
2618 }
2619
2620 cur_cfg_comp_dest = new_dest;
2621 break;
2622 }
2623 case OPT_PARAMS:
2624 {
8eee8ea2
PP
2625 bt_value *params;
2626 bt_value *params_to_set;
9009cc24
PP
2627
2628 if (!cur_cfg_comp) {
2629 printf_err("Cannot add parameters to unavailable component:\n %s\n",
2630 arg);
2631 goto error;
2632 }
2633
ce141536 2634 params = bt_value_from_arg(arg);
9009cc24
PP
2635 if (!params) {
2636 printf_err("Invalid format for --params option's argument:\n %s\n",
2637 arg);
2638 goto error;
2639 }
2640
7be9d1d3
PP
2641 status = bt_value_map_extend(cur_cfg_comp->params,
2642 params, &params_to_set);
8c6884d9 2643 BT_VALUE_PUT_REF_AND_RESET(params);
b5cdc106 2644 if (status != BT_VALUE_STATUS_OK) {
9009cc24
PP
2645 printf_err("Cannot extend current component parameters with --params option's argument:\n %s\n",
2646 arg);
2647 goto error;
2648 }
2649
8138bfe1 2650 BT_OBJECT_MOVE_REF(cur_cfg_comp->params, params_to_set);
9009cc24
PP
2651 break;
2652 }
9009cc24
PP
2653 case OPT_NAME:
2654 if (!cur_cfg_comp) {
2655 printf_err("Cannot set the name of unavailable component:\n %s\n",
2656 arg);
2657 goto error;
2658 }
2659
2660 g_string_assign(cur_cfg_comp->instance_name, arg);
2661 break;
c4f81dc9
PP
2662 case OPT_LOG_LEVEL:
2663 if (!cur_cfg_comp) {
2664 printf_err("Cannot set the log level of unavailable component:\n %s\n",
2665 arg);
2666 goto error;
2667 }
2668
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",
2673 arg);
2674 goto error;
2675 }
2676 break;
9009cc24
PP
2677 case OPT_BASE_PARAMS:
2678 {
8eee8ea2 2679 bt_value *params =
ce141536 2680 bt_value_from_arg(arg);
9009cc24
PP
2681
2682 if (!params) {
2683 printf_err("Invalid format for --base-params option's argument:\n %s\n",
2684 arg);
2685 goto error;
2686 }
2687
8138bfe1 2688 BT_OBJECT_MOVE_REF(cur_base_params, params);
9009cc24
PP
2689 break;
2690 }
2691 case OPT_RESET_BASE_PARAMS:
8c6884d9 2692 BT_VALUE_PUT_REF_AND_RESET(cur_base_params);
ce141536 2693 cur_base_params = bt_value_map_create();
9009cc24
PP
2694 if (!cur_base_params) {
2695 print_err_oom();
2696 goto error;
2697 }
2698 break;
2699 case OPT_CONNECT:
ce141536 2700 if (bt_value_array_append_string_element(
44514773 2701 connection_args, arg)) {
9009cc24
PP
2702 print_err_oom();
2703 goto error;
2704 }
2705 break;
2706 case OPT_RETRY_DURATION:
2707 if (retry_duration < 0) {
b25dc5ae 2708 printf_err("--retry-duration option's argument must be positive or 0: %ld\n",
9009cc24
PP
2709 retry_duration);
2710 goto error;
2711 }
2712
2713 cfg->cmd_data.run.retry_duration_us =
2714 (uint64_t) retry_duration;
2715 break;
2716 case OPT_HELP:
2717 print_run_usage(stdout);
2718 *retcode = -1;
8138bfe1 2719 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
2720 goto end;
2721 default:
2722 printf_err("Unknown command-line option specified (option code %d)\n",
2723 opt);
2724 goto error;
2725 }
2726
2727 free(arg);
2728 arg = NULL;
2729 }
2730
2731 /* Check for option parsing error */
2732 if (opt < -1) {
2733 printf_err("While parsing command-line options, at option %s: %s\n",
2734 poptBadOption(pc, 0), poptStrerror(opt));
2735 goto error;
2736 }
2737
2738 /* This command does not accept leftover arguments */
2739 if (poptPeekArg(pc)) {
2740 printf_err("Unexpected argument: %s\n", poptPeekArg(pc));
2741 goto error;
2742 }
2743
2744 /* Add current component */
2745 if (cur_cfg_comp) {
2746 ret = add_run_cfg_comp_check_name(cfg, cur_cfg_comp,
2747 cur_cfg_comp_dest, instance_names);
8138bfe1 2748 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp);
9009cc24
PP
2749 if (ret) {
2750 goto error;
2751 }
2752 }
2753
2754 if (cfg->cmd_data.run.sources->len == 0) {
2755 printf_err("Incomplete graph: no source component\n");
2756 goto error;
2757 }
2758
2759 if (cfg->cmd_data.run.sinks->len == 0) {
2760 printf_err("Incomplete graph: no sink component\n");
2761 goto error;
2762 }
2763
2764 if (append_home_and_system_plugin_paths_cfg(cfg)) {
2765 goto error;
2766 }
2767
17582c6d 2768 ret = bt_config_cli_args_create_connections(cfg,
ce141536 2769 connection_args,
9009cc24
PP
2770 error_buf, 256);
2771 if (ret) {
2772 printf_err("Cannot creation connections:\n%s", error_buf);
2773 goto error;
2774 }
2775
2776 goto end;
2777
2778error:
2779 *retcode = 1;
8138bfe1 2780 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
2781
2782end:
2783 if (pc) {
2784 poptFreeContext(pc);
2785 }
2786
9009cc24 2787 free(arg);
8138bfe1 2788 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp);
8c6884d9
PP
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);
9009cc24
PP
2792 return cfg;
2793}
2794
2795static
8eee8ea2 2796struct bt_config *bt_config_run_from_args_array(const bt_value *run_args,
9009cc24
PP
2797 int *retcode, bool force_omit_system_plugin_path,
2798 bool force_omit_home_plugin_path,
c4f81dc9 2799 const bt_value *initial_plugin_paths, int default_log_level)
9009cc24
PP
2800{
2801 struct bt_config *cfg = NULL;
2802 const char **argv;
0d3e053b 2803 int64_t i, len;
44514773 2804 const size_t argc = bt_value_array_get_size(run_args) + 1;
9009cc24
PP
2805
2806 argv = calloc(argc, sizeof(*argv));
2807 if (!argv) {
2808 print_err_oom();
2809 goto end;
2810 }
2811
2812 argv[0] = "run";
2813
44514773 2814 len = bt_value_array_get_size(run_args);
0d3e053b
MD
2815 if (len < 0) {
2816 printf_err("Invalid executable arguments\n");
2817 goto end;
2818 }
2819 for (i = 0; i < len; i++) {
8eee8ea2 2820 const bt_value *arg_value =
ce141536
PP
2821 bt_value_array_borrow_element_by_index_const(run_args,
2822 i);
9009cc24
PP
2823 const char *arg;
2824
8b45963b 2825 BT_ASSERT(arg_value);
b5cdc106 2826 arg = bt_value_string_get(arg_value);
8b45963b 2827 BT_ASSERT(arg);
9009cc24 2828 argv[i + 1] = arg;
9009cc24
PP
2829 }
2830
2831 cfg = bt_config_run_from_args(argc, argv, retcode,
2832 force_omit_system_plugin_path, force_omit_home_plugin_path,
c4f81dc9 2833 initial_plugin_paths, default_log_level);
9009cc24
PP
2834
2835end:
2836 free(argv);
2837 return cfg;
2838}
2839
2840/*
2841 * Prints the convert command usage.
2842 */
2843static
2844void print_convert_usage(FILE *fp)
2845{
142ac9b0 2846 fprintf(fp, "Usage: babeltrace2 [GENERAL OPTIONS] [convert] [OPTIONS] [PATH/URL]\n");
9009cc24
PP
2847 fprintf(fp, "\n");
2848 fprintf(fp, "Options:\n");
2849 fprintf(fp, "\n");
fd5f8053
PP
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");
c4f81dc9
PP
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");
9009cc24
PP
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");
fed88866 2863 fprintf(fp, " (~/.local/lib/babeltrace2/plugins)\n");
9009cc24
PP
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");
142ac9b0 2871 fprintf(fp, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
9009cc24
PP
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");
8320cc93
PP
2881 fprintf(fp, " --stream-intersection Only process events when all streams\n");
2882 fprintf(fp, " are active\n");
9009cc24
PP
2883 fprintf(fp, " -u, --url=URL Set the `url` string parameter of the\n");
2884 fprintf(fp, " current component to URL\n");
9e503aa9 2885 fprintf(fp, " -h, --help Show this help and quit\n");
9009cc24 2886 fprintf(fp, "\n");
e7ad156c 2887 fprintf(fp, "Implicit `source.ctf.fs` component options:\n");
9009cc24
PP
2888 fprintf(fp, "\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");
9009cc24 2891 fprintf(fp, "\n");
e7ad156c 2892 fprintf(fp, "Implicit `sink.text.pretty` component options:\n");
9009cc24
PP
2893 fprintf(fp, "\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");
2915 fprintf(fp, "\n");
e7ad156c 2916 fprintf(fp, "Implicit `filter.utils.muxer` component options:\n");
9009cc24
PP
2917 fprintf(fp, "\n");
2918 fprintf(fp, " --clock-force-correlate Assume that clocks are inherently\n");
2919 fprintf(fp, " correlated across traces\n");
2920 fprintf(fp, "\n");
e7ad156c 2921 fprintf(fp, "Implicit `filter.utils.trimmer` component options:\n");
9009cc24
PP
2922 fprintf(fp, "\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");
2931 fprintf(fp, "\n");
e7ad156c 2932 fprintf(fp, "Implicit `filter.lttng-utils.debug-info` component options:\n");
9009cc24 2933 fprintf(fp, "\n");
8e765000
PP
2934 fprintf(fp, " --debug-info Create an implicit\n");
2935 fprintf(fp, " `filter.lttng-utils.debug-info` component\n");
9009cc24
PP
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");
9009cc24
PP
2944 fprintf(fp, "\n");
2945 fprintf(fp, "Legacy options that still work:\n");
2946 fprintf(fp, "\n");
2947 fprintf(fp, " -i, --input-format=(ctf | lttng-live)\n");
2948 fprintf(fp, " `ctf`:\n");
fd5f8053 2949 fprintf(fp, " Create an implicit `source.ctf.fs`\n");
9009cc24
PP
2950 fprintf(fp, " component\n");
2951 fprintf(fp, " `lttng-live`:\n");
fd5f8053
PP
2952 fprintf(fp, " Create an implicit `source.ctf.lttng-live`\n");
2953 fprintf(fp, " component\n");
e7ad156c 2954 fprintf(fp, " -o, --output-format=(text | ctf | dummy | ctf-metadata)\n");
9009cc24 2955 fprintf(fp, " `text`:\n");
fd5f8053 2956 fprintf(fp, " Create an implicit `sink.text.pretty`\n");
9009cc24 2957 fprintf(fp, " component\n");
8f4f40e7 2958 fprintf(fp, " `ctf`:\n");
e7ad156c
PP
2959 fprintf(fp, " Create an implicit `sink.ctf.fs`\n");
2960 fprintf(fp, " component\n");
9009cc24 2961 fprintf(fp, " `dummy`:\n");
fd5f8053 2962 fprintf(fp, " Create an implicit `sink.utils.dummy`\n");
9009cc24
PP
2963 fprintf(fp, " component\n");
2964 fprintf(fp, " `ctf-metadata`:\n");
fd5f8053
PP
2965 fprintf(fp, " Query the `source.ctf.fs` component class\n");
2966 fprintf(fp, " for metadata text and quit\n");
9009cc24 2967 fprintf(fp, "\n");
142ac9b0 2968 fprintf(fp, "See `babeltrace2 --help` for the list of general options.\n");
9009cc24
PP
2969 fprintf(fp, "\n\n");
2970 fprintf(fp, "Format of BEGIN and END\n");
2971 fprintf(fp, "-----------------------\n");
2972 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);
2976}
2977
2978static
2979struct 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 },
fd5f8053 2990 { "component", 'c', POPT_ARG_STRING, NULL, OPT_COMPONENT, NULL, NULL },
9009cc24
PP
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 },
9009cc24
PP
2997 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
2998 { "input-format", 'i', POPT_ARG_STRING, NULL, OPT_INPUT_FORMAT, NULL, NULL },
c4f81dc9 2999 { "log-level", 'l', POPT_ARG_STRING, NULL, OPT_LOG_LEVEL, NULL, NULL },
9009cc24
PP
3000 { "name", '\0', POPT_ARG_STRING, NULL, OPT_NAME, NULL, NULL },
3001 { "names", 'n', POPT_ARG_STRING, NULL, OPT_NAMES, NULL, NULL },
8e765000 3002 { "debug-info", '\0', POPT_ARG_NONE, NULL, OPT_DEBUG_INFO, NULL, NULL },
9009cc24
PP
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 },
9009cc24
PP
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 },
3019};
3020
3021static
3022GString *get_component_auto_name(const char *prefix,
8eee8ea2 3023 const bt_value *existing_names)
9009cc24
PP
3024{
3025 unsigned int i = 0;
3026 GString *auto_name = g_string_new(NULL);
3027
3028 if (!auto_name) {
3029 print_err_oom();
3030 goto end;
3031 }
3032
44514773 3033 if (!bt_value_map_has_entry(existing_names, prefix)) {
9009cc24
PP
3034 g_string_assign(auto_name, prefix);
3035 goto end;
3036 }
3037
3038 do {
3039 g_string_printf(auto_name, "%s-%d", prefix, i);
3040 i++;
44514773 3041 } while (bt_value_map_has_entry(existing_names, auto_name->str));
9009cc24
PP
3042
3043end:
3044 return auto_name;
3045}
3046
3047struct implicit_component_args {
3048 bool exists;
fd5f8053 3049 GString *comp_arg;
9009cc24
PP
3050 GString *name_arg;
3051 GString *params_arg;
8eee8ea2 3052 bt_value *extra_params;
9009cc24
PP
3053};
3054
3055static
3056int assign_name_to_implicit_component(struct implicit_component_args *args,
8eee8ea2 3057 const char *prefix, bt_value *existing_names,
9009cc24
PP
3058 GList **comp_names, bool append_to_comp_names)
3059{
3060 int ret = 0;
3061 GString *name = NULL;
3062
3063 if (!args->exists) {
3064 goto end;
3065 }
3066
17582c6d 3067 name = get_component_auto_name(prefix,
ce141536 3068 existing_names);
9009cc24
PP
3069
3070 if (!name) {
3071 ret = -1;
3072 goto end;
3073 }
3074
3075 g_string_assign(args->name_arg, name->str);
3076
ce141536 3077 if (bt_value_map_insert_entry(existing_names, name->str,
9009cc24
PP
3078 bt_value_null)) {
3079 print_err_oom();
3080 ret = -1;
3081 goto end;
3082 }
3083
3084 if (append_to_comp_names) {
3085 *comp_names = g_list_append(*comp_names, name);
3086 name = NULL;
3087 }
3088
3089end:
3090 if (name) {
3091 g_string_free(name, TRUE);
3092 }
3093
3094 return ret;
3095}
3096
3097static
3098int append_run_args_for_implicit_component(
9009cc24 3099 struct implicit_component_args *impl_args,
8eee8ea2 3100 bt_value *run_args)
9009cc24
PP
3101{
3102 int ret = 0;
3103 size_t i;
3104
3105 if (!impl_args->exists) {
3106 goto end;
3107 }
3108
ce141536 3109 if (bt_value_array_append_string_element(run_args, "--component")) {
fd5f8053
PP
3110 print_err_oom();
3111 goto error;
9009cc24
PP
3112 }
3113
ce141536 3114 if (bt_value_array_append_string_element(run_args, impl_args->comp_arg->str)) {
9009cc24
PP
3115 print_err_oom();
3116 goto error;
3117 }
3118
ce141536 3119 if (bt_value_array_append_string_element(run_args, "--name")) {
9009cc24
PP
3120 print_err_oom();
3121 goto error;
3122 }
3123
ce141536 3124 if (bt_value_array_append_string_element(run_args, impl_args->name_arg->str)) {
9009cc24
PP
3125 print_err_oom();
3126 goto error;
3127 }
3128
3129 if (impl_args->params_arg->len > 0) {
ce141536 3130 if (bt_value_array_append_string_element(run_args, "--params")) {
9009cc24
PP
3131 print_err_oom();
3132 goto error;
3133 }
3134
ce141536 3135 if (bt_value_array_append_string_element(run_args,
9009cc24
PP
3136 impl_args->params_arg->str)) {
3137 print_err_oom();
3138 goto error;
3139 }
3140 }
3141
ce141536 3142 for (i = 0; i < bt_value_array_get_size(impl_args->extra_params);
17582c6d 3143 i++) {
8eee8ea2 3144 const bt_value *elem;
9009cc24
PP
3145 const char *arg;
3146
ce141536
PP
3147 elem = bt_value_array_borrow_element_by_index(impl_args->extra_params,
3148 i);
9009cc24
PP
3149 if (!elem) {
3150 goto error;
3151 }
3152
8b45963b 3153 BT_ASSERT(bt_value_is_string(elem));
b5cdc106 3154 arg = bt_value_string_get(elem);
ce141536 3155 ret = bt_value_array_append_string_element(run_args, arg);
9009cc24
PP
3156 if (ret) {
3157 print_err_oom();
3158 goto error;
3159 }
3160 }
3161
3162 goto end;
3163
3164error:
3165 ret = -1;
3166
3167end:
3168 return ret;
3169}
3170
3171static
94023a1c 3172void finalize_implicit_component_args(struct implicit_component_args *args)
9009cc24 3173{
8b45963b 3174 BT_ASSERT(args);
9009cc24 3175
fd5f8053
PP
3176 if (args->comp_arg) {
3177 g_string_free(args->comp_arg, TRUE);
9009cc24
PP
3178 }
3179
3180 if (args->name_arg) {
3181 g_string_free(args->name_arg, TRUE);
3182 }
3183
3184 if (args->params_arg) {
3185 g_string_free(args->params_arg, TRUE);
3186 }
3187
8c6884d9 3188 bt_value_put_ref(args->extra_params);
9009cc24
PP
3189}
3190
3191static
3192int init_implicit_component_args(struct implicit_component_args *args,
fd5f8053 3193 const char *comp_arg, bool exists)
9009cc24
PP
3194{
3195 int ret = 0;
3196
3197 args->exists = exists;
fd5f8053 3198 args->comp_arg = g_string_new(comp_arg);
9009cc24
PP
3199 args->name_arg = g_string_new(NULL);
3200 args->params_arg = g_string_new(NULL);
ce141536 3201 args->extra_params = bt_value_array_create();
9009cc24 3202
fd5f8053 3203 if (!args->comp_arg || !args->name_arg ||
e7ad156c 3204 !args->params_arg || !args->extra_params) {
9009cc24 3205 ret = -1;
94023a1c 3206 finalize_implicit_component_args(args);
9009cc24
PP
3207 print_err_oom();
3208 goto end;
3209 }
3210
3211end:
3212 return ret;
3213}
3214
3215static
3216void append_implicit_component_param(struct implicit_component_args *args,
3217 const char *key, const char *value)
3218{
8b45963b
PP
3219 BT_ASSERT(args);
3220 BT_ASSERT(key);
3221 BT_ASSERT(value);
9009cc24
PP
3222 append_param_arg(args->params_arg, key, value);
3223}
3224
b2f834c9 3225/* Escape value to make it suitable to use as a string parameter value. */
9009cc24 3226static
b2f834c9 3227gchar *escape_string_value(const char *value)
9009cc24 3228{
b2f834c9
SM
3229 GString *ret;
3230 const char *in;
3231
3232 ret = g_string_new(NULL);
3233 if (!ret) {
3234 print_err_oom();
3235 goto end;
3236 }
3237
3238 in = value;
3239 while (*in) {
3240 switch (*in) {
3241 case '"':
3242 case '\\':
3243 g_string_append_c(ret, '\\');
3244 break;
3245 }
3246
3247 g_string_append_c(ret, *in);
3248
3249 in++;
3250 }
3251
3252end:
3253 return g_string_free(ret, FALSE);
3254}
9009cc24 3255
242407a9 3256static
bf5b7748 3257int bt_value_to_cli_param_value_append(const bt_value *value, GString *buf)
242407a9 3258{
bf5b7748 3259 BT_ASSERT(buf);
242407a9 3260
bf5b7748 3261 int ret = -1;
242407a9
SM
3262
3263 switch (bt_value_get_type(value)) {
3264 case BT_VALUE_TYPE_STRING:
3265 {
3266 const char *str_value = bt_value_string_get(value);
3267 gchar *escaped_str_value;
3268
3269 escaped_str_value = escape_string_value(str_value);
3270 if (!escaped_str_value) {
bf5b7748 3271 goto end;
242407a9
SM
3272 }
3273
bf5b7748 3274 g_string_append_printf(buf, "\"%s\"", escaped_str_value);
242407a9
SM
3275
3276 g_free(escaped_str_value);
3277 break;
3278 }
bf5b7748
SM
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++) {
58f62bf9
PP
3283 const bt_value *item;
3284 int ret;
3285
bf5b7748 3286 if (i > 0) {
58f62bf9 3287 g_string_append(buf, ", ");
bf5b7748 3288 }
58f62bf9
PP
3289
3290 item = bt_value_array_borrow_element_by_index_const(
3291 value, i);
3292 ret = bt_value_to_cli_param_value_append(item, buf);
3293
bf5b7748
SM
3294 if (ret) {
3295 goto end;
3296 }
3297 }
3298 g_string_append_c(buf, ']');
3299 break;
3300 }
242407a9
SM
3301 default:
3302 abort();
3303 }
3304
bf5b7748
SM
3305 ret = 0;
3306
3307end:
3308 return ret;
3309}
3310
3311/*
3312 * Convert `value` to its equivalent representation as a command line parameter
3313 * value.
3314 */
3315
3316static
3317gchar *bt_value_to_cli_param_value(bt_value *value)
3318{
3319 GString *buf;
3320 gchar *result = NULL;
3321
3322 buf = g_string_new(NULL);
3323 if (!buf) {
3324 print_err_oom();
3325 goto error;
3326 }
3327
3328 if (bt_value_to_cli_param_value_append(value, buf)) {
3329 goto error;
3330 }
3331
242407a9
SM
3332 result = g_string_free(buf, FALSE);
3333 buf = NULL;
3334
3335 goto end;
3336
3337error:
3338 if (buf) {
3339 g_string_free(buf, TRUE);
3340 }
3341
3342end:
3343 return result;
3344}
3345
b2f834c9 3346static
242407a9 3347int append_parameter_to_args(bt_value *args, const char *key, bt_value *value)
b2f834c9 3348{
8b45963b 3349 BT_ASSERT(args);
b2f834c9 3350 BT_ASSERT(bt_value_get_type(args) == BT_VALUE_TYPE_ARRAY);
8b45963b
PP
3351 BT_ASSERT(key);
3352 BT_ASSERT(value);
9009cc24 3353
b2f834c9 3354 int ret = 0;
242407a9 3355 gchar *str_value = NULL;
b2f834c9
SM
3356 GString *parameter = NULL;
3357
3358 if (bt_value_array_append_string_element(args, "--params")) {
9009cc24
PP
3359 print_err_oom();
3360 ret = -1;
3361 goto end;
3362 }
3363
242407a9
SM
3364 str_value = bt_value_to_cli_param_value(value);
3365 if (!str_value) {
9009cc24
PP
3366 ret = -1;
3367 goto end;
3368 }
3369
b2f834c9
SM
3370 parameter = g_string_new(NULL);
3371 if (!parameter) {
9009cc24
PP
3372 print_err_oom();
3373 ret = -1;
3374 goto end;
3375 }
3376
242407a9 3377 g_string_printf(parameter, "%s=%s", key, str_value);
b2f834c9
SM
3378
3379 if (bt_value_array_append_string_element(args, parameter->str)) {
9009cc24
PP
3380 print_err_oom();
3381 ret = -1;
3382 goto end;
3383 }
3384
3385end:
b2f834c9
SM
3386 if (parameter) {
3387 g_string_free(parameter, TRUE);
3388 parameter = NULL;
3389 }
3390
242407a9
SM
3391 if (str_value) {
3392 g_free(str_value);
3393 str_value = NULL;
3394 }
3395
3396 return ret;
3397}
3398
3399static
3400int append_string_parameter_to_args(bt_value *args, const char *key, const char *value)
3401{
3402 bt_value *str_value;
3403 int ret;
3404
3405 str_value = bt_value_string_create_init(value);
3406
3407 if (!str_value) {
3408 print_err_oom();
3409 ret = -1;
3410 goto end;
3411 }
3412
3413 ret = append_parameter_to_args(args, key, str_value);
3414
3415end:
3416 BT_VALUE_PUT_REF_AND_RESET(str_value);
9009cc24
PP
3417 return ret;
3418}
3419
b2f834c9
SM
3420static
3421int append_implicit_component_extra_param(struct implicit_component_args *args,
3422 const char *key, const char *value)
3423{
242407a9 3424 return append_string_parameter_to_args(args->extra_params, key, value);
b2f834c9
SM
3425}
3426
9009cc24
PP
3427static
3428int convert_append_name_param(enum bt_config_component_dest dest,
3429 GString *cur_name, GString *cur_name_prefix,
8eee8ea2
PP
3430 bt_value *run_args,
3431 bt_value *all_names,
9009cc24
PP
3432 GList **source_names, GList **filter_names,
3433 GList **sink_names)
3434{
3435 int ret = 0;
3436
3437 if (cur_name_prefix->len > 0) {
fd5f8053 3438 /* We're after a --component option */
9009cc24
PP
3439 GString *name = NULL;
3440 bool append_name_opt = false;
3441
3442 if (cur_name->len == 0) {
3443 /*
3444 * No explicit name was provided for the user
3445 * component.
3446 */
fd5f8053 3447 name = get_component_auto_name(cur_name_prefix->str,
ce141536 3448 all_names);
9009cc24
PP
3449 append_name_opt = true;
3450 } else {
3451 /*
3452 * An explicit name was provided for the user
3453 * component.
3454 */
ce141536
PP
3455 if (bt_value_map_has_entry(all_names,
3456 cur_name->str)) {
9009cc24
PP
3457 printf_err("Duplicate component instance name:\n %s\n",
3458 cur_name->str);
3459 goto error;
3460 }
3461
3462 name = g_string_new(cur_name->str);
3463 }
3464
3465 if (!name) {
3466 print_err_oom();
3467 goto error;
3468 }
3469
3470 /*
3471 * Remember this name globally, for the uniqueness of
3472 * all component names.
3473 */
ce141536 3474 if (bt_value_map_insert_entry(all_names, name->str, bt_value_null)) {
9009cc24
PP
3475 print_err_oom();
3476 goto error;
3477 }
3478
3479 /*
3480 * Append the --name option if necessary.
3481 */
3482 if (append_name_opt) {
ce141536 3483 if (bt_value_array_append_string_element(run_args, "--name")) {
9009cc24
PP
3484 print_err_oom();
3485 goto error;
3486 }
3487
ce141536 3488 if (bt_value_array_append_string_element(run_args, name->str)) {
9009cc24
PP
3489 print_err_oom();
3490 goto error;
3491 }
3492 }
3493
3494 /*
3495 * Remember this name specifically for the type of the
3496 * component. This is to create connection arguments.
3497 */
3498 switch (dest) {
3499 case BT_CONFIG_COMPONENT_DEST_SOURCE:
3500 *source_names = g_list_append(*source_names, name);
3501 break;
3502 case BT_CONFIG_COMPONENT_DEST_FILTER:
3503 *filter_names = g_list_append(*filter_names, name);
3504 break;
3505 case BT_CONFIG_COMPONENT_DEST_SINK:
3506 *sink_names = g_list_append(*sink_names, name);
3507 break;
3508 default:
0fbb9a9f 3509 abort();
9009cc24
PP
3510 }
3511
3512 g_string_assign(cur_name_prefix, "");
3513 }
3514
3515 goto end;
3516
3517error:
3518 ret = -1;
3519
3520end:
3521 return ret;
3522}
3523
3524/*
3525 * Escapes `.`, `:`, and `\` of `input` with `\`.
3526 */
3527static
3528GString *escape_dot_colon(const char *input)
3529{
3530 GString *output = g_string_new(NULL);
3531 const char *ch;
3532
3533 if (!output) {
3534 print_err_oom();
3535 goto end;
3536 }
3537
3538 for (ch = input; *ch != '\0'; ch++) {
3539 if (*ch == '\\' || *ch == '.' || *ch == ':') {
3540 g_string_append_c(output, '\\');
3541 }
3542
3543 g_string_append_c(output, *ch);
3544 }
3545
3546end:
3547 return output;
3548}
3549
3550/*
3551 * Appends a --connect option to a list of arguments. `upstream_name`
3552 * and `downstream_name` are escaped with escape_dot_colon() in this
3553 * function.
3554 */
3555static
8eee8ea2 3556int append_connect_arg(bt_value *run_args,
9009cc24
PP
3557 const char *upstream_name, const char *downstream_name)
3558{
3559 int ret = 0;
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);
3563
3564 if (!e_upstream_name || !e_downstream_name || !arg) {
3565 print_err_oom();
3566 ret = -1;
3567 goto end;
3568 }
3569
ce141536 3570 ret = bt_value_array_append_string_element(run_args, "--connect");
9009cc24
PP
3571 if (ret) {
3572 print_err_oom();
3573 ret = -1;
3574 goto end;
3575 }
3576
3577 g_string_append(arg, e_upstream_name->str);
3578 g_string_append_c(arg, ':');
3579 g_string_append(arg, e_downstream_name->str);
ce141536 3580 ret = bt_value_array_append_string_element(run_args, arg->str);
9009cc24
PP
3581 if (ret) {
3582 print_err_oom();
3583 ret = -1;
3584 goto end;
3585 }
3586
3587end:
3588 if (arg) {
3589 g_string_free(arg, TRUE);
3590 }
3591
3592 if (e_upstream_name) {
3593 g_string_free(e_upstream_name, TRUE);
3594 }
3595
3596 if (e_downstream_name) {
3597 g_string_free(e_downstream_name, TRUE);
3598 }
3599
3600 return ret;
3601}
3602
3603/*
3604 * Appends the run command's --connect options for the convert command.
3605 */
3606static
8eee8ea2 3607int convert_auto_connect(bt_value *run_args,
9009cc24
PP
3608 GList *source_names, GList *filter_names,
3609 GList *sink_names)
3610{
3611 int ret = 0;
3612 GList *source_at = source_names;
3613 GList *filter_at = filter_names;
3614 GList *filter_prev;
3615 GList *sink_at = sink_names;
3616
8b45963b
PP
3617 BT_ASSERT(source_names);
3618 BT_ASSERT(filter_names);
3619 BT_ASSERT(sink_names);
9009cc24
PP
3620
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;
3625
3626 ret = append_connect_arg(run_args, source_name->str,
3627 filter_name->str);
3628 if (ret) {
3629 goto error;
3630 }
3631 }
3632
3633 filter_prev = filter_at;
3634 filter_at = g_list_next(filter_at);
3635
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;
3640
3641 ret = append_connect_arg(run_args, filter_prev_name->str,
3642 filter_name->str);
3643 if (ret) {
3644 goto error;
3645 }
3646 }
3647
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;
3652
3653 ret = append_connect_arg(run_args, filter_name->str,
3654 sink_name->str);
3655 if (ret) {
3656 goto error;
3657 }
3658 }
3659
3660 goto end;
3661
3662error:
3663 ret = -1;
3664
3665end:
3666 return ret;
3667}
3668
3669static
3670int split_timerange(const char *arg, char **begin, char **end)
3671{
3672 int ret = 0;
3673 const char *ch = arg;
3674 size_t end_pos;
3675 GString *g_begin = NULL;
3676 GString *g_end = NULL;
3677
8b45963b 3678 BT_ASSERT(arg);
9009cc24
PP
3679
3680 if (*ch == '[') {
3681 ch++;
3682 }
3683
3684 g_begin = bt_common_string_until(ch, "", ",", &end_pos);
3685 if (!g_begin || ch[end_pos] != ',' || g_begin->len == 0) {
3686 goto error;
3687 }
3688
3689 ch += end_pos + 1;
3690
3691 g_end = bt_common_string_until(ch, "", "]", &end_pos);
3692 if (!g_end || g_end->len == 0) {
3693 goto error;
3694 }
3695
8b45963b
PP
3696 BT_ASSERT(begin);
3697 BT_ASSERT(end);
9009cc24
PP
3698 *begin = g_begin->str;
3699 *end = g_end->str;
3700 g_string_free(g_begin, FALSE);
3701 g_string_free(g_end, FALSE);
3702 g_begin = NULL;
3703 g_end = NULL;
3704 goto end;
3705
3706error:
3707 ret = -1;
3708
3709end:
3710 if (g_begin) {
3711 g_string_free(g_begin, TRUE);
3712 }
3713
3714 if (g_end) {
3715 g_string_free(g_end, TRUE);
3716 }
3717
3718 return ret;
3719}
3720
3721static
3722int g_list_prepend_gstring(GList **list, const char *string)
3723{
3724 int ret = 0;
3725 GString *gs = g_string_new(string);
3726
8b45963b 3727 BT_ASSERT(list);
9009cc24
PP
3728
3729 if (!gs) {
3730 print_err_oom();
3731 goto end;
3732 }
3733
3734 *list = g_list_prepend(*list, gs);
3735
3736end:
3737 return ret;
3738}
3739
3740/*
3741 * Creates a Babeltrace config object from the arguments of a convert
3742 * command.
3743 *
3744 * *retcode is set to the appropriate exit code to use.
3745 */
3746static
3747struct bt_config *bt_config_convert_from_args(int argc, const char *argv[],
3748 int *retcode, bool force_omit_system_plugin_path,
8e765000 3749 bool force_omit_home_plugin_path,
c4f81dc9 3750 const bt_value *initial_plugin_paths, int *default_log_level)
9009cc24
PP
3751{
3752 poptContext pc = NULL;
3753 char *arg = NULL;
0a011c88
JG
3754 enum bt_config_component_dest cur_comp_dest =
3755 BT_CONFIG_COMPONENT_DEST_UNKNOWN;
9009cc24
PP
3756 int opt, ret = 0;
3757 struct bt_config *cfg = NULL;
9009cc24
PP
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;
b5fe3e00 3762 bool stream_intersection_mode = false;
9009cc24
PP
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;
8eee8ea2
PP
3769 bt_value *run_args = NULL;
3770 bt_value *all_names = NULL;
9009cc24
PP
3771 GList *source_names = NULL;
3772 GList *filter_names = NULL;
3773 GList *sink_names = NULL;
bf5b7748
SM
3774 bt_value *leftovers = NULL;
3775 struct implicit_component_args implicit_ctf_input_args = { 0 };
e7ad156c 3776 struct implicit_component_args implicit_ctf_output_args = { 0 };
9009cc24
PP
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 };
8eee8ea2 3783 bt_value *plugin_paths;
9009cc24
PP
3784 char error_buf[256] = { 0 };
3785 size_t i;
3786 struct bt_common_lttng_live_url_parts lttng_live_url_parts = { 0 };
e7ad156c 3787 char *output = NULL;
9009cc24 3788
6284461f 3789 (void) bt_value_copy(initial_plugin_paths, &plugin_paths);
4b70020d 3790
9009cc24
PP
3791 *retcode = 0;
3792
3793 if (argc <= 1) {
3794 print_convert_usage(stdout);
3795 *retcode = -1;
3796 goto end;
3797 }
3798
bf5b7748 3799 if (init_implicit_component_args(&implicit_ctf_input_args,
fd5f8053 3800 "source.ctf.fs", false)) {
9009cc24
PP
3801 goto error;
3802 }
3803
e7ad156c
PP
3804 if (init_implicit_component_args(&implicit_ctf_output_args,
3805 "sink.ctf.fs", false)) {
3806 goto error;
3807 }
3808
9009cc24 3809 if (init_implicit_component_args(&implicit_lttng_live_args,
fd5f8053 3810 "source.ctf.lttng-live", false)) {
9009cc24
PP
3811 goto error;
3812 }
3813
fd5f8053
PP
3814 if (init_implicit_component_args(&implicit_text_args,
3815 "sink.text.pretty", false)) {
9009cc24
PP
3816 goto error;
3817 }
3818
fd5f8053
PP
3819 if (init_implicit_component_args(&implicit_dummy_args,
3820 "sink.utils.dummy", false)) {
9009cc24
PP
3821 goto error;
3822 }
3823
3824 if (init_implicit_component_args(&implicit_debug_info_args,
8e765000 3825 "filter.lttng-utils.debug-info", false)) {
9009cc24
PP
3826 goto error;
3827 }
3828
fd5f8053
PP
3829 if (init_implicit_component_args(&implicit_muxer_args,
3830 "filter.utils.muxer", true)) {
9009cc24
PP
3831 goto error;
3832 }
3833
3834 if (init_implicit_component_args(&implicit_trimmer_args,
fd5f8053 3835 "filter.utils.trimmer", false)) {
9009cc24
PP
3836 goto error;
3837 }
3838
ce141536 3839 all_names = bt_value_map_create();
9009cc24
PP
3840 if (!all_names) {
3841 print_err_oom();
3842 goto error;
3843 }
3844
ce141536 3845 run_args = bt_value_array_create();
9009cc24
PP
3846 if (!run_args) {
3847 print_err_oom();
3848 goto error;
3849 }
3850
3851 cur_name = g_string_new(NULL);
3852 if (!cur_name) {
3853 print_err_oom();
3854 goto error;
3855 }
3856
3857 cur_name_prefix = g_string_new(NULL);
3858 if (!cur_name_prefix) {
3859 print_err_oom();
3860 goto error;
3861 }
3862
3863 ret = append_env_var_plugin_paths(plugin_paths);
3864 if (ret) {
3865 goto error;
3866 }
3867
bf5b7748
SM
3868 leftovers = bt_value_array_create();
3869 if (!leftovers) {
3870 print_err_oom();
3871 goto error;
3872 }
3873
9009cc24
PP
3874 /*
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:
3879 *
b2f834c9
SM
3880 * --path=PATH -> --params=path="PATH"
3881 * --url=URL -> --params=url="URL"
9009cc24
PP
3882 *
3883 * Also it appends the plugin paths of --plugin-path to
3884 * `plugin_paths`.
3885 */
3886 pc = poptGetContext(NULL, argc, (const char **) argv,
3887 convert_long_options, 0);
3888 if (!pc) {
3889 printf_err("Cannot get popt context\n");
3890 goto error;
3891 }
3892
3893 poptReadDefaultConfig(pc, 0);
3894
3895 while ((opt = poptGetNextOpt(pc)) > 0) {
3896 char *name = NULL;
3897 char *plugin_name = NULL;
3898 char *comp_cls_name = NULL;
3899
3900 arg = poptGetOptArg(pc);
3901
3902 switch (opt) {
fd5f8053
PP
3903 case OPT_COMPONENT:
3904 {
ee78f405 3905 bt_component_class_type type;
fd5f8053
PP
3906 const char *type_prefix;
3907
9009cc24
PP
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);
3912 if (ret) {
3913 goto error;
3914 }
3915
3916 /* Parse the argument */
3917 plugin_comp_cls_names(arg, &name, &plugin_name,
fd5f8053 3918 &comp_cls_name, &type);
9009cc24 3919 if (!plugin_name || !comp_cls_name) {
fd5f8053 3920 printf_err("Invalid format for --component option's argument:\n %s\n",
9009cc24
PP
3921 arg);
3922 goto error;
3923 }
3924
3925 if (name) {
3926 g_string_assign(cur_name, name);
3927 } else {
3928 g_string_assign(cur_name, "");
3929 }
3930
fd5f8053
PP
3931 switch (type) {
3932 case BT_COMPONENT_CLASS_TYPE_SOURCE:
3933 cur_comp_dest = BT_CONFIG_COMPONENT_DEST_SOURCE;
3934 type_prefix = "source";
9009cc24 3935 break;
fd5f8053
PP
3936 case BT_COMPONENT_CLASS_TYPE_FILTER:
3937 cur_comp_dest = BT_CONFIG_COMPONENT_DEST_FILTER;
3938 type_prefix = "filter";
9009cc24 3939 break;
fd5f8053
PP
3940 case BT_COMPONENT_CLASS_TYPE_SINK:
3941 cur_comp_dest = BT_CONFIG_COMPONENT_DEST_SINK;
3942 type_prefix = "sink";
9009cc24
PP
3943 break;
3944 default:
0fbb9a9f 3945 abort();
9009cc24
PP
3946 }
3947
ce141536 3948 if (bt_value_array_append_string_element(run_args,
fd5f8053
PP
3949 "--component")) {
3950 print_err_oom();
3951 goto error;
3952 }
3953
ce141536 3954 if (bt_value_array_append_string_element(run_args, arg)) {
9009cc24
PP
3955 print_err_oom();
3956 goto error;
3957 }
3958
3959 g_string_assign(cur_name_prefix, "");
fd5f8053
PP
3960 g_string_append_printf(cur_name_prefix, "%s.%s.%s",
3961 type_prefix, plugin_name, comp_cls_name);
9009cc24
PP
3962 free(name);
3963 free(plugin_name);
3964 free(comp_cls_name);
3965 name = NULL;
3966 plugin_name = NULL;
3967 comp_cls_name = NULL;
3968 break;
fd5f8053 3969 }
9009cc24
PP
3970 case OPT_PARAMS:
3971 if (cur_name_prefix->len == 0) {
3972 printf_err("No current component of which to set parameters:\n %s\n",
3973 arg);
3974 goto error;
3975 }
3976
ce141536 3977 if (bt_value_array_append_string_element(run_args,
9009cc24
PP
3978 "--params")) {
3979 print_err_oom();
3980 goto error;
3981 }
3982
ce141536 3983 if (bt_value_array_append_string_element(run_args, arg)) {
9009cc24
PP
3984 print_err_oom();
3985 goto error;
3986 }
3987 break;
3988 case OPT_PATH:
3989 if (cur_name_prefix->len == 0) {
3990 printf_err("No current component of which to set `path` parameter:\n %s\n",
3991 arg);
3992 goto error;
3993 }
3994
242407a9 3995 if (append_string_parameter_to_args(run_args, "path", arg)) {
9009cc24
PP
3996 goto error;
3997 }
3998 break;
3999 case OPT_URL:
4000 if (cur_name_prefix->len == 0) {
4001 printf_err("No current component of which to set `url` parameter:\n %s\n",
4002 arg);
4003 goto error;
4004 }
4005
9009cc24 4006
242407a9 4007 if (append_string_parameter_to_args(run_args, "url", arg)) {
9009cc24
PP
4008 goto error;
4009 }
4010 break;
4011 case OPT_NAME:
4012 if (cur_name_prefix->len == 0) {
4013 printf_err("No current component to name:\n %s\n",
4014 arg);
4015 goto error;
4016 }
4017
ce141536 4018 if (bt_value_array_append_string_element(run_args, "--name")) {
9009cc24
PP
4019 print_err_oom();
4020 goto error;
4021 }
4022
ce141536 4023 if (bt_value_array_append_string_element(run_args, arg)) {
9009cc24
PP
4024 print_err_oom();
4025 goto error;
4026 }
4027
4028 g_string_assign(cur_name, arg);
c4f81dc9
PP
4029 break;
4030 case OPT_LOG_LEVEL:
4031 if (cur_name_prefix->len == 0) {
4032 printf_err("No current component to assign a log level to:\n %s\n",
4033 arg);
4034 goto error;
4035 }
4036
4037 if (bt_value_array_append_string_element(run_args, "--log-level")) {
4038 print_err_oom();
4039 goto error;
4040 }
4041
4042 if (bt_value_array_append_string_element(run_args, arg)) {
4043 print_err_oom();
4044 goto error;
4045 }
4046
9009cc24
PP
4047 break;
4048 case OPT_OMIT_HOME_PLUGIN_PATH:
4049 force_omit_home_plugin_path = true;
4050
ce141536 4051 if (bt_value_array_append_string_element(run_args,
9009cc24
PP
4052 "--omit-home-plugin-path")) {
4053 print_err_oom();
4054 goto error;
4055 }
4056 break;
4057 case OPT_RETRY_DURATION:
ce141536 4058 if (bt_value_array_append_string_element(run_args,
9009cc24
PP
4059 "--retry-duration")) {
4060 print_err_oom();
4061 goto error;
4062 }
4063
ce141536 4064 if (bt_value_array_append_string_element(run_args, arg)) {
9009cc24
PP
4065 print_err_oom();
4066 goto error;
4067 }
4068 break;
4069 case OPT_OMIT_SYSTEM_PLUGIN_PATH:
4070 force_omit_system_plugin_path = true;
4071
ce141536 4072 if (bt_value_array_append_string_element(run_args,
9009cc24
PP
4073 "--omit-system-plugin-path")) {
4074 print_err_oom();
4075 goto error;
4076 }
4077 break;
4078 case OPT_PLUGIN_PATH:
4079 if (bt_config_append_plugin_paths_check_setuid_setgid(
4080 plugin_paths, arg)) {
4081 goto error;
4082 }
4083
ce141536 4084 if (bt_value_array_append_string_element(run_args,
9009cc24
PP
4085 "--plugin-path")) {
4086 print_err_oom();
4087 goto error;
4088 }
4089
ce141536 4090 if (bt_value_array_append_string_element(run_args, arg)) {
9009cc24
PP
4091 print_err_oom();
4092 goto error;
4093 }
4094 break;
4095 case OPT_HELP:
4096 print_convert_usage(stdout);
4097 *retcode = -1;
8138bfe1 4098 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
4099 goto end;
4100 case OPT_BEGIN:
4101 case OPT_CLOCK_CYCLES:
4102 case OPT_CLOCK_DATE:
4103 case OPT_CLOCK_FORCE_CORRELATE:
4104 case OPT_CLOCK_GMT:
4105 case OPT_CLOCK_OFFSET:
4106 case OPT_CLOCK_OFFSET_NS:
4107 case OPT_CLOCK_SECONDS:
4108 case OPT_COLOR:
4109 case OPT_DEBUG:
8e765000 4110 case OPT_DEBUG_INFO:
9009cc24
PP
4111 case OPT_DEBUG_INFO_DIR:
4112 case OPT_DEBUG_INFO_FULL_PATH:
4113 case OPT_DEBUG_INFO_TARGET_PREFIX:
4114 case OPT_END:
4115 case OPT_FIELDS:
4116 case OPT_INPUT_FORMAT:
4117 case OPT_NAMES:
9009cc24
PP
4118 case OPT_NO_DELTA:
4119 case OPT_OUTPUT_FORMAT:
4120 case OPT_OUTPUT:
4121 case OPT_RUN_ARGS:
4122 case OPT_RUN_ARGS_0:
4123 case OPT_STREAM_INTERSECTION:
4124 case OPT_TIMERANGE:
4125 case OPT_VERBOSE:
4126 /* Ignore in this pass */
4127 break;
4128 default:
4129 printf_err("Unknown command-line option specified (option code %d)\n",
4130 opt);
4131 goto error;
4132 }
4133
4134 free(arg);
4135 arg = NULL;
4136 }
4137
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);
4142 if (ret) {
4143 goto error;
4144 }
4145
4146 /* Check for option parsing error */
4147 if (opt < -1) {
4148 printf_err("While parsing command-line options, at option %s: %s\n",
4149 poptBadOption(pc, 0), poptStrerror(opt));
4150 goto error;
4151 }
4152
4153 poptFreeContext(pc);
4154 free(arg);
4155 arg = NULL;
4156
4157 /*
4158 * Second pass: transform the convert-specific options and
4159 * arguments into implicit component instances for the run
4160 * command.
4161 */
4162 pc = poptGetContext(NULL, argc, (const char **) argv,
4163 convert_long_options, 0);
4164 if (!pc) {
4165 printf_err("Cannot get popt context\n");
4166 goto error;
4167 }
4168
4169 poptReadDefaultConfig(pc, 0);
4170
4171 while ((opt = poptGetNextOpt(pc)) > 0) {
4172 arg = poptGetOptArg(pc);
4173
4174 switch (opt) {
4175 case OPT_BEGIN:
4176 if (trimmer_has_begin) {
4177 printf("At --begin option: --begin or --timerange option already specified\n %s\n",
4178 arg);
4179 goto error;
4180 }
4181
4182 trimmer_has_begin = true;
e7ad156c 4183 ret = append_implicit_component_extra_param(
9009cc24
PP
4184 &implicit_trimmer_args, "begin", arg);
4185 implicit_trimmer_args.exists = true;
4186 if (ret) {
4187 goto error;
4188 }
4189 break;
4190 case OPT_END:
4191 if (trimmer_has_end) {
4192 printf("At --end option: --end or --timerange option already specified\n %s\n",
4193 arg);
4194 goto error;
4195 }
4196
4197 trimmer_has_end = true;
e7ad156c 4198 ret = append_implicit_component_extra_param(
9009cc24
PP
4199 &implicit_trimmer_args, "end", arg);
4200 implicit_trimmer_args.exists = true;
4201 if (ret) {
4202 goto error;
4203 }
4204 break;
4205 case OPT_TIMERANGE:
4206 {
4207 char *begin;
4208 char *end;
4209
4210 if (trimmer_has_begin || trimmer_has_end) {
4211 printf("At --timerange option: --begin, --end, or --timerange option already specified\n %s\n",
4212 arg);
4213 goto error;
4214 }
4215
4216 ret = split_timerange(arg, &begin, &end);
4217 if (ret) {
4218 printf_err("Invalid --timerange option's argument: expecting BEGIN,END or [BEGIN,END]:\n %s\n",
4219 arg);
4220 goto error;
4221 }
4222
e7ad156c 4223 ret = append_implicit_component_extra_param(
9009cc24 4224 &implicit_trimmer_args, "begin", begin);
e7ad156c 4225 ret |= append_implicit_component_extra_param(
9009cc24
PP
4226 &implicit_trimmer_args, "end", end);
4227 implicit_trimmer_args.exists = true;
4228 free(begin);
4229 free(end);
4230 if (ret) {
4231 goto error;
4232 }
4233 break;
4234 }
4235 case OPT_CLOCK_CYCLES:
4236 append_implicit_component_param(
4237 &implicit_text_args, "clock-cycles", "yes");
4238 implicit_text_args.exists = true;
4239 break;
4240 case OPT_CLOCK_DATE:
4241 append_implicit_component_param(
4242 &implicit_text_args, "clock-date", "yes");
4243 implicit_text_args.exists = true;
4244 break;
4245 case OPT_CLOCK_FORCE_CORRELATE:
4246 append_implicit_component_param(
53ac3428
PP
4247 &implicit_muxer_args,
4248 "assume-absolute-clock-classes", "yes");
9009cc24
PP
4249 break;
4250 case OPT_CLOCK_GMT:
4251 append_implicit_component_param(
4252 &implicit_text_args, "clock-gmt", "yes");
af47368e 4253 append_implicit_component_param(
480c0b45 4254 &implicit_trimmer_args, "gmt", "yes");
9009cc24
PP
4255 implicit_text_args.exists = true;
4256 break;
4257 case OPT_CLOCK_OFFSET:
bf5b7748 4258 implicit_ctf_input_args.exists = true;
53ac3428 4259 append_implicit_component_param(
bf5b7748 4260 &implicit_ctf_input_args,
53ac3428 4261 "clock-class-offset-s", arg);
9009cc24
PP
4262 break;
4263 case OPT_CLOCK_OFFSET_NS:
bf5b7748 4264 implicit_ctf_input_args.exists = true;
7be7e609 4265 append_implicit_component_param(
bf5b7748 4266 &implicit_ctf_input_args,
7be7e609 4267 "clock-class-offset-ns", arg);
9009cc24
PP
4268 break;
4269 case OPT_CLOCK_SECONDS:
4270 append_implicit_component_param(
4271 &implicit_text_args, "clock-seconds", "yes");
4272 implicit_text_args.exists = true;
4273 break;
4274 case OPT_COLOR:
9009cc24 4275 implicit_text_args.exists = true;
e7ad156c
PP
4276 ret = append_implicit_component_extra_param(
4277 &implicit_text_args, "color", arg);
9009cc24
PP
4278 if (ret) {
4279 goto error;
4280 }
4281 break;
8e765000
PP
4282 case OPT_DEBUG_INFO:
4283 implicit_debug_info_args.exists = true;
9009cc24
PP
4284 break;
4285 case OPT_DEBUG_INFO_DIR:
e7ad156c
PP
4286 implicit_debug_info_args.exists = true;
4287 ret = append_implicit_component_extra_param(
395a08d0 4288 &implicit_debug_info_args, "debug-info-dir", arg);
9009cc24
PP
4289 if (ret) {
4290 goto error;
4291 }
4292 break;
4293 case OPT_DEBUG_INFO_FULL_PATH:
e7ad156c 4294 implicit_debug_info_args.exists = true;
9009cc24
PP
4295 append_implicit_component_param(
4296 &implicit_debug_info_args, "full-path", "yes");
4297 break;
4298 case OPT_DEBUG_INFO_TARGET_PREFIX:
e7ad156c
PP
4299 implicit_debug_info_args.exists = true;
4300 ret = append_implicit_component_extra_param(
9009cc24
PP
4301 &implicit_debug_info_args,
4302 "target-prefix", arg);
4303 if (ret) {
4304 goto error;
4305 }
4306 break;
4307 case OPT_FIELDS:
4308 {
8eee8ea2 4309 bt_value *fields = fields_from_arg(arg);
9009cc24
PP
4310
4311 if (!fields) {
4312 goto error;
4313 }
4314
e7ad156c 4315 implicit_text_args.exists = true;
9009cc24
PP
4316 ret = insert_flat_params_from_array(
4317 implicit_text_args.params_arg,
ce141536 4318 fields, "field");
8c6884d9 4319 bt_value_put_ref(fields);
9009cc24
PP
4320 if (ret) {
4321 goto error;
4322 }
4323 break;
4324 }
4325 case OPT_NAMES:
4326 {
8eee8ea2 4327 bt_value *names = names_from_arg(arg);
9009cc24
PP
4328
4329 if (!names) {
4330 goto error;
4331 }
4332
e7ad156c 4333 implicit_text_args.exists = true;
9009cc24
PP
4334 ret = insert_flat_params_from_array(
4335 implicit_text_args.params_arg,
ce141536 4336 names, "name");
8c6884d9 4337 bt_value_put_ref(names);
9009cc24
PP
4338 if (ret) {
4339 goto error;
4340 }
4341 break;
4342 }
4343 case OPT_NO_DELTA:
4344 append_implicit_component_param(
4345 &implicit_text_args, "no-delta", "yes");
4346 implicit_text_args.exists = true;
4347 break;
4348 case OPT_INPUT_FORMAT:
4349 if (got_input_format_opt) {
4350 printf_err("Duplicate --input-format option\n");
4351 goto error;
4352 }
4353
4354 got_input_format_opt = true;
4355
4356 if (strcmp(arg, "ctf") == 0) {
bf5b7748 4357 implicit_ctf_input_args.exists = true;
9009cc24
PP
4358 } else if (strcmp(arg, "lttng-live") == 0) {
4359 implicit_lttng_live_args.exists = true;
4360 } else {
4361 printf_err("Unknown legacy input format:\n %s\n",
4362 arg);
4363 goto error;
4364 }
4365 break;
4366 case OPT_OUTPUT_FORMAT:
4367 if (got_output_format_opt) {
4368 printf_err("Duplicate --output-format option\n");
4369 goto error;
4370 }
4371
4372 got_output_format_opt = true;
4373
4374 if (strcmp(arg, "text") == 0) {
4375 implicit_text_args.exists = true;
e7ad156c
PP
4376 } else if (strcmp(arg, "ctf") == 0) {
4377 implicit_ctf_output_args.exists = true;
9009cc24
PP
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;
4382 } else {
4383 printf_err("Unknown legacy output format:\n %s\n",
4384 arg);
4385 goto error;
4386 }
4387 break;
4388 case OPT_OUTPUT:
e7ad156c
PP
4389 if (output) {
4390 printf_err("Duplicate --output option\n");
4391 goto error;
4392 }
4393
4394 output = strdup(arg);
4395 if (!output) {
4396 print_err_oom();
9009cc24
PP
4397 goto error;
4398 }
4399 break;
4400 case OPT_RUN_ARGS:
4401 if (print_run_args_0) {
4402 printf_err("Cannot specify --run-args and --run-args-0\n");
4403 goto error;
4404 }
4405
4406 print_run_args = true;
4407 break;
4408 case OPT_RUN_ARGS_0:
4409 if (print_run_args) {
4410 printf_err("Cannot specify --run-args and --run-args-0\n");
4411 goto error;
4412 }
4413
4414 print_run_args_0 = true;
4415 break;
4416 case OPT_STREAM_INTERSECTION:
b5fe3e00
JG
4417 /*
4418 * Applies to all traces implementing the trace-info
4419 * query.
4420 */
4421 stream_intersection_mode = true;
9009cc24
PP
4422 break;
4423 case OPT_VERBOSE:
c4f81dc9
PP
4424 if (*default_log_level != BT_LOG_VERBOSE &&
4425 *default_log_level != BT_LOG_DEBUG) {
4426 *default_log_level = BT_LOG_INFO;
9009cc24 4427 }
9009cc24
PP
4428 break;
4429 case OPT_DEBUG:
c4f81dc9
PP
4430 *default_log_level = BT_LOG_VERBOSE;
4431 break;
4432 default:
9009cc24
PP
4433 break;
4434 }
4435
4436 free(arg);
4437 arg = NULL;
4438 }
4439
4440 /* Check for option parsing error */
4441 if (opt < -1) {
4442 printf_err("While parsing command-line options, at option %s: %s\n",
4443 poptBadOption(pc, 0), poptStrerror(opt));
4444 goto error;
4445 }
4446
9e503aa9
PP
4447 /*
4448 * Legacy behaviour: --verbose used to make the `text` output
4449 * format print more information. --verbose is now equivalent to
8a46ff40
PP
4450 * the INFO log level, which is why we compare to `BT_LOG_INFO`
4451 * here.
9e503aa9 4452 */
c4f81dc9 4453 if (*default_log_level == BT_LOG_INFO) {
9e503aa9
PP
4454 append_implicit_component_param(&implicit_text_args,
4455 "verbose", "yes");
4456 }
4457
9009cc24
PP
4458 /*
4459 * Append home and system plugin paths now that we possibly got
4460 * --plugin-path.
4461 */
4462 if (append_home_and_system_plugin_paths(plugin_paths,
4463 force_omit_system_plugin_path,
4464 force_omit_home_plugin_path)) {
4465 goto error;
4466 }
4467
94023a1c
PP
4468 /* Consume and keep leftover arguments */
4469 while ((leftover = poptGetArg(pc))) {
bf5b7748
SM
4470 bt_value_status status = bt_value_array_append_string_element(leftovers, leftover);
4471 if (status != BT_VALUE_STATUS_OK) {
94023a1c
PP
4472 print_err_oom();
4473 goto error;
4474 }
9009cc24
PP
4475 }
4476
4477 /* Print CTF metadata or print LTTng live sessions */
4478 if (print_ctf_metadata) {
bf5b7748 4479 const bt_value *bt_val_leftover;
94023a1c 4480
bf5b7748 4481 if (bt_value_array_is_empty(leftovers)) {
9009cc24
PP
4482 printf_err("--output-format=ctf-metadata specified without a path\n");
4483 goto error;
4484 }
4485
bf5b7748 4486 if (bt_value_array_get_size(leftovers) > 1) {
94023a1c
PP
4487 printf_err("Too many paths specified for --output-format=ctf-metadata\n");
4488 goto error;
4489 }
4490
9009cc24
PP
4491 cfg = bt_config_print_ctf_metadata_create(plugin_paths);
4492 if (!cfg) {
4493 goto error;
4494 }
4495
bf5b7748 4496 bt_val_leftover = bt_value_array_borrow_element_by_index_const(leftovers, 0);
9009cc24 4497 g_string_assign(cfg->cmd_data.print_ctf_metadata.path,
bf5b7748 4498 bt_value_string_get(bt_val_leftover));
a107deea
PP
4499
4500 if (output) {
4501 g_string_assign(
4502 cfg->cmd_data.print_ctf_metadata.output_path,
4503 output);
4504 }
4505
9009cc24
PP
4506 goto end;
4507 }
4508
4509 /*
e7ad156c
PP
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
4513 * is not specified.
4514 */
4515 if (implicit_ctf_output_args.exists) {
4516 if (!output) {
4517 printf_err("--output-format=ctf specified without --output (trace output path)\n");
4518 goto error;
4519 }
4520
4521 /*
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:
4526 *
142ac9b0 4527 * babeltrace2 --names=all -o ctf --output=/tmp/path my-trace
e7ad156c
PP
4528 *
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.
4532 */
4533 if (implicit_text_args.exists) {
4534 printf_err("Ambiguous --output option: --output-format=ctf specified but another option implies --output-format=text\n");
4535 goto error;
4536 }
4537 }
4538
4539 /*
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.
9009cc24 4543 */
e7ad156c
PP
4544 if (!implicit_dummy_args.exists && !implicit_ctf_output_args.exists &&
4545 !sink_names) {
9009cc24
PP
4546 implicit_text_args.exists = true;
4547 }
4548
e7ad156c
PP
4549 /*
4550 * Set implicit `sink.text.pretty` or `sink.ctf.fs` component's
4551 * `path` parameter if --output was specified.
4552 */
4553 if (output) {
4554 if (implicit_text_args.exists) {
4555 append_implicit_component_extra_param(&implicit_text_args,
4556 "path", output);
4557 } else if (implicit_ctf_output_args.exists) {
4558 append_implicit_component_extra_param(&implicit_ctf_output_args,
4559 "path", output);
4560 }
4561 }
4562
94023a1c 4563 /* Decide where the leftover argument(s) go */
bf5b7748 4564 if (bt_value_array_get_size(leftovers) > 0) {
9009cc24 4565 if (implicit_lttng_live_args.exists) {
bf5b7748 4566 const bt_value *bt_val_leftover;
94023a1c 4567
bf5b7748 4568 if (bt_value_array_get_size(leftovers) > 1) {
94023a1c
PP
4569 printf_err("Too many URLs specified for --output-format=lttng-live\n");
4570 goto error;
4571 }
4572
bf5b7748 4573 bt_val_leftover = bt_value_array_borrow_element_by_index_const(leftovers, 0);
9009cc24 4574 lttng_live_url_parts =
bf5b7748 4575 bt_common_parse_lttng_live_url(bt_value_string_get(bt_val_leftover),
94b828f3 4576 error_buf, sizeof(error_buf));
9009cc24
PP
4577 if (!lttng_live_url_parts.proto) {
4578 printf_err("Invalid LTTng live URL format: %s\n",
4579 error_buf);
4580 goto error;
4581 }
4582
4583 if (!lttng_live_url_parts.session_name) {
4584 /* Print LTTng live sessions */
4585 cfg = bt_config_print_lttng_live_sessions_create(
4586 plugin_paths);
4587 if (!cfg) {
4588 goto error;
4589 }
4590
9009cc24 4591 g_string_assign(cfg->cmd_data.print_lttng_live_sessions.url,
bf5b7748 4592 bt_value_string_get(bt_val_leftover));
a107deea
PP
4593
4594 if (output) {
4595 g_string_assign(
4596 cfg->cmd_data.print_lttng_live_sessions.output_path,
4597 output);
4598 }
4599
9009cc24
PP
4600 goto end;
4601 }
4602
e7ad156c 4603 ret = append_implicit_component_extra_param(
94023a1c 4604 &implicit_lttng_live_args, "url",
bf5b7748 4605 bt_value_string_get(bt_val_leftover));
9009cc24
PP
4606 if (ret) {
4607 goto error;
4608 }
1e20174d
FD
4609
4610 ret = append_implicit_component_extra_param(
4611 &implicit_lttng_live_args,
4612 "session-not-found-action", "end");
4613 if (ret) {
4614 goto error;
4615 }
9009cc24 4616 } else {
94023a1c 4617 /*
bf5b7748
SM
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.
94023a1c 4621 */
bf5b7748
SM
4622 implicit_ctf_input_args.exists = true;
4623 ret = append_parameter_to_args(implicit_ctf_input_args.extra_params,
4624 "paths", leftovers);
9009cc24
PP
4625 if (ret) {
4626 goto error;
4627 }
4628 }
4629 }
4630
4631 /*
fd5f8053
PP
4632 * Ensure mutual exclusion between implicit `source.ctf.fs` and
4633 * `source.ctf.lttng-live` components.
9009cc24 4634 */
bf5b7748 4635 if (implicit_ctf_input_args.exists && implicit_lttng_live_args.exists) {
fd5f8053 4636 printf_err("Cannot create both implicit `%s` and `%s` components\n",
bf5b7748 4637 implicit_ctf_input_args.comp_arg->str,
fd5f8053 4638 implicit_lttng_live_args.comp_arg->str);
9009cc24
PP
4639 goto error;
4640 }
4641
4642 /*
fd5f8053 4643 * If the implicit `source.ctf.fs` or `source.ctf.lttng-live`
94023a1c
PP
4644 * components exists, make sure there's at least one leftover
4645 * (which is the path or URL).
9009cc24 4646 */
bf5b7748 4647 if (implicit_ctf_input_args.exists && bt_value_array_is_empty(leftovers)) {
fd5f8053 4648 printf_err("Missing path for implicit `%s` component\n",
bf5b7748 4649 implicit_ctf_input_args.comp_arg->str);
9009cc24
PP
4650 goto error;
4651 }
4652
bf5b7748 4653 if (implicit_lttng_live_args.exists && bt_value_array_is_empty(leftovers)) {
fd5f8053
PP
4654 printf_err("Missing URL for implicit `%s` component\n",
4655 implicit_lttng_live_args.comp_arg->str);
9009cc24
PP
4656 goto error;
4657 }
4658
4659 /* Assign names to implicit components */
bf5b7748
SM
4660 ret = assign_name_to_implicit_component(&implicit_ctf_input_args,
4661 "source-ctf-fs", all_names, &source_names, true);
4662 if (ret) {
4663 goto error;
9009cc24
PP
4664 }
4665
4666 ret = assign_name_to_implicit_component(&implicit_lttng_live_args,
4667 "lttng-live", all_names, &source_names, true);
4668 if (ret) {
4669 goto error;
4670 }
4671
4672 ret = assign_name_to_implicit_component(&implicit_text_args,
4673 "pretty", all_names, &sink_names, true);
4674 if (ret) {
4675 goto error;
4676 }
4677
e7ad156c
PP
4678 ret = assign_name_to_implicit_component(&implicit_ctf_output_args,
4679 "sink-ctf-fs", all_names, &sink_names, true);
4680 if (ret) {
4681 goto error;
4682 }
4683
9009cc24
PP
4684 ret = assign_name_to_implicit_component(&implicit_dummy_args,
4685 "dummy", all_names, &sink_names, true);
4686 if (ret) {
4687 goto error;
4688 }
4689
4690 ret = assign_name_to_implicit_component(&implicit_muxer_args,
4691 "muxer", all_names, NULL, false);
4692 if (ret) {
4693 goto error;
4694 }
4695
4696 ret = assign_name_to_implicit_component(&implicit_trimmer_args,
4697 "trimmer", all_names, NULL, false);
4698 if (ret) {
4699 goto error;
4700 }
4701
4702 ret = assign_name_to_implicit_component(&implicit_debug_info_args,
4703 "debug-info", all_names, NULL, false);
4704 if (ret) {
4705 goto error;
4706 }
4707
4708 /* Make sure there's at least one source and one sink */
4709 if (!source_names) {
4710 printf_err("No source component\n");
4711 goto error;
4712 }
4713
4714 if (!sink_names) {
4715 printf_err("No sink component\n");
4716 goto error;
4717 }
4718
4719 /*
4720 * Prepend the muxer, the trimmer, and the debug info to the
4721 * filter chain so that we have:
4722 *
4723 * sources -> muxer -> [trimmer] -> [debug info] ->
4724 * [user filters] -> sinks
4725 */
4726 if (implicit_debug_info_args.exists) {
4727 if (g_list_prepend_gstring(&filter_names,
4728 implicit_debug_info_args.name_arg->str)) {
4729 goto error;
4730 }
4731 }
4732
4733 if (implicit_trimmer_args.exists) {
4734 if (g_list_prepend_gstring(&filter_names,
4735 implicit_trimmer_args.name_arg->str)) {
4736 goto error;
4737 }
4738 }
4739
4740 if (g_list_prepend_gstring(&filter_names,
4741 implicit_muxer_args.name_arg->str)) {
4742 goto error;
4743 }
4744
4745 /*
4746 * Append the equivalent run arguments for the implicit
4747 * components.
4748 */
bf5b7748
SM
4749 ret = append_run_args_for_implicit_component(&implicit_ctf_input_args, run_args);
4750 if (ret) {
4751 goto error;
9009cc24
PP
4752 }
4753
fd5f8053 4754 ret = append_run_args_for_implicit_component(&implicit_lttng_live_args,
9009cc24
PP
4755 run_args);
4756 if (ret) {
4757 goto error;
4758 }
4759
fd5f8053
PP
4760 ret = append_run_args_for_implicit_component(&implicit_text_args,
4761 run_args);
9009cc24
PP
4762 if (ret) {
4763 goto error;
4764 }
4765
e7ad156c
PP
4766 ret = append_run_args_for_implicit_component(&implicit_ctf_output_args,
4767 run_args);
4768 if (ret) {
4769 goto error;
4770 }
4771
fd5f8053
PP
4772 ret = append_run_args_for_implicit_component(&implicit_dummy_args,
4773 run_args);
9009cc24
PP
4774 if (ret) {
4775 goto error;
4776 }
4777
fd5f8053
PP
4778 ret = append_run_args_for_implicit_component(&implicit_muxer_args,
4779 run_args);
9009cc24
PP
4780 if (ret) {
4781 goto error;
4782 }
4783
fd5f8053 4784 ret = append_run_args_for_implicit_component(&implicit_trimmer_args,
9009cc24
PP
4785 run_args);
4786 if (ret) {
4787 goto error;
4788 }
4789
fd5f8053 4790 ret = append_run_args_for_implicit_component(&implicit_debug_info_args,
9009cc24
PP
4791 run_args);
4792 if (ret) {
4793 goto error;
4794 }
4795
4796 /* Auto-connect components */
4797 ret = convert_auto_connect(run_args, source_names, filter_names,
4798 sink_names);
4799 if (ret) {
4800 printf_err("Cannot auto-connect components\n");
4801 goto error;
4802 }
4803
4804 /*
4805 * We have all the run command arguments now. Depending on
4806 * --run-args, we pass this to the run command or print them
4807 * here.
4808 */
4809 if (print_run_args || print_run_args_0) {
f9692f14
PP
4810 if (stream_intersection_mode) {
4811 printf_err("Cannot specify --stream-intersection with --run-args or --run-args-0\n");
4812 goto error;
4813 }
4814
ce141536 4815 for (i = 0; i < bt_value_array_get_size(run_args); i++) {
8eee8ea2 4816 const bt_value *arg_value =
ce141536
PP
4817 bt_value_array_borrow_element_by_index(run_args,
4818 i);
9009cc24
PP
4819 const char *arg;
4820 GString *quoted = NULL;
4821 const char *arg_to_print;
4822
8b45963b 4823 BT_ASSERT(arg_value);
b5cdc106 4824 arg = bt_value_string_get(arg_value);
9009cc24
PP
4825
4826 if (print_run_args) {
4827 quoted = bt_common_shell_quote(arg, true);
4828 if (!quoted) {
4829 goto error;
4830 }
4831
4832 arg_to_print = quoted->str;
4833 } else {
4834 arg_to_print = arg;
4835 }
4836
4837 printf("%s", arg_to_print);
4838
4839 if (quoted) {
4840 g_string_free(quoted, TRUE);
4841 }
4842
ce141536 4843 if (i < bt_value_array_get_size(run_args) - 1) {
9009cc24
PP
4844 if (print_run_args) {
4845 putchar(' ');
4846 } else {
4847 putchar('\0');
4848 }
4849 }
4850 }
4851
4852 *retcode = -1;
8138bfe1 4853 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
4854 goto end;
4855 }
4856
c4f81dc9
PP
4857 /*
4858 * If the log level is still unset at this point, set it to
4859 * the program's default.
4860 */
4861 if (*default_log_level < 0) {
4862 *default_log_level = cli_default_log_level;
4863 }
4864
ce141536 4865 cfg = bt_config_run_from_args_array(run_args, retcode,
c4f81dc9
PP
4866 force_omit_system_plugin_path,
4867 force_omit_home_plugin_path,
4868 initial_plugin_paths, *default_log_level);
fc11b6a6
PP
4869 if (!cfg) {
4870 goto error;
4871 }
4872
b5fe3e00 4873 cfg->cmd_data.run.stream_intersection_mode = stream_intersection_mode;
9009cc24
PP
4874 goto end;
4875
4876error:
4877 *retcode = 1;
8138bfe1 4878 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
4879
4880end:
4881 if (pc) {
4882 poptFreeContext(pc);
4883 }
4884
4885 free(arg);
e7ad156c 4886 free(output);
9009cc24
PP
4887
4888 if (cur_name) {
4889 g_string_free(cur_name, TRUE);
4890 }
4891
4892 if (cur_name_prefix) {
4893 g_string_free(cur_name_prefix, TRUE);
4894 }
4895
8c6884d9
PP
4896 bt_value_put_ref(run_args);
4897 bt_value_put_ref(all_names);
9009cc24
PP
4898 destroy_glist_of_gstring(source_names);
4899 destroy_glist_of_gstring(filter_names);
4900 destroy_glist_of_gstring(sink_names);
bf5b7748
SM
4901 bt_value_put_ref(leftovers);
4902 finalize_implicit_component_args(&implicit_ctf_input_args);
94023a1c
PP
4903 finalize_implicit_component_args(&implicit_ctf_output_args);
4904 finalize_implicit_component_args(&implicit_lttng_live_args);
4905 finalize_implicit_component_args(&implicit_dummy_args);
4906 finalize_implicit_component_args(&implicit_text_args);
4907 finalize_implicit_component_args(&implicit_debug_info_args);
4908 finalize_implicit_component_args(&implicit_muxer_args);
4909 finalize_implicit_component_args(&implicit_trimmer_args);
8c6884d9 4910 bt_value_put_ref(plugin_paths);
9009cc24
PP
4911 bt_common_destroy_lttng_live_url_parts(&lttng_live_url_parts);
4912 return cfg;
4913}
4914
4915/*
4916 * Prints the Babeltrace 2.x general usage.
4917 */
4918static
4919void print_gen_usage(FILE *fp)
4920{
142ac9b0 4921 fprintf(fp, "Usage: babeltrace2 [GENERAL OPTIONS] [COMMAND] [COMMAND ARGUMENTS]\n");
9009cc24
PP
4922 fprintf(fp, "\n");
4923 fprintf(fp, "General options:\n");
4924 fprintf(fp, "\n");
9e503aa9
PP
4925 fprintf(fp, " -d, --debug Enable debug mode (same as --log-level=V)\n");
4926 fprintf(fp, " -h, --help Show this help and quit\n");
c4f81dc9 4927 fprintf(fp, " -l, --log-level=LVL Set the default log level to LVL (`N`, `V`, `D`,\n");
9e503aa9
PP
4928 fprintf(fp, " `I`, `W` (default), `E`, or `F`)\n");
4929 fprintf(fp, " -v, --verbose Enable verbose mode (same as --log-level=I)\n");
4930 fprintf(fp, " -V, --version Show version and quit\n");
9009cc24
PP
4931 fprintf(fp, "\n");
4932 fprintf(fp, "Available commands:\n");
4933 fprintf(fp, "\n");
4934 fprintf(fp, " convert Convert and trim traces (default)\n");
4935 fprintf(fp, " help Get help for a plugin or a component class\n");
4936 fprintf(fp, " list-plugins List available plugins and their content\n");
4937 fprintf(fp, " query Query objects from a component class\n");
4938 fprintf(fp, " run Build a processing graph and run it\n");
4939 fprintf(fp, "\n");
142ac9b0 4940 fprintf(fp, "Use `babeltrace2 COMMAND --help` to show the help of COMMAND.\n");
9009cc24
PP
4941}
4942
4943struct bt_config *bt_config_cli_args_create(int argc, const char *argv[],
4944 int *retcode, bool force_omit_system_plugin_path,
8e765000 4945 bool force_omit_home_plugin_path,
8eee8ea2 4946 const bt_value *initial_plugin_paths)
9009cc24
PP
4947{
4948 struct bt_config *config = NULL;
9009cc24
PP
4949 int i;
4950 const char **command_argv = NULL;
4951 int command_argc = -1;
4952 const char *command_name = NULL;
c4f81dc9 4953 int default_log_level = -1;
9009cc24
PP
4954
4955 enum command_type {
4956 COMMAND_TYPE_NONE = -1,
4957 COMMAND_TYPE_RUN = 0,
4958 COMMAND_TYPE_CONVERT,
4959 COMMAND_TYPE_LIST_PLUGINS,
4960 COMMAND_TYPE_HELP,
4961 COMMAND_TYPE_QUERY,
4962 } command_type = COMMAND_TYPE_NONE;
4963
4964 *retcode = -1;
4965
4966 if (!initial_plugin_paths) {
ce141536 4967 initial_plugin_paths = bt_value_array_create();
9009cc24
PP
4968 if (!initial_plugin_paths) {
4969 *retcode = 1;
4970 goto end;
4971 }
4972 } else {
8c6884d9 4973 bt_value_get_ref(initial_plugin_paths);
9009cc24
PP
4974 }
4975
4976 if (argc <= 1) {
329e245b
PP
4977 print_version();
4978 puts("");
9009cc24
PP
4979 print_gen_usage(stdout);
4980 goto end;
4981 }
4982
4983 for (i = 1; i < argc; i++) {
4984 const char *cur_arg = argv[i];
9e503aa9 4985 const char *next_arg = i == (argc - 1) ? NULL : argv[i + 1];
9009cc24
PP
4986
4987 if (strcmp(cur_arg, "-d") == 0 ||
4988 strcmp(cur_arg, "--debug") == 0) {
c4f81dc9 4989 default_log_level = BT_LOG_VERBOSE;
9009cc24
PP
4990 } else if (strcmp(cur_arg, "-v") == 0 ||
4991 strcmp(cur_arg, "--verbose") == 0) {
c4f81dc9
PP
4992 if (default_log_level != BT_LOG_VERBOSE &&
4993 default_log_level != BT_LOG_DEBUG) {
9e503aa9
PP
4994 /*
4995 * Legacy: do not override a previous
4996 * --debug because --verbose and --debug
4997 * can be specified together (in this
4998 * case we want the lowest log level to
4999 * apply, VERBOSE).
5000 */
c4f81dc9 5001 default_log_level = BT_LOG_INFO;
9e503aa9 5002 }
21b5b16a
PP
5003 } else if (strcmp(cur_arg, "--log-level") == 0 ||
5004 strcmp(cur_arg, "-l") == 0) {
9e503aa9
PP
5005 if (!next_arg) {
5006 printf_err("Missing log level value for --log-level option\n");
5007 *retcode = 1;
5008 goto end;
5009 }
5010
c4f81dc9
PP
5011 default_log_level =
5012 bt_log_get_level_from_string(next_arg);
5013 if (default_log_level < 0) {
9e503aa9
PP
5014 printf_err("Invalid argument for --log-level option:\n %s\n",
5015 next_arg);
5016 *retcode = 1;
5017 goto end;
5018 }
5019
5020 i++;
5021 } else if (strncmp(cur_arg, "--log-level=", 12) == 0) {
5022 const char *arg = &cur_arg[12];
5023
c4f81dc9
PP
5024 default_log_level = bt_log_get_level_from_string(arg);
5025 if (default_log_level < 0) {
21b5b16a
PP
5026 printf_err("Invalid argument for --log-level option:\n %s\n",
5027 arg);
5028 *retcode = 1;
5029 goto end;
5030 }
5031 } else if (strncmp(cur_arg, "-l", 2) == 0) {
5032 const char *arg = &cur_arg[2];
5033
c4f81dc9
PP
5034 default_log_level = bt_log_get_level_from_string(arg);
5035 if (default_log_level < 0) {
9e503aa9
PP
5036 printf_err("Invalid argument for --log-level option:\n %s\n",
5037 arg);
5038 *retcode = 1;
5039 goto end;
5040 }
9009cc24
PP
5041 } else if (strcmp(cur_arg, "-V") == 0 ||
5042 strcmp(cur_arg, "--version") == 0) {
5043 print_version();
5044 goto end;
5045 } else if (strcmp(cur_arg, "-h") == 0 ||
5046 strcmp(cur_arg, "--help") == 0) {
5047 print_gen_usage(stdout);
5048 goto end;
5049 } else {
9009cc24
PP
5050 /*
5051 * First unknown argument: is it a known command
5052 * name?
5053 */
9e503aa9
PP
5054 command_argv = &argv[i];
5055 command_argc = argc - i;
5056
9009cc24
PP
5057 if (strcmp(cur_arg, "convert") == 0) {
5058 command_type = COMMAND_TYPE_CONVERT;
5059 } else if (strcmp(cur_arg, "list-plugins") == 0) {
5060 command_type = COMMAND_TYPE_LIST_PLUGINS;
5061 } else if (strcmp(cur_arg, "help") == 0) {
5062 command_type = COMMAND_TYPE_HELP;
5063 } else if (strcmp(cur_arg, "query") == 0) {
5064 command_type = COMMAND_TYPE_QUERY;
5065 } else if (strcmp(cur_arg, "run") == 0) {
5066 command_type = COMMAND_TYPE_RUN;
5067 } else {
5068 /*
5069 * Unknown argument, but not a known
9e503aa9
PP
5070 * command name: assume the default
5071 * `convert` command.
9009cc24
PP
5072 */
5073 command_type = COMMAND_TYPE_CONVERT;
9e503aa9
PP
5074 command_name = "convert";
5075 command_argv = &argv[i - 1];
5076 command_argc = argc - i + 1;
9009cc24
PP
5077 }
5078 break;
5079 }
5080 }
5081
5082 if (command_type == COMMAND_TYPE_NONE) {
5083 /*
5084 * We only got non-help, non-version general options
5085 * like --verbose and --debug, without any other
5086 * arguments, so we can't do anything useful: print the
5087 * usage and quit.
5088 */
5089 print_gen_usage(stdout);
5090 goto end;
5091 }
5092
8b45963b
PP
5093 BT_ASSERT(command_argv);
5094 BT_ASSERT(command_argc >= 0);
9009cc24 5095
c4f81dc9
PP
5096 /*
5097 * The convert command can set its own default log level for
5098 * backward compatibility reasons. It only does so if there's no
5099 * log level yet, so do not force one for this command.
5100 */
5101 if (command_type != COMMAND_TYPE_CONVERT && default_log_level < 0) {
5102 /* Default log level */
5103 default_log_level = cli_default_log_level;
5104 }
5105
9009cc24
PP
5106 switch (command_type) {
5107 case COMMAND_TYPE_RUN:
5108 config = bt_config_run_from_args(command_argc, command_argv,
5109 retcode, force_omit_system_plugin_path,
c4f81dc9
PP
5110 force_omit_home_plugin_path, initial_plugin_paths,
5111 default_log_level);
9009cc24
PP
5112 break;
5113 case COMMAND_TYPE_CONVERT:
5114 config = bt_config_convert_from_args(command_argc, command_argv,
5115 retcode, force_omit_system_plugin_path,
8e765000 5116 force_omit_home_plugin_path,
c4f81dc9 5117 initial_plugin_paths, &default_log_level);
9009cc24
PP
5118 break;
5119 case COMMAND_TYPE_LIST_PLUGINS:
5120 config = bt_config_list_plugins_from_args(command_argc,
5121 command_argv, retcode, force_omit_system_plugin_path,
5122 force_omit_home_plugin_path, initial_plugin_paths);
5123 break;
5124 case COMMAND_TYPE_HELP:
5125 config = bt_config_help_from_args(command_argc,
5126 command_argv, retcode, force_omit_system_plugin_path,
c4f81dc9
PP
5127 force_omit_home_plugin_path, initial_plugin_paths,
5128 default_log_level);
9009cc24
PP
5129 break;
5130 case COMMAND_TYPE_QUERY:
5131 config = bt_config_query_from_args(command_argc,
5132 command_argv, retcode, force_omit_system_plugin_path,
c4f81dc9
PP
5133 force_omit_home_plugin_path, initial_plugin_paths,
5134 default_log_level);
9009cc24
PP
5135 break;
5136 default:
0fbb9a9f 5137 abort();
9009cc24
PP
5138 }
5139
5140 if (config) {
c4f81dc9
PP
5141 BT_ASSERT(default_log_level >= BT_LOG_VERBOSE);
5142 config->log_level = default_log_level;
9009cc24
PP
5143 config->command_name = command_name;
5144 }
5145
5146end:
8c6884d9 5147 bt_value_put_ref(initial_plugin_paths);
9009cc24
PP
5148 return config;
5149}
This page took 0.27803 seconds and 4 git commands to generate.