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