Fix: cli: Acquire reference on bt_value_null while parsing args
[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
f4f9e43b
PP
25#define BT_LOG_TAG "CLI-CFG-CLI-ARGS"
26#include "logging.h"
27
9009cc24
PP
28#include <errno.h>
29#include <stdlib.h>
30#include <string.h>
8b45963b 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"
b6a0d2d9 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) */
8eee8ea2 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;
8eee8ea2
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;
8eee8ea2 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
ce141536
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
ce141536 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 */
ce141536 300 value = bt_value_real_create_init(state->scanner->value.v_float);
9009cc24
PP
301 break;
302 case G_TOKEN_STRING:
303 /* Quoted string */
ce141536 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")) {
ce141536 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")) {
ce141536 331 value = bt_value_bool_create_init(false);
9009cc24 332 } else {
ce141536 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
ce141536 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 */
ce141536 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) {
ce141536 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:
8c6884d9 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
8eee8ea2 430bt_value *bt_value_from_ini(const char *arg,
17582c6d 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
ce141536 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:
8c6884d9 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
8eee8ea2 556bt_value *bt_value_from_arg(const char *arg)
9009cc24 557{
8eee8ea2 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 */
ce141536 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 }
17582c6d 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
75a1a799
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,
ee78f405 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
8b45963b
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') {
93c1364b 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{
b6a0d2d9
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
8eee8ea2 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
8c6884d9 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(
ee78f405 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
1d7bf349
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 */
ce141536 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:
8138bfe1 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;
ee78f405 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:
8138bfe1 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
8eee8ea2 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
8c6884d9 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:
8138bfe1 902 BT_OBJECT_PUT_REF_AND_RESET(cfg->cmd_data.help.cfg_component);
9009cc24
PP
903 break;
904 case BT_CONFIG_COMMAND_QUERY:
8138bfe1 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);
a107deea
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);
a107deea
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
8eee8ea2 1010bt_value *names_from_arg(const char *arg)
9009cc24
PP
1011{
1012 GScanner *scanner = NULL;
8eee8ea2 1013 bt_value *names = NULL;
9009cc24
PP
1014 bool found_all = false, found_none = false, found_item = false;
1015
ce141536 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;
ce141536 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;
ce141536 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;
ce141536 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;
ce141536 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;
ce141536 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) {
ce141536 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:
8c6884d9 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
8eee8ea2 1122bt_value *fields_from_arg(const char *arg)
9009cc24
PP
1123{
1124 GScanner *scanner = NULL;
8eee8ea2 1125 bt_value *fields;
9009cc24 1126
ce141536 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")) {
ce141536 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:
8c6884d9 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{
8b45963b
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,
8eee8ea2 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
44514773 1240 for (i = 0; i < bt_value_array_get_size(names_array); i++) {
8eee8ea2 1241 const bt_value *str_obj =
ce141536
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
b5cdc106 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,
8e765000 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{
8138bfe1 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,
8eee8ea2 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
ce141536
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
ce141536 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
8eee8ea2 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()) {
f4f9e43b 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
8eee8ea2 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()) {
f4f9e43b 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,
8eee8ea2 1487 const bt_value *initial_plugin_paths,
17582c6d 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
1d7bf349 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) {
8eee8ea2 1504 bt_value *initial_plugin_paths_copy;
ce141536 1505
6284461f
PP
1506 (void) bt_value_copy(initial_plugin_paths,
1507 &initial_plugin_paths_copy);
ce141536 1508 cfg->plugin_paths = initial_plugin_paths_copy;
9009cc24 1509 } else {
ce141536 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:
8138bfe1 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(
8eee8ea2 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(
8138bfe1 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(
8138bfe1 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(
8138bfe1 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:
8138bfe1 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(
8eee8ea2 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:
8138bfe1 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(
8eee8ea2 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 =
834e9996 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:
8138bfe1 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(
8eee8ea2 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:
8138bfe1 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(
8eee8ea2 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
a107deea
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:
8138bfe1 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(
8eee8ea2 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
a107deea
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:
8138bfe1 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(
8eee8ea2 1725 bt_value *plugin_paths, const char *arg)
9009cc24
PP
1726{
1727 int ret = 0;
1728
1729 if (bt_common_is_setuid_setgid()) {
f4f9e43b 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");
75a1a799 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");
9e503aa9 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,
8eee8ea2 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;
8138bfe1 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) {
75a1a799 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) {
75a1a799
PP
1903 /* Component class help */
1904 g_string_assign(
1905 cfg->cmd_data.help.cfg_component->plugin_name,
9009cc24 1906 plugin_name);
75a1a799
PP
1907 g_string_assign(
1908 cfg->cmd_data.help.cfg_component->comp_cls_name,
9009cc24
PP
1909 comp_cls_name);
1910 } else {
75a1a799 1911 /* Fall back to plugin help */
75a1a799
PP
1912 g_string_assign(
1913 cfg->cmd_data.help.cfg_component->plugin_name,
1914 leftover);
9009cc24 1915 }
75a1a799
PP
1916 } else {
1917 print_help_usage(stdout);
1918 *retcode = -1;
8138bfe1 1919 BT_OBJECT_PUT_REF_AND_RESET(cfg);
75a1a799 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;
8138bfe1 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{
93c1364b 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");
9e503aa9 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,
8eee8ea2 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;
13041794
SM
1996 bt_value *params;
1997
1998 params = bt_value_null;
1999 bt_value_get_ref(bt_value_null);
9009cc24
PP
2000
2001 *retcode = 0;
2002 cfg = bt_config_query_create(initial_plugin_paths);
2003 if (!cfg) {
2004 goto error;
2005 }
2006
2007 cfg->omit_system_plugin_path = force_omit_system_plugin_path;
2008 cfg->omit_home_plugin_path = force_omit_home_plugin_path;
2009 ret = append_env_var_plugin_paths(cfg->plugin_paths);
2010 if (ret) {
2011 goto error;
2012 }
2013
2014 /* Parse options */
2015 pc = poptGetContext(NULL, argc, (const char **) argv,
2016 query_long_options, 0);
2017 if (!pc) {
2018 printf_err("Cannot get popt context\n");
2019 goto error;
2020 }
2021
2022 poptReadDefaultConfig(pc, 0);
2023
2024 while ((opt = poptGetNextOpt(pc)) > 0) {
2025 arg = poptGetOptArg(pc);
2026
2027 switch (opt) {
2028 case OPT_PLUGIN_PATH:
2029 if (bt_config_append_plugin_paths_check_setuid_setgid(
2030 cfg->plugin_paths, arg)) {
2031 goto error;
2032 }
2033 break;
2034 case OPT_OMIT_SYSTEM_PLUGIN_PATH:
2035 cfg->omit_system_plugin_path = true;
2036 break;
2037 case OPT_OMIT_HOME_PLUGIN_PATH:
2038 cfg->omit_home_plugin_path = true;
2039 break;
9009cc24
PP
2040 case OPT_PARAMS:
2041 {
8c6884d9 2042 bt_value_put_ref(params);
ce141536 2043 params = bt_value_from_arg(arg);
9009cc24
PP
2044 if (!params) {
2045 printf_err("Invalid format for --params option's argument:\n %s\n",
2046 arg);
2047 goto error;
2048 }
2049 break;
2050 }
2051 case OPT_HELP:
2052 print_query_usage(stdout);
2053 *retcode = -1;
8138bfe1 2054 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
2055 goto end;
2056 default:
2057 printf_err("Unknown command-line option specified (option code %d)\n",
2058 opt);
2059 goto error;
2060 }
2061
2062 free(arg);
2063 arg = NULL;
2064 }
2065
9009cc24
PP
2066 /* Check for option parsing error */
2067 if (opt < -1) {
2068 printf_err("While parsing command-line options, at option %s: %s\n",
2069 poptBadOption(pc, 0), poptStrerror(opt));
2070 goto error;
2071 }
2072
2073 /*
93c1364b
PP
2074 * We need exactly two leftover arguments which are the
2075 * mandatory component class specification and query object.
9009cc24 2076 */
93c1364b
PP
2077 leftover = poptGetArg(pc);
2078 if (leftover) {
2079 cfg->cmd_data.query.cfg_component =
2080 bt_config_component_from_arg(leftover);
2081 if (!cfg->cmd_data.query.cfg_component) {
2082 printf_err("Invalid format for component class specification:\n %s\n",
2083 leftover);
2084 goto error;
2085 }
2086
8b45963b 2087 BT_ASSERT(params);
17582c6d
PP
2088 BT_OBJECT_MOVE_REF(cfg->cmd_data.query.cfg_component->params,
2089 params);
93c1364b
PP
2090 } else {
2091 print_query_usage(stdout);
2092 *retcode = -1;
8138bfe1 2093 BT_OBJECT_PUT_REF_AND_RESET(cfg);
93c1364b
PP
2094 goto end;
2095 }
2096
9009cc24
PP
2097 leftover = poptGetArg(pc);
2098 if (leftover) {
2099 if (strlen(leftover) == 0) {
2100 printf_err("Invalid empty object\n");
2101 goto error;
2102 }
2103
2104 g_string_assign(cfg->cmd_data.query.object, leftover);
2105 } else {
2106 print_query_usage(stdout);
2107 *retcode = -1;
8138bfe1 2108 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
2109 goto end;
2110 }
2111
2112 leftover = poptGetArg(pc);
2113 if (leftover) {
2114 printf_err("Unexpected argument: %s\n", leftover);
2115 goto error;
2116 }
2117
2118 if (append_home_and_system_plugin_paths_cfg(cfg)) {
2119 goto error;
2120 }
2121
2122 goto end;
2123
2124error:
2125 *retcode = 1;
8138bfe1 2126 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
2127
2128end:
2129 if (pc) {
2130 poptFreeContext(pc);
2131 }
2132
8c6884d9 2133 bt_value_put_ref(params);
9009cc24
PP
2134 free(arg);
2135 return cfg;
2136}
2137
2138/*
2139 * Prints the list-plugins command usage.
2140 */
2141static
2142void print_list_plugins_usage(FILE *fp)
2143{
2144 fprintf(fp, "Usage: babeltrace [GENERAL OPTIONS] list-plugins [OPTIONS]\n");
2145 fprintf(fp, "\n");
2146 fprintf(fp, "Options:\n");
2147 fprintf(fp, "\n");
2148 fprintf(fp, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2149 fprintf(fp, " (~/.local/lib/babeltrace/plugins)\n");
2150 fprintf(fp, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2151 fprintf(fp, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2152 fprintf(fp, " dynamic plugins can be loaded\n");
9e503aa9 2153 fprintf(fp, " -h, --help Show this help and quit\n");
9009cc24
PP
2154 fprintf(fp, "\n");
2155 fprintf(fp, "See `babeltrace --help` for the list of general options.\n");
2156 fprintf(fp, "\n");
2157 fprintf(fp, "Use `babeltrace help` to get help for a specific plugin or component class.\n");
2158}
2159
2160static
2161struct poptOption list_plugins_long_options[] = {
2162 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
2163 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
2164 { "omit-home-plugin-path", '\0', POPT_ARG_NONE, NULL, OPT_OMIT_HOME_PLUGIN_PATH, NULL, NULL },
2165 { "omit-system-plugin-path", '\0', POPT_ARG_NONE, NULL, OPT_OMIT_SYSTEM_PLUGIN_PATH, NULL, NULL },
2166 { "plugin-path", '\0', POPT_ARG_STRING, NULL, OPT_PLUGIN_PATH, NULL, NULL },
2167 { NULL, 0, '\0', NULL, 0, NULL, NULL },
2168};
2169
2170/*
2171 * Creates a Babeltrace config object from the arguments of a
2172 * list-plugins command.
2173 *
2174 * *retcode is set to the appropriate exit code to use.
2175 */
2176static
2177struct bt_config *bt_config_list_plugins_from_args(int argc, const char *argv[],
2178 int *retcode, bool force_omit_system_plugin_path,
2179 bool force_omit_home_plugin_path,
8eee8ea2 2180 const bt_value *initial_plugin_paths)
9009cc24
PP
2181{
2182 poptContext pc = NULL;
2183 char *arg = NULL;
2184 int opt;
2185 int ret;
2186 struct bt_config *cfg = NULL;
2187 const char *leftover;
2188
2189 *retcode = 0;
2190 cfg = bt_config_list_plugins_create(initial_plugin_paths);
2191 if (!cfg) {
2192 goto error;
2193 }
2194
2195 cfg->omit_system_plugin_path = force_omit_system_plugin_path;
2196 cfg->omit_home_plugin_path = force_omit_home_plugin_path;
2197 ret = append_env_var_plugin_paths(cfg->plugin_paths);
2198 if (ret) {
2199 goto error;
2200 }
2201
2202 /* Parse options */
2203 pc = poptGetContext(NULL, argc, (const char **) argv,
2204 list_plugins_long_options, 0);
2205 if (!pc) {
2206 printf_err("Cannot get popt context\n");
2207 goto error;
2208 }
2209
2210 poptReadDefaultConfig(pc, 0);
2211
2212 while ((opt = poptGetNextOpt(pc)) > 0) {
2213 arg = poptGetOptArg(pc);
2214
2215 switch (opt) {
2216 case OPT_PLUGIN_PATH:
2217 if (bt_config_append_plugin_paths_check_setuid_setgid(
2218 cfg->plugin_paths, arg)) {
2219 goto error;
2220 }
2221 break;
2222 case OPT_OMIT_SYSTEM_PLUGIN_PATH:
2223 cfg->omit_system_plugin_path = true;
2224 break;
2225 case OPT_OMIT_HOME_PLUGIN_PATH:
2226 cfg->omit_home_plugin_path = true;
2227 break;
2228 case OPT_HELP:
2229 print_list_plugins_usage(stdout);
2230 *retcode = -1;
8138bfe1 2231 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
2232 goto end;
2233 default:
2234 printf_err("Unknown command-line option specified (option code %d)\n",
2235 opt);
2236 goto error;
2237 }
2238
2239 free(arg);
2240 arg = NULL;
2241 }
2242
2243 /* Check for option parsing error */
2244 if (opt < -1) {
2245 printf_err("While parsing command-line options, at option %s: %s\n",
2246 poptBadOption(pc, 0), poptStrerror(opt));
2247 goto error;
2248 }
2249
2250 leftover = poptGetArg(pc);
2251 if (leftover) {
2252 printf_err("Unexpected argument: %s\n", leftover);
2253 goto error;
2254 }
2255
2256 if (append_home_and_system_plugin_paths_cfg(cfg)) {
2257 goto error;
2258 }
2259
2260 goto end;
2261
2262error:
2263 *retcode = 1;
8138bfe1 2264 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
2265
2266end:
2267 if (pc) {
2268 poptFreeContext(pc);
2269 }
2270
2271 free(arg);
2272 return cfg;
2273}
2274
2275/*
2276 * Prints the run command usage.
2277 */
2278static
2279void print_run_usage(FILE *fp)
2280{
2281 fprintf(fp, "Usage: babeltrace [GENERAL OPTIONS] run [OPTIONS]\n");
2282 fprintf(fp, "\n");
2283 fprintf(fp, "Options:\n");
2284 fprintf(fp, "\n");
2285 fprintf(fp, " -b, --base-params=PARAMS Set PARAMS as the current base parameters\n");
2286 fprintf(fp, " for all the following components until\n");
2287 fprintf(fp, " --reset-base-params is encountered\n");
2288 fprintf(fp, " (see the expected format of PARAMS below)\n");
fd5f8053
PP
2289 fprintf(fp, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2290 fprintf(fp, " Instantiate the component class CLS of type\n");
2291 fprintf(fp, " TYPE (`source`, `filter`, or `sink`) found\n");
2292 fprintf(fp, " in the plugin PLUGIN, add it to the graph,\n");
2293 fprintf(fp, " and optionally name it NAME (you can also\n");
2294 fprintf(fp, " specify the name with --name)\n");
d5128b09 2295 fprintf(fp, " -x, --connect=CONNECTION Connect two created components (see the\n");
9009cc24 2296 fprintf(fp, " expected format of CONNECTION below)\n");
9009cc24
PP
2297 fprintf(fp, " --key=KEY Set the current initialization string\n");
2298 fprintf(fp, " parameter key to KEY (see --value)\n");
2299 fprintf(fp, " -n, --name=NAME Set the name of the current component\n");
2300 fprintf(fp, " to NAME (must be unique amongst all the\n");
2301 fprintf(fp, " names of the created components)\n");
2302 fprintf(fp, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2303 fprintf(fp, " (~/.local/lib/babeltrace/plugins)\n");
2304 fprintf(fp, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2305 fprintf(fp, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2306 fprintf(fp, " current component (see the expected format\n");
2307 fprintf(fp, " of PARAMS below)\n");
2308 fprintf(fp, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2309 fprintf(fp, " dynamic plugins can be loaded\n");
2310 fprintf(fp, " -r, --reset-base-params Reset the current base parameters to an\n");
2311 fprintf(fp, " empty map\n");
2312 fprintf(fp, " --retry-duration=DUR When babeltrace(1) needs to retry to run\n");
2313 fprintf(fp, " the graph later, retry in DUR µs\n");
2314 fprintf(fp, " (default: 100000)\n");
9009cc24
PP
2315 fprintf(fp, " --value=VAL Add a string initialization parameter to\n");
2316 fprintf(fp, " the current component with a name given by\n");
2317 fprintf(fp, " the last argument of the --key option and a\n");
2318 fprintf(fp, " value set to VAL\n");
9e503aa9 2319 fprintf(fp, " -h, --help Show this help and quit\n");
9009cc24
PP
2320 fprintf(fp, "\n");
2321 fprintf(fp, "See `babeltrace --help` for the list of general options.\n");
2322 fprintf(fp, "\n\n");
2323 fprintf(fp, "Expected format of CONNECTION\n");
2324 fprintf(fp, "-----------------------------\n");
2325 fprintf(fp, "\n");
2326 fprintf(fp, " UPSTREAM[.UPSTREAM-PORT]:DOWNSTREAM[.DOWNSTREAM-PORT]\n");
2327 fprintf(fp, "\n");
2328 fprintf(fp, "UPSTREAM and DOWNSTREAM are names of the upstream and downstream\n");
2329 fprintf(fp, "components to connect together. You must escape the following characters\n\n");
2330 fprintf(fp, "with `\\`: `\\`, `.`, and `:`. You can set the name of the current\n");
2331 fprintf(fp, "component with the --name option.\n");
2332 fprintf(fp, "\n");
2333 fprintf(fp, "UPSTREAM-PORT and DOWNSTREAM-PORT are optional globbing patterns to\n");
2334 fprintf(fp, "identify the upstream and downstream ports to use for the connection.\n");
2335 fprintf(fp, "When the port is not specified, `*` is used.\n");
2336 fprintf(fp, "\n");
2337 fprintf(fp, "When a component named UPSTREAM has an available port which matches the\n");
2338 fprintf(fp, "UPSTREAM-PORT globbing pattern, it is connected to the first port which\n");
2339 fprintf(fp, "matches the DOWNSTREAM-PORT globbing pattern of the component named\n");
2340 fprintf(fp, "DOWNSTREAM.\n");
2341 fprintf(fp, "\n");
2342 fprintf(fp, "The only special character in UPSTREAM-PORT and DOWNSTREAM-PORT is `*`\n");
2343 fprintf(fp, "which matches anything. You must escape the following characters\n");
2344 fprintf(fp, "with `\\`: `\\`, `*`, `?`, `[`, `.`, and `:`.\n");
2345 fprintf(fp, "\n");
2346 fprintf(fp, "You can connect a source component to a filter or sink component. You\n");
2347 fprintf(fp, "can connect a filter component to a sink component.\n");
2348 fprintf(fp, "\n");
2349 fprintf(fp, "Examples:\n");
2350 fprintf(fp, "\n");
2351 fprintf(fp, " my-src:my-sink\n");
2352 fprintf(fp, " ctf-fs.*stream*:utils-muxer:*\n");
2353 fprintf(fp, "\n");
2354 fprintf(fp, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
2355 fprintf(fp, "babeltrace from a shell.\n");
2356 fprintf(fp, "\n\n");
2357 print_expected_params_format(fp);
2358}
2359
2360/*
2361 * Creates a Babeltrace config object from the arguments of a run
2362 * command.
2363 *
2364 * *retcode is set to the appropriate exit code to use.
2365 */
2366static
2367struct bt_config *bt_config_run_from_args(int argc, const char *argv[],
2368 int *retcode, bool force_omit_system_plugin_path,
2369 bool force_omit_home_plugin_path,
8eee8ea2 2370 const bt_value *initial_plugin_paths)
9009cc24
PP
2371{
2372 poptContext pc = NULL;
2373 char *arg = NULL;
2374 struct bt_config_component *cur_cfg_comp = NULL;
0a011c88
JG
2375 enum bt_config_component_dest cur_cfg_comp_dest =
2376 BT_CONFIG_COMPONENT_DEST_UNKNOWN;
8eee8ea2 2377 bt_value *cur_base_params = NULL;
9009cc24
PP
2378 int opt, ret = 0;
2379 struct bt_config *cfg = NULL;
8eee8ea2
PP
2380 bt_value *instance_names = NULL;
2381 bt_value *connection_args = NULL;
9009cc24
PP
2382 GString *cur_param_key = NULL;
2383 char error_buf[256] = { 0 };
738302b8 2384 long retry_duration = -1;
ee78f405 2385 bt_value_status status;
9009cc24
PP
2386 struct poptOption run_long_options[] = {
2387 { "base-params", 'b', POPT_ARG_STRING, NULL, OPT_BASE_PARAMS, NULL, NULL },
fd5f8053 2388 { "component", 'c', POPT_ARG_STRING, NULL, OPT_COMPONENT, NULL, NULL },
d5128b09 2389 { "connect", 'x', POPT_ARG_STRING, NULL, OPT_CONNECT, NULL, NULL },
9009cc24
PP
2390 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
2391 { "key", '\0', POPT_ARG_STRING, NULL, OPT_KEY, NULL, NULL },
2392 { "name", 'n', POPT_ARG_STRING, NULL, OPT_NAME, NULL, NULL },
2393 { "omit-home-plugin-path", '\0', POPT_ARG_NONE, NULL, OPT_OMIT_HOME_PLUGIN_PATH, NULL, NULL },
2394 { "omit-system-plugin-path", '\0', POPT_ARG_NONE, NULL, OPT_OMIT_SYSTEM_PLUGIN_PATH, NULL, NULL },
2395 { "params", 'p', POPT_ARG_STRING, NULL, OPT_PARAMS, NULL, NULL },
2396 { "plugin-path", '\0', POPT_ARG_STRING, NULL, OPT_PLUGIN_PATH, NULL, NULL },
2397 { "reset-base-params", 'r', POPT_ARG_NONE, NULL, OPT_RESET_BASE_PARAMS, NULL, NULL },
738302b8 2398 { "retry-duration", '\0', POPT_ARG_LONG, &retry_duration, OPT_RETRY_DURATION, NULL, NULL },
9009cc24
PP
2399 { "value", '\0', POPT_ARG_STRING, NULL, OPT_VALUE, NULL, NULL },
2400 { NULL, 0, '\0', NULL, 0, NULL, NULL },
2401 };
2402
2403 *retcode = 0;
2404 cur_param_key = g_string_new(NULL);
2405 if (!cur_param_key) {
2406 print_err_oom();
2407 goto error;
2408 }
2409
2410 if (argc <= 1) {
2411 print_run_usage(stdout);
2412 *retcode = -1;
2413 goto end;
2414 }
2415
2416 cfg = bt_config_run_create(initial_plugin_paths);
2417 if (!cfg) {
2418 goto error;
2419 }
2420
2421 cfg->cmd_data.run.retry_duration_us = 100000;
2422 cfg->omit_system_plugin_path = force_omit_system_plugin_path;
2423 cfg->omit_home_plugin_path = force_omit_home_plugin_path;
ce141536 2424 cur_base_params = bt_value_map_create();
9009cc24
PP
2425 if (!cur_base_params) {
2426 print_err_oom();
2427 goto error;
2428 }
2429
ce141536 2430 instance_names = bt_value_map_create();
9009cc24
PP
2431 if (!instance_names) {
2432 print_err_oom();
2433 goto error;
2434 }
2435
ce141536 2436 connection_args = bt_value_array_create();
9009cc24
PP
2437 if (!connection_args) {
2438 print_err_oom();
2439 goto error;
2440 }
2441
2442 ret = append_env_var_plugin_paths(cfg->plugin_paths);
2443 if (ret) {
2444 goto error;
2445 }
2446
2447 /* Parse options */
2448 pc = poptGetContext(NULL, argc, (const char **) argv,
2449 run_long_options, 0);
2450 if (!pc) {
2451 printf_err("Cannot get popt context\n");
2452 goto error;
2453 }
2454
2455 poptReadDefaultConfig(pc, 0);
2456
2457 while ((opt = poptGetNextOpt(pc)) > 0) {
2458 arg = poptGetOptArg(pc);
2459
2460 switch (opt) {
2461 case OPT_PLUGIN_PATH:
2462 if (bt_config_append_plugin_paths_check_setuid_setgid(
2463 cfg->plugin_paths, arg)) {
2464 goto error;
2465 }
2466 break;
2467 case OPT_OMIT_SYSTEM_PLUGIN_PATH:
2468 cfg->omit_system_plugin_path = true;
2469 break;
2470 case OPT_OMIT_HOME_PLUGIN_PATH:
2471 cfg->omit_home_plugin_path = true;
2472 break;
fd5f8053 2473 case OPT_COMPONENT:
9009cc24 2474 {
9009cc24 2475 enum bt_config_component_dest new_dest;
9009cc24
PP
2476
2477 if (cur_cfg_comp) {
2478 ret = add_run_cfg_comp_check_name(cfg,
2479 cur_cfg_comp, cur_cfg_comp_dest,
2480 instance_names);
8138bfe1 2481 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp);
9009cc24
PP
2482 if (ret) {
2483 goto error;
2484 }
2485 }
2486
fd5f8053 2487 cur_cfg_comp = bt_config_component_from_arg(arg);
9009cc24 2488 if (!cur_cfg_comp) {
fd5f8053
PP
2489 printf_err("Invalid format for --component option's argument:\n %s\n",
2490 arg);
9009cc24
PP
2491 goto error;
2492 }
2493
fd5f8053
PP
2494 switch (cur_cfg_comp->type) {
2495 case BT_COMPONENT_CLASS_TYPE_SOURCE:
2496 new_dest = BT_CONFIG_COMPONENT_DEST_SOURCE;
2497 break;
2498 case BT_COMPONENT_CLASS_TYPE_FILTER:
2499 new_dest = BT_CONFIG_COMPONENT_DEST_FILTER;
2500 break;
2501 case BT_COMPONENT_CLASS_TYPE_SINK:
2502 new_dest = BT_CONFIG_COMPONENT_DEST_SINK;
2503 break;
2504 default:
2505 abort();
2506 }
2507
8b45963b 2508 BT_ASSERT(cur_base_params);
8c6884d9 2509 bt_value_put_ref(cur_cfg_comp->params);
6284461f
PP
2510 status = bt_value_copy(cur_base_params,
2511 &cur_cfg_comp->params);
b5cdc106 2512 if (status != BT_VALUE_STATUS_OK) {
9009cc24
PP
2513 print_err_oom();
2514 goto error;
2515 }
2516
2517 cur_cfg_comp_dest = new_dest;
2518 break;
2519 }
2520 case OPT_PARAMS:
2521 {
8eee8ea2
PP
2522 bt_value *params;
2523 bt_value *params_to_set;
9009cc24
PP
2524
2525 if (!cur_cfg_comp) {
2526 printf_err("Cannot add parameters to unavailable component:\n %s\n",
2527 arg);
2528 goto error;
2529 }
2530
ce141536 2531 params = bt_value_from_arg(arg);
9009cc24
PP
2532 if (!params) {
2533 printf_err("Invalid format for --params option's argument:\n %s\n",
2534 arg);
2535 goto error;
2536 }
2537
7be9d1d3
PP
2538 status = bt_value_map_extend(cur_cfg_comp->params,
2539 params, &params_to_set);
8c6884d9 2540 BT_VALUE_PUT_REF_AND_RESET(params);
b5cdc106 2541 if (status != BT_VALUE_STATUS_OK) {
9009cc24
PP
2542 printf_err("Cannot extend current component parameters with --params option's argument:\n %s\n",
2543 arg);
2544 goto error;
2545 }
2546
8138bfe1 2547 BT_OBJECT_MOVE_REF(cur_cfg_comp->params, params_to_set);
9009cc24
PP
2548 break;
2549 }
2550 case OPT_KEY:
2551 if (strlen(arg) == 0) {
2552 printf_err("Cannot set an empty string as the current parameter key\n");
2553 goto error;
2554 }
2555
2556 g_string_assign(cur_param_key, arg);
2557 break;
2558 case OPT_VALUE:
2559 if (!cur_cfg_comp) {
2560 printf_err("Cannot set a parameter's value of unavailable component:\n %s\n",
2561 arg);
2562 goto error;
2563 }
2564
2565 if (cur_param_key->len == 0) {
2566 printf_err("--value option specified without preceding --key option:\n %s\n",
2567 arg);
2568 goto error;
2569 }
2570
ce141536 2571 if (bt_value_map_insert_string_entry(cur_cfg_comp->params,
9009cc24
PP
2572 cur_param_key->str, arg)) {
2573 print_err_oom();
2574 goto error;
2575 }
2576 break;
2577 case OPT_NAME:
2578 if (!cur_cfg_comp) {
2579 printf_err("Cannot set the name of unavailable component:\n %s\n",
2580 arg);
2581 goto error;
2582 }
2583
2584 g_string_assign(cur_cfg_comp->instance_name, arg);
2585 break;
2586 case OPT_BASE_PARAMS:
2587 {
8eee8ea2 2588 bt_value *params =
ce141536 2589 bt_value_from_arg(arg);
9009cc24
PP
2590
2591 if (!params) {
2592 printf_err("Invalid format for --base-params option's argument:\n %s\n",
2593 arg);
2594 goto error;
2595 }
2596
8138bfe1 2597 BT_OBJECT_MOVE_REF(cur_base_params, params);
9009cc24
PP
2598 break;
2599 }
2600 case OPT_RESET_BASE_PARAMS:
8c6884d9 2601 BT_VALUE_PUT_REF_AND_RESET(cur_base_params);
ce141536 2602 cur_base_params = bt_value_map_create();
9009cc24
PP
2603 if (!cur_base_params) {
2604 print_err_oom();
2605 goto error;
2606 }
2607 break;
2608 case OPT_CONNECT:
ce141536 2609 if (bt_value_array_append_string_element(
44514773 2610 connection_args, arg)) {
9009cc24
PP
2611 print_err_oom();
2612 goto error;
2613 }
2614 break;
2615 case OPT_RETRY_DURATION:
2616 if (retry_duration < 0) {
b25dc5ae 2617 printf_err("--retry-duration option's argument must be positive or 0: %ld\n",
9009cc24
PP
2618 retry_duration);
2619 goto error;
2620 }
2621
2622 cfg->cmd_data.run.retry_duration_us =
2623 (uint64_t) retry_duration;
2624 break;
2625 case OPT_HELP:
2626 print_run_usage(stdout);
2627 *retcode = -1;
8138bfe1 2628 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
2629 goto end;
2630 default:
2631 printf_err("Unknown command-line option specified (option code %d)\n",
2632 opt);
2633 goto error;
2634 }
2635
2636 free(arg);
2637 arg = NULL;
2638 }
2639
2640 /* Check for option parsing error */
2641 if (opt < -1) {
2642 printf_err("While parsing command-line options, at option %s: %s\n",
2643 poptBadOption(pc, 0), poptStrerror(opt));
2644 goto error;
2645 }
2646
2647 /* This command does not accept leftover arguments */
2648 if (poptPeekArg(pc)) {
2649 printf_err("Unexpected argument: %s\n", poptPeekArg(pc));
2650 goto error;
2651 }
2652
2653 /* Add current component */
2654 if (cur_cfg_comp) {
2655 ret = add_run_cfg_comp_check_name(cfg, cur_cfg_comp,
2656 cur_cfg_comp_dest, instance_names);
8138bfe1 2657 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp);
9009cc24
PP
2658 if (ret) {
2659 goto error;
2660 }
2661 }
2662
2663 if (cfg->cmd_data.run.sources->len == 0) {
2664 printf_err("Incomplete graph: no source component\n");
2665 goto error;
2666 }
2667
2668 if (cfg->cmd_data.run.sinks->len == 0) {
2669 printf_err("Incomplete graph: no sink component\n");
2670 goto error;
2671 }
2672
2673 if (append_home_and_system_plugin_paths_cfg(cfg)) {
2674 goto error;
2675 }
2676
17582c6d 2677 ret = bt_config_cli_args_create_connections(cfg,
ce141536 2678 connection_args,
9009cc24
PP
2679 error_buf, 256);
2680 if (ret) {
2681 printf_err("Cannot creation connections:\n%s", error_buf);
2682 goto error;
2683 }
2684
2685 goto end;
2686
2687error:
2688 *retcode = 1;
8138bfe1 2689 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
2690
2691end:
2692 if (pc) {
2693 poptFreeContext(pc);
2694 }
2695
2696 if (cur_param_key) {
2697 g_string_free(cur_param_key, TRUE);
2698 }
2699
2700 free(arg);
8138bfe1 2701 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp);
8c6884d9
PP
2702 BT_VALUE_PUT_REF_AND_RESET(cur_base_params);
2703 BT_VALUE_PUT_REF_AND_RESET(instance_names);
2704 BT_VALUE_PUT_REF_AND_RESET(connection_args);
9009cc24
PP
2705 return cfg;
2706}
2707
2708static
8eee8ea2 2709struct bt_config *bt_config_run_from_args_array(const bt_value *run_args,
9009cc24
PP
2710 int *retcode, bool force_omit_system_plugin_path,
2711 bool force_omit_home_plugin_path,
8eee8ea2 2712 const bt_value *initial_plugin_paths)
9009cc24
PP
2713{
2714 struct bt_config *cfg = NULL;
2715 const char **argv;
0d3e053b 2716 int64_t i, len;
44514773 2717 const size_t argc = bt_value_array_get_size(run_args) + 1;
9009cc24
PP
2718
2719 argv = calloc(argc, sizeof(*argv));
2720 if (!argv) {
2721 print_err_oom();
2722 goto end;
2723 }
2724
2725 argv[0] = "run";
2726
44514773 2727 len = bt_value_array_get_size(run_args);
0d3e053b
MD
2728 if (len < 0) {
2729 printf_err("Invalid executable arguments\n");
2730 goto end;
2731 }
2732 for (i = 0; i < len; i++) {
8eee8ea2 2733 const bt_value *arg_value =
ce141536
PP
2734 bt_value_array_borrow_element_by_index_const(run_args,
2735 i);
9009cc24
PP
2736 const char *arg;
2737
8b45963b 2738 BT_ASSERT(arg_value);
b5cdc106 2739 arg = bt_value_string_get(arg_value);
8b45963b 2740 BT_ASSERT(arg);
9009cc24 2741 argv[i + 1] = arg;
9009cc24
PP
2742 }
2743
2744 cfg = bt_config_run_from_args(argc, argv, retcode,
2745 force_omit_system_plugin_path, force_omit_home_plugin_path,
2746 initial_plugin_paths);
2747
2748end:
2749 free(argv);
2750 return cfg;
2751}
2752
2753/*
2754 * Prints the convert command usage.
2755 */
2756static
2757void print_convert_usage(FILE *fp)
2758{
2759 fprintf(fp, "Usage: babeltrace [GENERAL OPTIONS] [convert] [OPTIONS] [PATH/URL]\n");
2760 fprintf(fp, "\n");
2761 fprintf(fp, "Options:\n");
2762 fprintf(fp, "\n");
fd5f8053
PP
2763 fprintf(fp, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2764 fprintf(fp, " Instantiate the component class CLS of type\n");
2765 fprintf(fp, " TYPE (`source`, `filter`, or `sink`) found\n");
2766 fprintf(fp, " in the plugin PLUGIN, add it to the\n");
2767 fprintf(fp, " conversion graph, and optionally name it\n");
2768 fprintf(fp, " NAME (you can also specify the name with\n");
2769 fprintf(fp, " --name)\n");
9009cc24
PP
2770 fprintf(fp, " --name=NAME Set the name of the current component\n");
2771 fprintf(fp, " to NAME (must be unique amongst all the\n");
2772 fprintf(fp, " names of the created components)\n");
2773 fprintf(fp, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2774 fprintf(fp, " (~/.local/lib/babeltrace/plugins)\n");
2775 fprintf(fp, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2776 fprintf(fp, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2777 fprintf(fp, " current component (see the expected format\n");
2778 fprintf(fp, " of PARAMS below)\n");
2779 fprintf(fp, " -P, --path=PATH Set the `path` string parameter of the\n");
2780 fprintf(fp, " current component to PATH\n");
2781 fprintf(fp, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2782 fprintf(fp, " --retry-duration=DUR When babeltrace(1) needs to retry to run\n");
2783 fprintf(fp, " the graph later, retry in DUR µs\n");
2784 fprintf(fp, " (default: 100000)\n");
2785 fprintf(fp, " dynamic plugins can be loaded\n");
2786 fprintf(fp, " --run-args Print the equivalent arguments for the\n");
2787 fprintf(fp, " `run` command to the standard output,\n");
2788 fprintf(fp, " formatted for a shell, and quit\n");
2789 fprintf(fp, " --run-args-0 Print the equivalent arguments for the\n");
2790 fprintf(fp, " `run` command to the standard output,\n");
2791 fprintf(fp, " formatted for `xargs -0`, and quit\n");
8320cc93
PP
2792 fprintf(fp, " --stream-intersection Only process events when all streams\n");
2793 fprintf(fp, " are active\n");
9009cc24
PP
2794 fprintf(fp, " -u, --url=URL Set the `url` string parameter of the\n");
2795 fprintf(fp, " current component to URL\n");
9e503aa9 2796 fprintf(fp, " -h, --help Show this help and quit\n");
9009cc24 2797 fprintf(fp, "\n");
e7ad156c 2798 fprintf(fp, "Implicit `source.ctf.fs` component options:\n");
9009cc24
PP
2799 fprintf(fp, "\n");
2800 fprintf(fp, " --clock-offset=SEC Set clock offset to SEC seconds\n");
2801 fprintf(fp, " --clock-offset-ns=NS Set clock offset to NS ns\n");
9009cc24 2802 fprintf(fp, "\n");
e7ad156c 2803 fprintf(fp, "Implicit `sink.text.pretty` component options:\n");
9009cc24
PP
2804 fprintf(fp, "\n");
2805 fprintf(fp, " --clock-cycles Print timestamps in clock cycles\n");
2806 fprintf(fp, " --clock-date Print timestamp dates\n");
2807 fprintf(fp, " --clock-gmt Print and parse timestamps in the GMT\n");
2808 fprintf(fp, " time zone instead of the local time zone\n");
2809 fprintf(fp, " --clock-seconds Print the timestamps as `SEC.NS` instead\n");
2810 fprintf(fp, " of `hh:mm:ss.nnnnnnnnn`\n");
2811 fprintf(fp, " --color=(never | auto | always)\n");
2812 fprintf(fp, " Never, automatically, or always emit\n");
2813 fprintf(fp, " console color codes\n");
2814 fprintf(fp, " -f, --fields=FIELD[,FIELD]... Print additional fields; FIELD can be:\n");
2815 fprintf(fp, " `all`, `trace`, `trace:hostname`,\n");
2816 fprintf(fp, " `trace:domain`, `trace:procname`,\n");
2817 fprintf(fp, " `trace:vpid`, `loglevel`, `emf`\n");
2818 fprintf(fp, " -n, --names=NAME[,NAME]... Print field names; NAME can be:\n");
2819 fprintf(fp, " `payload` (or `arg` or `args`), `none`,\n");
2820 fprintf(fp, " `all`, `scope`, `header`, `context`\n");
2821 fprintf(fp, " (or `ctx`)\n");
2822 fprintf(fp, " --no-delta Do not print time delta between\n");
2823 fprintf(fp, " consecutive events\n");
2824 fprintf(fp, " -w, --output=PATH Write output text to PATH instead of\n");
2825 fprintf(fp, " the standard output\n");
2826 fprintf(fp, "\n");
e7ad156c 2827 fprintf(fp, "Implicit `filter.utils.muxer` component options:\n");
9009cc24
PP
2828 fprintf(fp, "\n");
2829 fprintf(fp, " --clock-force-correlate Assume that clocks are inherently\n");
2830 fprintf(fp, " correlated across traces\n");
2831 fprintf(fp, "\n");
e7ad156c 2832 fprintf(fp, "Implicit `filter.utils.trimmer` component options:\n");
9009cc24
PP
2833 fprintf(fp, "\n");
2834 fprintf(fp, " -b, --begin=BEGIN Set the beginning time of the conversion\n");
2835 fprintf(fp, " time range to BEGIN (see the format of\n");
2836 fprintf(fp, " BEGIN below)\n");
2837 fprintf(fp, " -e, --end=END Set the end time of the conversion time\n");
2838 fprintf(fp, " range to END (see the format of END below)\n");
2839 fprintf(fp, " -t, --timerange=TIMERANGE Set conversion time range to TIMERANGE:\n");
2840 fprintf(fp, " BEGIN,END or [BEGIN,END] (literally `[` and\n");
2841 fprintf(fp, " `]`) (see the format of BEGIN/END below)\n");
2842 fprintf(fp, "\n");
e7ad156c 2843 fprintf(fp, "Implicit `filter.lttng-utils.debug-info` component options:\n");
9009cc24 2844 fprintf(fp, "\n");
8e765000
PP
2845 fprintf(fp, " --debug-info Create an implicit\n");
2846 fprintf(fp, " `filter.lttng-utils.debug-info` component\n");
9009cc24
PP
2847 fprintf(fp, " --debug-info-dir=DIR Search for debug info in directory DIR\n");
2848 fprintf(fp, " instead of `/usr/lib/debug`\n");
2849 fprintf(fp, " --debug-info-full-path Show full debug info source and\n");
2850 fprintf(fp, " binary paths instead of just names\n");
2851 fprintf(fp, " --debug-info-target-prefix=DIR\n");
2852 fprintf(fp, " Use directory DIR as a prefix when\n");
2853 fprintf(fp, " looking up executables during debug\n");
2854 fprintf(fp, " info analysis\n");
9009cc24
PP
2855 fprintf(fp, "\n");
2856 fprintf(fp, "Legacy options that still work:\n");
2857 fprintf(fp, "\n");
2858 fprintf(fp, " -i, --input-format=(ctf | lttng-live)\n");
2859 fprintf(fp, " `ctf`:\n");
fd5f8053 2860 fprintf(fp, " Create an implicit `source.ctf.fs`\n");
9009cc24
PP
2861 fprintf(fp, " component\n");
2862 fprintf(fp, " `lttng-live`:\n");
fd5f8053
PP
2863 fprintf(fp, " Create an implicit `source.ctf.lttng-live`\n");
2864 fprintf(fp, " component\n");
e7ad156c 2865 fprintf(fp, " -o, --output-format=(text | ctf | dummy | ctf-metadata)\n");
9009cc24 2866 fprintf(fp, " `text`:\n");
fd5f8053 2867 fprintf(fp, " Create an implicit `sink.text.pretty`\n");
9009cc24 2868 fprintf(fp, " component\n");
8f4f40e7 2869 fprintf(fp, " `ctf`:\n");
e7ad156c
PP
2870 fprintf(fp, " Create an implicit `sink.ctf.fs`\n");
2871 fprintf(fp, " component\n");
9009cc24 2872 fprintf(fp, " `dummy`:\n");
fd5f8053 2873 fprintf(fp, " Create an implicit `sink.utils.dummy`\n");
9009cc24
PP
2874 fprintf(fp, " component\n");
2875 fprintf(fp, " `ctf-metadata`:\n");
fd5f8053
PP
2876 fprintf(fp, " Query the `source.ctf.fs` component class\n");
2877 fprintf(fp, " for metadata text and quit\n");
9009cc24
PP
2878 fprintf(fp, "\n");
2879 fprintf(fp, "See `babeltrace --help` for the list of general options.\n");
2880 fprintf(fp, "\n\n");
2881 fprintf(fp, "Format of BEGIN and END\n");
2882 fprintf(fp, "-----------------------\n");
2883 fprintf(fp, "\n");
2884 fprintf(fp, " [YYYY-MM-DD [hh:mm:]]ss[.nnnnnnnnn]\n");
2885 fprintf(fp, "\n\n");
2886 print_expected_params_format(fp);
2887}
2888
2889static
2890struct poptOption convert_long_options[] = {
2891 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
2892 { "begin", 'b', POPT_ARG_STRING, NULL, OPT_BEGIN, NULL, NULL },
2893 { "clock-cycles", '\0', POPT_ARG_NONE, NULL, OPT_CLOCK_CYCLES, NULL, NULL },
2894 { "clock-date", '\0', POPT_ARG_NONE, NULL, OPT_CLOCK_DATE, NULL, NULL },
2895 { "clock-force-correlate", '\0', POPT_ARG_NONE, NULL, OPT_CLOCK_FORCE_CORRELATE, NULL, NULL },
2896 { "clock-gmt", '\0', POPT_ARG_NONE, NULL, OPT_CLOCK_GMT, NULL, NULL },
2897 { "clock-offset", '\0', POPT_ARG_STRING, NULL, OPT_CLOCK_OFFSET, NULL, NULL },
2898 { "clock-offset-ns", '\0', POPT_ARG_STRING, NULL, OPT_CLOCK_OFFSET_NS, NULL, NULL },
2899 { "clock-seconds", '\0', POPT_ARG_NONE, NULL, OPT_CLOCK_SECONDS, NULL, NULL },
2900 { "color", '\0', POPT_ARG_STRING, NULL, OPT_COLOR, NULL, NULL },
fd5f8053 2901 { "component", 'c', POPT_ARG_STRING, NULL, OPT_COMPONENT, NULL, NULL },
9009cc24
PP
2902 { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL },
2903 { "debug-info-dir", 0, POPT_ARG_STRING, NULL, OPT_DEBUG_INFO_DIR, NULL, NULL },
2904 { "debug-info-full-path", 0, POPT_ARG_NONE, NULL, OPT_DEBUG_INFO_FULL_PATH, NULL, NULL },
2905 { "debug-info-target-prefix", 0, POPT_ARG_STRING, NULL, OPT_DEBUG_INFO_TARGET_PREFIX, NULL, NULL },
2906 { "end", 'e', POPT_ARG_STRING, NULL, OPT_END, NULL, NULL },
2907 { "fields", 'f', POPT_ARG_STRING, NULL, OPT_FIELDS, NULL, NULL },
9009cc24
PP
2908 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
2909 { "input-format", 'i', POPT_ARG_STRING, NULL, OPT_INPUT_FORMAT, NULL, NULL },
2910 { "name", '\0', POPT_ARG_STRING, NULL, OPT_NAME, NULL, NULL },
2911 { "names", 'n', POPT_ARG_STRING, NULL, OPT_NAMES, NULL, NULL },
8e765000 2912 { "debug-info", '\0', POPT_ARG_NONE, NULL, OPT_DEBUG_INFO, NULL, NULL },
9009cc24
PP
2913 { "no-delta", '\0', POPT_ARG_NONE, NULL, OPT_NO_DELTA, NULL, NULL },
2914 { "omit-home-plugin-path", '\0', POPT_ARG_NONE, NULL, OPT_OMIT_HOME_PLUGIN_PATH, NULL, NULL },
2915 { "omit-system-plugin-path", '\0', POPT_ARG_NONE, NULL, OPT_OMIT_SYSTEM_PLUGIN_PATH, NULL, NULL },
2916 { "output", 'w', POPT_ARG_STRING, NULL, OPT_OUTPUT, NULL, NULL },
2917 { "output-format", 'o', POPT_ARG_STRING, NULL, OPT_OUTPUT_FORMAT, NULL, NULL },
2918 { "params", 'p', POPT_ARG_STRING, NULL, OPT_PARAMS, NULL, NULL },
2919 { "path", 'P', POPT_ARG_STRING, NULL, OPT_PATH, NULL, NULL },
2920 { "plugin-path", '\0', POPT_ARG_STRING, NULL, OPT_PLUGIN_PATH, NULL, NULL },
2921 { "retry-duration", '\0', POPT_ARG_STRING, NULL, OPT_RETRY_DURATION, NULL, NULL },
2922 { "run-args", '\0', POPT_ARG_NONE, NULL, OPT_RUN_ARGS, NULL, NULL },
2923 { "run-args-0", '\0', POPT_ARG_NONE, NULL, OPT_RUN_ARGS_0, NULL, NULL },
9009cc24
PP
2924 { "stream-intersection", '\0', POPT_ARG_NONE, NULL, OPT_STREAM_INTERSECTION, NULL, NULL },
2925 { "timerange", '\0', POPT_ARG_STRING, NULL, OPT_TIMERANGE, NULL, NULL },
2926 { "url", 'u', POPT_ARG_STRING, NULL, OPT_URL, NULL, NULL },
2927 { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL },
2928 { NULL, 0, '\0', NULL, 0, NULL, NULL },
2929};
2930
2931static
2932GString *get_component_auto_name(const char *prefix,
8eee8ea2 2933 const bt_value *existing_names)
9009cc24
PP
2934{
2935 unsigned int i = 0;
2936 GString *auto_name = g_string_new(NULL);
2937
2938 if (!auto_name) {
2939 print_err_oom();
2940 goto end;
2941 }
2942
44514773 2943 if (!bt_value_map_has_entry(existing_names, prefix)) {
9009cc24
PP
2944 g_string_assign(auto_name, prefix);
2945 goto end;
2946 }
2947
2948 do {
2949 g_string_printf(auto_name, "%s-%d", prefix, i);
2950 i++;
44514773 2951 } while (bt_value_map_has_entry(existing_names, auto_name->str));
9009cc24
PP
2952
2953end:
2954 return auto_name;
2955}
2956
2957struct implicit_component_args {
2958 bool exists;
fd5f8053 2959 GString *comp_arg;
9009cc24
PP
2960 GString *name_arg;
2961 GString *params_arg;
8eee8ea2 2962 bt_value *extra_params;
9009cc24
PP
2963};
2964
2965static
2966int assign_name_to_implicit_component(struct implicit_component_args *args,
8eee8ea2 2967 const char *prefix, bt_value *existing_names,
9009cc24
PP
2968 GList **comp_names, bool append_to_comp_names)
2969{
2970 int ret = 0;
2971 GString *name = NULL;
2972
2973 if (!args->exists) {
2974 goto end;
2975 }
2976
17582c6d 2977 name = get_component_auto_name(prefix,
ce141536 2978 existing_names);
9009cc24
PP
2979
2980 if (!name) {
2981 ret = -1;
2982 goto end;
2983 }
2984
2985 g_string_assign(args->name_arg, name->str);
2986
ce141536 2987 if (bt_value_map_insert_entry(existing_names, name->str,
9009cc24
PP
2988 bt_value_null)) {
2989 print_err_oom();
2990 ret = -1;
2991 goto end;
2992 }
2993
2994 if (append_to_comp_names) {
2995 *comp_names = g_list_append(*comp_names, name);
2996 name = NULL;
2997 }
2998
2999end:
3000 if (name) {
3001 g_string_free(name, TRUE);
3002 }
3003
3004 return ret;
3005}
3006
3007static
3008int append_run_args_for_implicit_component(
9009cc24 3009 struct implicit_component_args *impl_args,
8eee8ea2 3010 bt_value *run_args)
9009cc24
PP
3011{
3012 int ret = 0;
3013 size_t i;
3014
3015 if (!impl_args->exists) {
3016 goto end;
3017 }
3018
ce141536 3019 if (bt_value_array_append_string_element(run_args, "--component")) {
fd5f8053
PP
3020 print_err_oom();
3021 goto error;
9009cc24
PP
3022 }
3023
ce141536 3024 if (bt_value_array_append_string_element(run_args, impl_args->comp_arg->str)) {
9009cc24
PP
3025 print_err_oom();
3026 goto error;
3027 }
3028
ce141536 3029 if (bt_value_array_append_string_element(run_args, "--name")) {
9009cc24
PP
3030 print_err_oom();
3031 goto error;
3032 }
3033
ce141536 3034 if (bt_value_array_append_string_element(run_args, impl_args->name_arg->str)) {
9009cc24
PP
3035 print_err_oom();
3036 goto error;
3037 }
3038
3039 if (impl_args->params_arg->len > 0) {
ce141536 3040 if (bt_value_array_append_string_element(run_args, "--params")) {
9009cc24
PP
3041 print_err_oom();
3042 goto error;
3043 }
3044
ce141536 3045 if (bt_value_array_append_string_element(run_args,
9009cc24
PP
3046 impl_args->params_arg->str)) {
3047 print_err_oom();
3048 goto error;
3049 }
3050 }
3051
ce141536 3052 for (i = 0; i < bt_value_array_get_size(impl_args->extra_params);
17582c6d 3053 i++) {
8eee8ea2 3054 const bt_value *elem;
9009cc24
PP
3055 const char *arg;
3056
ce141536
PP
3057 elem = bt_value_array_borrow_element_by_index(impl_args->extra_params,
3058 i);
9009cc24
PP
3059 if (!elem) {
3060 goto error;
3061 }
3062
8b45963b 3063 BT_ASSERT(bt_value_is_string(elem));
b5cdc106 3064 arg = bt_value_string_get(elem);
ce141536 3065 ret = bt_value_array_append_string_element(run_args, arg);
9009cc24
PP
3066 if (ret) {
3067 print_err_oom();
3068 goto error;
3069 }
3070 }
3071
3072 goto end;
3073
3074error:
3075 ret = -1;
3076
3077end:
3078 return ret;
3079}
3080
3081static
94023a1c 3082void finalize_implicit_component_args(struct implicit_component_args *args)
9009cc24 3083{
8b45963b 3084 BT_ASSERT(args);
9009cc24 3085
fd5f8053
PP
3086 if (args->comp_arg) {
3087 g_string_free(args->comp_arg, TRUE);
9009cc24
PP
3088 }
3089
3090 if (args->name_arg) {
3091 g_string_free(args->name_arg, TRUE);
3092 }
3093
3094 if (args->params_arg) {
3095 g_string_free(args->params_arg, TRUE);
3096 }
3097
8c6884d9 3098 bt_value_put_ref(args->extra_params);
9009cc24
PP
3099}
3100
94023a1c
PP
3101static
3102void destroy_implicit_component_args(void *args)
3103{
3104 if (!args) {
3105 return;
3106 }
3107
3108 finalize_implicit_component_args(args);
3109 g_free(args);
3110}
3111
9009cc24
PP
3112static
3113int init_implicit_component_args(struct implicit_component_args *args,
fd5f8053 3114 const char *comp_arg, bool exists)
9009cc24
PP
3115{
3116 int ret = 0;
3117
3118 args->exists = exists;
fd5f8053 3119 args->comp_arg = g_string_new(comp_arg);
9009cc24
PP
3120 args->name_arg = g_string_new(NULL);
3121 args->params_arg = g_string_new(NULL);
ce141536 3122 args->extra_params = bt_value_array_create();
9009cc24 3123
fd5f8053 3124 if (!args->comp_arg || !args->name_arg ||
e7ad156c 3125 !args->params_arg || !args->extra_params) {
9009cc24 3126 ret = -1;
94023a1c 3127 finalize_implicit_component_args(args);
9009cc24
PP
3128 print_err_oom();
3129 goto end;
3130 }
3131
3132end:
3133 return ret;
3134}
3135
3136static
3137void append_implicit_component_param(struct implicit_component_args *args,
3138 const char *key, const char *value)
3139{
8b45963b
PP
3140 BT_ASSERT(args);
3141 BT_ASSERT(key);
3142 BT_ASSERT(value);
9009cc24
PP
3143 append_param_arg(args->params_arg, key, value);
3144}
3145
3146static
e7ad156c 3147int append_implicit_component_extra_param(struct implicit_component_args *args,
9009cc24
PP
3148 const char *key, const char *value)
3149{
3150 int ret = 0;
3151
8b45963b
PP
3152 BT_ASSERT(args);
3153 BT_ASSERT(key);
3154 BT_ASSERT(value);
9009cc24 3155
ce141536 3156 if (bt_value_array_append_string_element(args->extra_params, "--key")) {
9009cc24
PP
3157 print_err_oom();
3158 ret = -1;
3159 goto end;
3160 }
3161
ce141536 3162 if (bt_value_array_append_string_element(args->extra_params, key)) {
9009cc24
PP
3163 print_err_oom();
3164 ret = -1;
3165 goto end;
3166 }
3167
ce141536 3168 if (bt_value_array_append_string_element(args->extra_params, "--value")) {
9009cc24
PP
3169 print_err_oom();
3170 ret = -1;
3171 goto end;
3172 }
3173
ce141536 3174 if (bt_value_array_append_string_element(args->extra_params, value)) {
9009cc24
PP
3175 print_err_oom();
3176 ret = -1;
3177 goto end;
3178 }
3179
3180end:
3181 return ret;
3182}
3183
3184static
3185int convert_append_name_param(enum bt_config_component_dest dest,
3186 GString *cur_name, GString *cur_name_prefix,
8eee8ea2
PP
3187 bt_value *run_args,
3188 bt_value *all_names,
9009cc24
PP
3189 GList **source_names, GList **filter_names,
3190 GList **sink_names)
3191{
3192 int ret = 0;
3193
3194 if (cur_name_prefix->len > 0) {
fd5f8053 3195 /* We're after a --component option */
9009cc24
PP
3196 GString *name = NULL;
3197 bool append_name_opt = false;
3198
3199 if (cur_name->len == 0) {
3200 /*
3201 * No explicit name was provided for the user
3202 * component.
3203 */
fd5f8053 3204 name = get_component_auto_name(cur_name_prefix->str,
ce141536 3205 all_names);
9009cc24
PP
3206 append_name_opt = true;
3207 } else {
3208 /*
3209 * An explicit name was provided for the user
3210 * component.
3211 */
ce141536
PP
3212 if (bt_value_map_has_entry(all_names,
3213 cur_name->str)) {
9009cc24
PP
3214 printf_err("Duplicate component instance name:\n %s\n",
3215 cur_name->str);
3216 goto error;
3217 }
3218
3219 name = g_string_new(cur_name->str);
3220 }
3221
3222 if (!name) {
3223 print_err_oom();
3224 goto error;
3225 }
3226
3227 /*
3228 * Remember this name globally, for the uniqueness of
3229 * all component names.
3230 */
ce141536 3231 if (bt_value_map_insert_entry(all_names, name->str, bt_value_null)) {
9009cc24
PP
3232 print_err_oom();
3233 goto error;
3234 }
3235
3236 /*
3237 * Append the --name option if necessary.
3238 */
3239 if (append_name_opt) {
ce141536 3240 if (bt_value_array_append_string_element(run_args, "--name")) {
9009cc24
PP
3241 print_err_oom();
3242 goto error;
3243 }
3244
ce141536 3245 if (bt_value_array_append_string_element(run_args, name->str)) {
9009cc24
PP
3246 print_err_oom();
3247 goto error;
3248 }
3249 }
3250
3251 /*
3252 * Remember this name specifically for the type of the
3253 * component. This is to create connection arguments.
3254 */
3255 switch (dest) {
3256 case BT_CONFIG_COMPONENT_DEST_SOURCE:
3257 *source_names = g_list_append(*source_names, name);
3258 break;
3259 case BT_CONFIG_COMPONENT_DEST_FILTER:
3260 *filter_names = g_list_append(*filter_names, name);
3261 break;
3262 case BT_CONFIG_COMPONENT_DEST_SINK:
3263 *sink_names = g_list_append(*sink_names, name);
3264 break;
3265 default:
0fbb9a9f 3266 abort();
9009cc24
PP
3267 }
3268
3269 g_string_assign(cur_name_prefix, "");
3270 }
3271
3272 goto end;
3273
3274error:
3275 ret = -1;
3276
3277end:
3278 return ret;
3279}
3280
3281/*
3282 * Escapes `.`, `:`, and `\` of `input` with `\`.
3283 */
3284static
3285GString *escape_dot_colon(const char *input)
3286{
3287 GString *output = g_string_new(NULL);
3288 const char *ch;
3289
3290 if (!output) {
3291 print_err_oom();
3292 goto end;
3293 }
3294
3295 for (ch = input; *ch != '\0'; ch++) {
3296 if (*ch == '\\' || *ch == '.' || *ch == ':') {
3297 g_string_append_c(output, '\\');
3298 }
3299
3300 g_string_append_c(output, *ch);
3301 }
3302
3303end:
3304 return output;
3305}
3306
3307/*
3308 * Appends a --connect option to a list of arguments. `upstream_name`
3309 * and `downstream_name` are escaped with escape_dot_colon() in this
3310 * function.
3311 */
3312static
8eee8ea2 3313int append_connect_arg(bt_value *run_args,
9009cc24
PP
3314 const char *upstream_name, const char *downstream_name)
3315{
3316 int ret = 0;
3317 GString *e_upstream_name = escape_dot_colon(upstream_name);
3318 GString *e_downstream_name = escape_dot_colon(downstream_name);
3319 GString *arg = g_string_new(NULL);
3320
3321 if (!e_upstream_name || !e_downstream_name || !arg) {
3322 print_err_oom();
3323 ret = -1;
3324 goto end;
3325 }
3326
ce141536 3327 ret = bt_value_array_append_string_element(run_args, "--connect");
9009cc24
PP
3328 if (ret) {
3329 print_err_oom();
3330 ret = -1;
3331 goto end;
3332 }
3333
3334 g_string_append(arg, e_upstream_name->str);
3335 g_string_append_c(arg, ':');
3336 g_string_append(arg, e_downstream_name->str);
ce141536 3337 ret = bt_value_array_append_string_element(run_args, arg->str);
9009cc24
PP
3338 if (ret) {
3339 print_err_oom();
3340 ret = -1;
3341 goto end;
3342 }
3343
3344end:
3345 if (arg) {
3346 g_string_free(arg, TRUE);
3347 }
3348
3349 if (e_upstream_name) {
3350 g_string_free(e_upstream_name, TRUE);
3351 }
3352
3353 if (e_downstream_name) {
3354 g_string_free(e_downstream_name, TRUE);
3355 }
3356
3357 return ret;
3358}
3359
3360/*
3361 * Appends the run command's --connect options for the convert command.
3362 */
3363static
8eee8ea2 3364int convert_auto_connect(bt_value *run_args,
9009cc24
PP
3365 GList *source_names, GList *filter_names,
3366 GList *sink_names)
3367{
3368 int ret = 0;
3369 GList *source_at = source_names;
3370 GList *filter_at = filter_names;
3371 GList *filter_prev;
3372 GList *sink_at = sink_names;
3373
8b45963b
PP
3374 BT_ASSERT(source_names);
3375 BT_ASSERT(filter_names);
3376 BT_ASSERT(sink_names);
9009cc24
PP
3377
3378 /* Connect all sources to the first filter */
3379 for (source_at = source_names; source_at != NULL; source_at = g_list_next(source_at)) {
3380 GString *source_name = source_at->data;
3381 GString *filter_name = filter_at->data;
3382
3383 ret = append_connect_arg(run_args, source_name->str,
3384 filter_name->str);
3385 if (ret) {
3386 goto error;
3387 }
3388 }
3389
3390 filter_prev = filter_at;
3391 filter_at = g_list_next(filter_at);
3392
3393 /* Connect remaining filters */
3394 for (; filter_at != NULL; filter_prev = filter_at, filter_at = g_list_next(filter_at)) {
3395 GString *filter_name = filter_at->data;
3396 GString *filter_prev_name = filter_prev->data;
3397
3398 ret = append_connect_arg(run_args, filter_prev_name->str,
3399 filter_name->str);
3400 if (ret) {
3401 goto error;
3402 }
3403 }
3404
3405 /* Connect last filter to all sinks */
3406 for (sink_at = sink_names; sink_at != NULL; sink_at = g_list_next(sink_at)) {
3407 GString *filter_name = filter_prev->data;
3408 GString *sink_name = sink_at->data;
3409
3410 ret = append_connect_arg(run_args, filter_name->str,
3411 sink_name->str);
3412 if (ret) {
3413 goto error;
3414 }
3415 }
3416
3417 goto end;
3418
3419error:
3420 ret = -1;
3421
3422end:
3423 return ret;
3424}
3425
3426static
3427int split_timerange(const char *arg, char **begin, char **end)
3428{
3429 int ret = 0;
3430 const char *ch = arg;
3431 size_t end_pos;
3432 GString *g_begin = NULL;
3433 GString *g_end = NULL;
3434
8b45963b 3435 BT_ASSERT(arg);
9009cc24
PP
3436
3437 if (*ch == '[') {
3438 ch++;
3439 }
3440
3441 g_begin = bt_common_string_until(ch, "", ",", &end_pos);
3442 if (!g_begin || ch[end_pos] != ',' || g_begin->len == 0) {
3443 goto error;
3444 }
3445
3446 ch += end_pos + 1;
3447
3448 g_end = bt_common_string_until(ch, "", "]", &end_pos);
3449 if (!g_end || g_end->len == 0) {
3450 goto error;
3451 }
3452
8b45963b
PP
3453 BT_ASSERT(begin);
3454 BT_ASSERT(end);
9009cc24
PP
3455 *begin = g_begin->str;
3456 *end = g_end->str;
3457 g_string_free(g_begin, FALSE);
3458 g_string_free(g_end, FALSE);
3459 g_begin = NULL;
3460 g_end = NULL;
3461 goto end;
3462
3463error:
3464 ret = -1;
3465
3466end:
3467 if (g_begin) {
3468 g_string_free(g_begin, TRUE);
3469 }
3470
3471 if (g_end) {
3472 g_string_free(g_end, TRUE);
3473 }
3474
3475 return ret;
3476}
3477
3478static
3479int g_list_prepend_gstring(GList **list, const char *string)
3480{
3481 int ret = 0;
3482 GString *gs = g_string_new(string);
3483
8b45963b 3484 BT_ASSERT(list);
9009cc24
PP
3485
3486 if (!gs) {
3487 print_err_oom();
3488 goto end;
3489 }
3490
3491 *list = g_list_prepend(*list, gs);
3492
3493end:
3494 return ret;
3495}
3496
94023a1c
PP
3497static
3498struct implicit_component_args *create_implicit_component_args(void)
3499{
3500 struct implicit_component_args *impl_args =
3501 g_new0(struct implicit_component_args, 1);
3502
3503 if (!impl_args) {
3504 goto end;
3505 }
3506
3507 if (init_implicit_component_args(impl_args, NULL, true)) {
3508 destroy_implicit_component_args(impl_args);
3509 impl_args = NULL;
3510 goto end;
3511 }
3512
3513end:
3514 return impl_args;
3515}
3516
3517static
3518int fill_implicit_ctf_inputs_args(GPtrArray *implicit_ctf_inputs_args,
3519 struct implicit_component_args *base_implicit_ctf_input_args,
3520 GList *leftovers)
3521{
3522 int ret = 0;
3523 GList *leftover;
ee78f405 3524 bt_value_status status;
94023a1c
PP
3525
3526 for (leftover = leftovers; leftover != NULL;
3527 leftover = g_list_next(leftover)) {
3528 GString *gs_leftover = leftover->data;
3529 struct implicit_component_args *impl_args =
3530 create_implicit_component_args();
3531
3532 if (!impl_args) {
3533 print_err_oom();
3534 goto error;
3535 }
3536
3537 impl_args->exists = true;
3538 g_string_assign(impl_args->comp_arg,
3539 base_implicit_ctf_input_args->comp_arg->str);
3540 g_string_assign(impl_args->params_arg,
3541 base_implicit_ctf_input_args->params_arg->str);
3542
3543 /*
3544 * We need our own copy of the extra parameters because
3545 * this is where the unique path goes.
3546 */
8c6884d9 3547 BT_VALUE_PUT_REF_AND_RESET(impl_args->extra_params);
6284461f
PP
3548 status = bt_value_copy(base_implicit_ctf_input_args->extra_params,
3549 &impl_args->extra_params);
b5cdc106 3550 if (status != BT_VALUE_STATUS_OK) {
94023a1c
PP
3551 print_err_oom();
3552 destroy_implicit_component_args(impl_args);
3553 goto error;
3554 }
3555
3556 /* Append unique path parameter */
3557 ret = append_implicit_component_extra_param(impl_args,
3558 "path", gs_leftover->str);
3559 if (ret) {
3560 destroy_implicit_component_args(impl_args);
3561 goto error;
3562 }
3563
3564 g_ptr_array_add(implicit_ctf_inputs_args, impl_args);
3565 }
3566
3567 goto end;
3568
3569error:
3570 ret = -1;
3571
3572end:
3573 return ret;
3574}
3575
9009cc24
PP
3576/*
3577 * Creates a Babeltrace config object from the arguments of a convert
3578 * command.
3579 *
3580 * *retcode is set to the appropriate exit code to use.
3581 */
3582static
3583struct bt_config *bt_config_convert_from_args(int argc, const char *argv[],
3584 int *retcode, bool force_omit_system_plugin_path,
8e765000 3585 bool force_omit_home_plugin_path,
8eee8ea2 3586 const bt_value *initial_plugin_paths, char *log_level)
9009cc24
PP
3587{
3588 poptContext pc = NULL;
3589 char *arg = NULL;
0a011c88
JG
3590 enum bt_config_component_dest cur_comp_dest =
3591 BT_CONFIG_COMPONENT_DEST_UNKNOWN;
9009cc24
PP
3592 int opt, ret = 0;
3593 struct bt_config *cfg = NULL;
9009cc24
PP
3594 bool got_input_format_opt = false;
3595 bool got_output_format_opt = false;
3596 bool trimmer_has_begin = false;
3597 bool trimmer_has_end = false;
b5fe3e00 3598 bool stream_intersection_mode = false;
9009cc24
PP
3599 GString *cur_name = NULL;
3600 GString *cur_name_prefix = NULL;
3601 const char *leftover = NULL;
3602 bool print_run_args = false;
3603 bool print_run_args_0 = false;
3604 bool print_ctf_metadata = false;
8eee8ea2
PP
3605 bt_value *run_args = NULL;
3606 bt_value *all_names = NULL;
9009cc24
PP
3607 GList *source_names = NULL;
3608 GList *filter_names = NULL;
3609 GList *sink_names = NULL;
94023a1c
PP
3610 GList *leftovers = NULL;
3611 GPtrArray *implicit_ctf_inputs_args = NULL;
3612 struct implicit_component_args base_implicit_ctf_input_args = { 0 };
e7ad156c 3613 struct implicit_component_args implicit_ctf_output_args = { 0 };
9009cc24
PP
3614 struct implicit_component_args implicit_lttng_live_args = { 0 };
3615 struct implicit_component_args implicit_dummy_args = { 0 };
3616 struct implicit_component_args implicit_text_args = { 0 };
3617 struct implicit_component_args implicit_debug_info_args = { 0 };
3618 struct implicit_component_args implicit_muxer_args = { 0 };
3619 struct implicit_component_args implicit_trimmer_args = { 0 };
8eee8ea2 3620 bt_value *plugin_paths;
9009cc24
PP
3621 char error_buf[256] = { 0 };
3622 size_t i;
3623 struct bt_common_lttng_live_url_parts lttng_live_url_parts = { 0 };
e7ad156c 3624 char *output = NULL;
9009cc24 3625
6284461f 3626 (void) bt_value_copy(initial_plugin_paths, &plugin_paths);
4b70020d 3627
9009cc24
PP
3628 *retcode = 0;
3629
3630 if (argc <= 1) {
3631 print_convert_usage(stdout);
3632 *retcode = -1;
3633 goto end;
3634 }
3635
94023a1c 3636 if (init_implicit_component_args(&base_implicit_ctf_input_args,
fd5f8053 3637 "source.ctf.fs", false)) {
9009cc24
PP
3638 goto error;
3639 }
3640
e7ad156c
PP
3641 if (init_implicit_component_args(&implicit_ctf_output_args,
3642 "sink.ctf.fs", false)) {
3643 goto error;
3644 }
3645
9009cc24 3646 if (init_implicit_component_args(&implicit_lttng_live_args,
fd5f8053 3647 "source.ctf.lttng-live", false)) {
9009cc24
PP
3648 goto error;
3649 }
3650
fd5f8053
PP
3651 if (init_implicit_component_args(&implicit_text_args,
3652 "sink.text.pretty", false)) {
9009cc24
PP
3653 goto error;
3654 }
3655
fd5f8053
PP
3656 if (init_implicit_component_args(&implicit_dummy_args,
3657 "sink.utils.dummy", false)) {
9009cc24
PP
3658 goto error;
3659 }
3660
3661 if (init_implicit_component_args(&implicit_debug_info_args,
8e765000 3662 "filter.lttng-utils.debug-info", false)) {
9009cc24
PP
3663 goto error;
3664 }
3665
fd5f8053
PP
3666 if (init_implicit_component_args(&implicit_muxer_args,
3667 "filter.utils.muxer", true)) {
9009cc24
PP
3668 goto error;
3669 }
3670
3671 if (init_implicit_component_args(&implicit_trimmer_args,
fd5f8053 3672 "filter.utils.trimmer", false)) {
9009cc24
PP
3673 goto error;
3674 }
3675
94023a1c
PP
3676 implicit_ctf_inputs_args = g_ptr_array_new_with_free_func(
3677 (GDestroyNotify) destroy_implicit_component_args);
3678 if (!implicit_ctf_inputs_args) {
3679 print_err_oom();
3680 goto error;
3681 }
3682
ce141536 3683 all_names = bt_value_map_create();
9009cc24
PP
3684 if (!all_names) {
3685 print_err_oom();
3686 goto error;
3687 }
3688
ce141536 3689 run_args = bt_value_array_create();
9009cc24
PP
3690 if (!run_args) {
3691 print_err_oom();
3692 goto error;
3693 }
3694
3695 cur_name = g_string_new(NULL);
3696 if (!cur_name) {
3697 print_err_oom();
3698 goto error;
3699 }
3700
3701 cur_name_prefix = g_string_new(NULL);
3702 if (!cur_name_prefix) {
3703 print_err_oom();
3704 goto error;
3705 }
3706
3707 ret = append_env_var_plugin_paths(plugin_paths);
3708 if (ret) {
3709 goto error;
3710 }
3711
3712 /*
3713 * First pass: collect all arguments which need to be passed
3714 * as is to the run command. This pass can also add --name
3715 * arguments if needed to automatically name unnamed component
3716 * instances. Also it does the following transformations:
3717 *
3718 * --path=PATH -> --key path --value PATH
3719 * --url=URL -> --key url --value URL
3720 *
3721 * Also it appends the plugin paths of --plugin-path to
3722 * `plugin_paths`.
3723 */
3724 pc = poptGetContext(NULL, argc, (const char **) argv,
3725 convert_long_options, 0);
3726 if (!pc) {
3727 printf_err("Cannot get popt context\n");
3728 goto error;
3729 }
3730
3731 poptReadDefaultConfig(pc, 0);
3732
3733 while ((opt = poptGetNextOpt(pc)) > 0) {
3734 char *name = NULL;
3735 char *plugin_name = NULL;
3736 char *comp_cls_name = NULL;
3737
3738 arg = poptGetOptArg(pc);
3739
3740 switch (opt) {
fd5f8053
PP
3741 case OPT_COMPONENT:
3742 {
ee78f405 3743 bt_component_class_type type;
fd5f8053
PP
3744 const char *type_prefix;
3745
9009cc24
PP
3746 /* Append current component's name if needed */
3747 ret = convert_append_name_param(cur_comp_dest, cur_name,
3748 cur_name_prefix, run_args, all_names,
3749 &source_names, &filter_names, &sink_names);
3750 if (ret) {
3751 goto error;
3752 }
3753
3754 /* Parse the argument */
3755 plugin_comp_cls_names(arg, &name, &plugin_name,
fd5f8053 3756 &comp_cls_name, &type);
9009cc24 3757 if (!plugin_name || !comp_cls_name) {
fd5f8053 3758 printf_err("Invalid format for --component option's argument:\n %s\n",
9009cc24
PP
3759 arg);
3760 goto error;
3761 }
3762
3763 if (name) {
3764 g_string_assign(cur_name, name);
3765 } else {
3766 g_string_assign(cur_name, "");
3767 }
3768
fd5f8053
PP
3769 switch (type) {
3770 case BT_COMPONENT_CLASS_TYPE_SOURCE:
3771 cur_comp_dest = BT_CONFIG_COMPONENT_DEST_SOURCE;
3772 type_prefix = "source";
9009cc24 3773 break;
fd5f8053
PP
3774 case BT_COMPONENT_CLASS_TYPE_FILTER:
3775 cur_comp_dest = BT_CONFIG_COMPONENT_DEST_FILTER;
3776 type_prefix = "filter";
9009cc24 3777 break;
fd5f8053
PP
3778 case BT_COMPONENT_CLASS_TYPE_SINK:
3779 cur_comp_dest = BT_CONFIG_COMPONENT_DEST_SINK;
3780 type_prefix = "sink";
9009cc24
PP
3781 break;
3782 default:
0fbb9a9f 3783 abort();
9009cc24
PP
3784 }
3785
ce141536 3786 if (bt_value_array_append_string_element(run_args,
fd5f8053
PP
3787 "--component")) {
3788 print_err_oom();
3789 goto error;
3790 }
3791
ce141536 3792 if (bt_value_array_append_string_element(run_args, arg)) {
9009cc24
PP
3793 print_err_oom();
3794 goto error;
3795 }
3796
3797 g_string_assign(cur_name_prefix, "");
fd5f8053
PP
3798 g_string_append_printf(cur_name_prefix, "%s.%s.%s",
3799 type_prefix, plugin_name, comp_cls_name);
9009cc24
PP
3800 free(name);
3801 free(plugin_name);
3802 free(comp_cls_name);
3803 name = NULL;
3804 plugin_name = NULL;
3805 comp_cls_name = NULL;
3806 break;
fd5f8053 3807 }
9009cc24
PP
3808 case OPT_PARAMS:
3809 if (cur_name_prefix->len == 0) {
3810 printf_err("No current component of which to set parameters:\n %s\n",
3811 arg);
3812 goto error;
3813 }
3814
ce141536 3815 if (bt_value_array_append_string_element(run_args,
9009cc24
PP
3816 "--params")) {
3817 print_err_oom();
3818 goto error;
3819 }
3820
ce141536 3821 if (bt_value_array_append_string_element(run_args, arg)) {
9009cc24
PP
3822 print_err_oom();
3823 goto error;
3824 }
3825 break;
3826 case OPT_PATH:
3827 if (cur_name_prefix->len == 0) {
3828 printf_err("No current component of which to set `path` parameter:\n %s\n",
3829 arg);
3830 goto error;
3831 }
3832
ce141536 3833 if (bt_value_array_append_string_element(run_args, "--key")) {
9009cc24
PP
3834 print_err_oom();
3835 goto error;
3836 }
3837
ce141536 3838 if (bt_value_array_append_string_element(run_args, "path")) {
9009cc24
PP
3839 print_err_oom();
3840 goto error;
3841 }
3842
ce141536 3843 if (bt_value_array_append_string_element(run_args, "--value")) {
9009cc24
PP
3844 print_err_oom();
3845 goto error;
3846 }
3847
ce141536 3848 if (bt_value_array_append_string_element(run_args, arg)) {
9009cc24
PP
3849 print_err_oom();
3850 goto error;
3851 }
3852 break;
3853 case OPT_URL:
3854 if (cur_name_prefix->len == 0) {
3855 printf_err("No current component of which to set `url` parameter:\n %s\n",
3856 arg);
3857 goto error;
3858 }
3859
ce141536 3860 if (bt_value_array_append_string_element(run_args, "--key")) {
9009cc24
PP
3861 print_err_oom();
3862 goto error;
3863 }
3864
ce141536 3865 if (bt_value_array_append_string_element(run_args, "url")) {
9009cc24
PP
3866 print_err_oom();
3867 goto error;
3868 }
3869
ce141536 3870 if (bt_value_array_append_string_element(run_args, "--value")) {
9009cc24
PP
3871 print_err_oom();
3872 goto error;
3873 }
3874
ce141536 3875 if (bt_value_array_append_string_element(run_args, arg)) {
9009cc24
PP
3876 print_err_oom();
3877 goto error;
3878 }
3879 break;
3880 case OPT_NAME:
3881 if (cur_name_prefix->len == 0) {
3882 printf_err("No current component to name:\n %s\n",
3883 arg);
3884 goto error;
3885 }
3886
ce141536 3887 if (bt_value_array_append_string_element(run_args, "--name")) {
9009cc24
PP
3888 print_err_oom();
3889 goto error;
3890 }
3891
ce141536 3892 if (bt_value_array_append_string_element(run_args, arg)) {
9009cc24
PP
3893 print_err_oom();
3894 goto error;
3895 }
3896
3897 g_string_assign(cur_name, arg);
3898 break;
3899 case OPT_OMIT_HOME_PLUGIN_PATH:
3900 force_omit_home_plugin_path = true;
3901
ce141536 3902 if (bt_value_array_append_string_element(run_args,
9009cc24
PP
3903 "--omit-home-plugin-path")) {
3904 print_err_oom();
3905 goto error;
3906 }
3907 break;
3908 case OPT_RETRY_DURATION:
ce141536 3909 if (bt_value_array_append_string_element(run_args,
9009cc24
PP
3910 "--retry-duration")) {
3911 print_err_oom();
3912 goto error;
3913 }
3914
ce141536 3915 if (bt_value_array_append_string_element(run_args, arg)) {
9009cc24
PP
3916 print_err_oom();
3917 goto error;
3918 }
3919 break;
3920 case OPT_OMIT_SYSTEM_PLUGIN_PATH:
3921 force_omit_system_plugin_path = true;
3922
ce141536 3923 if (bt_value_array_append_string_element(run_args,
9009cc24
PP
3924 "--omit-system-plugin-path")) {
3925 print_err_oom();
3926 goto error;
3927 }
3928 break;
3929 case OPT_PLUGIN_PATH:
3930 if (bt_config_append_plugin_paths_check_setuid_setgid(
3931 plugin_paths, arg)) {
3932 goto error;
3933 }
3934
ce141536 3935 if (bt_value_array_append_string_element(run_args,
9009cc24
PP
3936 "--plugin-path")) {
3937 print_err_oom();
3938 goto error;
3939 }
3940
ce141536 3941 if (bt_value_array_append_string_element(run_args, arg)) {
9009cc24
PP
3942 print_err_oom();
3943 goto error;
3944 }
3945 break;
3946 case OPT_HELP:
3947 print_convert_usage(stdout);
3948 *retcode = -1;
8138bfe1 3949 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
3950 goto end;
3951 case OPT_BEGIN:
3952 case OPT_CLOCK_CYCLES:
3953 case OPT_CLOCK_DATE:
3954 case OPT_CLOCK_FORCE_CORRELATE:
3955 case OPT_CLOCK_GMT:
3956 case OPT_CLOCK_OFFSET:
3957 case OPT_CLOCK_OFFSET_NS:
3958 case OPT_CLOCK_SECONDS:
3959 case OPT_COLOR:
3960 case OPT_DEBUG:
8e765000 3961 case OPT_DEBUG_INFO:
9009cc24
PP
3962 case OPT_DEBUG_INFO_DIR:
3963 case OPT_DEBUG_INFO_FULL_PATH:
3964 case OPT_DEBUG_INFO_TARGET_PREFIX:
3965 case OPT_END:
3966 case OPT_FIELDS:
3967 case OPT_INPUT_FORMAT:
3968 case OPT_NAMES:
9009cc24
PP
3969 case OPT_NO_DELTA:
3970 case OPT_OUTPUT_FORMAT:
3971 case OPT_OUTPUT:
3972 case OPT_RUN_ARGS:
3973 case OPT_RUN_ARGS_0:
3974 case OPT_STREAM_INTERSECTION:
3975 case OPT_TIMERANGE:
3976 case OPT_VERBOSE:
3977 /* Ignore in this pass */
3978 break;
3979 default:
3980 printf_err("Unknown command-line option specified (option code %d)\n",
3981 opt);
3982 goto error;
3983 }
3984
3985 free(arg);
3986 arg = NULL;
3987 }
3988
3989 /* Append current component's name if needed */
3990 ret = convert_append_name_param(cur_comp_dest, cur_name,
3991 cur_name_prefix, run_args, all_names, &source_names,
3992 &filter_names, &sink_names);
3993 if (ret) {
3994 goto error;
3995 }
3996
3997 /* Check for option parsing error */
3998 if (opt < -1) {
3999 printf_err("While parsing command-line options, at option %s: %s\n",
4000 poptBadOption(pc, 0), poptStrerror(opt));
4001 goto error;
4002 }
4003
4004 poptFreeContext(pc);
4005 free(arg);
4006 arg = NULL;
4007
4008 /*
4009 * Second pass: transform the convert-specific options and
4010 * arguments into implicit component instances for the run
4011 * command.
4012 */
4013 pc = poptGetContext(NULL, argc, (const char **) argv,
4014 convert_long_options, 0);
4015 if (!pc) {
4016 printf_err("Cannot get popt context\n");
4017 goto error;
4018 }
4019
4020 poptReadDefaultConfig(pc, 0);
4021
4022 while ((opt = poptGetNextOpt(pc)) > 0) {
4023 arg = poptGetOptArg(pc);
4024
4025 switch (opt) {
4026 case OPT_BEGIN:
4027 if (trimmer_has_begin) {
4028 printf("At --begin option: --begin or --timerange option already specified\n %s\n",
4029 arg);
4030 goto error;
4031 }
4032
4033 trimmer_has_begin = true;
e7ad156c 4034 ret = append_implicit_component_extra_param(
9009cc24
PP
4035 &implicit_trimmer_args, "begin", arg);
4036 implicit_trimmer_args.exists = true;
4037 if (ret) {
4038 goto error;
4039 }
4040 break;
4041 case OPT_END:
4042 if (trimmer_has_end) {
4043 printf("At --end option: --end or --timerange option already specified\n %s\n",
4044 arg);
4045 goto error;
4046 }
4047
4048 trimmer_has_end = true;
e7ad156c 4049 ret = append_implicit_component_extra_param(
9009cc24
PP
4050 &implicit_trimmer_args, "end", arg);
4051 implicit_trimmer_args.exists = true;
4052 if (ret) {
4053 goto error;
4054 }
4055 break;
4056 case OPT_TIMERANGE:
4057 {
4058 char *begin;
4059 char *end;
4060
4061 if (trimmer_has_begin || trimmer_has_end) {
4062 printf("At --timerange option: --begin, --end, or --timerange option already specified\n %s\n",
4063 arg);
4064 goto error;
4065 }
4066
4067 ret = split_timerange(arg, &begin, &end);
4068 if (ret) {
4069 printf_err("Invalid --timerange option's argument: expecting BEGIN,END or [BEGIN,END]:\n %s\n",
4070 arg);
4071 goto error;
4072 }
4073
e7ad156c 4074 ret = append_implicit_component_extra_param(
9009cc24 4075 &implicit_trimmer_args, "begin", begin);
e7ad156c 4076 ret |= append_implicit_component_extra_param(
9009cc24
PP
4077 &implicit_trimmer_args, "end", end);
4078 implicit_trimmer_args.exists = true;
4079 free(begin);
4080 free(end);
4081 if (ret) {
4082 goto error;
4083 }
4084 break;
4085 }
4086 case OPT_CLOCK_CYCLES:
4087 append_implicit_component_param(
4088 &implicit_text_args, "clock-cycles", "yes");
4089 implicit_text_args.exists = true;
4090 break;
4091 case OPT_CLOCK_DATE:
4092 append_implicit_component_param(
4093 &implicit_text_args, "clock-date", "yes");
4094 implicit_text_args.exists = true;
4095 break;
4096 case OPT_CLOCK_FORCE_CORRELATE:
4097 append_implicit_component_param(
53ac3428
PP
4098 &implicit_muxer_args,
4099 "assume-absolute-clock-classes", "yes");
9009cc24
PP
4100 break;
4101 case OPT_CLOCK_GMT:
4102 append_implicit_component_param(
4103 &implicit_text_args, "clock-gmt", "yes");
af47368e
JD
4104 append_implicit_component_param(
4105 &implicit_trimmer_args, "clock-gmt", "yes");
9009cc24
PP
4106 implicit_text_args.exists = true;
4107 break;
4108 case OPT_CLOCK_OFFSET:
94023a1c 4109 base_implicit_ctf_input_args.exists = true;
53ac3428 4110 append_implicit_component_param(
7be7e609 4111 &base_implicit_ctf_input_args,
53ac3428 4112 "clock-class-offset-s", arg);
9009cc24
PP
4113 break;
4114 case OPT_CLOCK_OFFSET_NS:
94023a1c 4115 base_implicit_ctf_input_args.exists = true;
7be7e609
PP
4116 append_implicit_component_param(
4117 &base_implicit_ctf_input_args,
4118 "clock-class-offset-ns", arg);
9009cc24
PP
4119 break;
4120 case OPT_CLOCK_SECONDS:
4121 append_implicit_component_param(
4122 &implicit_text_args, "clock-seconds", "yes");
4123 implicit_text_args.exists = true;
4124 break;
4125 case OPT_COLOR:
9009cc24 4126 implicit_text_args.exists = true;
e7ad156c
PP
4127 ret = append_implicit_component_extra_param(
4128 &implicit_text_args, "color", arg);
9009cc24
PP
4129 if (ret) {
4130 goto error;
4131 }
4132 break;
8e765000
PP
4133 case OPT_DEBUG_INFO:
4134 implicit_debug_info_args.exists = true;
9009cc24
PP
4135 break;
4136 case OPT_DEBUG_INFO_DIR:
e7ad156c
PP
4137 implicit_debug_info_args.exists = true;
4138 ret = append_implicit_component_extra_param(
395a08d0 4139 &implicit_debug_info_args, "debug-info-dir", arg);
9009cc24
PP
4140 if (ret) {
4141 goto error;
4142 }
4143 break;
4144 case OPT_DEBUG_INFO_FULL_PATH:
e7ad156c 4145 implicit_debug_info_args.exists = true;
9009cc24
PP
4146 append_implicit_component_param(
4147 &implicit_debug_info_args, "full-path", "yes");
4148 break;
4149 case OPT_DEBUG_INFO_TARGET_PREFIX:
e7ad156c
PP
4150 implicit_debug_info_args.exists = true;
4151 ret = append_implicit_component_extra_param(
9009cc24
PP
4152 &implicit_debug_info_args,
4153 "target-prefix", arg);
4154 if (ret) {
4155 goto error;
4156 }
4157 break;
4158 case OPT_FIELDS:
4159 {
8eee8ea2 4160 bt_value *fields = fields_from_arg(arg);
9009cc24
PP
4161
4162 if (!fields) {
4163 goto error;
4164 }
4165
e7ad156c 4166 implicit_text_args.exists = true;
9009cc24
PP
4167 ret = insert_flat_params_from_array(
4168 implicit_text_args.params_arg,
ce141536 4169 fields, "field");
8c6884d9 4170 bt_value_put_ref(fields);
9009cc24
PP
4171 if (ret) {
4172 goto error;
4173 }
4174 break;
4175 }
4176 case OPT_NAMES:
4177 {
8eee8ea2 4178 bt_value *names = names_from_arg(arg);
9009cc24
PP
4179
4180 if (!names) {
4181 goto error;
4182 }
4183
e7ad156c 4184 implicit_text_args.exists = true;
9009cc24
PP
4185 ret = insert_flat_params_from_array(
4186 implicit_text_args.params_arg,
ce141536 4187 names, "name");
8c6884d9 4188 bt_value_put_ref(names);
9009cc24
PP
4189 if (ret) {
4190 goto error;
4191 }
4192 break;
4193 }
4194 case OPT_NO_DELTA:
4195 append_implicit_component_param(
4196 &implicit_text_args, "no-delta", "yes");
4197 implicit_text_args.exists = true;
4198 break;
4199 case OPT_INPUT_FORMAT:
4200 if (got_input_format_opt) {
4201 printf_err("Duplicate --input-format option\n");
4202 goto error;
4203 }
4204
4205 got_input_format_opt = true;
4206
4207 if (strcmp(arg, "ctf") == 0) {
94023a1c 4208 base_implicit_ctf_input_args.exists = true;
9009cc24
PP
4209 } else if (strcmp(arg, "lttng-live") == 0) {
4210 implicit_lttng_live_args.exists = true;
4211 } else {
4212 printf_err("Unknown legacy input format:\n %s\n",
4213 arg);
4214 goto error;
4215 }
4216 break;
4217 case OPT_OUTPUT_FORMAT:
4218 if (got_output_format_opt) {
4219 printf_err("Duplicate --output-format option\n");
4220 goto error;
4221 }
4222
4223 got_output_format_opt = true;
4224
4225 if (strcmp(arg, "text") == 0) {
4226 implicit_text_args.exists = true;
e7ad156c
PP
4227 } else if (strcmp(arg, "ctf") == 0) {
4228 implicit_ctf_output_args.exists = true;
9009cc24
PP
4229 } else if (strcmp(arg, "dummy") == 0) {
4230 implicit_dummy_args.exists = true;
4231 } else if (strcmp(arg, "ctf-metadata") == 0) {
4232 print_ctf_metadata = true;
4233 } else {
4234 printf_err("Unknown legacy output format:\n %s\n",
4235 arg);
4236 goto error;
4237 }
4238 break;
4239 case OPT_OUTPUT:
e7ad156c
PP
4240 if (output) {
4241 printf_err("Duplicate --output option\n");
4242 goto error;
4243 }
4244
4245 output = strdup(arg);
4246 if (!output) {
4247 print_err_oom();
9009cc24
PP
4248 goto error;
4249 }
4250 break;
4251 case OPT_RUN_ARGS:
4252 if (print_run_args_0) {
4253 printf_err("Cannot specify --run-args and --run-args-0\n");
4254 goto error;
4255 }
4256
4257 print_run_args = true;
4258 break;
4259 case OPT_RUN_ARGS_0:
4260 if (print_run_args) {
4261 printf_err("Cannot specify --run-args and --run-args-0\n");
4262 goto error;
4263 }
4264
4265 print_run_args_0 = true;
4266 break;
4267 case OPT_STREAM_INTERSECTION:
b5fe3e00
JG
4268 /*
4269 * Applies to all traces implementing the trace-info
4270 * query.
4271 */
4272 stream_intersection_mode = true;
9009cc24
PP
4273 break;
4274 case OPT_VERBOSE:
9e503aa9
PP
4275 if (*log_level != 'V' && *log_level != 'D') {
4276 *log_level = 'I';
9009cc24 4277 }
9009cc24
PP
4278 break;
4279 case OPT_DEBUG:
9e503aa9 4280 *log_level = 'V';
9009cc24
PP
4281 break;
4282 }
4283
4284 free(arg);
4285 arg = NULL;
4286 }
4287
4288 /* Check for option parsing error */
4289 if (opt < -1) {
4290 printf_err("While parsing command-line options, at option %s: %s\n",
4291 poptBadOption(pc, 0), poptStrerror(opt));
4292 goto error;
4293 }
4294
9e503aa9
PP
4295 /*
4296 * Legacy behaviour: --verbose used to make the `text` output
4297 * format print more information. --verbose is now equivalent to
4298 * the INFO log level, which is why we compare to 'I' here.
4299 */
4300 if (*log_level == 'I') {
4301 append_implicit_component_param(&implicit_text_args,
4302 "verbose", "yes");
4303 }
4304
9009cc24
PP
4305 /*
4306 * Append home and system plugin paths now that we possibly got
4307 * --plugin-path.
4308 */
4309 if (append_home_and_system_plugin_paths(plugin_paths,
4310 force_omit_system_plugin_path,
4311 force_omit_home_plugin_path)) {
4312 goto error;
4313 }
4314
94023a1c
PP
4315 /* Consume and keep leftover arguments */
4316 while ((leftover = poptGetArg(pc))) {
4317 GString *gs_leftover = g_string_new(leftover);
9009cc24 4318
94023a1c
PP
4319 if (!gs_leftover) {
4320 print_err_oom();
4321 goto error;
4322 }
4323
4324 leftovers = g_list_append(leftovers, gs_leftover);
4325 if (!leftovers) {
4326 g_string_free(gs_leftover, TRUE);
4327 print_err_oom();
4328 goto error;
4329 }
9009cc24
PP
4330 }
4331
4332 /* Print CTF metadata or print LTTng live sessions */
4333 if (print_ctf_metadata) {
94023a1c
PP
4334 GString *gs_leftover;
4335
4336 if (g_list_length(leftovers) == 0) {
9009cc24
PP
4337 printf_err("--output-format=ctf-metadata specified without a path\n");
4338 goto error;
4339 }
4340
94023a1c
PP
4341 if (g_list_length(leftovers) > 1) {
4342 printf_err("Too many paths specified for --output-format=ctf-metadata\n");
4343 goto error;
4344 }
4345
9009cc24
PP
4346 cfg = bt_config_print_ctf_metadata_create(plugin_paths);
4347 if (!cfg) {
4348 goto error;
4349 }
4350
94023a1c 4351 gs_leftover = leftovers->data;
9009cc24 4352 g_string_assign(cfg->cmd_data.print_ctf_metadata.path,
94023a1c 4353 gs_leftover->str);
a107deea
PP
4354
4355 if (output) {
4356 g_string_assign(
4357 cfg->cmd_data.print_ctf_metadata.output_path,
4358 output);
4359 }
4360
9009cc24
PP
4361 goto end;
4362 }
4363
4364 /*
e7ad156c
PP
4365 * If -o ctf was specified, make sure an output path (--output)
4366 * was also specified. --output does not imply -o ctf because
4367 * it's also used for the default, implicit -o text if -o ctf
4368 * is not specified.
4369 */
4370 if (implicit_ctf_output_args.exists) {
4371 if (!output) {
4372 printf_err("--output-format=ctf specified without --output (trace output path)\n");
4373 goto error;
4374 }
4375
4376 /*
4377 * At this point we know that -o ctf AND --output were
4378 * specified. Make sure that no options were specified
4379 * which would imply -o text because --output would be
4380 * ambiguous in this case. For example, this is wrong:
4381 *
4382 * babeltrace --names=all -o ctf --output=/tmp/path my-trace
4383 *
4384 * because --names=all implies -o text, and --output
4385 * could apply to both the sink.text.pretty and
4386 * sink.ctf.fs implicit components.
4387 */
4388 if (implicit_text_args.exists) {
4389 printf_err("Ambiguous --output option: --output-format=ctf specified but another option implies --output-format=text\n");
4390 goto error;
4391 }
4392 }
4393
4394 /*
4395 * If -o dummy and -o ctf were not specified, and if there are
4396 * no explicit sink components, then use an implicit
4397 * `sink.text.pretty` component.
9009cc24 4398 */
e7ad156c
PP
4399 if (!implicit_dummy_args.exists && !implicit_ctf_output_args.exists &&
4400 !sink_names) {
9009cc24
PP
4401 implicit_text_args.exists = true;
4402 }
4403
e7ad156c
PP
4404 /*
4405 * Set implicit `sink.text.pretty` or `sink.ctf.fs` component's
4406 * `path` parameter if --output was specified.
4407 */
4408 if (output) {
4409 if (implicit_text_args.exists) {
4410 append_implicit_component_extra_param(&implicit_text_args,
4411 "path", output);
4412 } else if (implicit_ctf_output_args.exists) {
4413 append_implicit_component_extra_param(&implicit_ctf_output_args,
4414 "path", output);
4415 }
4416 }
4417
94023a1c
PP
4418 /* Decide where the leftover argument(s) go */
4419 if (g_list_length(leftovers) > 0) {
9009cc24 4420 if (implicit_lttng_live_args.exists) {
94023a1c
PP
4421 GString *gs_leftover;
4422
4423 if (g_list_length(leftovers) > 1) {
4424 printf_err("Too many URLs specified for --output-format=lttng-live\n");
4425 goto error;
4426 }
4427
4428 gs_leftover = leftovers->data;
9009cc24 4429 lttng_live_url_parts =
94023a1c 4430 bt_common_parse_lttng_live_url(gs_leftover->str,
94b828f3 4431 error_buf, sizeof(error_buf));
9009cc24
PP
4432 if (!lttng_live_url_parts.proto) {
4433 printf_err("Invalid LTTng live URL format: %s\n",
4434 error_buf);
4435 goto error;
4436 }
4437
4438 if (!lttng_live_url_parts.session_name) {
4439 /* Print LTTng live sessions */
4440 cfg = bt_config_print_lttng_live_sessions_create(
4441 plugin_paths);
4442 if (!cfg) {
4443 goto error;
4444 }
4445
9009cc24 4446 g_string_assign(cfg->cmd_data.print_lttng_live_sessions.url,
94023a1c 4447 gs_leftover->str);
a107deea
PP
4448
4449 if (output) {
4450 g_string_assign(
4451 cfg->cmd_data.print_lttng_live_sessions.output_path,
4452 output);
4453 }
4454
9009cc24
PP
4455 goto end;
4456 }
4457
e7ad156c 4458 ret = append_implicit_component_extra_param(
94023a1c
PP
4459 &implicit_lttng_live_args, "url",
4460 gs_leftover->str);
9009cc24
PP
4461 if (ret) {
4462 goto error;
4463 }
4464 } else {
94023a1c
PP
4465 /*
4466 * Append one implicit component argument set
4467 * for each leftover (souce.ctf.fs paths). Copy
4468 * the base implicit component arguments.
4469 * Note that they still have to be named later.
4470 */
4471 ret = fill_implicit_ctf_inputs_args(
4472 implicit_ctf_inputs_args,
4473 &base_implicit_ctf_input_args, leftovers);
9009cc24
PP
4474 if (ret) {
4475 goto error;
4476 }
4477 }
4478 }
4479
4480 /*
fd5f8053
PP
4481 * Ensure mutual exclusion between implicit `source.ctf.fs` and
4482 * `source.ctf.lttng-live` components.
9009cc24 4483 */
94023a1c
PP
4484 if (base_implicit_ctf_input_args.exists &&
4485 implicit_lttng_live_args.exists) {
fd5f8053 4486 printf_err("Cannot create both implicit `%s` and `%s` components\n",
94023a1c 4487 base_implicit_ctf_input_args.comp_arg->str,
fd5f8053 4488 implicit_lttng_live_args.comp_arg->str);
9009cc24
PP
4489 goto error;
4490 }
4491
4492 /*
fd5f8053 4493 * If the implicit `source.ctf.fs` or `source.ctf.lttng-live`
94023a1c
PP
4494 * components exists, make sure there's at least one leftover
4495 * (which is the path or URL).
9009cc24 4496 */
94023a1c
PP
4497 if (base_implicit_ctf_input_args.exists &&
4498 g_list_length(leftovers) == 0) {
fd5f8053 4499 printf_err("Missing path for implicit `%s` component\n",
94023a1c 4500 base_implicit_ctf_input_args.comp_arg->str);
9009cc24
PP
4501 goto error;
4502 }
4503
94023a1c 4504 if (implicit_lttng_live_args.exists && g_list_length(leftovers) == 0) {
fd5f8053
PP
4505 printf_err("Missing URL for implicit `%s` component\n",
4506 implicit_lttng_live_args.comp_arg->str);
9009cc24
PP
4507 goto error;
4508 }
4509
4510 /* Assign names to implicit components */
94023a1c
PP
4511 for (i = 0; i < implicit_ctf_inputs_args->len; i++) {
4512 struct implicit_component_args *impl_args =
4513 g_ptr_array_index(implicit_ctf_inputs_args, i);
4514
4515 ret = assign_name_to_implicit_component(impl_args,
4516 "source-ctf-fs", all_names, &source_names, true);
4517 if (ret) {
4518 goto error;
4519 }
9009cc24
PP
4520 }
4521
4522 ret = assign_name_to_implicit_component(&implicit_lttng_live_args,
4523 "lttng-live", all_names, &source_names, true);
4524 if (ret) {
4525 goto error;
4526 }
4527
4528 ret = assign_name_to_implicit_component(&implicit_text_args,
4529 "pretty", all_names, &sink_names, true);
4530 if (ret) {
4531 goto error;
4532 }
4533
e7ad156c
PP
4534 ret = assign_name_to_implicit_component(&implicit_ctf_output_args,
4535 "sink-ctf-fs", all_names, &sink_names, true);
4536 if (ret) {
4537 goto error;
4538 }
4539
9009cc24
PP
4540 ret = assign_name_to_implicit_component(&implicit_dummy_args,
4541 "dummy", all_names, &sink_names, true);
4542 if (ret) {
4543 goto error;
4544 }
4545
4546 ret = assign_name_to_implicit_component(&implicit_muxer_args,
4547 "muxer", all_names, NULL, false);
4548 if (ret) {
4549 goto error;
4550 }
4551
4552 ret = assign_name_to_implicit_component(&implicit_trimmer_args,
4553 "trimmer", all_names, NULL, false);
4554 if (ret) {
4555 goto error;
4556 }
4557
4558 ret = assign_name_to_implicit_component(&implicit_debug_info_args,
4559 "debug-info", all_names, NULL, false);
4560 if (ret) {
4561 goto error;
4562 }
4563
4564 /* Make sure there's at least one source and one sink */
4565 if (!source_names) {
4566 printf_err("No source component\n");
4567 goto error;
4568 }
4569
4570 if (!sink_names) {
4571 printf_err("No sink component\n");
4572 goto error;
4573 }
4574
4575 /*
4576 * Prepend the muxer, the trimmer, and the debug info to the
4577 * filter chain so that we have:
4578 *
4579 * sources -> muxer -> [trimmer] -> [debug info] ->
4580 * [user filters] -> sinks
4581 */
4582 if (implicit_debug_info_args.exists) {
4583 if (g_list_prepend_gstring(&filter_names,
4584 implicit_debug_info_args.name_arg->str)) {
4585 goto error;
4586 }
4587 }
4588
4589 if (implicit_trimmer_args.exists) {
4590 if (g_list_prepend_gstring(&filter_names,
4591 implicit_trimmer_args.name_arg->str)) {
4592 goto error;
4593 }
4594 }
4595
4596 if (g_list_prepend_gstring(&filter_names,
4597 implicit_muxer_args.name_arg->str)) {
4598 goto error;
4599 }
4600
4601 /*
4602 * Append the equivalent run arguments for the implicit
4603 * components.
4604 */
94023a1c
PP
4605 for (i = 0; i < implicit_ctf_inputs_args->len; i++) {
4606 struct implicit_component_args *impl_args =
4607 g_ptr_array_index(implicit_ctf_inputs_args, i);
4608
4609 ret = append_run_args_for_implicit_component(impl_args,
4610 run_args);
4611 if (ret) {
4612 goto error;
4613 }
9009cc24
PP
4614 }
4615
fd5f8053 4616 ret = append_run_args_for_implicit_component(&implicit_lttng_live_args,
9009cc24
PP
4617 run_args);
4618 if (ret) {
4619 goto error;
4620 }
4621
fd5f8053
PP
4622 ret = append_run_args_for_implicit_component(&implicit_text_args,
4623 run_args);
9009cc24
PP
4624 if (ret) {
4625 goto error;
4626 }
4627
e7ad156c
PP
4628 ret = append_run_args_for_implicit_component(&implicit_ctf_output_args,
4629 run_args);
4630 if (ret) {
4631 goto error;
4632 }
4633
fd5f8053
PP
4634 ret = append_run_args_for_implicit_component(&implicit_dummy_args,
4635 run_args);
9009cc24
PP
4636 if (ret) {
4637 goto error;
4638 }
4639
fd5f8053
PP
4640 ret = append_run_args_for_implicit_component(&implicit_muxer_args,
4641 run_args);
9009cc24
PP
4642 if (ret) {
4643 goto error;
4644 }
4645
fd5f8053 4646 ret = append_run_args_for_implicit_component(&implicit_trimmer_args,
9009cc24
PP
4647 run_args);
4648 if (ret) {
4649 goto error;
4650 }
4651
fd5f8053 4652 ret = append_run_args_for_implicit_component(&implicit_debug_info_args,
9009cc24
PP
4653 run_args);
4654 if (ret) {
4655 goto error;
4656 }
4657
4658 /* Auto-connect components */
4659 ret = convert_auto_connect(run_args, source_names, filter_names,
4660 sink_names);
4661 if (ret) {
4662 printf_err("Cannot auto-connect components\n");
4663 goto error;
4664 }
4665
4666 /*
4667 * We have all the run command arguments now. Depending on
4668 * --run-args, we pass this to the run command or print them
4669 * here.
4670 */
4671 if (print_run_args || print_run_args_0) {
f9692f14
PP
4672 if (stream_intersection_mode) {
4673 printf_err("Cannot specify --stream-intersection with --run-args or --run-args-0\n");
4674 goto error;
4675 }
4676
ce141536 4677 for (i = 0; i < bt_value_array_get_size(run_args); i++) {
8eee8ea2 4678 const bt_value *arg_value =
ce141536
PP
4679 bt_value_array_borrow_element_by_index(run_args,
4680 i);
9009cc24
PP
4681 const char *arg;
4682 GString *quoted = NULL;
4683 const char *arg_to_print;
4684
8b45963b 4685 BT_ASSERT(arg_value);
b5cdc106 4686 arg = bt_value_string_get(arg_value);
9009cc24
PP
4687
4688 if (print_run_args) {
4689 quoted = bt_common_shell_quote(arg, true);
4690 if (!quoted) {
4691 goto error;
4692 }
4693
4694 arg_to_print = quoted->str;
4695 } else {
4696 arg_to_print = arg;
4697 }
4698
4699 printf("%s", arg_to_print);
4700
4701 if (quoted) {
4702 g_string_free(quoted, TRUE);
4703 }
4704
ce141536 4705 if (i < bt_value_array_get_size(run_args) - 1) {
9009cc24
PP
4706 if (print_run_args) {
4707 putchar(' ');
4708 } else {
4709 putchar('\0');
4710 }
4711 }
4712 }
4713
4714 *retcode = -1;
8138bfe1 4715 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
4716 goto end;
4717 }
4718
ce141536
PP
4719 cfg = bt_config_run_from_args_array(run_args, retcode,
4720 force_omit_system_plugin_path,
4721 force_omit_home_plugin_path,
4722 initial_plugin_paths);
fc11b6a6
PP
4723 if (!cfg) {
4724 goto error;
4725 }
4726
b5fe3e00 4727 cfg->cmd_data.run.stream_intersection_mode = stream_intersection_mode;
9009cc24
PP
4728 goto end;
4729
4730error:
4731 *retcode = 1;
8138bfe1 4732 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
4733
4734end:
4735 if (pc) {
4736 poptFreeContext(pc);
4737 }
4738
4739 free(arg);
e7ad156c 4740 free(output);
9009cc24
PP
4741
4742 if (cur_name) {
4743 g_string_free(cur_name, TRUE);
4744 }
4745
4746 if (cur_name_prefix) {
4747 g_string_free(cur_name_prefix, TRUE);
4748 }
4749
94023a1c
PP
4750 if (implicit_ctf_inputs_args) {
4751 g_ptr_array_free(implicit_ctf_inputs_args, TRUE);
4752 }
4753
8c6884d9
PP
4754 bt_value_put_ref(run_args);
4755 bt_value_put_ref(all_names);
9009cc24
PP
4756 destroy_glist_of_gstring(source_names);
4757 destroy_glist_of_gstring(filter_names);
4758 destroy_glist_of_gstring(sink_names);
94023a1c
PP
4759 destroy_glist_of_gstring(leftovers);
4760 finalize_implicit_component_args(&base_implicit_ctf_input_args);
4761 finalize_implicit_component_args(&implicit_ctf_output_args);
4762 finalize_implicit_component_args(&implicit_lttng_live_args);
4763 finalize_implicit_component_args(&implicit_dummy_args);
4764 finalize_implicit_component_args(&implicit_text_args);
4765 finalize_implicit_component_args(&implicit_debug_info_args);
4766 finalize_implicit_component_args(&implicit_muxer_args);
4767 finalize_implicit_component_args(&implicit_trimmer_args);
8c6884d9 4768 bt_value_put_ref(plugin_paths);
9009cc24
PP
4769 bt_common_destroy_lttng_live_url_parts(&lttng_live_url_parts);
4770 return cfg;
4771}
4772
4773/*
4774 * Prints the Babeltrace 2.x general usage.
4775 */
4776static
4777void print_gen_usage(FILE *fp)
4778{
4779 fprintf(fp, "Usage: babeltrace [GENERAL OPTIONS] [COMMAND] [COMMAND ARGUMENTS]\n");
4780 fprintf(fp, "\n");
4781 fprintf(fp, "General options:\n");
4782 fprintf(fp, "\n");
9e503aa9
PP
4783 fprintf(fp, " -d, --debug Enable debug mode (same as --log-level=V)\n");
4784 fprintf(fp, " -h, --help Show this help and quit\n");
21b5b16a 4785 fprintf(fp, " -l, --log-level=LVL Set all log levels to LVL (`N`, `V`, `D`,\n");
9e503aa9
PP
4786 fprintf(fp, " `I`, `W` (default), `E`, or `F`)\n");
4787 fprintf(fp, " -v, --verbose Enable verbose mode (same as --log-level=I)\n");
4788 fprintf(fp, " -V, --version Show version and quit\n");
9009cc24
PP
4789 fprintf(fp, "\n");
4790 fprintf(fp, "Available commands:\n");
4791 fprintf(fp, "\n");
4792 fprintf(fp, " convert Convert and trim traces (default)\n");
4793 fprintf(fp, " help Get help for a plugin or a component class\n");
4794 fprintf(fp, " list-plugins List available plugins and their content\n");
4795 fprintf(fp, " query Query objects from a component class\n");
4796 fprintf(fp, " run Build a processing graph and run it\n");
4797 fprintf(fp, "\n");
4798 fprintf(fp, "Use `babeltrace COMMAND --help` to show the help of COMMAND.\n");
4799}
4800
9e503aa9
PP
4801static
4802char log_level_from_arg(const char *arg)
4803{
4804 char level = 'U';
4805
4806 if (strcmp(arg, "VERBOSE") == 0 ||
4807 strcmp(arg, "V") == 0) {
4808 level = 'V';
4809 } else if (strcmp(arg, "DEBUG") == 0 ||
4810 strcmp(arg, "D") == 0) {
4811 level = 'D';
4812 } else if (strcmp(arg, "INFO") == 0 ||
4813 strcmp(arg, "I") == 0) {
4814 level = 'I';
4815 } else if (strcmp(arg, "WARN") == 0 ||
4816 strcmp(arg, "WARNING") == 0 ||
4817 strcmp(arg, "W") == 0) {
4818 level = 'W';
4819 } else if (strcmp(arg, "ERROR") == 0 ||
4820 strcmp(arg, "E") == 0) {
4821 level = 'E';
4822 } else if (strcmp(arg, "FATAL") == 0 ||
4823 strcmp(arg, "F") == 0) {
4824 level = 'F';
4825 } else if (strcmp(arg, "NONE") == 0 ||
4826 strcmp(arg, "N") == 0) {
4827 level = 'N';
4828 }
4829
4830 return level;
4831}
4832
9009cc24
PP
4833struct bt_config *bt_config_cli_args_create(int argc, const char *argv[],
4834 int *retcode, bool force_omit_system_plugin_path,
8e765000 4835 bool force_omit_home_plugin_path,
8eee8ea2 4836 const bt_value *initial_plugin_paths)
9009cc24
PP
4837{
4838 struct bt_config *config = NULL;
9009cc24
PP
4839 int i;
4840 const char **command_argv = NULL;
4841 int command_argc = -1;
4842 const char *command_name = NULL;
9e503aa9 4843 char log_level = 'U';
9009cc24
PP
4844
4845 enum command_type {
4846 COMMAND_TYPE_NONE = -1,
4847 COMMAND_TYPE_RUN = 0,
4848 COMMAND_TYPE_CONVERT,
4849 COMMAND_TYPE_LIST_PLUGINS,
4850 COMMAND_TYPE_HELP,
4851 COMMAND_TYPE_QUERY,
4852 } command_type = COMMAND_TYPE_NONE;
4853
4854 *retcode = -1;
4855
4856 if (!initial_plugin_paths) {
ce141536 4857 initial_plugin_paths = bt_value_array_create();
9009cc24
PP
4858 if (!initial_plugin_paths) {
4859 *retcode = 1;
4860 goto end;
4861 }
4862 } else {
8c6884d9 4863 bt_value_get_ref(initial_plugin_paths);
9009cc24
PP
4864 }
4865
4866 if (argc <= 1) {
329e245b
PP
4867 print_version();
4868 puts("");
9009cc24
PP
4869 print_gen_usage(stdout);
4870 goto end;
4871 }
4872
4873 for (i = 1; i < argc; i++) {
4874 const char *cur_arg = argv[i];
9e503aa9 4875 const char *next_arg = i == (argc - 1) ? NULL : argv[i + 1];
9009cc24
PP
4876
4877 if (strcmp(cur_arg, "-d") == 0 ||
4878 strcmp(cur_arg, "--debug") == 0) {
9e503aa9 4879 log_level = 'V';
9009cc24
PP
4880 } else if (strcmp(cur_arg, "-v") == 0 ||
4881 strcmp(cur_arg, "--verbose") == 0) {
9e503aa9
PP
4882 if (log_level != 'V' && log_level != 'D') {
4883 /*
4884 * Legacy: do not override a previous
4885 * --debug because --verbose and --debug
4886 * can be specified together (in this
4887 * case we want the lowest log level to
4888 * apply, VERBOSE).
4889 */
4890 log_level = 'I';
4891 }
21b5b16a
PP
4892 } else if (strcmp(cur_arg, "--log-level") == 0 ||
4893 strcmp(cur_arg, "-l") == 0) {
9e503aa9
PP
4894 if (!next_arg) {
4895 printf_err("Missing log level value for --log-level option\n");
4896 *retcode = 1;
4897 goto end;
4898 }
4899
4900 log_level = log_level_from_arg(next_arg);
4901 if (log_level == 'U') {
4902 printf_err("Invalid argument for --log-level option:\n %s\n",
4903 next_arg);
4904 *retcode = 1;
4905 goto end;
4906 }
4907
4908 i++;
4909 } else if (strncmp(cur_arg, "--log-level=", 12) == 0) {
4910 const char *arg = &cur_arg[12];
4911
21b5b16a
PP
4912 log_level = log_level_from_arg(arg);
4913 if (log_level == 'U') {
4914 printf_err("Invalid argument for --log-level option:\n %s\n",
4915 arg);
4916 *retcode = 1;
4917 goto end;
4918 }
4919 } else if (strncmp(cur_arg, "-l", 2) == 0) {
4920 const char *arg = &cur_arg[2];
4921
9e503aa9
PP
4922 log_level = log_level_from_arg(arg);
4923 if (log_level == 'U') {
4924 printf_err("Invalid argument for --log-level option:\n %s\n",
4925 arg);
4926 *retcode = 1;
4927 goto end;
4928 }
9009cc24
PP
4929 } else if (strcmp(cur_arg, "-V") == 0 ||
4930 strcmp(cur_arg, "--version") == 0) {
4931 print_version();
4932 goto end;
4933 } else if (strcmp(cur_arg, "-h") == 0 ||
4934 strcmp(cur_arg, "--help") == 0) {
4935 print_gen_usage(stdout);
4936 goto end;
4937 } else {
9009cc24
PP
4938 /*
4939 * First unknown argument: is it a known command
4940 * name?
4941 */
9e503aa9
PP
4942 command_argv = &argv[i];
4943 command_argc = argc - i;
4944
9009cc24
PP
4945 if (strcmp(cur_arg, "convert") == 0) {
4946 command_type = COMMAND_TYPE_CONVERT;
4947 } else if (strcmp(cur_arg, "list-plugins") == 0) {
4948 command_type = COMMAND_TYPE_LIST_PLUGINS;
4949 } else if (strcmp(cur_arg, "help") == 0) {
4950 command_type = COMMAND_TYPE_HELP;
4951 } else if (strcmp(cur_arg, "query") == 0) {
4952 command_type = COMMAND_TYPE_QUERY;
4953 } else if (strcmp(cur_arg, "run") == 0) {
4954 command_type = COMMAND_TYPE_RUN;
4955 } else {
4956 /*
4957 * Unknown argument, but not a known
9e503aa9
PP
4958 * command name: assume the default
4959 * `convert` command.
9009cc24
PP
4960 */
4961 command_type = COMMAND_TYPE_CONVERT;
9e503aa9
PP
4962 command_name = "convert";
4963 command_argv = &argv[i - 1];
4964 command_argc = argc - i + 1;
9009cc24
PP
4965 }
4966 break;
4967 }
4968 }
4969
4970 if (command_type == COMMAND_TYPE_NONE) {
4971 /*
4972 * We only got non-help, non-version general options
4973 * like --verbose and --debug, without any other
4974 * arguments, so we can't do anything useful: print the
4975 * usage and quit.
4976 */
4977 print_gen_usage(stdout);
4978 goto end;
4979 }
4980
8b45963b
PP
4981 BT_ASSERT(command_argv);
4982 BT_ASSERT(command_argc >= 0);
9009cc24
PP
4983
4984 switch (command_type) {
4985 case COMMAND_TYPE_RUN:
4986 config = bt_config_run_from_args(command_argc, command_argv,
4987 retcode, force_omit_system_plugin_path,
4988 force_omit_home_plugin_path, initial_plugin_paths);
4989 break;
4990 case COMMAND_TYPE_CONVERT:
4991 config = bt_config_convert_from_args(command_argc, command_argv,
4992 retcode, force_omit_system_plugin_path,
8e765000 4993 force_omit_home_plugin_path,
9e503aa9 4994 initial_plugin_paths, &log_level);
9009cc24
PP
4995 break;
4996 case COMMAND_TYPE_LIST_PLUGINS:
4997 config = bt_config_list_plugins_from_args(command_argc,
4998 command_argv, retcode, force_omit_system_plugin_path,
4999 force_omit_home_plugin_path, initial_plugin_paths);
5000 break;
5001 case COMMAND_TYPE_HELP:
5002 config = bt_config_help_from_args(command_argc,
5003 command_argv, retcode, force_omit_system_plugin_path,
5004 force_omit_home_plugin_path, initial_plugin_paths);
5005 break;
5006 case COMMAND_TYPE_QUERY:
5007 config = bt_config_query_from_args(command_argc,
5008 command_argv, retcode, force_omit_system_plugin_path,
5009 force_omit_home_plugin_path, initial_plugin_paths);
5010 break;
5011 default:
0fbb9a9f 5012 abort();
9009cc24
PP
5013 }
5014
5015 if (config) {
9e503aa9
PP
5016 if (log_level == 'U') {
5017 log_level = 'W';
9009cc24
PP
5018 }
5019
9e503aa9 5020 config->log_level = log_level;
9009cc24
PP
5021 config->command_name = command_name;
5022 }
5023
5024end:
8c6884d9 5025 bt_value_put_ref(initial_plugin_paths);
9009cc24
PP
5026 return config;
5027}
This page took 0.299091 seconds and 4 git commands to generate.