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