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