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