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