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