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