5df6ce3a4a3ce813d30ca6fe0f1f682bc9750357
[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 bt_value *params;
1484 GString *error_str = NULL;
1485 struct bt_argpar_parse_ret argpar_parse_ret = { 0 };
1486
1487 params = bt_value_null;
1488 bt_value_get_ref(bt_value_null);
1489
1490 *retcode = 0;
1491 cfg = bt_config_query_create(plugin_paths);
1492 if (!cfg) {
1493 goto error;
1494 }
1495
1496 error_str = g_string_new(NULL);
1497 if (!error_str) {
1498 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1499 goto error;
1500 }
1501
1502 /* Parse options */
1503 argpar_parse_ret = bt_argpar_parse(argc, argv, query_options, true);
1504 if (argpar_parse_ret.error) {
1505 BT_CLI_LOGE_APPEND_CAUSE(
1506 "While parsing `query` command's command-line arguments: %s",
1507 argpar_parse_ret.error->str);
1508 goto error;
1509 }
1510
1511 if (help_option_is_specified(&argpar_parse_ret)) {
1512 print_query_usage(stdout);
1513 *retcode = -1;
1514 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1515 goto end;
1516 }
1517
1518 for (i = 0; i < argpar_parse_ret.items->len; i++) {
1519 struct bt_argpar_item *argpar_item =
1520 g_ptr_array_index(argpar_parse_ret.items, i);
1521
1522 if (argpar_item->type == BT_ARGPAR_ITEM_TYPE_OPT) {
1523 struct bt_argpar_item_opt *argpar_item_opt =
1524 (struct bt_argpar_item_opt *) argpar_item;
1525 const char *arg = argpar_item_opt->arg;
1526
1527 switch (argpar_item_opt->descr->id) {
1528 case OPT_PARAMS:
1529 {
1530 bt_value_put_ref(params);
1531 params = bt_param_parse(arg, error_str);
1532 if (!params) {
1533 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
1534 error_str->str);
1535 goto error;
1536 }
1537 break;
1538 }
1539 default:
1540 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1541 argpar_item_opt->descr->id);
1542 goto error;
1543 }
1544 } else {
1545 struct bt_argpar_item_non_opt *argpar_item_non_opt
1546 = (struct bt_argpar_item_non_opt *) argpar_item;
1547
1548 /*
1549 * We need exactly two non-option arguments
1550 * which are the mandatory component class
1551 * specification and query object.
1552 */
1553 if (!component_class_spec) {
1554 component_class_spec = argpar_item_non_opt->arg;
1555 } else if (!query_object) {
1556 query_object = argpar_item_non_opt->arg;
1557 } else {
1558 BT_CLI_LOGE_APPEND_CAUSE("Extraneous command-line argument specified to `query` command: `%s`.",
1559 argpar_item_non_opt->arg);
1560 goto error;
1561 }
1562 }
1563 }
1564
1565 if (!component_class_spec || !query_object) {
1566 print_query_usage(stdout);
1567 *retcode = -1;
1568 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1569 goto end;
1570 }
1571
1572 cfg->cmd_data.query.cfg_component =
1573 bt_config_component_from_arg(component_class_spec,
1574 default_log_level);
1575 if (!cfg->cmd_data.query.cfg_component) {
1576 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for component class specification:\n %s",
1577 component_class_spec);
1578 goto error;
1579 }
1580
1581 BT_ASSERT(params);
1582 BT_OBJECT_MOVE_REF(cfg->cmd_data.query.cfg_component->params, params);
1583
1584 if (strlen(query_object) == 0) {
1585 BT_CLI_LOGE_APPEND_CAUSE("Invalid empty object.");
1586 goto error;
1587 }
1588
1589 g_string_assign(cfg->cmd_data.query.object, query_object);
1590 goto end;
1591
1592 error:
1593 *retcode = 1;
1594 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1595
1596 end:
1597 bt_argpar_parse_ret_fini(&argpar_parse_ret);
1598
1599 if (error_str) {
1600 g_string_free(error_str, TRUE);
1601 }
1602
1603 bt_value_put_ref(params);
1604 return cfg;
1605 }
1606
1607 /*
1608 * Prints the list-plugins command usage.
1609 */
1610 static
1611 void print_list_plugins_usage(FILE *fp)
1612 {
1613 fprintf(fp, "Usage: babeltrace2 [GENERAL OPTIONS] list-plugins [OPTIONS]\n");
1614 fprintf(fp, "\n");
1615 fprintf(fp, "Options:\n");
1616 fprintf(fp, "\n");
1617 fprintf(fp, " -h, --help Show this help and quit\n");
1618 fprintf(fp, "\n");
1619 fprintf(fp, "See `babeltrace2 --help` for the list of general options.\n");
1620 fprintf(fp, "\n");
1621 fprintf(fp, "Use `babeltrace2 help` to get help for a specific plugin or component class.\n");
1622 }
1623
1624 static
1625 const struct bt_argpar_opt_descr list_plugins_options[] = {
1626 /* id, short_name, long_name, with_arg */
1627 { OPT_HELP, 'h', "help", false },
1628 BT_ARGPAR_OPT_DESCR_SENTINEL
1629 };
1630
1631 /*
1632 * Creates a Babeltrace config object from the arguments of a
1633 * list-plugins command.
1634 *
1635 * *retcode is set to the appropriate exit code to use.
1636 */
1637 static
1638 struct bt_config *bt_config_list_plugins_from_args(int argc, const char *argv[],
1639 int *retcode, const bt_value *plugin_paths)
1640 {
1641 struct bt_config *cfg = NULL;
1642 struct bt_argpar_parse_ret argpar_parse_ret = { 0 };
1643
1644 *retcode = 0;
1645 cfg = bt_config_list_plugins_create(plugin_paths);
1646 if (!cfg) {
1647 goto error;
1648 }
1649
1650 /* Parse options */
1651 argpar_parse_ret = bt_argpar_parse(argc, argv, list_plugins_options, true);
1652 if (argpar_parse_ret.error) {
1653 BT_CLI_LOGE_APPEND_CAUSE(
1654 "While parsing `list-plugins` command's command-line arguments: %s",
1655 argpar_parse_ret.error->str);
1656 goto error;
1657 }
1658
1659 if (help_option_is_specified(&argpar_parse_ret)) {
1660 print_list_plugins_usage(stdout);
1661 *retcode = -1;
1662 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1663 goto end;
1664 }
1665
1666 if (argpar_parse_ret.items->len > 0) {
1667 /*
1668 * At this point we know there's at least one non-option
1669 * argument because we don't reach here with `--help`,
1670 * the only option.
1671 */
1672 struct bt_argpar_item_non_opt *non_opt =
1673 argpar_parse_ret.items->pdata[0];
1674
1675 BT_CLI_LOGE_APPEND_CAUSE(
1676 "Extraneous command-line argument specified to `list-plugins` command: `%s`.",
1677 non_opt->arg);
1678 goto error;
1679 }
1680
1681 goto end;
1682
1683 error:
1684 *retcode = 1;
1685 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1686
1687 end:
1688 bt_argpar_parse_ret_fini(&argpar_parse_ret);
1689
1690 return cfg;
1691 }
1692
1693 /*
1694 * Prints the run command usage.
1695 */
1696 static
1697 void print_run_usage(FILE *fp)
1698 {
1699 fprintf(fp, "Usage: babeltrace2 [GENERAL OPTIONS] run [OPTIONS]\n");
1700 fprintf(fp, "\n");
1701 fprintf(fp, "Options:\n");
1702 fprintf(fp, "\n");
1703 fprintf(fp, " -b, --base-params=PARAMS Set PARAMS as the current base parameters\n");
1704 fprintf(fp, " for all the following components until\n");
1705 fprintf(fp, " --reset-base-params is encountered\n");
1706 fprintf(fp, " (see the expected format of PARAMS below)\n");
1707 fprintf(fp, " -c, --component=NAME:TYPE.PLUGIN.CLS\n");
1708 fprintf(fp, " Instantiate the component class CLS of type\n");
1709 fprintf(fp, " TYPE (`source`, `filter`, or `sink`) found\n");
1710 fprintf(fp, " in the plugin PLUGIN, add it to the graph,\n");
1711 fprintf(fp, " and name it NAME");
1712 fprintf(fp, " -x, --connect=CONNECTION Connect two created components (see the\n");
1713 fprintf(fp, " expected format of CONNECTION below)\n");
1714 fprintf(fp, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
1715 fprintf(fp, " (`N`, `T`, `D`, `I`, `W`, `E`, or `F`)\n");
1716 fprintf(fp, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
1717 fprintf(fp, " current component (see the expected format\n");
1718 fprintf(fp, " of PARAMS below)\n");
1719 fprintf(fp, " -r, --reset-base-params Reset the current base parameters to an\n");
1720 fprintf(fp, " empty map\n");
1721 fprintf(fp, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
1722 fprintf(fp, " the graph later, retry in DUR µs\n");
1723 fprintf(fp, " (default: 100000)\n");
1724 fprintf(fp, " -h, --help Show this help and quit\n");
1725 fprintf(fp, "\n");
1726 fprintf(fp, "See `babeltrace2 --help` for the list of general options.\n");
1727 fprintf(fp, "\n\n");
1728 fprintf(fp, "Expected format of CONNECTION\n");
1729 fprintf(fp, "-----------------------------\n");
1730 fprintf(fp, "\n");
1731 fprintf(fp, " UPSTREAM[.UPSTREAM-PORT]:DOWNSTREAM[.DOWNSTREAM-PORT]\n");
1732 fprintf(fp, "\n");
1733 fprintf(fp, "UPSTREAM and DOWNSTREAM are names of the upstream and downstream\n");
1734 fprintf(fp, "components to connect together. You must escape the following characters\n\n");
1735 fprintf(fp, "with `\\`: `\\`, `.`, and `:`. You must set the name of the current\n");
1736 fprintf(fp, "component using the NAME prefix of the --component option.\n");
1737 fprintf(fp, "\n");
1738 fprintf(fp, "UPSTREAM-PORT and DOWNSTREAM-PORT are optional globbing patterns to\n");
1739 fprintf(fp, "identify the upstream and downstream ports to use for the connection.\n");
1740 fprintf(fp, "When the port is not specified, `*` is used.\n");
1741 fprintf(fp, "\n");
1742 fprintf(fp, "When a component named UPSTREAM has an available port which matches the\n");
1743 fprintf(fp, "UPSTREAM-PORT globbing pattern, it is connected to the first port which\n");
1744 fprintf(fp, "matches the DOWNSTREAM-PORT globbing pattern of the component named\n");
1745 fprintf(fp, "DOWNSTREAM.\n");
1746 fprintf(fp, "\n");
1747 fprintf(fp, "The only special character in UPSTREAM-PORT and DOWNSTREAM-PORT is `*`\n");
1748 fprintf(fp, "which matches anything. You must escape the following characters\n");
1749 fprintf(fp, "with `\\`: `\\`, `*`, `?`, `[`, `.`, and `:`.\n");
1750 fprintf(fp, "\n");
1751 fprintf(fp, "You can connect a source component to a filter or sink component. You\n");
1752 fprintf(fp, "can connect a filter component to a sink component.\n");
1753 fprintf(fp, "\n");
1754 fprintf(fp, "Examples:\n");
1755 fprintf(fp, "\n");
1756 fprintf(fp, " my-src:my-sink\n");
1757 fprintf(fp, " ctf-fs.*stream*:utils-muxer:*\n");
1758 fprintf(fp, "\n");
1759 fprintf(fp, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1760 fprintf(fp, "babeltrace2 from a shell.\n");
1761 fprintf(fp, "\n\n");
1762 print_expected_params_format(fp);
1763 }
1764
1765 /*
1766 * Creates a Babeltrace config object from the arguments of a run
1767 * command.
1768 *
1769 * *retcode is set to the appropriate exit code to use.
1770 */
1771 static
1772 struct bt_config *bt_config_run_from_args(int argc, const char *argv[],
1773 int *retcode, const bt_value *plugin_paths,
1774 int default_log_level)
1775 {
1776 struct bt_config_component *cur_cfg_comp = NULL;
1777 bt_value *cur_base_params = NULL;
1778 int ret = 0;
1779 struct bt_config *cfg = NULL;
1780 bt_value *instance_names = NULL;
1781 bt_value *connection_args = NULL;
1782 char error_buf[256] = { 0 };
1783 long retry_duration = -1;
1784 bt_value_map_extend_status extend_status;
1785 GString *error_str = NULL;
1786 struct bt_argpar_parse_ret argpar_parse_ret = { 0 };
1787 int i;
1788
1789 static const struct bt_argpar_opt_descr run_options[] = {
1790 { OPT_BASE_PARAMS, 'b', "base-params", true },
1791 { OPT_COMPONENT, 'c', "component", true },
1792 { OPT_CONNECT, 'x', "connect", true },
1793 { OPT_HELP, 'h', "help", false },
1794 { OPT_LOG_LEVEL, 'l', "log-level", true },
1795 { OPT_PARAMS, 'p', "params", true },
1796 { OPT_RESET_BASE_PARAMS, 'r', "reset-base-params", false },
1797 { OPT_RETRY_DURATION, '\0', "retry-duration", true },
1798 BT_ARGPAR_OPT_DESCR_SENTINEL
1799 };
1800
1801 *retcode = 0;
1802
1803 error_str = g_string_new(NULL);
1804 if (!error_str) {
1805 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1806 goto error;
1807 }
1808
1809 if (argc < 1) {
1810 print_run_usage(stdout);
1811 *retcode = -1;
1812 goto end;
1813 }
1814
1815 cfg = bt_config_run_create(plugin_paths);
1816 if (!cfg) {
1817 goto error;
1818 }
1819
1820 cfg->cmd_data.run.retry_duration_us = 100000;
1821 cur_base_params = bt_value_map_create();
1822 if (!cur_base_params) {
1823 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1824 goto error;
1825 }
1826
1827 instance_names = bt_value_map_create();
1828 if (!instance_names) {
1829 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1830 goto error;
1831 }
1832
1833 connection_args = bt_value_array_create();
1834 if (!connection_args) {
1835 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1836 goto error;
1837 }
1838
1839 /* Parse options */
1840 argpar_parse_ret = bt_argpar_parse(argc, argv, run_options, true);
1841 if (argpar_parse_ret.error) {
1842 BT_CLI_LOGE_APPEND_CAUSE(
1843 "While parsing `run` command's command-line arguments: %s",
1844 argpar_parse_ret.error->str);
1845 goto error;
1846 }
1847
1848 if (help_option_is_specified(&argpar_parse_ret)) {
1849 print_run_usage(stdout);
1850 *retcode = -1;
1851 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1852 goto end;
1853 }
1854
1855 for (i = 0; i < argpar_parse_ret.items->len; i++) {
1856 struct bt_argpar_item *argpar_item =
1857 g_ptr_array_index(argpar_parse_ret.items, i);
1858 struct bt_argpar_item_opt *argpar_item_opt;
1859 const char *arg;
1860
1861 /* This command does not accept non-option arguments.*/
1862 if (argpar_item->type == BT_ARGPAR_ITEM_TYPE_NON_OPT) {
1863 struct bt_argpar_item_non_opt *argpar_nonopt_item =
1864 (struct bt_argpar_item_non_opt *) argpar_item;
1865
1866 BT_CLI_LOGE_APPEND_CAUSE("Unexpected argument: `%s`",
1867 argpar_nonopt_item->arg);
1868 goto error;
1869 }
1870
1871 argpar_item_opt = (struct bt_argpar_item_opt *) argpar_item;
1872 arg = argpar_item_opt->arg;
1873
1874 switch (argpar_item_opt->descr->id) {
1875 case OPT_COMPONENT:
1876 {
1877 enum bt_config_component_dest dest;
1878
1879 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp);
1880 cur_cfg_comp = bt_config_component_from_arg(arg,
1881 default_log_level);
1882 if (!cur_cfg_comp) {
1883 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --component option's argument:\n %s",
1884 arg);
1885 goto error;
1886 }
1887
1888 switch (cur_cfg_comp->type) {
1889 case BT_COMPONENT_CLASS_TYPE_SOURCE:
1890 dest = BT_CONFIG_COMPONENT_DEST_SOURCE;
1891 break;
1892 case BT_COMPONENT_CLASS_TYPE_FILTER:
1893 dest = BT_CONFIG_COMPONENT_DEST_FILTER;
1894 break;
1895 case BT_COMPONENT_CLASS_TYPE_SINK:
1896 dest = BT_CONFIG_COMPONENT_DEST_SINK;
1897 break;
1898 default:
1899 abort();
1900 }
1901
1902 BT_ASSERT(cur_base_params);
1903 bt_value_put_ref(cur_cfg_comp->params);
1904 if (bt_value_copy(cur_base_params,
1905 &cur_cfg_comp->params) < 0) {
1906 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1907 goto error;
1908 }
1909
1910 ret = add_run_cfg_comp_check_name(cfg,
1911 cur_cfg_comp, dest,
1912 instance_names);
1913 if (ret) {
1914 goto error;
1915 }
1916
1917 break;
1918 }
1919 case OPT_PARAMS:
1920 {
1921 bt_value *params;
1922 bt_value *params_to_set;
1923
1924 if (!cur_cfg_comp) {
1925 BT_CLI_LOGE_APPEND_CAUSE("Cannot add parameters to unavailable component:\n %s",
1926 arg);
1927 goto error;
1928 }
1929
1930 params = bt_param_parse(arg, error_str);
1931 if (!params) {
1932 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
1933 error_str->str);
1934 goto error;
1935 }
1936
1937 extend_status = bt_value_map_extend(
1938 cur_cfg_comp->params, params, &params_to_set);
1939 BT_VALUE_PUT_REF_AND_RESET(params);
1940 if (extend_status != BT_VALUE_MAP_EXTEND_STATUS_OK) {
1941 BT_CLI_LOGE_APPEND_CAUSE("Cannot extend current component parameters with --params option's argument:\n %s",
1942 arg);
1943 goto error;
1944 }
1945
1946 BT_OBJECT_MOVE_REF(cur_cfg_comp->params, params_to_set);
1947 break;
1948 }
1949 case OPT_LOG_LEVEL:
1950 if (!cur_cfg_comp) {
1951 BT_CLI_LOGE_APPEND_CAUSE("Cannot set the log level of unavailable component:\n %s",
1952 arg);
1953 goto error;
1954 }
1955
1956 cur_cfg_comp->log_level =
1957 bt_log_get_level_from_string(arg);
1958 if (cur_cfg_comp->log_level < 0) {
1959 BT_CLI_LOGE_APPEND_CAUSE("Invalid argument for --log-level option:\n %s",
1960 arg);
1961 goto error;
1962 }
1963 break;
1964 case OPT_BASE_PARAMS:
1965 {
1966 bt_value *params = bt_param_parse(arg, error_str);
1967
1968 if (!params) {
1969 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --base-params option's argument:\n %s",
1970 error_str->str);
1971 goto error;
1972 }
1973
1974 BT_OBJECT_MOVE_REF(cur_base_params, params);
1975 break;
1976 }
1977 case OPT_RESET_BASE_PARAMS:
1978 BT_VALUE_PUT_REF_AND_RESET(cur_base_params);
1979 cur_base_params = bt_value_map_create();
1980 if (!cur_base_params) {
1981 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1982 goto error;
1983 }
1984 break;
1985 case OPT_CONNECT:
1986 if (bt_value_array_append_string_element(
1987 connection_args, arg)) {
1988 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1989 goto error;
1990 }
1991 break;
1992 case OPT_RETRY_DURATION: {
1993 gchar *end;
1994 size_t arg_len = strlen(argpar_item_opt->arg);
1995
1996 retry_duration = g_ascii_strtoll(argpar_item_opt->arg, &end, 10);
1997
1998 if (arg_len == 0 || end != (argpar_item_opt->arg + arg_len)) {
1999 BT_CLI_LOGE_APPEND_CAUSE(
2000 "Could not parse --retry-duration option's argument as an unsigned integer: `%s`",
2001 argpar_item_opt->arg);
2002 goto error;
2003 }
2004
2005 if (retry_duration < 0) {
2006 BT_CLI_LOGE_APPEND_CAUSE("--retry-duration option's argument must be positive or 0: %ld",
2007 retry_duration);
2008 goto error;
2009 }
2010
2011 cfg->cmd_data.run.retry_duration_us =
2012 (uint64_t) retry_duration;
2013 break;
2014 }
2015 default:
2016 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
2017 argpar_item_opt->descr->id);
2018 goto error;
2019 }
2020 }
2021
2022 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp);
2023
2024 if (cfg->cmd_data.run.sources->len == 0) {
2025 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no source component.");
2026 goto error;
2027 }
2028
2029 if (cfg->cmd_data.run.sinks->len == 0) {
2030 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no sink component.");
2031 goto error;
2032 }
2033
2034 ret = bt_config_cli_args_create_connections(cfg,
2035 connection_args,
2036 error_buf, 256);
2037 if (ret) {
2038 BT_CLI_LOGE_APPEND_CAUSE("Cannot creation connections:\n%s", error_buf);
2039 goto error;
2040 }
2041
2042 goto end;
2043
2044 error:
2045 *retcode = 1;
2046 BT_OBJECT_PUT_REF_AND_RESET(cfg);
2047
2048 end:
2049 if (error_str) {
2050 g_string_free(error_str, TRUE);
2051 }
2052
2053 bt_argpar_parse_ret_fini(&argpar_parse_ret);
2054 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp);
2055 BT_VALUE_PUT_REF_AND_RESET(cur_base_params);
2056 BT_VALUE_PUT_REF_AND_RESET(instance_names);
2057 BT_VALUE_PUT_REF_AND_RESET(connection_args);
2058 return cfg;
2059 }
2060
2061 static
2062 struct bt_config *bt_config_run_from_args_array(const bt_value *run_args,
2063 int *retcode, const bt_value *plugin_paths,
2064 int default_log_level)
2065 {
2066 struct bt_config *cfg = NULL;
2067 const char **argv;
2068 int64_t i, len;
2069 const size_t argc = bt_value_array_get_length(run_args);
2070
2071 argv = calloc(argc, sizeof(*argv));
2072 if (!argv) {
2073 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2074 goto end;
2075 }
2076
2077 len = bt_value_array_get_length(run_args);
2078 if (len < 0) {
2079 BT_CLI_LOGE_APPEND_CAUSE("Invalid executable arguments.");
2080 goto end;
2081 }
2082 for (i = 0; i < len; i++) {
2083 const bt_value *arg_value =
2084 bt_value_array_borrow_element_by_index_const(run_args,
2085 i);
2086 const char *arg;
2087
2088 BT_ASSERT(arg_value);
2089 arg = bt_value_string_get(arg_value);
2090 BT_ASSERT(arg);
2091 argv[i] = arg;
2092 }
2093
2094 cfg = bt_config_run_from_args(argc, argv, retcode,
2095 plugin_paths, default_log_level);
2096
2097 end:
2098 free(argv);
2099 return cfg;
2100 }
2101
2102 /*
2103 * Prints the convert command usage.
2104 */
2105 static
2106 void print_convert_usage(FILE *fp)
2107 {
2108 fprintf(fp, "Usage: babeltrace2 [GENERAL OPTIONS] [convert] [OPTIONS] [PATH/URL]\n");
2109 fprintf(fp, "\n");
2110 fprintf(fp, "Options:\n");
2111 fprintf(fp, "\n");
2112 fprintf(fp, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2113 fprintf(fp, " Instantiate the component class CLS of type\n");
2114 fprintf(fp, " TYPE (`source`, `filter`, or `sink`) found\n");
2115 fprintf(fp, " in the plugin PLUGIN, add it to the\n");
2116 fprintf(fp, " conversion graph, and optionally name it\n");
2117 fprintf(fp, " NAME\n");
2118 fprintf(fp, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
2119 fprintf(fp, " (`N`, `T`, `D`, `I`, `W`, `E`, or `F`)\n");
2120 fprintf(fp, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2121 fprintf(fp, " current component (see the expected format\n");
2122 fprintf(fp, " of PARAMS below)\n");
2123 fprintf(fp, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
2124 fprintf(fp, " the graph later, retry in DUR µs\n");
2125 fprintf(fp, " (default: 100000)\n");
2126 fprintf(fp, " dynamic plugins can be loaded\n");
2127 fprintf(fp, " --run-args Print the equivalent arguments for the\n");
2128 fprintf(fp, " `run` command to the standard output,\n");
2129 fprintf(fp, " formatted for a shell, and quit\n");
2130 fprintf(fp, " --run-args-0 Print the equivalent arguments for the\n");
2131 fprintf(fp, " `run` command to the standard output,\n");
2132 fprintf(fp, " formatted for `xargs -0`, and quit\n");
2133 fprintf(fp, " --stream-intersection Only process events when all streams\n");
2134 fprintf(fp, " are active\n");
2135 fprintf(fp, " -h, --help Show this help and quit\n");
2136 fprintf(fp, "\n");
2137 fprintf(fp, "Implicit `source.ctf.fs` component options:\n");
2138 fprintf(fp, "\n");
2139 fprintf(fp, " --clock-force-correlate Force the origin of all clocks\n");
2140 fprintf(fp, " to the Unix epoch\n");
2141 fprintf(fp, " --clock-offset=SEC Set clock offset to SEC seconds\n");
2142 fprintf(fp, " --clock-offset-ns=NS Set clock offset to NS ns\n");
2143 fprintf(fp, "\n");
2144 fprintf(fp, "Implicit `sink.text.pretty` component options:\n");
2145 fprintf(fp, "\n");
2146 fprintf(fp, " --clock-cycles Print timestamps in clock cycles\n");
2147 fprintf(fp, " --clock-date Print timestamp dates\n");
2148 fprintf(fp, " --clock-gmt Print and parse timestamps in the GMT\n");
2149 fprintf(fp, " time zone instead of the local time zone\n");
2150 fprintf(fp, " --clock-seconds Print the timestamps as `SEC.NS` instead\n");
2151 fprintf(fp, " of `hh:mm:ss.nnnnnnnnn`\n");
2152 fprintf(fp, " --color=(never | auto | always)\n");
2153 fprintf(fp, " Never, automatically, or always emit\n");
2154 fprintf(fp, " console color codes\n");
2155 fprintf(fp, " -f, --fields=FIELD[,FIELD]... Print additional fields; FIELD can be:\n");
2156 fprintf(fp, " `all`, `trace`, `trace:hostname`,\n");
2157 fprintf(fp, " `trace:domain`, `trace:procname`,\n");
2158 fprintf(fp, " `trace:vpid`, `loglevel`, `emf`\n");
2159 fprintf(fp, " -n, --names=NAME[,NAME]... Print field names; NAME can be:\n");
2160 fprintf(fp, " `payload` (or `arg` or `args`), `none`,\n");
2161 fprintf(fp, " `all`, `scope`, `header`, `context`\n");
2162 fprintf(fp, " (or `ctx`)\n");
2163 fprintf(fp, " --no-delta Do not print time delta between\n");
2164 fprintf(fp, " consecutive events\n");
2165 fprintf(fp, " -w, --output=PATH Write output text to PATH instead of\n");
2166 fprintf(fp, " the standard output\n");
2167 fprintf(fp, "\n");
2168 fprintf(fp, "Implicit `filter.utils.trimmer` component options:\n");
2169 fprintf(fp, "\n");
2170 fprintf(fp, " -b, --begin=BEGIN Set the beginning time of the conversion\n");
2171 fprintf(fp, " time range to BEGIN (see the format of\n");
2172 fprintf(fp, " BEGIN below)\n");
2173 fprintf(fp, " -e, --end=END Set the end time of the conversion time\n");
2174 fprintf(fp, " range to END (see the format of END below)\n");
2175 fprintf(fp, " -t, --timerange=TIMERANGE Set conversion time range to TIMERANGE:\n");
2176 fprintf(fp, " BEGIN,END or [BEGIN,END] (literally `[` and\n");
2177 fprintf(fp, " `]`) (see the format of BEGIN/END below)\n");
2178 fprintf(fp, "\n");
2179 fprintf(fp, "Implicit `filter.lttng-utils.debug-info` component options:\n");
2180 fprintf(fp, "\n");
2181 fprintf(fp, " --debug-info Create an implicit\n");
2182 fprintf(fp, " `filter.lttng-utils.debug-info` component\n");
2183 fprintf(fp, " --debug-info-dir=DIR Search for debug info in directory DIR\n");
2184 fprintf(fp, " instead of `/usr/lib/debug`\n");
2185 fprintf(fp, " --debug-info-full-path Show full debug info source and\n");
2186 fprintf(fp, " binary paths instead of just names\n");
2187 fprintf(fp, " --debug-info-target-prefix=DIR\n");
2188 fprintf(fp, " Use directory DIR as a prefix when\n");
2189 fprintf(fp, " looking up executables during debug\n");
2190 fprintf(fp, " info analysis\n");
2191 fprintf(fp, "\n");
2192 fprintf(fp, "Legacy options that still work:\n");
2193 fprintf(fp, "\n");
2194 fprintf(fp, " -i, --input-format=(ctf | lttng-live)\n");
2195 fprintf(fp, " `ctf`:\n");
2196 fprintf(fp, " Create an implicit `source.ctf.fs`\n");
2197 fprintf(fp, " component\n");
2198 fprintf(fp, " `lttng-live`:\n");
2199 fprintf(fp, " Create an implicit `source.ctf.lttng-live`\n");
2200 fprintf(fp, " component\n");
2201 fprintf(fp, " -o, --output-format=(text | ctf | dummy | ctf-metadata)\n");
2202 fprintf(fp, " `text`:\n");
2203 fprintf(fp, " Create an implicit `sink.text.pretty`\n");
2204 fprintf(fp, " component\n");
2205 fprintf(fp, " `ctf`:\n");
2206 fprintf(fp, " Create an implicit `sink.ctf.fs`\n");
2207 fprintf(fp, " component\n");
2208 fprintf(fp, " `dummy`:\n");
2209 fprintf(fp, " Create an implicit `sink.utils.dummy`\n");
2210 fprintf(fp, " component\n");
2211 fprintf(fp, " `ctf-metadata`:\n");
2212 fprintf(fp, " Query the `source.ctf.fs` component class\n");
2213 fprintf(fp, " for metadata text and quit\n");
2214 fprintf(fp, "\n");
2215 fprintf(fp, "See `babeltrace2 --help` for the list of general options.\n");
2216 fprintf(fp, "\n\n");
2217 fprintf(fp, "Format of BEGIN and END\n");
2218 fprintf(fp, "-----------------------\n");
2219 fprintf(fp, "\n");
2220 fprintf(fp, " [YYYY-MM-DD [hh:mm:]]ss[.nnnnnnnnn]\n");
2221 fprintf(fp, "\n\n");
2222 print_expected_params_format(fp);
2223 }
2224
2225 static
2226 const struct bt_argpar_opt_descr convert_options[] = {
2227 /* id, short_name, long_name, with_arg */
2228 { OPT_BEGIN, 'b', "begin", true },
2229 { OPT_CLOCK_CYCLES, '\0', "clock-cycles", false },
2230 { OPT_CLOCK_DATE, '\0', "clock-date", false },
2231 { OPT_CLOCK_FORCE_CORRELATE, '\0', "clock-force-correlate", false },
2232 { OPT_CLOCK_GMT, '\0', "clock-gmt", false },
2233 { OPT_CLOCK_OFFSET, '\0', "clock-offset", true },
2234 { OPT_CLOCK_OFFSET_NS, '\0', "clock-offset-ns", true },
2235 { OPT_CLOCK_SECONDS, '\0', "clock-seconds", false },
2236 { OPT_COLOR, '\0', "color", true },
2237 { OPT_COMPONENT, 'c', "component", true },
2238 { OPT_DEBUG, 'd', "debug", false },
2239 { OPT_DEBUG_INFO_DIR, '\0', "debug-info-dir", true },
2240 { OPT_DEBUG_INFO_FULL_PATH, '\0', "debug-info-full-path", false },
2241 { OPT_DEBUG_INFO_TARGET_PREFIX, '\0', "debug-info-target-prefix", true },
2242 { OPT_END, 'e', "end", true },
2243 { OPT_FIELDS, 'f', "fields", true },
2244 { OPT_HELP, 'h', "help", false },
2245 { OPT_INPUT_FORMAT, 'i', "input-format", true },
2246 { OPT_LOG_LEVEL, 'l', "log-level", true },
2247 { OPT_NAMES, 'n', "names", true },
2248 { OPT_DEBUG_INFO, '\0', "debug-info", false },
2249 { OPT_NO_DELTA, '\0', "no-delta", false },
2250 { OPT_OMIT_HOME_PLUGIN_PATH, '\0', "omit-home-plugin-path", false },
2251 { OPT_OMIT_SYSTEM_PLUGIN_PATH, '\0', "omit-system-plugin-path", false },
2252 { OPT_OUTPUT, 'w', "output", true },
2253 { OPT_OUTPUT_FORMAT, 'o', "output-format", true },
2254 { OPT_PARAMS, 'p', "params", true },
2255 { OPT_PLUGIN_PATH, '\0', "plugin-path", true },
2256 { OPT_RETRY_DURATION, '\0', "retry-duration", true },
2257 { OPT_RUN_ARGS, '\0', "run-args", false },
2258 { OPT_RUN_ARGS_0, '\0', "run-args-0", false },
2259 { OPT_STREAM_INTERSECTION, '\0', "stream-intersection", false },
2260 { OPT_TIMERANGE, '\0', "timerange", true },
2261 { OPT_VERBOSE, 'v', "verbose", false },
2262 BT_ARGPAR_OPT_DESCR_SENTINEL
2263 };
2264
2265 static
2266 GString *get_component_auto_name(const char *prefix,
2267 const bt_value *existing_names)
2268 {
2269 unsigned int i = 0;
2270 GString *auto_name = g_string_new(NULL);
2271
2272 if (!auto_name) {
2273 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2274 goto end;
2275 }
2276
2277 if (!bt_value_map_has_entry(existing_names, prefix)) {
2278 g_string_assign(auto_name, prefix);
2279 goto end;
2280 }
2281
2282 do {
2283 g_string_printf(auto_name, "%s-%d", prefix, i);
2284 i++;
2285 } while (bt_value_map_has_entry(existing_names, auto_name->str));
2286
2287 end:
2288 return auto_name;
2289 }
2290
2291 struct implicit_component_args {
2292 bool exists;
2293
2294 /* The component class name (e.g. src.ctf.fs). */
2295 GString *comp_arg;
2296
2297 /* The component instance name. */
2298 GString *name_arg;
2299
2300 GString *params_arg;
2301 bt_value *extra_params;
2302 };
2303
2304 static
2305 int assign_name_to_implicit_component(struct implicit_component_args *args,
2306 const char *prefix, bt_value *existing_names,
2307 GList **comp_names, bool append_to_comp_names)
2308 {
2309 int ret = 0;
2310 GString *name = NULL;
2311
2312 if (!args->exists) {
2313 goto end;
2314 }
2315
2316 name = get_component_auto_name(prefix,
2317 existing_names);
2318
2319 if (!name) {
2320 ret = -1;
2321 goto end;
2322 }
2323
2324 g_string_assign(args->name_arg, name->str);
2325
2326 if (bt_value_map_insert_entry(existing_names, name->str,
2327 bt_value_null)) {
2328 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2329 ret = -1;
2330 goto end;
2331 }
2332
2333 if (append_to_comp_names) {
2334 *comp_names = g_list_append(*comp_names, name);
2335 name = NULL;
2336 }
2337
2338 end:
2339 if (name) {
2340 g_string_free(name, TRUE);
2341 }
2342
2343 return ret;
2344 }
2345
2346 static
2347 int append_run_args_for_implicit_component(
2348 struct implicit_component_args *impl_args,
2349 bt_value *run_args)
2350 {
2351 int ret = 0;
2352 size_t i;
2353 GString *component_arg_for_run = NULL;
2354
2355 if (!impl_args->exists) {
2356 goto end;
2357 }
2358
2359 component_arg_for_run = g_string_new(NULL);
2360 if (!component_arg_for_run) {
2361 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2362 goto error;
2363 }
2364
2365 /* Build the full `name:type.plugin.cls`. */
2366 BT_ASSERT(!strchr(impl_args->name_arg->str, '\\'));
2367 BT_ASSERT(!strchr(impl_args->name_arg->str, ':'));
2368 g_string_printf(component_arg_for_run, "%s:%s",
2369 impl_args->name_arg->str, impl_args->comp_arg->str);
2370
2371 if (bt_value_array_append_string_element(run_args, "--component")) {
2372 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2373 goto error;
2374 }
2375
2376 if (bt_value_array_append_string_element(run_args,
2377 component_arg_for_run->str)) {
2378 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2379 goto error;
2380 }
2381
2382 if (impl_args->params_arg->len > 0) {
2383 if (bt_value_array_append_string_element(run_args, "--params")) {
2384 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2385 goto error;
2386 }
2387
2388 if (bt_value_array_append_string_element(run_args,
2389 impl_args->params_arg->str)) {
2390 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2391 goto error;
2392 }
2393 }
2394
2395 for (i = 0; i < bt_value_array_get_length(impl_args->extra_params);
2396 i++) {
2397 const bt_value *elem;
2398 const char *arg;
2399
2400 elem = bt_value_array_borrow_element_by_index(impl_args->extra_params,
2401 i);
2402 if (!elem) {
2403 goto error;
2404 }
2405
2406 BT_ASSERT(bt_value_is_string(elem));
2407 arg = bt_value_string_get(elem);
2408 ret = bt_value_array_append_string_element(run_args, arg);
2409 if (ret) {
2410 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2411 goto error;
2412 }
2413 }
2414
2415 goto end;
2416
2417 error:
2418 ret = -1;
2419
2420 end:
2421 if (component_arg_for_run) {
2422 g_string_free(component_arg_for_run, TRUE);
2423 }
2424
2425 return ret;
2426 }
2427
2428 /* Free the fields of a `struct implicit_component_args`. */
2429
2430 static
2431 void finalize_implicit_component_args(struct implicit_component_args *args)
2432 {
2433 BT_ASSERT(args);
2434
2435 if (args->comp_arg) {
2436 g_string_free(args->comp_arg, TRUE);
2437 }
2438
2439 if (args->name_arg) {
2440 g_string_free(args->name_arg, TRUE);
2441 }
2442
2443 if (args->params_arg) {
2444 g_string_free(args->params_arg, TRUE);
2445 }
2446
2447 bt_value_put_ref(args->extra_params);
2448 }
2449
2450 /* Destroy a dynamically-allocated `struct implicit_component_args`. */
2451
2452 static
2453 void destroy_implicit_component_args(struct implicit_component_args *args)
2454 {
2455 finalize_implicit_component_args(args);
2456 g_free(args);
2457 }
2458
2459 /* Initialize the fields of an already allocated `struct implicit_component_args`. */
2460
2461 static
2462 int init_implicit_component_args(struct implicit_component_args *args,
2463 const char *comp_arg, bool exists)
2464 {
2465 int ret = 0;
2466
2467 args->exists = exists;
2468 args->comp_arg = g_string_new(comp_arg);
2469 args->name_arg = g_string_new(NULL);
2470 args->params_arg = g_string_new(NULL);
2471 args->extra_params = bt_value_array_create();
2472
2473 if (!args->comp_arg || !args->name_arg ||
2474 !args->params_arg || !args->extra_params) {
2475 ret = -1;
2476 finalize_implicit_component_args(args);
2477 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2478 goto end;
2479 }
2480
2481 end:
2482 return ret;
2483 }
2484
2485 /* Dynamically allocate and initialize a `struct implicit_component_args`. */
2486
2487 static
2488 struct implicit_component_args *create_implicit_component_args(
2489 const char *comp_arg)
2490 {
2491 struct implicit_component_args *args;
2492 int status;
2493
2494 args = g_new(struct implicit_component_args, 1);
2495 if (!args) {
2496 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2497 goto end;
2498 }
2499
2500 status = init_implicit_component_args(args, comp_arg, true);
2501 if (status != 0) {
2502 g_free(args);
2503 args = NULL;
2504 }
2505
2506 end:
2507 return args;
2508 }
2509
2510 static
2511 void append_implicit_component_param(struct implicit_component_args *args,
2512 const char *key, const char *value)
2513 {
2514 BT_ASSERT(args);
2515 BT_ASSERT(key);
2516 BT_ASSERT(value);
2517 append_param_arg(args->params_arg, key, value);
2518 }
2519
2520 /*
2521 * Append the given parameter (`key=value`) to all component specifications
2522 * in `implicit_comp_args` (an array of `struct implicit_component_args *`)
2523 * which match `comp_arg`.
2524 *
2525 * Return the number of matching components.
2526 */
2527
2528 static
2529 int append_multiple_implicit_components_param(GPtrArray *implicit_comp_args,
2530 const char *comp_arg, const char *key, const char *value)
2531 {
2532 int i;
2533 int n = 0;
2534
2535 for (i = 0; i < implicit_comp_args->len; i++) {
2536 struct implicit_component_args *args = implicit_comp_args->pdata[i];
2537
2538 if (strcmp(args->comp_arg->str, comp_arg) == 0) {
2539 append_implicit_component_param(args, key, value);
2540 n++;
2541 }
2542 }
2543
2544 return n;
2545 }
2546
2547 /* Escape value to make it suitable to use as a string parameter value. */
2548 static
2549 gchar *escape_string_value(const char *value)
2550 {
2551 GString *ret;
2552 const char *in;
2553
2554 ret = g_string_new(NULL);
2555 if (!ret) {
2556 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2557 goto end;
2558 }
2559
2560 in = value;
2561 while (*in) {
2562 switch (*in) {
2563 case '"':
2564 case '\\':
2565 g_string_append_c(ret, '\\');
2566 break;
2567 }
2568
2569 g_string_append_c(ret, *in);
2570
2571 in++;
2572 }
2573
2574 end:
2575 return g_string_free(ret, FALSE);
2576 }
2577
2578 static
2579 int bt_value_to_cli_param_value_append(const bt_value *value, GString *buf)
2580 {
2581 BT_ASSERT(buf);
2582
2583 int ret = -1;
2584
2585 switch (bt_value_get_type(value)) {
2586 case BT_VALUE_TYPE_STRING:
2587 {
2588 const char *str_value = bt_value_string_get(value);
2589 gchar *escaped_str_value;
2590
2591 escaped_str_value = escape_string_value(str_value);
2592 if (!escaped_str_value) {
2593 goto end;
2594 }
2595
2596 g_string_append_printf(buf, "\"%s\"", escaped_str_value);
2597
2598 g_free(escaped_str_value);
2599 break;
2600 }
2601 case BT_VALUE_TYPE_ARRAY: {
2602 g_string_append_c(buf, '[');
2603 uint64_t sz = bt_value_array_get_length(value);
2604 for (uint64_t i = 0; i < sz; i++) {
2605 const bt_value *item;
2606 int ret;
2607
2608 if (i > 0) {
2609 g_string_append(buf, ", ");
2610 }
2611
2612 item = bt_value_array_borrow_element_by_index_const(
2613 value, i);
2614 ret = bt_value_to_cli_param_value_append(item, buf);
2615
2616 if (ret) {
2617 goto end;
2618 }
2619 }
2620 g_string_append_c(buf, ']');
2621 break;
2622 }
2623 default:
2624 abort();
2625 }
2626
2627 ret = 0;
2628
2629 end:
2630 return ret;
2631 }
2632
2633 /*
2634 * Convert `value` to its equivalent representation as a command line parameter
2635 * value.
2636 */
2637
2638 static
2639 gchar *bt_value_to_cli_param_value(bt_value *value)
2640 {
2641 GString *buf;
2642 gchar *result = NULL;
2643
2644 buf = g_string_new(NULL);
2645 if (!buf) {
2646 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2647 goto error;
2648 }
2649
2650 if (bt_value_to_cli_param_value_append(value, buf)) {
2651 goto error;
2652 }
2653
2654 result = g_string_free(buf, FALSE);
2655 buf = NULL;
2656
2657 goto end;
2658
2659 error:
2660 if (buf) {
2661 g_string_free(buf, TRUE);
2662 }
2663
2664 end:
2665 return result;
2666 }
2667
2668 static
2669 int append_parameter_to_args(bt_value *args, const char *key, bt_value *value)
2670 {
2671 BT_ASSERT(args);
2672 BT_ASSERT(bt_value_get_type(args) == BT_VALUE_TYPE_ARRAY);
2673 BT_ASSERT(key);
2674 BT_ASSERT(value);
2675
2676 int ret = 0;
2677 gchar *str_value = NULL;
2678 GString *parameter = NULL;
2679
2680 if (bt_value_array_append_string_element(args, "--params")) {
2681 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2682 ret = -1;
2683 goto end;
2684 }
2685
2686 str_value = bt_value_to_cli_param_value(value);
2687 if (!str_value) {
2688 ret = -1;
2689 goto end;
2690 }
2691
2692 parameter = g_string_new(NULL);
2693 if (!parameter) {
2694 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2695 ret = -1;
2696 goto end;
2697 }
2698
2699 g_string_printf(parameter, "%s=%s", key, str_value);
2700
2701 if (bt_value_array_append_string_element(args, parameter->str)) {
2702 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2703 ret = -1;
2704 goto end;
2705 }
2706
2707 end:
2708 if (parameter) {
2709 g_string_free(parameter, TRUE);
2710 parameter = NULL;
2711 }
2712
2713 if (str_value) {
2714 g_free(str_value);
2715 str_value = NULL;
2716 }
2717
2718 return ret;
2719 }
2720
2721 static
2722 int append_string_parameter_to_args(bt_value *args, const char *key, const char *value)
2723 {
2724 bt_value *str_value;
2725 int ret;
2726
2727 str_value = bt_value_string_create_init(value);
2728
2729 if (!str_value) {
2730 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2731 ret = -1;
2732 goto end;
2733 }
2734
2735 ret = append_parameter_to_args(args, key, str_value);
2736
2737 end:
2738 BT_VALUE_PUT_REF_AND_RESET(str_value);
2739 return ret;
2740 }
2741
2742 static
2743 int append_implicit_component_extra_param(struct implicit_component_args *args,
2744 const char *key, const char *value)
2745 {
2746 return append_string_parameter_to_args(args->extra_params, key, value);
2747 }
2748
2749 /*
2750 * Escapes `.`, `:`, and `\` of `input` with `\`.
2751 */
2752 static
2753 GString *escape_dot_colon(const char *input)
2754 {
2755 GString *output = g_string_new(NULL);
2756 const char *ch;
2757
2758 if (!output) {
2759 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2760 goto end;
2761 }
2762
2763 for (ch = input; *ch != '\0'; ch++) {
2764 if (*ch == '\\' || *ch == '.' || *ch == ':') {
2765 g_string_append_c(output, '\\');
2766 }
2767
2768 g_string_append_c(output, *ch);
2769 }
2770
2771 end:
2772 return output;
2773 }
2774
2775 /*
2776 * Appends a --connect option to a list of arguments. `upstream_name`
2777 * and `downstream_name` are escaped with escape_dot_colon() in this
2778 * function.
2779 */
2780 static
2781 int append_connect_arg(bt_value *run_args,
2782 const char *upstream_name, const char *downstream_name)
2783 {
2784 int ret = 0;
2785 GString *e_upstream_name = escape_dot_colon(upstream_name);
2786 GString *e_downstream_name = escape_dot_colon(downstream_name);
2787 GString *arg = g_string_new(NULL);
2788
2789 if (!e_upstream_name || !e_downstream_name || !arg) {
2790 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2791 ret = -1;
2792 goto end;
2793 }
2794
2795 ret = bt_value_array_append_string_element(run_args, "--connect");
2796 if (ret) {
2797 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2798 ret = -1;
2799 goto end;
2800 }
2801
2802 g_string_append(arg, e_upstream_name->str);
2803 g_string_append_c(arg, ':');
2804 g_string_append(arg, e_downstream_name->str);
2805 ret = bt_value_array_append_string_element(run_args, arg->str);
2806 if (ret) {
2807 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2808 ret = -1;
2809 goto end;
2810 }
2811
2812 end:
2813 if (arg) {
2814 g_string_free(arg, TRUE);
2815 }
2816
2817 if (e_upstream_name) {
2818 g_string_free(e_upstream_name, TRUE);
2819 }
2820
2821 if (e_downstream_name) {
2822 g_string_free(e_downstream_name, TRUE);
2823 }
2824
2825 return ret;
2826 }
2827
2828 /*
2829 * Appends the run command's --connect options for the convert command.
2830 */
2831 static
2832 int convert_auto_connect(bt_value *run_args,
2833 GList *source_names, GList *filter_names,
2834 GList *sink_names)
2835 {
2836 int ret = 0;
2837 GList *source_at = source_names;
2838 GList *filter_at = filter_names;
2839 GList *filter_prev;
2840 GList *sink_at = sink_names;
2841
2842 BT_ASSERT(source_names);
2843 BT_ASSERT(filter_names);
2844 BT_ASSERT(sink_names);
2845
2846 /* Connect all sources to the first filter */
2847 for (source_at = source_names; source_at; source_at = g_list_next(source_at)) {
2848 GString *source_name = source_at->data;
2849 GString *filter_name = filter_at->data;
2850
2851 ret = append_connect_arg(run_args, source_name->str,
2852 filter_name->str);
2853 if (ret) {
2854 goto error;
2855 }
2856 }
2857
2858 filter_prev = filter_at;
2859 filter_at = g_list_next(filter_at);
2860
2861 /* Connect remaining filters */
2862 for (; filter_at; filter_prev = filter_at, filter_at = g_list_next(filter_at)) {
2863 GString *filter_name = filter_at->data;
2864 GString *filter_prev_name = filter_prev->data;
2865
2866 ret = append_connect_arg(run_args, filter_prev_name->str,
2867 filter_name->str);
2868 if (ret) {
2869 goto error;
2870 }
2871 }
2872
2873 /* Connect last filter to all sinks */
2874 for (sink_at = sink_names; sink_at; sink_at = g_list_next(sink_at)) {
2875 GString *filter_name = filter_prev->data;
2876 GString *sink_name = sink_at->data;
2877
2878 ret = append_connect_arg(run_args, filter_name->str,
2879 sink_name->str);
2880 if (ret) {
2881 goto error;
2882 }
2883 }
2884
2885 goto end;
2886
2887 error:
2888 ret = -1;
2889
2890 end:
2891 return ret;
2892 }
2893
2894 static
2895 int split_timerange(const char *arg, char **begin, char **end)
2896 {
2897 int ret = 0;
2898 const char *ch = arg;
2899 size_t end_pos;
2900 GString *g_begin = NULL;
2901 GString *g_end = NULL;
2902
2903 BT_ASSERT(arg);
2904
2905 if (*ch == '[') {
2906 ch++;
2907 }
2908
2909 g_begin = bt_common_string_until(ch, "", ",", &end_pos);
2910 if (!g_begin || ch[end_pos] != ',' || g_begin->len == 0) {
2911 goto error;
2912 }
2913
2914 ch += end_pos + 1;
2915
2916 g_end = bt_common_string_until(ch, "", "]", &end_pos);
2917 if (!g_end || g_end->len == 0) {
2918 goto error;
2919 }
2920
2921 BT_ASSERT(begin);
2922 BT_ASSERT(end);
2923 *begin = g_begin->str;
2924 *end = g_end->str;
2925 g_string_free(g_begin, FALSE);
2926 g_string_free(g_end, FALSE);
2927 g_begin = NULL;
2928 g_end = NULL;
2929 goto end;
2930
2931 error:
2932 ret = -1;
2933
2934 end:
2935 if (g_begin) {
2936 g_string_free(g_begin, TRUE);
2937 }
2938
2939 if (g_end) {
2940 g_string_free(g_end, TRUE);
2941 }
2942
2943 return ret;
2944 }
2945
2946 static
2947 int g_list_prepend_gstring(GList **list, const char *string)
2948 {
2949 int ret = 0;
2950 GString *gs = g_string_new(string);
2951
2952 BT_ASSERT(list);
2953
2954 if (!gs) {
2955 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2956 goto end;
2957 }
2958
2959 *list = g_list_prepend(*list, gs);
2960
2961 end:
2962 return ret;
2963 }
2964
2965 /*
2966 * Create `struct implicit_component_args` structures for each of the
2967 * source components we identified. Add them to `component_args`.
2968 *
2969 * `non_opts` is an array of the non-option arguments passed on the command
2970 * line.
2971 *
2972 * `non_opt_params` is an array where each element is an array of
2973 * strings containing all the arguments to `--params` that apply to the
2974 * non-option argument at the same index. For example, if, for a
2975 * non-option argument, the following `--params` options applied:
2976 *
2977 * --params=a=2 --params=b=3,c=4
2978 *
2979 * its entry in `non_opt_params` would contain
2980 *
2981 * ["a=2", "b=3,c=4"]
2982 */
2983
2984 static
2985 int create_implicit_component_args_from_auto_discovered_sources(
2986 const struct auto_source_discovery *auto_disc,
2987 const bt_value *non_opts,
2988 const bt_value *non_opt_params,
2989 const bt_value *non_opt_loglevels,
2990 GPtrArray *component_args)
2991 {
2992 gchar *cc_name = NULL;
2993 struct implicit_component_args *comp = NULL;
2994 int status;
2995 guint i, len;
2996
2997 len = auto_disc->results->len;
2998
2999 for (i = 0; i < len; i++) {
3000 struct auto_source_discovery_result *res =
3001 g_ptr_array_index(auto_disc->results, i);
3002 uint64_t orig_indices_i, orig_indices_count;
3003
3004 g_free(cc_name);
3005 cc_name = g_strdup_printf("source.%s.%s", res->plugin_name, res->source_cc_name);
3006 if (!cc_name) {
3007 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3008 goto error;
3009 }
3010
3011 comp = create_implicit_component_args(cc_name);
3012 if (!comp) {
3013 goto error;
3014 }
3015
3016 /*
3017 * Append parameters and log levels of all the
3018 * non-option arguments that contributed to this
3019 * component instance coming into existence.
3020 */
3021 orig_indices_count = bt_value_array_get_length(res->original_input_indices);
3022 for (orig_indices_i = 0; orig_indices_i < orig_indices_count; orig_indices_i++) {
3023 const bt_value *orig_idx_value =
3024 bt_value_array_borrow_element_by_index(
3025 res->original_input_indices, orig_indices_i);
3026 uint64_t orig_idx = bt_value_integer_unsigned_get(orig_idx_value);
3027 const bt_value *params_array =
3028 bt_value_array_borrow_element_by_index_const(
3029 non_opt_params, orig_idx);
3030 uint64_t params_i, params_count;
3031 const bt_value *loglevel_value;
3032
3033 params_count = bt_value_array_get_length(params_array);
3034 for (params_i = 0; params_i < params_count; params_i++) {
3035 const bt_value *params_value =
3036 bt_value_array_borrow_element_by_index_const(
3037 params_array, params_i);
3038 const char *params = bt_value_string_get(params_value);
3039 bt_value_array_append_element_status append_status;
3040
3041 append_status = bt_value_array_append_string_element(
3042 comp->extra_params, "--params");
3043 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
3044 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3045 goto error;
3046 }
3047
3048 append_status = bt_value_array_append_string_element(
3049 comp->extra_params, params);
3050 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
3051 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3052 goto error;
3053 }
3054 }
3055
3056 loglevel_value = bt_value_array_borrow_element_by_index_const(
3057 non_opt_loglevels, orig_idx);
3058 if (bt_value_get_type(loglevel_value) == BT_VALUE_TYPE_STRING) {
3059 const char *loglevel = bt_value_string_get(loglevel_value);
3060 bt_value_array_append_element_status append_status;
3061
3062 append_status = bt_value_array_append_string_element(
3063 comp->extra_params, "--log-level");
3064 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
3065 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3066 goto error;
3067 }
3068
3069 append_status = bt_value_array_append_string_element(
3070 comp->extra_params, loglevel);
3071 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
3072 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3073 goto error;
3074 }
3075 }
3076 }
3077
3078 /*
3079 * If single input and a src.ctf.fs component, provide the
3080 * relative path from the path passed on the command line to the
3081 * found trace.
3082 */
3083 if (bt_value_array_get_length(res->inputs) == 1 &&
3084 strcmp(res->plugin_name, "ctf") == 0 &&
3085 strcmp(res->source_cc_name, "fs") == 0) {
3086 const bt_value *orig_idx_value =
3087 bt_value_array_borrow_element_by_index(
3088 res->original_input_indices, 0);
3089 uint64_t orig_idx = bt_value_integer_unsigned_get(orig_idx_value);
3090 const bt_value *non_opt_value =
3091 bt_value_array_borrow_element_by_index_const(
3092 non_opts, orig_idx);
3093 const char *non_opt = bt_value_string_get(non_opt_value);
3094 const bt_value *input_value =
3095 bt_value_array_borrow_element_by_index_const(
3096 res->inputs, 0);
3097 const char *input = bt_value_string_get(input_value);
3098
3099 BT_ASSERT(orig_indices_count == 1);
3100 BT_ASSERT(g_str_has_prefix(input, non_opt));
3101
3102 input += strlen(non_opt);
3103
3104 while (G_IS_DIR_SEPARATOR(*input)) {
3105 input++;
3106 }
3107
3108 if (strlen(input) > 0) {
3109 append_string_parameter_to_args(comp->extra_params,
3110 "trace-name", input);
3111 }
3112 }
3113
3114 status = append_parameter_to_args(comp->extra_params, "inputs", res->inputs);
3115 if (status != 0) {
3116 goto error;
3117 }
3118
3119 g_ptr_array_add(component_args, comp);
3120 comp = NULL;
3121 }
3122
3123 status = 0;
3124 goto end;
3125
3126 error:
3127 status = -1;
3128
3129 end:
3130 g_free(cc_name);
3131
3132 if (comp) {
3133 destroy_implicit_component_args(comp);
3134 }
3135
3136 return status;
3137 }
3138
3139 /*
3140 * As we iterate the arguments to the convert command, this tracks what is the
3141 * type of the current item, to which some contextual options (e.g. --params)
3142 * apply to.
3143 */
3144 enum convert_current_item_type {
3145 /* There is no current item. */
3146 CONVERT_CURRENT_ITEM_TYPE_NONE,
3147
3148 /* Current item is a component. */
3149 CONVERT_CURRENT_ITEM_TYPE_COMPONENT,
3150
3151 /* Current item is a non-option argument. */
3152 CONVERT_CURRENT_ITEM_TYPE_NON_OPT,
3153 };
3154
3155 /*
3156 * Creates a Babeltrace config object from the arguments of a convert
3157 * command.
3158 *
3159 * *retcode is set to the appropriate exit code to use.
3160 */
3161 static
3162 struct bt_config *bt_config_convert_from_args(int argc, const char *argv[],
3163 int *retcode, const bt_value *plugin_paths,
3164 int *default_log_level, const bt_interrupter *interrupter)
3165 {
3166 enum convert_current_item_type current_item_type =
3167 CONVERT_CURRENT_ITEM_TYPE_NONE;
3168 int ret = 0;
3169 struct bt_config *cfg = NULL;
3170 bool got_input_format_opt = false;
3171 bool got_output_format_opt = false;
3172 bool trimmer_has_begin = false;
3173 bool trimmer_has_end = false;
3174 bool stream_intersection_mode = false;
3175 bool print_run_args = false;
3176 bool print_run_args_0 = false;
3177 bool print_ctf_metadata = false;
3178 bt_value *run_args = NULL;
3179 bt_value *all_names = NULL;
3180 GList *source_names = NULL;
3181 GList *filter_names = NULL;
3182 GList *sink_names = NULL;
3183 bt_value *non_opts = NULL;
3184 bt_value *non_opt_params = NULL;
3185 bt_value *non_opt_loglevels = NULL;
3186 struct implicit_component_args implicit_ctf_output_args = { 0 };
3187 struct implicit_component_args implicit_lttng_live_args = { 0 };
3188 struct implicit_component_args implicit_dummy_args = { 0 };
3189 struct implicit_component_args implicit_text_args = { 0 };
3190 struct implicit_component_args implicit_debug_info_args = { 0 };
3191 struct implicit_component_args implicit_muxer_args = { 0 };
3192 struct implicit_component_args implicit_trimmer_args = { 0 };
3193 char error_buf[256] = { 0 };
3194 size_t i;
3195 struct bt_common_lttng_live_url_parts lttng_live_url_parts = { 0 };
3196 char *output = NULL;
3197 struct auto_source_discovery auto_disc = { NULL };
3198 GString *auto_disc_comp_name = NULL;
3199 struct bt_argpar_parse_ret argpar_parse_ret = { 0 };
3200 GString *name_gstr = NULL;
3201 GString *component_arg_for_run = NULL;
3202 bt_value *live_inputs_array_val = NULL;
3203
3204 /*
3205 * Array of `struct implicit_component_args *` created for the sources
3206 * we have auto-discovered.
3207 */
3208 GPtrArray *discovered_source_args = NULL;
3209
3210 /*
3211 * If set, restrict automatic source discovery to this component class
3212 * of this plugin.
3213 */
3214 const char *auto_source_discovery_restrict_plugin_name = NULL;
3215 const char *auto_source_discovery_restrict_component_class_name = NULL;
3216
3217 bool ctf_fs_source_force_clock_class_unix_epoch_origin = false;
3218 gchar *ctf_fs_source_clock_class_offset_arg = NULL;
3219 gchar *ctf_fs_source_clock_class_offset_ns_arg = NULL;
3220 *retcode = 0;
3221
3222 if (argc < 1) {
3223 print_convert_usage(stdout);
3224 *retcode = -1;
3225 goto end;
3226 }
3227
3228 if (init_implicit_component_args(&implicit_ctf_output_args,
3229 "sink.ctf.fs", false)) {
3230 goto error;
3231 }
3232
3233 if (init_implicit_component_args(&implicit_lttng_live_args,
3234 "source.ctf.lttng-live", false)) {
3235 goto error;
3236 }
3237
3238 if (init_implicit_component_args(&implicit_text_args,
3239 "sink.text.pretty", false)) {
3240 goto error;
3241 }
3242
3243 if (init_implicit_component_args(&implicit_dummy_args,
3244 "sink.utils.dummy", false)) {
3245 goto error;
3246 }
3247
3248 if (init_implicit_component_args(&implicit_debug_info_args,
3249 "filter.lttng-utils.debug-info", false)) {
3250 goto error;
3251 }
3252
3253 if (init_implicit_component_args(&implicit_muxer_args,
3254 "filter.utils.muxer", true)) {
3255 goto error;
3256 }
3257
3258 if (init_implicit_component_args(&implicit_trimmer_args,
3259 "filter.utils.trimmer", false)) {
3260 goto error;
3261 }
3262
3263 all_names = bt_value_map_create();
3264 if (!all_names) {
3265 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3266 goto error;
3267 }
3268
3269 run_args = bt_value_array_create();
3270 if (!run_args) {
3271 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3272 goto error;
3273 }
3274
3275 component_arg_for_run = g_string_new(NULL);
3276 if (!component_arg_for_run) {
3277 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3278 goto error;
3279 }
3280
3281 non_opts = bt_value_array_create();
3282 if (!non_opts) {
3283 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3284 goto error;
3285 }
3286
3287 non_opt_params = bt_value_array_create();
3288 if (!non_opt_params) {
3289 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3290 goto error;
3291 }
3292
3293 non_opt_loglevels = bt_value_array_create();
3294 if (!non_opt_loglevels) {
3295 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3296 goto error;
3297 }
3298
3299 if (auto_source_discovery_init(&auto_disc) != 0) {
3300 goto error;
3301 }
3302
3303 discovered_source_args =
3304 g_ptr_array_new_with_free_func((GDestroyNotify) destroy_implicit_component_args);
3305 if (!discovered_source_args) {
3306 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3307 goto error;
3308 }
3309
3310 auto_disc_comp_name = g_string_new(NULL);
3311 if (!auto_disc_comp_name) {
3312 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3313 goto error;
3314 }
3315
3316 /*
3317 * First pass: collect all arguments which need to be passed
3318 * as is to the run command. This pass can also add --name
3319 * arguments if needed to automatically name unnamed component
3320 * instances.
3321 */
3322 argpar_parse_ret = bt_argpar_parse(argc, argv, convert_options, true);
3323 if (argpar_parse_ret.error) {
3324 BT_CLI_LOGE_APPEND_CAUSE(
3325 "While parsing `convert` command's command-line arguments: %s",
3326 argpar_parse_ret.error->str);
3327 goto error;
3328 }
3329
3330 if (help_option_is_specified(&argpar_parse_ret)) {
3331 print_convert_usage(stdout);
3332 *retcode = -1;
3333 BT_OBJECT_PUT_REF_AND_RESET(cfg);
3334 goto end;
3335 }
3336
3337 for (i = 0; i < argpar_parse_ret.items->len; i++) {
3338 struct bt_argpar_item *argpar_item =
3339 g_ptr_array_index(argpar_parse_ret.items, i);
3340 struct bt_argpar_item_opt *argpar_item_opt;
3341 char *name = NULL;
3342 char *plugin_name = NULL;
3343 char *comp_cls_name = NULL;
3344 const char *arg;
3345
3346 if (argpar_item->type == BT_ARGPAR_ITEM_TYPE_OPT) {
3347 argpar_item_opt = (struct bt_argpar_item_opt *) argpar_item;
3348 arg = argpar_item_opt->arg;
3349
3350 switch (argpar_item_opt->descr->id) {
3351 case OPT_COMPONENT:
3352 {
3353 bt_component_class_type type;
3354
3355 current_item_type = CONVERT_CURRENT_ITEM_TYPE_COMPONENT;
3356
3357 /* Parse the argument */
3358 plugin_comp_cls_names(arg, &name, &plugin_name,
3359 &comp_cls_name, &type);
3360 if (!plugin_name || !comp_cls_name) {
3361 BT_CLI_LOGE_APPEND_CAUSE(
3362 "Invalid format for --component option's argument:\n %s",
3363 arg);
3364 goto error;
3365 }
3366
3367 if (name) {
3368 /*
3369 * Name was given by the user, verify it isn't
3370 * taken.
3371 */
3372 if (bt_value_map_has_entry(all_names, name)) {
3373 BT_CLI_LOGE_APPEND_CAUSE(
3374 "Duplicate component instance name:\n %s",
3375 name);
3376 goto error;
3377 }
3378
3379 name_gstr = g_string_new(name);
3380 if (!name_gstr) {
3381 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3382 goto error;
3383 }
3384
3385 g_string_assign(component_arg_for_run, arg);
3386 } else {
3387 /* Name not given by user, generate one. */
3388 name_gstr = get_component_auto_name(arg, all_names);
3389 if (!name_gstr) {
3390 goto error;
3391 }
3392
3393 g_string_printf(component_arg_for_run, "%s:%s",
3394 name_gstr->str, arg);
3395 }
3396
3397 if (bt_value_array_append_string_element(run_args,
3398 "--component")) {
3399 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3400 goto error;
3401 }
3402
3403 if (bt_value_array_append_string_element(run_args,
3404 component_arg_for_run->str)) {
3405 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3406 goto error;
3407 }
3408
3409 /*
3410 * Remember this name globally, for the uniqueness of
3411 * all component names.
3412 */
3413 if (bt_value_map_insert_entry(all_names,
3414 name_gstr->str, bt_value_null)) {
3415 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3416 goto error;
3417 }
3418
3419 /*
3420 * Remember this name specifically for the type of the
3421 * component. This is to create connection arguments.
3422 *
3423 * The list takes ownership of `name_gstr`.
3424 */
3425 switch (type) {
3426 case BT_COMPONENT_CLASS_TYPE_SOURCE:
3427 source_names = g_list_append(source_names, name_gstr);
3428 break;
3429 case BT_COMPONENT_CLASS_TYPE_FILTER:
3430 filter_names = g_list_append(filter_names, name_gstr);
3431 break;
3432 case BT_COMPONENT_CLASS_TYPE_SINK:
3433 sink_names = g_list_append(sink_names, name_gstr);
3434 break;
3435 default:
3436 abort();
3437 }
3438 name_gstr = NULL;
3439
3440 free(name);
3441 free(plugin_name);
3442 free(comp_cls_name);
3443 name = NULL;
3444 plugin_name = NULL;
3445 comp_cls_name = NULL;
3446 break;
3447 }
3448 case OPT_PARAMS:
3449 if (current_item_type == CONVERT_CURRENT_ITEM_TYPE_COMPONENT) {
3450 /*
3451 * The current item is a component (--component option),
3452 * pass it directly to the run args.
3453 */
3454 if (bt_value_array_append_string_element(run_args,
3455 "--params")) {
3456 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3457 goto error;
3458 }
3459
3460 if (bt_value_array_append_string_element(run_args, arg)) {
3461 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3462 goto error;
3463 }
3464 } else if (current_item_type == CONVERT_CURRENT_ITEM_TYPE_NON_OPT) {
3465 /*
3466 * The current item is a
3467 * non-option argument, record
3468 * it in `non_opt_params`.
3469 */
3470 bt_value *array;
3471 bt_value_array_append_element_status append_element_status;
3472 uint64_t idx = bt_value_array_get_length(non_opt_params) - 1;
3473
3474 array = bt_value_array_borrow_element_by_index(non_opt_params, idx);
3475
3476 append_element_status = bt_value_array_append_string_element(array, arg);
3477 if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
3478 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3479 goto error;
3480 }
3481 } else {
3482 BT_CLI_LOGE_APPEND_CAUSE(
3483 "No current component (--component option) or non-option argument of which to set parameters:\n %s",
3484 arg);
3485 goto error;
3486 }
3487 break;
3488 case OPT_LOG_LEVEL:
3489 if (current_item_type == CONVERT_CURRENT_ITEM_TYPE_COMPONENT) {
3490 if (bt_value_array_append_string_element(run_args, "--log-level")) {
3491 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3492 goto error;
3493 }
3494
3495 if (bt_value_array_append_string_element(run_args, arg)) {
3496 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3497 goto error;
3498 }
3499 } else if (current_item_type == CONVERT_CURRENT_ITEM_TYPE_NON_OPT) {
3500 uint64_t idx = bt_value_array_get_length(non_opt_loglevels) - 1;
3501 bt_value *log_level_str_value;
3502
3503 log_level_str_value = bt_value_string_create_init(arg);
3504 if (!log_level_str_value) {
3505 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3506 goto error;
3507 }
3508
3509 if (bt_value_array_set_element_by_index(non_opt_loglevels, idx,
3510 log_level_str_value)) {
3511 bt_value_put_ref(log_level_str_value);
3512 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3513 goto error;
3514 }
3515 } else {
3516 BT_CLI_LOGE_APPEND_CAUSE(
3517 "No current component (--component option) or non-option argument to assign a log level to:\n %s",
3518 arg);
3519 goto error;
3520 }
3521
3522 break;
3523 case OPT_RETRY_DURATION:
3524 if (bt_value_array_append_string_element(run_args,
3525 "--retry-duration")) {
3526 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3527 goto error;
3528 }
3529
3530 if (bt_value_array_append_string_element(run_args, arg)) {
3531 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3532 goto error;
3533 }
3534 break;
3535 case OPT_BEGIN:
3536 case OPT_CLOCK_CYCLES:
3537 case OPT_CLOCK_DATE:
3538 case OPT_CLOCK_FORCE_CORRELATE:
3539 case OPT_CLOCK_GMT:
3540 case OPT_CLOCK_OFFSET:
3541 case OPT_CLOCK_OFFSET_NS:
3542 case OPT_CLOCK_SECONDS:
3543 case OPT_COLOR:
3544 case OPT_DEBUG:
3545 case OPT_DEBUG_INFO:
3546 case OPT_DEBUG_INFO_DIR:
3547 case OPT_DEBUG_INFO_FULL_PATH:
3548 case OPT_DEBUG_INFO_TARGET_PREFIX:
3549 case OPT_END:
3550 case OPT_FIELDS:
3551 case OPT_INPUT_FORMAT:
3552 case OPT_NAMES:
3553 case OPT_NO_DELTA:
3554 case OPT_OUTPUT_FORMAT:
3555 case OPT_OUTPUT:
3556 case OPT_RUN_ARGS:
3557 case OPT_RUN_ARGS_0:
3558 case OPT_STREAM_INTERSECTION:
3559 case OPT_TIMERANGE:
3560 case OPT_VERBOSE:
3561 /* Ignore in this pass */
3562 break;
3563 default:
3564 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
3565 argpar_item_opt->descr->id);
3566 goto error;
3567 }
3568 } else if (argpar_item->type == BT_ARGPAR_ITEM_TYPE_NON_OPT) {
3569 struct bt_argpar_item_non_opt *argpar_item_non_opt;
3570 bt_value_array_append_element_status append_status;
3571
3572 current_item_type = CONVERT_CURRENT_ITEM_TYPE_NON_OPT;
3573
3574 argpar_item_non_opt = (struct bt_argpar_item_non_opt *) argpar_item;
3575
3576 append_status = bt_value_array_append_string_element(non_opts,
3577 argpar_item_non_opt->arg);
3578 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
3579 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3580 goto error;
3581 }
3582
3583 append_status = bt_value_array_append_empty_array_element(
3584 non_opt_params, NULL);
3585 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
3586 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3587 goto error;
3588 }
3589
3590 append_status = bt_value_array_append_element(non_opt_loglevels, bt_value_null);
3591 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
3592 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3593 goto error;
3594 }
3595 } else {
3596 abort();
3597 }
3598 }
3599
3600 /*
3601 * Second pass: transform the convert-specific options and
3602 * arguments into implicit component instances for the run
3603 * command.
3604 */
3605 for (i = 0; i < argpar_parse_ret.items->len; i++) {
3606 struct bt_argpar_item *argpar_item =
3607 g_ptr_array_index(argpar_parse_ret.items, i);
3608 struct bt_argpar_item_opt *argpar_item_opt;
3609 const char *arg;
3610
3611 if (argpar_item->type != BT_ARGPAR_ITEM_TYPE_OPT) {
3612 continue;
3613 }
3614
3615 argpar_item_opt = (struct bt_argpar_item_opt *) argpar_item;
3616 arg = argpar_item_opt->arg;
3617
3618 switch (argpar_item_opt->descr->id) {
3619 case OPT_BEGIN:
3620 if (trimmer_has_begin) {
3621 printf("At --begin option: --begin or --timerange option already specified\n %s\n",
3622 arg);
3623 goto error;
3624 }
3625
3626 trimmer_has_begin = true;
3627 ret = append_implicit_component_extra_param(
3628 &implicit_trimmer_args, "begin", arg);
3629 implicit_trimmer_args.exists = true;
3630 if (ret) {
3631 goto error;
3632 }
3633 break;
3634 case OPT_END:
3635 if (trimmer_has_end) {
3636 printf("At --end option: --end or --timerange option already specified\n %s\n",
3637 arg);
3638 goto error;
3639 }
3640
3641 trimmer_has_end = true;
3642 ret = append_implicit_component_extra_param(
3643 &implicit_trimmer_args, "end", arg);
3644 implicit_trimmer_args.exists = true;
3645 if (ret) {
3646 goto error;
3647 }
3648 break;
3649 case OPT_TIMERANGE:
3650 {
3651 char *begin;
3652 char *end;
3653
3654 if (trimmer_has_begin || trimmer_has_end) {
3655 printf("At --timerange option: --begin, --end, or --timerange option already specified\n %s\n",
3656 arg);
3657 goto error;
3658 }
3659
3660 ret = split_timerange(arg, &begin, &end);
3661 if (ret) {
3662 BT_CLI_LOGE_APPEND_CAUSE("Invalid --timerange option's argument: expecting BEGIN,END or [BEGIN,END]:\n %s",
3663 arg);
3664 goto error;
3665 }
3666
3667 ret = append_implicit_component_extra_param(
3668 &implicit_trimmer_args, "begin", begin);
3669 ret |= append_implicit_component_extra_param(
3670 &implicit_trimmer_args, "end", end);
3671 implicit_trimmer_args.exists = true;
3672 free(begin);
3673 free(end);
3674 if (ret) {
3675 goto error;
3676 }
3677 break;
3678 }
3679 case OPT_CLOCK_CYCLES:
3680 append_implicit_component_param(
3681 &implicit_text_args, "clock-cycles", "yes");
3682 implicit_text_args.exists = true;
3683 break;
3684 case OPT_CLOCK_DATE:
3685 append_implicit_component_param(
3686 &implicit_text_args, "clock-date", "yes");
3687 implicit_text_args.exists = true;
3688 break;
3689 case OPT_CLOCK_FORCE_CORRELATE:
3690 ctf_fs_source_force_clock_class_unix_epoch_origin = true;
3691 break;
3692 case OPT_CLOCK_GMT:
3693 append_implicit_component_param(
3694 &implicit_text_args, "clock-gmt", "yes");
3695 append_implicit_component_param(
3696 &implicit_trimmer_args, "gmt", "yes");
3697 implicit_text_args.exists = true;
3698 break;
3699 case OPT_CLOCK_OFFSET:
3700 if (ctf_fs_source_clock_class_offset_arg) {
3701 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --clock-offset option\n");
3702 goto error;
3703 }
3704
3705 ctf_fs_source_clock_class_offset_arg = g_strdup(arg);
3706 if (!ctf_fs_source_clock_class_offset_arg) {
3707 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3708 goto error;
3709 }
3710 break;
3711 case OPT_CLOCK_OFFSET_NS:
3712 if (ctf_fs_source_clock_class_offset_ns_arg) {
3713 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --clock-offset-ns option\n");
3714 goto error;
3715 }
3716
3717 ctf_fs_source_clock_class_offset_ns_arg = g_strdup(arg);
3718 if (!ctf_fs_source_clock_class_offset_ns_arg) {
3719 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3720 goto error;
3721 }
3722 break;
3723 case OPT_CLOCK_SECONDS:
3724 append_implicit_component_param(
3725 &implicit_text_args, "clock-seconds", "yes");
3726 implicit_text_args.exists = true;
3727 break;
3728 case OPT_COLOR:
3729 implicit_text_args.exists = true;
3730 ret = append_implicit_component_extra_param(
3731 &implicit_text_args, "color", arg);
3732 if (ret) {
3733 goto error;
3734 }
3735 break;
3736 case OPT_DEBUG_INFO:
3737 implicit_debug_info_args.exists = true;
3738 break;
3739 case OPT_DEBUG_INFO_DIR:
3740 implicit_debug_info_args.exists = true;
3741 ret = append_implicit_component_extra_param(
3742 &implicit_debug_info_args, "debug-info-dir", arg);
3743 if (ret) {
3744 goto error;
3745 }
3746 break;
3747 case OPT_DEBUG_INFO_FULL_PATH:
3748 implicit_debug_info_args.exists = true;
3749 append_implicit_component_param(
3750 &implicit_debug_info_args, "full-path", "yes");
3751 break;
3752 case OPT_DEBUG_INFO_TARGET_PREFIX:
3753 implicit_debug_info_args.exists = true;
3754 ret = append_implicit_component_extra_param(
3755 &implicit_debug_info_args,
3756 "target-prefix", arg);
3757 if (ret) {
3758 goto error;
3759 }
3760 break;
3761 case OPT_FIELDS:
3762 {
3763 bt_value *fields = fields_from_arg(arg);
3764
3765 if (!fields) {
3766 goto error;
3767 }
3768
3769 implicit_text_args.exists = true;
3770 ret = insert_flat_params_from_array(
3771 implicit_text_args.params_arg,
3772 fields, "field");
3773 bt_value_put_ref(fields);
3774 if (ret) {
3775 goto error;
3776 }
3777 break;
3778 }
3779 case OPT_NAMES:
3780 {
3781 bt_value *names = names_from_arg(arg);
3782
3783 if (!names) {
3784 goto error;
3785 }
3786
3787 implicit_text_args.exists = true;
3788 ret = insert_flat_params_from_array(
3789 implicit_text_args.params_arg,
3790 names, "name");
3791 bt_value_put_ref(names);
3792 if (ret) {
3793 goto error;
3794 }
3795 break;
3796 }
3797 case OPT_NO_DELTA:
3798 append_implicit_component_param(
3799 &implicit_text_args, "no-delta", "yes");
3800 implicit_text_args.exists = true;
3801 break;
3802 case OPT_INPUT_FORMAT:
3803 if (got_input_format_opt) {
3804 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --input-format option.");
3805 goto error;
3806 }
3807
3808 got_input_format_opt = true;
3809
3810 if (strcmp(arg, "ctf") == 0) {
3811 auto_source_discovery_restrict_plugin_name = "ctf";
3812 auto_source_discovery_restrict_component_class_name = "fs";
3813 } else if (strcmp(arg, "lttng-live") == 0) {
3814 auto_source_discovery_restrict_plugin_name = "ctf";
3815 auto_source_discovery_restrict_component_class_name = "lttng-live";
3816 implicit_lttng_live_args.exists = true;
3817 } else {
3818 BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy input format:\n %s",
3819 arg);
3820 goto error;
3821 }
3822 break;
3823 case OPT_OUTPUT_FORMAT:
3824 if (got_output_format_opt) {
3825 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output-format option.");
3826 goto error;
3827 }
3828
3829 got_output_format_opt = true;
3830
3831 if (strcmp(arg, "text") == 0) {
3832 implicit_text_args.exists = true;
3833 } else if (strcmp(arg, "ctf") == 0) {
3834 implicit_ctf_output_args.exists = true;
3835 } else if (strcmp(arg, "dummy") == 0) {
3836 implicit_dummy_args.exists = true;
3837 } else if (strcmp(arg, "ctf-metadata") == 0) {
3838 print_ctf_metadata = true;
3839 } else {
3840 BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy output format:\n %s",
3841 arg);
3842 goto error;
3843 }
3844 break;
3845 case OPT_OUTPUT:
3846 if (output) {
3847 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output option");
3848 goto error;
3849 }
3850
3851 output = strdup(arg);
3852 if (!output) {
3853 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3854 goto error;
3855 }
3856 break;
3857 case OPT_RUN_ARGS:
3858 if (print_run_args_0) {
3859 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0.");
3860 goto error;
3861 }
3862
3863 print_run_args = true;
3864 break;
3865 case OPT_RUN_ARGS_0:
3866 if (print_run_args) {
3867 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0.");
3868 goto error;
3869 }
3870
3871 print_run_args_0 = true;
3872 break;
3873 case OPT_STREAM_INTERSECTION:
3874 /*
3875 * Applies to all traces implementing the
3876 * babeltrace.trace-infos query.
3877 */
3878 stream_intersection_mode = true;
3879 break;
3880 case OPT_VERBOSE:
3881 *default_log_level =
3882 logging_level_min(*default_log_level, BT_LOG_INFO);
3883 break;
3884 case OPT_DEBUG:
3885 *default_log_level =
3886 logging_level_min(*default_log_level, BT_LOG_TRACE);
3887 break;
3888 default:
3889 break;
3890 }
3891 }
3892
3893 set_auto_log_levels(default_log_level);
3894
3895 /*
3896 * Legacy behaviour: --verbose used to make the `text` output
3897 * format print more information. --verbose is now equivalent to
3898 * the INFO log level, which is why we compare to `BT_LOG_INFO`
3899 * here.
3900 */
3901 if (*default_log_level == BT_LOG_INFO) {
3902 append_implicit_component_param(&implicit_text_args,
3903 "verbose", "yes");
3904 }
3905
3906 /* Print CTF metadata or print LTTng live sessions */
3907 if (print_ctf_metadata) {
3908 const bt_value *bt_val_non_opt;
3909
3910 if (bt_value_array_is_empty(non_opts)) {
3911 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf-metadata specified without a path.");
3912 goto error;
3913 }
3914
3915 if (bt_value_array_get_length(non_opts) > 1) {
3916 BT_CLI_LOGE_APPEND_CAUSE("Too many paths specified for --output-format=ctf-metadata.");
3917 goto error;
3918 }
3919
3920 cfg = bt_config_print_ctf_metadata_create(plugin_paths);
3921 if (!cfg) {
3922 goto error;
3923 }
3924
3925 bt_val_non_opt = bt_value_array_borrow_element_by_index_const(non_opts, 0);
3926 g_string_assign(cfg->cmd_data.print_ctf_metadata.path,
3927 bt_value_string_get(bt_val_non_opt));
3928
3929 if (output) {
3930 g_string_assign(
3931 cfg->cmd_data.print_ctf_metadata.output_path,
3932 output);
3933 }
3934
3935 goto end;
3936 }
3937
3938 /*
3939 * If -o ctf was specified, make sure an output path (--output)
3940 * was also specified. --output does not imply -o ctf because
3941 * it's also used for the default, implicit -o text if -o ctf
3942 * is not specified.
3943 */
3944 if (implicit_ctf_output_args.exists) {
3945 if (!output) {
3946 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf specified without --output (trace output path).");
3947 goto error;
3948 }
3949
3950 /*
3951 * At this point we know that -o ctf AND --output were
3952 * specified. Make sure that no options were specified
3953 * which would imply -o text because --output would be
3954 * ambiguous in this case. For example, this is wrong:
3955 *
3956 * babeltrace2 --names=all -o ctf --output=/tmp/path my-trace
3957 *
3958 * because --names=all implies -o text, and --output
3959 * could apply to both the sink.text.pretty and
3960 * sink.ctf.fs implicit components.
3961 */
3962 if (implicit_text_args.exists) {
3963 BT_CLI_LOGE_APPEND_CAUSE("Ambiguous --output option: --output-format=ctf specified but another option implies --output-format=text.");
3964 goto error;
3965 }
3966 }
3967
3968 /*
3969 * If -o dummy and -o ctf were not specified, and if there are
3970 * no explicit sink components, then use an implicit
3971 * `sink.text.pretty` component.
3972 */
3973 if (!implicit_dummy_args.exists && !implicit_ctf_output_args.exists &&
3974 !sink_names) {
3975 implicit_text_args.exists = true;
3976 }
3977
3978 /*
3979 * Set implicit `sink.text.pretty` or `sink.ctf.fs` component's
3980 * `path` parameter if --output was specified.
3981 */
3982 if (output) {
3983 if (implicit_text_args.exists) {
3984 append_implicit_component_extra_param(&implicit_text_args,
3985 "path", output);
3986 } else if (implicit_ctf_output_args.exists) {
3987 append_implicit_component_extra_param(&implicit_ctf_output_args,
3988 "path", output);
3989 }
3990 }
3991
3992 /* Decide where the non-option argument(s) go */
3993 if (bt_value_array_get_length(non_opts) > 0) {
3994 if (implicit_lttng_live_args.exists) {
3995 const bt_value *bt_val_non_opt;
3996
3997 if (bt_value_array_get_length(non_opts) > 1) {
3998 BT_CLI_LOGE_APPEND_CAUSE("Too many URLs specified for --input-format=lttng-live.");
3999 goto error;
4000 }
4001
4002 bt_val_non_opt = bt_value_array_borrow_element_by_index_const(non_opts, 0);
4003 lttng_live_url_parts =
4004 bt_common_parse_lttng_live_url(bt_value_string_get(bt_val_non_opt),
4005 error_buf, sizeof(error_buf));
4006 if (!lttng_live_url_parts.proto) {
4007 BT_CLI_LOGE_APPEND_CAUSE("Invalid LTTng live URL format: %s.",
4008 error_buf);
4009 goto error;
4010 }
4011
4012 if (!lttng_live_url_parts.session_name) {
4013 /* Print LTTng live sessions */
4014 cfg = bt_config_print_lttng_live_sessions_create(
4015 plugin_paths);
4016 if (!cfg) {
4017 goto error;
4018 }
4019
4020 g_string_assign(cfg->cmd_data.print_lttng_live_sessions.url,
4021 bt_value_string_get(bt_val_non_opt));
4022
4023 if (output) {
4024 g_string_assign(
4025 cfg->cmd_data.print_lttng_live_sessions.output_path,
4026 output);
4027 }
4028
4029 goto end;
4030 }
4031
4032 live_inputs_array_val = bt_value_array_create();
4033 if (!live_inputs_array_val) {
4034 BT_CLI_LOGE_APPEND_CAUSE_OOM();
4035 goto error;
4036 }
4037
4038 if (bt_value_array_append_string_element(
4039 live_inputs_array_val,
4040 bt_value_string_get(bt_val_non_opt))) {
4041 BT_CLI_LOGE_APPEND_CAUSE_OOM();
4042 goto error;
4043 }
4044
4045 ret = append_parameter_to_args(
4046 implicit_lttng_live_args.extra_params,
4047 "inputs", live_inputs_array_val);
4048 if (ret) {
4049 goto error;
4050 }
4051
4052 ret = append_implicit_component_extra_param(
4053 &implicit_lttng_live_args,
4054 "session-not-found-action", "end");
4055 if (ret) {
4056 goto error;
4057 }
4058 } else {
4059 int status;
4060 size_t plugin_count;
4061 const bt_plugin **plugins;
4062 const bt_plugin *plugin;
4063
4064 status = require_loaded_plugins(plugin_paths);
4065 if (status != 0) {
4066 goto error;
4067 }
4068
4069 if (auto_source_discovery_restrict_plugin_name) {
4070 plugin_count = 1;
4071 plugin = find_loaded_plugin(auto_source_discovery_restrict_plugin_name);
4072 plugins = &plugin;
4073 } else {
4074 plugin_count = get_loaded_plugins_count();
4075 plugins = borrow_loaded_plugins();
4076 }
4077
4078 status = auto_discover_source_components(non_opts, plugins, plugin_count,
4079 auto_source_discovery_restrict_component_class_name,
4080 *default_log_level, &auto_disc, interrupter);
4081
4082 if (status != 0) {
4083 if (status == AUTO_SOURCE_DISCOVERY_STATUS_INTERRUPTED) {
4084 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
4085 "Babeltrace CLI", "Automatic source discovery interrupted by the user");
4086 }
4087 goto error;
4088 }
4089
4090 status = create_implicit_component_args_from_auto_discovered_sources(
4091 &auto_disc, non_opts, non_opt_params, non_opt_loglevels,
4092 discovered_source_args);
4093 if (status != 0) {
4094 goto error;
4095 }
4096 }
4097 }
4098
4099
4100 /*
4101 * If --clock-force-correlated was given, apply it to any src.ctf.fs
4102 * component.
4103 */
4104 if (ctf_fs_source_force_clock_class_unix_epoch_origin) {
4105 int n;
4106
4107 n = append_multiple_implicit_components_param(
4108 discovered_source_args, "source.ctf.fs", "force-clock-class-origin-unix-epoch",
4109 "yes");
4110 if (n == 0) {
4111 BT_CLI_LOGE_APPEND_CAUSE("--clock-force-correlate specified, but no source.ctf.fs component instantiated.");
4112 goto error;
4113 }
4114 }
4115
4116 /* If --clock-offset was given, apply it to any src.ctf.fs component. */
4117 if (ctf_fs_source_clock_class_offset_arg) {
4118 int n;
4119
4120 n = append_multiple_implicit_components_param(
4121 discovered_source_args, "source.ctf.fs", "clock-class-offset-s",
4122 ctf_fs_source_clock_class_offset_arg);
4123
4124 if (n == 0) {
4125 BT_CLI_LOGE_APPEND_CAUSE("--clock-offset specified, but no source.ctf.fs component instantiated.");
4126 goto error;
4127 }
4128 }
4129
4130 /* If --clock-offset-ns was given, apply it to any src.ctf.fs component. */
4131 if (ctf_fs_source_clock_class_offset_ns_arg) {
4132 int n;
4133
4134 n = append_multiple_implicit_components_param(
4135 discovered_source_args, "source.ctf.fs", "clock-class-offset-ns",
4136 ctf_fs_source_clock_class_offset_ns_arg);
4137
4138 if (n == 0) {
4139 BT_CLI_LOGE_APPEND_CAUSE("--clock-offset-ns specified, but no source.ctf.fs component instantiated.");
4140 goto error;
4141 }
4142 }
4143
4144 /*
4145 * If the implicit `source.ctf.lttng-live` component exists,
4146 * make sure there's at least one non-option argument (which is
4147 * the URL).
4148 */
4149 if (implicit_lttng_live_args.exists && bt_value_array_is_empty(non_opts)) {
4150 BT_CLI_LOGE_APPEND_CAUSE("Missing URL for implicit `%s` component.",
4151 implicit_lttng_live_args.comp_arg->str);
4152 goto error;
4153 }
4154
4155 /* Assign names to implicit components */
4156 for (i = 0; i < discovered_source_args->len; i++) {
4157 struct implicit_component_args *args;
4158 int j;
4159
4160 args = discovered_source_args->pdata[i];
4161
4162 g_string_printf(auto_disc_comp_name, "auto-disc-%s", args->comp_arg->str);
4163
4164 /* Give it a name like `auto-disc-src-ctf-fs`. */
4165 for (j = 0; j < auto_disc_comp_name->len; j++) {
4166 if (auto_disc_comp_name->str[j] == '.') {
4167 auto_disc_comp_name->str[j] = '-';
4168 }
4169 }
4170
4171 ret = assign_name_to_implicit_component(args,
4172 auto_disc_comp_name->str, all_names, &source_names, true);
4173 if (ret) {
4174 goto error;
4175 }
4176 }
4177
4178 ret = assign_name_to_implicit_component(&implicit_lttng_live_args,
4179 "lttng-live", all_names, &source_names, true);
4180 if (ret) {
4181 goto error;
4182 }
4183
4184 ret = assign_name_to_implicit_component(&implicit_text_args,
4185 "pretty", all_names, &sink_names, true);
4186 if (ret) {
4187 goto error;
4188 }
4189
4190 ret = assign_name_to_implicit_component(&implicit_ctf_output_args,
4191 "sink-ctf-fs", all_names, &sink_names, true);
4192 if (ret) {
4193 goto error;
4194 }
4195
4196 ret = assign_name_to_implicit_component(&implicit_dummy_args,
4197 "dummy", all_names, &sink_names, true);
4198 if (ret) {
4199 goto error;
4200 }
4201
4202 ret = assign_name_to_implicit_component(&implicit_muxer_args,
4203 "muxer", all_names, NULL, false);
4204 if (ret) {
4205 goto error;
4206 }
4207
4208 ret = assign_name_to_implicit_component(&implicit_trimmer_args,
4209 "trimmer", all_names, NULL, false);
4210 if (ret) {
4211 goto error;
4212 }
4213
4214 ret = assign_name_to_implicit_component(&implicit_debug_info_args,
4215 "debug-info", all_names, NULL, false);
4216 if (ret) {
4217 goto error;
4218 }
4219
4220 /* Make sure there's at least one source and one sink */
4221 if (!source_names) {
4222 BT_CLI_LOGE_APPEND_CAUSE("No source component.");
4223 goto error;
4224 }
4225
4226 if (!sink_names) {
4227 BT_CLI_LOGE_APPEND_CAUSE("No sink component.");
4228 goto error;
4229 }
4230
4231 /* Make sure there's a single sink component */
4232 if (g_list_length(sink_names) != 1) {
4233 BT_CLI_LOGE_APPEND_CAUSE(
4234 "More than one sink component specified.");
4235 goto error;
4236 }
4237
4238 /*
4239 * Prepend the muxer, the trimmer, and the debug info to the
4240 * filter chain so that we have:
4241 *
4242 * sources -> muxer -> [trimmer] -> [debug info] ->
4243 * [user filters] -> sinks
4244 */
4245 if (implicit_debug_info_args.exists) {
4246 if (g_list_prepend_gstring(&filter_names,
4247 implicit_debug_info_args.name_arg->str)) {
4248 goto error;
4249 }
4250 }
4251
4252 if (implicit_trimmer_args.exists) {
4253 if (g_list_prepend_gstring(&filter_names,
4254 implicit_trimmer_args.name_arg->str)) {
4255 goto error;
4256 }
4257 }
4258
4259 if (g_list_prepend_gstring(&filter_names,
4260 implicit_muxer_args.name_arg->str)) {
4261 goto error;
4262 }
4263
4264 /*
4265 * Append the equivalent run arguments for the implicit
4266 * components.
4267 */
4268 for (i = 0; i < discovered_source_args->len; i++) {
4269 struct implicit_component_args *args =
4270 discovered_source_args->pdata[i];
4271
4272 ret = append_run_args_for_implicit_component(args, run_args);
4273 if (ret) {
4274 goto error;
4275 }
4276 }
4277
4278 ret = append_run_args_for_implicit_component(&implicit_lttng_live_args,
4279 run_args);
4280 if (ret) {
4281 goto error;
4282 }
4283
4284 ret = append_run_args_for_implicit_component(&implicit_text_args,
4285 run_args);
4286 if (ret) {
4287 goto error;
4288 }
4289
4290 ret = append_run_args_for_implicit_component(&implicit_ctf_output_args,
4291 run_args);
4292 if (ret) {
4293 goto error;
4294 }
4295
4296 ret = append_run_args_for_implicit_component(&implicit_dummy_args,
4297 run_args);
4298 if (ret) {
4299 goto error;
4300 }
4301
4302 ret = append_run_args_for_implicit_component(&implicit_muxer_args,
4303 run_args);
4304 if (ret) {
4305 goto error;
4306 }
4307
4308 ret = append_run_args_for_implicit_component(&implicit_trimmer_args,
4309 run_args);
4310 if (ret) {
4311 goto error;
4312 }
4313
4314 ret = append_run_args_for_implicit_component(&implicit_debug_info_args,
4315 run_args);
4316 if (ret) {
4317 goto error;
4318 }
4319
4320 /* Auto-connect components */
4321 ret = convert_auto_connect(run_args, source_names, filter_names,
4322 sink_names);
4323 if (ret) {
4324 BT_CLI_LOGE_APPEND_CAUSE("Cannot auto-connect components.");
4325 goto error;
4326 }
4327
4328 /*
4329 * We have all the run command arguments now. Depending on
4330 * --run-args, we pass this to the run command or print them
4331 * here.
4332 */
4333 if (print_run_args || print_run_args_0) {
4334 if (stream_intersection_mode) {
4335 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --stream-intersection with --run-args or --run-args-0.");
4336 goto error;
4337 }
4338
4339 for (i = 0; i < bt_value_array_get_length(run_args); i++) {
4340 const bt_value *arg_value =
4341 bt_value_array_borrow_element_by_index(run_args,
4342 i);
4343 const char *arg;
4344 GString *quoted = NULL;
4345 const char *arg_to_print;
4346
4347 BT_ASSERT(arg_value);
4348 arg = bt_value_string_get(arg_value);
4349
4350 if (print_run_args) {
4351 quoted = bt_common_shell_quote(arg, true);
4352 if (!quoted) {
4353 goto error;
4354 }
4355
4356 arg_to_print = quoted->str;
4357 } else {
4358 arg_to_print = arg;
4359 }
4360
4361 printf("%s", arg_to_print);
4362
4363 if (quoted) {
4364 g_string_free(quoted, TRUE);
4365 }
4366
4367 if (i < bt_value_array_get_length(run_args) - 1) {
4368 if (print_run_args) {
4369 putchar(' ');
4370 } else {
4371 putchar('\0');
4372 }
4373 }
4374 }
4375
4376 *retcode = -1;
4377 BT_OBJECT_PUT_REF_AND_RESET(cfg);
4378 goto end;
4379 }
4380
4381 cfg = bt_config_run_from_args_array(run_args, retcode,
4382 plugin_paths, *default_log_level);
4383 if (!cfg) {
4384 goto error;
4385 }
4386
4387 cfg->cmd_data.run.stream_intersection_mode = stream_intersection_mode;
4388 goto end;
4389
4390 error:
4391 *retcode = 1;
4392 BT_OBJECT_PUT_REF_AND_RESET(cfg);
4393
4394 end:
4395 bt_argpar_parse_ret_fini(&argpar_parse_ret);
4396
4397 free(output);
4398
4399 if (component_arg_for_run) {
4400 g_string_free(component_arg_for_run, TRUE);
4401 }
4402
4403 if (name_gstr) {
4404 g_string_free(name_gstr, TRUE);
4405 }
4406
4407 bt_value_put_ref(live_inputs_array_val);
4408 bt_value_put_ref(run_args);
4409 bt_value_put_ref(all_names);
4410 destroy_glist_of_gstring(source_names);
4411 destroy_glist_of_gstring(filter_names);
4412 destroy_glist_of_gstring(sink_names);
4413 bt_value_put_ref(non_opt_params);
4414 bt_value_put_ref(non_opt_loglevels);
4415 bt_value_put_ref(non_opts);
4416 finalize_implicit_component_args(&implicit_ctf_output_args);
4417 finalize_implicit_component_args(&implicit_lttng_live_args);
4418 finalize_implicit_component_args(&implicit_dummy_args);
4419 finalize_implicit_component_args(&implicit_text_args);
4420 finalize_implicit_component_args(&implicit_debug_info_args);
4421 finalize_implicit_component_args(&implicit_muxer_args);
4422 finalize_implicit_component_args(&implicit_trimmer_args);
4423 bt_common_destroy_lttng_live_url_parts(&lttng_live_url_parts);
4424 auto_source_discovery_fini(&auto_disc);
4425
4426 if (discovered_source_args) {
4427 g_ptr_array_free(discovered_source_args, TRUE);
4428 }
4429
4430 g_free(ctf_fs_source_clock_class_offset_arg);
4431 g_free(ctf_fs_source_clock_class_offset_ns_arg);
4432
4433 if (auto_disc_comp_name) {
4434 g_string_free(auto_disc_comp_name, TRUE);
4435 }
4436
4437 return cfg;
4438 }
4439
4440 /*
4441 * Prints the Babeltrace 2.x general usage.
4442 */
4443 static
4444 void print_gen_usage(FILE *fp)
4445 {
4446 fprintf(fp, "Usage: babeltrace2 [GENERAL OPTIONS] [COMMAND] [COMMAND ARGUMENTS]\n");
4447 fprintf(fp, "\n");
4448 fprintf(fp, "General options:\n");
4449 fprintf(fp, "\n");
4450 fprintf(fp, " -d, --debug Enable debug mode (same as --log-level=T)\n");
4451 fprintf(fp, " -h, --help Show this help and quit\n");
4452 fprintf(fp, " -l, --log-level=LVL Set the default log level to LVL (`N`, `T`, `D`,\n");
4453 fprintf(fp, " `I`, `W` (default), `E`, or `F`)\n");
4454 fprintf(fp, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
4455 fprintf(fp, " (~/.local/lib/babeltrace2/plugins)\n");
4456 fprintf(fp, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
4457 fprintf(fp, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
4458 fprintf(fp, " dynamic plugins can be loaded\n");
4459 fprintf(fp, " -v, --verbose Enable verbose mode (same as --log-level=I)\n");
4460 fprintf(fp, " -V, --version Show version and quit\n");
4461 fprintf(fp, "\n");
4462 fprintf(fp, "Available commands:\n");
4463 fprintf(fp, "\n");
4464 fprintf(fp, " convert Convert and trim traces (default)\n");
4465 fprintf(fp, " help Get help for a plugin or a component class\n");
4466 fprintf(fp, " list-plugins List available plugins and their content\n");
4467 fprintf(fp, " query Query objects from a component class\n");
4468 fprintf(fp, " run Build a processing graph and run it\n");
4469 fprintf(fp, "\n");
4470 fprintf(fp, "Use `babeltrace2 COMMAND --help` to show the help of COMMAND.\n");
4471 }
4472
4473 struct bt_config *bt_config_cli_args_create(int argc, const char *argv[],
4474 int *retcode, bool omit_system_plugin_path,
4475 bool omit_home_plugin_path,
4476 const bt_value *initial_plugin_paths,
4477 const bt_interrupter *interrupter)
4478 {
4479 struct bt_config *config = NULL;
4480 int i;
4481 int top_level_argc;
4482 const char **top_level_argv;
4483 int command_argc = -1;
4484 const char **command_argv = NULL;
4485 const char *command_name = NULL;
4486 int default_log_level = -1;
4487 struct bt_argpar_parse_ret argpar_parse_ret = { 0 };
4488 bt_value *plugin_paths = NULL;
4489
4490 /* Top-level option descriptions. */
4491 static const struct bt_argpar_opt_descr descrs[] = {
4492 { OPT_DEBUG, 'd', "debug", false },
4493 { OPT_HELP, 'h', "help", false },
4494 { OPT_LOG_LEVEL, 'l', "log-level", true },
4495 { OPT_VERBOSE, 'v', "verbose", false },
4496 { OPT_VERSION, 'V', "version", false},
4497 { OPT_OMIT_HOME_PLUGIN_PATH, '\0', "omit-home-plugin-path", false },
4498 { OPT_OMIT_SYSTEM_PLUGIN_PATH, '\0', "omit-system-plugin-path", false },
4499 { OPT_PLUGIN_PATH, '\0', "plugin-path", true },
4500 BT_ARGPAR_OPT_DESCR_SENTINEL
4501 };
4502
4503 enum command_type {
4504 COMMAND_TYPE_NONE = -1,
4505 COMMAND_TYPE_RUN = 0,
4506 COMMAND_TYPE_CONVERT,
4507 COMMAND_TYPE_LIST_PLUGINS,
4508 COMMAND_TYPE_HELP,
4509 COMMAND_TYPE_QUERY,
4510 } command_type = COMMAND_TYPE_NONE;
4511
4512 *retcode = -1;
4513
4514 if (!initial_plugin_paths) {
4515 plugin_paths = bt_value_array_create();
4516 if (!plugin_paths) {
4517 goto error;
4518 }
4519 } else {
4520 bt_value_copy_status copy_status = bt_value_copy(
4521 initial_plugin_paths, &plugin_paths);
4522 if (copy_status) {
4523 goto error;
4524 }
4525 }
4526
4527 BT_ASSERT(plugin_paths);
4528
4529 /*
4530 * The `BABELTRACE_PLUGIN_PATH` paths take precedence over the
4531 * `--plugin-path` option's paths, so append it now before
4532 * parsing the general options.
4533 */
4534 if (append_env_var_plugin_paths(plugin_paths)) {
4535 goto error;
4536 }
4537
4538 if (argc <= 1) {
4539 print_version();
4540 puts("");
4541 print_gen_usage(stdout);
4542 goto end;
4543 }
4544
4545 /* Skip first argument, the name of the program. */
4546 top_level_argc = argc - 1;
4547 top_level_argv = argv + 1;
4548 argpar_parse_ret = bt_argpar_parse(top_level_argc, top_level_argv,
4549 descrs, false);
4550
4551 if (argpar_parse_ret.error) {
4552 BT_CLI_LOGE_APPEND_CAUSE(
4553 "While parsing command-line arguments: %s",
4554 argpar_parse_ret.error->str);
4555 goto error;
4556 }
4557
4558 for (i = 0; i < argpar_parse_ret.items->len; i++) {
4559 struct bt_argpar_item *item;
4560
4561 item = g_ptr_array_index(argpar_parse_ret.items, i);
4562
4563 if (item->type == BT_ARGPAR_ITEM_TYPE_OPT) {
4564 struct bt_argpar_item_opt *item_opt =
4565 (struct bt_argpar_item_opt *) item;
4566
4567 switch (item_opt->descr->id) {
4568 case OPT_DEBUG:
4569 default_log_level =
4570 logging_level_min(default_log_level, BT_LOG_TRACE);
4571 break;
4572 case OPT_VERBOSE:
4573 default_log_level =
4574 logging_level_min(default_log_level, BT_LOG_INFO);
4575 break;
4576 case OPT_LOG_LEVEL:
4577 {
4578 int level = bt_log_get_level_from_string(item_opt->arg);
4579
4580 if (level < 0) {
4581 BT_CLI_LOGE_APPEND_CAUSE(
4582 "Invalid argument for --log-level option:\n %s",
4583 item_opt->arg);
4584 goto error;
4585 }
4586
4587 default_log_level =
4588 logging_level_min(default_log_level, level);
4589 break;
4590 }
4591 case OPT_PLUGIN_PATH:
4592 if (bt_config_append_plugin_paths_check_setuid_setgid(
4593 plugin_paths, item_opt->arg)) {
4594 goto error;
4595 }
4596 break;
4597 case OPT_OMIT_SYSTEM_PLUGIN_PATH:
4598 omit_system_plugin_path = true;
4599 break;
4600 case OPT_OMIT_HOME_PLUGIN_PATH:
4601 omit_home_plugin_path = true;
4602 break;
4603 case OPT_VERSION:
4604 print_version();
4605 goto end;
4606 case OPT_HELP:
4607 print_gen_usage(stdout);
4608 goto end;
4609 }
4610 } else if (item->type == BT_ARGPAR_ITEM_TYPE_NON_OPT) {
4611 struct bt_argpar_item_non_opt *item_non_opt =
4612 (struct bt_argpar_item_non_opt *) item;
4613 /*
4614 * First unknown argument: is it a known command
4615 * name?
4616 */
4617 command_argc =
4618 top_level_argc - item_non_opt->orig_index - 1;
4619 command_argv =
4620 &top_level_argv[item_non_opt->orig_index + 1];
4621
4622 if (strcmp(item_non_opt->arg, "convert") == 0) {
4623 command_type = COMMAND_TYPE_CONVERT;
4624 } else if (strcmp(item_non_opt->arg, "list-plugins") == 0) {
4625 command_type = COMMAND_TYPE_LIST_PLUGINS;
4626 } else if (strcmp(item_non_opt->arg, "help") == 0) {
4627 command_type = COMMAND_TYPE_HELP;
4628 } else if (strcmp(item_non_opt->arg, "query") == 0) {
4629 command_type = COMMAND_TYPE_QUERY;
4630 } else if (strcmp(item_non_opt->arg, "run") == 0) {
4631 command_type = COMMAND_TYPE_RUN;
4632 } else {
4633 /*
4634 * Non-option argument, but not a known
4635 * command name: assume the default
4636 * `convert` command.
4637 */
4638 command_type = COMMAND_TYPE_CONVERT;
4639 command_name = "convert";
4640 command_argc++;
4641 command_argv--;
4642 }
4643 break;
4644 }
4645 }
4646
4647 if (command_type == COMMAND_TYPE_NONE) {
4648 if (argpar_parse_ret.ingested_orig_args == top_level_argc) {
4649 /*
4650 * We only got non-help, non-version general options
4651 * like --verbose and --debug, without any other
4652 * arguments, so we can't do anything useful: print the
4653 * usage and quit.
4654 */
4655 print_gen_usage(stdout);
4656 goto end;
4657 }
4658
4659 /*
4660 * We stopped on an unknown option argument (and therefore
4661 * didn't see a command name). Assume `convert` command.
4662 */
4663 command_type = COMMAND_TYPE_CONVERT;
4664 command_name = "convert";
4665 command_argc =
4666 top_level_argc - argpar_parse_ret.ingested_orig_args;
4667 command_argv =
4668 &top_level_argv[argpar_parse_ret.ingested_orig_args];
4669 }
4670
4671 BT_ASSERT(command_argv);
4672 BT_ASSERT(command_argc >= 0);
4673
4674 /*
4675 * For all commands other than `convert`, we now know the log level to
4676 * use, so we can apply it with `set_auto_log_levels`.
4677 *
4678 * The convert command has `--debug` and `--verbose` arguments that are
4679 * equivalent to the top-level arguments of the same name. So after it
4680 * has parsed its arguments, `bt_config_convert_from_args` calls
4681 * `set_auto_log_levels` itself.
4682 */
4683 if (command_type != COMMAND_TYPE_CONVERT) {
4684 set_auto_log_levels(&default_log_level);
4685 }
4686
4687 /*
4688 * At this point, `plugin_paths` contains the initial plugin
4689 * paths, the paths from the `BABELTRACE_PLUGIN_PATH` paths, and
4690 * the paths from the `--plugin-path` option.
4691 *
4692 * Now append the user and system plugin paths.
4693 */
4694 if (append_home_and_system_plugin_paths(plugin_paths,
4695 omit_system_plugin_path, omit_home_plugin_path)) {
4696 goto error;
4697 }
4698
4699 switch (command_type) {
4700 case COMMAND_TYPE_RUN:
4701 config = bt_config_run_from_args(command_argc, command_argv,
4702 retcode, plugin_paths,
4703 default_log_level);
4704 break;
4705 case COMMAND_TYPE_CONVERT:
4706 config = bt_config_convert_from_args(command_argc, command_argv,
4707 retcode, plugin_paths, &default_log_level, interrupter);
4708 break;
4709 case COMMAND_TYPE_LIST_PLUGINS:
4710 config = bt_config_list_plugins_from_args(command_argc,
4711 command_argv, retcode, plugin_paths);
4712 break;
4713 case COMMAND_TYPE_HELP:
4714 config = bt_config_help_from_args(command_argc,
4715 command_argv, retcode, plugin_paths,
4716 default_log_level);
4717 break;
4718 case COMMAND_TYPE_QUERY:
4719 config = bt_config_query_from_args(command_argc,
4720 command_argv, retcode, plugin_paths,
4721 default_log_level);
4722 break;
4723 default:
4724 abort();
4725 }
4726
4727 if (config) {
4728 BT_ASSERT(default_log_level >= BT_LOG_TRACE);
4729 config->log_level = default_log_level;
4730 config->command_name = command_name;
4731 }
4732
4733 goto end;
4734
4735 error:
4736 *retcode = 1;
4737
4738 end:
4739 bt_argpar_parse_ret_fini(&argpar_parse_ret);
4740 bt_value_put_ref(plugin_paths);
4741 return config;
4742 }
This page took 0.195116 seconds and 3 git commands to generate.