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