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