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