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