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