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