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