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