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