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