Fix: bt2: duplicate test name in test_field.py
[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");
3971c701
SM
1303 fprintf(fp, "* Map, formatted as an opening `{`, a comma-separated list of PARAM=VALUE\n");
1304 fprintf(fp, " assignments and a closing `}`.\n");
9009cc24
PP
1305 fprintf(fp, "\n");
1306 fprintf(fp, "You can put whitespaces allowed around individual `=` and `,` symbols.\n");
1307 fprintf(fp, "\n");
1308 fprintf(fp, "Example:\n");
1309 fprintf(fp, "\n");
1310 fprintf(fp, " many=null, fresh=yes, condition=false, squirrel=-782329,\n");
fdd3a2da 1311 fprintf(fp, " play=+23, observe=3.14, simple=beef, needs-quotes=\"some string\",\n");
b25da386
SM
1312 fprintf(fp, " escape.chars-are:allowed=\"this is a \\\" double quote\",\n");
1313 fprintf(fp, " things=[1, \"2\", 3]\n");
9009cc24
PP
1314 fprintf(fp, "\n");
1315 fprintf(fp, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
ec2c5e50 1316 fprintf(fp, "babeltrace2 from a shell.\n");
9009cc24
PP
1317}
1318
976b24cf
SM
1319static
1320bool help_option_is_specified(
1321 const struct bt_argpar_parse_ret *argpar_parse_ret)
1322{
1323 int i;
1324 bool specified = false;
1325
1326 for (i = 0; i < argpar_parse_ret->items->len; i++) {
1327 struct bt_argpar_item *argpar_item =
1328 g_ptr_array_index(argpar_parse_ret->items, i);
1329 struct bt_argpar_item_opt *argpar_item_opt;
1330
1331 if (argpar_item->type != BT_ARGPAR_ITEM_TYPE_OPT) {
1332 continue;
1333 }
1334
1335 argpar_item_opt = (struct bt_argpar_item_opt *) argpar_item;
1336 if (argpar_item_opt->descr->id == OPT_HELP) {
1337 specified = true;
1338 break;
1339 }
1340 }
1341
1342 return specified;
1343}
9009cc24
PP
1344
1345/*
1346 * Prints the help command usage.
1347 */
1348static
1349void print_help_usage(FILE *fp)
1350{
ec2c5e50
MJ
1351 fprintf(fp, "Usage: babeltrace2 [GENERAL OPTIONS] help [OPTIONS] PLUGIN\n");
1352 fprintf(fp, " babeltrace2 [GENERAL OPTIONS] help [OPTIONS] TYPE.PLUGIN.CLS\n");
9009cc24
PP
1353 fprintf(fp, "\n");
1354 fprintf(fp, "Options:\n");
1355 fprintf(fp, "\n");
02510056 1356 fprintf(fp, " -h, --help Show this help and quit\n");
9009cc24 1357 fprintf(fp, "\n");
ec2c5e50 1358 fprintf(fp, "See `babeltrace2 --help` for the list of general options.\n");
9009cc24 1359 fprintf(fp, "\n");
ec2c5e50 1360 fprintf(fp, "Use `babeltrace2 list-plugins` to show the list of available plugins.\n");
9009cc24
PP
1361}
1362
1363static
13ae36e1
SM
1364const struct bt_argpar_opt_descr help_options[] = {
1365 /* id, short_name, long_name, with_arg */
1366 { OPT_HELP, 'h', "help", false },
13ae36e1 1367 BT_ARGPAR_OPT_DESCR_SENTINEL
9009cc24
PP
1368};
1369
1370/*
1371 * Creates a Babeltrace config object from the arguments of a help
1372 * command.
1373 *
1374 * *retcode is set to the appropriate exit code to use.
1375 */
1376static
1377struct bt_config *bt_config_help_from_args(int argc, const char *argv[],
02510056
PP
1378 int *retcode, const bt_value *plugin_paths,
1379 int default_log_level)
9009cc24 1380{
9009cc24 1381 struct bt_config *cfg = NULL;
9009cc24 1382 char *plugin_name = NULL, *comp_cls_name = NULL;
5fd3a2a7 1383 struct bt_argpar_parse_ret argpar_parse_ret = { 0 };
02510056 1384 struct bt_argpar_item_non_opt *non_opt;
9009cc24
PP
1385
1386 *retcode = 0;
02510056 1387 cfg = bt_config_help_create(plugin_paths, default_log_level);
9009cc24
PP
1388 if (!cfg) {
1389 goto error;
1390 }
1391
9009cc24 1392 /* Parse options */
13ae36e1
SM
1393 argpar_parse_ret = bt_argpar_parse(argc, argv, help_options, true);
1394 if (argpar_parse_ret.error) {
1395 BT_CLI_LOGE_APPEND_CAUSE(
1396 "While parsing `help` command's command-line arguments: %s",
1397 argpar_parse_ret.error->str);
9009cc24
PP
1398 goto error;
1399 }
1400
13ae36e1
SM
1401 if (help_option_is_specified(&argpar_parse_ret)) {
1402 print_help_usage(stdout);
1403 *retcode = -1;
1404 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1405 goto end;
1406 }
9009cc24 1407
02510056
PP
1408 if (argpar_parse_ret.items->len == 0) {
1409 BT_CLI_LOGE_APPEND_CAUSE(
1410 "Missing plugin name or component class descriptor.");
1411 goto error;
1412 } else if (argpar_parse_ret.items->len > 1) {
1413 /*
1414 * At this point we know there are least two non-option
1415 * arguments because we don't reach here with `--help`,
1416 * the only option.
1417 */
1418 non_opt = argpar_parse_ret.items->pdata[1];
1419 BT_CLI_LOGE_APPEND_CAUSE(
1420 "Extraneous command-line argument specified to `help` command: `%s`.",
1421 non_opt->arg);
1422 goto error;
9009cc24
PP
1423 }
1424
02510056
PP
1425 non_opt = argpar_parse_ret.items->pdata[0];
1426 plugin_comp_cls_names(non_opt->arg, NULL, &plugin_name, &comp_cls_name,
1427 &cfg->cmd_data.help.cfg_component->type);
1428 if (plugin_name && comp_cls_name) {
1429 /* Component class help */
1430 g_string_assign(cfg->cmd_data.help.cfg_component->plugin_name,
1431 plugin_name);
1432 g_string_assign(cfg->cmd_data.help.cfg_component->comp_cls_name,
1433 comp_cls_name);
e5c7cd9b 1434 } else {
02510056
PP
1435 /* Fall back to plugin help */
1436 g_string_assign(cfg->cmd_data.help.cfg_component->plugin_name,
1437 non_opt->arg);
9009cc24
PP
1438 }
1439
1440 goto end;
1441
1442error:
1443 *retcode = 1;
65300d60 1444 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
1445
1446end:
9009cc24
PP
1447 g_free(plugin_name);
1448 g_free(comp_cls_name);
1449
13ae36e1 1450 bt_argpar_parse_ret_fini(&argpar_parse_ret);
9009cc24 1451
9009cc24
PP
1452 return cfg;
1453}
1454
1455/*
1456 * Prints the help command usage.
1457 */
1458static
1459void print_query_usage(FILE *fp)
1460{
ec2c5e50 1461 fprintf(fp, "Usage: babeltrace2 [GEN OPTS] query [OPTS] TYPE.PLUGIN.CLS OBJECT\n");
9009cc24
PP
1462 fprintf(fp, "\n");
1463 fprintf(fp, "Options:\n");
1464 fprintf(fp, "\n");
02510056
PP
1465 fprintf(fp, " -p, --params=PARAMS Set the query parameters to PARAMS (see the expected\n");
1466 fprintf(fp, " format of PARAMS below)\n");
1467 fprintf(fp, " -h, --help Show this help and quit\n");
9009cc24
PP
1468 fprintf(fp, "\n\n");
1469 print_expected_params_format(fp);
1470}
1471
1472static
22692f90
SM
1473const struct bt_argpar_opt_descr query_options[] = {
1474 /* id, short_name, long_name, with_arg */
1475 { OPT_HELP, 'h', "help", false },
22692f90 1476 { OPT_PARAMS, 'p', "params", true },
22692f90 1477 BT_ARGPAR_OPT_DESCR_SENTINEL
9009cc24
PP
1478};
1479
1480/*
1481 * Creates a Babeltrace config object from the arguments of a query
1482 * command.
1483 *
1484 * *retcode is set to the appropriate exit code to use.
1485 */
1486static
1487struct bt_config *bt_config_query_from_args(int argc, const char *argv[],
02510056 1488 int *retcode, const bt_value *plugin_paths,
29da2ffc 1489 int default_log_level)
9009cc24 1490{
02510056 1491 int i;
9009cc24 1492 struct bt_config *cfg = NULL;
22692f90
SM
1493 const char *component_class_spec = NULL;
1494 const char *query_object = NULL;
ac72582c 1495 bt_value *params;
8bd04432 1496 GString *error_str = NULL;
5fd3a2a7 1497 struct bt_argpar_parse_ret argpar_parse_ret = { 0 };
ac72582c
SM
1498
1499 params = bt_value_null;
1500 bt_value_get_ref(bt_value_null);
9009cc24
PP
1501
1502 *retcode = 0;
02510056 1503 cfg = bt_config_query_create(plugin_paths);
9009cc24
PP
1504 if (!cfg) {
1505 goto error;
1506 }
1507
8bd04432
PP
1508 error_str = g_string_new(NULL);
1509 if (!error_str) {
9f901451 1510 BT_CLI_LOGE_APPEND_CAUSE_OOM();
8bd04432
PP
1511 goto error;
1512 }
1513
9009cc24 1514 /* Parse options */
22692f90
SM
1515 argpar_parse_ret = bt_argpar_parse(argc, argv, query_options, true);
1516 if (argpar_parse_ret.error) {
1517 BT_CLI_LOGE_APPEND_CAUSE(
1518 "While parsing `query` command's command-line arguments: %s",
1519 argpar_parse_ret.error->str);
9009cc24
PP
1520 goto error;
1521 }
1522
22692f90
SM
1523 if (help_option_is_specified(&argpar_parse_ret)) {
1524 print_query_usage(stdout);
1525 *retcode = -1;
1526 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1527 goto end;
1528 }
9009cc24 1529
22692f90
SM
1530 for (i = 0; i < argpar_parse_ret.items->len; i++) {
1531 struct bt_argpar_item *argpar_item =
1532 g_ptr_array_index(argpar_parse_ret.items, i);
9009cc24 1533
22692f90
SM
1534 if (argpar_item->type == BT_ARGPAR_ITEM_TYPE_OPT) {
1535 struct bt_argpar_item_opt *argpar_item_opt =
1536 (struct bt_argpar_item_opt *) argpar_item;
1537 const char *arg = argpar_item_opt->arg;
1538
1539 switch (argpar_item_opt->descr->id) {
22692f90
SM
1540 case OPT_PARAMS:
1541 {
1542 bt_value_put_ref(params);
1543 params = cli_value_from_arg(arg, error_str);
1544 if (!params) {
1545 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
1546 error_str->str);
1547 goto error;
1548 }
1549 break;
1550 }
1551 default:
1552 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1553 argpar_item_opt->descr->id);
9009cc24
PP
1554 goto error;
1555 }
22692f90
SM
1556 } else {
1557 struct bt_argpar_item_non_opt *argpar_item_non_opt
1558 = (struct bt_argpar_item_non_opt *) argpar_item;
1559
1560 /*
24594611
PP
1561 * We need exactly two non-option arguments
1562 * which are the mandatory component class
1563 * specification and query object.
22692f90
SM
1564 */
1565 if (!component_class_spec) {
1566 component_class_spec = argpar_item_non_opt->arg;
1567 } else if (!query_object) {
1568 query_object = argpar_item_non_opt->arg;
1569 } else {
1570 BT_CLI_LOGE_APPEND_CAUSE("Extraneous command-line argument specified to `query` command: `%s`.",
1571 argpar_item_non_opt->arg);
9009cc24
PP
1572 goto error;
1573 }
9009cc24 1574 }
9009cc24
PP
1575 }
1576
22692f90 1577 if (!component_class_spec || !query_object) {
7b1e06a1
PP
1578 print_query_usage(stdout);
1579 *retcode = -1;
65300d60 1580 BT_OBJECT_PUT_REF_AND_RESET(cfg);
7b1e06a1
PP
1581 goto end;
1582 }
1583
22692f90
SM
1584 cfg->cmd_data.query.cfg_component =
1585 bt_config_component_from_arg(component_class_spec,
1586 default_log_level);
1587 if (!cfg->cmd_data.query.cfg_component) {
1588 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for component class specification:\n %s",
1589 component_class_spec);
1590 goto error;
9009cc24
PP
1591 }
1592
22692f90
SM
1593 BT_ASSERT(params);
1594 BT_OBJECT_MOVE_REF(cfg->cmd_data.query.cfg_component->params, params);
1595
1596 if (strlen(query_object) == 0) {
1597 BT_CLI_LOGE_APPEND_CAUSE("Invalid empty object.");
9009cc24
PP
1598 goto error;
1599 }
1600
22692f90 1601 g_string_assign(cfg->cmd_data.query.object, query_object);
9009cc24
PP
1602 goto end;
1603
1604error:
1605 *retcode = 1;
65300d60 1606 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
1607
1608end:
22692f90 1609 bt_argpar_parse_ret_fini(&argpar_parse_ret);
9009cc24 1610
8bd04432
PP
1611 if (error_str) {
1612 g_string_free(error_str, TRUE);
1613 }
1614
c5b9b441 1615 bt_value_put_ref(params);
9009cc24
PP
1616 return cfg;
1617}
1618
1619/*
1620 * Prints the list-plugins command usage.
1621 */
1622static
1623void print_list_plugins_usage(FILE *fp)
1624{
ec2c5e50 1625 fprintf(fp, "Usage: babeltrace2 [GENERAL OPTIONS] list-plugins [OPTIONS]\n");
9009cc24
PP
1626 fprintf(fp, "\n");
1627 fprintf(fp, "Options:\n");
1628 fprintf(fp, "\n");
3efa3052 1629 fprintf(fp, " -h, --help Show this help and quit\n");
9009cc24 1630 fprintf(fp, "\n");
ec2c5e50 1631 fprintf(fp, "See `babeltrace2 --help` for the list of general options.\n");
9009cc24 1632 fprintf(fp, "\n");
ec2c5e50 1633 fprintf(fp, "Use `babeltrace2 help` to get help for a specific plugin or component class.\n");
9009cc24
PP
1634}
1635
1636static
96d065ef
SM
1637const struct bt_argpar_opt_descr list_plugins_options[] = {
1638 /* id, short_name, long_name, with_arg */
1639 { OPT_HELP, 'h', "help", false },
96d065ef 1640 BT_ARGPAR_OPT_DESCR_SENTINEL
9009cc24
PP
1641};
1642
1643/*
1644 * Creates a Babeltrace config object from the arguments of a
1645 * list-plugins command.
1646 *
1647 * *retcode is set to the appropriate exit code to use.
1648 */
1649static
1650struct bt_config *bt_config_list_plugins_from_args(int argc, const char *argv[],
02510056 1651 int *retcode, const bt_value *plugin_paths)
9009cc24 1652{
9009cc24 1653 struct bt_config *cfg = NULL;
96d065ef 1654 struct bt_argpar_parse_ret argpar_parse_ret = { 0 };
9009cc24
PP
1655
1656 *retcode = 0;
02510056 1657 cfg = bt_config_list_plugins_create(plugin_paths);
9009cc24
PP
1658 if (!cfg) {
1659 goto error;
1660 }
1661
9009cc24 1662 /* Parse options */
96d065ef
SM
1663 argpar_parse_ret = bt_argpar_parse(argc, argv, list_plugins_options, true);
1664 if (argpar_parse_ret.error) {
1665 BT_CLI_LOGE_APPEND_CAUSE(
1666 "While parsing `list-plugins` command's command-line arguments: %s",
1667 argpar_parse_ret.error->str);
9009cc24
PP
1668 goto error;
1669 }
1670
96d065ef
SM
1671 if (help_option_is_specified(&argpar_parse_ret)) {
1672 print_list_plugins_usage(stdout);
1673 *retcode = -1;
1674 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1675 goto end;
1676 }
9009cc24 1677
02510056
PP
1678 if (argpar_parse_ret.items->len > 0) {
1679 /*
1680 * At this point we know there's at least one non-option
1681 * argument because we don't reach here with `--help`,
1682 * the only option.
1683 */
1684 struct bt_argpar_item_non_opt *non_opt =
1685 argpar_parse_ret.items->pdata[0];
9009cc24 1686
02510056
PP
1687 BT_CLI_LOGE_APPEND_CAUSE(
1688 "Extraneous command-line argument specified to `list-plugins` command: `%s`.",
1689 non_opt->arg);
9009cc24
PP
1690 goto error;
1691 }
1692
1693 goto end;
1694
1695error:
1696 *retcode = 1;
65300d60 1697 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
1698
1699end:
96d065ef 1700 bt_argpar_parse_ret_fini(&argpar_parse_ret);
9009cc24 1701
9009cc24
PP
1702 return cfg;
1703}
1704
1705/*
1706 * Prints the run command usage.
1707 */
1708static
1709void print_run_usage(FILE *fp)
1710{
ec2c5e50 1711 fprintf(fp, "Usage: babeltrace2 [GENERAL OPTIONS] run [OPTIONS]\n");
9009cc24
PP
1712 fprintf(fp, "\n");
1713 fprintf(fp, "Options:\n");
1714 fprintf(fp, "\n");
1715 fprintf(fp, " -b, --base-params=PARAMS Set PARAMS as the current base parameters\n");
1716 fprintf(fp, " for all the following components until\n");
1717 fprintf(fp, " --reset-base-params is encountered\n");
1718 fprintf(fp, " (see the expected format of PARAMS below)\n");
505d9178 1719 fprintf(fp, " -c, --component=NAME:TYPE.PLUGIN.CLS\n");
fd5f8053
PP
1720 fprintf(fp, " Instantiate the component class CLS of type\n");
1721 fprintf(fp, " TYPE (`source`, `filter`, or `sink`) found\n");
1722 fprintf(fp, " in the plugin PLUGIN, add it to the graph,\n");
505d9178 1723 fprintf(fp, " and name it NAME");
b87236ec 1724 fprintf(fp, " -x, --connect=CONNECTION Connect two created components (see the\n");
9009cc24 1725 fprintf(fp, " expected format of CONNECTION below)\n");
29da2ffc 1726 fprintf(fp, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
20931d22 1727 fprintf(fp, " (`N`, `T`, `D`, `I`, `W`, `E`, or `F`)\n");
9009cc24
PP
1728 fprintf(fp, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
1729 fprintf(fp, " current component (see the expected format\n");
1730 fprintf(fp, " of PARAMS below)\n");
9009cc24
PP
1731 fprintf(fp, " -r, --reset-base-params Reset the current base parameters to an\n");
1732 fprintf(fp, " empty map\n");
ec2c5e50 1733 fprintf(fp, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
9009cc24
PP
1734 fprintf(fp, " the graph later, retry in DUR µs\n");
1735 fprintf(fp, " (default: 100000)\n");
3efa3052 1736 fprintf(fp, " -h, --help Show this help and quit\n");
9009cc24 1737 fprintf(fp, "\n");
ec2c5e50 1738 fprintf(fp, "See `babeltrace2 --help` for the list of general options.\n");
9009cc24
PP
1739 fprintf(fp, "\n\n");
1740 fprintf(fp, "Expected format of CONNECTION\n");
1741 fprintf(fp, "-----------------------------\n");
1742 fprintf(fp, "\n");
1743 fprintf(fp, " UPSTREAM[.UPSTREAM-PORT]:DOWNSTREAM[.DOWNSTREAM-PORT]\n");
1744 fprintf(fp, "\n");
1745 fprintf(fp, "UPSTREAM and DOWNSTREAM are names of the upstream and downstream\n");
1746 fprintf(fp, "components to connect together. You must escape the following characters\n\n");
505d9178
SM
1747 fprintf(fp, "with `\\`: `\\`, `.`, and `:`. You must set the name of the current\n");
1748 fprintf(fp, "component using the NAME prefix of the --component option.\n");
9009cc24
PP
1749 fprintf(fp, "\n");
1750 fprintf(fp, "UPSTREAM-PORT and DOWNSTREAM-PORT are optional globbing patterns to\n");
1751 fprintf(fp, "identify the upstream and downstream ports to use for the connection.\n");
1752 fprintf(fp, "When the port is not specified, `*` is used.\n");
1753 fprintf(fp, "\n");
1754 fprintf(fp, "When a component named UPSTREAM has an available port which matches the\n");
1755 fprintf(fp, "UPSTREAM-PORT globbing pattern, it is connected to the first port which\n");
1756 fprintf(fp, "matches the DOWNSTREAM-PORT globbing pattern of the component named\n");
1757 fprintf(fp, "DOWNSTREAM.\n");
1758 fprintf(fp, "\n");
1759 fprintf(fp, "The only special character in UPSTREAM-PORT and DOWNSTREAM-PORT is `*`\n");
1760 fprintf(fp, "which matches anything. You must escape the following characters\n");
1761 fprintf(fp, "with `\\`: `\\`, `*`, `?`, `[`, `.`, and `:`.\n");
1762 fprintf(fp, "\n");
1763 fprintf(fp, "You can connect a source component to a filter or sink component. You\n");
1764 fprintf(fp, "can connect a filter component to a sink component.\n");
1765 fprintf(fp, "\n");
1766 fprintf(fp, "Examples:\n");
1767 fprintf(fp, "\n");
1768 fprintf(fp, " my-src:my-sink\n");
1769 fprintf(fp, " ctf-fs.*stream*:utils-muxer:*\n");
1770 fprintf(fp, "\n");
1771 fprintf(fp, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
ec2c5e50 1772 fprintf(fp, "babeltrace2 from a shell.\n");
9009cc24
PP
1773 fprintf(fp, "\n\n");
1774 print_expected_params_format(fp);
1775}
1776
1777/*
1778 * Creates a Babeltrace config object from the arguments of a run
1779 * command.
1780 *
1781 * *retcode is set to the appropriate exit code to use.
1782 */
1783static
1784struct bt_config *bt_config_run_from_args(int argc, const char *argv[],
02510056
PP
1785 int *retcode, const bt_value *plugin_paths,
1786 int default_log_level)
9009cc24 1787{
9009cc24 1788 struct bt_config_component *cur_cfg_comp = NULL;
b19ff26f 1789 bt_value *cur_base_params = NULL;
976b24cf 1790 int ret = 0;
9009cc24 1791 struct bt_config *cfg = NULL;
b19ff26f
PP
1792 bt_value *instance_names = NULL;
1793 bt_value *connection_args = NULL;
9009cc24 1794 char error_buf[256] = { 0 };
c0521175 1795 long retry_duration = -1;
d24d5663 1796 bt_value_map_extend_status extend_status;
8bd04432 1797 GString *error_str = NULL;
976b24cf
SM
1798 struct bt_argpar_parse_ret argpar_parse_ret = { 0 };
1799 int i;
1800
1801 static const struct bt_argpar_opt_descr run_options[] = {
1802 { OPT_BASE_PARAMS, 'b', "base-params", true },
1803 { OPT_COMPONENT, 'c', "component", true },
1804 { OPT_CONNECT, 'x', "connect", true },
1805 { OPT_HELP, 'h', "help", false },
1806 { OPT_LOG_LEVEL, 'l', "log-level", true },
976b24cf 1807 { OPT_PARAMS, 'p', "params", true },
976b24cf
SM
1808 { OPT_RESET_BASE_PARAMS, 'r', "reset-base-params", false },
1809 { OPT_RETRY_DURATION, '\0', "retry-duration", true },
1810 BT_ARGPAR_OPT_DESCR_SENTINEL
9009cc24
PP
1811 };
1812
1813 *retcode = 0;
9009cc24 1814
8bd04432
PP
1815 error_str = g_string_new(NULL);
1816 if (!error_str) {
9f901451 1817 BT_CLI_LOGE_APPEND_CAUSE_OOM();
8bd04432
PP
1818 goto error;
1819 }
1820
0e497e1c 1821 if (argc < 1) {
9009cc24
PP
1822 print_run_usage(stdout);
1823 *retcode = -1;
1824 goto end;
1825 }
1826
02510056 1827 cfg = bt_config_run_create(plugin_paths);
9009cc24
PP
1828 if (!cfg) {
1829 goto error;
1830 }
1831
1832 cfg->cmd_data.run.retry_duration_us = 100000;
05e21286 1833 cur_base_params = bt_value_map_create();
9009cc24 1834 if (!cur_base_params) {
9f901451 1835 BT_CLI_LOGE_APPEND_CAUSE_OOM();
9009cc24
PP
1836 goto error;
1837 }
1838
05e21286 1839 instance_names = bt_value_map_create();
9009cc24 1840 if (!instance_names) {
9f901451 1841 BT_CLI_LOGE_APPEND_CAUSE_OOM();
9009cc24
PP
1842 goto error;
1843 }
1844
05e21286 1845 connection_args = bt_value_array_create();
9009cc24 1846 if (!connection_args) {
9f901451 1847 BT_CLI_LOGE_APPEND_CAUSE_OOM();
9009cc24
PP
1848 goto error;
1849 }
1850
9009cc24 1851 /* Parse options */
976b24cf
SM
1852 argpar_parse_ret = bt_argpar_parse(argc, argv, run_options, true);
1853 if (argpar_parse_ret.error) {
1854 BT_CLI_LOGE_APPEND_CAUSE(
1855 "While parsing `run` command's command-line arguments: %s",
1856 argpar_parse_ret.error->str);
9009cc24
PP
1857 goto error;
1858 }
1859
976b24cf
SM
1860 if (help_option_is_specified(&argpar_parse_ret)) {
1861 print_run_usage(stdout);
1862 *retcode = -1;
1863 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1864 goto end;
1865 }
9009cc24 1866
976b24cf
SM
1867 for (i = 0; i < argpar_parse_ret.items->len; i++) {
1868 struct bt_argpar_item *argpar_item =
1869 g_ptr_array_index(argpar_parse_ret.items, i);
1870 struct bt_argpar_item_opt *argpar_item_opt;
1871 const char *arg;
9009cc24 1872
24594611 1873 /* This command does not accept non-option arguments.*/
976b24cf
SM
1874 if (argpar_item->type == BT_ARGPAR_ITEM_TYPE_NON_OPT) {
1875 struct bt_argpar_item_non_opt *argpar_nonopt_item =
1876 (struct bt_argpar_item_non_opt *) argpar_item;
1877
1878 BT_CLI_LOGE_APPEND_CAUSE("Unexpected argument: `%s`",
1879 argpar_nonopt_item->arg);
1880 goto error;
1881 }
1882
1883 argpar_item_opt = (struct bt_argpar_item_opt *) argpar_item;
1884 arg = argpar_item_opt->arg;
1885
1886 switch (argpar_item_opt->descr->id) {
fd5f8053 1887 case OPT_COMPONENT:
9009cc24 1888 {
505d9178 1889 enum bt_config_component_dest dest;
9009cc24 1890
505d9178 1891 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp);
29da2ffc
PP
1892 cur_cfg_comp = bt_config_component_from_arg(arg,
1893 default_log_level);
9009cc24 1894 if (!cur_cfg_comp) {
9f901451 1895 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --component option's argument:\n %s",
fd5f8053 1896 arg);
9009cc24
PP
1897 goto error;
1898 }
1899
fd5f8053
PP
1900 switch (cur_cfg_comp->type) {
1901 case BT_COMPONENT_CLASS_TYPE_SOURCE:
505d9178 1902 dest = BT_CONFIG_COMPONENT_DEST_SOURCE;
fd5f8053
PP
1903 break;
1904 case BT_COMPONENT_CLASS_TYPE_FILTER:
505d9178 1905 dest = BT_CONFIG_COMPONENT_DEST_FILTER;
fd5f8053
PP
1906 break;
1907 case BT_COMPONENT_CLASS_TYPE_SINK:
505d9178 1908 dest = BT_CONFIG_COMPONENT_DEST_SINK;
fd5f8053
PP
1909 break;
1910 default:
1911 abort();
1912 }
1913
f6ccaed9 1914 BT_ASSERT(cur_base_params);
c5b9b441 1915 bt_value_put_ref(cur_cfg_comp->params);
d24d5663
PP
1916 if (bt_value_copy(cur_base_params,
1917 &cur_cfg_comp->params) < 0) {
9f901451 1918 BT_CLI_LOGE_APPEND_CAUSE_OOM();
9009cc24
PP
1919 goto error;
1920 }
1921
505d9178
SM
1922 ret = add_run_cfg_comp_check_name(cfg,
1923 cur_cfg_comp, dest,
1924 instance_names);
1925 if (ret) {
1926 goto error;
1927 }
1928
9009cc24
PP
1929 break;
1930 }
1931 case OPT_PARAMS:
1932 {
b19ff26f
PP
1933 bt_value *params;
1934 bt_value *params_to_set;
9009cc24
PP
1935
1936 if (!cur_cfg_comp) {
9f901451 1937 BT_CLI_LOGE_APPEND_CAUSE("Cannot add parameters to unavailable component:\n %s",
9009cc24
PP
1938 arg);
1939 goto error;
1940 }
1941
8bd04432 1942 params = cli_value_from_arg(arg, error_str);
9009cc24 1943 if (!params) {
9f901451 1944 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
8bd04432 1945 error_str->str);
9009cc24
PP
1946 goto error;
1947 }
1948
d24d5663
PP
1949 extend_status = bt_value_map_extend(
1950 cur_cfg_comp->params, params, &params_to_set);
c5b9b441 1951 BT_VALUE_PUT_REF_AND_RESET(params);
d24d5663 1952 if (extend_status != BT_VALUE_MAP_EXTEND_STATUS_OK) {
9f901451 1953 BT_CLI_LOGE_APPEND_CAUSE("Cannot extend current component parameters with --params option's argument:\n %s",
9009cc24
PP
1954 arg);
1955 goto error;
1956 }
1957
65300d60 1958 BT_OBJECT_MOVE_REF(cur_cfg_comp->params, params_to_set);
9009cc24
PP
1959 break;
1960 }
29da2ffc
PP
1961 case OPT_LOG_LEVEL:
1962 if (!cur_cfg_comp) {
9f901451 1963 BT_CLI_LOGE_APPEND_CAUSE("Cannot set the log level of unavailable component:\n %s",
29da2ffc
PP
1964 arg);
1965 goto error;
1966 }
1967
1968 cur_cfg_comp->log_level =
1969 bt_log_get_level_from_string(arg);
1970 if (cur_cfg_comp->log_level < 0) {
9f901451 1971 BT_CLI_LOGE_APPEND_CAUSE("Invalid argument for --log-level option:\n %s",
29da2ffc
PP
1972 arg);
1973 goto error;
1974 }
1975 break;
9009cc24
PP
1976 case OPT_BASE_PARAMS:
1977 {
8bd04432 1978 bt_value *params = cli_value_from_arg(arg, error_str);
9009cc24
PP
1979
1980 if (!params) {
9f901451 1981 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --base-params option's argument:\n %s",
8bd04432 1982 error_str->str);
9009cc24
PP
1983 goto error;
1984 }
1985
65300d60 1986 BT_OBJECT_MOVE_REF(cur_base_params, params);
9009cc24
PP
1987 break;
1988 }
1989 case OPT_RESET_BASE_PARAMS:
c5b9b441 1990 BT_VALUE_PUT_REF_AND_RESET(cur_base_params);
05e21286 1991 cur_base_params = bt_value_map_create();
9009cc24 1992 if (!cur_base_params) {
9f901451 1993 BT_CLI_LOGE_APPEND_CAUSE_OOM();
9009cc24
PP
1994 goto error;
1995 }
1996 break;
1997 case OPT_CONNECT:
05e21286 1998 if (bt_value_array_append_string_element(
07208d85 1999 connection_args, arg)) {
9f901451 2000 BT_CLI_LOGE_APPEND_CAUSE_OOM();
9009cc24
PP
2001 goto error;
2002 }
2003 break;
976b24cf
SM
2004 case OPT_RETRY_DURATION: {
2005 gchar *end;
2006 size_t arg_len = strlen(argpar_item_opt->arg);
2007
2008 retry_duration = g_ascii_strtoll(argpar_item_opt->arg, &end, 10);
2009
2010 if (arg_len == 0 || end != (argpar_item_opt->arg + arg_len)) {
2011 BT_CLI_LOGE_APPEND_CAUSE(
2012 "Could not parse --retry-duration option's argument as an unsigned integer: `%s`",
2013 argpar_item_opt->arg);
2014 goto error;
2015 }
2016
9009cc24 2017 if (retry_duration < 0) {
9f901451 2018 BT_CLI_LOGE_APPEND_CAUSE("--retry-duration option's argument must be positive or 0: %ld",
9009cc24
PP
2019 retry_duration);
2020 goto error;
2021 }
2022
2023 cfg->cmd_data.run.retry_duration_us =
2024 (uint64_t) retry_duration;
2025 break;
976b24cf 2026 }
9009cc24 2027 default:
9f901451 2028 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
976b24cf 2029 argpar_item_opt->descr->id);
9009cc24
PP
2030 goto error;
2031 }
9009cc24
PP
2032 }
2033
505d9178 2034 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp);
9009cc24
PP
2035
2036 if (cfg->cmd_data.run.sources->len == 0) {
9f901451 2037 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no source component.");
9009cc24
PP
2038 goto error;
2039 }
2040
2041 if (cfg->cmd_data.run.sinks->len == 0) {
9f901451 2042 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no sink component.");
9009cc24
PP
2043 goto error;
2044 }
2045
da91b29a 2046 ret = bt_config_cli_args_create_connections(cfg,
05e21286 2047 connection_args,
9009cc24
PP
2048 error_buf, 256);
2049 if (ret) {
9f901451 2050 BT_CLI_LOGE_APPEND_CAUSE("Cannot creation connections:\n%s", error_buf);
9009cc24
PP
2051 goto error;
2052 }
2053
2054 goto end;
2055
2056error:
2057 *retcode = 1;
65300d60 2058 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
2059
2060end:
8bd04432
PP
2061 if (error_str) {
2062 g_string_free(error_str, TRUE);
2063 }
2064
976b24cf 2065 bt_argpar_parse_ret_fini(&argpar_parse_ret);
65300d60 2066 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp);
c5b9b441
PP
2067 BT_VALUE_PUT_REF_AND_RESET(cur_base_params);
2068 BT_VALUE_PUT_REF_AND_RESET(instance_names);
2069 BT_VALUE_PUT_REF_AND_RESET(connection_args);
9009cc24
PP
2070 return cfg;
2071}
2072
2073static
b19ff26f 2074struct bt_config *bt_config_run_from_args_array(const bt_value *run_args,
02510056
PP
2075 int *retcode, const bt_value *plugin_paths,
2076 int default_log_level)
9009cc24
PP
2077{
2078 struct bt_config *cfg = NULL;
2079 const char **argv;
0ca8409d 2080 int64_t i, len;
393729a6 2081 const size_t argc = bt_value_array_get_length(run_args);
9009cc24
PP
2082
2083 argv = calloc(argc, sizeof(*argv));
2084 if (!argv) {
9f901451 2085 BT_CLI_LOGE_APPEND_CAUSE_OOM();
9009cc24
PP
2086 goto end;
2087 }
2088
393729a6 2089 len = bt_value_array_get_length(run_args);
0ca8409d 2090 if (len < 0) {
9f901451 2091 BT_CLI_LOGE_APPEND_CAUSE("Invalid executable arguments.");
0ca8409d
MD
2092 goto end;
2093 }
2094 for (i = 0; i < len; i++) {
b19ff26f 2095 const bt_value *arg_value =
05e21286
PP
2096 bt_value_array_borrow_element_by_index_const(run_args,
2097 i);
9009cc24
PP
2098 const char *arg;
2099
f6ccaed9 2100 BT_ASSERT(arg_value);
601b0d3c 2101 arg = bt_value_string_get(arg_value);
f6ccaed9 2102 BT_ASSERT(arg);
0e497e1c 2103 argv[i] = arg;
9009cc24
PP
2104 }
2105
2106 cfg = bt_config_run_from_args(argc, argv, retcode,
02510056 2107 plugin_paths, default_log_level);
9009cc24
PP
2108
2109end:
2110 free(argv);
2111 return cfg;
2112}
2113
2114/*
2115 * Prints the convert command usage.
2116 */
2117static
2118void print_convert_usage(FILE *fp)
2119{
ec2c5e50 2120 fprintf(fp, "Usage: babeltrace2 [GENERAL OPTIONS] [convert] [OPTIONS] [PATH/URL]\n");
9009cc24
PP
2121 fprintf(fp, "\n");
2122 fprintf(fp, "Options:\n");
2123 fprintf(fp, "\n");
fd5f8053
PP
2124 fprintf(fp, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2125 fprintf(fp, " Instantiate the component class CLS of type\n");
2126 fprintf(fp, " TYPE (`source`, `filter`, or `sink`) found\n");
2127 fprintf(fp, " in the plugin PLUGIN, add it to the\n");
2128 fprintf(fp, " conversion graph, and optionally name it\n");
505d9178 2129 fprintf(fp, " NAME\n");
29da2ffc 2130 fprintf(fp, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
20931d22 2131 fprintf(fp, " (`N`, `T`, `D`, `I`, `W`, `E`, or `F`)\n");
9009cc24
PP
2132 fprintf(fp, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2133 fprintf(fp, " current component (see the expected format\n");
2134 fprintf(fp, " of PARAMS below)\n");
ec2c5e50 2135 fprintf(fp, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
9009cc24
PP
2136 fprintf(fp, " the graph later, retry in DUR µs\n");
2137 fprintf(fp, " (default: 100000)\n");
2138 fprintf(fp, " dynamic plugins can be loaded\n");
2139 fprintf(fp, " --run-args Print the equivalent arguments for the\n");
2140 fprintf(fp, " `run` command to the standard output,\n");
2141 fprintf(fp, " formatted for a shell, and quit\n");
2142 fprintf(fp, " --run-args-0 Print the equivalent arguments for the\n");
2143 fprintf(fp, " `run` command to the standard output,\n");
2144 fprintf(fp, " formatted for `xargs -0`, and quit\n");
8ed0bf10
PP
2145 fprintf(fp, " --stream-intersection Only process events when all streams\n");
2146 fprintf(fp, " are active\n");
3efa3052 2147 fprintf(fp, " -h, --help Show this help and quit\n");
9009cc24 2148 fprintf(fp, "\n");
e7ad156c 2149 fprintf(fp, "Implicit `source.ctf.fs` component options:\n");
9009cc24 2150 fprintf(fp, "\n");
9412c556
FD
2151 fprintf(fp, " --clock-force-correlate Force the origin of all clocks\n");
2152 fprintf(fp, " to the Unix epoch\n");
9009cc24
PP
2153 fprintf(fp, " --clock-offset=SEC Set clock offset to SEC seconds\n");
2154 fprintf(fp, " --clock-offset-ns=NS Set clock offset to NS ns\n");
9009cc24 2155 fprintf(fp, "\n");
e7ad156c 2156 fprintf(fp, "Implicit `sink.text.pretty` component options:\n");
9009cc24
PP
2157 fprintf(fp, "\n");
2158 fprintf(fp, " --clock-cycles Print timestamps in clock cycles\n");
2159 fprintf(fp, " --clock-date Print timestamp dates\n");
2160 fprintf(fp, " --clock-gmt Print and parse timestamps in the GMT\n");
2161 fprintf(fp, " time zone instead of the local time zone\n");
2162 fprintf(fp, " --clock-seconds Print the timestamps as `SEC.NS` instead\n");
2163 fprintf(fp, " of `hh:mm:ss.nnnnnnnnn`\n");
2164 fprintf(fp, " --color=(never | auto | always)\n");
2165 fprintf(fp, " Never, automatically, or always emit\n");
2166 fprintf(fp, " console color codes\n");
2167 fprintf(fp, " -f, --fields=FIELD[,FIELD]... Print additional fields; FIELD can be:\n");
2168 fprintf(fp, " `all`, `trace`, `trace:hostname`,\n");
2169 fprintf(fp, " `trace:domain`, `trace:procname`,\n");
2170 fprintf(fp, " `trace:vpid`, `loglevel`, `emf`\n");
2171 fprintf(fp, " -n, --names=NAME[,NAME]... Print field names; NAME can be:\n");
2172 fprintf(fp, " `payload` (or `arg` or `args`), `none`,\n");
2173 fprintf(fp, " `all`, `scope`, `header`, `context`\n");
2174 fprintf(fp, " (or `ctx`)\n");
2175 fprintf(fp, " --no-delta Do not print time delta between\n");
2176 fprintf(fp, " consecutive events\n");
2177 fprintf(fp, " -w, --output=PATH Write output text to PATH instead of\n");
2178 fprintf(fp, " the standard output\n");
2179 fprintf(fp, "\n");
e7ad156c 2180 fprintf(fp, "Implicit `filter.utils.trimmer` component options:\n");
9009cc24
PP
2181 fprintf(fp, "\n");
2182 fprintf(fp, " -b, --begin=BEGIN Set the beginning time of the conversion\n");
2183 fprintf(fp, " time range to BEGIN (see the format of\n");
2184 fprintf(fp, " BEGIN below)\n");
2185 fprintf(fp, " -e, --end=END Set the end time of the conversion time\n");
2186 fprintf(fp, " range to END (see the format of END below)\n");
2187 fprintf(fp, " -t, --timerange=TIMERANGE Set conversion time range to TIMERANGE:\n");
2188 fprintf(fp, " BEGIN,END or [BEGIN,END] (literally `[` and\n");
2189 fprintf(fp, " `]`) (see the format of BEGIN/END below)\n");
2190 fprintf(fp, "\n");
e7ad156c 2191 fprintf(fp, "Implicit `filter.lttng-utils.debug-info` component options:\n");
9009cc24 2192 fprintf(fp, "\n");
9a16feea
PP
2193 fprintf(fp, " --debug-info Create an implicit\n");
2194 fprintf(fp, " `filter.lttng-utils.debug-info` component\n");
9009cc24
PP
2195 fprintf(fp, " --debug-info-dir=DIR Search for debug info in directory DIR\n");
2196 fprintf(fp, " instead of `/usr/lib/debug`\n");
2197 fprintf(fp, " --debug-info-full-path Show full debug info source and\n");
2198 fprintf(fp, " binary paths instead of just names\n");
2199 fprintf(fp, " --debug-info-target-prefix=DIR\n");
2200 fprintf(fp, " Use directory DIR as a prefix when\n");
2201 fprintf(fp, " looking up executables during debug\n");
2202 fprintf(fp, " info analysis\n");
9009cc24
PP
2203 fprintf(fp, "\n");
2204 fprintf(fp, "Legacy options that still work:\n");
2205 fprintf(fp, "\n");
2206 fprintf(fp, " -i, --input-format=(ctf | lttng-live)\n");
2207 fprintf(fp, " `ctf`:\n");
fd5f8053 2208 fprintf(fp, " Create an implicit `source.ctf.fs`\n");
9009cc24
PP
2209 fprintf(fp, " component\n");
2210 fprintf(fp, " `lttng-live`:\n");
fd5f8053
PP
2211 fprintf(fp, " Create an implicit `source.ctf.lttng-live`\n");
2212 fprintf(fp, " component\n");
e7ad156c 2213 fprintf(fp, " -o, --output-format=(text | ctf | dummy | ctf-metadata)\n");
9009cc24 2214 fprintf(fp, " `text`:\n");
fd5f8053 2215 fprintf(fp, " Create an implicit `sink.text.pretty`\n");
9009cc24 2216 fprintf(fp, " component\n");
dd46f7ea 2217 fprintf(fp, " `ctf`:\n");
e7ad156c
PP
2218 fprintf(fp, " Create an implicit `sink.ctf.fs`\n");
2219 fprintf(fp, " component\n");
9009cc24 2220 fprintf(fp, " `dummy`:\n");
fd5f8053 2221 fprintf(fp, " Create an implicit `sink.utils.dummy`\n");
9009cc24
PP
2222 fprintf(fp, " component\n");
2223 fprintf(fp, " `ctf-metadata`:\n");
fd5f8053
PP
2224 fprintf(fp, " Query the `source.ctf.fs` component class\n");
2225 fprintf(fp, " for metadata text and quit\n");
9009cc24 2226 fprintf(fp, "\n");
ec2c5e50 2227 fprintf(fp, "See `babeltrace2 --help` for the list of general options.\n");
9009cc24
PP
2228 fprintf(fp, "\n\n");
2229 fprintf(fp, "Format of BEGIN and END\n");
2230 fprintf(fp, "-----------------------\n");
2231 fprintf(fp, "\n");
2232 fprintf(fp, " [YYYY-MM-DD [hh:mm:]]ss[.nnnnnnnnn]\n");
2233 fprintf(fp, "\n\n");
2234 print_expected_params_format(fp);
2235}
2236
2237static
ca60bbc3
SM
2238const struct bt_argpar_opt_descr convert_options[] = {
2239 /* id, short_name, long_name, with_arg */
2240 { OPT_BEGIN, 'b', "begin", true },
2241 { OPT_CLOCK_CYCLES, '\0', "clock-cycles", false },
2242 { OPT_CLOCK_DATE, '\0', "clock-date", false },
2243 { OPT_CLOCK_FORCE_CORRELATE, '\0', "clock-force-correlate", false },
2244 { OPT_CLOCK_GMT, '\0', "clock-gmt", false },
2245 { OPT_CLOCK_OFFSET, '\0', "clock-offset", true },
2246 { OPT_CLOCK_OFFSET_NS, '\0', "clock-offset-ns", true },
2247 { OPT_CLOCK_SECONDS, '\0', "clock-seconds", false },
2248 { OPT_COLOR, '\0', "color", true },
2249 { OPT_COMPONENT, 'c', "component", true },
2250 { OPT_DEBUG, 'd', "debug", false },
2251 { OPT_DEBUG_INFO_DIR, '\0', "debug-info-dir", true },
2252 { OPT_DEBUG_INFO_FULL_PATH, '\0', "debug-info-full-path", false },
2253 { OPT_DEBUG_INFO_TARGET_PREFIX, '\0', "debug-info-target-prefix", true },
2254 { OPT_END, 'e', "end", true },
2255 { OPT_FIELDS, 'f', "fields", true },
2256 { OPT_HELP, 'h', "help", false },
2257 { OPT_INPUT_FORMAT, 'i', "input-format", true },
2258 { OPT_LOG_LEVEL, 'l', "log-level", true },
ca60bbc3
SM
2259 { OPT_NAMES, 'n', "names", true },
2260 { OPT_DEBUG_INFO, '\0', "debug-info", false },
2261 { OPT_NO_DELTA, '\0', "no-delta", false },
2262 { OPT_OMIT_HOME_PLUGIN_PATH, '\0', "omit-home-plugin-path", false },
2263 { OPT_OMIT_SYSTEM_PLUGIN_PATH, '\0', "omit-system-plugin-path", false },
2264 { OPT_OUTPUT, 'w', "output", true },
2265 { OPT_OUTPUT_FORMAT, 'o', "output-format", true },
2266 { OPT_PARAMS, 'p', "params", true },
ca60bbc3
SM
2267 { OPT_PLUGIN_PATH, '\0', "plugin-path", true },
2268 { OPT_RETRY_DURATION, '\0', "retry-duration", true },
2269 { OPT_RUN_ARGS, '\0', "run-args", false },
2270 { OPT_RUN_ARGS_0, '\0', "run-args-0", false },
2271 { OPT_STREAM_INTERSECTION, '\0', "stream-intersection", false },
2272 { OPT_TIMERANGE, '\0', "timerange", true },
ca60bbc3
SM
2273 { OPT_VERBOSE, 'v', "verbose", false },
2274 BT_ARGPAR_OPT_DESCR_SENTINEL
9009cc24
PP
2275};
2276
2277static
2278GString *get_component_auto_name(const char *prefix,
b19ff26f 2279 const bt_value *existing_names)
9009cc24
PP
2280{
2281 unsigned int i = 0;
2282 GString *auto_name = g_string_new(NULL);
2283
2284 if (!auto_name) {
9f901451 2285 BT_CLI_LOGE_APPEND_CAUSE_OOM();
9009cc24
PP
2286 goto end;
2287 }
2288
07208d85 2289 if (!bt_value_map_has_entry(existing_names, prefix)) {
9009cc24
PP
2290 g_string_assign(auto_name, prefix);
2291 goto end;
2292 }
2293
2294 do {
2295 g_string_printf(auto_name, "%s-%d", prefix, i);
2296 i++;
07208d85 2297 } while (bt_value_map_has_entry(existing_names, auto_name->str));
9009cc24
PP
2298
2299end:
2300 return auto_name;
2301}
2302
2303struct implicit_component_args {
2304 bool exists;
73760435
SM
2305
2306 /* The component class name (e.g. src.ctf.fs). */
fd5f8053 2307 GString *comp_arg;
73760435
SM
2308
2309 /* The component instance name. */
9009cc24 2310 GString *name_arg;
73760435 2311
9009cc24 2312 GString *params_arg;
b19ff26f 2313 bt_value *extra_params;
9009cc24
PP
2314};
2315
2316static
2317int assign_name_to_implicit_component(struct implicit_component_args *args,
b19ff26f 2318 const char *prefix, bt_value *existing_names,
9009cc24
PP
2319 GList **comp_names, bool append_to_comp_names)
2320{
2321 int ret = 0;
2322 GString *name = NULL;
2323
2324 if (!args->exists) {
2325 goto end;
2326 }
2327
da91b29a 2328 name = get_component_auto_name(prefix,
05e21286 2329 existing_names);
9009cc24
PP
2330
2331 if (!name) {
2332 ret = -1;
2333 goto end;
2334 }
2335
2336 g_string_assign(args->name_arg, name->str);
2337
05e21286 2338 if (bt_value_map_insert_entry(existing_names, name->str,
9009cc24 2339 bt_value_null)) {
9f901451 2340 BT_CLI_LOGE_APPEND_CAUSE_OOM();
9009cc24
PP
2341 ret = -1;
2342 goto end;
2343 }
2344
2345 if (append_to_comp_names) {
2346 *comp_names = g_list_append(*comp_names, name);
2347 name = NULL;
2348 }
2349
2350end:
2351 if (name) {
2352 g_string_free(name, TRUE);
2353 }
2354
2355 return ret;
2356}
2357
2358static
2359int append_run_args_for_implicit_component(
9009cc24 2360 struct implicit_component_args *impl_args,
b19ff26f 2361 bt_value *run_args)
9009cc24
PP
2362{
2363 int ret = 0;
2364 size_t i;
505d9178 2365 GString *component_arg_for_run = NULL;
9009cc24
PP
2366
2367 if (!impl_args->exists) {
2368 goto end;
2369 }
2370
505d9178
SM
2371 component_arg_for_run = g_string_new(NULL);
2372 if (!component_arg_for_run) {
9f901451 2373 BT_CLI_LOGE_APPEND_CAUSE_OOM();
fd5f8053 2374 goto error;
9009cc24
PP
2375 }
2376
505d9178
SM
2377 /* Build the full `name:type.plugin.cls`. */
2378 BT_ASSERT(!strchr(impl_args->name_arg->str, '\\'));
2379 BT_ASSERT(!strchr(impl_args->name_arg->str, ':'));
2380 g_string_printf(component_arg_for_run, "%s:%s",
2381 impl_args->name_arg->str, impl_args->comp_arg->str);
9009cc24 2382
505d9178 2383 if (bt_value_array_append_string_element(run_args, "--component")) {
9f901451 2384 BT_CLI_LOGE_APPEND_CAUSE_OOM();
9009cc24
PP
2385 goto error;
2386 }
2387
505d9178
SM
2388 if (bt_value_array_append_string_element(run_args,
2389 component_arg_for_run->str)) {
9f901451 2390 BT_CLI_LOGE_APPEND_CAUSE_OOM();
9009cc24
PP
2391 goto error;
2392 }
2393
2394 if (impl_args->params_arg->len > 0) {
05e21286 2395 if (bt_value_array_append_string_element(run_args, "--params")) {
9f901451 2396 BT_CLI_LOGE_APPEND_CAUSE_OOM();
9009cc24
PP
2397 goto error;
2398 }
2399
05e21286 2400 if (bt_value_array_append_string_element(run_args,
9009cc24 2401 impl_args->params_arg->str)) {
9f901451 2402 BT_CLI_LOGE_APPEND_CAUSE_OOM();
9009cc24
PP
2403 goto error;
2404 }
2405 }
2406
393729a6 2407 for (i = 0; i < bt_value_array_get_length(impl_args->extra_params);
da91b29a 2408 i++) {
b19ff26f 2409 const bt_value *elem;
9009cc24
PP
2410 const char *arg;
2411
05e21286
PP
2412 elem = bt_value_array_borrow_element_by_index(impl_args->extra_params,
2413 i);
9009cc24
PP
2414 if (!elem) {
2415 goto error;
2416 }
2417
f6ccaed9 2418 BT_ASSERT(bt_value_is_string(elem));
601b0d3c 2419 arg = bt_value_string_get(elem);
05e21286 2420 ret = bt_value_array_append_string_element(run_args, arg);
9009cc24 2421 if (ret) {
9f901451 2422 BT_CLI_LOGE_APPEND_CAUSE_OOM();
9009cc24
PP
2423 goto error;
2424 }
2425 }
2426
2427 goto end;
2428
2429error:
2430 ret = -1;
2431
2432end:
505d9178
SM
2433 if (component_arg_for_run) {
2434 g_string_free(component_arg_for_run, TRUE);
2435 }
2436
9009cc24
PP
2437 return ret;
2438}
2439
73760435
SM
2440/* Free the fields of a `struct implicit_component_args`. */
2441
9009cc24 2442static
94023a1c 2443void finalize_implicit_component_args(struct implicit_component_args *args)
9009cc24 2444{
f6ccaed9 2445 BT_ASSERT(args);
9009cc24 2446
fd5f8053
PP
2447 if (args->comp_arg) {
2448 g_string_free(args->comp_arg, TRUE);
9009cc24
PP
2449 }
2450
2451 if (args->name_arg) {
2452 g_string_free(args->name_arg, TRUE);
2453 }
2454
2455 if (args->params_arg) {
2456 g_string_free(args->params_arg, TRUE);
2457 }
2458
c5b9b441 2459 bt_value_put_ref(args->extra_params);
9009cc24
PP
2460}
2461
73760435
SM
2462/* Destroy a dynamically-allocated `struct implicit_component_args`. */
2463
2464static
2465void destroy_implicit_component_args(struct implicit_component_args *args)
2466{
2467 finalize_implicit_component_args(args);
2468 g_free(args);
2469}
2470
2471/* Initialize the fields of an already allocated `struct implicit_component_args`. */
2472
9009cc24
PP
2473static
2474int init_implicit_component_args(struct implicit_component_args *args,
fd5f8053 2475 const char *comp_arg, bool exists)
9009cc24
PP
2476{
2477 int ret = 0;
2478
2479 args->exists = exists;
fd5f8053 2480 args->comp_arg = g_string_new(comp_arg);
9009cc24
PP
2481 args->name_arg = g_string_new(NULL);
2482 args->params_arg = g_string_new(NULL);
05e21286 2483 args->extra_params = bt_value_array_create();
9009cc24 2484
fd5f8053 2485 if (!args->comp_arg || !args->name_arg ||
e7ad156c 2486 !args->params_arg || !args->extra_params) {
9009cc24 2487 ret = -1;
94023a1c 2488 finalize_implicit_component_args(args);
9f901451 2489 BT_CLI_LOGE_APPEND_CAUSE_OOM();
9009cc24
PP
2490 goto end;
2491 }
2492
2493end:
2494 return ret;
2495}
2496
73760435
SM
2497/* Dynamically allocate and initialize a `struct implicit_component_args`. */
2498
2499static
2500struct implicit_component_args *create_implicit_component_args(
2501 const char *comp_arg)
2502{
2503 struct implicit_component_args *args;
2504 int status;
2505
2506 args = g_new(struct implicit_component_args, 1);
2507 if (!args) {
2508 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2509 goto end;
2510 }
2511
2512 status = init_implicit_component_args(args, comp_arg, true);
2513 if (status != 0) {
2514 g_free(args);
2515 args = NULL;
2516 }
2517
2518end:
2519 return args;
2520}
2521
9009cc24
PP
2522static
2523void append_implicit_component_param(struct implicit_component_args *args,
2524 const char *key, const char *value)
2525{
f6ccaed9
PP
2526 BT_ASSERT(args);
2527 BT_ASSERT(key);
2528 BT_ASSERT(value);
9009cc24
PP
2529 append_param_arg(args->params_arg, key, value);
2530}
2531
73760435
SM
2532/*
2533 * Append the given parameter (`key=value`) to all component specifications
2534 * in `implicit_comp_args` (an array of `struct implicit_component_args *`)
2535 * which match `comp_arg`.
2536 *
2537 * Return the number of matching components.
2538 */
2539
2540static
2541int append_multiple_implicit_components_param(GPtrArray *implicit_comp_args,
2542 const char *comp_arg, const char *key, const char *value)
2543{
2544 int i;
2545 int n = 0;
2546
2547 for (i = 0; i < implicit_comp_args->len; i++) {
2548 struct implicit_component_args *args = implicit_comp_args->pdata[i];
2549
2550 if (strcmp(args->comp_arg->str, comp_arg) == 0) {
2551 append_implicit_component_param(args, key, value);
2552 n++;
2553 }
2554 }
2555
2556 return n;
2557}
2558
c7b0cd78 2559/* Escape value to make it suitable to use as a string parameter value. */
9009cc24 2560static
c7b0cd78 2561gchar *escape_string_value(const char *value)
9009cc24 2562{
c7b0cd78
SM
2563 GString *ret;
2564 const char *in;
2565
2566 ret = g_string_new(NULL);
2567 if (!ret) {
9f901451 2568 BT_CLI_LOGE_APPEND_CAUSE_OOM();
c7b0cd78
SM
2569 goto end;
2570 }
2571
2572 in = value;
2573 while (*in) {
2574 switch (*in) {
2575 case '"':
2576 case '\\':
2577 g_string_append_c(ret, '\\');
2578 break;
2579 }
2580
2581 g_string_append_c(ret, *in);
2582
2583 in++;
2584 }
2585
2586end:
2587 return g_string_free(ret, FALSE);
2588}
9009cc24 2589
77957ab5 2590static
f280892e 2591int bt_value_to_cli_param_value_append(const bt_value *value, GString *buf)
77957ab5 2592{
f280892e 2593 BT_ASSERT(buf);
77957ab5 2594
f280892e 2595 int ret = -1;
77957ab5
SM
2596
2597 switch (bt_value_get_type(value)) {
2598 case BT_VALUE_TYPE_STRING:
2599 {
2600 const char *str_value = bt_value_string_get(value);
2601 gchar *escaped_str_value;
2602
2603 escaped_str_value = escape_string_value(str_value);
2604 if (!escaped_str_value) {
f280892e 2605 goto end;
77957ab5
SM
2606 }
2607
f280892e 2608 g_string_append_printf(buf, "\"%s\"", escaped_str_value);
77957ab5
SM
2609
2610 g_free(escaped_str_value);
2611 break;
2612 }
f280892e
SM
2613 case BT_VALUE_TYPE_ARRAY: {
2614 g_string_append_c(buf, '[');
393729a6 2615 uint64_t sz = bt_value_array_get_length(value);
f280892e 2616 for (uint64_t i = 0; i < sz; i++) {
bea5b968
PP
2617 const bt_value *item;
2618 int ret;
2619
f280892e 2620 if (i > 0) {
bea5b968 2621 g_string_append(buf, ", ");
f280892e 2622 }
bea5b968
PP
2623
2624 item = bt_value_array_borrow_element_by_index_const(
2625 value, i);
2626 ret = bt_value_to_cli_param_value_append(item, buf);
2627
f280892e
SM
2628 if (ret) {
2629 goto end;
2630 }
2631 }
2632 g_string_append_c(buf, ']');
2633 break;
2634 }
77957ab5
SM
2635 default:
2636 abort();
2637 }
2638
f280892e
SM
2639 ret = 0;
2640
2641end:
2642 return ret;
2643}
2644
2645/*
2646 * Convert `value` to its equivalent representation as a command line parameter
2647 * value.
2648 */
2649
2650static
2651gchar *bt_value_to_cli_param_value(bt_value *value)
2652{
2653 GString *buf;
2654 gchar *result = NULL;
2655
2656 buf = g_string_new(NULL);
2657 if (!buf) {
9f901451 2658 BT_CLI_LOGE_APPEND_CAUSE_OOM();
f280892e
SM
2659 goto error;
2660 }
2661
2662 if (bt_value_to_cli_param_value_append(value, buf)) {
2663 goto error;
2664 }
2665
77957ab5
SM
2666 result = g_string_free(buf, FALSE);
2667 buf = NULL;
2668
2669 goto end;
2670
2671error:
2672 if (buf) {
2673 g_string_free(buf, TRUE);
2674 }
2675
2676end:
2677 return result;
2678}
2679
c7b0cd78 2680static
77957ab5 2681int append_parameter_to_args(bt_value *args, const char *key, bt_value *value)
c7b0cd78 2682{
f6ccaed9 2683 BT_ASSERT(args);
c7b0cd78 2684 BT_ASSERT(bt_value_get_type(args) == BT_VALUE_TYPE_ARRAY);
f6ccaed9
PP
2685 BT_ASSERT(key);
2686 BT_ASSERT(value);
9009cc24 2687
c7b0cd78 2688 int ret = 0;
77957ab5 2689 gchar *str_value = NULL;
c7b0cd78
SM
2690 GString *parameter = NULL;
2691
2692 if (bt_value_array_append_string_element(args, "--params")) {
9f901451 2693 BT_CLI_LOGE_APPEND_CAUSE_OOM();
9009cc24
PP
2694 ret = -1;
2695 goto end;
2696 }
2697
77957ab5
SM
2698 str_value = bt_value_to_cli_param_value(value);
2699 if (!str_value) {
9009cc24
PP
2700 ret = -1;
2701 goto end;
2702 }
2703
c7b0cd78
SM
2704 parameter = g_string_new(NULL);
2705 if (!parameter) {
9f901451 2706 BT_CLI_LOGE_APPEND_CAUSE_OOM();
9009cc24
PP
2707 ret = -1;
2708 goto end;
2709 }
2710
77957ab5 2711 g_string_printf(parameter, "%s=%s", key, str_value);
c7b0cd78
SM
2712
2713 if (bt_value_array_append_string_element(args, parameter->str)) {
9f901451 2714 BT_CLI_LOGE_APPEND_CAUSE_OOM();
9009cc24
PP
2715 ret = -1;
2716 goto end;
2717 }
2718
2719end:
c7b0cd78
SM
2720 if (parameter) {
2721 g_string_free(parameter, TRUE);
2722 parameter = NULL;
2723 }
2724
77957ab5
SM
2725 if (str_value) {
2726 g_free(str_value);
2727 str_value = NULL;
2728 }
2729
2730 return ret;
2731}
2732
2733static
2734int append_string_parameter_to_args(bt_value *args, const char *key, const char *value)
2735{
2736 bt_value *str_value;
2737 int ret;
2738
2739 str_value = bt_value_string_create_init(value);
2740
2741 if (!str_value) {
9f901451 2742 BT_CLI_LOGE_APPEND_CAUSE_OOM();
77957ab5
SM
2743 ret = -1;
2744 goto end;
2745 }
2746
2747 ret = append_parameter_to_args(args, key, str_value);
2748
2749end:
2750 BT_VALUE_PUT_REF_AND_RESET(str_value);
9009cc24
PP
2751 return ret;
2752}
2753
c7b0cd78
SM
2754static
2755int append_implicit_component_extra_param(struct implicit_component_args *args,
2756 const char *key, const char *value)
2757{
77957ab5 2758 return append_string_parameter_to_args(args->extra_params, key, value);
c7b0cd78
SM
2759}
2760
9009cc24
PP
2761/*
2762 * Escapes `.`, `:`, and `\` of `input` with `\`.
2763 */
2764static
2765GString *escape_dot_colon(const char *input)
2766{
2767 GString *output = g_string_new(NULL);
2768 const char *ch;
2769
2770 if (!output) {
9f901451 2771 BT_CLI_LOGE_APPEND_CAUSE_OOM();
9009cc24
PP
2772 goto end;
2773 }
2774
2775 for (ch = input; *ch != '\0'; ch++) {
2776 if (*ch == '\\' || *ch == '.' || *ch == ':') {
2777 g_string_append_c(output, '\\');
2778 }
2779
2780 g_string_append_c(output, *ch);
2781 }
2782
2783end:
2784 return output;
2785}
2786
2787/*
2788 * Appends a --connect option to a list of arguments. `upstream_name`
2789 * and `downstream_name` are escaped with escape_dot_colon() in this
2790 * function.
2791 */
2792static
b19ff26f 2793int append_connect_arg(bt_value *run_args,
9009cc24
PP
2794 const char *upstream_name, const char *downstream_name)
2795{
2796 int ret = 0;
2797 GString *e_upstream_name = escape_dot_colon(upstream_name);
2798 GString *e_downstream_name = escape_dot_colon(downstream_name);
2799 GString *arg = g_string_new(NULL);
2800
2801 if (!e_upstream_name || !e_downstream_name || !arg) {
9f901451 2802 BT_CLI_LOGE_APPEND_CAUSE_OOM();
9009cc24
PP
2803 ret = -1;
2804 goto end;
2805 }
2806
05e21286 2807 ret = bt_value_array_append_string_element(run_args, "--connect");
9009cc24 2808 if (ret) {
9f901451 2809 BT_CLI_LOGE_APPEND_CAUSE_OOM();
9009cc24
PP
2810 ret = -1;
2811 goto end;
2812 }
2813
2814 g_string_append(arg, e_upstream_name->str);
2815 g_string_append_c(arg, ':');
2816 g_string_append(arg, e_downstream_name->str);
05e21286 2817 ret = bt_value_array_append_string_element(run_args, arg->str);
9009cc24 2818 if (ret) {
9f901451 2819 BT_CLI_LOGE_APPEND_CAUSE_OOM();
9009cc24
PP
2820 ret = -1;
2821 goto end;
2822 }
2823
2824end:
2825 if (arg) {
2826 g_string_free(arg, TRUE);
2827 }
2828
2829 if (e_upstream_name) {
2830 g_string_free(e_upstream_name, TRUE);
2831 }
2832
2833 if (e_downstream_name) {
2834 g_string_free(e_downstream_name, TRUE);
2835 }
2836
2837 return ret;
2838}
2839
2840/*
2841 * Appends the run command's --connect options for the convert command.
2842 */
2843static
b19ff26f 2844int convert_auto_connect(bt_value *run_args,
9009cc24
PP
2845 GList *source_names, GList *filter_names,
2846 GList *sink_names)
2847{
2848 int ret = 0;
2849 GList *source_at = source_names;
2850 GList *filter_at = filter_names;
2851 GList *filter_prev;
2852 GList *sink_at = sink_names;
2853
f6ccaed9
PP
2854 BT_ASSERT(source_names);
2855 BT_ASSERT(filter_names);
2856 BT_ASSERT(sink_names);
9009cc24
PP
2857
2858 /* Connect all sources to the first filter */
5084732e 2859 for (source_at = source_names; source_at; source_at = g_list_next(source_at)) {
9009cc24
PP
2860 GString *source_name = source_at->data;
2861 GString *filter_name = filter_at->data;
2862
2863 ret = append_connect_arg(run_args, source_name->str,
2864 filter_name->str);
2865 if (ret) {
2866 goto error;
2867 }
2868 }
2869
2870 filter_prev = filter_at;
2871 filter_at = g_list_next(filter_at);
2872
2873 /* Connect remaining filters */
5084732e 2874 for (; filter_at; filter_prev = filter_at, filter_at = g_list_next(filter_at)) {
9009cc24
PP
2875 GString *filter_name = filter_at->data;
2876 GString *filter_prev_name = filter_prev->data;
2877
2878 ret = append_connect_arg(run_args, filter_prev_name->str,
2879 filter_name->str);
2880 if (ret) {
2881 goto error;
2882 }
2883 }
2884
2885 /* Connect last filter to all sinks */
5084732e 2886 for (sink_at = sink_names; sink_at; sink_at = g_list_next(sink_at)) {
9009cc24
PP
2887 GString *filter_name = filter_prev->data;
2888 GString *sink_name = sink_at->data;
2889
2890 ret = append_connect_arg(run_args, filter_name->str,
2891 sink_name->str);
2892 if (ret) {
2893 goto error;
2894 }
2895 }
2896
2897 goto end;
2898
2899error:
2900 ret = -1;
2901
2902end:
2903 return ret;
2904}
2905
2906static
2907int split_timerange(const char *arg, char **begin, char **end)
2908{
2909 int ret = 0;
2910 const char *ch = arg;
2911 size_t end_pos;
2912 GString *g_begin = NULL;
2913 GString *g_end = NULL;
2914
f6ccaed9 2915 BT_ASSERT(arg);
9009cc24
PP
2916
2917 if (*ch == '[') {
2918 ch++;
2919 }
2920
2921 g_begin = bt_common_string_until(ch, "", ",", &end_pos);
2922 if (!g_begin || ch[end_pos] != ',' || g_begin->len == 0) {
2923 goto error;
2924 }
2925
2926 ch += end_pos + 1;
2927
2928 g_end = bt_common_string_until(ch, "", "]", &end_pos);
2929 if (!g_end || g_end->len == 0) {
2930 goto error;
2931 }
2932
f6ccaed9
PP
2933 BT_ASSERT(begin);
2934 BT_ASSERT(end);
9009cc24
PP
2935 *begin = g_begin->str;
2936 *end = g_end->str;
2937 g_string_free(g_begin, FALSE);
2938 g_string_free(g_end, FALSE);
2939 g_begin = NULL;
2940 g_end = NULL;
2941 goto end;
2942
2943error:
2944 ret = -1;
2945
2946end:
2947 if (g_begin) {
2948 g_string_free(g_begin, TRUE);
2949 }
2950
2951 if (g_end) {
2952 g_string_free(g_end, TRUE);
2953 }
2954
2955 return ret;
2956}
2957
2958static
2959int g_list_prepend_gstring(GList **list, const char *string)
2960{
2961 int ret = 0;
2962 GString *gs = g_string_new(string);
2963
f6ccaed9 2964 BT_ASSERT(list);
9009cc24
PP
2965
2966 if (!gs) {
9f901451 2967 BT_CLI_LOGE_APPEND_CAUSE_OOM();
9009cc24
PP
2968 goto end;
2969 }
2970
2971 *list = g_list_prepend(*list, gs);
2972
2973end:
2974 return ret;
2975}
2976
73760435 2977/*
24594611
PP
2978 * Create `struct implicit_component_args` structures for each of the
2979 * source components we identified. Add them to `component_args`.
1ead9076 2980 *
005d49d6
SM
2981 * `non_opts` is an array of the non-option arguments passed on the command
2982 * line.
2983 *
24594611
PP
2984 * `non_opt_params` is an array where each element is an array of
2985 * strings containing all the arguments to `--params` that apply to the
2986 * non-option argument at the same index. For example, if, for a
2987 * non-option argument, the following `--params` options applied:
1ead9076
SM
2988 *
2989 * --params=a=2 --params=b=3,c=4
2990 *
24594611 2991 * its entry in `non_opt_params` would contain
1ead9076
SM
2992 *
2993 * ["a=2", "b=3,c=4"]
73760435
SM
2994 */
2995
2996static
1ead9076
SM
2997int create_implicit_component_args_from_auto_discovered_sources(
2998 const struct auto_source_discovery *auto_disc,
005d49d6 2999 const bt_value *non_opts,
24594611
PP
3000 const bt_value *non_opt_params,
3001 const bt_value *non_opt_loglevels,
1ead9076 3002 GPtrArray *component_args)
73760435
SM
3003{
3004 gchar *cc_name = NULL;
3005 struct implicit_component_args *comp = NULL;
3006 int status;
3007 guint i, len;
3008
3009 len = auto_disc->results->len;
3010
3011 for (i = 0; i < len; i++) {
3012 struct auto_source_discovery_result *res =
3013 g_ptr_array_index(auto_disc->results, i);
1ead9076 3014 uint64_t orig_indices_i, orig_indices_count;
73760435
SM
3015
3016 g_free(cc_name);
3017 cc_name = g_strdup_printf("source.%s.%s", res->plugin_name, res->source_cc_name);
3018 if (!cc_name) {
3019 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1ead9076 3020 goto error;
73760435
SM
3021 }
3022
3023 comp = create_implicit_component_args(cc_name);
3024 if (!comp) {
1ead9076
SM
3025 goto error;
3026 }
3027
3028 /*
24594611
PP
3029 * Append parameters and log levels of all the
3030 * non-option arguments that contributed to this
3031 * component instance coming into existence.
1ead9076 3032 */
393729a6 3033 orig_indices_count = bt_value_array_get_length(res->original_input_indices);
1ead9076
SM
3034 for (orig_indices_i = 0; orig_indices_i < orig_indices_count; orig_indices_i++) {
3035 const bt_value *orig_idx_value =
3036 bt_value_array_borrow_element_by_index(
3037 res->original_input_indices, orig_indices_i);
3038 uint64_t orig_idx = bt_value_integer_unsigned_get(orig_idx_value);
3039 const bt_value *params_array =
3040 bt_value_array_borrow_element_by_index_const(
24594611 3041 non_opt_params, orig_idx);
1ead9076 3042 uint64_t params_i, params_count;
1b2b6649 3043 const bt_value *loglevel_value;
1ead9076 3044
393729a6 3045 params_count = bt_value_array_get_length(params_array);
1ead9076
SM
3046 for (params_i = 0; params_i < params_count; params_i++) {
3047 const bt_value *params_value =
3048 bt_value_array_borrow_element_by_index_const(
3049 params_array, params_i);
3050 const char *params = bt_value_string_get(params_value);
3051 bt_value_array_append_element_status append_status;
3052
3053 append_status = bt_value_array_append_string_element(
3054 comp->extra_params, "--params");
3055 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
3056 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3057 goto error;
3058 }
3059
3060 append_status = bt_value_array_append_string_element(
3061 comp->extra_params, params);
3062 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
3063 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3064 goto error;
3065 }
3066 }
1b2b6649
SM
3067
3068 loglevel_value = bt_value_array_borrow_element_by_index_const(
24594611 3069 non_opt_loglevels, orig_idx);
1b2b6649
SM
3070 if (bt_value_get_type(loglevel_value) == BT_VALUE_TYPE_STRING) {
3071 const char *loglevel = bt_value_string_get(loglevel_value);
3072 bt_value_array_append_element_status append_status;
3073
3074 append_status = bt_value_array_append_string_element(
3075 comp->extra_params, "--log-level");
3076 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
3077 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3078 goto error;
3079 }
3080
3081 append_status = bt_value_array_append_string_element(
3082 comp->extra_params, loglevel);
3083 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
3084 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3085 goto error;
3086 }
3087 }
73760435
SM
3088 }
3089
005d49d6
SM
3090 /*
3091 * If single input and a src.ctf.fs component, provide the
3092 * relative path from the path passed on the command line to the
3093 * found trace.
3094 */
3095 if (bt_value_array_get_length(res->inputs) == 1 &&
3096 strcmp(res->plugin_name, "ctf") == 0 &&
3097 strcmp(res->source_cc_name, "fs") == 0) {
3098 const bt_value *orig_idx_value =
3099 bt_value_array_borrow_element_by_index(
3100 res->original_input_indices, 0);
3101 uint64_t orig_idx = bt_value_integer_unsigned_get(orig_idx_value);
3102 const bt_value *non_opt_value =
3103 bt_value_array_borrow_element_by_index_const(
3104 non_opts, orig_idx);
3105 const char *non_opt = bt_value_string_get(non_opt_value);
3106 const bt_value *input_value =
3107 bt_value_array_borrow_element_by_index_const(
3108 res->inputs, 0);
3109 const char *input = bt_value_string_get(input_value);
3110
3111 BT_ASSERT(orig_indices_count == 1);
3112 BT_ASSERT(g_str_has_prefix(input, non_opt));
3113
3114 input += strlen(non_opt);
3115
3116 while (G_IS_DIR_SEPARATOR(*input)) {
3117 input++;
3118 }
3119
3120 if (strlen(input) > 0) {
3121 append_string_parameter_to_args(comp->extra_params,
3122 "trace-name", input);
3123 }
3124 }
3125
73760435
SM
3126 status = append_parameter_to_args(comp->extra_params, "inputs", res->inputs);
3127 if (status != 0) {
1ead9076 3128 goto error;
73760435
SM
3129 }
3130
3131 g_ptr_array_add(component_args, comp);
3132 comp = NULL;
3133 }
3134
1ead9076
SM
3135 status = 0;
3136 goto end;
3137
3138error:
3139 status = -1;
3140
73760435
SM
3141end:
3142 g_free(cc_name);
3143
3144 if (comp) {
3145 destroy_implicit_component_args(comp);
3146 }
1ead9076
SM
3147
3148 return status;
73760435
SM
3149}
3150
505d9178
SM
3151/*
3152 * As we iterate the arguments to the convert command, this tracks what is the
3153 * type of the current item, to which some contextual options (e.g. --params)
3154 * apply to.
3155 */
3156enum convert_current_item_type {
3157 /* There is no current item. */
3158 CONVERT_CURRENT_ITEM_TYPE_NONE,
3159
3160 /* Current item is a component. */
3161 CONVERT_CURRENT_ITEM_TYPE_COMPONENT,
8b4cd08a 3162
24594611
PP
3163 /* Current item is a non-option argument. */
3164 CONVERT_CURRENT_ITEM_TYPE_NON_OPT,
505d9178
SM
3165};
3166
9009cc24
PP
3167/*
3168 * Creates a Babeltrace config object from the arguments of a convert
3169 * command.
3170 *
3171 * *retcode is set to the appropriate exit code to use.
3172 */
3173static
3174struct bt_config *bt_config_convert_from_args(int argc, const char *argv[],
02510056 3175 int *retcode, const bt_value *plugin_paths,
3dae1685 3176 int *default_log_level, const bt_interrupter *interrupter)
9009cc24 3177{
505d9178
SM
3178 enum convert_current_item_type current_item_type =
3179 CONVERT_CURRENT_ITEM_TYPE_NONE;
ca60bbc3 3180 int ret = 0;
9009cc24 3181 struct bt_config *cfg = NULL;
9009cc24
PP
3182 bool got_input_format_opt = false;
3183 bool got_output_format_opt = false;
3184 bool trimmer_has_begin = false;
3185 bool trimmer_has_end = false;
75a2cb9b 3186 bool stream_intersection_mode = false;
9009cc24
PP
3187 bool print_run_args = false;
3188 bool print_run_args_0 = false;
3189 bool print_ctf_metadata = false;
b19ff26f
PP
3190 bt_value *run_args = NULL;
3191 bt_value *all_names = NULL;
9009cc24
PP
3192 GList *source_names = NULL;
3193 GList *filter_names = NULL;
3194 GList *sink_names = NULL;
24594611
PP
3195 bt_value *non_opts = NULL;
3196 bt_value *non_opt_params = NULL;
3197 bt_value *non_opt_loglevels = NULL;
e7ad156c 3198 struct implicit_component_args implicit_ctf_output_args = { 0 };
9009cc24
PP
3199 struct implicit_component_args implicit_lttng_live_args = { 0 };
3200 struct implicit_component_args implicit_dummy_args = { 0 };
3201 struct implicit_component_args implicit_text_args = { 0 };
3202 struct implicit_component_args implicit_debug_info_args = { 0 };
3203 struct implicit_component_args implicit_muxer_args = { 0 };
3204 struct implicit_component_args implicit_trimmer_args = { 0 };
9009cc24
PP
3205 char error_buf[256] = { 0 };
3206 size_t i;
3207 struct bt_common_lttng_live_url_parts lttng_live_url_parts = { 0 };
e7ad156c 3208 char *output = NULL;
73760435
SM
3209 struct auto_source_discovery auto_disc = { NULL };
3210 GString *auto_disc_comp_name = NULL;
ca60bbc3 3211 struct bt_argpar_parse_ret argpar_parse_ret = { 0 };
505d9178
SM
3212 GString *name_gstr = NULL;
3213 GString *component_arg_for_run = NULL;
c57d9a63 3214 bt_value *live_inputs_array_val = NULL;
73760435
SM
3215
3216 /*
3217 * Array of `struct implicit_component_args *` created for the sources
3218 * we have auto-discovered.
3219 */
3220 GPtrArray *discovered_source_args = NULL;
3221
3222 /*
3223 * If set, restrict automatic source discovery to this component class
3224 * of this plugin.
3225 */
3226 const char *auto_source_discovery_restrict_plugin_name = NULL;
3227 const char *auto_source_discovery_restrict_component_class_name = NULL;
3228
9412c556 3229 bool ctf_fs_source_force_clock_class_unix_epoch_origin = false;
73760435
SM
3230 gchar *ctf_fs_source_clock_class_offset_arg = NULL;
3231 gchar *ctf_fs_source_clock_class_offset_ns_arg = NULL;
9009cc24
PP
3232 *retcode = 0;
3233
0e497e1c 3234 if (argc < 1) {
9009cc24
PP
3235 print_convert_usage(stdout);
3236 *retcode = -1;
3237 goto end;
3238 }
3239
e7ad156c
PP
3240 if (init_implicit_component_args(&implicit_ctf_output_args,
3241 "sink.ctf.fs", false)) {
3242 goto error;
3243 }
3244
9009cc24 3245 if (init_implicit_component_args(&implicit_lttng_live_args,
fd5f8053 3246 "source.ctf.lttng-live", false)) {
9009cc24
PP
3247 goto error;
3248 }
3249
fd5f8053
PP
3250 if (init_implicit_component_args(&implicit_text_args,
3251 "sink.text.pretty", false)) {
9009cc24
PP
3252 goto error;
3253 }
3254
fd5f8053
PP
3255 if (init_implicit_component_args(&implicit_dummy_args,
3256 "sink.utils.dummy", false)) {
9009cc24
PP
3257 goto error;
3258 }
3259
3260 if (init_implicit_component_args(&implicit_debug_info_args,
9a16feea 3261 "filter.lttng-utils.debug-info", false)) {
9009cc24
PP
3262 goto error;
3263 }
3264
fd5f8053
PP
3265 if (init_implicit_component_args(&implicit_muxer_args,
3266 "filter.utils.muxer", true)) {
9009cc24
PP
3267 goto error;
3268 }
3269
3270 if (init_implicit_component_args(&implicit_trimmer_args,
fd5f8053 3271 "filter.utils.trimmer", false)) {
9009cc24
PP
3272 goto error;
3273 }
3274
05e21286 3275 all_names = bt_value_map_create();
9009cc24 3276 if (!all_names) {
9f901451 3277 BT_CLI_LOGE_APPEND_CAUSE_OOM();
9009cc24
PP
3278 goto error;
3279 }
3280
05e21286 3281 run_args = bt_value_array_create();
9009cc24 3282 if (!run_args) {
9f901451 3283 BT_CLI_LOGE_APPEND_CAUSE_OOM();
9009cc24
PP
3284 goto error;
3285 }
3286
505d9178
SM
3287 component_arg_for_run = g_string_new(NULL);
3288 if (!component_arg_for_run) {
9f901451 3289 BT_CLI_LOGE_APPEND_CAUSE_OOM();
9009cc24
PP
3290 goto error;
3291 }
3292
24594611
PP
3293 non_opts = bt_value_array_create();
3294 if (!non_opts) {
9f901451 3295 BT_CLI_LOGE_APPEND_CAUSE_OOM();
f280892e
SM
3296 goto error;
3297 }
3298
24594611
PP
3299 non_opt_params = bt_value_array_create();
3300 if (!non_opt_params) {
1ead9076
SM
3301 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3302 goto error;
3303 }
3304
24594611
PP
3305 non_opt_loglevels = bt_value_array_create();
3306 if (!non_opt_loglevels) {
1b2b6649
SM
3307 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3308 goto error;
3309 }
3310
73760435
SM
3311 if (auto_source_discovery_init(&auto_disc) != 0) {
3312 goto error;
3313 }
3314
3315 discovered_source_args =
3316 g_ptr_array_new_with_free_func((GDestroyNotify) destroy_implicit_component_args);
3317 if (!discovered_source_args) {
3318 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3319 goto error;
3320 }
3321
3322 auto_disc_comp_name = g_string_new(NULL);
3323 if (!auto_disc_comp_name) {
3324 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3325 goto error;
3326 }
3327
9009cc24
PP
3328 /*
3329 * First pass: collect all arguments which need to be passed
3330 * as is to the run command. This pass can also add --name
3331 * arguments if needed to automatically name unnamed component
453b777b 3332 * instances.
9009cc24 3333 */
ca60bbc3
SM
3334 argpar_parse_ret = bt_argpar_parse(argc, argv, convert_options, true);
3335 if (argpar_parse_ret.error) {
3336 BT_CLI_LOGE_APPEND_CAUSE(
3337 "While parsing `convert` command's command-line arguments: %s",
3338 argpar_parse_ret.error->str);
9009cc24
PP
3339 goto error;
3340 }
3341
ca60bbc3
SM
3342 if (help_option_is_specified(&argpar_parse_ret)) {
3343 print_convert_usage(stdout);
3344 *retcode = -1;
3345 BT_OBJECT_PUT_REF_AND_RESET(cfg);
3346 goto end;
3347 }
9009cc24 3348
ca60bbc3
SM
3349 for (i = 0; i < argpar_parse_ret.items->len; i++) {
3350 struct bt_argpar_item *argpar_item =
3351 g_ptr_array_index(argpar_parse_ret.items, i);
3352 struct bt_argpar_item_opt *argpar_item_opt;
9009cc24
PP
3353 char *name = NULL;
3354 char *plugin_name = NULL;
3355 char *comp_cls_name = NULL;
ca60bbc3 3356 const char *arg;
9009cc24 3357
8b4cd08a
SM
3358 if (argpar_item->type == BT_ARGPAR_ITEM_TYPE_OPT) {
3359 argpar_item_opt = (struct bt_argpar_item_opt *) argpar_item;
3360 arg = argpar_item_opt->arg;
fd5f8053 3361
8b4cd08a
SM
3362 switch (argpar_item_opt->descr->id) {
3363 case OPT_COMPONENT:
3364 {
3365 bt_component_class_type type;
9009cc24 3366
8b4cd08a 3367 current_item_type = CONVERT_CURRENT_ITEM_TYPE_COMPONENT;
9009cc24 3368
8b4cd08a
SM
3369 /* Parse the argument */
3370 plugin_comp_cls_names(arg, &name, &plugin_name,
3371 &comp_cls_name, &type);
3372 if (!plugin_name || !comp_cls_name) {
505d9178 3373 BT_CLI_LOGE_APPEND_CAUSE(
8b4cd08a
SM
3374 "Invalid format for --component option's argument:\n %s",
3375 arg);
505d9178
SM
3376 goto error;
3377 }
3378
8b4cd08a
SM
3379 if (name) {
3380 /*
3381 * Name was given by the user, verify it isn't
3382 * taken.
3383 */
3384 if (bt_value_map_has_entry(all_names, name)) {
3385 BT_CLI_LOGE_APPEND_CAUSE(
3386 "Duplicate component instance name:\n %s",
3387 name);
3388 goto error;
3389 }
3390
3391 name_gstr = g_string_new(name);
3392 if (!name_gstr) {
3393 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3394 goto error;
3395 }
3396
3397 g_string_assign(component_arg_for_run, arg);
3398 } else {
3399 /* Name not given by user, generate one. */
3400 name_gstr = get_component_auto_name(arg, all_names);
3401 if (!name_gstr) {
3402 goto error;
3403 }
3404
3405 g_string_printf(component_arg_for_run, "%s:%s",
3406 name_gstr->str, arg);
3407 }
3408
3409 if (bt_value_array_append_string_element(run_args,
3410 "--component")) {
505d9178
SM
3411 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3412 goto error;
3413 }
3414
8b4cd08a
SM
3415 if (bt_value_array_append_string_element(run_args,
3416 component_arg_for_run->str)) {
3417 BT_CLI_LOGE_APPEND_CAUSE_OOM();
505d9178
SM
3418 goto error;
3419 }
9009cc24 3420
8b4cd08a
SM
3421 /*
3422 * Remember this name globally, for the uniqueness of
3423 * all component names.
3424 */
3425 if (bt_value_map_insert_entry(all_names,
3426 name_gstr->str, bt_value_null)) {
3427 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3428 goto error;
3429 }
fd5f8053 3430
8b4cd08a
SM
3431 /*
3432 * Remember this name specifically for the type of the
3433 * component. This is to create connection arguments.
3434 *
3435 * The list takes ownership of `name_gstr`.
3436 */
3437 switch (type) {
3438 case BT_COMPONENT_CLASS_TYPE_SOURCE:
3439 source_names = g_list_append(source_names, name_gstr);
3440 break;
3441 case BT_COMPONENT_CLASS_TYPE_FILTER:
3442 filter_names = g_list_append(filter_names, name_gstr);
3443 break;
3444 case BT_COMPONENT_CLASS_TYPE_SINK:
3445 sink_names = g_list_append(sink_names, name_gstr);
3446 break;
3447 default:
3448 abort();
3449 }
3450 name_gstr = NULL;
3451
3452 free(name);
3453 free(plugin_name);
3454 free(comp_cls_name);
3455 name = NULL;
3456 plugin_name = NULL;
3457 comp_cls_name = NULL;
3458 break;
505d9178 3459 }
8b4cd08a 3460 case OPT_PARAMS:
1ead9076
SM
3461 if (current_item_type == CONVERT_CURRENT_ITEM_TYPE_COMPONENT) {
3462 /*
3463 * The current item is a component (--component option),
3464 * pass it directly to the run args.
3465 */
3466 if (bt_value_array_append_string_element(run_args,
3467 "--params")) {
3468 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3469 goto error;
3470 }
505d9178 3471
1ead9076
SM
3472 if (bt_value_array_append_string_element(run_args, arg)) {
3473 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3474 goto error;
3475 }
24594611
PP
3476 } else if (current_item_type == CONVERT_CURRENT_ITEM_TYPE_NON_OPT) {
3477 /*
3478 * The current item is a
3479 * non-option argument, record
3480 * it in `non_opt_params`.
3481 */
1ead9076 3482 bt_value *array;
1db12e0d 3483 bt_value_array_append_element_status append_element_status;
393729a6 3484 uint64_t idx = bt_value_array_get_length(non_opt_params) - 1;
9009cc24 3485
24594611 3486 array = bt_value_array_borrow_element_by_index(non_opt_params, idx);
1db12e0d
SM
3487
3488 append_element_status = bt_value_array_append_string_element(array, arg);
3489 if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
3490 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3491 goto error;
3492 }
1ead9076
SM
3493 } else {
3494 BT_CLI_LOGE_APPEND_CAUSE(
3495 "No current component (--component option) or non-option argument of which to set parameters:\n %s",
3496 arg);
8b4cd08a
SM
3497 goto error;
3498 }
505d9178 3499 break;
8b4cd08a 3500 case OPT_LOG_LEVEL:
1b2b6649
SM
3501 if (current_item_type == CONVERT_CURRENT_ITEM_TYPE_COMPONENT) {
3502 if (bt_value_array_append_string_element(run_args, "--log-level")) {
3503 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3504 goto error;
3505 }
9009cc24 3506
1b2b6649
SM
3507 if (bt_value_array_append_string_element(run_args, arg)) {
3508 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3509 goto error;
3510 }
24594611 3511 } else if (current_item_type == CONVERT_CURRENT_ITEM_TYPE_NON_OPT) {
393729a6 3512 uint64_t idx = bt_value_array_get_length(non_opt_loglevels) - 1;
1b2b6649 3513 bt_value *log_level_str_value;
9009cc24 3514
1b2b6649
SM
3515 log_level_str_value = bt_value_string_create_init(arg);
3516 if (!log_level_str_value) {
3517 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3518 goto error;
3519 }
3520
24594611 3521 if (bt_value_array_set_element_by_index(non_opt_loglevels, idx,
1b2b6649
SM
3522 log_level_str_value)) {
3523 bt_value_put_ref(log_level_str_value);
3524 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3525 goto error;
3526 }
3527 } else {
3528 BT_CLI_LOGE_APPEND_CAUSE(
3529 "No current component (--component option) or non-option argument to assign a log level to:\n %s",
3530 arg);
8b4cd08a
SM
3531 goto error;
3532 }
29da2ffc 3533
8b4cd08a
SM
3534 break;
3535 case OPT_RETRY_DURATION:
3536 if (bt_value_array_append_string_element(run_args,
3537 "--retry-duration")) {
3538 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3539 goto error;
3540 }
29da2ffc 3541
8b4cd08a
SM
3542 if (bt_value_array_append_string_element(run_args, arg)) {
3543 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3544 goto error;
3545 }
3546 break;
3547 case OPT_BEGIN:
3548 case OPT_CLOCK_CYCLES:
3549 case OPT_CLOCK_DATE:
3550 case OPT_CLOCK_FORCE_CORRELATE:
3551 case OPT_CLOCK_GMT:
3552 case OPT_CLOCK_OFFSET:
3553 case OPT_CLOCK_OFFSET_NS:
3554 case OPT_CLOCK_SECONDS:
3555 case OPT_COLOR:
3556 case OPT_DEBUG:
3557 case OPT_DEBUG_INFO:
3558 case OPT_DEBUG_INFO_DIR:
3559 case OPT_DEBUG_INFO_FULL_PATH:
3560 case OPT_DEBUG_INFO_TARGET_PREFIX:
3561 case OPT_END:
3562 case OPT_FIELDS:
3563 case OPT_INPUT_FORMAT:
3564 case OPT_NAMES:
3565 case OPT_NO_DELTA:
3566 case OPT_OUTPUT_FORMAT:
3567 case OPT_OUTPUT:
3568 case OPT_RUN_ARGS:
3569 case OPT_RUN_ARGS_0:
3570 case OPT_STREAM_INTERSECTION:
3571 case OPT_TIMERANGE:
3572 case OPT_VERBOSE:
3573 /* Ignore in this pass */
3574 break;
3575 default:
3576 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
3577 argpar_item_opt->descr->id);
9009cc24
PP
3578 goto error;
3579 }
8b4cd08a
SM
3580 } else if (argpar_item->type == BT_ARGPAR_ITEM_TYPE_NON_OPT) {
3581 struct bt_argpar_item_non_opt *argpar_item_non_opt;
3582 bt_value_array_append_element_status append_status;
9009cc24 3583
24594611 3584 current_item_type = CONVERT_CURRENT_ITEM_TYPE_NON_OPT;
9009cc24 3585
8b4cd08a
SM
3586 argpar_item_non_opt = (struct bt_argpar_item_non_opt *) argpar_item;
3587
24594611 3588 append_status = bt_value_array_append_string_element(non_opts,
8b4cd08a
SM
3589 argpar_item_non_opt->arg);
3590 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
9f901451 3591 BT_CLI_LOGE_APPEND_CAUSE_OOM();
9009cc24
PP
3592 goto error;
3593 }
1ead9076 3594
847ab606
PP
3595 append_status = bt_value_array_append_empty_array_element(
3596 non_opt_params, NULL);
1ead9076
SM
3597 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
3598 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3599 goto error;
3600 }
1b2b6649 3601
24594611 3602 append_status = bt_value_array_append_element(non_opt_loglevels, bt_value_null);
1b2b6649
SM
3603 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
3604 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3605 goto error;
3606 }
8b4cd08a
SM
3607 } else {
3608 abort();
9009cc24 3609 }
9009cc24
PP
3610 }
3611
9009cc24
PP
3612 /*
3613 * Second pass: transform the convert-specific options and
3614 * arguments into implicit component instances for the run
3615 * command.
3616 */
ca60bbc3
SM
3617 for (i = 0; i < argpar_parse_ret.items->len; i++) {
3618 struct bt_argpar_item *argpar_item =
3619 g_ptr_array_index(argpar_parse_ret.items, i);
3620 struct bt_argpar_item_opt *argpar_item_opt;
3621 const char *arg;
9009cc24 3622
ca60bbc3
SM
3623 if (argpar_item->type != BT_ARGPAR_ITEM_TYPE_OPT) {
3624 continue;
3625 }
9009cc24 3626
ca60bbc3
SM
3627 argpar_item_opt = (struct bt_argpar_item_opt *) argpar_item;
3628 arg = argpar_item_opt->arg;
9009cc24 3629
ca60bbc3 3630 switch (argpar_item_opt->descr->id) {
9009cc24
PP
3631 case OPT_BEGIN:
3632 if (trimmer_has_begin) {
3633 printf("At --begin option: --begin or --timerange option already specified\n %s\n",
3634 arg);
3635 goto error;
3636 }
3637
3638 trimmer_has_begin = true;
e7ad156c 3639 ret = append_implicit_component_extra_param(
9009cc24
PP
3640 &implicit_trimmer_args, "begin", arg);
3641 implicit_trimmer_args.exists = true;
3642 if (ret) {
3643 goto error;
3644 }
3645 break;
3646 case OPT_END:
3647 if (trimmer_has_end) {
3648 printf("At --end option: --end or --timerange option already specified\n %s\n",
3649 arg);
3650 goto error;
3651 }
3652
3653 trimmer_has_end = true;
e7ad156c 3654 ret = append_implicit_component_extra_param(
9009cc24
PP
3655 &implicit_trimmer_args, "end", arg);
3656 implicit_trimmer_args.exists = true;
3657 if (ret) {
3658 goto error;
3659 }
3660 break;
3661 case OPT_TIMERANGE:
3662 {
3663 char *begin;
3664 char *end;
3665
3666 if (trimmer_has_begin || trimmer_has_end) {
3667 printf("At --timerange option: --begin, --end, or --timerange option already specified\n %s\n",
3668 arg);
3669 goto error;
3670 }
3671
3672 ret = split_timerange(arg, &begin, &end);
3673 if (ret) {
9f901451 3674 BT_CLI_LOGE_APPEND_CAUSE("Invalid --timerange option's argument: expecting BEGIN,END or [BEGIN,END]:\n %s",
9009cc24
PP
3675 arg);
3676 goto error;
3677 }
3678
e7ad156c 3679 ret = append_implicit_component_extra_param(
9009cc24 3680 &implicit_trimmer_args, "begin", begin);
e7ad156c 3681 ret |= append_implicit_component_extra_param(
9009cc24
PP
3682 &implicit_trimmer_args, "end", end);
3683 implicit_trimmer_args.exists = true;
3684 free(begin);
3685 free(end);
3686 if (ret) {
3687 goto error;
3688 }
3689 break;
3690 }
3691 case OPT_CLOCK_CYCLES:
3692 append_implicit_component_param(
3693 &implicit_text_args, "clock-cycles", "yes");
3694 implicit_text_args.exists = true;
3695 break;
3696 case OPT_CLOCK_DATE:
3697 append_implicit_component_param(
3698 &implicit_text_args, "clock-date", "yes");
3699 implicit_text_args.exists = true;
3700 break;
3701 case OPT_CLOCK_FORCE_CORRELATE:
9412c556 3702 ctf_fs_source_force_clock_class_unix_epoch_origin = true;
9009cc24
PP
3703 break;
3704 case OPT_CLOCK_GMT:
3705 append_implicit_component_param(
3706 &implicit_text_args, "clock-gmt", "yes");
eb01fbce 3707 append_implicit_component_param(
f855116d 3708 &implicit_trimmer_args, "gmt", "yes");
9009cc24
PP
3709 implicit_text_args.exists = true;
3710 break;
3711 case OPT_CLOCK_OFFSET:
73760435
SM
3712 if (ctf_fs_source_clock_class_offset_arg) {
3713 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --clock-offset option\n");
3714 goto error;
3715 }
3716
3717 ctf_fs_source_clock_class_offset_arg = g_strdup(arg);
3718 if (!ctf_fs_source_clock_class_offset_arg) {
3719 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3720 goto error;
3721 }
9009cc24
PP
3722 break;
3723 case OPT_CLOCK_OFFSET_NS:
73760435
SM
3724 if (ctf_fs_source_clock_class_offset_ns_arg) {
3725 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --clock-offset-ns option\n");
3726 goto error;
3727 }
3728
3729 ctf_fs_source_clock_class_offset_ns_arg = g_strdup(arg);
3730 if (!ctf_fs_source_clock_class_offset_ns_arg) {
3731 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3732 goto error;
3733 }
9009cc24
PP
3734 break;
3735 case OPT_CLOCK_SECONDS:
3736 append_implicit_component_param(
3737 &implicit_text_args, "clock-seconds", "yes");
3738 implicit_text_args.exists = true;
3739 break;
3740 case OPT_COLOR:
9009cc24 3741 implicit_text_args.exists = true;
e7ad156c
PP
3742 ret = append_implicit_component_extra_param(
3743 &implicit_text_args, "color", arg);
9009cc24
PP
3744 if (ret) {
3745 goto error;
3746 }
3747 break;
9a16feea
PP
3748 case OPT_DEBUG_INFO:
3749 implicit_debug_info_args.exists = true;
9009cc24
PP
3750 break;
3751 case OPT_DEBUG_INFO_DIR:
e7ad156c
PP
3752 implicit_debug_info_args.exists = true;
3753 ret = append_implicit_component_extra_param(
4cd687b9 3754 &implicit_debug_info_args, "debug-info-dir", arg);
9009cc24
PP
3755 if (ret) {
3756 goto error;
3757 }
3758 break;
3759 case OPT_DEBUG_INFO_FULL_PATH:
e7ad156c 3760 implicit_debug_info_args.exists = true;
9009cc24
PP
3761 append_implicit_component_param(
3762 &implicit_debug_info_args, "full-path", "yes");
3763 break;
3764 case OPT_DEBUG_INFO_TARGET_PREFIX:
e7ad156c
PP
3765 implicit_debug_info_args.exists = true;
3766 ret = append_implicit_component_extra_param(
9009cc24
PP
3767 &implicit_debug_info_args,
3768 "target-prefix", arg);
3769 if (ret) {
3770 goto error;
3771 }
3772 break;
3773 case OPT_FIELDS:
3774 {
b19ff26f 3775 bt_value *fields = fields_from_arg(arg);
9009cc24
PP
3776
3777 if (!fields) {
3778 goto error;
3779 }
3780
e7ad156c 3781 implicit_text_args.exists = true;
9009cc24
PP
3782 ret = insert_flat_params_from_array(
3783 implicit_text_args.params_arg,
05e21286 3784 fields, "field");
c5b9b441 3785 bt_value_put_ref(fields);
9009cc24
PP
3786 if (ret) {
3787 goto error;
3788 }
3789 break;
3790 }
3791 case OPT_NAMES:
3792 {
b19ff26f 3793 bt_value *names = names_from_arg(arg);
9009cc24
PP
3794
3795 if (!names) {
3796 goto error;
3797 }
3798
e7ad156c 3799 implicit_text_args.exists = true;
9009cc24
PP
3800 ret = insert_flat_params_from_array(
3801 implicit_text_args.params_arg,
05e21286 3802 names, "name");
c5b9b441 3803 bt_value_put_ref(names);
9009cc24
PP
3804 if (ret) {
3805 goto error;
3806 }
3807 break;
3808 }
3809 case OPT_NO_DELTA:
3810 append_implicit_component_param(
3811 &implicit_text_args, "no-delta", "yes");
3812 implicit_text_args.exists = true;
3813 break;
3814 case OPT_INPUT_FORMAT:
3815 if (got_input_format_opt) {
9f901451 3816 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --input-format option.");
9009cc24
PP
3817 goto error;
3818 }
3819
3820 got_input_format_opt = true;
3821
3822 if (strcmp(arg, "ctf") == 0) {
73760435
SM
3823 auto_source_discovery_restrict_plugin_name = "ctf";
3824 auto_source_discovery_restrict_component_class_name = "fs";
9009cc24 3825 } else if (strcmp(arg, "lttng-live") == 0) {
73760435
SM
3826 auto_source_discovery_restrict_plugin_name = "ctf";
3827 auto_source_discovery_restrict_component_class_name = "lttng-live";
9009cc24
PP
3828 implicit_lttng_live_args.exists = true;
3829 } else {
9f901451 3830 BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy input format:\n %s",
9009cc24
PP
3831 arg);
3832 goto error;
3833 }
3834 break;
3835 case OPT_OUTPUT_FORMAT:
3836 if (got_output_format_opt) {
9f901451 3837 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output-format option.");
9009cc24
PP
3838 goto error;
3839 }
3840
3841 got_output_format_opt = true;
3842
3843 if (strcmp(arg, "text") == 0) {
3844 implicit_text_args.exists = true;
e7ad156c
PP
3845 } else if (strcmp(arg, "ctf") == 0) {
3846 implicit_ctf_output_args.exists = true;
9009cc24
PP
3847 } else if (strcmp(arg, "dummy") == 0) {
3848 implicit_dummy_args.exists = true;
3849 } else if (strcmp(arg, "ctf-metadata") == 0) {
3850 print_ctf_metadata = true;
3851 } else {
9f901451 3852 BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy output format:\n %s",
9009cc24
PP
3853 arg);
3854 goto error;
3855 }
3856 break;
3857 case OPT_OUTPUT:
e7ad156c 3858 if (output) {
9f901451 3859 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output option");
e7ad156c
PP
3860 goto error;
3861 }
3862
3863 output = strdup(arg);
3864 if (!output) {
9f901451 3865 BT_CLI_LOGE_APPEND_CAUSE_OOM();
9009cc24
PP
3866 goto error;
3867 }
3868 break;
3869 case OPT_RUN_ARGS:
3870 if (print_run_args_0) {
9f901451 3871 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0.");
9009cc24
PP
3872 goto error;
3873 }
3874
3875 print_run_args = true;
3876 break;
3877 case OPT_RUN_ARGS_0:
3878 if (print_run_args) {
9f901451 3879 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0.");
9009cc24
PP
3880 goto error;
3881 }
3882
3883 print_run_args_0 = true;
3884 break;
3885 case OPT_STREAM_INTERSECTION:
75a2cb9b 3886 /*
1a29b831 3887 * Applies to all traces implementing the
5f2a1585 3888 * babeltrace.trace-infos query.
75a2cb9b
JG
3889 */
3890 stream_intersection_mode = true;
9009cc24
PP
3891 break;
3892 case OPT_VERBOSE:
ebddaeb8
SM
3893 *default_log_level =
3894 logging_level_min(*default_log_level, BT_LOG_INFO);
9009cc24
PP
3895 break;
3896 case OPT_DEBUG:
ebddaeb8
SM
3897 *default_log_level =
3898 logging_level_min(*default_log_level, BT_LOG_TRACE);
29da2ffc
PP
3899 break;
3900 default:
9009cc24
PP
3901 break;
3902 }
9009cc24
PP
3903 }
3904
ebddaeb8
SM
3905 set_auto_log_levels(default_log_level);
3906
3efa3052
PP
3907 /*
3908 * Legacy behaviour: --verbose used to make the `text` output
3909 * format print more information. --verbose is now equivalent to
83094759
PP
3910 * the INFO log level, which is why we compare to `BT_LOG_INFO`
3911 * here.
3efa3052 3912 */
29da2ffc 3913 if (*default_log_level == BT_LOG_INFO) {
3efa3052
PP
3914 append_implicit_component_param(&implicit_text_args,
3915 "verbose", "yes");
3916 }
3917
9009cc24
PP
3918 /* Print CTF metadata or print LTTng live sessions */
3919 if (print_ctf_metadata) {
24594611 3920 const bt_value *bt_val_non_opt;
94023a1c 3921
24594611 3922 if (bt_value_array_is_empty(non_opts)) {
9f901451 3923 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf-metadata specified without a path.");
9009cc24
PP
3924 goto error;
3925 }
3926
393729a6 3927 if (bt_value_array_get_length(non_opts) > 1) {
9f901451 3928 BT_CLI_LOGE_APPEND_CAUSE("Too many paths specified for --output-format=ctf-metadata.");
94023a1c
PP
3929 goto error;
3930 }
3931
9009cc24
PP
3932 cfg = bt_config_print_ctf_metadata_create(plugin_paths);
3933 if (!cfg) {
3934 goto error;
3935 }
3936
24594611 3937 bt_val_non_opt = bt_value_array_borrow_element_by_index_const(non_opts, 0);
9009cc24 3938 g_string_assign(cfg->cmd_data.print_ctf_metadata.path,
24594611 3939 bt_value_string_get(bt_val_non_opt));
c327e427
PP
3940
3941 if (output) {
3942 g_string_assign(
3943 cfg->cmd_data.print_ctf_metadata.output_path,
3944 output);
3945 }
3946
9009cc24
PP
3947 goto end;
3948 }
3949
3950 /*
e7ad156c
PP
3951 * If -o ctf was specified, make sure an output path (--output)
3952 * was also specified. --output does not imply -o ctf because
3953 * it's also used for the default, implicit -o text if -o ctf
3954 * is not specified.
3955 */
3956 if (implicit_ctf_output_args.exists) {
3957 if (!output) {
9f901451 3958 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf specified without --output (trace output path).");
e7ad156c
PP
3959 goto error;
3960 }
3961
3962 /*
3963 * At this point we know that -o ctf AND --output were
3964 * specified. Make sure that no options were specified
3965 * which would imply -o text because --output would be
3966 * ambiguous in this case. For example, this is wrong:
3967 *
ec2c5e50 3968 * babeltrace2 --names=all -o ctf --output=/tmp/path my-trace
e7ad156c
PP
3969 *
3970 * because --names=all implies -o text, and --output
3971 * could apply to both the sink.text.pretty and
3972 * sink.ctf.fs implicit components.
3973 */
3974 if (implicit_text_args.exists) {
9f901451 3975 BT_CLI_LOGE_APPEND_CAUSE("Ambiguous --output option: --output-format=ctf specified but another option implies --output-format=text.");
e7ad156c
PP
3976 goto error;
3977 }
3978 }
3979
3980 /*
3981 * If -o dummy and -o ctf were not specified, and if there are
3982 * no explicit sink components, then use an implicit
3983 * `sink.text.pretty` component.
9009cc24 3984 */
e7ad156c
PP
3985 if (!implicit_dummy_args.exists && !implicit_ctf_output_args.exists &&
3986 !sink_names) {
9009cc24
PP
3987 implicit_text_args.exists = true;
3988 }
3989
e7ad156c
PP
3990 /*
3991 * Set implicit `sink.text.pretty` or `sink.ctf.fs` component's
3992 * `path` parameter if --output was specified.
3993 */
3994 if (output) {
3995 if (implicit_text_args.exists) {
3996 append_implicit_component_extra_param(&implicit_text_args,
3997 "path", output);
3998 } else if (implicit_ctf_output_args.exists) {
3999 append_implicit_component_extra_param(&implicit_ctf_output_args,
4000 "path", output);
4001 }
4002 }
4003
24594611 4004 /* Decide where the non-option argument(s) go */
393729a6 4005 if (bt_value_array_get_length(non_opts) > 0) {
9009cc24 4006 if (implicit_lttng_live_args.exists) {
24594611 4007 const bt_value *bt_val_non_opt;
94023a1c 4008
393729a6 4009 if (bt_value_array_get_length(non_opts) > 1) {
9f901451 4010 BT_CLI_LOGE_APPEND_CAUSE("Too many URLs specified for --input-format=lttng-live.");
94023a1c
PP
4011 goto error;
4012 }
4013
24594611 4014 bt_val_non_opt = bt_value_array_borrow_element_by_index_const(non_opts, 0);
9009cc24 4015 lttng_live_url_parts =
24594611 4016 bt_common_parse_lttng_live_url(bt_value_string_get(bt_val_non_opt),
94b828f3 4017 error_buf, sizeof(error_buf));
9009cc24 4018 if (!lttng_live_url_parts.proto) {
9f901451 4019 BT_CLI_LOGE_APPEND_CAUSE("Invalid LTTng live URL format: %s.",
9009cc24
PP
4020 error_buf);
4021 goto error;
4022 }
4023
4024 if (!lttng_live_url_parts.session_name) {
4025 /* Print LTTng live sessions */
4026 cfg = bt_config_print_lttng_live_sessions_create(
4027 plugin_paths);
4028 if (!cfg) {
4029 goto error;
4030 }
4031
9009cc24 4032 g_string_assign(cfg->cmd_data.print_lttng_live_sessions.url,
24594611 4033 bt_value_string_get(bt_val_non_opt));
c327e427
PP
4034
4035 if (output) {
4036 g_string_assign(
4037 cfg->cmd_data.print_lttng_live_sessions.output_path,
4038 output);
4039 }
4040
9009cc24
PP
4041 goto end;
4042 }
4043
c57d9a63
PP
4044 live_inputs_array_val = bt_value_array_create();
4045 if (!live_inputs_array_val) {
4046 BT_CLI_LOGE_APPEND_CAUSE_OOM();
4047 goto error;
4048 }
4049
4050 if (bt_value_array_append_string_element(
4051 live_inputs_array_val,
4052 bt_value_string_get(bt_val_non_opt))) {
4053 BT_CLI_LOGE_APPEND_CAUSE_OOM();
4054 goto error;
4055 }
4056
4057 ret = append_parameter_to_args(
4058 implicit_lttng_live_args.extra_params,
4059 "inputs", live_inputs_array_val);
9009cc24
PP
4060 if (ret) {
4061 goto error;
4062 }
dccb8b6f
FD
4063
4064 ret = append_implicit_component_extra_param(
4065 &implicit_lttng_live_args,
4066 "session-not-found-action", "end");
4067 if (ret) {
4068 goto error;
4069 }
9009cc24 4070 } else {
73760435 4071 int status;
97010665
SM
4072 size_t plugin_count;
4073 const bt_plugin **plugins;
4074 const bt_plugin *plugin;
73760435 4075
97010665
SM
4076 status = require_loaded_plugins(plugin_paths);
4077 if (status != 0) {
4078 goto error;
4079 }
4080
4081 if (auto_source_discovery_restrict_plugin_name) {
4082 plugin_count = 1;
4083 plugin = find_loaded_plugin(auto_source_discovery_restrict_plugin_name);
4084 plugins = &plugin;
4085 } else {
4086 plugin_count = get_loaded_plugins_count();
4087 plugins = borrow_loaded_plugins();
4088 }
4089
4090 status = auto_discover_source_components(non_opts, plugins, plugin_count,
73760435 4091 auto_source_discovery_restrict_component_class_name,
3dae1685 4092 *default_log_level, &auto_disc, interrupter);
73760435
SM
4093
4094 if (status != 0) {
3dae1685
SM
4095 if (status == AUTO_SOURCE_DISCOVERY_STATUS_INTERRUPTED) {
4096 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
4097 "Babeltrace CLI", "Automatic source discovery interrupted by the user");
4098 }
9009cc24
PP
4099 goto error;
4100 }
73760435 4101
1ead9076 4102 status = create_implicit_component_args_from_auto_discovered_sources(
005d49d6 4103 &auto_disc, non_opts, non_opt_params, non_opt_loglevels,
1b2b6649 4104 discovered_source_args);
1ead9076
SM
4105 if (status != 0) {
4106 goto error;
4107 }
9009cc24
PP
4108 }
4109 }
4110
9412c556
FD
4111
4112 /*
4113 * If --clock-force-correlated was given, apply it to any src.ctf.fs
4114 * component.
4115 */
4116 if (ctf_fs_source_force_clock_class_unix_epoch_origin) {
4117 int n;
4118
4119 n = append_multiple_implicit_components_param(
4120 discovered_source_args, "source.ctf.fs", "force-clock-class-origin-unix-epoch",
4121 "yes");
4122 if (n == 0) {
4123 BT_CLI_LOGE_APPEND_CAUSE("--clock-force-correlate specified, but no source.ctf.fs component instantiated.");
4124 goto error;
4125 }
4126 }
4127
73760435
SM
4128 /* If --clock-offset was given, apply it to any src.ctf.fs component. */
4129 if (ctf_fs_source_clock_class_offset_arg) {
4130 int n;
4131
4132 n = append_multiple_implicit_components_param(
4133 discovered_source_args, "source.ctf.fs", "clock-class-offset-s",
4134 ctf_fs_source_clock_class_offset_arg);
4135
4136 if (n == 0) {
4137 BT_CLI_LOGE_APPEND_CAUSE("--clock-offset specified, but no source.ctf.fs component instantiated.");
4138 goto error;
4139 }
9009cc24
PP
4140 }
4141
73760435
SM
4142 /* If --clock-offset-ns was given, apply it to any src.ctf.fs component. */
4143 if (ctf_fs_source_clock_class_offset_ns_arg) {
4144 int n;
4145
4146 n = append_multiple_implicit_components_param(
4147 discovered_source_args, "source.ctf.fs", "clock-class-offset-ns",
4148 ctf_fs_source_clock_class_offset_ns_arg);
4149
4150 if (n == 0) {
4151 BT_CLI_LOGE_APPEND_CAUSE("--clock-offset-ns specified, but no source.ctf.fs component instantiated.");
4152 goto error;
4153 }
9009cc24
PP
4154 }
4155
73760435 4156 /*
24594611
PP
4157 * If the implicit `source.ctf.lttng-live` component exists,
4158 * make sure there's at least one non-option argument (which is
4159 * the URL).
73760435 4160 */
24594611 4161 if (implicit_lttng_live_args.exists && bt_value_array_is_empty(non_opts)) {
9f901451 4162 BT_CLI_LOGE_APPEND_CAUSE("Missing URL for implicit `%s` component.",
fd5f8053 4163 implicit_lttng_live_args.comp_arg->str);
9009cc24
PP
4164 goto error;
4165 }
4166
4167 /* Assign names to implicit components */
73760435
SM
4168 for (i = 0; i < discovered_source_args->len; i++) {
4169 struct implicit_component_args *args;
4170 int j;
4171
4172 args = discovered_source_args->pdata[i];
4173
4174 g_string_printf(auto_disc_comp_name, "auto-disc-%s", args->comp_arg->str);
4175
4176 /* Give it a name like `auto-disc-src-ctf-fs`. */
4177 for (j = 0; j < auto_disc_comp_name->len; j++) {
4178 if (auto_disc_comp_name->str[j] == '.') {
4179 auto_disc_comp_name->str[j] = '-';
4180 }
4181 }
4182
4183 ret = assign_name_to_implicit_component(args,
4184 auto_disc_comp_name->str, all_names, &source_names, true);
4185 if (ret) {
4186 goto error;
4187 }
9009cc24
PP
4188 }
4189
4190 ret = assign_name_to_implicit_component(&implicit_lttng_live_args,
4191 "lttng-live", all_names, &source_names, true);
4192 if (ret) {
4193 goto error;
4194 }
4195
4196 ret = assign_name_to_implicit_component(&implicit_text_args,
4197 "pretty", all_names, &sink_names, true);
4198 if (ret) {
4199 goto error;
4200 }
4201
e7ad156c
PP
4202 ret = assign_name_to_implicit_component(&implicit_ctf_output_args,
4203 "sink-ctf-fs", all_names, &sink_names, true);
4204 if (ret) {
4205 goto error;
4206 }
4207
9009cc24
PP
4208 ret = assign_name_to_implicit_component(&implicit_dummy_args,
4209 "dummy", all_names, &sink_names, true);
4210 if (ret) {
4211 goto error;
4212 }
4213
4214 ret = assign_name_to_implicit_component(&implicit_muxer_args,
4215 "muxer", all_names, NULL, false);
4216 if (ret) {
4217 goto error;
4218 }
4219
4220 ret = assign_name_to_implicit_component(&implicit_trimmer_args,
4221 "trimmer", all_names, NULL, false);
4222 if (ret) {
4223 goto error;
4224 }
4225
4226 ret = assign_name_to_implicit_component(&implicit_debug_info_args,
4227 "debug-info", all_names, NULL, false);
4228 if (ret) {
4229 goto error;
4230 }
4231
4232 /* Make sure there's at least one source and one sink */
4233 if (!source_names) {
9f901451 4234 BT_CLI_LOGE_APPEND_CAUSE("No source component.");
9009cc24
PP
4235 goto error;
4236 }
4237
4238 if (!sink_names) {
9f901451 4239 BT_CLI_LOGE_APPEND_CAUSE("No sink component.");
9009cc24
PP
4240 goto error;
4241 }
4242
46f55736
PP
4243 /* Make sure there's a single sink component */
4244 if (g_list_length(sink_names) != 1) {
4245 BT_CLI_LOGE_APPEND_CAUSE(
4246 "More than one sink component specified.");
4247 goto error;
4248 }
4249
9009cc24
PP
4250 /*
4251 * Prepend the muxer, the trimmer, and the debug info to the
4252 * filter chain so that we have:
4253 *
4254 * sources -> muxer -> [trimmer] -> [debug info] ->
4255 * [user filters] -> sinks
4256 */
4257 if (implicit_debug_info_args.exists) {
4258 if (g_list_prepend_gstring(&filter_names,
4259 implicit_debug_info_args.name_arg->str)) {
4260 goto error;
4261 }
4262 }
4263
4264 if (implicit_trimmer_args.exists) {
4265 if (g_list_prepend_gstring(&filter_names,
4266 implicit_trimmer_args.name_arg->str)) {
4267 goto error;
4268 }
4269 }
4270
4271 if (g_list_prepend_gstring(&filter_names,
4272 implicit_muxer_args.name_arg->str)) {
4273 goto error;
4274 }
4275
4276 /*
4277 * Append the equivalent run arguments for the implicit
4278 * components.
4279 */
73760435
SM
4280 for (i = 0; i < discovered_source_args->len; i++) {
4281 struct implicit_component_args *args =
4282 discovered_source_args->pdata[i];
4283
4284 ret = append_run_args_for_implicit_component(args, run_args);
4285 if (ret) {
4286 goto error;
4287 }
9009cc24
PP
4288 }
4289
fd5f8053 4290 ret = append_run_args_for_implicit_component(&implicit_lttng_live_args,
9009cc24
PP
4291 run_args);
4292 if (ret) {
4293 goto error;
4294 }
4295
fd5f8053
PP
4296 ret = append_run_args_for_implicit_component(&implicit_text_args,
4297 run_args);
9009cc24
PP
4298 if (ret) {
4299 goto error;
4300 }
4301
e7ad156c
PP
4302 ret = append_run_args_for_implicit_component(&implicit_ctf_output_args,
4303 run_args);
4304 if (ret) {
4305 goto error;
4306 }
4307
fd5f8053
PP
4308 ret = append_run_args_for_implicit_component(&implicit_dummy_args,
4309 run_args);
9009cc24
PP
4310 if (ret) {
4311 goto error;
4312 }
4313
fd5f8053
PP
4314 ret = append_run_args_for_implicit_component(&implicit_muxer_args,
4315 run_args);
9009cc24
PP
4316 if (ret) {
4317 goto error;
4318 }
4319
fd5f8053 4320 ret = append_run_args_for_implicit_component(&implicit_trimmer_args,
9009cc24
PP
4321 run_args);
4322 if (ret) {
4323 goto error;
4324 }
4325
fd5f8053 4326 ret = append_run_args_for_implicit_component(&implicit_debug_info_args,
9009cc24
PP
4327 run_args);
4328 if (ret) {
4329 goto error;
4330 }
4331
4332 /* Auto-connect components */
4333 ret = convert_auto_connect(run_args, source_names, filter_names,
4334 sink_names);
4335 if (ret) {
9f901451 4336 BT_CLI_LOGE_APPEND_CAUSE("Cannot auto-connect components.");
9009cc24
PP
4337 goto error;
4338 }
4339
4340 /*
4341 * We have all the run command arguments now. Depending on
4342 * --run-args, we pass this to the run command or print them
4343 * here.
4344 */
4345 if (print_run_args || print_run_args_0) {
c60cf081 4346 if (stream_intersection_mode) {
9f901451 4347 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --stream-intersection with --run-args or --run-args-0.");
c60cf081
PP
4348 goto error;
4349 }
4350
393729a6 4351 for (i = 0; i < bt_value_array_get_length(run_args); i++) {
b19ff26f 4352 const bt_value *arg_value =
05e21286
PP
4353 bt_value_array_borrow_element_by_index(run_args,
4354 i);
9009cc24
PP
4355 const char *arg;
4356 GString *quoted = NULL;
4357 const char *arg_to_print;
4358
f6ccaed9 4359 BT_ASSERT(arg_value);
601b0d3c 4360 arg = bt_value_string_get(arg_value);
9009cc24
PP
4361
4362 if (print_run_args) {
4363 quoted = bt_common_shell_quote(arg, true);
4364 if (!quoted) {
4365 goto error;
4366 }
4367
4368 arg_to_print = quoted->str;
4369 } else {
4370 arg_to_print = arg;
4371 }
4372
4373 printf("%s", arg_to_print);
4374
4375 if (quoted) {
4376 g_string_free(quoted, TRUE);
4377 }
4378
393729a6 4379 if (i < bt_value_array_get_length(run_args) - 1) {
9009cc24
PP
4380 if (print_run_args) {
4381 putchar(' ');
4382 } else {
4383 putchar('\0');
4384 }
4385 }
4386 }
4387
4388 *retcode = -1;
65300d60 4389 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
4390 goto end;
4391 }
4392
05e21286 4393 cfg = bt_config_run_from_args_array(run_args, retcode,
02510056 4394 plugin_paths, *default_log_level);
fc11b6a6
PP
4395 if (!cfg) {
4396 goto error;
4397 }
4398
75a2cb9b 4399 cfg->cmd_data.run.stream_intersection_mode = stream_intersection_mode;
9009cc24
PP
4400 goto end;
4401
4402error:
4403 *retcode = 1;
65300d60 4404 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24
PP
4405
4406end:
ca60bbc3 4407 bt_argpar_parse_ret_fini(&argpar_parse_ret);
9009cc24 4408
e7ad156c 4409 free(output);
9009cc24 4410
505d9178
SM
4411 if (component_arg_for_run) {
4412 g_string_free(component_arg_for_run, TRUE);
9009cc24
PP
4413 }
4414
505d9178
SM
4415 if (name_gstr) {
4416 g_string_free(name_gstr, TRUE);
9009cc24
PP
4417 }
4418
c57d9a63 4419 bt_value_put_ref(live_inputs_array_val);
c5b9b441
PP
4420 bt_value_put_ref(run_args);
4421 bt_value_put_ref(all_names);
9009cc24
PP
4422 destroy_glist_of_gstring(source_names);
4423 destroy_glist_of_gstring(filter_names);
4424 destroy_glist_of_gstring(sink_names);
24594611
PP
4425 bt_value_put_ref(non_opt_params);
4426 bt_value_put_ref(non_opt_loglevels);
4427 bt_value_put_ref(non_opts);
94023a1c
PP
4428 finalize_implicit_component_args(&implicit_ctf_output_args);
4429 finalize_implicit_component_args(&implicit_lttng_live_args);
4430 finalize_implicit_component_args(&implicit_dummy_args);
4431 finalize_implicit_component_args(&implicit_text_args);
4432 finalize_implicit_component_args(&implicit_debug_info_args);
4433 finalize_implicit_component_args(&implicit_muxer_args);
4434 finalize_implicit_component_args(&implicit_trimmer_args);
9009cc24 4435 bt_common_destroy_lttng_live_url_parts(&lttng_live_url_parts);
73760435
SM
4436 auto_source_discovery_fini(&auto_disc);
4437
4438 if (discovered_source_args) {
4439 g_ptr_array_free(discovered_source_args, TRUE);
4440 }
4441
4442 g_free(ctf_fs_source_clock_class_offset_arg);
4443 g_free(ctf_fs_source_clock_class_offset_ns_arg);
4444
4445 if (auto_disc_comp_name) {
4446 g_string_free(auto_disc_comp_name, TRUE);
4447 }
4448
9009cc24
PP
4449 return cfg;
4450}
4451
4452/*
4453 * Prints the Babeltrace 2.x general usage.
4454 */
4455static
4456void print_gen_usage(FILE *fp)
4457{
ec2c5e50 4458 fprintf(fp, "Usage: babeltrace2 [GENERAL OPTIONS] [COMMAND] [COMMAND ARGUMENTS]\n");
9009cc24
PP
4459 fprintf(fp, "\n");
4460 fprintf(fp, "General options:\n");
4461 fprintf(fp, "\n");
20931d22 4462 fprintf(fp, " -d, --debug Enable debug mode (same as --log-level=T)\n");
02510056 4463 fprintf(fp, " -h, --help Show this help and quit\n");
20931d22 4464 fprintf(fp, " -l, --log-level=LVL Set the default log level to LVL (`N`, `T`, `D`,\n");
02510056
PP
4465 fprintf(fp, " `I`, `W` (default), `E`, or `F`)\n");
4466 fprintf(fp, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
4467 fprintf(fp, " (~/.local/lib/babeltrace2/plugins)\n");
4468 fprintf(fp, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
4469 fprintf(fp, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
4470 fprintf(fp, " dynamic plugins can be loaded\n");
4471 fprintf(fp, " -v, --verbose Enable verbose mode (same as --log-level=I)\n");
4472 fprintf(fp, " -V, --version Show version and quit\n");
9009cc24
PP
4473 fprintf(fp, "\n");
4474 fprintf(fp, "Available commands:\n");
4475 fprintf(fp, "\n");
4476 fprintf(fp, " convert Convert and trim traces (default)\n");
4477 fprintf(fp, " help Get help for a plugin or a component class\n");
4478 fprintf(fp, " list-plugins List available plugins and their content\n");
4479 fprintf(fp, " query Query objects from a component class\n");
4480 fprintf(fp, " run Build a processing graph and run it\n");
4481 fprintf(fp, "\n");
ec2c5e50 4482 fprintf(fp, "Use `babeltrace2 COMMAND --help` to show the help of COMMAND.\n");
9009cc24
PP
4483}
4484
4485struct bt_config *bt_config_cli_args_create(int argc, const char *argv[],
02510056
PP
4486 int *retcode, bool omit_system_plugin_path,
4487 bool omit_home_plugin_path,
3dae1685
SM
4488 const bt_value *initial_plugin_paths,
4489 const bt_interrupter *interrupter)
9009cc24
PP
4490{
4491 struct bt_config *config = NULL;
9009cc24 4492 int i;
0e497e1c
SM
4493 int top_level_argc;
4494 const char **top_level_argv;
9009cc24 4495 int command_argc = -1;
0e497e1c 4496 const char **command_argv = NULL;
9009cc24 4497 const char *command_name = NULL;
29da2ffc 4498 int default_log_level = -1;
0e497e1c 4499 struct bt_argpar_parse_ret argpar_parse_ret = { 0 };
02510056 4500 bt_value *plugin_paths = NULL;
0e497e1c
SM
4501
4502 /* Top-level option descriptions. */
4503 static const struct bt_argpar_opt_descr descrs[] = {
4504 { OPT_DEBUG, 'd', "debug", false },
4505 { OPT_HELP, 'h', "help", false },
4506 { OPT_LOG_LEVEL, 'l', "log-level", true },
4507 { OPT_VERBOSE, 'v', "verbose", false },
4508 { OPT_VERSION, 'V', "version", false},
02510056
PP
4509 { OPT_OMIT_HOME_PLUGIN_PATH, '\0', "omit-home-plugin-path", false },
4510 { OPT_OMIT_SYSTEM_PLUGIN_PATH, '\0', "omit-system-plugin-path", false },
4511 { OPT_PLUGIN_PATH, '\0', "plugin-path", true },
0e497e1c
SM
4512 BT_ARGPAR_OPT_DESCR_SENTINEL
4513 };
9009cc24
PP
4514
4515 enum command_type {
4516 COMMAND_TYPE_NONE = -1,
4517 COMMAND_TYPE_RUN = 0,
4518 COMMAND_TYPE_CONVERT,
4519 COMMAND_TYPE_LIST_PLUGINS,
4520 COMMAND_TYPE_HELP,
4521 COMMAND_TYPE_QUERY,
4522 } command_type = COMMAND_TYPE_NONE;
4523
4524 *retcode = -1;
4525
4526 if (!initial_plugin_paths) {
02510056
PP
4527 plugin_paths = bt_value_array_create();
4528 if (!plugin_paths) {
4529 goto error;
9009cc24
PP
4530 }
4531 } else {
02510056
PP
4532 bt_value_copy_status copy_status = bt_value_copy(
4533 initial_plugin_paths, &plugin_paths);
4534 if (copy_status) {
4535 goto error;
4536 }
4537 }
4538
4539 BT_ASSERT(plugin_paths);
4540
4541 /*
4542 * The `BABELTRACE_PLUGIN_PATH` paths take precedence over the
4543 * `--plugin-path` option's paths, so append it now before
4544 * parsing the general options.
4545 */
4546 if (append_env_var_plugin_paths(plugin_paths)) {
4547 goto error;
9009cc24
PP
4548 }
4549
4550 if (argc <= 1) {
d878cbfd
PP
4551 print_version();
4552 puts("");
9009cc24
PP
4553 print_gen_usage(stdout);
4554 goto end;
4555 }
4556
0e497e1c
SM
4557 /* Skip first argument, the name of the program. */
4558 top_level_argc = argc - 1;
4559 top_level_argv = argv + 1;
4560 argpar_parse_ret = bt_argpar_parse(top_level_argc, top_level_argv,
4561 descrs, false);
3efa3052 4562
0e497e1c
SM
4563 if (argpar_parse_ret.error) {
4564 BT_CLI_LOGE_APPEND_CAUSE(
4565 "While parsing command-line arguments: %s",
4566 argpar_parse_ret.error->str);
4567 goto error;
4568 }
5bd4da00 4569
0e497e1c
SM
4570 for (i = 0; i < argpar_parse_ret.items->len; i++) {
4571 struct bt_argpar_item *item;
4572
4573 item = g_ptr_array_index(argpar_parse_ret.items, i);
4574
4575 if (item->type == BT_ARGPAR_ITEM_TYPE_OPT) {
4576 struct bt_argpar_item_opt *item_opt =
4577 (struct bt_argpar_item_opt *) item;
4578
4579 switch (item_opt->descr->id) {
4580 case OPT_DEBUG:
ebddaeb8
SM
4581 default_log_level =
4582 logging_level_min(default_log_level, BT_LOG_TRACE);
0e497e1c
SM
4583 break;
4584 case OPT_VERBOSE:
ebddaeb8
SM
4585 default_log_level =
4586 logging_level_min(default_log_level, BT_LOG_INFO);
0e497e1c
SM
4587 break;
4588 case OPT_LOG_LEVEL:
ebddaeb8
SM
4589 {
4590 int level = bt_log_get_level_from_string(item_opt->arg);
4591
4592 if (level < 0) {
0e497e1c
SM
4593 BT_CLI_LOGE_APPEND_CAUSE(
4594 "Invalid argument for --log-level option:\n %s",
4595 item_opt->arg);
4596 goto error;
4597 }
ebddaeb8
SM
4598
4599 default_log_level =
4600 logging_level_min(default_log_level, level);
0e497e1c 4601 break;
ebddaeb8 4602 }
02510056
PP
4603 case OPT_PLUGIN_PATH:
4604 if (bt_config_append_plugin_paths_check_setuid_setgid(
4605 plugin_paths, item_opt->arg)) {
4606 goto error;
4607 }
4608 break;
4609 case OPT_OMIT_SYSTEM_PLUGIN_PATH:
4610 omit_system_plugin_path = true;
4611 break;
4612 case OPT_OMIT_HOME_PLUGIN_PATH:
4613 omit_home_plugin_path = true;
4614 break;
0e497e1c
SM
4615 case OPT_VERSION:
4616 print_version();
4617 goto end;
4618 case OPT_HELP:
4619 print_gen_usage(stdout);
4620 goto end;
3efa3052 4621 }
0e497e1c
SM
4622 } else if (item->type == BT_ARGPAR_ITEM_TYPE_NON_OPT) {
4623 struct bt_argpar_item_non_opt *item_non_opt =
4624 (struct bt_argpar_item_non_opt *) item;
9009cc24
PP
4625 /*
4626 * First unknown argument: is it a known command
4627 * name?
4628 */
0e497e1c
SM
4629 command_argc =
4630 top_level_argc - item_non_opt->orig_index - 1;
4631 command_argv =
4632 &top_level_argv[item_non_opt->orig_index + 1];
3efa3052 4633
0e497e1c 4634 if (strcmp(item_non_opt->arg, "convert") == 0) {
9009cc24 4635 command_type = COMMAND_TYPE_CONVERT;
0e497e1c 4636 } else if (strcmp(item_non_opt->arg, "list-plugins") == 0) {
9009cc24 4637 command_type = COMMAND_TYPE_LIST_PLUGINS;
0e497e1c 4638 } else if (strcmp(item_non_opt->arg, "help") == 0) {
9009cc24 4639 command_type = COMMAND_TYPE_HELP;
0e497e1c 4640 } else if (strcmp(item_non_opt->arg, "query") == 0) {
9009cc24 4641 command_type = COMMAND_TYPE_QUERY;
0e497e1c 4642 } else if (strcmp(item_non_opt->arg, "run") == 0) {
9009cc24
PP
4643 command_type = COMMAND_TYPE_RUN;
4644 } else {
4645 /*
0e497e1c 4646 * Non-option argument, but not a known
3efa3052
PP
4647 * command name: assume the default
4648 * `convert` command.
9009cc24
PP
4649 */
4650 command_type = COMMAND_TYPE_CONVERT;
3efa3052 4651 command_name = "convert";
0e497e1c
SM
4652 command_argc++;
4653 command_argv--;
9009cc24
PP
4654 }
4655 break;
4656 }
4657 }
4658
4659 if (command_type == COMMAND_TYPE_NONE) {
0e497e1c
SM
4660 if (argpar_parse_ret.ingested_orig_args == top_level_argc) {
4661 /*
4662 * We only got non-help, non-version general options
4663 * like --verbose and --debug, without any other
4664 * arguments, so we can't do anything useful: print the
4665 * usage and quit.
4666 */
4667 print_gen_usage(stdout);
4668 goto end;
4669 }
4670
9009cc24 4671 /*
0e497e1c
SM
4672 * We stopped on an unknown option argument (and therefore
4673 * didn't see a command name). Assume `convert` command.
9009cc24 4674 */
0e497e1c
SM
4675 command_type = COMMAND_TYPE_CONVERT;
4676 command_name = "convert";
4677 command_argc =
4678 top_level_argc - argpar_parse_ret.ingested_orig_args;
4679 command_argv =
4680 &top_level_argv[argpar_parse_ret.ingested_orig_args];
9009cc24
PP
4681 }
4682
f6ccaed9
PP
4683 BT_ASSERT(command_argv);
4684 BT_ASSERT(command_argc >= 0);
9009cc24 4685
29da2ffc 4686 /*
ebddaeb8
SM
4687 * For all commands other than `convert`, we now know the log level to
4688 * use, so we can apply it with `set_auto_log_levels`.
4689 *
4690 * The convert command has `--debug` and `--verbose` arguments that are
4691 * equivalent to the top-level arguments of the same name. So after it
4692 * has parsed its arguments, `bt_config_convert_from_args` calls
4693 * `set_auto_log_levels` itself.
29da2ffc 4694 */
ebddaeb8
SM
4695 if (command_type != COMMAND_TYPE_CONVERT) {
4696 set_auto_log_levels(&default_log_level);
29da2ffc
PP
4697 }
4698
02510056
PP
4699 /*
4700 * At this point, `plugin_paths` contains the initial plugin
4701 * paths, the paths from the `BABELTRACE_PLUGIN_PATH` paths, and
4702 * the paths from the `--plugin-path` option.
4703 *
4704 * Now append the user and system plugin paths.
4705 */
4706 if (append_home_and_system_plugin_paths(plugin_paths,
4707 omit_system_plugin_path, omit_home_plugin_path)) {
4708 goto error;
4709 }
4710
9009cc24
PP
4711 switch (command_type) {
4712 case COMMAND_TYPE_RUN:
4713 config = bt_config_run_from_args(command_argc, command_argv,
02510056 4714 retcode, plugin_paths,
29da2ffc 4715 default_log_level);
9009cc24
PP
4716 break;
4717 case COMMAND_TYPE_CONVERT:
4718 config = bt_config_convert_from_args(command_argc, command_argv,
3dae1685 4719 retcode, plugin_paths, &default_log_level, interrupter);
9009cc24
PP
4720 break;
4721 case COMMAND_TYPE_LIST_PLUGINS:
4722 config = bt_config_list_plugins_from_args(command_argc,
02510056 4723 command_argv, retcode, plugin_paths);
9009cc24
PP
4724 break;
4725 case COMMAND_TYPE_HELP:
4726 config = bt_config_help_from_args(command_argc,
02510056 4727 command_argv, retcode, plugin_paths,
29da2ffc 4728 default_log_level);
9009cc24
PP
4729 break;
4730 case COMMAND_TYPE_QUERY:
4731 config = bt_config_query_from_args(command_argc,
02510056 4732 command_argv, retcode, plugin_paths,
29da2ffc 4733 default_log_level);
9009cc24
PP
4734 break;
4735 default:
0fbb9a9f 4736 abort();
9009cc24
PP
4737 }
4738
4739 if (config) {
ef267d12 4740 BT_ASSERT(default_log_level >= BT_LOG_TRACE);
29da2ffc 4741 config->log_level = default_log_level;
9009cc24
PP
4742 config->command_name = command_name;
4743 }
4744
0e497e1c
SM
4745 goto end;
4746
4747error:
4748 *retcode = 1;
4749
9009cc24 4750end:
0e497e1c 4751 bt_argpar_parse_ret_fini(&argpar_parse_ret);
02510056 4752 bt_value_put_ref(plugin_paths);
9009cc24
PP
4753 return config;
4754}
This page took 0.322398 seconds and 4 git commands to generate.