Move autodisc to its own convenience library
[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 <glib.h>
38 #include <sys/types.h>
39 #include "argpar/argpar.h"
40 #include "babeltrace2-cfg.h"
41 #include "babeltrace2-cfg-cli-args.h"
42 #include "babeltrace2-cfg-cli-args-connect.h"
43 #include "babeltrace2-cfg-cli-params-arg.h"
44 #include "babeltrace2-plugins.h"
45 #include "babeltrace2-query.h"
46 #include "autodisc/autodisc.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 /* argpar 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_NAMES,
886 OPT_NO_DELTA,
887 OPT_OMIT_HOME_PLUGIN_PATH,
888 OPT_OMIT_SYSTEM_PLUGIN_PATH,
889 OPT_OUTPUT,
890 OPT_OUTPUT_FORMAT,
891 OPT_PARAMS,
892 OPT_PLUGIN_PATH,
893 OPT_RESET_BASE_PARAMS,
894 OPT_RETRY_DURATION,
895 OPT_RUN_ARGS,
896 OPT_RUN_ARGS_0,
897 OPT_STREAM_INTERSECTION,
898 OPT_TIMERANGE,
899 OPT_VERBOSE,
900 OPT_VERSION,
901 };
902
903 enum bt_config_component_dest {
904 BT_CONFIG_COMPONENT_DEST_UNKNOWN = -1,
905 BT_CONFIG_COMPONENT_DEST_SOURCE,
906 BT_CONFIG_COMPONENT_DEST_FILTER,
907 BT_CONFIG_COMPONENT_DEST_SINK,
908 };
909
910 /*
911 * Adds a configuration component to the appropriate configuration
912 * array depending on the destination.
913 */
914 static
915 void add_run_cfg_comp(struct bt_config *cfg,
916 struct bt_config_component *cfg_comp,
917 enum bt_config_component_dest dest)
918 {
919 bt_object_get_ref(cfg_comp);
920
921 switch (dest) {
922 case BT_CONFIG_COMPONENT_DEST_SOURCE:
923 g_ptr_array_add(cfg->cmd_data.run.sources, cfg_comp);
924 break;
925 case BT_CONFIG_COMPONENT_DEST_FILTER:
926 g_ptr_array_add(cfg->cmd_data.run.filters, cfg_comp);
927 break;
928 case BT_CONFIG_COMPONENT_DEST_SINK:
929 g_ptr_array_add(cfg->cmd_data.run.sinks, cfg_comp);
930 break;
931 default:
932 abort();
933 }
934 }
935
936 static
937 int add_run_cfg_comp_check_name(struct bt_config *cfg,
938 struct bt_config_component *cfg_comp,
939 enum bt_config_component_dest dest,
940 bt_value *instance_names)
941 {
942 int ret = 0;
943
944 if (cfg_comp->instance_name->len == 0) {
945 BT_CLI_LOGE_APPEND_CAUSE("Found an unnamed component.");
946 ret = -1;
947 goto end;
948 }
949
950 if (bt_value_map_has_entry(instance_names,
951 cfg_comp->instance_name->str)) {
952 BT_CLI_LOGE_APPEND_CAUSE("Duplicate component instance name:\n %s",
953 cfg_comp->instance_name->str);
954 ret = -1;
955 goto end;
956 }
957
958 if (bt_value_map_insert_entry(instance_names,
959 cfg_comp->instance_name->str, bt_value_null)) {
960 BT_CLI_LOGE_APPEND_CAUSE_OOM();
961 ret = -1;
962 goto end;
963 }
964
965 add_run_cfg_comp(cfg, cfg_comp, dest);
966
967 end:
968 return ret;
969 }
970
971 static
972 int append_env_var_plugin_paths(bt_value *plugin_paths)
973 {
974 int ret = 0;
975 const char *envvar;
976
977 if (bt_common_is_setuid_setgid()) {
978 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
979 goto end;
980 }
981
982 envvar = getenv("BABELTRACE_PLUGIN_PATH");
983 if (!envvar) {
984 goto end;
985 }
986
987 ret = bt_config_append_plugin_paths(plugin_paths, envvar);
988
989 end:
990 if (ret) {
991 BT_CLI_LOGE_APPEND_CAUSE("Cannot append plugin paths from BABELTRACE_PLUGIN_PATH.");
992 }
993
994 return ret;
995 }
996
997 static
998 int append_home_and_system_plugin_paths(bt_value *plugin_paths,
999 bool omit_system_plugin_path, bool omit_home_plugin_path)
1000 {
1001 int ret;
1002
1003 if (!omit_home_plugin_path) {
1004 if (bt_common_is_setuid_setgid()) {
1005 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1006 } else {
1007 char *home_plugin_dir = bt_common_get_home_plugin_path(
1008 BT_LOG_OUTPUT_LEVEL);
1009
1010 if (home_plugin_dir) {
1011 ret = bt_config_append_plugin_paths(
1012 plugin_paths, home_plugin_dir);
1013 free(home_plugin_dir);
1014
1015 if (ret) {
1016 BT_CLI_LOGE_APPEND_CAUSE("Invalid home plugin path.");
1017 goto error;
1018 }
1019 }
1020 }
1021 }
1022
1023 if (!omit_system_plugin_path) {
1024 if (bt_config_append_plugin_paths(plugin_paths,
1025 bt_common_get_system_plugin_path())) {
1026 BT_CLI_LOGE_APPEND_CAUSE("Invalid system plugin path.");
1027 goto error;
1028 }
1029 }
1030 return 0;
1031 error:
1032 BT_CLI_LOGE_APPEND_CAUSE("Cannot append home and system plugin paths.");
1033 return -1;
1034 }
1035
1036 static
1037 int append_home_and_system_plugin_paths_cfg(struct bt_config *cfg)
1038 {
1039 return append_home_and_system_plugin_paths(cfg->plugin_paths,
1040 cfg->omit_system_plugin_path, cfg->omit_home_plugin_path);
1041 }
1042
1043 static
1044 struct bt_config *bt_config_base_create(enum bt_config_command command,
1045 const bt_value *initial_plugin_paths,
1046 bool needs_plugins)
1047 {
1048 struct bt_config *cfg;
1049
1050 /* Create config */
1051 cfg = g_new0(struct bt_config, 1);
1052 if (!cfg) {
1053 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1054 goto error;
1055 }
1056
1057 bt_object_init_shared(&cfg->base, bt_config_destroy);
1058 cfg->command = command;
1059 cfg->command_needs_plugins = needs_plugins;
1060
1061 if (initial_plugin_paths) {
1062 bt_value *initial_plugin_paths_copy;
1063
1064 (void) bt_value_copy(initial_plugin_paths,
1065 &initial_plugin_paths_copy);
1066 cfg->plugin_paths = initial_plugin_paths_copy;
1067 } else {
1068 cfg->plugin_paths = bt_value_array_create();
1069 if (!cfg->plugin_paths) {
1070 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1071 goto error;
1072 }
1073 }
1074
1075 goto end;
1076
1077 error:
1078 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1079
1080 end:
1081 return cfg;
1082 }
1083
1084 static
1085 struct bt_config *bt_config_run_create(
1086 const bt_value *initial_plugin_paths)
1087 {
1088 struct bt_config *cfg;
1089
1090 /* Create config */
1091 cfg = bt_config_base_create(BT_CONFIG_COMMAND_RUN,
1092 initial_plugin_paths, true);
1093 if (!cfg) {
1094 goto error;
1095 }
1096
1097 cfg->cmd_data.run.sources = g_ptr_array_new_with_free_func(
1098 (GDestroyNotify) bt_object_put_ref);
1099 if (!cfg->cmd_data.run.sources) {
1100 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1101 goto error;
1102 }
1103
1104 cfg->cmd_data.run.filters = g_ptr_array_new_with_free_func(
1105 (GDestroyNotify) bt_object_put_ref);
1106 if (!cfg->cmd_data.run.filters) {
1107 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1108 goto error;
1109 }
1110
1111 cfg->cmd_data.run.sinks = g_ptr_array_new_with_free_func(
1112 (GDestroyNotify) bt_object_put_ref);
1113 if (!cfg->cmd_data.run.sinks) {
1114 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1115 goto error;
1116 }
1117
1118 cfg->cmd_data.run.connections = g_ptr_array_new_with_free_func(
1119 (GDestroyNotify) bt_config_connection_destroy);
1120 if (!cfg->cmd_data.run.connections) {
1121 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1122 goto error;
1123 }
1124
1125 goto end;
1126
1127 error:
1128 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1129
1130 end:
1131 return cfg;
1132 }
1133
1134 static
1135 struct bt_config *bt_config_list_plugins_create(
1136 const bt_value *initial_plugin_paths)
1137 {
1138 struct bt_config *cfg;
1139
1140 /* Create config */
1141 cfg = bt_config_base_create(BT_CONFIG_COMMAND_LIST_PLUGINS,
1142 initial_plugin_paths, true);
1143 if (!cfg) {
1144 goto error;
1145 }
1146
1147 goto end;
1148
1149 error:
1150 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1151
1152 end:
1153 return cfg;
1154 }
1155
1156 static
1157 struct bt_config *bt_config_help_create(
1158 const bt_value *initial_plugin_paths,
1159 int default_log_level)
1160 {
1161 struct bt_config *cfg;
1162
1163 /* Create config */
1164 cfg = bt_config_base_create(BT_CONFIG_COMMAND_HELP,
1165 initial_plugin_paths, true);
1166 if (!cfg) {
1167 goto error;
1168 }
1169
1170 cfg->cmd_data.help.cfg_component =
1171 bt_config_component_create(-1, NULL, NULL, default_log_level);
1172 if (!cfg->cmd_data.help.cfg_component) {
1173 goto error;
1174 }
1175
1176 goto end;
1177
1178 error:
1179 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1180
1181 end:
1182 return cfg;
1183 }
1184
1185 static
1186 struct bt_config *bt_config_query_create(
1187 const bt_value *initial_plugin_paths)
1188 {
1189 struct bt_config *cfg;
1190
1191 /* Create config */
1192 cfg = bt_config_base_create(BT_CONFIG_COMMAND_QUERY,
1193 initial_plugin_paths, true);
1194 if (!cfg) {
1195 goto error;
1196 }
1197
1198 cfg->cmd_data.query.object = g_string_new(NULL);
1199 if (!cfg->cmd_data.query.object) {
1200 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1201 goto error;
1202 }
1203
1204 goto end;
1205
1206 error:
1207 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1208
1209 end:
1210 return cfg;
1211 }
1212
1213 static
1214 struct bt_config *bt_config_print_ctf_metadata_create(
1215 const bt_value *initial_plugin_paths)
1216 {
1217 struct bt_config *cfg;
1218
1219 /* Create config */
1220 cfg = bt_config_base_create(BT_CONFIG_COMMAND_PRINT_CTF_METADATA,
1221 initial_plugin_paths, true);
1222 if (!cfg) {
1223 goto error;
1224 }
1225
1226 cfg->cmd_data.print_ctf_metadata.path = g_string_new(NULL);
1227 if (!cfg->cmd_data.print_ctf_metadata.path) {
1228 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1229 goto error;
1230 }
1231
1232 cfg->cmd_data.print_ctf_metadata.output_path = g_string_new(NULL);
1233 if (!cfg->cmd_data.print_ctf_metadata.output_path) {
1234 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1235 goto error;
1236 }
1237
1238 goto end;
1239
1240 error:
1241 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1242
1243 end:
1244 return cfg;
1245 }
1246
1247 static
1248 struct bt_config *bt_config_print_lttng_live_sessions_create(
1249 const bt_value *initial_plugin_paths)
1250 {
1251 struct bt_config *cfg;
1252
1253 /* Create config */
1254 cfg = bt_config_base_create(BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS,
1255 initial_plugin_paths, true);
1256 if (!cfg) {
1257 goto error;
1258 }
1259
1260 cfg->cmd_data.print_lttng_live_sessions.url = g_string_new(NULL);
1261 if (!cfg->cmd_data.print_lttng_live_sessions.url) {
1262 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1263 goto error;
1264 }
1265
1266 cfg->cmd_data.print_lttng_live_sessions.output_path =
1267 g_string_new(NULL);
1268 if (!cfg->cmd_data.print_lttng_live_sessions.output_path) {
1269 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1270 goto error;
1271 }
1272
1273 goto end;
1274
1275 error:
1276 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1277
1278 end:
1279 return cfg;
1280 }
1281
1282 static
1283 int bt_config_append_plugin_paths_check_setuid_setgid(
1284 bt_value *plugin_paths, const char *arg)
1285 {
1286 int ret = 0;
1287
1288 if (bt_common_is_setuid_setgid()) {
1289 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1290 goto end;
1291 }
1292
1293 if (bt_config_append_plugin_paths(plugin_paths, arg)) {
1294 BT_CLI_LOGE_APPEND_CAUSE("Invalid --plugin-path option's argument:\n %s",
1295 arg);
1296 ret = -1;
1297 goto end;
1298 }
1299
1300 end:
1301 return ret;
1302 }
1303
1304 /*
1305 * Prints the expected format for a --params option.
1306 */
1307 static
1308 void print_expected_params_format(FILE *fp)
1309 {
1310 fprintf(fp, "Expected format of PARAMS\n");
1311 fprintf(fp, "-------------------------\n");
1312 fprintf(fp, "\n");
1313 fprintf(fp, " PARAM=VALUE[,PARAM=VALUE]...\n");
1314 fprintf(fp, "\n");
1315 fprintf(fp, "The parameter string is a comma-separated list of PARAM=VALUE assignments,\n");
1316 fprintf(fp, "where PARAM is the parameter name (C identifier plus the [:.-] characters),\n");
1317 fprintf(fp, "and VALUE can be one of:\n");
1318 fprintf(fp, "\n");
1319 fprintf(fp, "* `null`, `nul`, `NULL`: null value (no backticks).\n");
1320 fprintf(fp, "* `true`, `TRUE`, `yes`, `YES`: true boolean value (no backticks).\n");
1321 fprintf(fp, "* `false`, `FALSE`, `no`, `NO`: false boolean value (no backticks).\n");
1322 fprintf(fp, "* Binary (`0b` prefix), octal (`0` prefix), decimal, or hexadecimal\n");
1323 fprintf(fp, " (`0x` prefix) unsigned (with `+` prefix) or signed 64-bit integer.\n");
1324 fprintf(fp, "* Double precision floating point number (scientific notation is accepted).\n");
1325 fprintf(fp, "* Unquoted string with no special characters, and not matching any of\n");
1326 fprintf(fp, " the null and boolean value symbols above.\n");
1327 fprintf(fp, "* Double-quoted string (accepts escape characters).\n");
1328 fprintf(fp, "* Array, formatted as an opening `[`, a list of comma-separated values\n");
1329 fprintf(fp, " (as described by the current list) and a closing `]`.\n");
1330 fprintf(fp, "\n");
1331 fprintf(fp, "You can put whitespaces allowed around individual `=` and `,` symbols.\n");
1332 fprintf(fp, "\n");
1333 fprintf(fp, "Example:\n");
1334 fprintf(fp, "\n");
1335 fprintf(fp, " many=null, fresh=yes, condition=false, squirrel=-782329,\n");
1336 fprintf(fp, " play=+23, observe=3.14, simple=beef, needs-quotes=\"some string\",\n");
1337 fprintf(fp, " escape.chars-are:allowed=\"this is a \\\" double quote\",\n");
1338 fprintf(fp, " things=[1, \"2\", 3]\n");
1339 fprintf(fp, "\n");
1340 fprintf(fp, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1341 fprintf(fp, "babeltrace2 from a shell.\n");
1342 }
1343
1344 static
1345 bool help_option_is_specified(
1346 const struct bt_argpar_parse_ret *argpar_parse_ret)
1347 {
1348 int i;
1349 bool specified = false;
1350
1351 for (i = 0; i < argpar_parse_ret->items->len; i++) {
1352 struct bt_argpar_item *argpar_item =
1353 g_ptr_array_index(argpar_parse_ret->items, i);
1354 struct bt_argpar_item_opt *argpar_item_opt;
1355
1356 if (argpar_item->type != BT_ARGPAR_ITEM_TYPE_OPT) {
1357 continue;
1358 }
1359
1360 argpar_item_opt = (struct bt_argpar_item_opt *) argpar_item;
1361 if (argpar_item_opt->descr->id == OPT_HELP) {
1362 specified = true;
1363 break;
1364 }
1365 }
1366
1367 return specified;
1368 }
1369
1370 /*
1371 * Prints the help command usage.
1372 */
1373 static
1374 void print_help_usage(FILE *fp)
1375 {
1376 fprintf(fp, "Usage: babeltrace2 [GENERAL OPTIONS] help [OPTIONS] PLUGIN\n");
1377 fprintf(fp, " babeltrace2 [GENERAL OPTIONS] help [OPTIONS] TYPE.PLUGIN.CLS\n");
1378 fprintf(fp, "\n");
1379 fprintf(fp, "Options:\n");
1380 fprintf(fp, "\n");
1381 fprintf(fp, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1382 fprintf(fp, " (~/.local/lib/babeltrace2/plugins)\n");
1383 fprintf(fp, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1384 fprintf(fp, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1385 fprintf(fp, " dynamic plugins can be loaded\n");
1386 fprintf(fp, " -h, --help Show this help and quit\n");
1387 fprintf(fp, "\n");
1388 fprintf(fp, "See `babeltrace2 --help` for the list of general options.\n");
1389 fprintf(fp, "\n");
1390 fprintf(fp, "Use `babeltrace2 list-plugins` to show the list of available plugins.\n");
1391 }
1392
1393 static
1394 const struct bt_argpar_opt_descr help_options[] = {
1395 /* id, short_name, long_name, with_arg */
1396 { OPT_HELP, 'h', "help", false },
1397 { OPT_OMIT_HOME_PLUGIN_PATH, '\0', "omit-home-plugin-path", false },
1398 { OPT_OMIT_SYSTEM_PLUGIN_PATH, '\0', "omit-system-plugin-path", false },
1399 { OPT_PLUGIN_PATH, '\0', "plugin-path", true },
1400 BT_ARGPAR_OPT_DESCR_SENTINEL
1401 };
1402
1403 /*
1404 * Creates a Babeltrace config object from the arguments of a help
1405 * command.
1406 *
1407 * *retcode is set to the appropriate exit code to use.
1408 */
1409 static
1410 struct bt_config *bt_config_help_from_args(int argc, const char *argv[],
1411 int *retcode, bool force_omit_system_plugin_path,
1412 bool force_omit_home_plugin_path,
1413 const bt_value *initial_plugin_paths, int default_log_level)
1414 {
1415 int ret, i;
1416 struct bt_config *cfg = NULL;
1417 const char *non_opt = NULL;
1418 char *plugin_name = NULL, *comp_cls_name = NULL;
1419 struct bt_argpar_parse_ret argpar_parse_ret = { 0 };
1420
1421 *retcode = 0;
1422 cfg = bt_config_help_create(initial_plugin_paths, default_log_level);
1423 if (!cfg) {
1424 goto error;
1425 }
1426
1427 cfg->omit_system_plugin_path = force_omit_system_plugin_path;
1428 cfg->omit_home_plugin_path = force_omit_home_plugin_path;
1429 ret = append_env_var_plugin_paths(cfg->plugin_paths);
1430 if (ret) {
1431 goto error;
1432 }
1433
1434 /* Parse options */
1435 argpar_parse_ret = bt_argpar_parse(argc, argv, help_options, true);
1436 if (argpar_parse_ret.error) {
1437 BT_CLI_LOGE_APPEND_CAUSE(
1438 "While parsing `help` command's command-line arguments: %s",
1439 argpar_parse_ret.error->str);
1440 goto error;
1441 }
1442
1443 if (help_option_is_specified(&argpar_parse_ret)) {
1444 print_help_usage(stdout);
1445 *retcode = -1;
1446 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1447 goto end;
1448 }
1449
1450 for (i = 0; i < argpar_parse_ret.items->len; i++) {
1451 struct bt_argpar_item *argpar_item =
1452 g_ptr_array_index(argpar_parse_ret.items, i);
1453
1454 if (argpar_item->type == BT_ARGPAR_ITEM_TYPE_OPT) {
1455 struct bt_argpar_item_opt *argpar_item_opt;
1456 const char *arg;
1457 argpar_item_opt = (struct bt_argpar_item_opt *) argpar_item;
1458 arg = argpar_item_opt->arg;
1459
1460 switch (argpar_item_opt->descr->id) {
1461 case OPT_PLUGIN_PATH:
1462 if (bt_config_append_plugin_paths_check_setuid_setgid(
1463 cfg->plugin_paths, arg)) {
1464 goto error;
1465 }
1466 break;
1467 case OPT_OMIT_SYSTEM_PLUGIN_PATH:
1468 cfg->omit_system_plugin_path = true;
1469 break;
1470 case OPT_OMIT_HOME_PLUGIN_PATH:
1471 cfg->omit_home_plugin_path = true;
1472 break;
1473 default:
1474 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1475 argpar_item_opt->descr->id);
1476 goto error;
1477 }
1478 } else {
1479 struct bt_argpar_item_non_opt *argpar_item_non_opt
1480 = (struct bt_argpar_item_non_opt *) argpar_item;
1481
1482 if (non_opt) {
1483 BT_CLI_LOGE_APPEND_CAUSE("Extraneous command-line argument specified to `help` command: `%s`.",
1484 argpar_item_non_opt->arg);
1485 goto error;
1486 }
1487
1488 non_opt = argpar_item_non_opt->arg;
1489 }
1490 }
1491
1492 if (non_opt) {
1493 plugin_comp_cls_names(non_opt, NULL,
1494 &plugin_name, &comp_cls_name,
1495 &cfg->cmd_data.help.cfg_component->type);
1496 if (plugin_name && comp_cls_name) {
1497 /* Component class help */
1498 g_string_assign(
1499 cfg->cmd_data.help.cfg_component->plugin_name,
1500 plugin_name);
1501 g_string_assign(
1502 cfg->cmd_data.help.cfg_component->comp_cls_name,
1503 comp_cls_name);
1504 } else {
1505 /* Fall back to plugin help */
1506 g_string_assign(
1507 cfg->cmd_data.help.cfg_component->plugin_name,
1508 non_opt);
1509 }
1510 } else {
1511 print_help_usage(stdout);
1512 *retcode = -1;
1513 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1514 goto end;
1515 }
1516
1517 if (append_home_and_system_plugin_paths_cfg(cfg)) {
1518 goto error;
1519 }
1520
1521 goto end;
1522
1523 error:
1524 *retcode = 1;
1525 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1526
1527 end:
1528 g_free(plugin_name);
1529 g_free(comp_cls_name);
1530
1531 bt_argpar_parse_ret_fini(&argpar_parse_ret);
1532
1533 return cfg;
1534 }
1535
1536 /*
1537 * Prints the help command usage.
1538 */
1539 static
1540 void print_query_usage(FILE *fp)
1541 {
1542 fprintf(fp, "Usage: babeltrace2 [GEN OPTS] query [OPTS] TYPE.PLUGIN.CLS OBJECT\n");
1543 fprintf(fp, "\n");
1544 fprintf(fp, "Options:\n");
1545 fprintf(fp, "\n");
1546 fprintf(fp, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1547 fprintf(fp, " (~/.local/lib/babeltrace2/plugins)\n");
1548 fprintf(fp, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1549 fprintf(fp, " -p, --params=PARAMS Set the query parameters to PARAMS\n");
1550 fprintf(fp, " (see the expected format of PARAMS below)\n");
1551 fprintf(fp, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1552 fprintf(fp, " dynamic plugins can be loaded\n");
1553 fprintf(fp, " -h, --help Show this help and quit\n");
1554 fprintf(fp, "\n\n");
1555 print_expected_params_format(fp);
1556 }
1557
1558 static
1559 const struct bt_argpar_opt_descr query_options[] = {
1560 /* id, short_name, long_name, with_arg */
1561 { OPT_HELP, 'h', "help", false },
1562 { OPT_OMIT_HOME_PLUGIN_PATH, '\0', "omit-home-plugin-path", false },
1563 { OPT_OMIT_SYSTEM_PLUGIN_PATH, '\0', "omit-system-plugin-path", false },
1564 { OPT_PARAMS, 'p', "params", true },
1565 { OPT_PLUGIN_PATH, '\0', "plugin-path", true },
1566 BT_ARGPAR_OPT_DESCR_SENTINEL
1567 };
1568
1569 /*
1570 * Creates a Babeltrace config object from the arguments of a query
1571 * command.
1572 *
1573 * *retcode is set to the appropriate exit code to use.
1574 */
1575 static
1576 struct bt_config *bt_config_query_from_args(int argc, const char *argv[],
1577 int *retcode, bool force_omit_system_plugin_path,
1578 bool force_omit_home_plugin_path,
1579 const bt_value *initial_plugin_paths,
1580 int default_log_level)
1581 {
1582 int ret, i;
1583 struct bt_config *cfg = NULL;
1584 const char *component_class_spec = NULL;
1585 const char *query_object = NULL;
1586 bt_value *params;
1587 GString *error_str = NULL;
1588 struct bt_argpar_parse_ret argpar_parse_ret = { 0 };
1589
1590 params = bt_value_null;
1591 bt_value_get_ref(bt_value_null);
1592
1593 *retcode = 0;
1594 cfg = bt_config_query_create(initial_plugin_paths);
1595 if (!cfg) {
1596 goto error;
1597 }
1598
1599 error_str = g_string_new(NULL);
1600 if (!error_str) {
1601 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1602 goto error;
1603 }
1604
1605 cfg->omit_system_plugin_path = force_omit_system_plugin_path;
1606 cfg->omit_home_plugin_path = force_omit_home_plugin_path;
1607 ret = append_env_var_plugin_paths(cfg->plugin_paths);
1608 if (ret) {
1609 goto error;
1610 }
1611
1612 /* Parse options */
1613 argpar_parse_ret = bt_argpar_parse(argc, argv, query_options, true);
1614 if (argpar_parse_ret.error) {
1615 BT_CLI_LOGE_APPEND_CAUSE(
1616 "While parsing `query` command's command-line arguments: %s",
1617 argpar_parse_ret.error->str);
1618 goto error;
1619 }
1620
1621 if (help_option_is_specified(&argpar_parse_ret)) {
1622 print_query_usage(stdout);
1623 *retcode = -1;
1624 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1625 goto end;
1626 }
1627
1628 for (i = 0; i < argpar_parse_ret.items->len; i++) {
1629 struct bt_argpar_item *argpar_item =
1630 g_ptr_array_index(argpar_parse_ret.items, i);
1631
1632 if (argpar_item->type == BT_ARGPAR_ITEM_TYPE_OPT) {
1633 struct bt_argpar_item_opt *argpar_item_opt =
1634 (struct bt_argpar_item_opt *) argpar_item;
1635 const char *arg = argpar_item_opt->arg;
1636
1637 switch (argpar_item_opt->descr->id) {
1638 case OPT_PLUGIN_PATH:
1639 if (bt_config_append_plugin_paths_check_setuid_setgid(
1640 cfg->plugin_paths, arg)) {
1641 goto error;
1642 }
1643 break;
1644 case OPT_OMIT_SYSTEM_PLUGIN_PATH:
1645 cfg->omit_system_plugin_path = true;
1646 break;
1647 case OPT_OMIT_HOME_PLUGIN_PATH:
1648 cfg->omit_home_plugin_path = true;
1649 break;
1650 case OPT_PARAMS:
1651 {
1652 bt_value_put_ref(params);
1653 params = cli_value_from_arg(arg, error_str);
1654 if (!params) {
1655 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
1656 error_str->str);
1657 goto error;
1658 }
1659 break;
1660 }
1661 default:
1662 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1663 argpar_item_opt->descr->id);
1664 goto error;
1665 }
1666 } else {
1667 struct bt_argpar_item_non_opt *argpar_item_non_opt
1668 = (struct bt_argpar_item_non_opt *) argpar_item;
1669
1670 /*
1671 * We need exactly two non-option arguments
1672 * which are the mandatory component class
1673 * specification and query object.
1674 */
1675 if (!component_class_spec) {
1676 component_class_spec = argpar_item_non_opt->arg;
1677 } else if (!query_object) {
1678 query_object = argpar_item_non_opt->arg;
1679 } else {
1680 BT_CLI_LOGE_APPEND_CAUSE("Extraneous command-line argument specified to `query` command: `%s`.",
1681 argpar_item_non_opt->arg);
1682 goto error;
1683 }
1684 }
1685 }
1686
1687 if (!component_class_spec || !query_object) {
1688 print_query_usage(stdout);
1689 *retcode = -1;
1690 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1691 goto end;
1692 }
1693
1694 cfg->cmd_data.query.cfg_component =
1695 bt_config_component_from_arg(component_class_spec,
1696 default_log_level);
1697 if (!cfg->cmd_data.query.cfg_component) {
1698 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for component class specification:\n %s",
1699 component_class_spec);
1700 goto error;
1701 }
1702
1703 BT_ASSERT(params);
1704 BT_OBJECT_MOVE_REF(cfg->cmd_data.query.cfg_component->params, params);
1705
1706 if (strlen(query_object) == 0) {
1707 BT_CLI_LOGE_APPEND_CAUSE("Invalid empty object.");
1708 goto error;
1709 }
1710
1711 g_string_assign(cfg->cmd_data.query.object, query_object);
1712
1713 if (append_home_and_system_plugin_paths_cfg(cfg)) {
1714 goto error;
1715 }
1716
1717 goto end;
1718
1719 error:
1720 *retcode = 1;
1721 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1722
1723 end:
1724 bt_argpar_parse_ret_fini(&argpar_parse_ret);
1725
1726 if (error_str) {
1727 g_string_free(error_str, TRUE);
1728 }
1729
1730 bt_value_put_ref(params);
1731 return cfg;
1732 }
1733
1734 /*
1735 * Prints the list-plugins command usage.
1736 */
1737 static
1738 void print_list_plugins_usage(FILE *fp)
1739 {
1740 fprintf(fp, "Usage: babeltrace2 [GENERAL OPTIONS] list-plugins [OPTIONS]\n");
1741 fprintf(fp, "\n");
1742 fprintf(fp, "Options:\n");
1743 fprintf(fp, "\n");
1744 fprintf(fp, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1745 fprintf(fp, " (~/.local/lib/babeltrace2/plugins)\n");
1746 fprintf(fp, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1747 fprintf(fp, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1748 fprintf(fp, " dynamic plugins can be loaded\n");
1749 fprintf(fp, " -h, --help Show this help and quit\n");
1750 fprintf(fp, "\n");
1751 fprintf(fp, "See `babeltrace2 --help` for the list of general options.\n");
1752 fprintf(fp, "\n");
1753 fprintf(fp, "Use `babeltrace2 help` to get help for a specific plugin or component class.\n");
1754 }
1755
1756 static
1757 const struct bt_argpar_opt_descr list_plugins_options[] = {
1758 /* id, short_name, long_name, with_arg */
1759 { OPT_HELP, 'h', "help", false },
1760 { OPT_OMIT_HOME_PLUGIN_PATH, '\0', "omit-home-plugin-path", false },
1761 { OPT_OMIT_SYSTEM_PLUGIN_PATH, '\0', "omit-system-plugin-path", false },
1762 { OPT_PLUGIN_PATH, '\0', "plugin-path", true },
1763 BT_ARGPAR_OPT_DESCR_SENTINEL
1764 };
1765
1766 /*
1767 * Creates a Babeltrace config object from the arguments of a
1768 * list-plugins command.
1769 *
1770 * *retcode is set to the appropriate exit code to use.
1771 */
1772 static
1773 struct bt_config *bt_config_list_plugins_from_args(int argc, const char *argv[],
1774 int *retcode, bool force_omit_system_plugin_path,
1775 bool force_omit_home_plugin_path,
1776 const bt_value *initial_plugin_paths)
1777 {
1778 int ret, i;
1779 struct bt_config *cfg = NULL;
1780 struct bt_argpar_parse_ret argpar_parse_ret = { 0 };
1781
1782 *retcode = 0;
1783 cfg = bt_config_list_plugins_create(initial_plugin_paths);
1784 if (!cfg) {
1785 goto error;
1786 }
1787
1788 cfg->omit_system_plugin_path = force_omit_system_plugin_path;
1789 cfg->omit_home_plugin_path = force_omit_home_plugin_path;
1790 ret = append_env_var_plugin_paths(cfg->plugin_paths);
1791 if (ret) {
1792 goto error;
1793 }
1794
1795 /* Parse options */
1796 argpar_parse_ret = bt_argpar_parse(argc, argv, list_plugins_options, true);
1797 if (argpar_parse_ret.error) {
1798 BT_CLI_LOGE_APPEND_CAUSE(
1799 "While parsing `list-plugins` command's command-line arguments: %s",
1800 argpar_parse_ret.error->str);
1801 goto error;
1802 }
1803
1804 if (help_option_is_specified(&argpar_parse_ret)) {
1805 print_list_plugins_usage(stdout);
1806 *retcode = -1;
1807 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1808 goto end;
1809 }
1810
1811 for (i = 0; i < argpar_parse_ret.items->len; i++) {
1812 struct bt_argpar_item *argpar_item =
1813 g_ptr_array_index(argpar_parse_ret.items, i);
1814 struct bt_argpar_item_opt *argpar_item_opt;
1815 const char *arg;
1816
1817 if (argpar_item->type == BT_ARGPAR_ITEM_TYPE_NON_OPT) {
1818 struct bt_argpar_item_non_opt *argpar_item_non_opt
1819 = (struct bt_argpar_item_non_opt *) argpar_item;
1820
1821 BT_CLI_LOGE_APPEND_CAUSE("Unexpected argument: `%s`.",
1822 argpar_item_non_opt->arg);
1823 goto error;
1824 }
1825
1826 argpar_item_opt = (struct bt_argpar_item_opt *) argpar_item;
1827 arg = argpar_item_opt->arg;
1828
1829 switch (argpar_item_opt->descr->id) {
1830 case OPT_PLUGIN_PATH:
1831 if (bt_config_append_plugin_paths_check_setuid_setgid(
1832 cfg->plugin_paths, arg)) {
1833 goto error;
1834 }
1835 break;
1836 case OPT_OMIT_SYSTEM_PLUGIN_PATH:
1837 cfg->omit_system_plugin_path = true;
1838 break;
1839 case OPT_OMIT_HOME_PLUGIN_PATH:
1840 cfg->omit_home_plugin_path = true;
1841 break;
1842 default:
1843 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1844 argpar_item_opt->descr->id);
1845 goto error;
1846 }
1847 }
1848
1849 if (append_home_and_system_plugin_paths_cfg(cfg)) {
1850 goto error;
1851 }
1852
1853 goto end;
1854
1855 error:
1856 *retcode = 1;
1857 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1858
1859 end:
1860 bt_argpar_parse_ret_fini(&argpar_parse_ret);
1861
1862 return cfg;
1863 }
1864
1865 /*
1866 * Prints the run command usage.
1867 */
1868 static
1869 void print_run_usage(FILE *fp)
1870 {
1871 fprintf(fp, "Usage: babeltrace2 [GENERAL OPTIONS] run [OPTIONS]\n");
1872 fprintf(fp, "\n");
1873 fprintf(fp, "Options:\n");
1874 fprintf(fp, "\n");
1875 fprintf(fp, " -b, --base-params=PARAMS Set PARAMS as the current base parameters\n");
1876 fprintf(fp, " for all the following components until\n");
1877 fprintf(fp, " --reset-base-params is encountered\n");
1878 fprintf(fp, " (see the expected format of PARAMS below)\n");
1879 fprintf(fp, " -c, --component=NAME:TYPE.PLUGIN.CLS\n");
1880 fprintf(fp, " Instantiate the component class CLS of type\n");
1881 fprintf(fp, " TYPE (`source`, `filter`, or `sink`) found\n");
1882 fprintf(fp, " in the plugin PLUGIN, add it to the graph,\n");
1883 fprintf(fp, " and name it NAME");
1884 fprintf(fp, " -x, --connect=CONNECTION Connect two created components (see the\n");
1885 fprintf(fp, " expected format of CONNECTION below)\n");
1886 fprintf(fp, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
1887 fprintf(fp, " (`N`, `V`, `D`, `I`, `W`, `E`, or `F`)\n");
1888 fprintf(fp, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1889 fprintf(fp, " (~/.local/lib/babeltrace2/plugins)\n");
1890 fprintf(fp, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1891 fprintf(fp, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
1892 fprintf(fp, " current component (see the expected format\n");
1893 fprintf(fp, " of PARAMS below)\n");
1894 fprintf(fp, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1895 fprintf(fp, " dynamic plugins can be loaded\n");
1896 fprintf(fp, " -r, --reset-base-params Reset the current base parameters to an\n");
1897 fprintf(fp, " empty map\n");
1898 fprintf(fp, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
1899 fprintf(fp, " the graph later, retry in DUR µs\n");
1900 fprintf(fp, " (default: 100000)\n");
1901 fprintf(fp, " -h, --help Show this help and quit\n");
1902 fprintf(fp, "\n");
1903 fprintf(fp, "See `babeltrace2 --help` for the list of general options.\n");
1904 fprintf(fp, "\n\n");
1905 fprintf(fp, "Expected format of CONNECTION\n");
1906 fprintf(fp, "-----------------------------\n");
1907 fprintf(fp, "\n");
1908 fprintf(fp, " UPSTREAM[.UPSTREAM-PORT]:DOWNSTREAM[.DOWNSTREAM-PORT]\n");
1909 fprintf(fp, "\n");
1910 fprintf(fp, "UPSTREAM and DOWNSTREAM are names of the upstream and downstream\n");
1911 fprintf(fp, "components to connect together. You must escape the following characters\n\n");
1912 fprintf(fp, "with `\\`: `\\`, `.`, and `:`. You must set the name of the current\n");
1913 fprintf(fp, "component using the NAME prefix of the --component option.\n");
1914 fprintf(fp, "\n");
1915 fprintf(fp, "UPSTREAM-PORT and DOWNSTREAM-PORT are optional globbing patterns to\n");
1916 fprintf(fp, "identify the upstream and downstream ports to use for the connection.\n");
1917 fprintf(fp, "When the port is not specified, `*` is used.\n");
1918 fprintf(fp, "\n");
1919 fprintf(fp, "When a component named UPSTREAM has an available port which matches the\n");
1920 fprintf(fp, "UPSTREAM-PORT globbing pattern, it is connected to the first port which\n");
1921 fprintf(fp, "matches the DOWNSTREAM-PORT globbing pattern of the component named\n");
1922 fprintf(fp, "DOWNSTREAM.\n");
1923 fprintf(fp, "\n");
1924 fprintf(fp, "The only special character in UPSTREAM-PORT and DOWNSTREAM-PORT is `*`\n");
1925 fprintf(fp, "which matches anything. You must escape the following characters\n");
1926 fprintf(fp, "with `\\`: `\\`, `*`, `?`, `[`, `.`, and `:`.\n");
1927 fprintf(fp, "\n");
1928 fprintf(fp, "You can connect a source component to a filter or sink component. You\n");
1929 fprintf(fp, "can connect a filter component to a sink component.\n");
1930 fprintf(fp, "\n");
1931 fprintf(fp, "Examples:\n");
1932 fprintf(fp, "\n");
1933 fprintf(fp, " my-src:my-sink\n");
1934 fprintf(fp, " ctf-fs.*stream*:utils-muxer:*\n");
1935 fprintf(fp, "\n");
1936 fprintf(fp, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1937 fprintf(fp, "babeltrace2 from a shell.\n");
1938 fprintf(fp, "\n\n");
1939 print_expected_params_format(fp);
1940 }
1941
1942 /*
1943 * Creates a Babeltrace config object from the arguments of a run
1944 * command.
1945 *
1946 * *retcode is set to the appropriate exit code to use.
1947 */
1948 static
1949 struct bt_config *bt_config_run_from_args(int argc, const char *argv[],
1950 int *retcode, bool force_omit_system_plugin_path,
1951 bool force_omit_home_plugin_path,
1952 const bt_value *initial_plugin_paths, int default_log_level)
1953 {
1954 struct bt_config_component *cur_cfg_comp = NULL;
1955 bt_value *cur_base_params = NULL;
1956 int ret = 0;
1957 struct bt_config *cfg = NULL;
1958 bt_value *instance_names = NULL;
1959 bt_value *connection_args = NULL;
1960 char error_buf[256] = { 0 };
1961 long retry_duration = -1;
1962 bt_value_map_extend_status extend_status;
1963 GString *error_str = NULL;
1964 struct bt_argpar_parse_ret argpar_parse_ret = { 0 };
1965 int i;
1966
1967 static const struct bt_argpar_opt_descr run_options[] = {
1968 { OPT_BASE_PARAMS, 'b', "base-params", true },
1969 { OPT_COMPONENT, 'c', "component", true },
1970 { OPT_CONNECT, 'x', "connect", true },
1971 { OPT_HELP, 'h', "help", false },
1972 { OPT_LOG_LEVEL, 'l', "log-level", true },
1973 { OPT_OMIT_HOME_PLUGIN_PATH, '\0', "omit-home-plugin-path", false },
1974 { OPT_OMIT_SYSTEM_PLUGIN_PATH, '\0', "omit-system-plugin-path", false },
1975 { OPT_PARAMS, 'p', "params", true },
1976 { OPT_PLUGIN_PATH, '\0', "plugin-path", true },
1977 { OPT_RESET_BASE_PARAMS, 'r', "reset-base-params", false },
1978 { OPT_RETRY_DURATION, '\0', "retry-duration", true },
1979 BT_ARGPAR_OPT_DESCR_SENTINEL
1980 };
1981
1982 *retcode = 0;
1983
1984 error_str = g_string_new(NULL);
1985 if (!error_str) {
1986 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1987 goto error;
1988 }
1989
1990 if (argc < 1) {
1991 print_run_usage(stdout);
1992 *retcode = -1;
1993 goto end;
1994 }
1995
1996 cfg = bt_config_run_create(initial_plugin_paths);
1997 if (!cfg) {
1998 goto error;
1999 }
2000
2001 cfg->cmd_data.run.retry_duration_us = 100000;
2002 cfg->omit_system_plugin_path = force_omit_system_plugin_path;
2003 cfg->omit_home_plugin_path = force_omit_home_plugin_path;
2004 cur_base_params = bt_value_map_create();
2005 if (!cur_base_params) {
2006 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2007 goto error;
2008 }
2009
2010 instance_names = bt_value_map_create();
2011 if (!instance_names) {
2012 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2013 goto error;
2014 }
2015
2016 connection_args = bt_value_array_create();
2017 if (!connection_args) {
2018 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2019 goto error;
2020 }
2021
2022 ret = append_env_var_plugin_paths(cfg->plugin_paths);
2023 if (ret) {
2024 goto error;
2025 }
2026
2027 /* Parse options */
2028 argpar_parse_ret = bt_argpar_parse(argc, argv, run_options, true);
2029 if (argpar_parse_ret.error) {
2030 BT_CLI_LOGE_APPEND_CAUSE(
2031 "While parsing `run` command's command-line arguments: %s",
2032 argpar_parse_ret.error->str);
2033 goto error;
2034 }
2035
2036 if (help_option_is_specified(&argpar_parse_ret)) {
2037 print_run_usage(stdout);
2038 *retcode = -1;
2039 BT_OBJECT_PUT_REF_AND_RESET(cfg);
2040 goto end;
2041 }
2042
2043 for (i = 0; i < argpar_parse_ret.items->len; i++) {
2044 struct bt_argpar_item *argpar_item =
2045 g_ptr_array_index(argpar_parse_ret.items, i);
2046 struct bt_argpar_item_opt *argpar_item_opt;
2047 const char *arg;
2048
2049 /* This command does not accept non-option arguments.*/
2050 if (argpar_item->type == BT_ARGPAR_ITEM_TYPE_NON_OPT) {
2051 struct bt_argpar_item_non_opt *argpar_nonopt_item =
2052 (struct bt_argpar_item_non_opt *) argpar_item;
2053
2054 BT_CLI_LOGE_APPEND_CAUSE("Unexpected argument: `%s`",
2055 argpar_nonopt_item->arg);
2056 goto error;
2057 }
2058
2059 argpar_item_opt = (struct bt_argpar_item_opt *) argpar_item;
2060 arg = argpar_item_opt->arg;
2061
2062 switch (argpar_item_opt->descr->id) {
2063 case OPT_PLUGIN_PATH:
2064 if (bt_config_append_plugin_paths_check_setuid_setgid(
2065 cfg->plugin_paths, arg)) {
2066 goto error;
2067 }
2068 break;
2069 case OPT_OMIT_SYSTEM_PLUGIN_PATH:
2070 cfg->omit_system_plugin_path = true;
2071 break;
2072 case OPT_OMIT_HOME_PLUGIN_PATH:
2073 cfg->omit_home_plugin_path = true;
2074 break;
2075 case OPT_COMPONENT:
2076 {
2077 enum bt_config_component_dest dest;
2078
2079 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp);
2080 cur_cfg_comp = bt_config_component_from_arg(arg,
2081 default_log_level);
2082 if (!cur_cfg_comp) {
2083 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --component option's argument:\n %s",
2084 arg);
2085 goto error;
2086 }
2087
2088 switch (cur_cfg_comp->type) {
2089 case BT_COMPONENT_CLASS_TYPE_SOURCE:
2090 dest = BT_CONFIG_COMPONENT_DEST_SOURCE;
2091 break;
2092 case BT_COMPONENT_CLASS_TYPE_FILTER:
2093 dest = BT_CONFIG_COMPONENT_DEST_FILTER;
2094 break;
2095 case BT_COMPONENT_CLASS_TYPE_SINK:
2096 dest = BT_CONFIG_COMPONENT_DEST_SINK;
2097 break;
2098 default:
2099 abort();
2100 }
2101
2102 BT_ASSERT(cur_base_params);
2103 bt_value_put_ref(cur_cfg_comp->params);
2104 if (bt_value_copy(cur_base_params,
2105 &cur_cfg_comp->params) < 0) {
2106 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2107 goto error;
2108 }
2109
2110 ret = add_run_cfg_comp_check_name(cfg,
2111 cur_cfg_comp, dest,
2112 instance_names);
2113 if (ret) {
2114 goto error;
2115 }
2116
2117 break;
2118 }
2119 case OPT_PARAMS:
2120 {
2121 bt_value *params;
2122 bt_value *params_to_set;
2123
2124 if (!cur_cfg_comp) {
2125 BT_CLI_LOGE_APPEND_CAUSE("Cannot add parameters to unavailable component:\n %s",
2126 arg);
2127 goto error;
2128 }
2129
2130 params = cli_value_from_arg(arg, error_str);
2131 if (!params) {
2132 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
2133 error_str->str);
2134 goto error;
2135 }
2136
2137 extend_status = bt_value_map_extend(
2138 cur_cfg_comp->params, params, &params_to_set);
2139 BT_VALUE_PUT_REF_AND_RESET(params);
2140 if (extend_status != BT_VALUE_MAP_EXTEND_STATUS_OK) {
2141 BT_CLI_LOGE_APPEND_CAUSE("Cannot extend current component parameters with --params option's argument:\n %s",
2142 arg);
2143 goto error;
2144 }
2145
2146 BT_OBJECT_MOVE_REF(cur_cfg_comp->params, params_to_set);
2147 break;
2148 }
2149 case OPT_LOG_LEVEL:
2150 if (!cur_cfg_comp) {
2151 BT_CLI_LOGE_APPEND_CAUSE("Cannot set the log level of unavailable component:\n %s",
2152 arg);
2153 goto error;
2154 }
2155
2156 cur_cfg_comp->log_level =
2157 bt_log_get_level_from_string(arg);
2158 if (cur_cfg_comp->log_level < 0) {
2159 BT_CLI_LOGE_APPEND_CAUSE("Invalid argument for --log-level option:\n %s",
2160 arg);
2161 goto error;
2162 }
2163 break;
2164 case OPT_BASE_PARAMS:
2165 {
2166 bt_value *params = cli_value_from_arg(arg, error_str);
2167
2168 if (!params) {
2169 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --base-params option's argument:\n %s",
2170 error_str->str);
2171 goto error;
2172 }
2173
2174 BT_OBJECT_MOVE_REF(cur_base_params, params);
2175 break;
2176 }
2177 case OPT_RESET_BASE_PARAMS:
2178 BT_VALUE_PUT_REF_AND_RESET(cur_base_params);
2179 cur_base_params = bt_value_map_create();
2180 if (!cur_base_params) {
2181 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2182 goto error;
2183 }
2184 break;
2185 case OPT_CONNECT:
2186 if (bt_value_array_append_string_element(
2187 connection_args, arg)) {
2188 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2189 goto error;
2190 }
2191 break;
2192 case OPT_RETRY_DURATION: {
2193 gchar *end;
2194 size_t arg_len = strlen(argpar_item_opt->arg);
2195
2196 retry_duration = g_ascii_strtoll(argpar_item_opt->arg, &end, 10);
2197
2198 if (arg_len == 0 || end != (argpar_item_opt->arg + arg_len)) {
2199 BT_CLI_LOGE_APPEND_CAUSE(
2200 "Could not parse --retry-duration option's argument as an unsigned integer: `%s`",
2201 argpar_item_opt->arg);
2202 goto error;
2203 }
2204
2205 if (retry_duration < 0) {
2206 BT_CLI_LOGE_APPEND_CAUSE("--retry-duration option's argument must be positive or 0: %ld",
2207 retry_duration);
2208 goto error;
2209 }
2210
2211 cfg->cmd_data.run.retry_duration_us =
2212 (uint64_t) retry_duration;
2213 break;
2214 }
2215 default:
2216 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
2217 argpar_item_opt->descr->id);
2218 goto error;
2219 }
2220 }
2221
2222 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp);
2223
2224 if (cfg->cmd_data.run.sources->len == 0) {
2225 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no source component.");
2226 goto error;
2227 }
2228
2229 if (cfg->cmd_data.run.sinks->len == 0) {
2230 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no sink component.");
2231 goto error;
2232 }
2233
2234 if (append_home_and_system_plugin_paths_cfg(cfg)) {
2235 goto error;
2236 }
2237
2238 ret = bt_config_cli_args_create_connections(cfg,
2239 connection_args,
2240 error_buf, 256);
2241 if (ret) {
2242 BT_CLI_LOGE_APPEND_CAUSE("Cannot creation connections:\n%s", error_buf);
2243 goto error;
2244 }
2245
2246 goto end;
2247
2248 error:
2249 *retcode = 1;
2250 BT_OBJECT_PUT_REF_AND_RESET(cfg);
2251
2252 end:
2253 if (error_str) {
2254 g_string_free(error_str, TRUE);
2255 }
2256
2257 bt_argpar_parse_ret_fini(&argpar_parse_ret);
2258 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp);
2259 BT_VALUE_PUT_REF_AND_RESET(cur_base_params);
2260 BT_VALUE_PUT_REF_AND_RESET(instance_names);
2261 BT_VALUE_PUT_REF_AND_RESET(connection_args);
2262 return cfg;
2263 }
2264
2265 static
2266 struct bt_config *bt_config_run_from_args_array(const bt_value *run_args,
2267 int *retcode, bool force_omit_system_plugin_path,
2268 bool force_omit_home_plugin_path,
2269 const bt_value *initial_plugin_paths, int default_log_level)
2270 {
2271 struct bt_config *cfg = NULL;
2272 const char **argv;
2273 int64_t i, len;
2274 const size_t argc = bt_value_array_get_size(run_args);
2275
2276 argv = calloc(argc, sizeof(*argv));
2277 if (!argv) {
2278 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2279 goto end;
2280 }
2281
2282 len = bt_value_array_get_size(run_args);
2283 if (len < 0) {
2284 BT_CLI_LOGE_APPEND_CAUSE("Invalid executable arguments.");
2285 goto end;
2286 }
2287 for (i = 0; i < len; i++) {
2288 const bt_value *arg_value =
2289 bt_value_array_borrow_element_by_index_const(run_args,
2290 i);
2291 const char *arg;
2292
2293 BT_ASSERT(arg_value);
2294 arg = bt_value_string_get(arg_value);
2295 BT_ASSERT(arg);
2296 argv[i] = arg;
2297 }
2298
2299 cfg = bt_config_run_from_args(argc, argv, retcode,
2300 force_omit_system_plugin_path, force_omit_home_plugin_path,
2301 initial_plugin_paths, default_log_level);
2302
2303 end:
2304 free(argv);
2305 return cfg;
2306 }
2307
2308 /*
2309 * Prints the convert command usage.
2310 */
2311 static
2312 void print_convert_usage(FILE *fp)
2313 {
2314 fprintf(fp, "Usage: babeltrace2 [GENERAL OPTIONS] [convert] [OPTIONS] [PATH/URL]\n");
2315 fprintf(fp, "\n");
2316 fprintf(fp, "Options:\n");
2317 fprintf(fp, "\n");
2318 fprintf(fp, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2319 fprintf(fp, " Instantiate the component class CLS of type\n");
2320 fprintf(fp, " TYPE (`source`, `filter`, or `sink`) found\n");
2321 fprintf(fp, " in the plugin PLUGIN, add it to the\n");
2322 fprintf(fp, " conversion graph, and optionally name it\n");
2323 fprintf(fp, " NAME\n");
2324 fprintf(fp, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
2325 fprintf(fp, " (`N`, `V`, `D`, `I`, `W`, `E`, or `F`)\n");
2326 fprintf(fp, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2327 fprintf(fp, " (~/.local/lib/babeltrace2/plugins)\n");
2328 fprintf(fp, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2329 fprintf(fp, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2330 fprintf(fp, " current component (see the expected format\n");
2331 fprintf(fp, " of PARAMS below)\n");
2332 fprintf(fp, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2333 fprintf(fp, " dynamic plugins can be loaded\n");
2334 fprintf(fp, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
2335 fprintf(fp, " the graph later, retry in DUR µs\n");
2336 fprintf(fp, " (default: 100000)\n");
2337 fprintf(fp, " dynamic plugins can be loaded\n");
2338 fprintf(fp, " --run-args Print the equivalent arguments for the\n");
2339 fprintf(fp, " `run` command to the standard output,\n");
2340 fprintf(fp, " formatted for a shell, and quit\n");
2341 fprintf(fp, " --run-args-0 Print the equivalent arguments for the\n");
2342 fprintf(fp, " `run` command to the standard output,\n");
2343 fprintf(fp, " formatted for `xargs -0`, and quit\n");
2344 fprintf(fp, " --stream-intersection Only process events when all streams\n");
2345 fprintf(fp, " are active\n");
2346 fprintf(fp, " -h, --help Show this help and quit\n");
2347 fprintf(fp, "\n");
2348 fprintf(fp, "Implicit `source.ctf.fs` component options:\n");
2349 fprintf(fp, "\n");
2350 fprintf(fp, " --clock-offset=SEC Set clock offset to SEC seconds\n");
2351 fprintf(fp, " --clock-offset-ns=NS Set clock offset to NS ns\n");
2352 fprintf(fp, "\n");
2353 fprintf(fp, "Implicit `sink.text.pretty` component options:\n");
2354 fprintf(fp, "\n");
2355 fprintf(fp, " --clock-cycles Print timestamps in clock cycles\n");
2356 fprintf(fp, " --clock-date Print timestamp dates\n");
2357 fprintf(fp, " --clock-gmt Print and parse timestamps in the GMT\n");
2358 fprintf(fp, " time zone instead of the local time zone\n");
2359 fprintf(fp, " --clock-seconds Print the timestamps as `SEC.NS` instead\n");
2360 fprintf(fp, " of `hh:mm:ss.nnnnnnnnn`\n");
2361 fprintf(fp, " --color=(never | auto | always)\n");
2362 fprintf(fp, " Never, automatically, or always emit\n");
2363 fprintf(fp, " console color codes\n");
2364 fprintf(fp, " -f, --fields=FIELD[,FIELD]... Print additional fields; FIELD can be:\n");
2365 fprintf(fp, " `all`, `trace`, `trace:hostname`,\n");
2366 fprintf(fp, " `trace:domain`, `trace:procname`,\n");
2367 fprintf(fp, " `trace:vpid`, `loglevel`, `emf`\n");
2368 fprintf(fp, " -n, --names=NAME[,NAME]... Print field names; NAME can be:\n");
2369 fprintf(fp, " `payload` (or `arg` or `args`), `none`,\n");
2370 fprintf(fp, " `all`, `scope`, `header`, `context`\n");
2371 fprintf(fp, " (or `ctx`)\n");
2372 fprintf(fp, " --no-delta Do not print time delta between\n");
2373 fprintf(fp, " consecutive events\n");
2374 fprintf(fp, " -w, --output=PATH Write output text to PATH instead of\n");
2375 fprintf(fp, " the standard output\n");
2376 fprintf(fp, "\n");
2377 fprintf(fp, "Implicit `filter.utils.muxer` component options:\n");
2378 fprintf(fp, "\n");
2379 fprintf(fp, " --clock-force-correlate Assume that clocks are inherently\n");
2380 fprintf(fp, " correlated across traces\n");
2381 fprintf(fp, "\n");
2382 fprintf(fp, "Implicit `filter.utils.trimmer` component options:\n");
2383 fprintf(fp, "\n");
2384 fprintf(fp, " -b, --begin=BEGIN Set the beginning time of the conversion\n");
2385 fprintf(fp, " time range to BEGIN (see the format of\n");
2386 fprintf(fp, " BEGIN below)\n");
2387 fprintf(fp, " -e, --end=END Set the end time of the conversion time\n");
2388 fprintf(fp, " range to END (see the format of END below)\n");
2389 fprintf(fp, " -t, --timerange=TIMERANGE Set conversion time range to TIMERANGE:\n");
2390 fprintf(fp, " BEGIN,END or [BEGIN,END] (literally `[` and\n");
2391 fprintf(fp, " `]`) (see the format of BEGIN/END below)\n");
2392 fprintf(fp, "\n");
2393 fprintf(fp, "Implicit `filter.lttng-utils.debug-info` component options:\n");
2394 fprintf(fp, "\n");
2395 fprintf(fp, " --debug-info Create an implicit\n");
2396 fprintf(fp, " `filter.lttng-utils.debug-info` component\n");
2397 fprintf(fp, " --debug-info-dir=DIR Search for debug info in directory DIR\n");
2398 fprintf(fp, " instead of `/usr/lib/debug`\n");
2399 fprintf(fp, " --debug-info-full-path Show full debug info source and\n");
2400 fprintf(fp, " binary paths instead of just names\n");
2401 fprintf(fp, " --debug-info-target-prefix=DIR\n");
2402 fprintf(fp, " Use directory DIR as a prefix when\n");
2403 fprintf(fp, " looking up executables during debug\n");
2404 fprintf(fp, " info analysis\n");
2405 fprintf(fp, "\n");
2406 fprintf(fp, "Legacy options that still work:\n");
2407 fprintf(fp, "\n");
2408 fprintf(fp, " -i, --input-format=(ctf | lttng-live)\n");
2409 fprintf(fp, " `ctf`:\n");
2410 fprintf(fp, " Create an implicit `source.ctf.fs`\n");
2411 fprintf(fp, " component\n");
2412 fprintf(fp, " `lttng-live`:\n");
2413 fprintf(fp, " Create an implicit `source.ctf.lttng-live`\n");
2414 fprintf(fp, " component\n");
2415 fprintf(fp, " -o, --output-format=(text | ctf | dummy | ctf-metadata)\n");
2416 fprintf(fp, " `text`:\n");
2417 fprintf(fp, " Create an implicit `sink.text.pretty`\n");
2418 fprintf(fp, " component\n");
2419 fprintf(fp, " `ctf`:\n");
2420 fprintf(fp, " Create an implicit `sink.ctf.fs`\n");
2421 fprintf(fp, " component\n");
2422 fprintf(fp, " `dummy`:\n");
2423 fprintf(fp, " Create an implicit `sink.utils.dummy`\n");
2424 fprintf(fp, " component\n");
2425 fprintf(fp, " `ctf-metadata`:\n");
2426 fprintf(fp, " Query the `source.ctf.fs` component class\n");
2427 fprintf(fp, " for metadata text and quit\n");
2428 fprintf(fp, "\n");
2429 fprintf(fp, "See `babeltrace2 --help` for the list of general options.\n");
2430 fprintf(fp, "\n\n");
2431 fprintf(fp, "Format of BEGIN and END\n");
2432 fprintf(fp, "-----------------------\n");
2433 fprintf(fp, "\n");
2434 fprintf(fp, " [YYYY-MM-DD [hh:mm:]]ss[.nnnnnnnnn]\n");
2435 fprintf(fp, "\n\n");
2436 print_expected_params_format(fp);
2437 }
2438
2439 static
2440 const struct bt_argpar_opt_descr convert_options[] = {
2441 /* id, short_name, long_name, with_arg */
2442 { OPT_BEGIN, 'b', "begin", true },
2443 { OPT_CLOCK_CYCLES, '\0', "clock-cycles", false },
2444 { OPT_CLOCK_DATE, '\0', "clock-date", false },
2445 { OPT_CLOCK_FORCE_CORRELATE, '\0', "clock-force-correlate", false },
2446 { OPT_CLOCK_GMT, '\0', "clock-gmt", false },
2447 { OPT_CLOCK_OFFSET, '\0', "clock-offset", true },
2448 { OPT_CLOCK_OFFSET_NS, '\0', "clock-offset-ns", true },
2449 { OPT_CLOCK_SECONDS, '\0', "clock-seconds", false },
2450 { OPT_COLOR, '\0', "color", true },
2451 { OPT_COMPONENT, 'c', "component", true },
2452 { OPT_DEBUG, 'd', "debug", false },
2453 { OPT_DEBUG_INFO_DIR, '\0', "debug-info-dir", true },
2454 { OPT_DEBUG_INFO_FULL_PATH, '\0', "debug-info-full-path", false },
2455 { OPT_DEBUG_INFO_TARGET_PREFIX, '\0', "debug-info-target-prefix", true },
2456 { OPT_END, 'e', "end", true },
2457 { OPT_FIELDS, 'f', "fields", true },
2458 { OPT_HELP, 'h', "help", false },
2459 { OPT_INPUT_FORMAT, 'i', "input-format", true },
2460 { OPT_LOG_LEVEL, 'l', "log-level", true },
2461 { OPT_NAMES, 'n', "names", true },
2462 { OPT_DEBUG_INFO, '\0', "debug-info", false },
2463 { OPT_NO_DELTA, '\0', "no-delta", false },
2464 { OPT_OMIT_HOME_PLUGIN_PATH, '\0', "omit-home-plugin-path", false },
2465 { OPT_OMIT_SYSTEM_PLUGIN_PATH, '\0', "omit-system-plugin-path", false },
2466 { OPT_OUTPUT, 'w', "output", true },
2467 { OPT_OUTPUT_FORMAT, 'o', "output-format", true },
2468 { OPT_PARAMS, 'p', "params", true },
2469 { OPT_PLUGIN_PATH, '\0', "plugin-path", true },
2470 { OPT_RETRY_DURATION, '\0', "retry-duration", true },
2471 { OPT_RUN_ARGS, '\0', "run-args", false },
2472 { OPT_RUN_ARGS_0, '\0', "run-args-0", false },
2473 { OPT_STREAM_INTERSECTION, '\0', "stream-intersection", false },
2474 { OPT_TIMERANGE, '\0', "timerange", true },
2475 { OPT_VERBOSE, 'v', "verbose", false },
2476 BT_ARGPAR_OPT_DESCR_SENTINEL
2477 };
2478
2479 static
2480 GString *get_component_auto_name(const char *prefix,
2481 const bt_value *existing_names)
2482 {
2483 unsigned int i = 0;
2484 GString *auto_name = g_string_new(NULL);
2485
2486 if (!auto_name) {
2487 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2488 goto end;
2489 }
2490
2491 if (!bt_value_map_has_entry(existing_names, prefix)) {
2492 g_string_assign(auto_name, prefix);
2493 goto end;
2494 }
2495
2496 do {
2497 g_string_printf(auto_name, "%s-%d", prefix, i);
2498 i++;
2499 } while (bt_value_map_has_entry(existing_names, auto_name->str));
2500
2501 end:
2502 return auto_name;
2503 }
2504
2505 struct implicit_component_args {
2506 bool exists;
2507
2508 /* The component class name (e.g. src.ctf.fs). */
2509 GString *comp_arg;
2510
2511 /* The component instance name. */
2512 GString *name_arg;
2513
2514 GString *params_arg;
2515 bt_value *extra_params;
2516 };
2517
2518 static
2519 int assign_name_to_implicit_component(struct implicit_component_args *args,
2520 const char *prefix, bt_value *existing_names,
2521 GList **comp_names, bool append_to_comp_names)
2522 {
2523 int ret = 0;
2524 GString *name = NULL;
2525
2526 if (!args->exists) {
2527 goto end;
2528 }
2529
2530 name = get_component_auto_name(prefix,
2531 existing_names);
2532
2533 if (!name) {
2534 ret = -1;
2535 goto end;
2536 }
2537
2538 g_string_assign(args->name_arg, name->str);
2539
2540 if (bt_value_map_insert_entry(existing_names, name->str,
2541 bt_value_null)) {
2542 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2543 ret = -1;
2544 goto end;
2545 }
2546
2547 if (append_to_comp_names) {
2548 *comp_names = g_list_append(*comp_names, name);
2549 name = NULL;
2550 }
2551
2552 end:
2553 if (name) {
2554 g_string_free(name, TRUE);
2555 }
2556
2557 return ret;
2558 }
2559
2560 static
2561 int append_run_args_for_implicit_component(
2562 struct implicit_component_args *impl_args,
2563 bt_value *run_args)
2564 {
2565 int ret = 0;
2566 size_t i;
2567 GString *component_arg_for_run = NULL;
2568
2569 if (!impl_args->exists) {
2570 goto end;
2571 }
2572
2573 component_arg_for_run = g_string_new(NULL);
2574 if (!component_arg_for_run) {
2575 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2576 goto error;
2577 }
2578
2579 /* Build the full `name:type.plugin.cls`. */
2580 BT_ASSERT(!strchr(impl_args->name_arg->str, '\\'));
2581 BT_ASSERT(!strchr(impl_args->name_arg->str, ':'));
2582 g_string_printf(component_arg_for_run, "%s:%s",
2583 impl_args->name_arg->str, impl_args->comp_arg->str);
2584
2585 if (bt_value_array_append_string_element(run_args, "--component")) {
2586 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2587 goto error;
2588 }
2589
2590 if (bt_value_array_append_string_element(run_args,
2591 component_arg_for_run->str)) {
2592 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2593 goto error;
2594 }
2595
2596 if (impl_args->params_arg->len > 0) {
2597 if (bt_value_array_append_string_element(run_args, "--params")) {
2598 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2599 goto error;
2600 }
2601
2602 if (bt_value_array_append_string_element(run_args,
2603 impl_args->params_arg->str)) {
2604 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2605 goto error;
2606 }
2607 }
2608
2609 for (i = 0; i < bt_value_array_get_size(impl_args->extra_params);
2610 i++) {
2611 const bt_value *elem;
2612 const char *arg;
2613
2614 elem = bt_value_array_borrow_element_by_index(impl_args->extra_params,
2615 i);
2616 if (!elem) {
2617 goto error;
2618 }
2619
2620 BT_ASSERT(bt_value_is_string(elem));
2621 arg = bt_value_string_get(elem);
2622 ret = bt_value_array_append_string_element(run_args, arg);
2623 if (ret) {
2624 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2625 goto error;
2626 }
2627 }
2628
2629 goto end;
2630
2631 error:
2632 ret = -1;
2633
2634 end:
2635 if (component_arg_for_run) {
2636 g_string_free(component_arg_for_run, TRUE);
2637 }
2638
2639 return ret;
2640 }
2641
2642 /* Free the fields of a `struct implicit_component_args`. */
2643
2644 static
2645 void finalize_implicit_component_args(struct implicit_component_args *args)
2646 {
2647 BT_ASSERT(args);
2648
2649 if (args->comp_arg) {
2650 g_string_free(args->comp_arg, TRUE);
2651 }
2652
2653 if (args->name_arg) {
2654 g_string_free(args->name_arg, TRUE);
2655 }
2656
2657 if (args->params_arg) {
2658 g_string_free(args->params_arg, TRUE);
2659 }
2660
2661 bt_value_put_ref(args->extra_params);
2662 }
2663
2664 /* Destroy a dynamically-allocated `struct implicit_component_args`. */
2665
2666 static
2667 void destroy_implicit_component_args(struct implicit_component_args *args)
2668 {
2669 finalize_implicit_component_args(args);
2670 g_free(args);
2671 }
2672
2673 /* Initialize the fields of an already allocated `struct implicit_component_args`. */
2674
2675 static
2676 int init_implicit_component_args(struct implicit_component_args *args,
2677 const char *comp_arg, bool exists)
2678 {
2679 int ret = 0;
2680
2681 args->exists = exists;
2682 args->comp_arg = g_string_new(comp_arg);
2683 args->name_arg = g_string_new(NULL);
2684 args->params_arg = g_string_new(NULL);
2685 args->extra_params = bt_value_array_create();
2686
2687 if (!args->comp_arg || !args->name_arg ||
2688 !args->params_arg || !args->extra_params) {
2689 ret = -1;
2690 finalize_implicit_component_args(args);
2691 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2692 goto end;
2693 }
2694
2695 end:
2696 return ret;
2697 }
2698
2699 /* Dynamically allocate and initialize a `struct implicit_component_args`. */
2700
2701 static
2702 struct implicit_component_args *create_implicit_component_args(
2703 const char *comp_arg)
2704 {
2705 struct implicit_component_args *args;
2706 int status;
2707
2708 args = g_new(struct implicit_component_args, 1);
2709 if (!args) {
2710 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2711 goto end;
2712 }
2713
2714 status = init_implicit_component_args(args, comp_arg, true);
2715 if (status != 0) {
2716 g_free(args);
2717 args = NULL;
2718 }
2719
2720 end:
2721 return args;
2722 }
2723
2724 static
2725 void append_implicit_component_param(struct implicit_component_args *args,
2726 const char *key, const char *value)
2727 {
2728 BT_ASSERT(args);
2729 BT_ASSERT(key);
2730 BT_ASSERT(value);
2731 append_param_arg(args->params_arg, key, value);
2732 }
2733
2734 /*
2735 * Append the given parameter (`key=value`) to all component specifications
2736 * in `implicit_comp_args` (an array of `struct implicit_component_args *`)
2737 * which match `comp_arg`.
2738 *
2739 * Return the number of matching components.
2740 */
2741
2742 static
2743 int append_multiple_implicit_components_param(GPtrArray *implicit_comp_args,
2744 const char *comp_arg, const char *key, const char *value)
2745 {
2746 int i;
2747 int n = 0;
2748
2749 for (i = 0; i < implicit_comp_args->len; i++) {
2750 struct implicit_component_args *args = implicit_comp_args->pdata[i];
2751
2752 if (strcmp(args->comp_arg->str, comp_arg) == 0) {
2753 append_implicit_component_param(args, key, value);
2754 n++;
2755 }
2756 }
2757
2758 return n;
2759 }
2760
2761 /* Escape value to make it suitable to use as a string parameter value. */
2762 static
2763 gchar *escape_string_value(const char *value)
2764 {
2765 GString *ret;
2766 const char *in;
2767
2768 ret = g_string_new(NULL);
2769 if (!ret) {
2770 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2771 goto end;
2772 }
2773
2774 in = value;
2775 while (*in) {
2776 switch (*in) {
2777 case '"':
2778 case '\\':
2779 g_string_append_c(ret, '\\');
2780 break;
2781 }
2782
2783 g_string_append_c(ret, *in);
2784
2785 in++;
2786 }
2787
2788 end:
2789 return g_string_free(ret, FALSE);
2790 }
2791
2792 static
2793 int bt_value_to_cli_param_value_append(const bt_value *value, GString *buf)
2794 {
2795 BT_ASSERT(buf);
2796
2797 int ret = -1;
2798
2799 switch (bt_value_get_type(value)) {
2800 case BT_VALUE_TYPE_STRING:
2801 {
2802 const char *str_value = bt_value_string_get(value);
2803 gchar *escaped_str_value;
2804
2805 escaped_str_value = escape_string_value(str_value);
2806 if (!escaped_str_value) {
2807 goto end;
2808 }
2809
2810 g_string_append_printf(buf, "\"%s\"", escaped_str_value);
2811
2812 g_free(escaped_str_value);
2813 break;
2814 }
2815 case BT_VALUE_TYPE_ARRAY: {
2816 g_string_append_c(buf, '[');
2817 uint64_t sz = bt_value_array_get_size(value);
2818 for (uint64_t i = 0; i < sz; i++) {
2819 const bt_value *item;
2820 int ret;
2821
2822 if (i > 0) {
2823 g_string_append(buf, ", ");
2824 }
2825
2826 item = bt_value_array_borrow_element_by_index_const(
2827 value, i);
2828 ret = bt_value_to_cli_param_value_append(item, buf);
2829
2830 if (ret) {
2831 goto end;
2832 }
2833 }
2834 g_string_append_c(buf, ']');
2835 break;
2836 }
2837 default:
2838 abort();
2839 }
2840
2841 ret = 0;
2842
2843 end:
2844 return ret;
2845 }
2846
2847 /*
2848 * Convert `value` to its equivalent representation as a command line parameter
2849 * value.
2850 */
2851
2852 static
2853 gchar *bt_value_to_cli_param_value(bt_value *value)
2854 {
2855 GString *buf;
2856 gchar *result = NULL;
2857
2858 buf = g_string_new(NULL);
2859 if (!buf) {
2860 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2861 goto error;
2862 }
2863
2864 if (bt_value_to_cli_param_value_append(value, buf)) {
2865 goto error;
2866 }
2867
2868 result = g_string_free(buf, FALSE);
2869 buf = NULL;
2870
2871 goto end;
2872
2873 error:
2874 if (buf) {
2875 g_string_free(buf, TRUE);
2876 }
2877
2878 end:
2879 return result;
2880 }
2881
2882 static
2883 int append_parameter_to_args(bt_value *args, const char *key, bt_value *value)
2884 {
2885 BT_ASSERT(args);
2886 BT_ASSERT(bt_value_get_type(args) == BT_VALUE_TYPE_ARRAY);
2887 BT_ASSERT(key);
2888 BT_ASSERT(value);
2889
2890 int ret = 0;
2891 gchar *str_value = NULL;
2892 GString *parameter = NULL;
2893
2894 if (bt_value_array_append_string_element(args, "--params")) {
2895 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2896 ret = -1;
2897 goto end;
2898 }
2899
2900 str_value = bt_value_to_cli_param_value(value);
2901 if (!str_value) {
2902 ret = -1;
2903 goto end;
2904 }
2905
2906 parameter = g_string_new(NULL);
2907 if (!parameter) {
2908 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2909 ret = -1;
2910 goto end;
2911 }
2912
2913 g_string_printf(parameter, "%s=%s", key, str_value);
2914
2915 if (bt_value_array_append_string_element(args, parameter->str)) {
2916 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2917 ret = -1;
2918 goto end;
2919 }
2920
2921 end:
2922 if (parameter) {
2923 g_string_free(parameter, TRUE);
2924 parameter = NULL;
2925 }
2926
2927 if (str_value) {
2928 g_free(str_value);
2929 str_value = NULL;
2930 }
2931
2932 return ret;
2933 }
2934
2935 static
2936 int append_string_parameter_to_args(bt_value *args, const char *key, const char *value)
2937 {
2938 bt_value *str_value;
2939 int ret;
2940
2941 str_value = bt_value_string_create_init(value);
2942
2943 if (!str_value) {
2944 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2945 ret = -1;
2946 goto end;
2947 }
2948
2949 ret = append_parameter_to_args(args, key, str_value);
2950
2951 end:
2952 BT_VALUE_PUT_REF_AND_RESET(str_value);
2953 return ret;
2954 }
2955
2956 static
2957 int append_implicit_component_extra_param(struct implicit_component_args *args,
2958 const char *key, const char *value)
2959 {
2960 return append_string_parameter_to_args(args->extra_params, key, value);
2961 }
2962
2963 /*
2964 * Escapes `.`, `:`, and `\` of `input` with `\`.
2965 */
2966 static
2967 GString *escape_dot_colon(const char *input)
2968 {
2969 GString *output = g_string_new(NULL);
2970 const char *ch;
2971
2972 if (!output) {
2973 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2974 goto end;
2975 }
2976
2977 for (ch = input; *ch != '\0'; ch++) {
2978 if (*ch == '\\' || *ch == '.' || *ch == ':') {
2979 g_string_append_c(output, '\\');
2980 }
2981
2982 g_string_append_c(output, *ch);
2983 }
2984
2985 end:
2986 return output;
2987 }
2988
2989 /*
2990 * Appends a --connect option to a list of arguments. `upstream_name`
2991 * and `downstream_name` are escaped with escape_dot_colon() in this
2992 * function.
2993 */
2994 static
2995 int append_connect_arg(bt_value *run_args,
2996 const char *upstream_name, const char *downstream_name)
2997 {
2998 int ret = 0;
2999 GString *e_upstream_name = escape_dot_colon(upstream_name);
3000 GString *e_downstream_name = escape_dot_colon(downstream_name);
3001 GString *arg = g_string_new(NULL);
3002
3003 if (!e_upstream_name || !e_downstream_name || !arg) {
3004 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3005 ret = -1;
3006 goto end;
3007 }
3008
3009 ret = bt_value_array_append_string_element(run_args, "--connect");
3010 if (ret) {
3011 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3012 ret = -1;
3013 goto end;
3014 }
3015
3016 g_string_append(arg, e_upstream_name->str);
3017 g_string_append_c(arg, ':');
3018 g_string_append(arg, e_downstream_name->str);
3019 ret = bt_value_array_append_string_element(run_args, arg->str);
3020 if (ret) {
3021 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3022 ret = -1;
3023 goto end;
3024 }
3025
3026 end:
3027 if (arg) {
3028 g_string_free(arg, TRUE);
3029 }
3030
3031 if (e_upstream_name) {
3032 g_string_free(e_upstream_name, TRUE);
3033 }
3034
3035 if (e_downstream_name) {
3036 g_string_free(e_downstream_name, TRUE);
3037 }
3038
3039 return ret;
3040 }
3041
3042 /*
3043 * Appends the run command's --connect options for the convert command.
3044 */
3045 static
3046 int convert_auto_connect(bt_value *run_args,
3047 GList *source_names, GList *filter_names,
3048 GList *sink_names)
3049 {
3050 int ret = 0;
3051 GList *source_at = source_names;
3052 GList *filter_at = filter_names;
3053 GList *filter_prev;
3054 GList *sink_at = sink_names;
3055
3056 BT_ASSERT(source_names);
3057 BT_ASSERT(filter_names);
3058 BT_ASSERT(sink_names);
3059
3060 /* Connect all sources to the first filter */
3061 for (source_at = source_names; source_at; source_at = g_list_next(source_at)) {
3062 GString *source_name = source_at->data;
3063 GString *filter_name = filter_at->data;
3064
3065 ret = append_connect_arg(run_args, source_name->str,
3066 filter_name->str);
3067 if (ret) {
3068 goto error;
3069 }
3070 }
3071
3072 filter_prev = filter_at;
3073 filter_at = g_list_next(filter_at);
3074
3075 /* Connect remaining filters */
3076 for (; filter_at; filter_prev = filter_at, filter_at = g_list_next(filter_at)) {
3077 GString *filter_name = filter_at->data;
3078 GString *filter_prev_name = filter_prev->data;
3079
3080 ret = append_connect_arg(run_args, filter_prev_name->str,
3081 filter_name->str);
3082 if (ret) {
3083 goto error;
3084 }
3085 }
3086
3087 /* Connect last filter to all sinks */
3088 for (sink_at = sink_names; sink_at; sink_at = g_list_next(sink_at)) {
3089 GString *filter_name = filter_prev->data;
3090 GString *sink_name = sink_at->data;
3091
3092 ret = append_connect_arg(run_args, filter_name->str,
3093 sink_name->str);
3094 if (ret) {
3095 goto error;
3096 }
3097 }
3098
3099 goto end;
3100
3101 error:
3102 ret = -1;
3103
3104 end:
3105 return ret;
3106 }
3107
3108 static
3109 int split_timerange(const char *arg, char **begin, char **end)
3110 {
3111 int ret = 0;
3112 const char *ch = arg;
3113 size_t end_pos;
3114 GString *g_begin = NULL;
3115 GString *g_end = NULL;
3116
3117 BT_ASSERT(arg);
3118
3119 if (*ch == '[') {
3120 ch++;
3121 }
3122
3123 g_begin = bt_common_string_until(ch, "", ",", &end_pos);
3124 if (!g_begin || ch[end_pos] != ',' || g_begin->len == 0) {
3125 goto error;
3126 }
3127
3128 ch += end_pos + 1;
3129
3130 g_end = bt_common_string_until(ch, "", "]", &end_pos);
3131 if (!g_end || g_end->len == 0) {
3132 goto error;
3133 }
3134
3135 BT_ASSERT(begin);
3136 BT_ASSERT(end);
3137 *begin = g_begin->str;
3138 *end = g_end->str;
3139 g_string_free(g_begin, FALSE);
3140 g_string_free(g_end, FALSE);
3141 g_begin = NULL;
3142 g_end = NULL;
3143 goto end;
3144
3145 error:
3146 ret = -1;
3147
3148 end:
3149 if (g_begin) {
3150 g_string_free(g_begin, TRUE);
3151 }
3152
3153 if (g_end) {
3154 g_string_free(g_end, TRUE);
3155 }
3156
3157 return ret;
3158 }
3159
3160 static
3161 int g_list_prepend_gstring(GList **list, const char *string)
3162 {
3163 int ret = 0;
3164 GString *gs = g_string_new(string);
3165
3166 BT_ASSERT(list);
3167
3168 if (!gs) {
3169 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3170 goto end;
3171 }
3172
3173 *list = g_list_prepend(*list, gs);
3174
3175 end:
3176 return ret;
3177 }
3178
3179 /*
3180 * Create `struct implicit_component_args` structures for each of the
3181 * source components we identified. Add them to `component_args`.
3182 *
3183 * `non_opt_params` is an array where each element is an array of
3184 * strings containing all the arguments to `--params` that apply to the
3185 * non-option argument at the same index. For example, if, for a
3186 * non-option argument, the following `--params` options applied:
3187 *
3188 * --params=a=2 --params=b=3,c=4
3189 *
3190 * its entry in `non_opt_params` would contain
3191 *
3192 * ["a=2", "b=3,c=4"]
3193 */
3194
3195 static
3196 int create_implicit_component_args_from_auto_discovered_sources(
3197 const struct auto_source_discovery *auto_disc,
3198 const bt_value *non_opt_params,
3199 const bt_value *non_opt_loglevels,
3200 GPtrArray *component_args)
3201 {
3202 gchar *cc_name = NULL;
3203 struct implicit_component_args *comp = NULL;
3204 int status;
3205 guint i, len;
3206
3207 len = auto_disc->results->len;
3208
3209 for (i = 0; i < len; i++) {
3210 struct auto_source_discovery_result *res =
3211 g_ptr_array_index(auto_disc->results, i);
3212 uint64_t orig_indices_i, orig_indices_count;
3213
3214 g_free(cc_name);
3215 cc_name = g_strdup_printf("source.%s.%s", res->plugin_name, res->source_cc_name);
3216 if (!cc_name) {
3217 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3218 goto error;
3219 }
3220
3221 comp = create_implicit_component_args(cc_name);
3222 if (!comp) {
3223 goto error;
3224 }
3225
3226 /*
3227 * Append parameters and log levels of all the
3228 * non-option arguments that contributed to this
3229 * component instance coming into existence.
3230 */
3231 orig_indices_count = bt_value_array_get_size(res->original_input_indices);
3232 for (orig_indices_i = 0; orig_indices_i < orig_indices_count; orig_indices_i++) {
3233 const bt_value *orig_idx_value =
3234 bt_value_array_borrow_element_by_index(
3235 res->original_input_indices, orig_indices_i);
3236 uint64_t orig_idx = bt_value_integer_unsigned_get(orig_idx_value);
3237 const bt_value *params_array =
3238 bt_value_array_borrow_element_by_index_const(
3239 non_opt_params, orig_idx);
3240 uint64_t params_i, params_count;
3241 const bt_value *loglevel_value;
3242
3243 params_count = bt_value_array_get_size(params_array);
3244 for (params_i = 0; params_i < params_count; params_i++) {
3245 const bt_value *params_value =
3246 bt_value_array_borrow_element_by_index_const(
3247 params_array, params_i);
3248 const char *params = bt_value_string_get(params_value);
3249 bt_value_array_append_element_status append_status;
3250
3251 append_status = bt_value_array_append_string_element(
3252 comp->extra_params, "--params");
3253 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
3254 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3255 goto error;
3256 }
3257
3258 append_status = bt_value_array_append_string_element(
3259 comp->extra_params, params);
3260 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
3261 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3262 goto error;
3263 }
3264 }
3265
3266 loglevel_value = bt_value_array_borrow_element_by_index_const(
3267 non_opt_loglevels, orig_idx);
3268 if (bt_value_get_type(loglevel_value) == BT_VALUE_TYPE_STRING) {
3269 const char *loglevel = bt_value_string_get(loglevel_value);
3270 bt_value_array_append_element_status append_status;
3271
3272 append_status = bt_value_array_append_string_element(
3273 comp->extra_params, "--log-level");
3274 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
3275 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3276 goto error;
3277 }
3278
3279 append_status = bt_value_array_append_string_element(
3280 comp->extra_params, loglevel);
3281 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
3282 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3283 goto error;
3284 }
3285 }
3286 }
3287
3288 status = append_parameter_to_args(comp->extra_params, "inputs", res->inputs);
3289 if (status != 0) {
3290 goto error;
3291 }
3292
3293 g_ptr_array_add(component_args, comp);
3294 comp = NULL;
3295 }
3296
3297 status = 0;
3298 goto end;
3299
3300 error:
3301 status = -1;
3302
3303 end:
3304 g_free(cc_name);
3305
3306 if (comp) {
3307 destroy_implicit_component_args(comp);
3308 }
3309
3310 return status;
3311 }
3312
3313 /*
3314 * As we iterate the arguments to the convert command, this tracks what is the
3315 * type of the current item, to which some contextual options (e.g. --params)
3316 * apply to.
3317 */
3318 enum convert_current_item_type {
3319 /* There is no current item. */
3320 CONVERT_CURRENT_ITEM_TYPE_NONE,
3321
3322 /* Current item is a component. */
3323 CONVERT_CURRENT_ITEM_TYPE_COMPONENT,
3324
3325 /* Current item is a non-option argument. */
3326 CONVERT_CURRENT_ITEM_TYPE_NON_OPT,
3327 };
3328
3329 /*
3330 * Creates a Babeltrace config object from the arguments of a convert
3331 * command.
3332 *
3333 * *retcode is set to the appropriate exit code to use.
3334 */
3335 static
3336 struct bt_config *bt_config_convert_from_args(int argc, const char *argv[],
3337 int *retcode, bool force_omit_system_plugin_path,
3338 bool force_omit_home_plugin_path,
3339 const bt_value *initial_plugin_paths, int *default_log_level)
3340 {
3341 enum convert_current_item_type current_item_type =
3342 CONVERT_CURRENT_ITEM_TYPE_NONE;
3343 int ret = 0;
3344 struct bt_config *cfg = NULL;
3345 bool got_input_format_opt = false;
3346 bool got_output_format_opt = false;
3347 bool trimmer_has_begin = false;
3348 bool trimmer_has_end = false;
3349 bool stream_intersection_mode = false;
3350 bool print_run_args = false;
3351 bool print_run_args_0 = false;
3352 bool print_ctf_metadata = false;
3353 bt_value *run_args = NULL;
3354 bt_value *all_names = NULL;
3355 GList *source_names = NULL;
3356 GList *filter_names = NULL;
3357 GList *sink_names = NULL;
3358 bt_value *non_opts = NULL;
3359 bt_value *non_opt_params = NULL;
3360 bt_value *non_opt_loglevels = NULL;
3361 struct implicit_component_args implicit_ctf_output_args = { 0 };
3362 struct implicit_component_args implicit_lttng_live_args = { 0 };
3363 struct implicit_component_args implicit_dummy_args = { 0 };
3364 struct implicit_component_args implicit_text_args = { 0 };
3365 struct implicit_component_args implicit_debug_info_args = { 0 };
3366 struct implicit_component_args implicit_muxer_args = { 0 };
3367 struct implicit_component_args implicit_trimmer_args = { 0 };
3368 bt_value *plugin_paths;
3369 char error_buf[256] = { 0 };
3370 size_t i;
3371 struct bt_common_lttng_live_url_parts lttng_live_url_parts = { 0 };
3372 char *output = NULL;
3373 struct auto_source_discovery auto_disc = { NULL };
3374 GString *auto_disc_comp_name = NULL;
3375 struct bt_argpar_parse_ret argpar_parse_ret = { 0 };
3376 GString *name_gstr = NULL;
3377 GString *component_arg_for_run = NULL;
3378
3379 /*
3380 * Array of `struct implicit_component_args *` created for the sources
3381 * we have auto-discovered.
3382 */
3383 GPtrArray *discovered_source_args = NULL;
3384
3385 /*
3386 * If set, restrict automatic source discovery to this component class
3387 * of this plugin.
3388 */
3389 const char *auto_source_discovery_restrict_plugin_name = NULL;
3390 const char *auto_source_discovery_restrict_component_class_name = NULL;
3391
3392 gchar *ctf_fs_source_clock_class_offset_arg = NULL;
3393 gchar *ctf_fs_source_clock_class_offset_ns_arg = NULL;
3394
3395 (void) bt_value_copy(initial_plugin_paths, &plugin_paths);
3396
3397 *retcode = 0;
3398
3399 if (argc < 1) {
3400 print_convert_usage(stdout);
3401 *retcode = -1;
3402 goto end;
3403 }
3404
3405 if (init_implicit_component_args(&implicit_ctf_output_args,
3406 "sink.ctf.fs", false)) {
3407 goto error;
3408 }
3409
3410 if (init_implicit_component_args(&implicit_lttng_live_args,
3411 "source.ctf.lttng-live", false)) {
3412 goto error;
3413 }
3414
3415 if (init_implicit_component_args(&implicit_text_args,
3416 "sink.text.pretty", false)) {
3417 goto error;
3418 }
3419
3420 if (init_implicit_component_args(&implicit_dummy_args,
3421 "sink.utils.dummy", false)) {
3422 goto error;
3423 }
3424
3425 if (init_implicit_component_args(&implicit_debug_info_args,
3426 "filter.lttng-utils.debug-info", false)) {
3427 goto error;
3428 }
3429
3430 if (init_implicit_component_args(&implicit_muxer_args,
3431 "filter.utils.muxer", true)) {
3432 goto error;
3433 }
3434
3435 if (init_implicit_component_args(&implicit_trimmer_args,
3436 "filter.utils.trimmer", false)) {
3437 goto error;
3438 }
3439
3440 all_names = bt_value_map_create();
3441 if (!all_names) {
3442 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3443 goto error;
3444 }
3445
3446 run_args = bt_value_array_create();
3447 if (!run_args) {
3448 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3449 goto error;
3450 }
3451
3452 component_arg_for_run = g_string_new(NULL);
3453 if (!component_arg_for_run) {
3454 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3455 goto error;
3456 }
3457
3458 ret = append_env_var_plugin_paths(plugin_paths);
3459 if (ret) {
3460 goto error;
3461 }
3462
3463 non_opts = bt_value_array_create();
3464 if (!non_opts) {
3465 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3466 goto error;
3467 }
3468
3469 non_opt_params = bt_value_array_create();
3470 if (!non_opt_params) {
3471 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3472 goto error;
3473 }
3474
3475 non_opt_loglevels = bt_value_array_create();
3476 if (!non_opt_loglevels) {
3477 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3478 goto error;
3479 }
3480
3481 if (auto_source_discovery_init(&auto_disc) != 0) {
3482 goto error;
3483 }
3484
3485 discovered_source_args =
3486 g_ptr_array_new_with_free_func((GDestroyNotify) destroy_implicit_component_args);
3487 if (!discovered_source_args) {
3488 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3489 goto error;
3490 }
3491
3492 auto_disc_comp_name = g_string_new(NULL);
3493 if (!auto_disc_comp_name) {
3494 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3495 goto error;
3496 }
3497
3498 /*
3499 * First pass: collect all arguments which need to be passed
3500 * as is to the run command. This pass can also add --name
3501 * arguments if needed to automatically name unnamed component
3502 * instances.
3503 *
3504 * Also it appends the plugin paths of --plugin-path to
3505 * `plugin_paths`.
3506 */
3507 argpar_parse_ret = bt_argpar_parse(argc, argv, convert_options, true);
3508 if (argpar_parse_ret.error) {
3509 BT_CLI_LOGE_APPEND_CAUSE(
3510 "While parsing `convert` command's command-line arguments: %s",
3511 argpar_parse_ret.error->str);
3512 goto error;
3513 }
3514
3515 if (help_option_is_specified(&argpar_parse_ret)) {
3516 print_convert_usage(stdout);
3517 *retcode = -1;
3518 BT_OBJECT_PUT_REF_AND_RESET(cfg);
3519 goto end;
3520 }
3521
3522 for (i = 0; i < argpar_parse_ret.items->len; i++) {
3523 struct bt_argpar_item *argpar_item =
3524 g_ptr_array_index(argpar_parse_ret.items, i);
3525 struct bt_argpar_item_opt *argpar_item_opt;
3526 char *name = NULL;
3527 char *plugin_name = NULL;
3528 char *comp_cls_name = NULL;
3529 const char *arg;
3530
3531 if (argpar_item->type == BT_ARGPAR_ITEM_TYPE_OPT) {
3532 argpar_item_opt = (struct bt_argpar_item_opt *) argpar_item;
3533 arg = argpar_item_opt->arg;
3534
3535 switch (argpar_item_opt->descr->id) {
3536 case OPT_COMPONENT:
3537 {
3538 bt_component_class_type type;
3539
3540 current_item_type = CONVERT_CURRENT_ITEM_TYPE_COMPONENT;
3541
3542 /* Parse the argument */
3543 plugin_comp_cls_names(arg, &name, &plugin_name,
3544 &comp_cls_name, &type);
3545 if (!plugin_name || !comp_cls_name) {
3546 BT_CLI_LOGE_APPEND_CAUSE(
3547 "Invalid format for --component option's argument:\n %s",
3548 arg);
3549 goto error;
3550 }
3551
3552 if (name) {
3553 /*
3554 * Name was given by the user, verify it isn't
3555 * taken.
3556 */
3557 if (bt_value_map_has_entry(all_names, name)) {
3558 BT_CLI_LOGE_APPEND_CAUSE(
3559 "Duplicate component instance name:\n %s",
3560 name);
3561 goto error;
3562 }
3563
3564 name_gstr = g_string_new(name);
3565 if (!name_gstr) {
3566 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3567 goto error;
3568 }
3569
3570 g_string_assign(component_arg_for_run, arg);
3571 } else {
3572 /* Name not given by user, generate one. */
3573 name_gstr = get_component_auto_name(arg, all_names);
3574 if (!name_gstr) {
3575 goto error;
3576 }
3577
3578 g_string_printf(component_arg_for_run, "%s:%s",
3579 name_gstr->str, arg);
3580 }
3581
3582 if (bt_value_array_append_string_element(run_args,
3583 "--component")) {
3584 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3585 goto error;
3586 }
3587
3588 if (bt_value_array_append_string_element(run_args,
3589 component_arg_for_run->str)) {
3590 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3591 goto error;
3592 }
3593
3594 /*
3595 * Remember this name globally, for the uniqueness of
3596 * all component names.
3597 */
3598 if (bt_value_map_insert_entry(all_names,
3599 name_gstr->str, bt_value_null)) {
3600 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3601 goto error;
3602 }
3603
3604 /*
3605 * Remember this name specifically for the type of the
3606 * component. This is to create connection arguments.
3607 *
3608 * The list takes ownership of `name_gstr`.
3609 */
3610 switch (type) {
3611 case BT_COMPONENT_CLASS_TYPE_SOURCE:
3612 source_names = g_list_append(source_names, name_gstr);
3613 break;
3614 case BT_COMPONENT_CLASS_TYPE_FILTER:
3615 filter_names = g_list_append(filter_names, name_gstr);
3616 break;
3617 case BT_COMPONENT_CLASS_TYPE_SINK:
3618 sink_names = g_list_append(sink_names, name_gstr);
3619 break;
3620 default:
3621 abort();
3622 }
3623 name_gstr = NULL;
3624
3625 free(name);
3626 free(plugin_name);
3627 free(comp_cls_name);
3628 name = NULL;
3629 plugin_name = NULL;
3630 comp_cls_name = NULL;
3631 break;
3632 }
3633 case OPT_PARAMS:
3634 if (current_item_type == CONVERT_CURRENT_ITEM_TYPE_COMPONENT) {
3635 /*
3636 * The current item is a component (--component option),
3637 * pass it directly to the run args.
3638 */
3639 if (bt_value_array_append_string_element(run_args,
3640 "--params")) {
3641 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3642 goto error;
3643 }
3644
3645 if (bt_value_array_append_string_element(run_args, arg)) {
3646 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3647 goto error;
3648 }
3649 } else if (current_item_type == CONVERT_CURRENT_ITEM_TYPE_NON_OPT) {
3650 /*
3651 * The current item is a
3652 * non-option argument, record
3653 * it in `non_opt_params`.
3654 */
3655 bt_value *array;
3656 uint64_t idx = bt_value_array_get_size(non_opt_params) - 1;
3657
3658 array = bt_value_array_borrow_element_by_index(non_opt_params, idx);
3659 bt_value_array_append_string_element(array, arg);
3660 } else {
3661 BT_CLI_LOGE_APPEND_CAUSE(
3662 "No current component (--component option) or non-option argument of which to set parameters:\n %s",
3663 arg);
3664 goto error;
3665 }
3666 break;
3667 case OPT_LOG_LEVEL:
3668 if (current_item_type == CONVERT_CURRENT_ITEM_TYPE_COMPONENT) {
3669 if (bt_value_array_append_string_element(run_args, "--log-level")) {
3670 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3671 goto error;
3672 }
3673
3674 if (bt_value_array_append_string_element(run_args, arg)) {
3675 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3676 goto error;
3677 }
3678 } else if (current_item_type == CONVERT_CURRENT_ITEM_TYPE_NON_OPT) {
3679 uint64_t idx = bt_value_array_get_size(non_opt_loglevels) - 1;
3680 bt_value *log_level_str_value;
3681
3682 log_level_str_value = bt_value_string_create_init(arg);
3683 if (!log_level_str_value) {
3684 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3685 goto error;
3686 }
3687
3688 if (bt_value_array_set_element_by_index(non_opt_loglevels, idx,
3689 log_level_str_value)) {
3690 bt_value_put_ref(log_level_str_value);
3691 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3692 goto error;
3693 }
3694 } else {
3695 BT_CLI_LOGE_APPEND_CAUSE(
3696 "No current component (--component option) or non-option argument to assign a log level to:\n %s",
3697 arg);
3698 goto error;
3699 }
3700
3701 break;
3702 case OPT_OMIT_HOME_PLUGIN_PATH:
3703 force_omit_home_plugin_path = true;
3704
3705 if (bt_value_array_append_string_element(run_args,
3706 "--omit-home-plugin-path")) {
3707 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3708 goto error;
3709 }
3710 break;
3711 case OPT_RETRY_DURATION:
3712 if (bt_value_array_append_string_element(run_args,
3713 "--retry-duration")) {
3714 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3715 goto error;
3716 }
3717
3718 if (bt_value_array_append_string_element(run_args, arg)) {
3719 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3720 goto error;
3721 }
3722 break;
3723 case OPT_OMIT_SYSTEM_PLUGIN_PATH:
3724 force_omit_system_plugin_path = true;
3725
3726 if (bt_value_array_append_string_element(run_args,
3727 "--omit-system-plugin-path")) {
3728 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3729 goto error;
3730 }
3731 break;
3732 case OPT_PLUGIN_PATH:
3733 if (bt_config_append_plugin_paths_check_setuid_setgid(
3734 plugin_paths, arg)) {
3735 goto error;
3736 }
3737
3738 if (bt_value_array_append_string_element(run_args,
3739 "--plugin-path")) {
3740 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3741 goto error;
3742 }
3743
3744 if (bt_value_array_append_string_element(run_args, arg)) {
3745 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3746 goto error;
3747 }
3748 break;
3749 case OPT_BEGIN:
3750 case OPT_CLOCK_CYCLES:
3751 case OPT_CLOCK_DATE:
3752 case OPT_CLOCK_FORCE_CORRELATE:
3753 case OPT_CLOCK_GMT:
3754 case OPT_CLOCK_OFFSET:
3755 case OPT_CLOCK_OFFSET_NS:
3756 case OPT_CLOCK_SECONDS:
3757 case OPT_COLOR:
3758 case OPT_DEBUG:
3759 case OPT_DEBUG_INFO:
3760 case OPT_DEBUG_INFO_DIR:
3761 case OPT_DEBUG_INFO_FULL_PATH:
3762 case OPT_DEBUG_INFO_TARGET_PREFIX:
3763 case OPT_END:
3764 case OPT_FIELDS:
3765 case OPT_INPUT_FORMAT:
3766 case OPT_NAMES:
3767 case OPT_NO_DELTA:
3768 case OPT_OUTPUT_FORMAT:
3769 case OPT_OUTPUT:
3770 case OPT_RUN_ARGS:
3771 case OPT_RUN_ARGS_0:
3772 case OPT_STREAM_INTERSECTION:
3773 case OPT_TIMERANGE:
3774 case OPT_VERBOSE:
3775 /* Ignore in this pass */
3776 break;
3777 default:
3778 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
3779 argpar_item_opt->descr->id);
3780 goto error;
3781 }
3782 } else if (argpar_item->type == BT_ARGPAR_ITEM_TYPE_NON_OPT) {
3783 struct bt_argpar_item_non_opt *argpar_item_non_opt;
3784 bt_value_array_append_element_status append_status;
3785
3786 current_item_type = CONVERT_CURRENT_ITEM_TYPE_NON_OPT;
3787
3788 argpar_item_non_opt = (struct bt_argpar_item_non_opt *) argpar_item;
3789
3790 append_status = bt_value_array_append_string_element(non_opts,
3791 argpar_item_non_opt->arg);
3792 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
3793 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3794 goto error;
3795 }
3796
3797 append_status = bt_value_array_append_empty_array_element(non_opt_params);
3798 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
3799 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3800 goto error;
3801 }
3802
3803 append_status = bt_value_array_append_element(non_opt_loglevels, bt_value_null);
3804 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
3805 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3806 goto error;
3807 }
3808 } else {
3809 abort();
3810 }
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 /* Print CTF metadata or print LTTng live sessions */
4131 if (print_ctf_metadata) {
4132 const bt_value *bt_val_non_opt;
4133
4134 if (bt_value_array_is_empty(non_opts)) {
4135 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf-metadata specified without a path.");
4136 goto error;
4137 }
4138
4139 if (bt_value_array_get_size(non_opts) > 1) {
4140 BT_CLI_LOGE_APPEND_CAUSE("Too many paths specified for --output-format=ctf-metadata.");
4141 goto error;
4142 }
4143
4144 cfg = bt_config_print_ctf_metadata_create(plugin_paths);
4145 if (!cfg) {
4146 goto error;
4147 }
4148
4149 bt_val_non_opt = bt_value_array_borrow_element_by_index_const(non_opts, 0);
4150 g_string_assign(cfg->cmd_data.print_ctf_metadata.path,
4151 bt_value_string_get(bt_val_non_opt));
4152
4153 if (output) {
4154 g_string_assign(
4155 cfg->cmd_data.print_ctf_metadata.output_path,
4156 output);
4157 }
4158
4159 goto end;
4160 }
4161
4162 /*
4163 * If -o ctf was specified, make sure an output path (--output)
4164 * was also specified. --output does not imply -o ctf because
4165 * it's also used for the default, implicit -o text if -o ctf
4166 * is not specified.
4167 */
4168 if (implicit_ctf_output_args.exists) {
4169 if (!output) {
4170 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf specified without --output (trace output path).");
4171 goto error;
4172 }
4173
4174 /*
4175 * At this point we know that -o ctf AND --output were
4176 * specified. Make sure that no options were specified
4177 * which would imply -o text because --output would be
4178 * ambiguous in this case. For example, this is wrong:
4179 *
4180 * babeltrace2 --names=all -o ctf --output=/tmp/path my-trace
4181 *
4182 * because --names=all implies -o text, and --output
4183 * could apply to both the sink.text.pretty and
4184 * sink.ctf.fs implicit components.
4185 */
4186 if (implicit_text_args.exists) {
4187 BT_CLI_LOGE_APPEND_CAUSE("Ambiguous --output option: --output-format=ctf specified but another option implies --output-format=text.");
4188 goto error;
4189 }
4190 }
4191
4192 /*
4193 * If -o dummy and -o ctf were not specified, and if there are
4194 * no explicit sink components, then use an implicit
4195 * `sink.text.pretty` component.
4196 */
4197 if (!implicit_dummy_args.exists && !implicit_ctf_output_args.exists &&
4198 !sink_names) {
4199 implicit_text_args.exists = true;
4200 }
4201
4202 /*
4203 * Set implicit `sink.text.pretty` or `sink.ctf.fs` component's
4204 * `path` parameter if --output was specified.
4205 */
4206 if (output) {
4207 if (implicit_text_args.exists) {
4208 append_implicit_component_extra_param(&implicit_text_args,
4209 "path", output);
4210 } else if (implicit_ctf_output_args.exists) {
4211 append_implicit_component_extra_param(&implicit_ctf_output_args,
4212 "path", output);
4213 }
4214 }
4215
4216 /* Decide where the non-option argument(s) go */
4217 if (bt_value_array_get_size(non_opts) > 0) {
4218 if (implicit_lttng_live_args.exists) {
4219 const bt_value *bt_val_non_opt;
4220
4221 if (bt_value_array_get_size(non_opts) > 1) {
4222 BT_CLI_LOGE_APPEND_CAUSE("Too many URLs specified for --input-format=lttng-live.");
4223 goto error;
4224 }
4225
4226 bt_val_non_opt = bt_value_array_borrow_element_by_index_const(non_opts, 0);
4227 lttng_live_url_parts =
4228 bt_common_parse_lttng_live_url(bt_value_string_get(bt_val_non_opt),
4229 error_buf, sizeof(error_buf));
4230 if (!lttng_live_url_parts.proto) {
4231 BT_CLI_LOGE_APPEND_CAUSE("Invalid LTTng live URL format: %s.",
4232 error_buf);
4233 goto error;
4234 }
4235
4236 if (!lttng_live_url_parts.session_name) {
4237 /* Print LTTng live sessions */
4238 cfg = bt_config_print_lttng_live_sessions_create(
4239 plugin_paths);
4240 if (!cfg) {
4241 goto error;
4242 }
4243
4244 g_string_assign(cfg->cmd_data.print_lttng_live_sessions.url,
4245 bt_value_string_get(bt_val_non_opt));
4246
4247 if (output) {
4248 g_string_assign(
4249 cfg->cmd_data.print_lttng_live_sessions.output_path,
4250 output);
4251 }
4252
4253 goto end;
4254 }
4255
4256 ret = append_implicit_component_extra_param(
4257 &implicit_lttng_live_args, "url",
4258 bt_value_string_get(bt_val_non_opt));
4259 if (ret) {
4260 goto error;
4261 }
4262
4263 ret = append_implicit_component_extra_param(
4264 &implicit_lttng_live_args,
4265 "session-not-found-action", "end");
4266 if (ret) {
4267 goto error;
4268 }
4269 } else {
4270 int status;
4271 size_t plugin_count;
4272 const bt_plugin **plugins;
4273 const bt_plugin *plugin;
4274
4275 status = require_loaded_plugins(plugin_paths);
4276 if (status != 0) {
4277 goto error;
4278 }
4279
4280 if (auto_source_discovery_restrict_plugin_name) {
4281 plugin_count = 1;
4282 plugin = find_loaded_plugin(auto_source_discovery_restrict_plugin_name);
4283 plugins = &plugin;
4284 } else {
4285 plugin_count = get_loaded_plugins_count();
4286 plugins = borrow_loaded_plugins();
4287 }
4288
4289 status = auto_discover_source_components(non_opts, plugins, plugin_count,
4290 auto_source_discovery_restrict_component_class_name,
4291 *default_log_level >= 0 ? *default_log_level : cli_default_log_level,
4292 &auto_disc);
4293
4294 if (status != 0) {
4295 goto error;
4296 }
4297
4298 status = create_implicit_component_args_from_auto_discovered_sources(
4299 &auto_disc, non_opt_params, non_opt_loglevels,
4300 discovered_source_args);
4301 if (status != 0) {
4302 goto error;
4303 }
4304 }
4305 }
4306
4307 /* If --clock-offset was given, apply it to any src.ctf.fs component. */
4308 if (ctf_fs_source_clock_class_offset_arg) {
4309 int n;
4310
4311 n = append_multiple_implicit_components_param(
4312 discovered_source_args, "source.ctf.fs", "clock-class-offset-s",
4313 ctf_fs_source_clock_class_offset_arg);
4314
4315 if (n == 0) {
4316 BT_CLI_LOGE_APPEND_CAUSE("--clock-offset specified, but no source.ctf.fs component instantiated.");
4317 goto error;
4318 }
4319 }
4320
4321 /* If --clock-offset-ns was given, apply it to any src.ctf.fs component. */
4322 if (ctf_fs_source_clock_class_offset_ns_arg) {
4323 int n;
4324
4325 n = append_multiple_implicit_components_param(
4326 discovered_source_args, "source.ctf.fs", "clock-class-offset-ns",
4327 ctf_fs_source_clock_class_offset_ns_arg);
4328
4329 if (n == 0) {
4330 BT_CLI_LOGE_APPEND_CAUSE("--clock-offset-ns specified, but no source.ctf.fs component instantiated.");
4331 goto error;
4332 }
4333 }
4334
4335 /*
4336 * If the implicit `source.ctf.lttng-live` component exists,
4337 * make sure there's at least one non-option argument (which is
4338 * the URL).
4339 */
4340 if (implicit_lttng_live_args.exists && bt_value_array_is_empty(non_opts)) {
4341 BT_CLI_LOGE_APPEND_CAUSE("Missing URL for implicit `%s` component.",
4342 implicit_lttng_live_args.comp_arg->str);
4343 goto error;
4344 }
4345
4346 /* Assign names to implicit components */
4347 for (i = 0; i < discovered_source_args->len; i++) {
4348 struct implicit_component_args *args;
4349 int j;
4350
4351 args = discovered_source_args->pdata[i];
4352
4353 g_string_printf(auto_disc_comp_name, "auto-disc-%s", args->comp_arg->str);
4354
4355 /* Give it a name like `auto-disc-src-ctf-fs`. */
4356 for (j = 0; j < auto_disc_comp_name->len; j++) {
4357 if (auto_disc_comp_name->str[j] == '.') {
4358 auto_disc_comp_name->str[j] = '-';
4359 }
4360 }
4361
4362 ret = assign_name_to_implicit_component(args,
4363 auto_disc_comp_name->str, all_names, &source_names, true);
4364 if (ret) {
4365 goto error;
4366 }
4367 }
4368
4369 ret = assign_name_to_implicit_component(&implicit_lttng_live_args,
4370 "lttng-live", all_names, &source_names, true);
4371 if (ret) {
4372 goto error;
4373 }
4374
4375 ret = assign_name_to_implicit_component(&implicit_text_args,
4376 "pretty", all_names, &sink_names, true);
4377 if (ret) {
4378 goto error;
4379 }
4380
4381 ret = assign_name_to_implicit_component(&implicit_ctf_output_args,
4382 "sink-ctf-fs", all_names, &sink_names, true);
4383 if (ret) {
4384 goto error;
4385 }
4386
4387 ret = assign_name_to_implicit_component(&implicit_dummy_args,
4388 "dummy", all_names, &sink_names, true);
4389 if (ret) {
4390 goto error;
4391 }
4392
4393 ret = assign_name_to_implicit_component(&implicit_muxer_args,
4394 "muxer", all_names, NULL, false);
4395 if (ret) {
4396 goto error;
4397 }
4398
4399 ret = assign_name_to_implicit_component(&implicit_trimmer_args,
4400 "trimmer", all_names, NULL, false);
4401 if (ret) {
4402 goto error;
4403 }
4404
4405 ret = assign_name_to_implicit_component(&implicit_debug_info_args,
4406 "debug-info", all_names, NULL, false);
4407 if (ret) {
4408 goto error;
4409 }
4410
4411 /* Make sure there's at least one source and one sink */
4412 if (!source_names) {
4413 BT_CLI_LOGE_APPEND_CAUSE("No source component.");
4414 goto error;
4415 }
4416
4417 if (!sink_names) {
4418 BT_CLI_LOGE_APPEND_CAUSE("No sink component.");
4419 goto error;
4420 }
4421
4422 /*
4423 * Prepend the muxer, the trimmer, and the debug info to the
4424 * filter chain so that we have:
4425 *
4426 * sources -> muxer -> [trimmer] -> [debug info] ->
4427 * [user filters] -> sinks
4428 */
4429 if (implicit_debug_info_args.exists) {
4430 if (g_list_prepend_gstring(&filter_names,
4431 implicit_debug_info_args.name_arg->str)) {
4432 goto error;
4433 }
4434 }
4435
4436 if (implicit_trimmer_args.exists) {
4437 if (g_list_prepend_gstring(&filter_names,
4438 implicit_trimmer_args.name_arg->str)) {
4439 goto error;
4440 }
4441 }
4442
4443 if (g_list_prepend_gstring(&filter_names,
4444 implicit_muxer_args.name_arg->str)) {
4445 goto error;
4446 }
4447
4448 /*
4449 * Append the equivalent run arguments for the implicit
4450 * components.
4451 */
4452 for (i = 0; i < discovered_source_args->len; i++) {
4453 struct implicit_component_args *args =
4454 discovered_source_args->pdata[i];
4455
4456 ret = append_run_args_for_implicit_component(args, run_args);
4457 if (ret) {
4458 goto error;
4459 }
4460 }
4461
4462 ret = append_run_args_for_implicit_component(&implicit_lttng_live_args,
4463 run_args);
4464 if (ret) {
4465 goto error;
4466 }
4467
4468 ret = append_run_args_for_implicit_component(&implicit_text_args,
4469 run_args);
4470 if (ret) {
4471 goto error;
4472 }
4473
4474 ret = append_run_args_for_implicit_component(&implicit_ctf_output_args,
4475 run_args);
4476 if (ret) {
4477 goto error;
4478 }
4479
4480 ret = append_run_args_for_implicit_component(&implicit_dummy_args,
4481 run_args);
4482 if (ret) {
4483 goto error;
4484 }
4485
4486 ret = append_run_args_for_implicit_component(&implicit_muxer_args,
4487 run_args);
4488 if (ret) {
4489 goto error;
4490 }
4491
4492 ret = append_run_args_for_implicit_component(&implicit_trimmer_args,
4493 run_args);
4494 if (ret) {
4495 goto error;
4496 }
4497
4498 ret = append_run_args_for_implicit_component(&implicit_debug_info_args,
4499 run_args);
4500 if (ret) {
4501 goto error;
4502 }
4503
4504 /* Auto-connect components */
4505 ret = convert_auto_connect(run_args, source_names, filter_names,
4506 sink_names);
4507 if (ret) {
4508 BT_CLI_LOGE_APPEND_CAUSE("Cannot auto-connect components.");
4509 goto error;
4510 }
4511
4512 /*
4513 * We have all the run command arguments now. Depending on
4514 * --run-args, we pass this to the run command or print them
4515 * here.
4516 */
4517 if (print_run_args || print_run_args_0) {
4518 if (stream_intersection_mode) {
4519 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --stream-intersection with --run-args or --run-args-0.");
4520 goto error;
4521 }
4522
4523 for (i = 0; i < bt_value_array_get_size(run_args); i++) {
4524 const bt_value *arg_value =
4525 bt_value_array_borrow_element_by_index(run_args,
4526 i);
4527 const char *arg;
4528 GString *quoted = NULL;
4529 const char *arg_to_print;
4530
4531 BT_ASSERT(arg_value);
4532 arg = bt_value_string_get(arg_value);
4533
4534 if (print_run_args) {
4535 quoted = bt_common_shell_quote(arg, true);
4536 if (!quoted) {
4537 goto error;
4538 }
4539
4540 arg_to_print = quoted->str;
4541 } else {
4542 arg_to_print = arg;
4543 }
4544
4545 printf("%s", arg_to_print);
4546
4547 if (quoted) {
4548 g_string_free(quoted, TRUE);
4549 }
4550
4551 if (i < bt_value_array_get_size(run_args) - 1) {
4552 if (print_run_args) {
4553 putchar(' ');
4554 } else {
4555 putchar('\0');
4556 }
4557 }
4558 }
4559
4560 *retcode = -1;
4561 BT_OBJECT_PUT_REF_AND_RESET(cfg);
4562 goto end;
4563 }
4564
4565 /*
4566 * If the log level is still unset at this point, set it to
4567 * the program's default.
4568 */
4569 if (*default_log_level < 0) {
4570 *default_log_level = cli_default_log_level;
4571 }
4572
4573 cfg = bt_config_run_from_args_array(run_args, retcode,
4574 force_omit_system_plugin_path,
4575 force_omit_home_plugin_path,
4576 initial_plugin_paths, *default_log_level);
4577 if (!cfg) {
4578 goto error;
4579 }
4580
4581 cfg->cmd_data.run.stream_intersection_mode = stream_intersection_mode;
4582 goto end;
4583
4584 error:
4585 *retcode = 1;
4586 BT_OBJECT_PUT_REF_AND_RESET(cfg);
4587
4588 end:
4589 /*
4590 * If the log level is still unset at this point, set it to
4591 * the program's default.
4592 */
4593 if (*default_log_level < 0) {
4594 *default_log_level = cli_default_log_level;
4595 }
4596
4597 bt_argpar_parse_ret_fini(&argpar_parse_ret);
4598
4599 free(output);
4600
4601 if (component_arg_for_run) {
4602 g_string_free(component_arg_for_run, TRUE);
4603 }
4604
4605 if (name_gstr) {
4606 g_string_free(name_gstr, TRUE);
4607 }
4608
4609 bt_value_put_ref(run_args);
4610 bt_value_put_ref(all_names);
4611 destroy_glist_of_gstring(source_names);
4612 destroy_glist_of_gstring(filter_names);
4613 destroy_glist_of_gstring(sink_names);
4614 bt_value_put_ref(non_opt_params);
4615 bt_value_put_ref(non_opt_loglevels);
4616 bt_value_put_ref(non_opts);
4617 finalize_implicit_component_args(&implicit_ctf_output_args);
4618 finalize_implicit_component_args(&implicit_lttng_live_args);
4619 finalize_implicit_component_args(&implicit_dummy_args);
4620 finalize_implicit_component_args(&implicit_text_args);
4621 finalize_implicit_component_args(&implicit_debug_info_args);
4622 finalize_implicit_component_args(&implicit_muxer_args);
4623 finalize_implicit_component_args(&implicit_trimmer_args);
4624 bt_value_put_ref(plugin_paths);
4625 bt_common_destroy_lttng_live_url_parts(&lttng_live_url_parts);
4626 auto_source_discovery_fini(&auto_disc);
4627
4628 if (discovered_source_args) {
4629 g_ptr_array_free(discovered_source_args, TRUE);
4630 }
4631
4632 g_free(ctf_fs_source_clock_class_offset_arg);
4633 g_free(ctf_fs_source_clock_class_offset_ns_arg);
4634
4635 if (auto_disc_comp_name) {
4636 g_string_free(auto_disc_comp_name, TRUE);
4637 }
4638
4639 return cfg;
4640 }
4641
4642 /*
4643 * Prints the Babeltrace 2.x general usage.
4644 */
4645 static
4646 void print_gen_usage(FILE *fp)
4647 {
4648 fprintf(fp, "Usage: babeltrace2 [GENERAL OPTIONS] [COMMAND] [COMMAND ARGUMENTS]\n");
4649 fprintf(fp, "\n");
4650 fprintf(fp, "General options:\n");
4651 fprintf(fp, "\n");
4652 fprintf(fp, " -d, --debug Enable debug mode (same as --log-level=V)\n");
4653 fprintf(fp, " -h, --help Show this help and quit\n");
4654 fprintf(fp, " -l, --log-level=LVL Set the default log level to LVL (`N`, `V`, `D`,\n");
4655 fprintf(fp, " `I`, `W` (default), `E`, or `F`)\n");
4656 fprintf(fp, " -v, --verbose Enable verbose mode (same as --log-level=I)\n");
4657 fprintf(fp, " -V, --version Show version and quit\n");
4658 fprintf(fp, "\n");
4659 fprintf(fp, "Available commands:\n");
4660 fprintf(fp, "\n");
4661 fprintf(fp, " convert Convert and trim traces (default)\n");
4662 fprintf(fp, " help Get help for a plugin or a component class\n");
4663 fprintf(fp, " list-plugins List available plugins and their content\n");
4664 fprintf(fp, " query Query objects from a component class\n");
4665 fprintf(fp, " run Build a processing graph and run it\n");
4666 fprintf(fp, "\n");
4667 fprintf(fp, "Use `babeltrace2 COMMAND --help` to show the help of COMMAND.\n");
4668 }
4669
4670 struct bt_config *bt_config_cli_args_create(int argc, const char *argv[],
4671 int *retcode, bool force_omit_system_plugin_path,
4672 bool force_omit_home_plugin_path,
4673 const bt_value *initial_plugin_paths)
4674 {
4675 struct bt_config *config = NULL;
4676 int i;
4677 int top_level_argc;
4678 const char **top_level_argv;
4679 int command_argc = -1;
4680 const char **command_argv = NULL;
4681 const char *command_name = NULL;
4682 int default_log_level = -1;
4683 struct bt_argpar_parse_ret argpar_parse_ret = { 0 };
4684
4685 /* Top-level option descriptions. */
4686 static const struct bt_argpar_opt_descr descrs[] = {
4687 { OPT_DEBUG, 'd', "debug", false },
4688 { OPT_HELP, 'h', "help", false },
4689 { OPT_LOG_LEVEL, 'l', "log-level", true },
4690 { OPT_VERBOSE, 'v', "verbose", false },
4691 { OPT_VERSION, 'V', "version", false},
4692 BT_ARGPAR_OPT_DESCR_SENTINEL
4693 };
4694
4695 enum command_type {
4696 COMMAND_TYPE_NONE = -1,
4697 COMMAND_TYPE_RUN = 0,
4698 COMMAND_TYPE_CONVERT,
4699 COMMAND_TYPE_LIST_PLUGINS,
4700 COMMAND_TYPE_HELP,
4701 COMMAND_TYPE_QUERY,
4702 } command_type = COMMAND_TYPE_NONE;
4703
4704 *retcode = -1;
4705
4706 if (!initial_plugin_paths) {
4707 initial_plugin_paths = bt_value_array_create();
4708 if (!initial_plugin_paths) {
4709 *retcode = 1;
4710 goto end;
4711 }
4712 } else {
4713 bt_value_get_ref(initial_plugin_paths);
4714 }
4715
4716 if (argc <= 1) {
4717 print_version();
4718 puts("");
4719 print_gen_usage(stdout);
4720 goto end;
4721 }
4722
4723 /* Skip first argument, the name of the program. */
4724 top_level_argc = argc - 1;
4725 top_level_argv = argv + 1;
4726 argpar_parse_ret = bt_argpar_parse(top_level_argc, top_level_argv,
4727 descrs, false);
4728
4729 if (argpar_parse_ret.error) {
4730 BT_CLI_LOGE_APPEND_CAUSE(
4731 "While parsing command-line arguments: %s",
4732 argpar_parse_ret.error->str);
4733 goto error;
4734 }
4735
4736 for (i = 0; i < argpar_parse_ret.items->len; i++) {
4737 struct bt_argpar_item *item;
4738
4739 item = g_ptr_array_index(argpar_parse_ret.items, i);
4740
4741 if (item->type == BT_ARGPAR_ITEM_TYPE_OPT) {
4742 struct bt_argpar_item_opt *item_opt =
4743 (struct bt_argpar_item_opt *) item;
4744
4745 switch (item_opt->descr->id) {
4746 case OPT_DEBUG:
4747 default_log_level = BT_LOG_TRACE;
4748 break;
4749 case OPT_VERBOSE:
4750 /*
4751 * Legacy: do not override a previous
4752 * --debug because --verbose and --debug
4753 * can be specified together (in this
4754 * case we want the lowest log level to
4755 * apply, TRACE).
4756 */
4757 default_log_level = BT_LOG_INFO;
4758 break;
4759 case OPT_LOG_LEVEL:
4760 default_log_level =
4761 bt_log_get_level_from_string(item_opt->arg);
4762 if (default_log_level < 0) {
4763 BT_CLI_LOGE_APPEND_CAUSE(
4764 "Invalid argument for --log-level option:\n %s",
4765 item_opt->arg);
4766 goto error;
4767 }
4768 break;
4769 case OPT_VERSION:
4770 print_version();
4771 goto end;
4772 case OPT_HELP:
4773 print_gen_usage(stdout);
4774 goto end;
4775 }
4776 } else if (item->type == BT_ARGPAR_ITEM_TYPE_NON_OPT) {
4777 struct bt_argpar_item_non_opt *item_non_opt =
4778 (struct bt_argpar_item_non_opt *) item;
4779 /*
4780 * First unknown argument: is it a known command
4781 * name?
4782 */
4783 command_argc =
4784 top_level_argc - item_non_opt->orig_index - 1;
4785 command_argv =
4786 &top_level_argv[item_non_opt->orig_index + 1];
4787
4788 if (strcmp(item_non_opt->arg, "convert") == 0) {
4789 command_type = COMMAND_TYPE_CONVERT;
4790 } else if (strcmp(item_non_opt->arg, "list-plugins") == 0) {
4791 command_type = COMMAND_TYPE_LIST_PLUGINS;
4792 } else if (strcmp(item_non_opt->arg, "help") == 0) {
4793 command_type = COMMAND_TYPE_HELP;
4794 } else if (strcmp(item_non_opt->arg, "query") == 0) {
4795 command_type = COMMAND_TYPE_QUERY;
4796 } else if (strcmp(item_non_opt->arg, "run") == 0) {
4797 command_type = COMMAND_TYPE_RUN;
4798 } else {
4799 /*
4800 * Non-option argument, but not a known
4801 * command name: assume the default
4802 * `convert` command.
4803 */
4804 command_type = COMMAND_TYPE_CONVERT;
4805 command_name = "convert";
4806 command_argc++;
4807 command_argv--;
4808 }
4809 break;
4810 }
4811 }
4812
4813 if (command_type == COMMAND_TYPE_NONE) {
4814 if (argpar_parse_ret.ingested_orig_args == top_level_argc) {
4815 /*
4816 * We only got non-help, non-version general options
4817 * like --verbose and --debug, without any other
4818 * arguments, so we can't do anything useful: print the
4819 * usage and quit.
4820 */
4821 print_gen_usage(stdout);
4822 goto end;
4823 }
4824
4825 /*
4826 * We stopped on an unknown option argument (and therefore
4827 * didn't see a command name). Assume `convert` command.
4828 */
4829 command_type = COMMAND_TYPE_CONVERT;
4830 command_name = "convert";
4831 command_argc =
4832 top_level_argc - argpar_parse_ret.ingested_orig_args;
4833 command_argv =
4834 &top_level_argv[argpar_parse_ret.ingested_orig_args];
4835 }
4836
4837 BT_ASSERT(command_argv);
4838 BT_ASSERT(command_argc >= 0);
4839
4840 /*
4841 * The convert command can set its own default log level for
4842 * backward compatibility reasons. It only does so if there's no
4843 * log level yet, so do not force one for this command.
4844 */
4845 if (command_type != COMMAND_TYPE_CONVERT && default_log_level < 0) {
4846 /* Default log level */
4847 default_log_level = cli_default_log_level;
4848 }
4849
4850 switch (command_type) {
4851 case COMMAND_TYPE_RUN:
4852 config = bt_config_run_from_args(command_argc, command_argv,
4853 retcode, force_omit_system_plugin_path,
4854 force_omit_home_plugin_path, initial_plugin_paths,
4855 default_log_level);
4856 break;
4857 case COMMAND_TYPE_CONVERT:
4858 config = bt_config_convert_from_args(command_argc, command_argv,
4859 retcode, force_omit_system_plugin_path,
4860 force_omit_home_plugin_path,
4861 initial_plugin_paths, &default_log_level);
4862 break;
4863 case COMMAND_TYPE_LIST_PLUGINS:
4864 config = bt_config_list_plugins_from_args(command_argc,
4865 command_argv, retcode, force_omit_system_plugin_path,
4866 force_omit_home_plugin_path, initial_plugin_paths);
4867 break;
4868 case COMMAND_TYPE_HELP:
4869 config = bt_config_help_from_args(command_argc,
4870 command_argv, retcode, force_omit_system_plugin_path,
4871 force_omit_home_plugin_path, initial_plugin_paths,
4872 default_log_level);
4873 break;
4874 case COMMAND_TYPE_QUERY:
4875 config = bt_config_query_from_args(command_argc,
4876 command_argv, retcode, force_omit_system_plugin_path,
4877 force_omit_home_plugin_path, initial_plugin_paths,
4878 default_log_level);
4879 break;
4880 default:
4881 abort();
4882 }
4883
4884 if (config) {
4885 BT_ASSERT(default_log_level >= BT_LOG_TRACE);
4886 config->log_level = default_log_level;
4887 config->command_name = command_name;
4888 }
4889
4890 goto end;
4891
4892 error:
4893 *retcode = 1;
4894
4895 end:
4896 bt_argpar_parse_ret_fini(&argpar_parse_ret);
4897 bt_value_put_ref(initial_plugin_paths);
4898 return config;
4899 }
This page took 0.208452 seconds and 4 git commands to generate.