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