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