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