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