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