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