cli: do not automatically create an implicit filter.lttng-utils.debug-info comp.
[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
f4f9e43b
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
75a1a799
PP
596 * and *comp_cls are owned by the caller. On success, *name can be NULL
597 * if no component class 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') {
93c1364b 659 printf_err("Missing plugin or component class 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,
8e765000 1308 OPT_DEBUG_INFO,
9009cc24
PP
1309 OPT_DEBUG_INFO_DIR,
1310 OPT_DEBUG_INFO_FULL_PATH,
1311 OPT_DEBUG_INFO_TARGET_PREFIX,
1312 OPT_END,
1313 OPT_FIELDS,
9009cc24
PP
1314 OPT_HELP,
1315 OPT_INPUT_FORMAT,
1316 OPT_KEY,
1317 OPT_LIST,
1318 OPT_NAME,
1319 OPT_NAMES,
9009cc24
PP
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()) {
f4f9e43b 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()) {
f4f9e43b 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()) {
f4f9e43b 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");
75a1a799 1766 fprintf(fp, " babeltrace [GENERAL OPTIONS] help [OPTIONS] TYPE.PLUGIN.CLS\n");
9009cc24
PP
1767 fprintf(fp, "\n");
1768 fprintf(fp, "Options:\n");
1769 fprintf(fp, "\n");
9009cc24
PP
1770 fprintf(fp, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1771 fprintf(fp, " (~/.local/lib/babeltrace/plugins)\n");
1772 fprintf(fp, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1773 fprintf(fp, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1774 fprintf(fp, " dynamic plugins can be loaded\n");
9e503aa9 1775 fprintf(fp, " -h, --help Show this help and quit\n");
9009cc24
PP
1776 fprintf(fp, "\n");
1777 fprintf(fp, "See `babeltrace --help` for the list of general options.\n");
1778 fprintf(fp, "\n");
1779 fprintf(fp, "Use `babeltrace list-plugins` to show the list of available plugins.\n");
1780}
1781
1782static
1783struct poptOption help_long_options[] = {
1784 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
9009cc24
PP
1785 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
1786 { "omit-home-plugin-path", '\0', POPT_ARG_NONE, NULL, OPT_OMIT_HOME_PLUGIN_PATH, NULL, NULL },
1787 { "omit-system-plugin-path", '\0', POPT_ARG_NONE, NULL, OPT_OMIT_SYSTEM_PLUGIN_PATH, NULL, NULL },
1788 { "plugin-path", '\0', POPT_ARG_STRING, NULL, OPT_PLUGIN_PATH, NULL, NULL },
9009cc24
PP
1789 { NULL, 0, '\0', NULL, 0, NULL, NULL },
1790};
1791
1792/*
1793 * Creates a Babeltrace config object from the arguments of a help
1794 * command.
1795 *
1796 * *retcode is set to the appropriate exit code to use.
1797 */
1798static
1799struct bt_config *bt_config_help_from_args(int argc, const char *argv[],
1800 int *retcode, bool force_omit_system_plugin_path,
1801 bool force_omit_home_plugin_path,
1802 struct bt_value *initial_plugin_paths)
1803{
1804 poptContext pc = NULL;
1805 char *arg = NULL;
1806 int opt;
1807 int ret;
1808 struct bt_config *cfg = NULL;
1809 const char *leftover;
1810 char *plugin_name = NULL, *comp_cls_name = NULL;
9009cc24
PP
1811
1812 *retcode = 0;
1813 cfg = bt_config_help_create(initial_plugin_paths);
1814 if (!cfg) {
1815 goto error;
1816 }
1817
1818 cfg->omit_system_plugin_path = force_omit_system_plugin_path;
1819 cfg->omit_home_plugin_path = force_omit_home_plugin_path;
1820 ret = append_env_var_plugin_paths(cfg->plugin_paths);
1821 if (ret) {
1822 goto error;
1823 }
1824
1825 /* Parse options */
1826 pc = poptGetContext(NULL, argc, (const char **) argv,
1827 help_long_options, 0);
1828 if (!pc) {
1829 printf_err("Cannot get popt context\n");
1830 goto error;
1831 }
1832
1833 poptReadDefaultConfig(pc, 0);
1834
1835 while ((opt = poptGetNextOpt(pc)) > 0) {
1836 arg = poptGetOptArg(pc);
1837
1838 switch (opt) {
1839 case OPT_PLUGIN_PATH:
1840 if (bt_config_append_plugin_paths_check_setuid_setgid(
1841 cfg->plugin_paths, arg)) {
1842 goto error;
1843 }
1844 break;
1845 case OPT_OMIT_SYSTEM_PLUGIN_PATH:
1846 cfg->omit_system_plugin_path = true;
1847 break;
1848 case OPT_OMIT_HOME_PLUGIN_PATH:
1849 cfg->omit_home_plugin_path = true;
1850 break;
9009cc24
PP
1851 case OPT_HELP:
1852 print_help_usage(stdout);
1853 *retcode = -1;
1854 BT_PUT(cfg);
1855 goto end;
1856 default:
1857 printf_err("Unknown command-line option specified (option code %d)\n",
1858 opt);
1859 goto error;
1860 }
1861
1862 free(arg);
1863 arg = NULL;
1864 }
1865
1866 /* Check for option parsing error */
1867 if (opt < -1) {
1868 printf_err("While parsing command-line options, at option %s: %s\n",
1869 poptBadOption(pc, 0), poptStrerror(opt));
1870 goto error;
1871 }
1872
1873 leftover = poptGetArg(pc);
1874 if (leftover) {
75a1a799 1875 plugin_comp_cls_names(leftover, NULL,
fd5f8053
PP
1876 &plugin_name, &comp_cls_name,
1877 &cfg->cmd_data.help.cfg_component->type);
9009cc24 1878 if (plugin_name && comp_cls_name) {
75a1a799
PP
1879 /* Component class help */
1880 g_string_assign(
1881 cfg->cmd_data.help.cfg_component->plugin_name,
9009cc24 1882 plugin_name);
75a1a799
PP
1883 g_string_assign(
1884 cfg->cmd_data.help.cfg_component->comp_cls_name,
9009cc24
PP
1885 comp_cls_name);
1886 } else {
75a1a799
PP
1887 /* Fall back to plugin help */
1888 cfg->cmd_data.help.cfg_component->type =
1889 BT_COMPONENT_CLASS_TYPE_UNKNOWN;
1890 g_string_assign(
1891 cfg->cmd_data.help.cfg_component->plugin_name,
1892 leftover);
9009cc24 1893 }
75a1a799
PP
1894 } else {
1895 print_help_usage(stdout);
1896 *retcode = -1;
1897 BT_PUT(cfg);
1898 goto end;
9009cc24
PP
1899 }
1900
1901 if (append_home_and_system_plugin_paths_cfg(cfg)) {
1902 goto error;
1903 }
1904
1905 goto end;
1906
1907error:
1908 *retcode = 1;
1909 BT_PUT(cfg);
1910
1911end:
9009cc24
PP
1912 g_free(plugin_name);
1913 g_free(comp_cls_name);
1914
1915 if (pc) {
1916 poptFreeContext(pc);
1917 }
1918
1919 free(arg);
1920 return cfg;
1921}
1922
1923/*
1924 * Prints the help command usage.
1925 */
1926static
1927void print_query_usage(FILE *fp)
1928{
93c1364b 1929 fprintf(fp, "Usage: babeltrace [GEN OPTS] query [OPTS] TYPE.PLUGIN.CLS OBJECT\n");
9009cc24
PP
1930 fprintf(fp, "\n");
1931 fprintf(fp, "Options:\n");
1932 fprintf(fp, "\n");
9009cc24
PP
1933 fprintf(fp, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1934 fprintf(fp, " (~/.local/lib/babeltrace/plugins)\n");
1935 fprintf(fp, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1936 fprintf(fp, " -p, --params=PARAMS Set the query parameters to PARAMS\n");
1937 fprintf(fp, " (see the expected format of PARAMS below)\n");
1938 fprintf(fp, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1939 fprintf(fp, " dynamic plugins can be loaded\n");
9e503aa9 1940 fprintf(fp, " -h, --help Show this help and quit\n");
9009cc24
PP
1941 fprintf(fp, "\n\n");
1942 print_expected_params_format(fp);
1943}
1944
1945static
1946struct poptOption query_long_options[] = {
1947 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
9009cc24
PP
1948 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
1949 { "omit-home-plugin-path", '\0', POPT_ARG_NONE, NULL, OPT_OMIT_HOME_PLUGIN_PATH, NULL, NULL },
1950 { "omit-system-plugin-path", '\0', POPT_ARG_NONE, NULL, OPT_OMIT_SYSTEM_PLUGIN_PATH, NULL, NULL },
1951 { "params", 'p', POPT_ARG_STRING, NULL, OPT_PARAMS, NULL, NULL },
1952 { "plugin-path", '\0', POPT_ARG_STRING, NULL, OPT_PLUGIN_PATH, NULL, NULL },
9009cc24
PP
1953 { NULL, 0, '\0', NULL, 0, NULL, NULL },
1954};
1955
1956/*
1957 * Creates a Babeltrace config object from the arguments of a query
1958 * command.
1959 *
1960 * *retcode is set to the appropriate exit code to use.
1961 */
1962static
1963struct bt_config *bt_config_query_from_args(int argc, const char *argv[],
1964 int *retcode, bool force_omit_system_plugin_path,
1965 bool force_omit_home_plugin_path,
1966 struct bt_value *initial_plugin_paths)
1967{
1968 poptContext pc = NULL;
1969 char *arg = NULL;
1970 int opt;
1971 int ret;
1972 struct bt_config *cfg = NULL;
1973 const char *leftover;
1974 struct bt_value *params = bt_value_null;
1975
1976 *retcode = 0;
1977 cfg = bt_config_query_create(initial_plugin_paths);
1978 if (!cfg) {
1979 goto error;
1980 }
1981
1982 cfg->omit_system_plugin_path = force_omit_system_plugin_path;
1983 cfg->omit_home_plugin_path = force_omit_home_plugin_path;
1984 ret = append_env_var_plugin_paths(cfg->plugin_paths);
1985 if (ret) {
1986 goto error;
1987 }
1988
1989 /* Parse options */
1990 pc = poptGetContext(NULL, argc, (const char **) argv,
1991 query_long_options, 0);
1992 if (!pc) {
1993 printf_err("Cannot get popt context\n");
1994 goto error;
1995 }
1996
1997 poptReadDefaultConfig(pc, 0);
1998
1999 while ((opt = poptGetNextOpt(pc)) > 0) {
2000 arg = poptGetOptArg(pc);
2001
2002 switch (opt) {
2003 case OPT_PLUGIN_PATH:
2004 if (bt_config_append_plugin_paths_check_setuid_setgid(
2005 cfg->plugin_paths, arg)) {
2006 goto error;
2007 }
2008 break;
2009 case OPT_OMIT_SYSTEM_PLUGIN_PATH:
2010 cfg->omit_system_plugin_path = true;
2011 break;
2012 case OPT_OMIT_HOME_PLUGIN_PATH:
2013 cfg->omit_home_plugin_path = true;
2014 break;
9009cc24
PP
2015 case OPT_PARAMS:
2016 {
93c1364b 2017 bt_put(params);
9009cc24
PP
2018 params = bt_value_from_arg(arg);
2019 if (!params) {
2020 printf_err("Invalid format for --params option's argument:\n %s\n",
2021 arg);
2022 goto error;
2023 }
2024 break;
2025 }
2026 case OPT_HELP:
2027 print_query_usage(stdout);
2028 *retcode = -1;
2029 BT_PUT(cfg);
2030 goto end;
2031 default:
2032 printf_err("Unknown command-line option specified (option code %d)\n",
2033 opt);
2034 goto error;
2035 }
2036
2037 free(arg);
2038 arg = NULL;
2039 }
2040
9009cc24
PP
2041 /* Check for option parsing error */
2042 if (opt < -1) {
2043 printf_err("While parsing command-line options, at option %s: %s\n",
2044 poptBadOption(pc, 0), poptStrerror(opt));
2045 goto error;
2046 }
2047
2048 /*
93c1364b
PP
2049 * We need exactly two leftover arguments which are the
2050 * mandatory component class specification and query object.
9009cc24 2051 */
93c1364b
PP
2052 leftover = poptGetArg(pc);
2053 if (leftover) {
2054 cfg->cmd_data.query.cfg_component =
2055 bt_config_component_from_arg(leftover);
2056 if (!cfg->cmd_data.query.cfg_component) {
2057 printf_err("Invalid format for component class specification:\n %s\n",
2058 leftover);
2059 goto error;
2060 }
2061
2062 assert(params);
2063 BT_MOVE(cfg->cmd_data.query.cfg_component->params, params);
2064 } else {
2065 print_query_usage(stdout);
2066 *retcode = -1;
2067 BT_PUT(cfg);
2068 goto end;
2069 }
2070
9009cc24
PP
2071 leftover = poptGetArg(pc);
2072 if (leftover) {
2073 if (strlen(leftover) == 0) {
2074 printf_err("Invalid empty object\n");
2075 goto error;
2076 }
2077
2078 g_string_assign(cfg->cmd_data.query.object, leftover);
2079 } else {
2080 print_query_usage(stdout);
2081 *retcode = -1;
2082 BT_PUT(cfg);
2083 goto end;
2084 }
2085
2086 leftover = poptGetArg(pc);
2087 if (leftover) {
2088 printf_err("Unexpected argument: %s\n", leftover);
2089 goto error;
2090 }
2091
2092 if (append_home_and_system_plugin_paths_cfg(cfg)) {
2093 goto error;
2094 }
2095
2096 goto end;
2097
2098error:
2099 *retcode = 1;
2100 BT_PUT(cfg);
2101
2102end:
2103 if (pc) {
2104 poptFreeContext(pc);
2105 }
2106
93c1364b 2107 bt_put(params);
9009cc24
PP
2108 free(arg);
2109 return cfg;
2110}
2111
2112/*
2113 * Prints the list-plugins command usage.
2114 */
2115static
2116void print_list_plugins_usage(FILE *fp)
2117{
2118 fprintf(fp, "Usage: babeltrace [GENERAL OPTIONS] list-plugins [OPTIONS]\n");
2119 fprintf(fp, "\n");
2120 fprintf(fp, "Options:\n");
2121 fprintf(fp, "\n");
2122 fprintf(fp, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2123 fprintf(fp, " (~/.local/lib/babeltrace/plugins)\n");
2124 fprintf(fp, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2125 fprintf(fp, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2126 fprintf(fp, " dynamic plugins can be loaded\n");
9e503aa9 2127 fprintf(fp, " -h, --help Show this help and quit\n");
9009cc24
PP
2128 fprintf(fp, "\n");
2129 fprintf(fp, "See `babeltrace --help` for the list of general options.\n");
2130 fprintf(fp, "\n");
2131 fprintf(fp, "Use `babeltrace help` to get help for a specific plugin or component class.\n");
2132}
2133
2134static
2135struct poptOption list_plugins_long_options[] = {
2136 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
2137 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
2138 { "omit-home-plugin-path", '\0', POPT_ARG_NONE, NULL, OPT_OMIT_HOME_PLUGIN_PATH, NULL, NULL },
2139 { "omit-system-plugin-path", '\0', POPT_ARG_NONE, NULL, OPT_OMIT_SYSTEM_PLUGIN_PATH, NULL, NULL },
2140 { "plugin-path", '\0', POPT_ARG_STRING, NULL, OPT_PLUGIN_PATH, NULL, NULL },
2141 { NULL, 0, '\0', NULL, 0, NULL, NULL },
2142};
2143
2144/*
2145 * Creates a Babeltrace config object from the arguments of a
2146 * list-plugins command.
2147 *
2148 * *retcode is set to the appropriate exit code to use.
2149 */
2150static
2151struct bt_config *bt_config_list_plugins_from_args(int argc, const char *argv[],
2152 int *retcode, bool force_omit_system_plugin_path,
2153 bool force_omit_home_plugin_path,
2154 struct bt_value *initial_plugin_paths)
2155{
2156 poptContext pc = NULL;
2157 char *arg = NULL;
2158 int opt;
2159 int ret;
2160 struct bt_config *cfg = NULL;
2161 const char *leftover;
2162
2163 *retcode = 0;
2164 cfg = bt_config_list_plugins_create(initial_plugin_paths);
2165 if (!cfg) {
2166 goto error;
2167 }
2168
2169 cfg->omit_system_plugin_path = force_omit_system_plugin_path;
2170 cfg->omit_home_plugin_path = force_omit_home_plugin_path;
2171 ret = append_env_var_plugin_paths(cfg->plugin_paths);
2172 if (ret) {
2173 goto error;
2174 }
2175
2176 /* Parse options */
2177 pc = poptGetContext(NULL, argc, (const char **) argv,
2178 list_plugins_long_options, 0);
2179 if (!pc) {
2180 printf_err("Cannot get popt context\n");
2181 goto error;
2182 }
2183
2184 poptReadDefaultConfig(pc, 0);
2185
2186 while ((opt = poptGetNextOpt(pc)) > 0) {
2187 arg = poptGetOptArg(pc);
2188
2189 switch (opt) {
2190 case OPT_PLUGIN_PATH:
2191 if (bt_config_append_plugin_paths_check_setuid_setgid(
2192 cfg->plugin_paths, arg)) {
2193 goto error;
2194 }
2195 break;
2196 case OPT_OMIT_SYSTEM_PLUGIN_PATH:
2197 cfg->omit_system_plugin_path = true;
2198 break;
2199 case OPT_OMIT_HOME_PLUGIN_PATH:
2200 cfg->omit_home_plugin_path = true;
2201 break;
2202 case OPT_HELP:
2203 print_list_plugins_usage(stdout);
2204 *retcode = -1;
2205 BT_PUT(cfg);
2206 goto end;
2207 default:
2208 printf_err("Unknown command-line option specified (option code %d)\n",
2209 opt);
2210 goto error;
2211 }
2212
2213 free(arg);
2214 arg = NULL;
2215 }
2216
2217 /* Check for option parsing error */
2218 if (opt < -1) {
2219 printf_err("While parsing command-line options, at option %s: %s\n",
2220 poptBadOption(pc, 0), poptStrerror(opt));
2221 goto error;
2222 }
2223
2224 leftover = poptGetArg(pc);
2225 if (leftover) {
2226 printf_err("Unexpected argument: %s\n", leftover);
2227 goto error;
2228 }
2229
2230 if (append_home_and_system_plugin_paths_cfg(cfg)) {
2231 goto error;
2232 }
2233
2234 goto end;
2235
2236error:
2237 *retcode = 1;
2238 BT_PUT(cfg);
2239
2240end:
2241 if (pc) {
2242 poptFreeContext(pc);
2243 }
2244
2245 free(arg);
2246 return cfg;
2247}
2248
2249/*
2250 * Prints the run command usage.
2251 */
2252static
2253void print_run_usage(FILE *fp)
2254{
2255 fprintf(fp, "Usage: babeltrace [GENERAL OPTIONS] run [OPTIONS]\n");
2256 fprintf(fp, "\n");
2257 fprintf(fp, "Options:\n");
2258 fprintf(fp, "\n");
2259 fprintf(fp, " -b, --base-params=PARAMS Set PARAMS as the current base parameters\n");
2260 fprintf(fp, " for all the following components until\n");
2261 fprintf(fp, " --reset-base-params is encountered\n");
2262 fprintf(fp, " (see the expected format of PARAMS below)\n");
fd5f8053
PP
2263 fprintf(fp, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2264 fprintf(fp, " Instantiate the component class CLS of type\n");
2265 fprintf(fp, " TYPE (`source`, `filter`, or `sink`) found\n");
2266 fprintf(fp, " in the plugin PLUGIN, add it to the graph,\n");
2267 fprintf(fp, " and optionally name it NAME (you can also\n");
2268 fprintf(fp, " specify the name with --name)\n");
2269 fprintf(fp, " -C, --connect=CONNECTION Connect two created components (see the\n");
9009cc24 2270 fprintf(fp, " expected format of CONNECTION below)\n");
9009cc24
PP
2271 fprintf(fp, " --key=KEY Set the current initialization string\n");
2272 fprintf(fp, " parameter key to KEY (see --value)\n");
2273 fprintf(fp, " -n, --name=NAME Set the name of the current component\n");
2274 fprintf(fp, " to NAME (must be unique amongst all the\n");
2275 fprintf(fp, " names of the created components)\n");
2276 fprintf(fp, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2277 fprintf(fp, " (~/.local/lib/babeltrace/plugins)\n");
2278 fprintf(fp, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2279 fprintf(fp, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2280 fprintf(fp, " current component (see the expected format\n");
2281 fprintf(fp, " of PARAMS below)\n");
2282 fprintf(fp, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2283 fprintf(fp, " dynamic plugins can be loaded\n");
2284 fprintf(fp, " -r, --reset-base-params Reset the current base parameters to an\n");
2285 fprintf(fp, " empty map\n");
2286 fprintf(fp, " --retry-duration=DUR When babeltrace(1) needs to retry to run\n");
2287 fprintf(fp, " the graph later, retry in DUR µs\n");
2288 fprintf(fp, " (default: 100000)\n");
9009cc24
PP
2289 fprintf(fp, " --value=VAL Add a string initialization parameter to\n");
2290 fprintf(fp, " the current component with a name given by\n");
2291 fprintf(fp, " the last argument of the --key option and a\n");
2292 fprintf(fp, " value set to VAL\n");
9e503aa9 2293 fprintf(fp, " -h, --help Show this help and quit\n");
9009cc24
PP
2294 fprintf(fp, "\n");
2295 fprintf(fp, "See `babeltrace --help` for the list of general options.\n");
2296 fprintf(fp, "\n\n");
2297 fprintf(fp, "Expected format of CONNECTION\n");
2298 fprintf(fp, "-----------------------------\n");
2299 fprintf(fp, "\n");
2300 fprintf(fp, " UPSTREAM[.UPSTREAM-PORT]:DOWNSTREAM[.DOWNSTREAM-PORT]\n");
2301 fprintf(fp, "\n");
2302 fprintf(fp, "UPSTREAM and DOWNSTREAM are names of the upstream and downstream\n");
2303 fprintf(fp, "components to connect together. You must escape the following characters\n\n");
2304 fprintf(fp, "with `\\`: `\\`, `.`, and `:`. You can set the name of the current\n");
2305 fprintf(fp, "component with the --name option.\n");
2306 fprintf(fp, "\n");
2307 fprintf(fp, "UPSTREAM-PORT and DOWNSTREAM-PORT are optional globbing patterns to\n");
2308 fprintf(fp, "identify the upstream and downstream ports to use for the connection.\n");
2309 fprintf(fp, "When the port is not specified, `*` is used.\n");
2310 fprintf(fp, "\n");
2311 fprintf(fp, "When a component named UPSTREAM has an available port which matches the\n");
2312 fprintf(fp, "UPSTREAM-PORT globbing pattern, it is connected to the first port which\n");
2313 fprintf(fp, "matches the DOWNSTREAM-PORT globbing pattern of the component named\n");
2314 fprintf(fp, "DOWNSTREAM.\n");
2315 fprintf(fp, "\n");
2316 fprintf(fp, "The only special character in UPSTREAM-PORT and DOWNSTREAM-PORT is `*`\n");
2317 fprintf(fp, "which matches anything. You must escape the following characters\n");
2318 fprintf(fp, "with `\\`: `\\`, `*`, `?`, `[`, `.`, and `:`.\n");
2319 fprintf(fp, "\n");
2320 fprintf(fp, "You can connect a source component to a filter or sink component. You\n");
2321 fprintf(fp, "can connect a filter component to a sink component.\n");
2322 fprintf(fp, "\n");
2323 fprintf(fp, "Examples:\n");
2324 fprintf(fp, "\n");
2325 fprintf(fp, " my-src:my-sink\n");
2326 fprintf(fp, " ctf-fs.*stream*:utils-muxer:*\n");
2327 fprintf(fp, "\n");
2328 fprintf(fp, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
2329 fprintf(fp, "babeltrace from a shell.\n");
2330 fprintf(fp, "\n\n");
2331 print_expected_params_format(fp);
2332}
2333
2334/*
2335 * Creates a Babeltrace config object from the arguments of a run
2336 * command.
2337 *
2338 * *retcode is set to the appropriate exit code to use.
2339 */
2340static
2341struct bt_config *bt_config_run_from_args(int argc, const char *argv[],
2342 int *retcode, bool force_omit_system_plugin_path,
2343 bool force_omit_home_plugin_path,
2344 struct bt_value *initial_plugin_paths)
2345{
2346 poptContext pc = NULL;
2347 char *arg = NULL;
2348 struct bt_config_component *cur_cfg_comp = NULL;
0a011c88
JG
2349 enum bt_config_component_dest cur_cfg_comp_dest =
2350 BT_CONFIG_COMPONENT_DEST_UNKNOWN;
9009cc24
PP
2351 struct bt_value *cur_base_params = NULL;
2352 int opt, ret = 0;
2353 struct bt_config *cfg = NULL;
2354 struct bt_value *instance_names = NULL;
2355 struct bt_value *connection_args = NULL;
2356 GString *cur_param_key = NULL;
2357 char error_buf[256] = { 0 };
2358 long long retry_duration = -1;
2359 struct poptOption run_long_options[] = {
2360 { "base-params", 'b', POPT_ARG_STRING, NULL, OPT_BASE_PARAMS, NULL, NULL },
fd5f8053
PP
2361 { "component", 'c', POPT_ARG_STRING, NULL, OPT_COMPONENT, NULL, NULL },
2362 { "connect", 'C', POPT_ARG_STRING, NULL, OPT_CONNECT, NULL, NULL },
9009cc24
PP
2363 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
2364 { "key", '\0', POPT_ARG_STRING, NULL, OPT_KEY, NULL, NULL },
2365 { "name", 'n', POPT_ARG_STRING, NULL, OPT_NAME, NULL, NULL },
2366 { "omit-home-plugin-path", '\0', POPT_ARG_NONE, NULL, OPT_OMIT_HOME_PLUGIN_PATH, NULL, NULL },
2367 { "omit-system-plugin-path", '\0', POPT_ARG_NONE, NULL, OPT_OMIT_SYSTEM_PLUGIN_PATH, NULL, NULL },
2368 { "params", 'p', POPT_ARG_STRING, NULL, OPT_PARAMS, NULL, NULL },
2369 { "plugin-path", '\0', POPT_ARG_STRING, NULL, OPT_PLUGIN_PATH, NULL, NULL },
2370 { "reset-base-params", 'r', POPT_ARG_NONE, NULL, OPT_RESET_BASE_PARAMS, NULL, NULL },
2371 { "retry-duration", '\0', POPT_ARG_LONGLONG, &retry_duration, OPT_RETRY_DURATION, NULL, NULL },
9009cc24
PP
2372 { "value", '\0', POPT_ARG_STRING, NULL, OPT_VALUE, NULL, NULL },
2373 { NULL, 0, '\0', NULL, 0, NULL, NULL },
2374 };
2375
2376 *retcode = 0;
2377 cur_param_key = g_string_new(NULL);
2378 if (!cur_param_key) {
2379 print_err_oom();
2380 goto error;
2381 }
2382
2383 if (argc <= 1) {
2384 print_run_usage(stdout);
2385 *retcode = -1;
2386 goto end;
2387 }
2388
2389 cfg = bt_config_run_create(initial_plugin_paths);
2390 if (!cfg) {
2391 goto error;
2392 }
2393
2394 cfg->cmd_data.run.retry_duration_us = 100000;
2395 cfg->omit_system_plugin_path = force_omit_system_plugin_path;
2396 cfg->omit_home_plugin_path = force_omit_home_plugin_path;
2397 cur_base_params = bt_value_map_create();
2398 if (!cur_base_params) {
2399 print_err_oom();
2400 goto error;
2401 }
2402
2403 instance_names = bt_value_map_create();
2404 if (!instance_names) {
2405 print_err_oom();
2406 goto error;
2407 }
2408
2409 connection_args = bt_value_array_create();
2410 if (!connection_args) {
2411 print_err_oom();
2412 goto error;
2413 }
2414
2415 ret = append_env_var_plugin_paths(cfg->plugin_paths);
2416 if (ret) {
2417 goto error;
2418 }
2419
2420 /* Parse options */
2421 pc = poptGetContext(NULL, argc, (const char **) argv,
2422 run_long_options, 0);
2423 if (!pc) {
2424 printf_err("Cannot get popt context\n");
2425 goto error;
2426 }
2427
2428 poptReadDefaultConfig(pc, 0);
2429
2430 while ((opt = poptGetNextOpt(pc)) > 0) {
2431 arg = poptGetOptArg(pc);
2432
2433 switch (opt) {
2434 case OPT_PLUGIN_PATH:
2435 if (bt_config_append_plugin_paths_check_setuid_setgid(
2436 cfg->plugin_paths, arg)) {
2437 goto error;
2438 }
2439 break;
2440 case OPT_OMIT_SYSTEM_PLUGIN_PATH:
2441 cfg->omit_system_plugin_path = true;
2442 break;
2443 case OPT_OMIT_HOME_PLUGIN_PATH:
2444 cfg->omit_home_plugin_path = true;
2445 break;
fd5f8053 2446 case OPT_COMPONENT:
9009cc24 2447 {
9009cc24 2448 enum bt_config_component_dest new_dest;
9009cc24
PP
2449
2450 if (cur_cfg_comp) {
2451 ret = add_run_cfg_comp_check_name(cfg,
2452 cur_cfg_comp, cur_cfg_comp_dest,
2453 instance_names);
2454 BT_PUT(cur_cfg_comp);
2455 if (ret) {
2456 goto error;
2457 }
2458 }
2459
fd5f8053 2460 cur_cfg_comp = bt_config_component_from_arg(arg);
9009cc24 2461 if (!cur_cfg_comp) {
fd5f8053
PP
2462 printf_err("Invalid format for --component option's argument:\n %s\n",
2463 arg);
9009cc24
PP
2464 goto error;
2465 }
2466
fd5f8053
PP
2467 switch (cur_cfg_comp->type) {
2468 case BT_COMPONENT_CLASS_TYPE_SOURCE:
2469 new_dest = BT_CONFIG_COMPONENT_DEST_SOURCE;
2470 break;
2471 case BT_COMPONENT_CLASS_TYPE_FILTER:
2472 new_dest = BT_CONFIG_COMPONENT_DEST_FILTER;
2473 break;
2474 case BT_COMPONENT_CLASS_TYPE_SINK:
2475 new_dest = BT_CONFIG_COMPONENT_DEST_SINK;
2476 break;
2477 default:
2478 abort();
2479 }
2480
9009cc24
PP
2481 assert(cur_base_params);
2482 bt_put(cur_cfg_comp->params);
2483 cur_cfg_comp->params = bt_value_copy(cur_base_params);
2484 if (!cur_cfg_comp->params) {
2485 print_err_oom();
2486 goto error;
2487 }
2488
2489 cur_cfg_comp_dest = new_dest;
2490 break;
2491 }
2492 case OPT_PARAMS:
2493 {
2494 struct bt_value *params;
2495 struct bt_value *params_to_set;
2496
2497 if (!cur_cfg_comp) {
2498 printf_err("Cannot add parameters to unavailable component:\n %s\n",
2499 arg);
2500 goto error;
2501 }
2502
2503 params = bt_value_from_arg(arg);
2504 if (!params) {
2505 printf_err("Invalid format for --params option's argument:\n %s\n",
2506 arg);
2507 goto error;
2508 }
2509
2510 params_to_set = bt_value_map_extend(cur_cfg_comp->params,
2511 params);
2512 BT_PUT(params);
2513 if (!params_to_set) {
2514 printf_err("Cannot extend current component parameters with --params option's argument:\n %s\n",
2515 arg);
2516 goto error;
2517 }
2518
2519 BT_MOVE(cur_cfg_comp->params, params_to_set);
2520 break;
2521 }
2522 case OPT_KEY:
2523 if (strlen(arg) == 0) {
2524 printf_err("Cannot set an empty string as the current parameter key\n");
2525 goto error;
2526 }
2527
2528 g_string_assign(cur_param_key, arg);
2529 break;
2530 case OPT_VALUE:
2531 if (!cur_cfg_comp) {
2532 printf_err("Cannot set a parameter's value of unavailable component:\n %s\n",
2533 arg);
2534 goto error;
2535 }
2536
2537 if (cur_param_key->len == 0) {
2538 printf_err("--value option specified without preceding --key option:\n %s\n",
2539 arg);
2540 goto error;
2541 }
2542
2543 if (bt_value_map_insert_string(cur_cfg_comp->params,
2544 cur_param_key->str, arg)) {
2545 print_err_oom();
2546 goto error;
2547 }
2548 break;
2549 case OPT_NAME:
2550 if (!cur_cfg_comp) {
2551 printf_err("Cannot set the name of unavailable component:\n %s\n",
2552 arg);
2553 goto error;
2554 }
2555
2556 g_string_assign(cur_cfg_comp->instance_name, arg);
2557 break;
2558 case OPT_BASE_PARAMS:
2559 {
2560 struct bt_value *params = bt_value_from_arg(arg);
2561
2562 if (!params) {
2563 printf_err("Invalid format for --base-params option's argument:\n %s\n",
2564 arg);
2565 goto error;
2566 }
2567
2568 BT_MOVE(cur_base_params, params);
2569 break;
2570 }
2571 case OPT_RESET_BASE_PARAMS:
2572 BT_PUT(cur_base_params);
2573 cur_base_params = bt_value_map_create();
2574 if (!cur_base_params) {
2575 print_err_oom();
2576 goto error;
2577 }
2578 break;
2579 case OPT_CONNECT:
2580 if (bt_value_array_append_string(connection_args,
2581 arg)) {
2582 print_err_oom();
2583 goto error;
2584 }
2585 break;
2586 case OPT_RETRY_DURATION:
2587 if (retry_duration < 0) {
2588 printf_err("--retry-duration option's argument must be positive or 0: %lld\n",
2589 retry_duration);
2590 goto error;
2591 }
2592
2593 cfg->cmd_data.run.retry_duration_us =
2594 (uint64_t) retry_duration;
2595 break;
2596 case OPT_HELP:
2597 print_run_usage(stdout);
2598 *retcode = -1;
2599 BT_PUT(cfg);
2600 goto end;
2601 default:
2602 printf_err("Unknown command-line option specified (option code %d)\n",
2603 opt);
2604 goto error;
2605 }
2606
2607 free(arg);
2608 arg = NULL;
2609 }
2610
2611 /* Check for option parsing error */
2612 if (opt < -1) {
2613 printf_err("While parsing command-line options, at option %s: %s\n",
2614 poptBadOption(pc, 0), poptStrerror(opt));
2615 goto error;
2616 }
2617
2618 /* This command does not accept leftover arguments */
2619 if (poptPeekArg(pc)) {
2620 printf_err("Unexpected argument: %s\n", poptPeekArg(pc));
2621 goto error;
2622 }
2623
2624 /* Add current component */
2625 if (cur_cfg_comp) {
2626 ret = add_run_cfg_comp_check_name(cfg, cur_cfg_comp,
2627 cur_cfg_comp_dest, instance_names);
2628 BT_PUT(cur_cfg_comp);
2629 if (ret) {
2630 goto error;
2631 }
2632 }
2633
2634 if (cfg->cmd_data.run.sources->len == 0) {
2635 printf_err("Incomplete graph: no source component\n");
2636 goto error;
2637 }
2638
2639 if (cfg->cmd_data.run.sinks->len == 0) {
2640 printf_err("Incomplete graph: no sink component\n");
2641 goto error;
2642 }
2643
2644 if (append_home_and_system_plugin_paths_cfg(cfg)) {
2645 goto error;
2646 }
2647
2648 ret = bt_config_cli_args_create_connections(cfg, connection_args,
2649 error_buf, 256);
2650 if (ret) {
2651 printf_err("Cannot creation connections:\n%s", error_buf);
2652 goto error;
2653 }
2654
2655 goto end;
2656
2657error:
2658 *retcode = 1;
2659 BT_PUT(cfg);
2660
2661end:
2662 if (pc) {
2663 poptFreeContext(pc);
2664 }
2665
2666 if (cur_param_key) {
2667 g_string_free(cur_param_key, TRUE);
2668 }
2669
2670 free(arg);
2671 BT_PUT(cur_cfg_comp);
2672 BT_PUT(cur_base_params);
2673 BT_PUT(instance_names);
2674 BT_PUT(connection_args);
2675 return cfg;
2676}
2677
2678static
2679struct bt_config *bt_config_run_from_args_array(struct bt_value *run_args,
2680 int *retcode, bool force_omit_system_plugin_path,
2681 bool force_omit_home_plugin_path,
2682 struct bt_value *initial_plugin_paths)
2683{
2684 struct bt_config *cfg = NULL;
2685 const char **argv;
0d3e053b 2686 int64_t i, len;
9009cc24
PP
2687 const size_t argc = bt_value_array_size(run_args) + 1;
2688
2689 argv = calloc(argc, sizeof(*argv));
2690 if (!argv) {
2691 print_err_oom();
2692 goto end;
2693 }
2694
2695 argv[0] = "run";
2696
0d3e053b
MD
2697 len = bt_value_array_size(run_args);
2698 if (len < 0) {
2699 printf_err("Invalid executable arguments\n");
2700 goto end;
2701 }
2702 for (i = 0; i < len; i++) {
9009cc24
PP
2703 int ret;
2704 struct bt_value *arg_value = bt_value_array_get(run_args, i);
2705 const char *arg;
2706
2707 assert(arg_value);
2708 ret = bt_value_string_get(arg_value, &arg);
2709 assert(ret == 0);
2710 assert(arg);
2711 argv[i + 1] = arg;
2712 bt_put(arg_value);
2713 }
2714
2715 cfg = bt_config_run_from_args(argc, argv, retcode,
2716 force_omit_system_plugin_path, force_omit_home_plugin_path,
2717 initial_plugin_paths);
2718
2719end:
2720 free(argv);
2721 return cfg;
2722}
2723
2724/*
2725 * Prints the convert command usage.
2726 */
2727static
2728void print_convert_usage(FILE *fp)
2729{
2730 fprintf(fp, "Usage: babeltrace [GENERAL OPTIONS] [convert] [OPTIONS] [PATH/URL]\n");
2731 fprintf(fp, "\n");
2732 fprintf(fp, "Options:\n");
2733 fprintf(fp, "\n");
fd5f8053
PP
2734 fprintf(fp, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2735 fprintf(fp, " Instantiate the component class CLS of type\n");
2736 fprintf(fp, " TYPE (`source`, `filter`, or `sink`) found\n");
2737 fprintf(fp, " in the plugin PLUGIN, add it to the\n");
2738 fprintf(fp, " conversion graph, and optionally name it\n");
2739 fprintf(fp, " NAME (you can also specify the name with\n");
2740 fprintf(fp, " --name)\n");
9009cc24
PP
2741 fprintf(fp, " --name=NAME Set the name of the current component\n");
2742 fprintf(fp, " to NAME (must be unique amongst all the\n");
2743 fprintf(fp, " names of the created components)\n");
2744 fprintf(fp, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2745 fprintf(fp, " (~/.local/lib/babeltrace/plugins)\n");
2746 fprintf(fp, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2747 fprintf(fp, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2748 fprintf(fp, " current component (see the expected format\n");
2749 fprintf(fp, " of PARAMS below)\n");
2750 fprintf(fp, " -P, --path=PATH Set the `path` string parameter of the\n");
2751 fprintf(fp, " current component to PATH\n");
2752 fprintf(fp, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2753 fprintf(fp, " --retry-duration=DUR When babeltrace(1) needs to retry to run\n");
2754 fprintf(fp, " the graph later, retry in DUR µs\n");
2755 fprintf(fp, " (default: 100000)\n");
2756 fprintf(fp, " dynamic plugins can be loaded\n");
2757 fprintf(fp, " --run-args Print the equivalent arguments for the\n");
2758 fprintf(fp, " `run` command to the standard output,\n");
2759 fprintf(fp, " formatted for a shell, and quit\n");
2760 fprintf(fp, " --run-args-0 Print the equivalent arguments for the\n");
2761 fprintf(fp, " `run` command to the standard output,\n");
2762 fprintf(fp, " formatted for `xargs -0`, and quit\n");
8320cc93
PP
2763 fprintf(fp, " --stream-intersection Only process events when all streams\n");
2764 fprintf(fp, " are active\n");
9009cc24
PP
2765 fprintf(fp, " -u, --url=URL Set the `url` string parameter of the\n");
2766 fprintf(fp, " current component to URL\n");
9e503aa9 2767 fprintf(fp, " -h, --help Show this help and quit\n");
9009cc24 2768 fprintf(fp, "\n");
e7ad156c 2769 fprintf(fp, "Implicit `source.ctf.fs` component options:\n");
9009cc24
PP
2770 fprintf(fp, "\n");
2771 fprintf(fp, " --clock-offset=SEC Set clock offset to SEC seconds\n");
2772 fprintf(fp, " --clock-offset-ns=NS Set clock offset to NS ns\n");
9009cc24 2773 fprintf(fp, "\n");
e7ad156c 2774 fprintf(fp, "Implicit `sink.text.pretty` component options:\n");
9009cc24
PP
2775 fprintf(fp, "\n");
2776 fprintf(fp, " --clock-cycles Print timestamps in clock cycles\n");
2777 fprintf(fp, " --clock-date Print timestamp dates\n");
2778 fprintf(fp, " --clock-gmt Print and parse timestamps in the GMT\n");
2779 fprintf(fp, " time zone instead of the local time zone\n");
2780 fprintf(fp, " --clock-seconds Print the timestamps as `SEC.NS` instead\n");
2781 fprintf(fp, " of `hh:mm:ss.nnnnnnnnn`\n");
2782 fprintf(fp, " --color=(never | auto | always)\n");
2783 fprintf(fp, " Never, automatically, or always emit\n");
2784 fprintf(fp, " console color codes\n");
2785 fprintf(fp, " -f, --fields=FIELD[,FIELD]... Print additional fields; FIELD can be:\n");
2786 fprintf(fp, " `all`, `trace`, `trace:hostname`,\n");
2787 fprintf(fp, " `trace:domain`, `trace:procname`,\n");
2788 fprintf(fp, " `trace:vpid`, `loglevel`, `emf`\n");
2789 fprintf(fp, " -n, --names=NAME[,NAME]... Print field names; NAME can be:\n");
2790 fprintf(fp, " `payload` (or `arg` or `args`), `none`,\n");
2791 fprintf(fp, " `all`, `scope`, `header`, `context`\n");
2792 fprintf(fp, " (or `ctx`)\n");
2793 fprintf(fp, " --no-delta Do not print time delta between\n");
2794 fprintf(fp, " consecutive events\n");
2795 fprintf(fp, " -w, --output=PATH Write output text to PATH instead of\n");
2796 fprintf(fp, " the standard output\n");
2797 fprintf(fp, "\n");
e7ad156c 2798 fprintf(fp, "Implicit `filter.utils.muxer` component options:\n");
9009cc24
PP
2799 fprintf(fp, "\n");
2800 fprintf(fp, " --clock-force-correlate Assume that clocks are inherently\n");
2801 fprintf(fp, " correlated across traces\n");
2802 fprintf(fp, "\n");
e7ad156c 2803 fprintf(fp, "Implicit `filter.utils.trimmer` component options:\n");
9009cc24
PP
2804 fprintf(fp, "\n");
2805 fprintf(fp, " -b, --begin=BEGIN Set the beginning time of the conversion\n");
2806 fprintf(fp, " time range to BEGIN (see the format of\n");
2807 fprintf(fp, " BEGIN below)\n");
2808 fprintf(fp, " -e, --end=END Set the end time of the conversion time\n");
2809 fprintf(fp, " range to END (see the format of END below)\n");
2810 fprintf(fp, " -t, --timerange=TIMERANGE Set conversion time range to TIMERANGE:\n");
2811 fprintf(fp, " BEGIN,END or [BEGIN,END] (literally `[` and\n");
2812 fprintf(fp, " `]`) (see the format of BEGIN/END below)\n");
2813 fprintf(fp, "\n");
e7ad156c 2814 fprintf(fp, "Implicit `filter.lttng-utils.debug-info` component options:\n");
9009cc24 2815 fprintf(fp, "\n");
8e765000
PP
2816 fprintf(fp, " --debug-info Create an implicit\n");
2817 fprintf(fp, " `filter.lttng-utils.debug-info` component\n");
9009cc24
PP
2818 fprintf(fp, " --debug-info-dir=DIR Search for debug info in directory DIR\n");
2819 fprintf(fp, " instead of `/usr/lib/debug`\n");
2820 fprintf(fp, " --debug-info-full-path Show full debug info source and\n");
2821 fprintf(fp, " binary paths instead of just names\n");
2822 fprintf(fp, " --debug-info-target-prefix=DIR\n");
2823 fprintf(fp, " Use directory DIR as a prefix when\n");
2824 fprintf(fp, " looking up executables during debug\n");
2825 fprintf(fp, " info analysis\n");
9009cc24
PP
2826 fprintf(fp, "\n");
2827 fprintf(fp, "Legacy options that still work:\n");
2828 fprintf(fp, "\n");
2829 fprintf(fp, " -i, --input-format=(ctf | lttng-live)\n");
2830 fprintf(fp, " `ctf`:\n");
fd5f8053 2831 fprintf(fp, " Create an implicit `source.ctf.fs`\n");
9009cc24
PP
2832 fprintf(fp, " component\n");
2833 fprintf(fp, " `lttng-live`:\n");
fd5f8053
PP
2834 fprintf(fp, " Create an implicit `source.ctf.lttng-live`\n");
2835 fprintf(fp, " component\n");
e7ad156c 2836 fprintf(fp, " -o, --output-format=(text | ctf | dummy | ctf-metadata)\n");
9009cc24 2837 fprintf(fp, " `text`:\n");
fd5f8053 2838 fprintf(fp, " Create an implicit `sink.text.pretty`\n");
9009cc24 2839 fprintf(fp, " component\n");
8f4f40e7 2840 fprintf(fp, " `ctf`:\n");
e7ad156c
PP
2841 fprintf(fp, " Create an implicit `sink.ctf.fs`\n");
2842 fprintf(fp, " component\n");
9009cc24 2843 fprintf(fp, " `dummy`:\n");
fd5f8053 2844 fprintf(fp, " Create an implicit `sink.utils.dummy`\n");
9009cc24
PP
2845 fprintf(fp, " component\n");
2846 fprintf(fp, " `ctf-metadata`:\n");
fd5f8053
PP
2847 fprintf(fp, " Query the `source.ctf.fs` component class\n");
2848 fprintf(fp, " for metadata text and quit\n");
9009cc24
PP
2849 fprintf(fp, "\n");
2850 fprintf(fp, "See `babeltrace --help` for the list of general options.\n");
2851 fprintf(fp, "\n\n");
2852 fprintf(fp, "Format of BEGIN and END\n");
2853 fprintf(fp, "-----------------------\n");
2854 fprintf(fp, "\n");
2855 fprintf(fp, " [YYYY-MM-DD [hh:mm:]]ss[.nnnnnnnnn]\n");
2856 fprintf(fp, "\n\n");
2857 print_expected_params_format(fp);
2858}
2859
2860static
2861struct poptOption convert_long_options[] = {
2862 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
2863 { "begin", 'b', POPT_ARG_STRING, NULL, OPT_BEGIN, NULL, NULL },
2864 { "clock-cycles", '\0', POPT_ARG_NONE, NULL, OPT_CLOCK_CYCLES, NULL, NULL },
2865 { "clock-date", '\0', POPT_ARG_NONE, NULL, OPT_CLOCK_DATE, NULL, NULL },
2866 { "clock-force-correlate", '\0', POPT_ARG_NONE, NULL, OPT_CLOCK_FORCE_CORRELATE, NULL, NULL },
2867 { "clock-gmt", '\0', POPT_ARG_NONE, NULL, OPT_CLOCK_GMT, NULL, NULL },
2868 { "clock-offset", '\0', POPT_ARG_STRING, NULL, OPT_CLOCK_OFFSET, NULL, NULL },
2869 { "clock-offset-ns", '\0', POPT_ARG_STRING, NULL, OPT_CLOCK_OFFSET_NS, NULL, NULL },
2870 { "clock-seconds", '\0', POPT_ARG_NONE, NULL, OPT_CLOCK_SECONDS, NULL, NULL },
2871 { "color", '\0', POPT_ARG_STRING, NULL, OPT_COLOR, NULL, NULL },
fd5f8053 2872 { "component", 'c', POPT_ARG_STRING, NULL, OPT_COMPONENT, NULL, NULL },
9009cc24
PP
2873 { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL },
2874 { "debug-info-dir", 0, POPT_ARG_STRING, NULL, OPT_DEBUG_INFO_DIR, NULL, NULL },
2875 { "debug-info-full-path", 0, POPT_ARG_NONE, NULL, OPT_DEBUG_INFO_FULL_PATH, NULL, NULL },
2876 { "debug-info-target-prefix", 0, POPT_ARG_STRING, NULL, OPT_DEBUG_INFO_TARGET_PREFIX, NULL, NULL },
2877 { "end", 'e', POPT_ARG_STRING, NULL, OPT_END, NULL, NULL },
2878 { "fields", 'f', POPT_ARG_STRING, NULL, OPT_FIELDS, NULL, NULL },
9009cc24
PP
2879 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
2880 { "input-format", 'i', POPT_ARG_STRING, NULL, OPT_INPUT_FORMAT, NULL, NULL },
2881 { "name", '\0', POPT_ARG_STRING, NULL, OPT_NAME, NULL, NULL },
2882 { "names", 'n', POPT_ARG_STRING, NULL, OPT_NAMES, NULL, NULL },
8e765000 2883 { "debug-info", '\0', POPT_ARG_NONE, NULL, OPT_DEBUG_INFO, NULL, NULL },
9009cc24
PP
2884 { "no-delta", '\0', POPT_ARG_NONE, NULL, OPT_NO_DELTA, NULL, NULL },
2885 { "omit-home-plugin-path", '\0', POPT_ARG_NONE, NULL, OPT_OMIT_HOME_PLUGIN_PATH, NULL, NULL },
2886 { "omit-system-plugin-path", '\0', POPT_ARG_NONE, NULL, OPT_OMIT_SYSTEM_PLUGIN_PATH, NULL, NULL },
2887 { "output", 'w', POPT_ARG_STRING, NULL, OPT_OUTPUT, NULL, NULL },
2888 { "output-format", 'o', POPT_ARG_STRING, NULL, OPT_OUTPUT_FORMAT, NULL, NULL },
2889 { "params", 'p', POPT_ARG_STRING, NULL, OPT_PARAMS, NULL, NULL },
2890 { "path", 'P', POPT_ARG_STRING, NULL, OPT_PATH, NULL, NULL },
2891 { "plugin-path", '\0', POPT_ARG_STRING, NULL, OPT_PLUGIN_PATH, NULL, NULL },
2892 { "retry-duration", '\0', POPT_ARG_STRING, NULL, OPT_RETRY_DURATION, NULL, NULL },
2893 { "run-args", '\0', POPT_ARG_NONE, NULL, OPT_RUN_ARGS, NULL, NULL },
2894 { "run-args-0", '\0', POPT_ARG_NONE, NULL, OPT_RUN_ARGS_0, NULL, NULL },
9009cc24
PP
2895 { "stream-intersection", '\0', POPT_ARG_NONE, NULL, OPT_STREAM_INTERSECTION, NULL, NULL },
2896 { "timerange", '\0', POPT_ARG_STRING, NULL, OPT_TIMERANGE, NULL, NULL },
2897 { "url", 'u', POPT_ARG_STRING, NULL, OPT_URL, NULL, NULL },
2898 { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL },
2899 { NULL, 0, '\0', NULL, 0, NULL, NULL },
2900};
2901
2902static
2903GString *get_component_auto_name(const char *prefix,
2904 struct bt_value *existing_names)
2905{
2906 unsigned int i = 0;
2907 GString *auto_name = g_string_new(NULL);
2908
2909 if (!auto_name) {
2910 print_err_oom();
2911 goto end;
2912 }
2913
2914 if (!bt_value_map_has_key(existing_names, prefix)) {
2915 g_string_assign(auto_name, prefix);
2916 goto end;
2917 }
2918
2919 do {
2920 g_string_printf(auto_name, "%s-%d", prefix, i);
2921 i++;
2922 } while (bt_value_map_has_key(existing_names, auto_name->str));
2923
2924end:
2925 return auto_name;
2926}
2927
2928struct implicit_component_args {
2929 bool exists;
fd5f8053 2930 GString *comp_arg;
9009cc24
PP
2931 GString *name_arg;
2932 GString *params_arg;
e7ad156c 2933 struct bt_value *extra_params;
9009cc24
PP
2934};
2935
2936static
2937int assign_name_to_implicit_component(struct implicit_component_args *args,
2938 const char *prefix, struct bt_value *existing_names,
2939 GList **comp_names, bool append_to_comp_names)
2940{
2941 int ret = 0;
2942 GString *name = NULL;
2943
2944 if (!args->exists) {
2945 goto end;
2946 }
2947
2948 name = get_component_auto_name(prefix, existing_names);
2949
2950 if (!name) {
2951 ret = -1;
2952 goto end;
2953 }
2954
2955 g_string_assign(args->name_arg, name->str);
2956
2957 if (bt_value_map_insert(existing_names, name->str,
2958 bt_value_null)) {
2959 print_err_oom();
2960 ret = -1;
2961 goto end;
2962 }
2963
2964 if (append_to_comp_names) {
2965 *comp_names = g_list_append(*comp_names, name);
2966 name = NULL;
2967 }
2968
2969end:
2970 if (name) {
2971 g_string_free(name, TRUE);
2972 }
2973
2974 return ret;
2975}
2976
2977static
2978int append_run_args_for_implicit_component(
9009cc24
PP
2979 struct implicit_component_args *impl_args,
2980 struct bt_value *run_args)
2981{
2982 int ret = 0;
2983 size_t i;
2984
2985 if (!impl_args->exists) {
2986 goto end;
2987 }
2988
fd5f8053
PP
2989 if (bt_value_array_append_string(run_args, "--component")) {
2990 print_err_oom();
2991 goto error;
9009cc24
PP
2992 }
2993
fd5f8053 2994 if (bt_value_array_append_string(run_args, impl_args->comp_arg->str)) {
9009cc24
PP
2995 print_err_oom();
2996 goto error;
2997 }
2998
2999 if (bt_value_array_append_string(run_args, "--name")) {
3000 print_err_oom();
3001 goto error;
3002 }
3003
3004 if (bt_value_array_append_string(run_args, impl_args->name_arg->str)) {
3005 print_err_oom();
3006 goto error;
3007 }
3008
3009 if (impl_args->params_arg->len > 0) {
3010 if (bt_value_array_append_string(run_args, "--params")) {
3011 print_err_oom();
3012 goto error;
3013 }
3014
3015 if (bt_value_array_append_string(run_args,
3016 impl_args->params_arg->str)) {
3017 print_err_oom();
3018 goto error;
3019 }
3020 }
3021
e7ad156c 3022 for (i = 0; i < bt_value_array_size(impl_args->extra_params); i++) {
9009cc24
PP
3023 struct bt_value *elem;
3024 const char *arg;
3025
e7ad156c 3026 elem = bt_value_array_get(impl_args->extra_params, i);
9009cc24
PP
3027 if (!elem) {
3028 goto error;
3029 }
3030
3031 assert(bt_value_is_string(elem));
3032 if (bt_value_string_get(elem, &arg)) {
3033 goto error;
3034 }
3035
3036 ret = bt_value_array_append_string(run_args, arg);
3037 bt_put(elem);
3038 if (ret) {
3039 print_err_oom();
3040 goto error;
3041 }
3042 }
3043
3044 goto end;
3045
3046error:
3047 ret = -1;
3048
3049end:
3050 return ret;
3051}
3052
3053static
94023a1c 3054void finalize_implicit_component_args(struct implicit_component_args *args)
9009cc24
PP
3055{
3056 assert(args);
3057
fd5f8053
PP
3058 if (args->comp_arg) {
3059 g_string_free(args->comp_arg, TRUE);
9009cc24
PP
3060 }
3061
3062 if (args->name_arg) {
3063 g_string_free(args->name_arg, TRUE);
3064 }
3065
3066 if (args->params_arg) {
3067 g_string_free(args->params_arg, TRUE);
3068 }
3069
e7ad156c 3070 bt_put(args->extra_params);
9009cc24
PP
3071}
3072
94023a1c
PP
3073static
3074void destroy_implicit_component_args(void *args)
3075{
3076 if (!args) {
3077 return;
3078 }
3079
3080 finalize_implicit_component_args(args);
3081 g_free(args);
3082}
3083
9009cc24
PP
3084static
3085int init_implicit_component_args(struct implicit_component_args *args,
fd5f8053 3086 const char *comp_arg, bool exists)
9009cc24
PP
3087{
3088 int ret = 0;
3089
3090 args->exists = exists;
fd5f8053 3091 args->comp_arg = g_string_new(comp_arg);
9009cc24
PP
3092 args->name_arg = g_string_new(NULL);
3093 args->params_arg = g_string_new(NULL);
e7ad156c 3094 args->extra_params = bt_value_array_create();
9009cc24 3095
fd5f8053 3096 if (!args->comp_arg || !args->name_arg ||
e7ad156c 3097 !args->params_arg || !args->extra_params) {
9009cc24 3098 ret = -1;
94023a1c 3099 finalize_implicit_component_args(args);
9009cc24
PP
3100 print_err_oom();
3101 goto end;
3102 }
3103
3104end:
3105 return ret;
3106}
3107
3108static
3109void append_implicit_component_param(struct implicit_component_args *args,
3110 const char *key, const char *value)
3111{
3112 assert(args);
3113 assert(key);
3114 assert(value);
3115 append_param_arg(args->params_arg, key, value);
3116}
3117
3118static
e7ad156c 3119int append_implicit_component_extra_param(struct implicit_component_args *args,
9009cc24
PP
3120 const char *key, const char *value)
3121{
3122 int ret = 0;
3123
3124 assert(args);
3125 assert(key);
3126 assert(value);
3127
e7ad156c 3128 if (bt_value_array_append_string(args->extra_params, "--key")) {
9009cc24
PP
3129 print_err_oom();
3130 ret = -1;
3131 goto end;
3132 }
3133
e7ad156c 3134 if (bt_value_array_append_string(args->extra_params, key)) {
9009cc24
PP
3135 print_err_oom();
3136 ret = -1;
3137 goto end;
3138 }
3139
e7ad156c 3140 if (bt_value_array_append_string(args->extra_params, "--value")) {
9009cc24
PP
3141 print_err_oom();
3142 ret = -1;
3143 goto end;
3144 }
3145
e7ad156c 3146 if (bt_value_array_append_string(args->extra_params, value)) {
9009cc24
PP
3147 print_err_oom();
3148 ret = -1;
3149 goto end;
3150 }
3151
3152end:
3153 return ret;
3154}
3155
3156static
3157int convert_append_name_param(enum bt_config_component_dest dest,
3158 GString *cur_name, GString *cur_name_prefix,
3159 struct bt_value *run_args, struct bt_value *all_names,
3160 GList **source_names, GList **filter_names,
3161 GList **sink_names)
3162{
3163 int ret = 0;
3164
3165 if (cur_name_prefix->len > 0) {
fd5f8053 3166 /* We're after a --component option */
9009cc24
PP
3167 GString *name = NULL;
3168 bool append_name_opt = false;
3169
3170 if (cur_name->len == 0) {
3171 /*
3172 * No explicit name was provided for the user
3173 * component.
3174 */
fd5f8053 3175 name = get_component_auto_name(cur_name_prefix->str,
9009cc24
PP
3176 all_names);
3177 append_name_opt = true;
3178 } else {
3179 /*
3180 * An explicit name was provided for the user
3181 * component.
3182 */
3183 if (bt_value_map_has_key(all_names,
3184 cur_name->str)) {
3185 printf_err("Duplicate component instance name:\n %s\n",
3186 cur_name->str);
3187 goto error;
3188 }
3189
3190 name = g_string_new(cur_name->str);
3191 }
3192
3193 if (!name) {
3194 print_err_oom();
3195 goto error;
3196 }
3197
3198 /*
3199 * Remember this name globally, for the uniqueness of
3200 * all component names.
3201 */
3202 if (bt_value_map_insert(all_names, name->str, bt_value_null)) {
3203 print_err_oom();
3204 goto error;
3205 }
3206
3207 /*
3208 * Append the --name option if necessary.
3209 */
3210 if (append_name_opt) {
3211 if (bt_value_array_append_string(run_args, "--name")) {
3212 print_err_oom();
3213 goto error;
3214 }
3215
3216 if (bt_value_array_append_string(run_args, name->str)) {
3217 print_err_oom();
3218 goto error;
3219 }
3220 }
3221
3222 /*
3223 * Remember this name specifically for the type of the
3224 * component. This is to create connection arguments.
3225 */
3226 switch (dest) {
3227 case BT_CONFIG_COMPONENT_DEST_SOURCE:
3228 *source_names = g_list_append(*source_names, name);
3229 break;
3230 case BT_CONFIG_COMPONENT_DEST_FILTER:
3231 *filter_names = g_list_append(*filter_names, name);
3232 break;
3233 case BT_CONFIG_COMPONENT_DEST_SINK:
3234 *sink_names = g_list_append(*sink_names, name);
3235 break;
3236 default:
0fbb9a9f 3237 abort();
9009cc24
PP
3238 }
3239
3240 g_string_assign(cur_name_prefix, "");
3241 }
3242
3243 goto end;
3244
3245error:
3246 ret = -1;
3247
3248end:
3249 return ret;
3250}
3251
3252/*
3253 * Escapes `.`, `:`, and `\` of `input` with `\`.
3254 */
3255static
3256GString *escape_dot_colon(const char *input)
3257{
3258 GString *output = g_string_new(NULL);
3259 const char *ch;
3260
3261 if (!output) {
3262 print_err_oom();
3263 goto end;
3264 }
3265
3266 for (ch = input; *ch != '\0'; ch++) {
3267 if (*ch == '\\' || *ch == '.' || *ch == ':') {
3268 g_string_append_c(output, '\\');
3269 }
3270
3271 g_string_append_c(output, *ch);
3272 }
3273
3274end:
3275 return output;
3276}
3277
3278/*
3279 * Appends a --connect option to a list of arguments. `upstream_name`
3280 * and `downstream_name` are escaped with escape_dot_colon() in this
3281 * function.
3282 */
3283static
3284int append_connect_arg(struct bt_value *run_args,
3285 const char *upstream_name, const char *downstream_name)
3286{
3287 int ret = 0;
3288 GString *e_upstream_name = escape_dot_colon(upstream_name);
3289 GString *e_downstream_name = escape_dot_colon(downstream_name);
3290 GString *arg = g_string_new(NULL);
3291
3292 if (!e_upstream_name || !e_downstream_name || !arg) {
3293 print_err_oom();
3294 ret = -1;
3295 goto end;
3296 }
3297
3298 ret = bt_value_array_append_string(run_args, "--connect");
3299 if (ret) {
3300 print_err_oom();
3301 ret = -1;
3302 goto end;
3303 }
3304
3305 g_string_append(arg, e_upstream_name->str);
3306 g_string_append_c(arg, ':');
3307 g_string_append(arg, e_downstream_name->str);
3308 ret = bt_value_array_append_string(run_args, arg->str);
3309 if (ret) {
3310 print_err_oom();
3311 ret = -1;
3312 goto end;
3313 }
3314
3315end:
3316 if (arg) {
3317 g_string_free(arg, TRUE);
3318 }
3319
3320 if (e_upstream_name) {
3321 g_string_free(e_upstream_name, TRUE);
3322 }
3323
3324 if (e_downstream_name) {
3325 g_string_free(e_downstream_name, TRUE);
3326 }
3327
3328 return ret;
3329}
3330
3331/*
3332 * Appends the run command's --connect options for the convert command.
3333 */
3334static
3335int convert_auto_connect(struct bt_value *run_args,
3336 GList *source_names, GList *filter_names,
3337 GList *sink_names)
3338{
3339 int ret = 0;
3340 GList *source_at = source_names;
3341 GList *filter_at = filter_names;
3342 GList *filter_prev;
3343 GList *sink_at = sink_names;
3344
3345 assert(source_names);
3346 assert(filter_names);
3347 assert(sink_names);
3348
3349 /* Connect all sources to the first filter */
3350 for (source_at = source_names; source_at != NULL; source_at = g_list_next(source_at)) {
3351 GString *source_name = source_at->data;
3352 GString *filter_name = filter_at->data;
3353
3354 ret = append_connect_arg(run_args, source_name->str,
3355 filter_name->str);
3356 if (ret) {
3357 goto error;
3358 }
3359 }
3360
3361 filter_prev = filter_at;
3362 filter_at = g_list_next(filter_at);
3363
3364 /* Connect remaining filters */
3365 for (; filter_at != NULL; filter_prev = filter_at, filter_at = g_list_next(filter_at)) {
3366 GString *filter_name = filter_at->data;
3367 GString *filter_prev_name = filter_prev->data;
3368
3369 ret = append_connect_arg(run_args, filter_prev_name->str,
3370 filter_name->str);
3371 if (ret) {
3372 goto error;
3373 }
3374 }
3375
3376 /* Connect last filter to all sinks */
3377 for (sink_at = sink_names; sink_at != NULL; sink_at = g_list_next(sink_at)) {
3378 GString *filter_name = filter_prev->data;
3379 GString *sink_name = sink_at->data;
3380
3381 ret = append_connect_arg(run_args, filter_name->str,
3382 sink_name->str);
3383 if (ret) {
3384 goto error;
3385 }
3386 }
3387
3388 goto end;
3389
3390error:
3391 ret = -1;
3392
3393end:
3394 return ret;
3395}
3396
3397static
3398int split_timerange(const char *arg, char **begin, char **end)
3399{
3400 int ret = 0;
3401 const char *ch = arg;
3402 size_t end_pos;
3403 GString *g_begin = NULL;
3404 GString *g_end = NULL;
3405
3406 assert(arg);
3407
3408 if (*ch == '[') {
3409 ch++;
3410 }
3411
3412 g_begin = bt_common_string_until(ch, "", ",", &end_pos);
3413 if (!g_begin || ch[end_pos] != ',' || g_begin->len == 0) {
3414 goto error;
3415 }
3416
3417 ch += end_pos + 1;
3418
3419 g_end = bt_common_string_until(ch, "", "]", &end_pos);
3420 if (!g_end || g_end->len == 0) {
3421 goto error;
3422 }
3423
3424 assert(begin);
3425 assert(end);
3426 *begin = g_begin->str;
3427 *end = g_end->str;
3428 g_string_free(g_begin, FALSE);
3429 g_string_free(g_end, FALSE);
3430 g_begin = NULL;
3431 g_end = NULL;
3432 goto end;
3433
3434error:
3435 ret = -1;
3436
3437end:
3438 if (g_begin) {
3439 g_string_free(g_begin, TRUE);
3440 }
3441
3442 if (g_end) {
3443 g_string_free(g_end, TRUE);
3444 }
3445
3446 return ret;
3447}
3448
3449static
3450int g_list_prepend_gstring(GList **list, const char *string)
3451{
3452 int ret = 0;
3453 GString *gs = g_string_new(string);
3454
3455 assert(list);
3456
3457 if (!gs) {
3458 print_err_oom();
3459 goto end;
3460 }
3461
3462 *list = g_list_prepend(*list, gs);
3463
3464end:
3465 return ret;
3466}
3467
94023a1c
PP
3468static
3469struct implicit_component_args *create_implicit_component_args(void)
3470{
3471 struct implicit_component_args *impl_args =
3472 g_new0(struct implicit_component_args, 1);
3473
3474 if (!impl_args) {
3475 goto end;
3476 }
3477
3478 if (init_implicit_component_args(impl_args, NULL, true)) {
3479 destroy_implicit_component_args(impl_args);
3480 impl_args = NULL;
3481 goto end;
3482 }
3483
3484end:
3485 return impl_args;
3486}
3487
3488static
3489int fill_implicit_ctf_inputs_args(GPtrArray *implicit_ctf_inputs_args,
3490 struct implicit_component_args *base_implicit_ctf_input_args,
3491 GList *leftovers)
3492{
3493 int ret = 0;
3494 GList *leftover;
3495
3496 for (leftover = leftovers; leftover != NULL;
3497 leftover = g_list_next(leftover)) {
3498 GString *gs_leftover = leftover->data;
3499 struct implicit_component_args *impl_args =
3500 create_implicit_component_args();
3501
3502 if (!impl_args) {
3503 print_err_oom();
3504 goto error;
3505 }
3506
3507 impl_args->exists = true;
3508 g_string_assign(impl_args->comp_arg,
3509 base_implicit_ctf_input_args->comp_arg->str);
3510 g_string_assign(impl_args->params_arg,
3511 base_implicit_ctf_input_args->params_arg->str);
3512
3513 /*
3514 * We need our own copy of the extra parameters because
3515 * this is where the unique path goes.
3516 */
3517 BT_PUT(impl_args->extra_params);
3518 impl_args->extra_params =
3519 bt_value_copy(base_implicit_ctf_input_args->extra_params);
d7771b22 3520 if (!impl_args->extra_params) {
94023a1c
PP
3521 print_err_oom();
3522 destroy_implicit_component_args(impl_args);
3523 goto error;
3524 }
3525
3526 /* Append unique path parameter */
3527 ret = append_implicit_component_extra_param(impl_args,
3528 "path", gs_leftover->str);
3529 if (ret) {
3530 destroy_implicit_component_args(impl_args);
3531 goto error;
3532 }
3533
3534 g_ptr_array_add(implicit_ctf_inputs_args, impl_args);
3535 }
3536
3537 goto end;
3538
3539error:
3540 ret = -1;
3541
3542end:
3543 return ret;
3544}
3545
9009cc24
PP
3546/*
3547 * Creates a Babeltrace config object from the arguments of a convert
3548 * command.
3549 *
3550 * *retcode is set to the appropriate exit code to use.
3551 */
3552static
3553struct bt_config *bt_config_convert_from_args(int argc, const char *argv[],
3554 int *retcode, bool force_omit_system_plugin_path,
8e765000 3555 bool force_omit_home_plugin_path,
9e503aa9 3556 struct bt_value *initial_plugin_paths, char *log_level)
9009cc24
PP
3557{
3558 poptContext pc = NULL;
3559 char *arg = NULL;
0a011c88
JG
3560 enum bt_config_component_dest cur_comp_dest =
3561 BT_CONFIG_COMPONENT_DEST_UNKNOWN;
9009cc24
PP
3562 int opt, ret = 0;
3563 struct bt_config *cfg = NULL;
9009cc24
PP
3564 bool got_input_format_opt = false;
3565 bool got_output_format_opt = false;
3566 bool trimmer_has_begin = false;
3567 bool trimmer_has_end = false;
b5fe3e00 3568 bool stream_intersection_mode = false;
9009cc24
PP
3569 GString *cur_name = NULL;
3570 GString *cur_name_prefix = NULL;
3571 const char *leftover = NULL;
3572 bool print_run_args = false;
3573 bool print_run_args_0 = false;
3574 bool print_ctf_metadata = false;
3575 struct bt_value *run_args = NULL;
3576 struct bt_value *all_names = NULL;
3577 GList *source_names = NULL;
3578 GList *filter_names = NULL;
3579 GList *sink_names = NULL;
94023a1c
PP
3580 GList *leftovers = NULL;
3581 GPtrArray *implicit_ctf_inputs_args = NULL;
3582 struct implicit_component_args base_implicit_ctf_input_args = { 0 };
e7ad156c 3583 struct implicit_component_args implicit_ctf_output_args = { 0 };
9009cc24
PP
3584 struct implicit_component_args implicit_lttng_live_args = { 0 };
3585 struct implicit_component_args implicit_dummy_args = { 0 };
3586 struct implicit_component_args implicit_text_args = { 0 };
3587 struct implicit_component_args implicit_debug_info_args = { 0 };
3588 struct implicit_component_args implicit_muxer_args = { 0 };
3589 struct implicit_component_args implicit_trimmer_args = { 0 };
3590 struct bt_value *plugin_paths = bt_get(initial_plugin_paths);
3591 char error_buf[256] = { 0 };
3592 size_t i;
3593 struct bt_common_lttng_live_url_parts lttng_live_url_parts = { 0 };
e7ad156c 3594 char *output = NULL;
9009cc24
PP
3595
3596 *retcode = 0;
3597
3598 if (argc <= 1) {
3599 print_convert_usage(stdout);
3600 *retcode = -1;
3601 goto end;
3602 }
3603
94023a1c 3604 if (init_implicit_component_args(&base_implicit_ctf_input_args,
fd5f8053 3605 "source.ctf.fs", false)) {
9009cc24
PP
3606 goto error;
3607 }
3608
e7ad156c
PP
3609 if (init_implicit_component_args(&implicit_ctf_output_args,
3610 "sink.ctf.fs", false)) {
3611 goto error;
3612 }
3613
9009cc24 3614 if (init_implicit_component_args(&implicit_lttng_live_args,
fd5f8053 3615 "source.ctf.lttng-live", false)) {
9009cc24
PP
3616 goto error;
3617 }
3618
fd5f8053
PP
3619 if (init_implicit_component_args(&implicit_text_args,
3620 "sink.text.pretty", false)) {
9009cc24
PP
3621 goto error;
3622 }
3623
fd5f8053
PP
3624 if (init_implicit_component_args(&implicit_dummy_args,
3625 "sink.utils.dummy", false)) {
9009cc24
PP
3626 goto error;
3627 }
3628
3629 if (init_implicit_component_args(&implicit_debug_info_args,
8e765000 3630 "filter.lttng-utils.debug-info", false)) {
9009cc24
PP
3631 goto error;
3632 }
3633
fd5f8053
PP
3634 if (init_implicit_component_args(&implicit_muxer_args,
3635 "filter.utils.muxer", true)) {
9009cc24
PP
3636 goto error;
3637 }
3638
3639 if (init_implicit_component_args(&implicit_trimmer_args,
fd5f8053 3640 "filter.utils.trimmer", false)) {
9009cc24
PP
3641 goto error;
3642 }
3643
94023a1c
PP
3644 implicit_ctf_inputs_args = g_ptr_array_new_with_free_func(
3645 (GDestroyNotify) destroy_implicit_component_args);
3646 if (!implicit_ctf_inputs_args) {
3647 print_err_oom();
3648 goto error;
3649 }
3650
9009cc24
PP
3651 all_names = bt_value_map_create();
3652 if (!all_names) {
3653 print_err_oom();
3654 goto error;
3655 }
3656
3657 run_args = bt_value_array_create();
3658 if (!run_args) {
3659 print_err_oom();
3660 goto error;
3661 }
3662
3663 cur_name = g_string_new(NULL);
3664 if (!cur_name) {
3665 print_err_oom();
3666 goto error;
3667 }
3668
3669 cur_name_prefix = g_string_new(NULL);
3670 if (!cur_name_prefix) {
3671 print_err_oom();
3672 goto error;
3673 }
3674
3675 ret = append_env_var_plugin_paths(plugin_paths);
3676 if (ret) {
3677 goto error;
3678 }
3679
3680 /*
3681 * First pass: collect all arguments which need to be passed
3682 * as is to the run command. This pass can also add --name
3683 * arguments if needed to automatically name unnamed component
3684 * instances. Also it does the following transformations:
3685 *
3686 * --path=PATH -> --key path --value PATH
3687 * --url=URL -> --key url --value URL
3688 *
3689 * Also it appends the plugin paths of --plugin-path to
3690 * `plugin_paths`.
3691 */
3692 pc = poptGetContext(NULL, argc, (const char **) argv,
3693 convert_long_options, 0);
3694 if (!pc) {
3695 printf_err("Cannot get popt context\n");
3696 goto error;
3697 }
3698
3699 poptReadDefaultConfig(pc, 0);
3700
3701 while ((opt = poptGetNextOpt(pc)) > 0) {
3702 char *name = NULL;
3703 char *plugin_name = NULL;
3704 char *comp_cls_name = NULL;
3705
3706 arg = poptGetOptArg(pc);
3707
3708 switch (opt) {
fd5f8053
PP
3709 case OPT_COMPONENT:
3710 {
3711 enum bt_component_class_type type;
3712 const char *type_prefix;
3713
9009cc24
PP
3714 /* Append current component's name if needed */
3715 ret = convert_append_name_param(cur_comp_dest, cur_name,
3716 cur_name_prefix, run_args, all_names,
3717 &source_names, &filter_names, &sink_names);
3718 if (ret) {
3719 goto error;
3720 }
3721
3722 /* Parse the argument */
3723 plugin_comp_cls_names(arg, &name, &plugin_name,
fd5f8053 3724 &comp_cls_name, &type);
9009cc24 3725 if (!plugin_name || !comp_cls_name) {
fd5f8053 3726 printf_err("Invalid format for --component option's argument:\n %s\n",
9009cc24
PP
3727 arg);
3728 goto error;
3729 }
3730
3731 if (name) {
3732 g_string_assign(cur_name, name);
3733 } else {
3734 g_string_assign(cur_name, "");
3735 }
3736
fd5f8053
PP
3737 switch (type) {
3738 case BT_COMPONENT_CLASS_TYPE_SOURCE:
3739 cur_comp_dest = BT_CONFIG_COMPONENT_DEST_SOURCE;
3740 type_prefix = "source";
9009cc24 3741 break;
fd5f8053
PP
3742 case BT_COMPONENT_CLASS_TYPE_FILTER:
3743 cur_comp_dest = BT_CONFIG_COMPONENT_DEST_FILTER;
3744 type_prefix = "filter";
9009cc24 3745 break;
fd5f8053
PP
3746 case BT_COMPONENT_CLASS_TYPE_SINK:
3747 cur_comp_dest = BT_CONFIG_COMPONENT_DEST_SINK;
3748 type_prefix = "sink";
9009cc24
PP
3749 break;
3750 default:
0fbb9a9f 3751 abort();
9009cc24
PP
3752 }
3753
fd5f8053
PP
3754 if (bt_value_array_append_string(run_args,
3755 "--component")) {
3756 print_err_oom();
3757 goto error;
3758 }
3759
9009cc24
PP
3760 if (bt_value_array_append_string(run_args, arg)) {
3761 print_err_oom();
3762 goto error;
3763 }
3764
3765 g_string_assign(cur_name_prefix, "");
fd5f8053
PP
3766 g_string_append_printf(cur_name_prefix, "%s.%s.%s",
3767 type_prefix, plugin_name, comp_cls_name);
9009cc24
PP
3768 free(name);
3769 free(plugin_name);
3770 free(comp_cls_name);
3771 name = NULL;
3772 plugin_name = NULL;
3773 comp_cls_name = NULL;
3774 break;
fd5f8053 3775 }
9009cc24
PP
3776 case OPT_PARAMS:
3777 if (cur_name_prefix->len == 0) {
3778 printf_err("No current component of which to set parameters:\n %s\n",
3779 arg);
3780 goto error;
3781 }
3782
3783 if (bt_value_array_append_string(run_args,
3784 "--params")) {
3785 print_err_oom();
3786 goto error;
3787 }
3788
3789 if (bt_value_array_append_string(run_args, arg)) {
3790 print_err_oom();
3791 goto error;
3792 }
3793 break;
3794 case OPT_PATH:
3795 if (cur_name_prefix->len == 0) {
3796 printf_err("No current component of which to set `path` parameter:\n %s\n",
3797 arg);
3798 goto error;
3799 }
3800
3801 if (bt_value_array_append_string(run_args, "--key")) {
3802 print_err_oom();
3803 goto error;
3804 }
3805
3806 if (bt_value_array_append_string(run_args, "path")) {
3807 print_err_oom();
3808 goto error;
3809 }
3810
3811 if (bt_value_array_append_string(run_args, "--value")) {
3812 print_err_oom();
3813 goto error;
3814 }
3815
3816 if (bt_value_array_append_string(run_args, arg)) {
3817 print_err_oom();
3818 goto error;
3819 }
3820 break;
3821 case OPT_URL:
3822 if (cur_name_prefix->len == 0) {
3823 printf_err("No current component of which to set `url` parameter:\n %s\n",
3824 arg);
3825 goto error;
3826 }
3827
3828 if (bt_value_array_append_string(run_args, "--key")) {
3829 print_err_oom();
3830 goto error;
3831 }
3832
3833 if (bt_value_array_append_string(run_args, "url")) {
3834 print_err_oom();
3835 goto error;
3836 }
3837
3838 if (bt_value_array_append_string(run_args, "--value")) {
3839 print_err_oom();
3840 goto error;
3841 }
3842
3843 if (bt_value_array_append_string(run_args, arg)) {
3844 print_err_oom();
3845 goto error;
3846 }
3847 break;
3848 case OPT_NAME:
3849 if (cur_name_prefix->len == 0) {
3850 printf_err("No current component to name:\n %s\n",
3851 arg);
3852 goto error;
3853 }
3854
3855 if (bt_value_array_append_string(run_args, "--name")) {
3856 print_err_oom();
3857 goto error;
3858 }
3859
3860 if (bt_value_array_append_string(run_args, arg)) {
3861 print_err_oom();
3862 goto error;
3863 }
3864
3865 g_string_assign(cur_name, arg);
3866 break;
3867 case OPT_OMIT_HOME_PLUGIN_PATH:
3868 force_omit_home_plugin_path = true;
3869
3870 if (bt_value_array_append_string(run_args,
3871 "--omit-home-plugin-path")) {
3872 print_err_oom();
3873 goto error;
3874 }
3875 break;
3876 case OPT_RETRY_DURATION:
3877 if (bt_value_array_append_string(run_args,
3878 "--retry-duration")) {
3879 print_err_oom();
3880 goto error;
3881 }
3882
3883 if (bt_value_array_append_string(run_args, arg)) {
3884 print_err_oom();
3885 goto error;
3886 }
3887 break;
3888 case OPT_OMIT_SYSTEM_PLUGIN_PATH:
3889 force_omit_system_plugin_path = true;
3890
3891 if (bt_value_array_append_string(run_args,
3892 "--omit-system-plugin-path")) {
3893 print_err_oom();
3894 goto error;
3895 }
3896 break;
3897 case OPT_PLUGIN_PATH:
3898 if (bt_config_append_plugin_paths_check_setuid_setgid(
3899 plugin_paths, arg)) {
3900 goto error;
3901 }
3902
3903 if (bt_value_array_append_string(run_args,
3904 "--plugin-path")) {
3905 print_err_oom();
3906 goto error;
3907 }
3908
3909 if (bt_value_array_append_string(run_args, arg)) {
3910 print_err_oom();
3911 goto error;
3912 }
3913 break;
3914 case OPT_HELP:
3915 print_convert_usage(stdout);
3916 *retcode = -1;
3917 BT_PUT(cfg);
3918 goto end;
3919 case OPT_BEGIN:
3920 case OPT_CLOCK_CYCLES:
3921 case OPT_CLOCK_DATE:
3922 case OPT_CLOCK_FORCE_CORRELATE:
3923 case OPT_CLOCK_GMT:
3924 case OPT_CLOCK_OFFSET:
3925 case OPT_CLOCK_OFFSET_NS:
3926 case OPT_CLOCK_SECONDS:
3927 case OPT_COLOR:
3928 case OPT_DEBUG:
8e765000 3929 case OPT_DEBUG_INFO:
9009cc24
PP
3930 case OPT_DEBUG_INFO_DIR:
3931 case OPT_DEBUG_INFO_FULL_PATH:
3932 case OPT_DEBUG_INFO_TARGET_PREFIX:
3933 case OPT_END:
3934 case OPT_FIELDS:
3935 case OPT_INPUT_FORMAT:
3936 case OPT_NAMES:
9009cc24
PP
3937 case OPT_NO_DELTA:
3938 case OPT_OUTPUT_FORMAT:
3939 case OPT_OUTPUT:
3940 case OPT_RUN_ARGS:
3941 case OPT_RUN_ARGS_0:
3942 case OPT_STREAM_INTERSECTION:
3943 case OPT_TIMERANGE:
3944 case OPT_VERBOSE:
3945 /* Ignore in this pass */
3946 break;
3947 default:
3948 printf_err("Unknown command-line option specified (option code %d)\n",
3949 opt);
3950 goto error;
3951 }
3952
3953 free(arg);
3954 arg = NULL;
3955 }
3956
3957 /* Append current component's name if needed */
3958 ret = convert_append_name_param(cur_comp_dest, cur_name,
3959 cur_name_prefix, run_args, all_names, &source_names,
3960 &filter_names, &sink_names);
3961 if (ret) {
3962 goto error;
3963 }
3964
3965 /* Check for option parsing error */
3966 if (opt < -1) {
3967 printf_err("While parsing command-line options, at option %s: %s\n",
3968 poptBadOption(pc, 0), poptStrerror(opt));
3969 goto error;
3970 }
3971
3972 poptFreeContext(pc);
3973 free(arg);
3974 arg = NULL;
3975
3976 /*
3977 * Second pass: transform the convert-specific options and
3978 * arguments into implicit component instances for the run
3979 * command.
3980 */
3981 pc = poptGetContext(NULL, argc, (const char **) argv,
3982 convert_long_options, 0);
3983 if (!pc) {
3984 printf_err("Cannot get popt context\n");
3985 goto error;
3986 }
3987
3988 poptReadDefaultConfig(pc, 0);
3989
3990 while ((opt = poptGetNextOpt(pc)) > 0) {
3991 arg = poptGetOptArg(pc);
3992
3993 switch (opt) {
3994 case OPT_BEGIN:
3995 if (trimmer_has_begin) {
3996 printf("At --begin option: --begin or --timerange option already specified\n %s\n",
3997 arg);
3998 goto error;
3999 }
4000
4001 trimmer_has_begin = true;
e7ad156c 4002 ret = append_implicit_component_extra_param(
9009cc24
PP
4003 &implicit_trimmer_args, "begin", arg);
4004 implicit_trimmer_args.exists = true;
4005 if (ret) {
4006 goto error;
4007 }
4008 break;
4009 case OPT_END:
4010 if (trimmer_has_end) {
4011 printf("At --end option: --end or --timerange option already specified\n %s\n",
4012 arg);
4013 goto error;
4014 }
4015
4016 trimmer_has_end = true;
e7ad156c 4017 ret = append_implicit_component_extra_param(
9009cc24
PP
4018 &implicit_trimmer_args, "end", arg);
4019 implicit_trimmer_args.exists = true;
4020 if (ret) {
4021 goto error;
4022 }
4023 break;
4024 case OPT_TIMERANGE:
4025 {
4026 char *begin;
4027 char *end;
4028
4029 if (trimmer_has_begin || trimmer_has_end) {
4030 printf("At --timerange option: --begin, --end, or --timerange option already specified\n %s\n",
4031 arg);
4032 goto error;
4033 }
4034
4035 ret = split_timerange(arg, &begin, &end);
4036 if (ret) {
4037 printf_err("Invalid --timerange option's argument: expecting BEGIN,END or [BEGIN,END]:\n %s\n",
4038 arg);
4039 goto error;
4040 }
4041
e7ad156c 4042 ret = append_implicit_component_extra_param(
9009cc24 4043 &implicit_trimmer_args, "begin", begin);
e7ad156c 4044 ret |= append_implicit_component_extra_param(
9009cc24
PP
4045 &implicit_trimmer_args, "end", end);
4046 implicit_trimmer_args.exists = true;
4047 free(begin);
4048 free(end);
4049 if (ret) {
4050 goto error;
4051 }
4052 break;
4053 }
4054 case OPT_CLOCK_CYCLES:
4055 append_implicit_component_param(
4056 &implicit_text_args, "clock-cycles", "yes");
4057 implicit_text_args.exists = true;
4058 break;
4059 case OPT_CLOCK_DATE:
4060 append_implicit_component_param(
4061 &implicit_text_args, "clock-date", "yes");
4062 implicit_text_args.exists = true;
4063 break;
4064 case OPT_CLOCK_FORCE_CORRELATE:
4065 append_implicit_component_param(
53ac3428
PP
4066 &implicit_muxer_args,
4067 "assume-absolute-clock-classes", "yes");
9009cc24
PP
4068 break;
4069 case OPT_CLOCK_GMT:
4070 append_implicit_component_param(
4071 &implicit_text_args, "clock-gmt", "yes");
af47368e
JD
4072 append_implicit_component_param(
4073 &implicit_trimmer_args, "clock-gmt", "yes");
9009cc24
PP
4074 implicit_text_args.exists = true;
4075 break;
4076 case OPT_CLOCK_OFFSET:
94023a1c 4077 base_implicit_ctf_input_args.exists = true;
53ac3428 4078 append_implicit_component_param(
7be7e609 4079 &base_implicit_ctf_input_args,
53ac3428 4080 "clock-class-offset-s", arg);
9009cc24
PP
4081 break;
4082 case OPT_CLOCK_OFFSET_NS:
94023a1c 4083 base_implicit_ctf_input_args.exists = true;
7be7e609
PP
4084 append_implicit_component_param(
4085 &base_implicit_ctf_input_args,
4086 "clock-class-offset-ns", arg);
9009cc24
PP
4087 break;
4088 case OPT_CLOCK_SECONDS:
4089 append_implicit_component_param(
4090 &implicit_text_args, "clock-seconds", "yes");
4091 implicit_text_args.exists = true;
4092 break;
4093 case OPT_COLOR:
9009cc24 4094 implicit_text_args.exists = true;
e7ad156c
PP
4095 ret = append_implicit_component_extra_param(
4096 &implicit_text_args, "color", arg);
9009cc24
PP
4097 if (ret) {
4098 goto error;
4099 }
4100 break;
8e765000
PP
4101 case OPT_DEBUG_INFO:
4102 implicit_debug_info_args.exists = true;
9009cc24
PP
4103 break;
4104 case OPT_DEBUG_INFO_DIR:
e7ad156c
PP
4105 implicit_debug_info_args.exists = true;
4106 ret = append_implicit_component_extra_param(
9009cc24
PP
4107 &implicit_debug_info_args, "dir", arg);
4108 if (ret) {
4109 goto error;
4110 }
4111 break;
4112 case OPT_DEBUG_INFO_FULL_PATH:
e7ad156c 4113 implicit_debug_info_args.exists = true;
9009cc24
PP
4114 append_implicit_component_param(
4115 &implicit_debug_info_args, "full-path", "yes");
4116 break;
4117 case OPT_DEBUG_INFO_TARGET_PREFIX:
e7ad156c
PP
4118 implicit_debug_info_args.exists = true;
4119 ret = append_implicit_component_extra_param(
9009cc24
PP
4120 &implicit_debug_info_args,
4121 "target-prefix", arg);
4122 if (ret) {
4123 goto error;
4124 }
4125 break;
4126 case OPT_FIELDS:
4127 {
4128 struct bt_value *fields = fields_from_arg(arg);
4129
4130 if (!fields) {
4131 goto error;
4132 }
4133
e7ad156c 4134 implicit_text_args.exists = true;
9009cc24
PP
4135 ret = insert_flat_params_from_array(
4136 implicit_text_args.params_arg,
4137 fields, "field");
4138 bt_put(fields);
4139 if (ret) {
4140 goto error;
4141 }
4142 break;
4143 }
4144 case OPT_NAMES:
4145 {
4146 struct bt_value *names = names_from_arg(arg);
4147
4148 if (!names) {
4149 goto error;
4150 }
4151
e7ad156c 4152 implicit_text_args.exists = true;
9009cc24
PP
4153 ret = insert_flat_params_from_array(
4154 implicit_text_args.params_arg,
4155 names, "name");
4156 bt_put(names);
4157 if (ret) {
4158 goto error;
4159 }
4160 break;
4161 }
4162 case OPT_NO_DELTA:
4163 append_implicit_component_param(
4164 &implicit_text_args, "no-delta", "yes");
4165 implicit_text_args.exists = true;
4166 break;
4167 case OPT_INPUT_FORMAT:
4168 if (got_input_format_opt) {
4169 printf_err("Duplicate --input-format option\n");
4170 goto error;
4171 }
4172
4173 got_input_format_opt = true;
4174
4175 if (strcmp(arg, "ctf") == 0) {
94023a1c 4176 base_implicit_ctf_input_args.exists = true;
9009cc24
PP
4177 } else if (strcmp(arg, "lttng-live") == 0) {
4178 implicit_lttng_live_args.exists = true;
4179 } else {
4180 printf_err("Unknown legacy input format:\n %s\n",
4181 arg);
4182 goto error;
4183 }
4184 break;
4185 case OPT_OUTPUT_FORMAT:
4186 if (got_output_format_opt) {
4187 printf_err("Duplicate --output-format option\n");
4188 goto error;
4189 }
4190
4191 got_output_format_opt = true;
4192
4193 if (strcmp(arg, "text") == 0) {
4194 implicit_text_args.exists = true;
e7ad156c
PP
4195 } else if (strcmp(arg, "ctf") == 0) {
4196 implicit_ctf_output_args.exists = true;
9009cc24
PP
4197 } else if (strcmp(arg, "dummy") == 0) {
4198 implicit_dummy_args.exists = true;
4199 } else if (strcmp(arg, "ctf-metadata") == 0) {
4200 print_ctf_metadata = true;
4201 } else {
4202 printf_err("Unknown legacy output format:\n %s\n",
4203 arg);
4204 goto error;
4205 }
4206 break;
4207 case OPT_OUTPUT:
e7ad156c
PP
4208 if (output) {
4209 printf_err("Duplicate --output option\n");
4210 goto error;
4211 }
4212
4213 output = strdup(arg);
4214 if (!output) {
4215 print_err_oom();
9009cc24
PP
4216 goto error;
4217 }
4218 break;
4219 case OPT_RUN_ARGS:
4220 if (print_run_args_0) {
4221 printf_err("Cannot specify --run-args and --run-args-0\n");
4222 goto error;
4223 }
4224
4225 print_run_args = true;
4226 break;
4227 case OPT_RUN_ARGS_0:
4228 if (print_run_args) {
4229 printf_err("Cannot specify --run-args and --run-args-0\n");
4230 goto error;
4231 }
4232
4233 print_run_args_0 = true;
4234 break;
4235 case OPT_STREAM_INTERSECTION:
b5fe3e00
JG
4236 /*
4237 * Applies to all traces implementing the trace-info
4238 * query.
4239 */
4240 stream_intersection_mode = true;
9009cc24
PP
4241 break;
4242 case OPT_VERBOSE:
9e503aa9
PP
4243 if (*log_level != 'V' && *log_level != 'D') {
4244 *log_level = 'I';
9009cc24 4245 }
9009cc24
PP
4246 break;
4247 case OPT_DEBUG:
9e503aa9 4248 *log_level = 'V';
9009cc24
PP
4249 break;
4250 }
4251
4252 free(arg);
4253 arg = NULL;
4254 }
4255
4256 /* Check for option parsing error */
4257 if (opt < -1) {
4258 printf_err("While parsing command-line options, at option %s: %s\n",
4259 poptBadOption(pc, 0), poptStrerror(opt));
4260 goto error;
4261 }
4262
9e503aa9
PP
4263 /*
4264 * Legacy behaviour: --verbose used to make the `text` output
4265 * format print more information. --verbose is now equivalent to
4266 * the INFO log level, which is why we compare to 'I' here.
4267 */
4268 if (*log_level == 'I') {
4269 append_implicit_component_param(&implicit_text_args,
4270 "verbose", "yes");
4271 }
4272
9009cc24
PP
4273 /*
4274 * Append home and system plugin paths now that we possibly got
4275 * --plugin-path.
4276 */
4277 if (append_home_and_system_plugin_paths(plugin_paths,
4278 force_omit_system_plugin_path,
4279 force_omit_home_plugin_path)) {
4280 goto error;
4281 }
4282
94023a1c
PP
4283 /* Consume and keep leftover arguments */
4284 while ((leftover = poptGetArg(pc))) {
4285 GString *gs_leftover = g_string_new(leftover);
9009cc24 4286
94023a1c
PP
4287 if (!gs_leftover) {
4288 print_err_oom();
4289 goto error;
4290 }
4291
4292 leftovers = g_list_append(leftovers, gs_leftover);
4293 if (!leftovers) {
4294 g_string_free(gs_leftover, TRUE);
4295 print_err_oom();
4296 goto error;
4297 }
9009cc24
PP
4298 }
4299
4300 /* Print CTF metadata or print LTTng live sessions */
4301 if (print_ctf_metadata) {
94023a1c
PP
4302 GString *gs_leftover;
4303
4304 if (g_list_length(leftovers) == 0) {
9009cc24
PP
4305 printf_err("--output-format=ctf-metadata specified without a path\n");
4306 goto error;
4307 }
4308
94023a1c
PP
4309 if (g_list_length(leftovers) > 1) {
4310 printf_err("Too many paths specified for --output-format=ctf-metadata\n");
4311 goto error;
4312 }
4313
9009cc24
PP
4314 cfg = bt_config_print_ctf_metadata_create(plugin_paths);
4315 if (!cfg) {
4316 goto error;
4317 }
4318
94023a1c 4319 gs_leftover = leftovers->data;
9009cc24 4320 g_string_assign(cfg->cmd_data.print_ctf_metadata.path,
94023a1c 4321 gs_leftover->str);
9009cc24
PP
4322 goto end;
4323 }
4324
4325 /*
e7ad156c
PP
4326 * If -o ctf was specified, make sure an output path (--output)
4327 * was also specified. --output does not imply -o ctf because
4328 * it's also used for the default, implicit -o text if -o ctf
4329 * is not specified.
4330 */
4331 if (implicit_ctf_output_args.exists) {
4332 if (!output) {
4333 printf_err("--output-format=ctf specified without --output (trace output path)\n");
4334 goto error;
4335 }
4336
4337 /*
4338 * At this point we know that -o ctf AND --output were
4339 * specified. Make sure that no options were specified
4340 * which would imply -o text because --output would be
4341 * ambiguous in this case. For example, this is wrong:
4342 *
4343 * babeltrace --names=all -o ctf --output=/tmp/path my-trace
4344 *
4345 * because --names=all implies -o text, and --output
4346 * could apply to both the sink.text.pretty and
4347 * sink.ctf.fs implicit components.
4348 */
4349 if (implicit_text_args.exists) {
4350 printf_err("Ambiguous --output option: --output-format=ctf specified but another option implies --output-format=text\n");
4351 goto error;
4352 }
4353 }
4354
4355 /*
4356 * If -o dummy and -o ctf were not specified, and if there are
4357 * no explicit sink components, then use an implicit
4358 * `sink.text.pretty` component.
9009cc24 4359 */
e7ad156c
PP
4360 if (!implicit_dummy_args.exists && !implicit_ctf_output_args.exists &&
4361 !sink_names) {
9009cc24
PP
4362 implicit_text_args.exists = true;
4363 }
4364
e7ad156c
PP
4365 /*
4366 * Set implicit `sink.text.pretty` or `sink.ctf.fs` component's
4367 * `path` parameter if --output was specified.
4368 */
4369 if (output) {
4370 if (implicit_text_args.exists) {
4371 append_implicit_component_extra_param(&implicit_text_args,
4372 "path", output);
4373 } else if (implicit_ctf_output_args.exists) {
4374 append_implicit_component_extra_param(&implicit_ctf_output_args,
4375 "path", output);
4376 }
4377 }
4378
94023a1c
PP
4379 /* Decide where the leftover argument(s) go */
4380 if (g_list_length(leftovers) > 0) {
9009cc24 4381 if (implicit_lttng_live_args.exists) {
94023a1c
PP
4382 GString *gs_leftover;
4383
4384 if (g_list_length(leftovers) > 1) {
4385 printf_err("Too many URLs specified for --output-format=lttng-live\n");
4386 goto error;
4387 }
4388
4389 gs_leftover = leftovers->data;
9009cc24 4390 lttng_live_url_parts =
94023a1c 4391 bt_common_parse_lttng_live_url(gs_leftover->str,
94b828f3 4392 error_buf, sizeof(error_buf));
9009cc24
PP
4393 if (!lttng_live_url_parts.proto) {
4394 printf_err("Invalid LTTng live URL format: %s\n",
4395 error_buf);
4396 goto error;
4397 }
4398
4399 if (!lttng_live_url_parts.session_name) {
4400 /* Print LTTng live sessions */
4401 cfg = bt_config_print_lttng_live_sessions_create(
4402 plugin_paths);
4403 if (!cfg) {
4404 goto error;
4405 }
4406
9009cc24 4407 g_string_assign(cfg->cmd_data.print_lttng_live_sessions.url,
94023a1c 4408 gs_leftover->str);
9009cc24
PP
4409 goto end;
4410 }
4411
e7ad156c 4412 ret = append_implicit_component_extra_param(
94023a1c
PP
4413 &implicit_lttng_live_args, "url",
4414 gs_leftover->str);
9009cc24
PP
4415 if (ret) {
4416 goto error;
4417 }
4418 } else {
94023a1c
PP
4419 /*
4420 * Append one implicit component argument set
4421 * for each leftover (souce.ctf.fs paths). Copy
4422 * the base implicit component arguments.
4423 * Note that they still have to be named later.
4424 */
4425 ret = fill_implicit_ctf_inputs_args(
4426 implicit_ctf_inputs_args,
4427 &base_implicit_ctf_input_args, leftovers);
9009cc24
PP
4428 if (ret) {
4429 goto error;
4430 }
4431 }
4432 }
4433
4434 /*
fd5f8053
PP
4435 * Ensure mutual exclusion between implicit `source.ctf.fs` and
4436 * `source.ctf.lttng-live` components.
9009cc24 4437 */
94023a1c
PP
4438 if (base_implicit_ctf_input_args.exists &&
4439 implicit_lttng_live_args.exists) {
fd5f8053 4440 printf_err("Cannot create both implicit `%s` and `%s` components\n",
94023a1c 4441 base_implicit_ctf_input_args.comp_arg->str,
fd5f8053 4442 implicit_lttng_live_args.comp_arg->str);
9009cc24
PP
4443 goto error;
4444 }
4445
4446 /*
fd5f8053 4447 * If the implicit `source.ctf.fs` or `source.ctf.lttng-live`
94023a1c
PP
4448 * components exists, make sure there's at least one leftover
4449 * (which is the path or URL).
9009cc24 4450 */
94023a1c
PP
4451 if (base_implicit_ctf_input_args.exists &&
4452 g_list_length(leftovers) == 0) {
fd5f8053 4453 printf_err("Missing path for implicit `%s` component\n",
94023a1c 4454 base_implicit_ctf_input_args.comp_arg->str);
9009cc24
PP
4455 goto error;
4456 }
4457
94023a1c 4458 if (implicit_lttng_live_args.exists && g_list_length(leftovers) == 0) {
fd5f8053
PP
4459 printf_err("Missing URL for implicit `%s` component\n",
4460 implicit_lttng_live_args.comp_arg->str);
9009cc24
PP
4461 goto error;
4462 }
4463
4464 /* Assign names to implicit components */
94023a1c
PP
4465 for (i = 0; i < implicit_ctf_inputs_args->len; i++) {
4466 struct implicit_component_args *impl_args =
4467 g_ptr_array_index(implicit_ctf_inputs_args, i);
4468
4469 ret = assign_name_to_implicit_component(impl_args,
4470 "source-ctf-fs", all_names, &source_names, true);
4471 if (ret) {
4472 goto error;
4473 }
9009cc24
PP
4474 }
4475
4476 ret = assign_name_to_implicit_component(&implicit_lttng_live_args,
4477 "lttng-live", all_names, &source_names, true);
4478 if (ret) {
4479 goto error;
4480 }
4481
4482 ret = assign_name_to_implicit_component(&implicit_text_args,
4483 "pretty", all_names, &sink_names, true);
4484 if (ret) {
4485 goto error;
4486 }
4487
e7ad156c
PP
4488 ret = assign_name_to_implicit_component(&implicit_ctf_output_args,
4489 "sink-ctf-fs", all_names, &sink_names, true);
4490 if (ret) {
4491 goto error;
4492 }
4493
9009cc24
PP
4494 ret = assign_name_to_implicit_component(&implicit_dummy_args,
4495 "dummy", all_names, &sink_names, true);
4496 if (ret) {
4497 goto error;
4498 }
4499
4500 ret = assign_name_to_implicit_component(&implicit_muxer_args,
4501 "muxer", all_names, NULL, false);
4502 if (ret) {
4503 goto error;
4504 }
4505
4506 ret = assign_name_to_implicit_component(&implicit_trimmer_args,
4507 "trimmer", all_names, NULL, false);
4508 if (ret) {
4509 goto error;
4510 }
4511
4512 ret = assign_name_to_implicit_component(&implicit_debug_info_args,
4513 "debug-info", all_names, NULL, false);
4514 if (ret) {
4515 goto error;
4516 }
4517
4518 /* Make sure there's at least one source and one sink */
4519 if (!source_names) {
4520 printf_err("No source component\n");
4521 goto error;
4522 }
4523
4524 if (!sink_names) {
4525 printf_err("No sink component\n");
4526 goto error;
4527 }
4528
4529 /*
4530 * Prepend the muxer, the trimmer, and the debug info to the
4531 * filter chain so that we have:
4532 *
4533 * sources -> muxer -> [trimmer] -> [debug info] ->
4534 * [user filters] -> sinks
4535 */
4536 if (implicit_debug_info_args.exists) {
4537 if (g_list_prepend_gstring(&filter_names,
4538 implicit_debug_info_args.name_arg->str)) {
4539 goto error;
4540 }
4541 }
4542
4543 if (implicit_trimmer_args.exists) {
4544 if (g_list_prepend_gstring(&filter_names,
4545 implicit_trimmer_args.name_arg->str)) {
4546 goto error;
4547 }
4548 }
4549
4550 if (g_list_prepend_gstring(&filter_names,
4551 implicit_muxer_args.name_arg->str)) {
4552 goto error;
4553 }
4554
4555 /*
4556 * Append the equivalent run arguments for the implicit
4557 * components.
4558 */
94023a1c
PP
4559 for (i = 0; i < implicit_ctf_inputs_args->len; i++) {
4560 struct implicit_component_args *impl_args =
4561 g_ptr_array_index(implicit_ctf_inputs_args, i);
4562
4563 ret = append_run_args_for_implicit_component(impl_args,
4564 run_args);
4565 if (ret) {
4566 goto error;
4567 }
9009cc24
PP
4568 }
4569
fd5f8053 4570 ret = append_run_args_for_implicit_component(&implicit_lttng_live_args,
9009cc24
PP
4571 run_args);
4572 if (ret) {
4573 goto error;
4574 }
4575
fd5f8053
PP
4576 ret = append_run_args_for_implicit_component(&implicit_text_args,
4577 run_args);
9009cc24
PP
4578 if (ret) {
4579 goto error;
4580 }
4581
e7ad156c
PP
4582 ret = append_run_args_for_implicit_component(&implicit_ctf_output_args,
4583 run_args);
4584 if (ret) {
4585 goto error;
4586 }
4587
fd5f8053
PP
4588 ret = append_run_args_for_implicit_component(&implicit_dummy_args,
4589 run_args);
9009cc24
PP
4590 if (ret) {
4591 goto error;
4592 }
4593
fd5f8053
PP
4594 ret = append_run_args_for_implicit_component(&implicit_muxer_args,
4595 run_args);
9009cc24
PP
4596 if (ret) {
4597 goto error;
4598 }
4599
fd5f8053 4600 ret = append_run_args_for_implicit_component(&implicit_trimmer_args,
9009cc24
PP
4601 run_args);
4602 if (ret) {
4603 goto error;
4604 }
4605
fd5f8053 4606 ret = append_run_args_for_implicit_component(&implicit_debug_info_args,
9009cc24
PP
4607 run_args);
4608 if (ret) {
4609 goto error;
4610 }
4611
4612 /* Auto-connect components */
4613 ret = convert_auto_connect(run_args, source_names, filter_names,
4614 sink_names);
4615 if (ret) {
4616 printf_err("Cannot auto-connect components\n");
4617 goto error;
4618 }
4619
4620 /*
4621 * We have all the run command arguments now. Depending on
4622 * --run-args, we pass this to the run command or print them
4623 * here.
4624 */
4625 if (print_run_args || print_run_args_0) {
f9692f14
PP
4626 if (stream_intersection_mode) {
4627 printf_err("Cannot specify --stream-intersection with --run-args or --run-args-0\n");
4628 goto error;
4629 }
4630
9009cc24
PP
4631 for (i = 0; i < bt_value_array_size(run_args); i++) {
4632 struct bt_value *arg_value =
4633 bt_value_array_get(run_args, i);
4634 const char *arg;
4635 GString *quoted = NULL;
4636 const char *arg_to_print;
4637
4638 assert(arg_value);
4639 ret = bt_value_string_get(arg_value, &arg);
4640 assert(ret == 0);
4641 BT_PUT(arg_value);
4642
4643 if (print_run_args) {
4644 quoted = bt_common_shell_quote(arg, true);
4645 if (!quoted) {
4646 goto error;
4647 }
4648
4649 arg_to_print = quoted->str;
4650 } else {
4651 arg_to_print = arg;
4652 }
4653
4654 printf("%s", arg_to_print);
4655
4656 if (quoted) {
4657 g_string_free(quoted, TRUE);
4658 }
4659
4660 if (i < bt_value_array_size(run_args) - 1) {
4661 if (print_run_args) {
4662 putchar(' ');
4663 } else {
4664 putchar('\0');
4665 }
4666 }
4667 }
4668
4669 *retcode = -1;
4670 BT_PUT(cfg);
4671 goto end;
4672 }
4673
4674 cfg = bt_config_run_from_args_array(run_args, retcode,
4675 force_omit_system_plugin_path, force_omit_home_plugin_path,
4676 initial_plugin_paths);
fc11b6a6
PP
4677 if (!cfg) {
4678 goto error;
4679 }
4680
b5fe3e00 4681 cfg->cmd_data.run.stream_intersection_mode = stream_intersection_mode;
9009cc24
PP
4682 goto end;
4683
4684error:
4685 *retcode = 1;
4686 BT_PUT(cfg);
4687
4688end:
4689 if (pc) {
4690 poptFreeContext(pc);
4691 }
4692
4693 free(arg);
e7ad156c 4694 free(output);
9009cc24
PP
4695
4696 if (cur_name) {
4697 g_string_free(cur_name, TRUE);
4698 }
4699
4700 if (cur_name_prefix) {
4701 g_string_free(cur_name_prefix, TRUE);
4702 }
4703
94023a1c
PP
4704 if (implicit_ctf_inputs_args) {
4705 g_ptr_array_free(implicit_ctf_inputs_args, TRUE);
4706 }
4707
9009cc24
PP
4708 bt_put(run_args);
4709 bt_put(all_names);
4710 destroy_glist_of_gstring(source_names);
4711 destroy_glist_of_gstring(filter_names);
4712 destroy_glist_of_gstring(sink_names);
94023a1c
PP
4713 destroy_glist_of_gstring(leftovers);
4714 finalize_implicit_component_args(&base_implicit_ctf_input_args);
4715 finalize_implicit_component_args(&implicit_ctf_output_args);
4716 finalize_implicit_component_args(&implicit_lttng_live_args);
4717 finalize_implicit_component_args(&implicit_dummy_args);
4718 finalize_implicit_component_args(&implicit_text_args);
4719 finalize_implicit_component_args(&implicit_debug_info_args);
4720 finalize_implicit_component_args(&implicit_muxer_args);
4721 finalize_implicit_component_args(&implicit_trimmer_args);
9009cc24
PP
4722 bt_put(plugin_paths);
4723 bt_common_destroy_lttng_live_url_parts(&lttng_live_url_parts);
4724 return cfg;
4725}
4726
4727/*
4728 * Prints the Babeltrace 2.x general usage.
4729 */
4730static
4731void print_gen_usage(FILE *fp)
4732{
4733 fprintf(fp, "Usage: babeltrace [GENERAL OPTIONS] [COMMAND] [COMMAND ARGUMENTS]\n");
4734 fprintf(fp, "\n");
4735 fprintf(fp, "General options:\n");
4736 fprintf(fp, "\n");
9e503aa9
PP
4737 fprintf(fp, " -d, --debug Enable debug mode (same as --log-level=V)\n");
4738 fprintf(fp, " -h, --help Show this help and quit\n");
4739 fprintf(fp, " --log-level=LVL Set all log levels to LVL (`N`, `V`, `D`,\n");
4740 fprintf(fp, " `I`, `W` (default), `E`, or `F`)\n");
4741 fprintf(fp, " -v, --verbose Enable verbose mode (same as --log-level=I)\n");
4742 fprintf(fp, " -V, --version Show version and quit\n");
9009cc24
PP
4743 fprintf(fp, "\n");
4744 fprintf(fp, "Available commands:\n");
4745 fprintf(fp, "\n");
4746 fprintf(fp, " convert Convert and trim traces (default)\n");
4747 fprintf(fp, " help Get help for a plugin or a component class\n");
4748 fprintf(fp, " list-plugins List available plugins and their content\n");
4749 fprintf(fp, " query Query objects from a component class\n");
4750 fprintf(fp, " run Build a processing graph and run it\n");
4751 fprintf(fp, "\n");
4752 fprintf(fp, "Use `babeltrace COMMAND --help` to show the help of COMMAND.\n");
4753}
4754
9e503aa9
PP
4755static
4756char log_level_from_arg(const char *arg)
4757{
4758 char level = 'U';
4759
4760 if (strcmp(arg, "VERBOSE") == 0 ||
4761 strcmp(arg, "V") == 0) {
4762 level = 'V';
4763 } else if (strcmp(arg, "DEBUG") == 0 ||
4764 strcmp(arg, "D") == 0) {
4765 level = 'D';
4766 } else if (strcmp(arg, "INFO") == 0 ||
4767 strcmp(arg, "I") == 0) {
4768 level = 'I';
4769 } else if (strcmp(arg, "WARN") == 0 ||
4770 strcmp(arg, "WARNING") == 0 ||
4771 strcmp(arg, "W") == 0) {
4772 level = 'W';
4773 } else if (strcmp(arg, "ERROR") == 0 ||
4774 strcmp(arg, "E") == 0) {
4775 level = 'E';
4776 } else if (strcmp(arg, "FATAL") == 0 ||
4777 strcmp(arg, "F") == 0) {
4778 level = 'F';
4779 } else if (strcmp(arg, "NONE") == 0 ||
4780 strcmp(arg, "N") == 0) {
4781 level = 'N';
4782 }
4783
4784 return level;
4785}
4786
9009cc24
PP
4787struct bt_config *bt_config_cli_args_create(int argc, const char *argv[],
4788 int *retcode, bool force_omit_system_plugin_path,
8e765000 4789 bool force_omit_home_plugin_path,
9009cc24
PP
4790 struct bt_value *initial_plugin_paths)
4791{
4792 struct bt_config *config = NULL;
9009cc24
PP
4793 int i;
4794 const char **command_argv = NULL;
4795 int command_argc = -1;
4796 const char *command_name = NULL;
9e503aa9 4797 char log_level = 'U';
9009cc24
PP
4798
4799 enum command_type {
4800 COMMAND_TYPE_NONE = -1,
4801 COMMAND_TYPE_RUN = 0,
4802 COMMAND_TYPE_CONVERT,
4803 COMMAND_TYPE_LIST_PLUGINS,
4804 COMMAND_TYPE_HELP,
4805 COMMAND_TYPE_QUERY,
4806 } command_type = COMMAND_TYPE_NONE;
4807
4808 *retcode = -1;
4809
4810 if (!initial_plugin_paths) {
4811 initial_plugin_paths = bt_value_array_create();
4812 if (!initial_plugin_paths) {
4813 *retcode = 1;
4814 goto end;
4815 }
4816 } else {
4817 bt_get(initial_plugin_paths);
4818 }
4819
4820 if (argc <= 1) {
329e245b
PP
4821 print_version();
4822 puts("");
9009cc24
PP
4823 print_gen_usage(stdout);
4824 goto end;
4825 }
4826
4827 for (i = 1; i < argc; i++) {
4828 const char *cur_arg = argv[i];
9e503aa9 4829 const char *next_arg = i == (argc - 1) ? NULL : argv[i + 1];
9009cc24
PP
4830
4831 if (strcmp(cur_arg, "-d") == 0 ||
4832 strcmp(cur_arg, "--debug") == 0) {
9e503aa9 4833 log_level = 'V';
9009cc24
PP
4834 } else if (strcmp(cur_arg, "-v") == 0 ||
4835 strcmp(cur_arg, "--verbose") == 0) {
9e503aa9
PP
4836 if (log_level != 'V' && log_level != 'D') {
4837 /*
4838 * Legacy: do not override a previous
4839 * --debug because --verbose and --debug
4840 * can be specified together (in this
4841 * case we want the lowest log level to
4842 * apply, VERBOSE).
4843 */
4844 log_level = 'I';
4845 }
4846 } else if (strcmp(cur_arg, "--log-level") == 0) {
4847 if (!next_arg) {
4848 printf_err("Missing log level value for --log-level option\n");
4849 *retcode = 1;
4850 goto end;
4851 }
4852
4853 log_level = log_level_from_arg(next_arg);
4854 if (log_level == 'U') {
4855 printf_err("Invalid argument for --log-level option:\n %s\n",
4856 next_arg);
4857 *retcode = 1;
4858 goto end;
4859 }
4860
4861 i++;
4862 } else if (strncmp(cur_arg, "--log-level=", 12) == 0) {
4863 const char *arg = &cur_arg[12];
4864
4865 log_level = log_level_from_arg(arg);
4866 if (log_level == 'U') {
4867 printf_err("Invalid argument for --log-level option:\n %s\n",
4868 arg);
4869 *retcode = 1;
4870 goto end;
4871 }
9009cc24
PP
4872 } else if (strcmp(cur_arg, "-V") == 0 ||
4873 strcmp(cur_arg, "--version") == 0) {
4874 print_version();
4875 goto end;
4876 } else if (strcmp(cur_arg, "-h") == 0 ||
4877 strcmp(cur_arg, "--help") == 0) {
4878 print_gen_usage(stdout);
4879 goto end;
4880 } else {
9009cc24
PP
4881 /*
4882 * First unknown argument: is it a known command
4883 * name?
4884 */
9e503aa9
PP
4885 command_argv = &argv[i];
4886 command_argc = argc - i;
4887
9009cc24
PP
4888 if (strcmp(cur_arg, "convert") == 0) {
4889 command_type = COMMAND_TYPE_CONVERT;
4890 } else if (strcmp(cur_arg, "list-plugins") == 0) {
4891 command_type = COMMAND_TYPE_LIST_PLUGINS;
4892 } else if (strcmp(cur_arg, "help") == 0) {
4893 command_type = COMMAND_TYPE_HELP;
4894 } else if (strcmp(cur_arg, "query") == 0) {
4895 command_type = COMMAND_TYPE_QUERY;
4896 } else if (strcmp(cur_arg, "run") == 0) {
4897 command_type = COMMAND_TYPE_RUN;
4898 } else {
4899 /*
4900 * Unknown argument, but not a known
9e503aa9
PP
4901 * command name: assume the default
4902 * `convert` command.
9009cc24
PP
4903 */
4904 command_type = COMMAND_TYPE_CONVERT;
9e503aa9
PP
4905 command_name = "convert";
4906 command_argv = &argv[i - 1];
4907 command_argc = argc - i + 1;
9009cc24
PP
4908 }
4909 break;
4910 }
4911 }
4912
4913 if (command_type == COMMAND_TYPE_NONE) {
4914 /*
4915 * We only got non-help, non-version general options
4916 * like --verbose and --debug, without any other
4917 * arguments, so we can't do anything useful: print the
4918 * usage and quit.
4919 */
4920 print_gen_usage(stdout);
4921 goto end;
4922 }
4923
4924 assert(command_argv);
4925 assert(command_argc >= 0);
4926
4927 switch (command_type) {
4928 case COMMAND_TYPE_RUN:
4929 config = bt_config_run_from_args(command_argc, command_argv,
4930 retcode, force_omit_system_plugin_path,
4931 force_omit_home_plugin_path, initial_plugin_paths);
4932 break;
4933 case COMMAND_TYPE_CONVERT:
4934 config = bt_config_convert_from_args(command_argc, command_argv,
4935 retcode, force_omit_system_plugin_path,
8e765000 4936 force_omit_home_plugin_path,
9e503aa9 4937 initial_plugin_paths, &log_level);
9009cc24
PP
4938 break;
4939 case COMMAND_TYPE_LIST_PLUGINS:
4940 config = bt_config_list_plugins_from_args(command_argc,
4941 command_argv, retcode, force_omit_system_plugin_path,
4942 force_omit_home_plugin_path, initial_plugin_paths);
4943 break;
4944 case COMMAND_TYPE_HELP:
4945 config = bt_config_help_from_args(command_argc,
4946 command_argv, retcode, force_omit_system_plugin_path,
4947 force_omit_home_plugin_path, initial_plugin_paths);
4948 break;
4949 case COMMAND_TYPE_QUERY:
4950 config = bt_config_query_from_args(command_argc,
4951 command_argv, retcode, force_omit_system_plugin_path,
4952 force_omit_home_plugin_path, initial_plugin_paths);
4953 break;
4954 default:
0fbb9a9f 4955 abort();
9009cc24
PP
4956 }
4957
4958 if (config) {
9e503aa9
PP
4959 if (log_level == 'U') {
4960 log_level = 'W';
9009cc24
PP
4961 }
4962
9e503aa9 4963 config->log_level = log_level;
9009cc24
PP
4964 config->command_name = command_name;
4965 }
4966
4967end:
4968 bt_put(initial_plugin_paths);
4969 return config;
4970}
This page took 0.24037 seconds and 4 git commands to generate.