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