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