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