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