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