lib: graph: add "self" and some "private" APIs
[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_private_value_borrow_value(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_private_value_borrow_value(
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_private_value_borrow_value(
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_private_value_borrow_value(
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_private_value_borrow_value(
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_private_value_borrow_value(
342 bt_private_value_bool_create_init(false));
343 } else {
344 value = bt_private_value_borrow_value(
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_private_value_borrow_value(
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_private_value_borrow_value(
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_private_value_borrow_value(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(-1, NULL, NULL);
1624 if (!cfg->cmd_data.help.cfg_component) {
1625 goto error;
1626 }
1627
1628 goto end;
1629
1630 error:
1631 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1632
1633 end:
1634 return cfg;
1635 }
1636
1637 static
1638 struct bt_config *bt_config_query_create(
1639 struct bt_private_value *initial_plugin_paths)
1640 {
1641 struct bt_config *cfg;
1642
1643 /* Create config */
1644 cfg = bt_config_base_create(BT_CONFIG_COMMAND_QUERY,
1645 initial_plugin_paths, true);
1646 if (!cfg) {
1647 goto error;
1648 }
1649
1650 cfg->cmd_data.query.object = g_string_new(NULL);
1651 if (!cfg->cmd_data.query.object) {
1652 print_err_oom();
1653 goto error;
1654 }
1655
1656 goto end;
1657
1658 error:
1659 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1660
1661 end:
1662 return cfg;
1663 }
1664
1665 static
1666 struct bt_config *bt_config_print_ctf_metadata_create(
1667 struct bt_private_value *initial_plugin_paths)
1668 {
1669 struct bt_config *cfg;
1670
1671 /* Create config */
1672 cfg = bt_config_base_create(BT_CONFIG_COMMAND_PRINT_CTF_METADATA,
1673 initial_plugin_paths, true);
1674 if (!cfg) {
1675 goto error;
1676 }
1677
1678 cfg->cmd_data.print_ctf_metadata.path = g_string_new(NULL);
1679 if (!cfg->cmd_data.print_ctf_metadata.path) {
1680 print_err_oom();
1681 goto error;
1682 }
1683
1684 cfg->cmd_data.print_ctf_metadata.output_path = g_string_new(NULL);
1685 if (!cfg->cmd_data.print_ctf_metadata.output_path) {
1686 print_err_oom();
1687 goto error;
1688 }
1689
1690 goto end;
1691
1692 error:
1693 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1694
1695 end:
1696 return cfg;
1697 }
1698
1699 static
1700 struct bt_config *bt_config_print_lttng_live_sessions_create(
1701 struct bt_private_value *initial_plugin_paths)
1702 {
1703 struct bt_config *cfg;
1704
1705 /* Create config */
1706 cfg = bt_config_base_create(BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS,
1707 initial_plugin_paths, true);
1708 if (!cfg) {
1709 goto error;
1710 }
1711
1712 cfg->cmd_data.print_lttng_live_sessions.url = g_string_new(NULL);
1713 if (!cfg->cmd_data.print_lttng_live_sessions.url) {
1714 print_err_oom();
1715 goto error;
1716 }
1717
1718 cfg->cmd_data.print_lttng_live_sessions.output_path =
1719 g_string_new(NULL);
1720 if (!cfg->cmd_data.print_lttng_live_sessions.output_path) {
1721 print_err_oom();
1722 goto error;
1723 }
1724
1725 goto end;
1726
1727 error:
1728 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1729
1730 end:
1731 return cfg;
1732 }
1733
1734 static
1735 int bt_config_append_plugin_paths_check_setuid_setgid(
1736 struct bt_private_value *plugin_paths, const char *arg)
1737 {
1738 int ret = 0;
1739
1740 if (bt_common_is_setuid_setgid()) {
1741 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1742 goto end;
1743 }
1744
1745 if (bt_config_append_plugin_paths(plugin_paths, arg)) {
1746 printf_err("Invalid --plugin-path option's argument:\n %s\n",
1747 arg);
1748 ret = -1;
1749 goto end;
1750 }
1751
1752 end:
1753 return ret;
1754 }
1755
1756 /*
1757 * Prints the expected format for a --params option.
1758 */
1759 static
1760 void print_expected_params_format(FILE *fp)
1761 {
1762 fprintf(fp, "Expected format of PARAMS\n");
1763 fprintf(fp, "-------------------------\n");
1764 fprintf(fp, "\n");
1765 fprintf(fp, " PARAM=VALUE[,PARAM=VALUE]...\n");
1766 fprintf(fp, "\n");
1767 fprintf(fp, "The parameter string is a comma-separated list of PARAM=VALUE assignments,\n");
1768 fprintf(fp, "where PARAM is the parameter name (C identifier plus the [:.-] characters),\n");
1769 fprintf(fp, "and VALUE can be one of:\n");
1770 fprintf(fp, "\n");
1771 fprintf(fp, "* `null`, `nul`, `NULL`: null value (no backticks).\n");
1772 fprintf(fp, "* `true`, `TRUE`, `yes`, `YES`: true boolean value (no backticks).\n");
1773 fprintf(fp, "* `false`, `FALSE`, `no`, `NO`: false boolean value (no backticks).\n");
1774 fprintf(fp, "* Binary (`0b` prefix), octal (`0` prefix), decimal, or hexadecimal\n");
1775 fprintf(fp, " (`0x` prefix) signed 64-bit integer.\n");
1776 fprintf(fp, "* Double precision floating point number (scientific notation is accepted).\n");
1777 fprintf(fp, "* Unquoted string with no special characters, and not matching any of\n");
1778 fprintf(fp, " the null and boolean value symbols above.\n");
1779 fprintf(fp, "* Double-quoted string (accepts escape characters).\n");
1780 fprintf(fp, "\n");
1781 fprintf(fp, "You can put whitespaces allowed around individual `=` and `,` symbols.\n");
1782 fprintf(fp, "\n");
1783 fprintf(fp, "Example:\n");
1784 fprintf(fp, "\n");
1785 fprintf(fp, " many=null, fresh=yes, condition=false, squirrel=-782329,\n");
1786 fprintf(fp, " observe=3.14, simple=beef, needs-quotes=\"some string\",\n");
1787 fprintf(fp, " escape.chars-are:allowed=\"this is a \\\" double quote\"\n");
1788 fprintf(fp, "\n");
1789 fprintf(fp, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1790 fprintf(fp, "babeltrace from a shell.\n");
1791 }
1792
1793
1794 /*
1795 * Prints the help command usage.
1796 */
1797 static
1798 void print_help_usage(FILE *fp)
1799 {
1800 fprintf(fp, "Usage: babeltrace [GENERAL OPTIONS] help [OPTIONS] PLUGIN\n");
1801 fprintf(fp, " babeltrace [GENERAL OPTIONS] help [OPTIONS] TYPE.PLUGIN.CLS\n");
1802 fprintf(fp, "\n");
1803 fprintf(fp, "Options:\n");
1804 fprintf(fp, "\n");
1805 fprintf(fp, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1806 fprintf(fp, " (~/.local/lib/babeltrace/plugins)\n");
1807 fprintf(fp, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1808 fprintf(fp, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1809 fprintf(fp, " dynamic plugins can be loaded\n");
1810 fprintf(fp, " -h, --help Show this help and quit\n");
1811 fprintf(fp, "\n");
1812 fprintf(fp, "See `babeltrace --help` for the list of general options.\n");
1813 fprintf(fp, "\n");
1814 fprintf(fp, "Use `babeltrace list-plugins` to show the list of available plugins.\n");
1815 }
1816
1817 static
1818 struct poptOption help_long_options[] = {
1819 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
1820 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
1821 { "omit-home-plugin-path", '\0', POPT_ARG_NONE, NULL, OPT_OMIT_HOME_PLUGIN_PATH, NULL, NULL },
1822 { "omit-system-plugin-path", '\0', POPT_ARG_NONE, NULL, OPT_OMIT_SYSTEM_PLUGIN_PATH, NULL, NULL },
1823 { "plugin-path", '\0', POPT_ARG_STRING, NULL, OPT_PLUGIN_PATH, NULL, NULL },
1824 { NULL, 0, '\0', NULL, 0, NULL, NULL },
1825 };
1826
1827 /*
1828 * Creates a Babeltrace config object from the arguments of a help
1829 * command.
1830 *
1831 * *retcode is set to the appropriate exit code to use.
1832 */
1833 static
1834 struct bt_config *bt_config_help_from_args(int argc, const char *argv[],
1835 int *retcode, bool force_omit_system_plugin_path,
1836 bool force_omit_home_plugin_path,
1837 struct bt_private_value *initial_plugin_paths)
1838 {
1839 poptContext pc = NULL;
1840 char *arg = NULL;
1841 int opt;
1842 int ret;
1843 struct bt_config *cfg = NULL;
1844 const char *leftover;
1845 char *plugin_name = NULL, *comp_cls_name = NULL;
1846
1847 *retcode = 0;
1848 cfg = bt_config_help_create(initial_plugin_paths);
1849 if (!cfg) {
1850 goto error;
1851 }
1852
1853 cfg->omit_system_plugin_path = force_omit_system_plugin_path;
1854 cfg->omit_home_plugin_path = force_omit_home_plugin_path;
1855 ret = append_env_var_plugin_paths(cfg->plugin_paths);
1856 if (ret) {
1857 goto error;
1858 }
1859
1860 /* Parse options */
1861 pc = poptGetContext(NULL, argc, (const char **) argv,
1862 help_long_options, 0);
1863 if (!pc) {
1864 printf_err("Cannot get popt context\n");
1865 goto error;
1866 }
1867
1868 poptReadDefaultConfig(pc, 0);
1869
1870 while ((opt = poptGetNextOpt(pc)) > 0) {
1871 arg = poptGetOptArg(pc);
1872
1873 switch (opt) {
1874 case OPT_PLUGIN_PATH:
1875 if (bt_config_append_plugin_paths_check_setuid_setgid(
1876 cfg->plugin_paths, arg)) {
1877 goto error;
1878 }
1879 break;
1880 case OPT_OMIT_SYSTEM_PLUGIN_PATH:
1881 cfg->omit_system_plugin_path = true;
1882 break;
1883 case OPT_OMIT_HOME_PLUGIN_PATH:
1884 cfg->omit_home_plugin_path = true;
1885 break;
1886 case OPT_HELP:
1887 print_help_usage(stdout);
1888 *retcode = -1;
1889 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1890 goto end;
1891 default:
1892 printf_err("Unknown command-line option specified (option code %d)\n",
1893 opt);
1894 goto error;
1895 }
1896
1897 free(arg);
1898 arg = NULL;
1899 }
1900
1901 /* Check for option parsing error */
1902 if (opt < -1) {
1903 printf_err("While parsing command-line options, at option %s: %s\n",
1904 poptBadOption(pc, 0), poptStrerror(opt));
1905 goto error;
1906 }
1907
1908 leftover = poptGetArg(pc);
1909 if (leftover) {
1910 plugin_comp_cls_names(leftover, NULL,
1911 &plugin_name, &comp_cls_name,
1912 &cfg->cmd_data.help.cfg_component->type);
1913 if (plugin_name && comp_cls_name) {
1914 /* Component class help */
1915 g_string_assign(
1916 cfg->cmd_data.help.cfg_component->plugin_name,
1917 plugin_name);
1918 g_string_assign(
1919 cfg->cmd_data.help.cfg_component->comp_cls_name,
1920 comp_cls_name);
1921 } else {
1922 /* Fall back to plugin help */
1923 g_string_assign(
1924 cfg->cmd_data.help.cfg_component->plugin_name,
1925 leftover);
1926 }
1927 } else {
1928 print_help_usage(stdout);
1929 *retcode = -1;
1930 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1931 goto end;
1932 }
1933
1934 if (append_home_and_system_plugin_paths_cfg(cfg)) {
1935 goto error;
1936 }
1937
1938 goto end;
1939
1940 error:
1941 *retcode = 1;
1942 BT_OBJECT_PUT_REF_AND_RESET(cfg);
1943
1944 end:
1945 g_free(plugin_name);
1946 g_free(comp_cls_name);
1947
1948 if (pc) {
1949 poptFreeContext(pc);
1950 }
1951
1952 free(arg);
1953 return cfg;
1954 }
1955
1956 /*
1957 * Prints the help command usage.
1958 */
1959 static
1960 void print_query_usage(FILE *fp)
1961 {
1962 fprintf(fp, "Usage: babeltrace [GEN OPTS] query [OPTS] TYPE.PLUGIN.CLS OBJECT\n");
1963 fprintf(fp, "\n");
1964 fprintf(fp, "Options:\n");
1965 fprintf(fp, "\n");
1966 fprintf(fp, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1967 fprintf(fp, " (~/.local/lib/babeltrace/plugins)\n");
1968 fprintf(fp, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1969 fprintf(fp, " -p, --params=PARAMS Set the query parameters to PARAMS\n");
1970 fprintf(fp, " (see the expected format of PARAMS below)\n");
1971 fprintf(fp, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1972 fprintf(fp, " dynamic plugins can be loaded\n");
1973 fprintf(fp, " -h, --help Show this help and quit\n");
1974 fprintf(fp, "\n\n");
1975 print_expected_params_format(fp);
1976 }
1977
1978 static
1979 struct poptOption query_long_options[] = {
1980 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
1981 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
1982 { "omit-home-plugin-path", '\0', POPT_ARG_NONE, NULL, OPT_OMIT_HOME_PLUGIN_PATH, NULL, NULL },
1983 { "omit-system-plugin-path", '\0', POPT_ARG_NONE, NULL, OPT_OMIT_SYSTEM_PLUGIN_PATH, NULL, NULL },
1984 { "params", 'p', POPT_ARG_STRING, NULL, OPT_PARAMS, NULL, NULL },
1985 { "plugin-path", '\0', POPT_ARG_STRING, NULL, OPT_PLUGIN_PATH, NULL, NULL },
1986 { NULL, 0, '\0', NULL, 0, NULL, NULL },
1987 };
1988
1989 /*
1990 * Creates a Babeltrace config object from the arguments of a query
1991 * command.
1992 *
1993 * *retcode is set to the appropriate exit code to use.
1994 */
1995 static
1996 struct bt_config *bt_config_query_from_args(int argc, const char *argv[],
1997 int *retcode, bool force_omit_system_plugin_path,
1998 bool force_omit_home_plugin_path,
1999 struct bt_private_value *initial_plugin_paths)
2000 {
2001 poptContext pc = NULL;
2002 char *arg = NULL;
2003 int opt;
2004 int ret;
2005 struct bt_config *cfg = NULL;
2006 const char *leftover;
2007 struct bt_private_value *params = bt_private_value_null;
2008
2009 *retcode = 0;
2010 cfg = bt_config_query_create(initial_plugin_paths);
2011 if (!cfg) {
2012 goto error;
2013 }
2014
2015 cfg->omit_system_plugin_path = force_omit_system_plugin_path;
2016 cfg->omit_home_plugin_path = force_omit_home_plugin_path;
2017 ret = append_env_var_plugin_paths(cfg->plugin_paths);
2018 if (ret) {
2019 goto error;
2020 }
2021
2022 /* Parse options */
2023 pc = poptGetContext(NULL, argc, (const char **) argv,
2024 query_long_options, 0);
2025 if (!pc) {
2026 printf_err("Cannot get popt context\n");
2027 goto error;
2028 }
2029
2030 poptReadDefaultConfig(pc, 0);
2031
2032 while ((opt = poptGetNextOpt(pc)) > 0) {
2033 arg = poptGetOptArg(pc);
2034
2035 switch (opt) {
2036 case OPT_PLUGIN_PATH:
2037 if (bt_config_append_plugin_paths_check_setuid_setgid(
2038 cfg->plugin_paths, arg)) {
2039 goto error;
2040 }
2041 break;
2042 case OPT_OMIT_SYSTEM_PLUGIN_PATH:
2043 cfg->omit_system_plugin_path = true;
2044 break;
2045 case OPT_OMIT_HOME_PLUGIN_PATH:
2046 cfg->omit_home_plugin_path = true;
2047 break;
2048 case OPT_PARAMS:
2049 {
2050 bt_object_put_ref(params);
2051 params = bt_private_value_from_arg(arg);
2052 if (!params) {
2053 printf_err("Invalid format for --params option's argument:\n %s\n",
2054 arg);
2055 goto error;
2056 }
2057 break;
2058 }
2059 case OPT_HELP:
2060 print_query_usage(stdout);
2061 *retcode = -1;
2062 BT_OBJECT_PUT_REF_AND_RESET(cfg);
2063 goto end;
2064 default:
2065 printf_err("Unknown command-line option specified (option code %d)\n",
2066 opt);
2067 goto error;
2068 }
2069
2070 free(arg);
2071 arg = NULL;
2072 }
2073
2074 /* Check for option parsing error */
2075 if (opt < -1) {
2076 printf_err("While parsing command-line options, at option %s: %s\n",
2077 poptBadOption(pc, 0), poptStrerror(opt));
2078 goto error;
2079 }
2080
2081 /*
2082 * We need exactly two leftover arguments which are the
2083 * mandatory component class specification and query object.
2084 */
2085 leftover = poptGetArg(pc);
2086 if (leftover) {
2087 cfg->cmd_data.query.cfg_component =
2088 bt_config_component_from_arg(leftover);
2089 if (!cfg->cmd_data.query.cfg_component) {
2090 printf_err("Invalid format for component class specification:\n %s\n",
2091 leftover);
2092 goto error;
2093 }
2094
2095 BT_ASSERT(params);
2096 BT_OBJECT_MOVE_REF(cfg->cmd_data.query.cfg_component->params,
2097 params);
2098 } else {
2099 print_query_usage(stdout);
2100 *retcode = -1;
2101 BT_OBJECT_PUT_REF_AND_RESET(cfg);
2102 goto end;
2103 }
2104
2105 leftover = poptGetArg(pc);
2106 if (leftover) {
2107 if (strlen(leftover) == 0) {
2108 printf_err("Invalid empty object\n");
2109 goto error;
2110 }
2111
2112 g_string_assign(cfg->cmd_data.query.object, leftover);
2113 } else {
2114 print_query_usage(stdout);
2115 *retcode = -1;
2116 BT_OBJECT_PUT_REF_AND_RESET(cfg);
2117 goto end;
2118 }
2119
2120 leftover = poptGetArg(pc);
2121 if (leftover) {
2122 printf_err("Unexpected argument: %s\n", leftover);
2123 goto error;
2124 }
2125
2126 if (append_home_and_system_plugin_paths_cfg(cfg)) {
2127 goto error;
2128 }
2129
2130 goto end;
2131
2132 error:
2133 *retcode = 1;
2134 BT_OBJECT_PUT_REF_AND_RESET(cfg);
2135
2136 end:
2137 if (pc) {
2138 poptFreeContext(pc);
2139 }
2140
2141 bt_object_put_ref(params);
2142 free(arg);
2143 return cfg;
2144 }
2145
2146 /*
2147 * Prints the list-plugins command usage.
2148 */
2149 static
2150 void print_list_plugins_usage(FILE *fp)
2151 {
2152 fprintf(fp, "Usage: babeltrace [GENERAL OPTIONS] list-plugins [OPTIONS]\n");
2153 fprintf(fp, "\n");
2154 fprintf(fp, "Options:\n");
2155 fprintf(fp, "\n");
2156 fprintf(fp, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2157 fprintf(fp, " (~/.local/lib/babeltrace/plugins)\n");
2158 fprintf(fp, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2159 fprintf(fp, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2160 fprintf(fp, " dynamic plugins can be loaded\n");
2161 fprintf(fp, " -h, --help Show this help and quit\n");
2162 fprintf(fp, "\n");
2163 fprintf(fp, "See `babeltrace --help` for the list of general options.\n");
2164 fprintf(fp, "\n");
2165 fprintf(fp, "Use `babeltrace help` to get help for a specific plugin or component class.\n");
2166 }
2167
2168 static
2169 struct poptOption list_plugins_long_options[] = {
2170 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
2171 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
2172 { "omit-home-plugin-path", '\0', POPT_ARG_NONE, NULL, OPT_OMIT_HOME_PLUGIN_PATH, NULL, NULL },
2173 { "omit-system-plugin-path", '\0', POPT_ARG_NONE, NULL, OPT_OMIT_SYSTEM_PLUGIN_PATH, NULL, NULL },
2174 { "plugin-path", '\0', POPT_ARG_STRING, NULL, OPT_PLUGIN_PATH, NULL, NULL },
2175 { NULL, 0, '\0', NULL, 0, NULL, NULL },
2176 };
2177
2178 /*
2179 * Creates a Babeltrace config object from the arguments of a
2180 * list-plugins command.
2181 *
2182 * *retcode is set to the appropriate exit code to use.
2183 */
2184 static
2185 struct bt_config *bt_config_list_plugins_from_args(int argc, const char *argv[],
2186 int *retcode, bool force_omit_system_plugin_path,
2187 bool force_omit_home_plugin_path,
2188 struct bt_private_value *initial_plugin_paths)
2189 {
2190 poptContext pc = NULL;
2191 char *arg = NULL;
2192 int opt;
2193 int ret;
2194 struct bt_config *cfg = NULL;
2195 const char *leftover;
2196
2197 *retcode = 0;
2198 cfg = bt_config_list_plugins_create(initial_plugin_paths);
2199 if (!cfg) {
2200 goto error;
2201 }
2202
2203 cfg->omit_system_plugin_path = force_omit_system_plugin_path;
2204 cfg->omit_home_plugin_path = force_omit_home_plugin_path;
2205 ret = append_env_var_plugin_paths(cfg->plugin_paths);
2206 if (ret) {
2207 goto error;
2208 }
2209
2210 /* Parse options */
2211 pc = poptGetContext(NULL, argc, (const char **) argv,
2212 list_plugins_long_options, 0);
2213 if (!pc) {
2214 printf_err("Cannot get popt context\n");
2215 goto error;
2216 }
2217
2218 poptReadDefaultConfig(pc, 0);
2219
2220 while ((opt = poptGetNextOpt(pc)) > 0) {
2221 arg = poptGetOptArg(pc);
2222
2223 switch (opt) {
2224 case OPT_PLUGIN_PATH:
2225 if (bt_config_append_plugin_paths_check_setuid_setgid(
2226 cfg->plugin_paths, arg)) {
2227 goto error;
2228 }
2229 break;
2230 case OPT_OMIT_SYSTEM_PLUGIN_PATH:
2231 cfg->omit_system_plugin_path = true;
2232 break;
2233 case OPT_OMIT_HOME_PLUGIN_PATH:
2234 cfg->omit_home_plugin_path = true;
2235 break;
2236 case OPT_HELP:
2237 print_list_plugins_usage(stdout);
2238 *retcode = -1;
2239 BT_OBJECT_PUT_REF_AND_RESET(cfg);
2240 goto end;
2241 default:
2242 printf_err("Unknown command-line option specified (option code %d)\n",
2243 opt);
2244 goto error;
2245 }
2246
2247 free(arg);
2248 arg = NULL;
2249 }
2250
2251 /* Check for option parsing error */
2252 if (opt < -1) {
2253 printf_err("While parsing command-line options, at option %s: %s\n",
2254 poptBadOption(pc, 0), poptStrerror(opt));
2255 goto error;
2256 }
2257
2258 leftover = poptGetArg(pc);
2259 if (leftover) {
2260 printf_err("Unexpected argument: %s\n", leftover);
2261 goto error;
2262 }
2263
2264 if (append_home_and_system_plugin_paths_cfg(cfg)) {
2265 goto error;
2266 }
2267
2268 goto end;
2269
2270 error:
2271 *retcode = 1;
2272 BT_OBJECT_PUT_REF_AND_RESET(cfg);
2273
2274 end:
2275 if (pc) {
2276 poptFreeContext(pc);
2277 }
2278
2279 free(arg);
2280 return cfg;
2281 }
2282
2283 /*
2284 * Prints the run command usage.
2285 */
2286 static
2287 void print_run_usage(FILE *fp)
2288 {
2289 fprintf(fp, "Usage: babeltrace [GENERAL OPTIONS] run [OPTIONS]\n");
2290 fprintf(fp, "\n");
2291 fprintf(fp, "Options:\n");
2292 fprintf(fp, "\n");
2293 fprintf(fp, " -b, --base-params=PARAMS Set PARAMS as the current base parameters\n");
2294 fprintf(fp, " for all the following components until\n");
2295 fprintf(fp, " --reset-base-params is encountered\n");
2296 fprintf(fp, " (see the expected format of PARAMS below)\n");
2297 fprintf(fp, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2298 fprintf(fp, " Instantiate the component class CLS of type\n");
2299 fprintf(fp, " TYPE (`source`, `filter`, or `sink`) found\n");
2300 fprintf(fp, " in the plugin PLUGIN, add it to the graph,\n");
2301 fprintf(fp, " and optionally name it NAME (you can also\n");
2302 fprintf(fp, " specify the name with --name)\n");
2303 fprintf(fp, " -C, --connect=CONNECTION Connect two created components (see the\n");
2304 fprintf(fp, " expected format of CONNECTION below)\n");
2305 fprintf(fp, " --key=KEY Set the current initialization string\n");
2306 fprintf(fp, " parameter key to KEY (see --value)\n");
2307 fprintf(fp, " -n, --name=NAME Set the name of the current component\n");
2308 fprintf(fp, " to NAME (must be unique amongst all the\n");
2309 fprintf(fp, " names of the created components)\n");
2310 fprintf(fp, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2311 fprintf(fp, " (~/.local/lib/babeltrace/plugins)\n");
2312 fprintf(fp, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2313 fprintf(fp, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2314 fprintf(fp, " current component (see the expected format\n");
2315 fprintf(fp, " of PARAMS below)\n");
2316 fprintf(fp, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2317 fprintf(fp, " dynamic plugins can be loaded\n");
2318 fprintf(fp, " -r, --reset-base-params Reset the current base parameters to an\n");
2319 fprintf(fp, " empty map\n");
2320 fprintf(fp, " --retry-duration=DUR When babeltrace(1) needs to retry to run\n");
2321 fprintf(fp, " the graph later, retry in DUR µs\n");
2322 fprintf(fp, " (default: 100000)\n");
2323 fprintf(fp, " --value=VAL Add a string initialization parameter to\n");
2324 fprintf(fp, " the current component with a name given by\n");
2325 fprintf(fp, " the last argument of the --key option and a\n");
2326 fprintf(fp, " value set to VAL\n");
2327 fprintf(fp, " -h, --help Show this help and quit\n");
2328 fprintf(fp, "\n");
2329 fprintf(fp, "See `babeltrace --help` for the list of general options.\n");
2330 fprintf(fp, "\n\n");
2331 fprintf(fp, "Expected format of CONNECTION\n");
2332 fprintf(fp, "-----------------------------\n");
2333 fprintf(fp, "\n");
2334 fprintf(fp, " UPSTREAM[.UPSTREAM-PORT]:DOWNSTREAM[.DOWNSTREAM-PORT]\n");
2335 fprintf(fp, "\n");
2336 fprintf(fp, "UPSTREAM and DOWNSTREAM are names of the upstream and downstream\n");
2337 fprintf(fp, "components to connect together. You must escape the following characters\n\n");
2338 fprintf(fp, "with `\\`: `\\`, `.`, and `:`. You can set the name of the current\n");
2339 fprintf(fp, "component with the --name option.\n");
2340 fprintf(fp, "\n");
2341 fprintf(fp, "UPSTREAM-PORT and DOWNSTREAM-PORT are optional globbing patterns to\n");
2342 fprintf(fp, "identify the upstream and downstream ports to use for the connection.\n");
2343 fprintf(fp, "When the port is not specified, `*` is used.\n");
2344 fprintf(fp, "\n");
2345 fprintf(fp, "When a component named UPSTREAM has an available port which matches the\n");
2346 fprintf(fp, "UPSTREAM-PORT globbing pattern, it is connected to the first port which\n");
2347 fprintf(fp, "matches the DOWNSTREAM-PORT globbing pattern of the component named\n");
2348 fprintf(fp, "DOWNSTREAM.\n");
2349 fprintf(fp, "\n");
2350 fprintf(fp, "The only special character in UPSTREAM-PORT and DOWNSTREAM-PORT is `*`\n");
2351 fprintf(fp, "which matches anything. You must escape the following characters\n");
2352 fprintf(fp, "with `\\`: `\\`, `*`, `?`, `[`, `.`, and `:`.\n");
2353 fprintf(fp, "\n");
2354 fprintf(fp, "You can connect a source component to a filter or sink component. You\n");
2355 fprintf(fp, "can connect a filter component to a sink component.\n");
2356 fprintf(fp, "\n");
2357 fprintf(fp, "Examples:\n");
2358 fprintf(fp, "\n");
2359 fprintf(fp, " my-src:my-sink\n");
2360 fprintf(fp, " ctf-fs.*stream*:utils-muxer:*\n");
2361 fprintf(fp, "\n");
2362 fprintf(fp, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
2363 fprintf(fp, "babeltrace from a shell.\n");
2364 fprintf(fp, "\n\n");
2365 print_expected_params_format(fp);
2366 }
2367
2368 /*
2369 * Creates a Babeltrace config object from the arguments of a run
2370 * command.
2371 *
2372 * *retcode is set to the appropriate exit code to use.
2373 */
2374 static
2375 struct bt_config *bt_config_run_from_args(int argc, const char *argv[],
2376 int *retcode, bool force_omit_system_plugin_path,
2377 bool force_omit_home_plugin_path,
2378 struct bt_private_value *initial_plugin_paths)
2379 {
2380 poptContext pc = NULL;
2381 char *arg = NULL;
2382 struct bt_config_component *cur_cfg_comp = NULL;
2383 enum bt_config_component_dest cur_cfg_comp_dest =
2384 BT_CONFIG_COMPONENT_DEST_UNKNOWN;
2385 struct bt_private_value *cur_base_params = NULL;
2386 int opt, ret = 0;
2387 struct bt_config *cfg = NULL;
2388 struct bt_private_value *instance_names = NULL;
2389 struct bt_private_value *connection_args = NULL;
2390 GString *cur_param_key = NULL;
2391 char error_buf[256] = { 0 };
2392 long retry_duration = -1;
2393 enum bt_value_status status;
2394 struct poptOption run_long_options[] = {
2395 { "base-params", 'b', POPT_ARG_STRING, NULL, OPT_BASE_PARAMS, NULL, NULL },
2396 { "component", 'c', POPT_ARG_STRING, NULL, OPT_COMPONENT, NULL, NULL },
2397 { "connect", 'C', POPT_ARG_STRING, NULL, OPT_CONNECT, NULL, NULL },
2398 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
2399 { "key", '\0', POPT_ARG_STRING, NULL, OPT_KEY, NULL, NULL },
2400 { "name", 'n', POPT_ARG_STRING, NULL, OPT_NAME, NULL, NULL },
2401 { "omit-home-plugin-path", '\0', POPT_ARG_NONE, NULL, OPT_OMIT_HOME_PLUGIN_PATH, NULL, NULL },
2402 { "omit-system-plugin-path", '\0', POPT_ARG_NONE, NULL, OPT_OMIT_SYSTEM_PLUGIN_PATH, NULL, NULL },
2403 { "params", 'p', POPT_ARG_STRING, NULL, OPT_PARAMS, NULL, NULL },
2404 { "plugin-path", '\0', POPT_ARG_STRING, NULL, OPT_PLUGIN_PATH, NULL, NULL },
2405 { "reset-base-params", 'r', POPT_ARG_NONE, NULL, OPT_RESET_BASE_PARAMS, NULL, NULL },
2406 { "retry-duration", '\0', POPT_ARG_LONG, &retry_duration, OPT_RETRY_DURATION, NULL, NULL },
2407 { "value", '\0', POPT_ARG_STRING, NULL, OPT_VALUE, NULL, NULL },
2408 { NULL, 0, '\0', NULL, 0, NULL, NULL },
2409 };
2410
2411 *retcode = 0;
2412 cur_param_key = g_string_new(NULL);
2413 if (!cur_param_key) {
2414 print_err_oom();
2415 goto error;
2416 }
2417
2418 if (argc <= 1) {
2419 print_run_usage(stdout);
2420 *retcode = -1;
2421 goto end;
2422 }
2423
2424 cfg = bt_config_run_create(initial_plugin_paths);
2425 if (!cfg) {
2426 goto error;
2427 }
2428
2429 cfg->cmd_data.run.retry_duration_us = 100000;
2430 cfg->omit_system_plugin_path = force_omit_system_plugin_path;
2431 cfg->omit_home_plugin_path = force_omit_home_plugin_path;
2432 cur_base_params = bt_private_value_map_create();
2433 if (!cur_base_params) {
2434 print_err_oom();
2435 goto error;
2436 }
2437
2438 instance_names = bt_private_value_map_create();
2439 if (!instance_names) {
2440 print_err_oom();
2441 goto error;
2442 }
2443
2444 connection_args = bt_private_value_array_create();
2445 if (!connection_args) {
2446 print_err_oom();
2447 goto error;
2448 }
2449
2450 ret = append_env_var_plugin_paths(cfg->plugin_paths);
2451 if (ret) {
2452 goto error;
2453 }
2454
2455 /* Parse options */
2456 pc = poptGetContext(NULL, argc, (const char **) argv,
2457 run_long_options, 0);
2458 if (!pc) {
2459 printf_err("Cannot get popt context\n");
2460 goto error;
2461 }
2462
2463 poptReadDefaultConfig(pc, 0);
2464
2465 while ((opt = poptGetNextOpt(pc)) > 0) {
2466 arg = poptGetOptArg(pc);
2467
2468 switch (opt) {
2469 case OPT_PLUGIN_PATH:
2470 if (bt_config_append_plugin_paths_check_setuid_setgid(
2471 cfg->plugin_paths, arg)) {
2472 goto error;
2473 }
2474 break;
2475 case OPT_OMIT_SYSTEM_PLUGIN_PATH:
2476 cfg->omit_system_plugin_path = true;
2477 break;
2478 case OPT_OMIT_HOME_PLUGIN_PATH:
2479 cfg->omit_home_plugin_path = true;
2480 break;
2481 case OPT_COMPONENT:
2482 {
2483 enum bt_config_component_dest new_dest;
2484
2485 if (cur_cfg_comp) {
2486 ret = add_run_cfg_comp_check_name(cfg,
2487 cur_cfg_comp, cur_cfg_comp_dest,
2488 instance_names);
2489 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp);
2490 if (ret) {
2491 goto error;
2492 }
2493 }
2494
2495 cur_cfg_comp = bt_config_component_from_arg(arg);
2496 if (!cur_cfg_comp) {
2497 printf_err("Invalid format for --component option's argument:\n %s\n",
2498 arg);
2499 goto error;
2500 }
2501
2502 switch (cur_cfg_comp->type) {
2503 case BT_COMPONENT_CLASS_TYPE_SOURCE:
2504 new_dest = BT_CONFIG_COMPONENT_DEST_SOURCE;
2505 break;
2506 case BT_COMPONENT_CLASS_TYPE_FILTER:
2507 new_dest = BT_CONFIG_COMPONENT_DEST_FILTER;
2508 break;
2509 case BT_COMPONENT_CLASS_TYPE_SINK:
2510 new_dest = BT_CONFIG_COMPONENT_DEST_SINK;
2511 break;
2512 default:
2513 abort();
2514 }
2515
2516 BT_ASSERT(cur_base_params);
2517 bt_object_put_ref(cur_cfg_comp->params);
2518 status = bt_value_copy(
2519 &cur_cfg_comp->params,
2520 bt_private_value_borrow_value(cur_base_params));
2521 if (status != BT_VALUE_STATUS_OK) {
2522 print_err_oom();
2523 goto error;
2524 }
2525
2526 cur_cfg_comp_dest = new_dest;
2527 break;
2528 }
2529 case OPT_PARAMS:
2530 {
2531 struct bt_private_value *params;
2532 struct bt_private_value *params_to_set;
2533
2534 if (!cur_cfg_comp) {
2535 printf_err("Cannot add parameters to unavailable component:\n %s\n",
2536 arg);
2537 goto error;
2538 }
2539
2540 params = bt_private_value_from_arg(arg);
2541 if (!params) {
2542 printf_err("Invalid format for --params option's argument:\n %s\n",
2543 arg);
2544 goto error;
2545 }
2546
2547 status = bt_value_map_extend(&params_to_set,
2548 bt_private_value_borrow_value(cur_cfg_comp->params),
2549 bt_private_value_borrow_value(params));
2550 BT_OBJECT_PUT_REF_AND_RESET(params);
2551 if (status != BT_VALUE_STATUS_OK) {
2552 printf_err("Cannot extend current component parameters with --params option's argument:\n %s\n",
2553 arg);
2554 goto error;
2555 }
2556
2557 BT_OBJECT_MOVE_REF(cur_cfg_comp->params, params_to_set);
2558 break;
2559 }
2560 case OPT_KEY:
2561 if (strlen(arg) == 0) {
2562 printf_err("Cannot set an empty string as the current parameter key\n");
2563 goto error;
2564 }
2565
2566 g_string_assign(cur_param_key, arg);
2567 break;
2568 case OPT_VALUE:
2569 if (!cur_cfg_comp) {
2570 printf_err("Cannot set a parameter's value of unavailable component:\n %s\n",
2571 arg);
2572 goto error;
2573 }
2574
2575 if (cur_param_key->len == 0) {
2576 printf_err("--value option specified without preceding --key option:\n %s\n",
2577 arg);
2578 goto error;
2579 }
2580
2581 if (bt_private_value_map_insert_string_entry(cur_cfg_comp->params,
2582 cur_param_key->str, arg)) {
2583 print_err_oom();
2584 goto error;
2585 }
2586 break;
2587 case OPT_NAME:
2588 if (!cur_cfg_comp) {
2589 printf_err("Cannot set the name of unavailable component:\n %s\n",
2590 arg);
2591 goto error;
2592 }
2593
2594 g_string_assign(cur_cfg_comp->instance_name, arg);
2595 break;
2596 case OPT_BASE_PARAMS:
2597 {
2598 struct bt_private_value *params =
2599 bt_private_value_from_arg(arg);
2600
2601 if (!params) {
2602 printf_err("Invalid format for --base-params option's argument:\n %s\n",
2603 arg);
2604 goto error;
2605 }
2606
2607 BT_OBJECT_MOVE_REF(cur_base_params, params);
2608 break;
2609 }
2610 case OPT_RESET_BASE_PARAMS:
2611 BT_OBJECT_PUT_REF_AND_RESET(cur_base_params);
2612 cur_base_params = bt_private_value_map_create();
2613 if (!cur_base_params) {
2614 print_err_oom();
2615 goto error;
2616 }
2617 break;
2618 case OPT_CONNECT:
2619 if (bt_private_value_array_append_string_element(
2620 connection_args, arg)) {
2621 print_err_oom();
2622 goto error;
2623 }
2624 break;
2625 case OPT_RETRY_DURATION:
2626 if (retry_duration < 0) {
2627 printf_err("--retry-duration option's argument must be positive or 0: %ld\n",
2628 retry_duration);
2629 goto error;
2630 }
2631
2632 cfg->cmd_data.run.retry_duration_us =
2633 (uint64_t) retry_duration;
2634 break;
2635 case OPT_HELP:
2636 print_run_usage(stdout);
2637 *retcode = -1;
2638 BT_OBJECT_PUT_REF_AND_RESET(cfg);
2639 goto end;
2640 default:
2641 printf_err("Unknown command-line option specified (option code %d)\n",
2642 opt);
2643 goto error;
2644 }
2645
2646 free(arg);
2647 arg = NULL;
2648 }
2649
2650 /* Check for option parsing error */
2651 if (opt < -1) {
2652 printf_err("While parsing command-line options, at option %s: %s\n",
2653 poptBadOption(pc, 0), poptStrerror(opt));
2654 goto error;
2655 }
2656
2657 /* This command does not accept leftover arguments */
2658 if (poptPeekArg(pc)) {
2659 printf_err("Unexpected argument: %s\n", poptPeekArg(pc));
2660 goto error;
2661 }
2662
2663 /* Add current component */
2664 if (cur_cfg_comp) {
2665 ret = add_run_cfg_comp_check_name(cfg, cur_cfg_comp,
2666 cur_cfg_comp_dest, instance_names);
2667 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp);
2668 if (ret) {
2669 goto error;
2670 }
2671 }
2672
2673 if (cfg->cmd_data.run.sources->len == 0) {
2674 printf_err("Incomplete graph: no source component\n");
2675 goto error;
2676 }
2677
2678 if (cfg->cmd_data.run.sinks->len == 0) {
2679 printf_err("Incomplete graph: no sink component\n");
2680 goto error;
2681 }
2682
2683 if (append_home_and_system_plugin_paths_cfg(cfg)) {
2684 goto error;
2685 }
2686
2687 ret = bt_config_cli_args_create_connections(cfg,
2688 bt_private_value_borrow_value(connection_args),
2689 error_buf, 256);
2690 if (ret) {
2691 printf_err("Cannot creation connections:\n%s", error_buf);
2692 goto error;
2693 }
2694
2695 goto end;
2696
2697 error:
2698 *retcode = 1;
2699 BT_OBJECT_PUT_REF_AND_RESET(cfg);
2700
2701 end:
2702 if (pc) {
2703 poptFreeContext(pc);
2704 }
2705
2706 if (cur_param_key) {
2707 g_string_free(cur_param_key, TRUE);
2708 }
2709
2710 free(arg);
2711 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp);
2712 BT_OBJECT_PUT_REF_AND_RESET(cur_base_params);
2713 BT_OBJECT_PUT_REF_AND_RESET(instance_names);
2714 BT_OBJECT_PUT_REF_AND_RESET(connection_args);
2715 return cfg;
2716 }
2717
2718 static
2719 struct bt_config *bt_config_run_from_args_array(struct bt_value *run_args,
2720 int *retcode, bool force_omit_system_plugin_path,
2721 bool force_omit_home_plugin_path,
2722 struct bt_private_value *initial_plugin_paths)
2723 {
2724 struct bt_config *cfg = NULL;
2725 const char **argv;
2726 int64_t i, len;
2727 const size_t argc = bt_value_array_get_size(run_args) + 1;
2728
2729 argv = calloc(argc, sizeof(*argv));
2730 if (!argv) {
2731 print_err_oom();
2732 goto end;
2733 }
2734
2735 argv[0] = "run";
2736
2737 len = bt_value_array_get_size(run_args);
2738 if (len < 0) {
2739 printf_err("Invalid executable arguments\n");
2740 goto end;
2741 }
2742 for (i = 0; i < len; i++) {
2743 struct bt_value *arg_value =
2744 bt_value_array_borrow_element_by_index(run_args, i);
2745 const char *arg;
2746
2747 BT_ASSERT(arg_value);
2748 arg = bt_value_string_get(arg_value);
2749 BT_ASSERT(arg);
2750 argv[i + 1] = arg;
2751 }
2752
2753 cfg = bt_config_run_from_args(argc, argv, retcode,
2754 force_omit_system_plugin_path, force_omit_home_plugin_path,
2755 initial_plugin_paths);
2756
2757 end:
2758 free(argv);
2759 return cfg;
2760 }
2761
2762 /*
2763 * Prints the convert command usage.
2764 */
2765 static
2766 void print_convert_usage(FILE *fp)
2767 {
2768 fprintf(fp, "Usage: babeltrace [GENERAL OPTIONS] [convert] [OPTIONS] [PATH/URL]\n");
2769 fprintf(fp, "\n");
2770 fprintf(fp, "Options:\n");
2771 fprintf(fp, "\n");
2772 fprintf(fp, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2773 fprintf(fp, " Instantiate the component class CLS of type\n");
2774 fprintf(fp, " TYPE (`source`, `filter`, or `sink`) found\n");
2775 fprintf(fp, " in the plugin PLUGIN, add it to the\n");
2776 fprintf(fp, " conversion graph, and optionally name it\n");
2777 fprintf(fp, " NAME (you can also specify the name with\n");
2778 fprintf(fp, " --name)\n");
2779 fprintf(fp, " --name=NAME Set the name of the current component\n");
2780 fprintf(fp, " to NAME (must be unique amongst all the\n");
2781 fprintf(fp, " names of the created components)\n");
2782 fprintf(fp, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2783 fprintf(fp, " (~/.local/lib/babeltrace/plugins)\n");
2784 fprintf(fp, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2785 fprintf(fp, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2786 fprintf(fp, " current component (see the expected format\n");
2787 fprintf(fp, " of PARAMS below)\n");
2788 fprintf(fp, " -P, --path=PATH Set the `path` string parameter of the\n");
2789 fprintf(fp, " current component to PATH\n");
2790 fprintf(fp, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2791 fprintf(fp, " --retry-duration=DUR When babeltrace(1) needs to retry to run\n");
2792 fprintf(fp, " the graph later, retry in DUR µs\n");
2793 fprintf(fp, " (default: 100000)\n");
2794 fprintf(fp, " dynamic plugins can be loaded\n");
2795 fprintf(fp, " --run-args Print the equivalent arguments for the\n");
2796 fprintf(fp, " `run` command to the standard output,\n");
2797 fprintf(fp, " formatted for a shell, and quit\n");
2798 fprintf(fp, " --run-args-0 Print the equivalent arguments for the\n");
2799 fprintf(fp, " `run` command to the standard output,\n");
2800 fprintf(fp, " formatted for `xargs -0`, and quit\n");
2801 fprintf(fp, " --stream-intersection Only process events when all streams\n");
2802 fprintf(fp, " are active\n");
2803 fprintf(fp, " -u, --url=URL Set the `url` string parameter of the\n");
2804 fprintf(fp, " current component to URL\n");
2805 fprintf(fp, " -h, --help Show this help and quit\n");
2806 fprintf(fp, "\n");
2807 fprintf(fp, "Implicit `source.ctf.fs` component options:\n");
2808 fprintf(fp, "\n");
2809 fprintf(fp, " --clock-offset=SEC Set clock offset to SEC seconds\n");
2810 fprintf(fp, " --clock-offset-ns=NS Set clock offset to NS ns\n");
2811 fprintf(fp, "\n");
2812 fprintf(fp, "Implicit `sink.text.pretty` component options:\n");
2813 fprintf(fp, "\n");
2814 fprintf(fp, " --clock-cycles Print timestamps in clock cycles\n");
2815 fprintf(fp, " --clock-date Print timestamp dates\n");
2816 fprintf(fp, " --clock-gmt Print and parse timestamps in the GMT\n");
2817 fprintf(fp, " time zone instead of the local time zone\n");
2818 fprintf(fp, " --clock-seconds Print the timestamps as `SEC.NS` instead\n");
2819 fprintf(fp, " of `hh:mm:ss.nnnnnnnnn`\n");
2820 fprintf(fp, " --color=(never | auto | always)\n");
2821 fprintf(fp, " Never, automatically, or always emit\n");
2822 fprintf(fp, " console color codes\n");
2823 fprintf(fp, " -f, --fields=FIELD[,FIELD]... Print additional fields; FIELD can be:\n");
2824 fprintf(fp, " `all`, `trace`, `trace:hostname`,\n");
2825 fprintf(fp, " `trace:domain`, `trace:procname`,\n");
2826 fprintf(fp, " `trace:vpid`, `loglevel`, `emf`\n");
2827 fprintf(fp, " -n, --names=NAME[,NAME]... Print field names; NAME can be:\n");
2828 fprintf(fp, " `payload` (or `arg` or `args`), `none`,\n");
2829 fprintf(fp, " `all`, `scope`, `header`, `context`\n");
2830 fprintf(fp, " (or `ctx`)\n");
2831 fprintf(fp, " --no-delta Do not print time delta between\n");
2832 fprintf(fp, " consecutive events\n");
2833 fprintf(fp, " -w, --output=PATH Write output text to PATH instead of\n");
2834 fprintf(fp, " the standard output\n");
2835 fprintf(fp, "\n");
2836 fprintf(fp, "Implicit `filter.utils.muxer` component options:\n");
2837 fprintf(fp, "\n");
2838 fprintf(fp, " --clock-force-correlate Assume that clocks are inherently\n");
2839 fprintf(fp, " correlated across traces\n");
2840 fprintf(fp, "\n");
2841 fprintf(fp, "Implicit `filter.utils.trimmer` component options:\n");
2842 fprintf(fp, "\n");
2843 fprintf(fp, " -b, --begin=BEGIN Set the beginning time of the conversion\n");
2844 fprintf(fp, " time range to BEGIN (see the format of\n");
2845 fprintf(fp, " BEGIN below)\n");
2846 fprintf(fp, " -e, --end=END Set the end time of the conversion time\n");
2847 fprintf(fp, " range to END (see the format of END below)\n");
2848 fprintf(fp, " -t, --timerange=TIMERANGE Set conversion time range to TIMERANGE:\n");
2849 fprintf(fp, " BEGIN,END or [BEGIN,END] (literally `[` and\n");
2850 fprintf(fp, " `]`) (see the format of BEGIN/END below)\n");
2851 fprintf(fp, "\n");
2852 fprintf(fp, "Implicit `filter.lttng-utils.debug-info` component options:\n");
2853 fprintf(fp, "\n");
2854 fprintf(fp, " --debug-info Create an implicit\n");
2855 fprintf(fp, " `filter.lttng-utils.debug-info` component\n");
2856 fprintf(fp, " --debug-info-dir=DIR Search for debug info in directory DIR\n");
2857 fprintf(fp, " instead of `/usr/lib/debug`\n");
2858 fprintf(fp, " --debug-info-full-path Show full debug info source and\n");
2859 fprintf(fp, " binary paths instead of just names\n");
2860 fprintf(fp, " --debug-info-target-prefix=DIR\n");
2861 fprintf(fp, " Use directory DIR as a prefix when\n");
2862 fprintf(fp, " looking up executables during debug\n");
2863 fprintf(fp, " info analysis\n");
2864 fprintf(fp, "\n");
2865 fprintf(fp, "Legacy options that still work:\n");
2866 fprintf(fp, "\n");
2867 fprintf(fp, " -i, --input-format=(ctf | lttng-live)\n");
2868 fprintf(fp, " `ctf`:\n");
2869 fprintf(fp, " Create an implicit `source.ctf.fs`\n");
2870 fprintf(fp, " component\n");
2871 fprintf(fp, " `lttng-live`:\n");
2872 fprintf(fp, " Create an implicit `source.ctf.lttng-live`\n");
2873 fprintf(fp, " component\n");
2874 fprintf(fp, " -o, --output-format=(text | ctf | dummy | ctf-metadata)\n");
2875 fprintf(fp, " `text`:\n");
2876 fprintf(fp, " Create an implicit `sink.text.pretty`\n");
2877 fprintf(fp, " component\n");
2878 fprintf(fp, " `ctf`:\n");
2879 fprintf(fp, " Create an implicit `sink.ctf.fs`\n");
2880 fprintf(fp, " component\n");
2881 fprintf(fp, " `dummy`:\n");
2882 fprintf(fp, " Create an implicit `sink.utils.dummy`\n");
2883 fprintf(fp, " component\n");
2884 fprintf(fp, " `ctf-metadata`:\n");
2885 fprintf(fp, " Query the `source.ctf.fs` component class\n");
2886 fprintf(fp, " for metadata text and quit\n");
2887 fprintf(fp, "\n");
2888 fprintf(fp, "See `babeltrace --help` for the list of general options.\n");
2889 fprintf(fp, "\n\n");
2890 fprintf(fp, "Format of BEGIN and END\n");
2891 fprintf(fp, "-----------------------\n");
2892 fprintf(fp, "\n");
2893 fprintf(fp, " [YYYY-MM-DD [hh:mm:]]ss[.nnnnnnnnn]\n");
2894 fprintf(fp, "\n\n");
2895 print_expected_params_format(fp);
2896 }
2897
2898 static
2899 struct poptOption convert_long_options[] = {
2900 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
2901 { "begin", 'b', POPT_ARG_STRING, NULL, OPT_BEGIN, NULL, NULL },
2902 { "clock-cycles", '\0', POPT_ARG_NONE, NULL, OPT_CLOCK_CYCLES, NULL, NULL },
2903 { "clock-date", '\0', POPT_ARG_NONE, NULL, OPT_CLOCK_DATE, NULL, NULL },
2904 { "clock-force-correlate", '\0', POPT_ARG_NONE, NULL, OPT_CLOCK_FORCE_CORRELATE, NULL, NULL },
2905 { "clock-gmt", '\0', POPT_ARG_NONE, NULL, OPT_CLOCK_GMT, NULL, NULL },
2906 { "clock-offset", '\0', POPT_ARG_STRING, NULL, OPT_CLOCK_OFFSET, NULL, NULL },
2907 { "clock-offset-ns", '\0', POPT_ARG_STRING, NULL, OPT_CLOCK_OFFSET_NS, NULL, NULL },
2908 { "clock-seconds", '\0', POPT_ARG_NONE, NULL, OPT_CLOCK_SECONDS, NULL, NULL },
2909 { "color", '\0', POPT_ARG_STRING, NULL, OPT_COLOR, NULL, NULL },
2910 { "component", 'c', POPT_ARG_STRING, NULL, OPT_COMPONENT, NULL, NULL },
2911 { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL },
2912 { "debug-info-dir", 0, POPT_ARG_STRING, NULL, OPT_DEBUG_INFO_DIR, NULL, NULL },
2913 { "debug-info-full-path", 0, POPT_ARG_NONE, NULL, OPT_DEBUG_INFO_FULL_PATH, NULL, NULL },
2914 { "debug-info-target-prefix", 0, POPT_ARG_STRING, NULL, OPT_DEBUG_INFO_TARGET_PREFIX, NULL, NULL },
2915 { "end", 'e', POPT_ARG_STRING, NULL, OPT_END, NULL, NULL },
2916 { "fields", 'f', POPT_ARG_STRING, NULL, OPT_FIELDS, NULL, NULL },
2917 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
2918 { "input-format", 'i', POPT_ARG_STRING, NULL, OPT_INPUT_FORMAT, NULL, NULL },
2919 { "name", '\0', POPT_ARG_STRING, NULL, OPT_NAME, NULL, NULL },
2920 { "names", 'n', POPT_ARG_STRING, NULL, OPT_NAMES, NULL, NULL },
2921 { "debug-info", '\0', POPT_ARG_NONE, NULL, OPT_DEBUG_INFO, NULL, NULL },
2922 { "no-delta", '\0', POPT_ARG_NONE, NULL, OPT_NO_DELTA, NULL, NULL },
2923 { "omit-home-plugin-path", '\0', POPT_ARG_NONE, NULL, OPT_OMIT_HOME_PLUGIN_PATH, NULL, NULL },
2924 { "omit-system-plugin-path", '\0', POPT_ARG_NONE, NULL, OPT_OMIT_SYSTEM_PLUGIN_PATH, NULL, NULL },
2925 { "output", 'w', POPT_ARG_STRING, NULL, OPT_OUTPUT, NULL, NULL },
2926 { "output-format", 'o', POPT_ARG_STRING, NULL, OPT_OUTPUT_FORMAT, NULL, NULL },
2927 { "params", 'p', POPT_ARG_STRING, NULL, OPT_PARAMS, NULL, NULL },
2928 { "path", 'P', POPT_ARG_STRING, NULL, OPT_PATH, NULL, NULL },
2929 { "plugin-path", '\0', POPT_ARG_STRING, NULL, OPT_PLUGIN_PATH, NULL, NULL },
2930 { "retry-duration", '\0', POPT_ARG_STRING, NULL, OPT_RETRY_DURATION, NULL, NULL },
2931 { "run-args", '\0', POPT_ARG_NONE, NULL, OPT_RUN_ARGS, NULL, NULL },
2932 { "run-args-0", '\0', POPT_ARG_NONE, NULL, OPT_RUN_ARGS_0, NULL, NULL },
2933 { "stream-intersection", '\0', POPT_ARG_NONE, NULL, OPT_STREAM_INTERSECTION, NULL, NULL },
2934 { "timerange", '\0', POPT_ARG_STRING, NULL, OPT_TIMERANGE, NULL, NULL },
2935 { "url", 'u', POPT_ARG_STRING, NULL, OPT_URL, NULL, NULL },
2936 { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL },
2937 { NULL, 0, '\0', NULL, 0, NULL, NULL },
2938 };
2939
2940 static
2941 GString *get_component_auto_name(const char *prefix,
2942 struct bt_value *existing_names)
2943 {
2944 unsigned int i = 0;
2945 GString *auto_name = g_string_new(NULL);
2946
2947 if (!auto_name) {
2948 print_err_oom();
2949 goto end;
2950 }
2951
2952 if (!bt_value_map_has_entry(existing_names, prefix)) {
2953 g_string_assign(auto_name, prefix);
2954 goto end;
2955 }
2956
2957 do {
2958 g_string_printf(auto_name, "%s-%d", prefix, i);
2959 i++;
2960 } while (bt_value_map_has_entry(existing_names, auto_name->str));
2961
2962 end:
2963 return auto_name;
2964 }
2965
2966 struct implicit_component_args {
2967 bool exists;
2968 GString *comp_arg;
2969 GString *name_arg;
2970 GString *params_arg;
2971 struct bt_private_value *extra_params;
2972 };
2973
2974 static
2975 int assign_name_to_implicit_component(struct implicit_component_args *args,
2976 const char *prefix, struct bt_private_value *existing_names,
2977 GList **comp_names, bool append_to_comp_names)
2978 {
2979 int ret = 0;
2980 GString *name = NULL;
2981
2982 if (!args->exists) {
2983 goto end;
2984 }
2985
2986 name = get_component_auto_name(prefix,
2987 bt_private_value_borrow_value(existing_names));
2988
2989 if (!name) {
2990 ret = -1;
2991 goto end;
2992 }
2993
2994 g_string_assign(args->name_arg, name->str);
2995
2996 if (bt_private_value_map_insert_entry(existing_names, name->str,
2997 bt_value_null)) {
2998 print_err_oom();
2999 ret = -1;
3000 goto end;
3001 }
3002
3003 if (append_to_comp_names) {
3004 *comp_names = g_list_append(*comp_names, name);
3005 name = NULL;
3006 }
3007
3008 end:
3009 if (name) {
3010 g_string_free(name, TRUE);
3011 }
3012
3013 return ret;
3014 }
3015
3016 static
3017 int append_run_args_for_implicit_component(
3018 struct implicit_component_args *impl_args,
3019 struct bt_private_value *run_args)
3020 {
3021 int ret = 0;
3022 size_t i;
3023
3024 if (!impl_args->exists) {
3025 goto end;
3026 }
3027
3028 if (bt_private_value_array_append_string_element(run_args, "--component")) {
3029 print_err_oom();
3030 goto error;
3031 }
3032
3033 if (bt_private_value_array_append_string_element(run_args, impl_args->comp_arg->str)) {
3034 print_err_oom();
3035 goto error;
3036 }
3037
3038 if (bt_private_value_array_append_string_element(run_args, "--name")) {
3039 print_err_oom();
3040 goto error;
3041 }
3042
3043 if (bt_private_value_array_append_string_element(run_args, impl_args->name_arg->str)) {
3044 print_err_oom();
3045 goto error;
3046 }
3047
3048 if (impl_args->params_arg->len > 0) {
3049 if (bt_private_value_array_append_string_element(run_args, "--params")) {
3050 print_err_oom();
3051 goto error;
3052 }
3053
3054 if (bt_private_value_array_append_string_element(run_args,
3055 impl_args->params_arg->str)) {
3056 print_err_oom();
3057 goto error;
3058 }
3059 }
3060
3061 for (i = 0; i < bt_value_array_get_size(
3062 bt_private_value_borrow_value(impl_args->extra_params));
3063 i++) {
3064 struct bt_value *elem;
3065 const char *arg;
3066
3067 elem = bt_value_array_borrow_element_by_index(
3068 bt_private_value_borrow_value(impl_args->extra_params),
3069 i);
3070 if (!elem) {
3071 goto error;
3072 }
3073
3074 BT_ASSERT(bt_value_is_string(elem));
3075 arg = bt_value_string_get(elem);
3076 ret = bt_private_value_array_append_string_element(run_args, arg);
3077 if (ret) {
3078 print_err_oom();
3079 goto error;
3080 }
3081 }
3082
3083 goto end;
3084
3085 error:
3086 ret = -1;
3087
3088 end:
3089 return ret;
3090 }
3091
3092 static
3093 void finalize_implicit_component_args(struct implicit_component_args *args)
3094 {
3095 BT_ASSERT(args);
3096
3097 if (args->comp_arg) {
3098 g_string_free(args->comp_arg, TRUE);
3099 }
3100
3101 if (args->name_arg) {
3102 g_string_free(args->name_arg, TRUE);
3103 }
3104
3105 if (args->params_arg) {
3106 g_string_free(args->params_arg, TRUE);
3107 }
3108
3109 bt_object_put_ref(args->extra_params);
3110 }
3111
3112 static
3113 void destroy_implicit_component_args(void *args)
3114 {
3115 if (!args) {
3116 return;
3117 }
3118
3119 finalize_implicit_component_args(args);
3120 g_free(args);
3121 }
3122
3123 static
3124 int init_implicit_component_args(struct implicit_component_args *args,
3125 const char *comp_arg, bool exists)
3126 {
3127 int ret = 0;
3128
3129 args->exists = exists;
3130 args->comp_arg = g_string_new(comp_arg);
3131 args->name_arg = g_string_new(NULL);
3132 args->params_arg = g_string_new(NULL);
3133 args->extra_params = bt_private_value_array_create();
3134
3135 if (!args->comp_arg || !args->name_arg ||
3136 !args->params_arg || !args->extra_params) {
3137 ret = -1;
3138 finalize_implicit_component_args(args);
3139 print_err_oom();
3140 goto end;
3141 }
3142
3143 end:
3144 return ret;
3145 }
3146
3147 static
3148 void append_implicit_component_param(struct implicit_component_args *args,
3149 const char *key, const char *value)
3150 {
3151 BT_ASSERT(args);
3152 BT_ASSERT(key);
3153 BT_ASSERT(value);
3154 append_param_arg(args->params_arg, key, value);
3155 }
3156
3157 static
3158 int append_implicit_component_extra_param(struct implicit_component_args *args,
3159 const char *key, const char *value)
3160 {
3161 int ret = 0;
3162
3163 BT_ASSERT(args);
3164 BT_ASSERT(key);
3165 BT_ASSERT(value);
3166
3167 if (bt_private_value_array_append_string_element(args->extra_params, "--key")) {
3168 print_err_oom();
3169 ret = -1;
3170 goto end;
3171 }
3172
3173 if (bt_private_value_array_append_string_element(args->extra_params, key)) {
3174 print_err_oom();
3175 ret = -1;
3176 goto end;
3177 }
3178
3179 if (bt_private_value_array_append_string_element(args->extra_params, "--value")) {
3180 print_err_oom();
3181 ret = -1;
3182 goto end;
3183 }
3184
3185 if (bt_private_value_array_append_string_element(args->extra_params, value)) {
3186 print_err_oom();
3187 ret = -1;
3188 goto end;
3189 }
3190
3191 end:
3192 return ret;
3193 }
3194
3195 static
3196 int convert_append_name_param(enum bt_config_component_dest dest,
3197 GString *cur_name, GString *cur_name_prefix,
3198 struct bt_private_value *run_args,
3199 struct bt_private_value *all_names,
3200 GList **source_names, GList **filter_names,
3201 GList **sink_names)
3202 {
3203 int ret = 0;
3204
3205 if (cur_name_prefix->len > 0) {
3206 /* We're after a --component option */
3207 GString *name = NULL;
3208 bool append_name_opt = false;
3209
3210 if (cur_name->len == 0) {
3211 /*
3212 * No explicit name was provided for the user
3213 * component.
3214 */
3215 name = get_component_auto_name(cur_name_prefix->str,
3216 bt_private_value_borrow_value(all_names));
3217 append_name_opt = true;
3218 } else {
3219 /*
3220 * An explicit name was provided for the user
3221 * component.
3222 */
3223 if (bt_value_map_has_entry(
3224 bt_private_value_borrow_value(all_names),
3225 cur_name->str)) {
3226 printf_err("Duplicate component instance name:\n %s\n",
3227 cur_name->str);
3228 goto error;
3229 }
3230
3231 name = g_string_new(cur_name->str);
3232 }
3233
3234 if (!name) {
3235 print_err_oom();
3236 goto error;
3237 }
3238
3239 /*
3240 * Remember this name globally, for the uniqueness of
3241 * all component names.
3242 */
3243 if (bt_private_value_map_insert_entry(all_names, name->str, bt_value_null)) {
3244 print_err_oom();
3245 goto error;
3246 }
3247
3248 /*
3249 * Append the --name option if necessary.
3250 */
3251 if (append_name_opt) {
3252 if (bt_private_value_array_append_string_element(run_args, "--name")) {
3253 print_err_oom();
3254 goto error;
3255 }
3256
3257 if (bt_private_value_array_append_string_element(run_args, name->str)) {
3258 print_err_oom();
3259 goto error;
3260 }
3261 }
3262
3263 /*
3264 * Remember this name specifically for the type of the
3265 * component. This is to create connection arguments.
3266 */
3267 switch (dest) {
3268 case BT_CONFIG_COMPONENT_DEST_SOURCE:
3269 *source_names = g_list_append(*source_names, name);
3270 break;
3271 case BT_CONFIG_COMPONENT_DEST_FILTER:
3272 *filter_names = g_list_append(*filter_names, name);
3273 break;
3274 case BT_CONFIG_COMPONENT_DEST_SINK:
3275 *sink_names = g_list_append(*sink_names, name);
3276 break;
3277 default:
3278 abort();
3279 }
3280
3281 g_string_assign(cur_name_prefix, "");
3282 }
3283
3284 goto end;
3285
3286 error:
3287 ret = -1;
3288
3289 end:
3290 return ret;
3291 }
3292
3293 /*
3294 * Escapes `.`, `:`, and `\` of `input` with `\`.
3295 */
3296 static
3297 GString *escape_dot_colon(const char *input)
3298 {
3299 GString *output = g_string_new(NULL);
3300 const char *ch;
3301
3302 if (!output) {
3303 print_err_oom();
3304 goto end;
3305 }
3306
3307 for (ch = input; *ch != '\0'; ch++) {
3308 if (*ch == '\\' || *ch == '.' || *ch == ':') {
3309 g_string_append_c(output, '\\');
3310 }
3311
3312 g_string_append_c(output, *ch);
3313 }
3314
3315 end:
3316 return output;
3317 }
3318
3319 /*
3320 * Appends a --connect option to a list of arguments. `upstream_name`
3321 * and `downstream_name` are escaped with escape_dot_colon() in this
3322 * function.
3323 */
3324 static
3325 int append_connect_arg(struct bt_private_value *run_args,
3326 const char *upstream_name, const char *downstream_name)
3327 {
3328 int ret = 0;
3329 GString *e_upstream_name = escape_dot_colon(upstream_name);
3330 GString *e_downstream_name = escape_dot_colon(downstream_name);
3331 GString *arg = g_string_new(NULL);
3332
3333 if (!e_upstream_name || !e_downstream_name || !arg) {
3334 print_err_oom();
3335 ret = -1;
3336 goto end;
3337 }
3338
3339 ret = bt_private_value_array_append_string_element(run_args, "--connect");
3340 if (ret) {
3341 print_err_oom();
3342 ret = -1;
3343 goto end;
3344 }
3345
3346 g_string_append(arg, e_upstream_name->str);
3347 g_string_append_c(arg, ':');
3348 g_string_append(arg, e_downstream_name->str);
3349 ret = bt_private_value_array_append_string_element(run_args, arg->str);
3350 if (ret) {
3351 print_err_oom();
3352 ret = -1;
3353 goto end;
3354 }
3355
3356 end:
3357 if (arg) {
3358 g_string_free(arg, TRUE);
3359 }
3360
3361 if (e_upstream_name) {
3362 g_string_free(e_upstream_name, TRUE);
3363 }
3364
3365 if (e_downstream_name) {
3366 g_string_free(e_downstream_name, TRUE);
3367 }
3368
3369 return ret;
3370 }
3371
3372 /*
3373 * Appends the run command's --connect options for the convert command.
3374 */
3375 static
3376 int convert_auto_connect(struct bt_private_value *run_args,
3377 GList *source_names, GList *filter_names,
3378 GList *sink_names)
3379 {
3380 int ret = 0;
3381 GList *source_at = source_names;
3382 GList *filter_at = filter_names;
3383 GList *filter_prev;
3384 GList *sink_at = sink_names;
3385
3386 BT_ASSERT(source_names);
3387 BT_ASSERT(filter_names);
3388 BT_ASSERT(sink_names);
3389
3390 /* Connect all sources to the first filter */
3391 for (source_at = source_names; source_at != NULL; source_at = g_list_next(source_at)) {
3392 GString *source_name = source_at->data;
3393 GString *filter_name = filter_at->data;
3394
3395 ret = append_connect_arg(run_args, source_name->str,
3396 filter_name->str);
3397 if (ret) {
3398 goto error;
3399 }
3400 }
3401
3402 filter_prev = filter_at;
3403 filter_at = g_list_next(filter_at);
3404
3405 /* Connect remaining filters */
3406 for (; filter_at != NULL; filter_prev = filter_at, filter_at = g_list_next(filter_at)) {
3407 GString *filter_name = filter_at->data;
3408 GString *filter_prev_name = filter_prev->data;
3409
3410 ret = append_connect_arg(run_args, filter_prev_name->str,
3411 filter_name->str);
3412 if (ret) {
3413 goto error;
3414 }
3415 }
3416
3417 /* Connect last filter to all sinks */
3418 for (sink_at = sink_names; sink_at != NULL; sink_at = g_list_next(sink_at)) {
3419 GString *filter_name = filter_prev->data;
3420 GString *sink_name = sink_at->data;
3421
3422 ret = append_connect_arg(run_args, filter_name->str,
3423 sink_name->str);
3424 if (ret) {
3425 goto error;
3426 }
3427 }
3428
3429 goto end;
3430
3431 error:
3432 ret = -1;
3433
3434 end:
3435 return ret;
3436 }
3437
3438 static
3439 int split_timerange(const char *arg, char **begin, char **end)
3440 {
3441 int ret = 0;
3442 const char *ch = arg;
3443 size_t end_pos;
3444 GString *g_begin = NULL;
3445 GString *g_end = NULL;
3446
3447 BT_ASSERT(arg);
3448
3449 if (*ch == '[') {
3450 ch++;
3451 }
3452
3453 g_begin = bt_common_string_until(ch, "", ",", &end_pos);
3454 if (!g_begin || ch[end_pos] != ',' || g_begin->len == 0) {
3455 goto error;
3456 }
3457
3458 ch += end_pos + 1;
3459
3460 g_end = bt_common_string_until(ch, "", "]", &end_pos);
3461 if (!g_end || g_end->len == 0) {
3462 goto error;
3463 }
3464
3465 BT_ASSERT(begin);
3466 BT_ASSERT(end);
3467 *begin = g_begin->str;
3468 *end = g_end->str;
3469 g_string_free(g_begin, FALSE);
3470 g_string_free(g_end, FALSE);
3471 g_begin = NULL;
3472 g_end = NULL;
3473 goto end;
3474
3475 error:
3476 ret = -1;
3477
3478 end:
3479 if (g_begin) {
3480 g_string_free(g_begin, TRUE);
3481 }
3482
3483 if (g_end) {
3484 g_string_free(g_end, TRUE);
3485 }
3486
3487 return ret;
3488 }
3489
3490 static
3491 int g_list_prepend_gstring(GList **list, const char *string)
3492 {
3493 int ret = 0;
3494 GString *gs = g_string_new(string);
3495
3496 BT_ASSERT(list);
3497
3498 if (!gs) {
3499 print_err_oom();
3500 goto end;
3501 }
3502
3503 *list = g_list_prepend(*list, gs);
3504
3505 end:
3506 return ret;
3507 }
3508
3509 static
3510 struct implicit_component_args *create_implicit_component_args(void)
3511 {
3512 struct implicit_component_args *impl_args =
3513 g_new0(struct implicit_component_args, 1);
3514
3515 if (!impl_args) {
3516 goto end;
3517 }
3518
3519 if (init_implicit_component_args(impl_args, NULL, true)) {
3520 destroy_implicit_component_args(impl_args);
3521 impl_args = NULL;
3522 goto end;
3523 }
3524
3525 end:
3526 return impl_args;
3527 }
3528
3529 static
3530 int fill_implicit_ctf_inputs_args(GPtrArray *implicit_ctf_inputs_args,
3531 struct implicit_component_args *base_implicit_ctf_input_args,
3532 GList *leftovers)
3533 {
3534 int ret = 0;
3535 GList *leftover;
3536 enum bt_value_status status;
3537
3538 for (leftover = leftovers; leftover != NULL;
3539 leftover = g_list_next(leftover)) {
3540 GString *gs_leftover = leftover->data;
3541 struct implicit_component_args *impl_args =
3542 create_implicit_component_args();
3543
3544 if (!impl_args) {
3545 print_err_oom();
3546 goto error;
3547 }
3548
3549 impl_args->exists = true;
3550 g_string_assign(impl_args->comp_arg,
3551 base_implicit_ctf_input_args->comp_arg->str);
3552 g_string_assign(impl_args->params_arg,
3553 base_implicit_ctf_input_args->params_arg->str);
3554
3555 /*
3556 * We need our own copy of the extra parameters because
3557 * this is where the unique path goes.
3558 */
3559 BT_OBJECT_PUT_REF_AND_RESET(impl_args->extra_params);
3560 status = bt_value_copy(&impl_args->extra_params,
3561 bt_private_value_borrow_value(
3562 base_implicit_ctf_input_args->extra_params));
3563 if (status != BT_VALUE_STATUS_OK) {
3564 print_err_oom();
3565 destroy_implicit_component_args(impl_args);
3566 goto error;
3567 }
3568
3569 /* Append unique path parameter */
3570 ret = append_implicit_component_extra_param(impl_args,
3571 "path", gs_leftover->str);
3572 if (ret) {
3573 destroy_implicit_component_args(impl_args);
3574 goto error;
3575 }
3576
3577 g_ptr_array_add(implicit_ctf_inputs_args, impl_args);
3578 }
3579
3580 goto end;
3581
3582 error:
3583 ret = -1;
3584
3585 end:
3586 return ret;
3587 }
3588
3589 /*
3590 * Creates a Babeltrace config object from the arguments of a convert
3591 * command.
3592 *
3593 * *retcode is set to the appropriate exit code to use.
3594 */
3595 static
3596 struct bt_config *bt_config_convert_from_args(int argc, const char *argv[],
3597 int *retcode, bool force_omit_system_plugin_path,
3598 bool force_omit_home_plugin_path,
3599 struct bt_private_value *initial_plugin_paths, char *log_level)
3600 {
3601 poptContext pc = NULL;
3602 char *arg = NULL;
3603 enum bt_config_component_dest cur_comp_dest =
3604 BT_CONFIG_COMPONENT_DEST_UNKNOWN;
3605 int opt, ret = 0;
3606 struct bt_config *cfg = NULL;
3607 bool got_input_format_opt = false;
3608 bool got_output_format_opt = false;
3609 bool trimmer_has_begin = false;
3610 bool trimmer_has_end = false;
3611 bool stream_intersection_mode = false;
3612 GString *cur_name = NULL;
3613 GString *cur_name_prefix = NULL;
3614 const char *leftover = NULL;
3615 bool print_run_args = false;
3616 bool print_run_args_0 = false;
3617 bool print_ctf_metadata = false;
3618 struct bt_private_value *run_args = NULL;
3619 struct bt_private_value *all_names = NULL;
3620 GList *source_names = NULL;
3621 GList *filter_names = NULL;
3622 GList *sink_names = NULL;
3623 GList *leftovers = NULL;
3624 GPtrArray *implicit_ctf_inputs_args = NULL;
3625 struct implicit_component_args base_implicit_ctf_input_args = { 0 };
3626 struct implicit_component_args implicit_ctf_output_args = { 0 };
3627 struct implicit_component_args implicit_lttng_live_args = { 0 };
3628 struct implicit_component_args implicit_dummy_args = { 0 };
3629 struct implicit_component_args implicit_text_args = { 0 };
3630 struct implicit_component_args implicit_debug_info_args = { 0 };
3631 struct implicit_component_args implicit_muxer_args = { 0 };
3632 struct implicit_component_args implicit_trimmer_args = { 0 };
3633 struct bt_private_value *plugin_paths =
3634 bt_object_get_ref(initial_plugin_paths);
3635 char error_buf[256] = { 0 };
3636 size_t i;
3637 struct bt_common_lttng_live_url_parts lttng_live_url_parts = { 0 };
3638 char *output = NULL;
3639
3640 *retcode = 0;
3641
3642 if (argc <= 1) {
3643 print_convert_usage(stdout);
3644 *retcode = -1;
3645 goto end;
3646 }
3647
3648 if (init_implicit_component_args(&base_implicit_ctf_input_args,
3649 "source.ctf.fs", false)) {
3650 goto error;
3651 }
3652
3653 if (init_implicit_component_args(&implicit_ctf_output_args,
3654 "sink.ctf.fs", false)) {
3655 goto error;
3656 }
3657
3658 if (init_implicit_component_args(&implicit_lttng_live_args,
3659 "source.ctf.lttng-live", false)) {
3660 goto error;
3661 }
3662
3663 if (init_implicit_component_args(&implicit_text_args,
3664 "sink.text.pretty", false)) {
3665 goto error;
3666 }
3667
3668 if (init_implicit_component_args(&implicit_dummy_args,
3669 "sink.utils.dummy", false)) {
3670 goto error;
3671 }
3672
3673 if (init_implicit_component_args(&implicit_debug_info_args,
3674 "filter.lttng-utils.debug-info", false)) {
3675 goto error;
3676 }
3677
3678 if (init_implicit_component_args(&implicit_muxer_args,
3679 "filter.utils.muxer", true)) {
3680 goto error;
3681 }
3682
3683 if (init_implicit_component_args(&implicit_trimmer_args,
3684 "filter.utils.trimmer", false)) {
3685 goto error;
3686 }
3687
3688 implicit_ctf_inputs_args = g_ptr_array_new_with_free_func(
3689 (GDestroyNotify) destroy_implicit_component_args);
3690 if (!implicit_ctf_inputs_args) {
3691 print_err_oom();
3692 goto error;
3693 }
3694
3695 all_names = bt_private_value_map_create();
3696 if (!all_names) {
3697 print_err_oom();
3698 goto error;
3699 }
3700
3701 run_args = bt_private_value_array_create();
3702 if (!run_args) {
3703 print_err_oom();
3704 goto error;
3705 }
3706
3707 cur_name = g_string_new(NULL);
3708 if (!cur_name) {
3709 print_err_oom();
3710 goto error;
3711 }
3712
3713 cur_name_prefix = g_string_new(NULL);
3714 if (!cur_name_prefix) {
3715 print_err_oom();
3716 goto error;
3717 }
3718
3719 ret = append_env_var_plugin_paths(plugin_paths);
3720 if (ret) {
3721 goto error;
3722 }
3723
3724 /*
3725 * First pass: collect all arguments which need to be passed
3726 * as is to the run command. This pass can also add --name
3727 * arguments if needed to automatically name unnamed component
3728 * instances. Also it does the following transformations:
3729 *
3730 * --path=PATH -> --key path --value PATH
3731 * --url=URL -> --key url --value URL
3732 *
3733 * Also it appends the plugin paths of --plugin-path to
3734 * `plugin_paths`.
3735 */
3736 pc = poptGetContext(NULL, argc, (const char **) argv,
3737 convert_long_options, 0);
3738 if (!pc) {
3739 printf_err("Cannot get popt context\n");
3740 goto error;
3741 }
3742
3743 poptReadDefaultConfig(pc, 0);
3744
3745 while ((opt = poptGetNextOpt(pc)) > 0) {
3746 char *name = NULL;
3747 char *plugin_name = NULL;
3748 char *comp_cls_name = NULL;
3749
3750 arg = poptGetOptArg(pc);
3751
3752 switch (opt) {
3753 case OPT_COMPONENT:
3754 {
3755 enum bt_component_class_type type;
3756 const char *type_prefix;
3757
3758 /* Append current component's name if needed */
3759 ret = convert_append_name_param(cur_comp_dest, cur_name,
3760 cur_name_prefix, run_args, all_names,
3761 &source_names, &filter_names, &sink_names);
3762 if (ret) {
3763 goto error;
3764 }
3765
3766 /* Parse the argument */
3767 plugin_comp_cls_names(arg, &name, &plugin_name,
3768 &comp_cls_name, &type);
3769 if (!plugin_name || !comp_cls_name) {
3770 printf_err("Invalid format for --component option's argument:\n %s\n",
3771 arg);
3772 goto error;
3773 }
3774
3775 if (name) {
3776 g_string_assign(cur_name, name);
3777 } else {
3778 g_string_assign(cur_name, "");
3779 }
3780
3781 switch (type) {
3782 case BT_COMPONENT_CLASS_TYPE_SOURCE:
3783 cur_comp_dest = BT_CONFIG_COMPONENT_DEST_SOURCE;
3784 type_prefix = "source";
3785 break;
3786 case BT_COMPONENT_CLASS_TYPE_FILTER:
3787 cur_comp_dest = BT_CONFIG_COMPONENT_DEST_FILTER;
3788 type_prefix = "filter";
3789 break;
3790 case BT_COMPONENT_CLASS_TYPE_SINK:
3791 cur_comp_dest = BT_CONFIG_COMPONENT_DEST_SINK;
3792 type_prefix = "sink";
3793 break;
3794 default:
3795 abort();
3796 }
3797
3798 if (bt_private_value_array_append_string_element(run_args,
3799 "--component")) {
3800 print_err_oom();
3801 goto error;
3802 }
3803
3804 if (bt_private_value_array_append_string_element(run_args, arg)) {
3805 print_err_oom();
3806 goto error;
3807 }
3808
3809 g_string_assign(cur_name_prefix, "");
3810 g_string_append_printf(cur_name_prefix, "%s.%s.%s",
3811 type_prefix, plugin_name, comp_cls_name);
3812 free(name);
3813 free(plugin_name);
3814 free(comp_cls_name);
3815 name = NULL;
3816 plugin_name = NULL;
3817 comp_cls_name = NULL;
3818 break;
3819 }
3820 case OPT_PARAMS:
3821 if (cur_name_prefix->len == 0) {
3822 printf_err("No current component of which to set parameters:\n %s\n",
3823 arg);
3824 goto error;
3825 }
3826
3827 if (bt_private_value_array_append_string_element(run_args,
3828 "--params")) {
3829 print_err_oom();
3830 goto error;
3831 }
3832
3833 if (bt_private_value_array_append_string_element(run_args, arg)) {
3834 print_err_oom();
3835 goto error;
3836 }
3837 break;
3838 case OPT_PATH:
3839 if (cur_name_prefix->len == 0) {
3840 printf_err("No current component of which to set `path` parameter:\n %s\n",
3841 arg);
3842 goto error;
3843 }
3844
3845 if (bt_private_value_array_append_string_element(run_args, "--key")) {
3846 print_err_oom();
3847 goto error;
3848 }
3849
3850 if (bt_private_value_array_append_string_element(run_args, "path")) {
3851 print_err_oom();
3852 goto error;
3853 }
3854
3855 if (bt_private_value_array_append_string_element(run_args, "--value")) {
3856 print_err_oom();
3857 goto error;
3858 }
3859
3860 if (bt_private_value_array_append_string_element(run_args, arg)) {
3861 print_err_oom();
3862 goto error;
3863 }
3864 break;
3865 case OPT_URL:
3866 if (cur_name_prefix->len == 0) {
3867 printf_err("No current component of which to set `url` parameter:\n %s\n",
3868 arg);
3869 goto error;
3870 }
3871
3872 if (bt_private_value_array_append_string_element(run_args, "--key")) {
3873 print_err_oom();
3874 goto error;
3875 }
3876
3877 if (bt_private_value_array_append_string_element(run_args, "url")) {
3878 print_err_oom();
3879 goto error;
3880 }
3881
3882 if (bt_private_value_array_append_string_element(run_args, "--value")) {
3883 print_err_oom();
3884 goto error;
3885 }
3886
3887 if (bt_private_value_array_append_string_element(run_args, arg)) {
3888 print_err_oom();
3889 goto error;
3890 }
3891 break;
3892 case OPT_NAME:
3893 if (cur_name_prefix->len == 0) {
3894 printf_err("No current component to name:\n %s\n",
3895 arg);
3896 goto error;
3897 }
3898
3899 if (bt_private_value_array_append_string_element(run_args, "--name")) {
3900 print_err_oom();
3901 goto error;
3902 }
3903
3904 if (bt_private_value_array_append_string_element(run_args, arg)) {
3905 print_err_oom();
3906 goto error;
3907 }
3908
3909 g_string_assign(cur_name, arg);
3910 break;
3911 case OPT_OMIT_HOME_PLUGIN_PATH:
3912 force_omit_home_plugin_path = true;
3913
3914 if (bt_private_value_array_append_string_element(run_args,
3915 "--omit-home-plugin-path")) {
3916 print_err_oom();
3917 goto error;
3918 }
3919 break;
3920 case OPT_RETRY_DURATION:
3921 if (bt_private_value_array_append_string_element(run_args,
3922 "--retry-duration")) {
3923 print_err_oom();
3924 goto error;
3925 }
3926
3927 if (bt_private_value_array_append_string_element(run_args, arg)) {
3928 print_err_oom();
3929 goto error;
3930 }
3931 break;
3932 case OPT_OMIT_SYSTEM_PLUGIN_PATH:
3933 force_omit_system_plugin_path = true;
3934
3935 if (bt_private_value_array_append_string_element(run_args,
3936 "--omit-system-plugin-path")) {
3937 print_err_oom();
3938 goto error;
3939 }
3940 break;
3941 case OPT_PLUGIN_PATH:
3942 if (bt_config_append_plugin_paths_check_setuid_setgid(
3943 plugin_paths, arg)) {
3944 goto error;
3945 }
3946
3947 if (bt_private_value_array_append_string_element(run_args,
3948 "--plugin-path")) {
3949 print_err_oom();
3950 goto error;
3951 }
3952
3953 if (bt_private_value_array_append_string_element(run_args, arg)) {
3954 print_err_oom();
3955 goto error;
3956 }
3957 break;
3958 case OPT_HELP:
3959 print_convert_usage(stdout);
3960 *retcode = -1;
3961 BT_OBJECT_PUT_REF_AND_RESET(cfg);
3962 goto end;
3963 case OPT_BEGIN:
3964 case OPT_CLOCK_CYCLES:
3965 case OPT_CLOCK_DATE:
3966 case OPT_CLOCK_FORCE_CORRELATE:
3967 case OPT_CLOCK_GMT:
3968 case OPT_CLOCK_OFFSET:
3969 case OPT_CLOCK_OFFSET_NS:
3970 case OPT_CLOCK_SECONDS:
3971 case OPT_COLOR:
3972 case OPT_DEBUG:
3973 case OPT_DEBUG_INFO:
3974 case OPT_DEBUG_INFO_DIR:
3975 case OPT_DEBUG_INFO_FULL_PATH:
3976 case OPT_DEBUG_INFO_TARGET_PREFIX:
3977 case OPT_END:
3978 case OPT_FIELDS:
3979 case OPT_INPUT_FORMAT:
3980 case OPT_NAMES:
3981 case OPT_NO_DELTA:
3982 case OPT_OUTPUT_FORMAT:
3983 case OPT_OUTPUT:
3984 case OPT_RUN_ARGS:
3985 case OPT_RUN_ARGS_0:
3986 case OPT_STREAM_INTERSECTION:
3987 case OPT_TIMERANGE:
3988 case OPT_VERBOSE:
3989 /* Ignore in this pass */
3990 break;
3991 default:
3992 printf_err("Unknown command-line option specified (option code %d)\n",
3993 opt);
3994 goto error;
3995 }
3996
3997 free(arg);
3998 arg = NULL;
3999 }
4000
4001 /* Append current component's name if needed */
4002 ret = convert_append_name_param(cur_comp_dest, cur_name,
4003 cur_name_prefix, run_args, all_names, &source_names,
4004 &filter_names, &sink_names);
4005 if (ret) {
4006 goto error;
4007 }
4008
4009 /* Check for option parsing error */
4010 if (opt < -1) {
4011 printf_err("While parsing command-line options, at option %s: %s\n",
4012 poptBadOption(pc, 0), poptStrerror(opt));
4013 goto error;
4014 }
4015
4016 poptFreeContext(pc);
4017 free(arg);
4018 arg = NULL;
4019
4020 /*
4021 * Second pass: transform the convert-specific options and
4022 * arguments into implicit component instances for the run
4023 * command.
4024 */
4025 pc = poptGetContext(NULL, argc, (const char **) argv,
4026 convert_long_options, 0);
4027 if (!pc) {
4028 printf_err("Cannot get popt context\n");
4029 goto error;
4030 }
4031
4032 poptReadDefaultConfig(pc, 0);
4033
4034 while ((opt = poptGetNextOpt(pc)) > 0) {
4035 arg = poptGetOptArg(pc);
4036
4037 switch (opt) {
4038 case OPT_BEGIN:
4039 if (trimmer_has_begin) {
4040 printf("At --begin option: --begin or --timerange option already specified\n %s\n",
4041 arg);
4042 goto error;
4043 }
4044
4045 trimmer_has_begin = true;
4046 ret = append_implicit_component_extra_param(
4047 &implicit_trimmer_args, "begin", arg);
4048 implicit_trimmer_args.exists = true;
4049 if (ret) {
4050 goto error;
4051 }
4052 break;
4053 case OPT_END:
4054 if (trimmer_has_end) {
4055 printf("At --end option: --end or --timerange option already specified\n %s\n",
4056 arg);
4057 goto error;
4058 }
4059
4060 trimmer_has_end = true;
4061 ret = append_implicit_component_extra_param(
4062 &implicit_trimmer_args, "end", arg);
4063 implicit_trimmer_args.exists = true;
4064 if (ret) {
4065 goto error;
4066 }
4067 break;
4068 case OPT_TIMERANGE:
4069 {
4070 char *begin;
4071 char *end;
4072
4073 if (trimmer_has_begin || trimmer_has_end) {
4074 printf("At --timerange option: --begin, --end, or --timerange option already specified\n %s\n",
4075 arg);
4076 goto error;
4077 }
4078
4079 ret = split_timerange(arg, &begin, &end);
4080 if (ret) {
4081 printf_err("Invalid --timerange option's argument: expecting BEGIN,END or [BEGIN,END]:\n %s\n",
4082 arg);
4083 goto error;
4084 }
4085
4086 ret = append_implicit_component_extra_param(
4087 &implicit_trimmer_args, "begin", begin);
4088 ret |= append_implicit_component_extra_param(
4089 &implicit_trimmer_args, "end", end);
4090 implicit_trimmer_args.exists = true;
4091 free(begin);
4092 free(end);
4093 if (ret) {
4094 goto error;
4095 }
4096 break;
4097 }
4098 case OPT_CLOCK_CYCLES:
4099 append_implicit_component_param(
4100 &implicit_text_args, "clock-cycles", "yes");
4101 implicit_text_args.exists = true;
4102 break;
4103 case OPT_CLOCK_DATE:
4104 append_implicit_component_param(
4105 &implicit_text_args, "clock-date", "yes");
4106 implicit_text_args.exists = true;
4107 break;
4108 case OPT_CLOCK_FORCE_CORRELATE:
4109 append_implicit_component_param(
4110 &implicit_muxer_args,
4111 "assume-absolute-clock-classes", "yes");
4112 break;
4113 case OPT_CLOCK_GMT:
4114 append_implicit_component_param(
4115 &implicit_text_args, "clock-gmt", "yes");
4116 append_implicit_component_param(
4117 &implicit_trimmer_args, "clock-gmt", "yes");
4118 implicit_text_args.exists = true;
4119 break;
4120 case OPT_CLOCK_OFFSET:
4121 base_implicit_ctf_input_args.exists = true;
4122 append_implicit_component_param(
4123 &base_implicit_ctf_input_args,
4124 "clock-class-offset-s", arg);
4125 break;
4126 case OPT_CLOCK_OFFSET_NS:
4127 base_implicit_ctf_input_args.exists = true;
4128 append_implicit_component_param(
4129 &base_implicit_ctf_input_args,
4130 "clock-class-offset-ns", arg);
4131 break;
4132 case OPT_CLOCK_SECONDS:
4133 append_implicit_component_param(
4134 &implicit_text_args, "clock-seconds", "yes");
4135 implicit_text_args.exists = true;
4136 break;
4137 case OPT_COLOR:
4138 implicit_text_args.exists = true;
4139 ret = append_implicit_component_extra_param(
4140 &implicit_text_args, "color", arg);
4141 if (ret) {
4142 goto error;
4143 }
4144 break;
4145 case OPT_DEBUG_INFO:
4146 implicit_debug_info_args.exists = true;
4147 break;
4148 case OPT_DEBUG_INFO_DIR:
4149 implicit_debug_info_args.exists = true;
4150 ret = append_implicit_component_extra_param(
4151 &implicit_debug_info_args, "debug-info-dir", arg);
4152 if (ret) {
4153 goto error;
4154 }
4155 break;
4156 case OPT_DEBUG_INFO_FULL_PATH:
4157 implicit_debug_info_args.exists = true;
4158 append_implicit_component_param(
4159 &implicit_debug_info_args, "full-path", "yes");
4160 break;
4161 case OPT_DEBUG_INFO_TARGET_PREFIX:
4162 implicit_debug_info_args.exists = true;
4163 ret = append_implicit_component_extra_param(
4164 &implicit_debug_info_args,
4165 "target-prefix", arg);
4166 if (ret) {
4167 goto error;
4168 }
4169 break;
4170 case OPT_FIELDS:
4171 {
4172 struct bt_private_value *fields = fields_from_arg(arg);
4173
4174 if (!fields) {
4175 goto error;
4176 }
4177
4178 implicit_text_args.exists = true;
4179 ret = insert_flat_params_from_array(
4180 implicit_text_args.params_arg,
4181 bt_private_value_borrow_value(fields), "field");
4182 bt_object_put_ref(fields);
4183 if (ret) {
4184 goto error;
4185 }
4186 break;
4187 }
4188 case OPT_NAMES:
4189 {
4190 struct bt_private_value *names = names_from_arg(arg);
4191
4192 if (!names) {
4193 goto error;
4194 }
4195
4196 implicit_text_args.exists = true;
4197 ret = insert_flat_params_from_array(
4198 implicit_text_args.params_arg,
4199 bt_private_value_borrow_value(names), "name");
4200 bt_object_put_ref(names);
4201 if (ret) {
4202 goto error;
4203 }
4204 break;
4205 }
4206 case OPT_NO_DELTA:
4207 append_implicit_component_param(
4208 &implicit_text_args, "no-delta", "yes");
4209 implicit_text_args.exists = true;
4210 break;
4211 case OPT_INPUT_FORMAT:
4212 if (got_input_format_opt) {
4213 printf_err("Duplicate --input-format option\n");
4214 goto error;
4215 }
4216
4217 got_input_format_opt = true;
4218
4219 if (strcmp(arg, "ctf") == 0) {
4220 base_implicit_ctf_input_args.exists = true;
4221 } else if (strcmp(arg, "lttng-live") == 0) {
4222 implicit_lttng_live_args.exists = true;
4223 } else {
4224 printf_err("Unknown legacy input format:\n %s\n",
4225 arg);
4226 goto error;
4227 }
4228 break;
4229 case OPT_OUTPUT_FORMAT:
4230 if (got_output_format_opt) {
4231 printf_err("Duplicate --output-format option\n");
4232 goto error;
4233 }
4234
4235 got_output_format_opt = true;
4236
4237 if (strcmp(arg, "text") == 0) {
4238 implicit_text_args.exists = true;
4239 } else if (strcmp(arg, "ctf") == 0) {
4240 implicit_ctf_output_args.exists = true;
4241 } else if (strcmp(arg, "dummy") == 0) {
4242 implicit_dummy_args.exists = true;
4243 } else if (strcmp(arg, "ctf-metadata") == 0) {
4244 print_ctf_metadata = true;
4245 } else {
4246 printf_err("Unknown legacy output format:\n %s\n",
4247 arg);
4248 goto error;
4249 }
4250 break;
4251 case OPT_OUTPUT:
4252 if (output) {
4253 printf_err("Duplicate --output option\n");
4254 goto error;
4255 }
4256
4257 output = strdup(arg);
4258 if (!output) {
4259 print_err_oom();
4260 goto error;
4261 }
4262 break;
4263 case OPT_RUN_ARGS:
4264 if (print_run_args_0) {
4265 printf_err("Cannot specify --run-args and --run-args-0\n");
4266 goto error;
4267 }
4268
4269 print_run_args = true;
4270 break;
4271 case OPT_RUN_ARGS_0:
4272 if (print_run_args) {
4273 printf_err("Cannot specify --run-args and --run-args-0\n");
4274 goto error;
4275 }
4276
4277 print_run_args_0 = true;
4278 break;
4279 case OPT_STREAM_INTERSECTION:
4280 /*
4281 * Applies to all traces implementing the trace-info
4282 * query.
4283 */
4284 stream_intersection_mode = true;
4285 break;
4286 case OPT_VERBOSE:
4287 if (*log_level != 'V' && *log_level != 'D') {
4288 *log_level = 'I';
4289 }
4290 break;
4291 case OPT_DEBUG:
4292 *log_level = 'V';
4293 break;
4294 }
4295
4296 free(arg);
4297 arg = NULL;
4298 }
4299
4300 /* Check for option parsing error */
4301 if (opt < -1) {
4302 printf_err("While parsing command-line options, at option %s: %s\n",
4303 poptBadOption(pc, 0), poptStrerror(opt));
4304 goto error;
4305 }
4306
4307 /*
4308 * Legacy behaviour: --verbose used to make the `text` output
4309 * format print more information. --verbose is now equivalent to
4310 * the INFO log level, which is why we compare to 'I' here.
4311 */
4312 if (*log_level == 'I') {
4313 append_implicit_component_param(&implicit_text_args,
4314 "verbose", "yes");
4315 }
4316
4317 /*
4318 * Append home and system plugin paths now that we possibly got
4319 * --plugin-path.
4320 */
4321 if (append_home_and_system_plugin_paths(plugin_paths,
4322 force_omit_system_plugin_path,
4323 force_omit_home_plugin_path)) {
4324 goto error;
4325 }
4326
4327 /* Consume and keep leftover arguments */
4328 while ((leftover = poptGetArg(pc))) {
4329 GString *gs_leftover = g_string_new(leftover);
4330
4331 if (!gs_leftover) {
4332 print_err_oom();
4333 goto error;
4334 }
4335
4336 leftovers = g_list_append(leftovers, gs_leftover);
4337 if (!leftovers) {
4338 g_string_free(gs_leftover, TRUE);
4339 print_err_oom();
4340 goto error;
4341 }
4342 }
4343
4344 /* Print CTF metadata or print LTTng live sessions */
4345 if (print_ctf_metadata) {
4346 GString *gs_leftover;
4347
4348 if (g_list_length(leftovers) == 0) {
4349 printf_err("--output-format=ctf-metadata specified without a path\n");
4350 goto error;
4351 }
4352
4353 if (g_list_length(leftovers) > 1) {
4354 printf_err("Too many paths specified for --output-format=ctf-metadata\n");
4355 goto error;
4356 }
4357
4358 cfg = bt_config_print_ctf_metadata_create(plugin_paths);
4359 if (!cfg) {
4360 goto error;
4361 }
4362
4363 gs_leftover = leftovers->data;
4364 g_string_assign(cfg->cmd_data.print_ctf_metadata.path,
4365 gs_leftover->str);
4366
4367 if (output) {
4368 g_string_assign(
4369 cfg->cmd_data.print_ctf_metadata.output_path,
4370 output);
4371 }
4372
4373 goto end;
4374 }
4375
4376 /*
4377 * If -o ctf was specified, make sure an output path (--output)
4378 * was also specified. --output does not imply -o ctf because
4379 * it's also used for the default, implicit -o text if -o ctf
4380 * is not specified.
4381 */
4382 if (implicit_ctf_output_args.exists) {
4383 if (!output) {
4384 printf_err("--output-format=ctf specified without --output (trace output path)\n");
4385 goto error;
4386 }
4387
4388 /*
4389 * At this point we know that -o ctf AND --output were
4390 * specified. Make sure that no options were specified
4391 * which would imply -o text because --output would be
4392 * ambiguous in this case. For example, this is wrong:
4393 *
4394 * babeltrace --names=all -o ctf --output=/tmp/path my-trace
4395 *
4396 * because --names=all implies -o text, and --output
4397 * could apply to both the sink.text.pretty and
4398 * sink.ctf.fs implicit components.
4399 */
4400 if (implicit_text_args.exists) {
4401 printf_err("Ambiguous --output option: --output-format=ctf specified but another option implies --output-format=text\n");
4402 goto error;
4403 }
4404 }
4405
4406 /*
4407 * If -o dummy and -o ctf were not specified, and if there are
4408 * no explicit sink components, then use an implicit
4409 * `sink.text.pretty` component.
4410 */
4411 if (!implicit_dummy_args.exists && !implicit_ctf_output_args.exists &&
4412 !sink_names) {
4413 implicit_text_args.exists = true;
4414 }
4415
4416 /*
4417 * Set implicit `sink.text.pretty` or `sink.ctf.fs` component's
4418 * `path` parameter if --output was specified.
4419 */
4420 if (output) {
4421 if (implicit_text_args.exists) {
4422 append_implicit_component_extra_param(&implicit_text_args,
4423 "path", output);
4424 } else if (implicit_ctf_output_args.exists) {
4425 append_implicit_component_extra_param(&implicit_ctf_output_args,
4426 "path", output);
4427 }
4428 }
4429
4430 /* Decide where the leftover argument(s) go */
4431 if (g_list_length(leftovers) > 0) {
4432 if (implicit_lttng_live_args.exists) {
4433 GString *gs_leftover;
4434
4435 if (g_list_length(leftovers) > 1) {
4436 printf_err("Too many URLs specified for --output-format=lttng-live\n");
4437 goto error;
4438 }
4439
4440 gs_leftover = leftovers->data;
4441 lttng_live_url_parts =
4442 bt_common_parse_lttng_live_url(gs_leftover->str,
4443 error_buf, sizeof(error_buf));
4444 if (!lttng_live_url_parts.proto) {
4445 printf_err("Invalid LTTng live URL format: %s\n",
4446 error_buf);
4447 goto error;
4448 }
4449
4450 if (!lttng_live_url_parts.session_name) {
4451 /* Print LTTng live sessions */
4452 cfg = bt_config_print_lttng_live_sessions_create(
4453 plugin_paths);
4454 if (!cfg) {
4455 goto error;
4456 }
4457
4458 g_string_assign(cfg->cmd_data.print_lttng_live_sessions.url,
4459 gs_leftover->str);
4460
4461 if (output) {
4462 g_string_assign(
4463 cfg->cmd_data.print_lttng_live_sessions.output_path,
4464 output);
4465 }
4466
4467 goto end;
4468 }
4469
4470 ret = append_implicit_component_extra_param(
4471 &implicit_lttng_live_args, "url",
4472 gs_leftover->str);
4473 if (ret) {
4474 goto error;
4475 }
4476 } else {
4477 /*
4478 * Append one implicit component argument set
4479 * for each leftover (souce.ctf.fs paths). Copy
4480 * the base implicit component arguments.
4481 * Note that they still have to be named later.
4482 */
4483 ret = fill_implicit_ctf_inputs_args(
4484 implicit_ctf_inputs_args,
4485 &base_implicit_ctf_input_args, leftovers);
4486 if (ret) {
4487 goto error;
4488 }
4489 }
4490 }
4491
4492 /*
4493 * Ensure mutual exclusion between implicit `source.ctf.fs` and
4494 * `source.ctf.lttng-live` components.
4495 */
4496 if (base_implicit_ctf_input_args.exists &&
4497 implicit_lttng_live_args.exists) {
4498 printf_err("Cannot create both implicit `%s` and `%s` components\n",
4499 base_implicit_ctf_input_args.comp_arg->str,
4500 implicit_lttng_live_args.comp_arg->str);
4501 goto error;
4502 }
4503
4504 /*
4505 * If the implicit `source.ctf.fs` or `source.ctf.lttng-live`
4506 * components exists, make sure there's at least one leftover
4507 * (which is the path or URL).
4508 */
4509 if (base_implicit_ctf_input_args.exists &&
4510 g_list_length(leftovers) == 0) {
4511 printf_err("Missing path for implicit `%s` component\n",
4512 base_implicit_ctf_input_args.comp_arg->str);
4513 goto error;
4514 }
4515
4516 if (implicit_lttng_live_args.exists && g_list_length(leftovers) == 0) {
4517 printf_err("Missing URL for implicit `%s` component\n",
4518 implicit_lttng_live_args.comp_arg->str);
4519 goto error;
4520 }
4521
4522 /* Assign names to implicit components */
4523 for (i = 0; i < implicit_ctf_inputs_args->len; i++) {
4524 struct implicit_component_args *impl_args =
4525 g_ptr_array_index(implicit_ctf_inputs_args, i);
4526
4527 ret = assign_name_to_implicit_component(impl_args,
4528 "source-ctf-fs", all_names, &source_names, true);
4529 if (ret) {
4530 goto error;
4531 }
4532 }
4533
4534 ret = assign_name_to_implicit_component(&implicit_lttng_live_args,
4535 "lttng-live", all_names, &source_names, true);
4536 if (ret) {
4537 goto error;
4538 }
4539
4540 ret = assign_name_to_implicit_component(&implicit_text_args,
4541 "pretty", all_names, &sink_names, true);
4542 if (ret) {
4543 goto error;
4544 }
4545
4546 ret = assign_name_to_implicit_component(&implicit_ctf_output_args,
4547 "sink-ctf-fs", all_names, &sink_names, true);
4548 if (ret) {
4549 goto error;
4550 }
4551
4552 ret = assign_name_to_implicit_component(&implicit_dummy_args,
4553 "dummy", all_names, &sink_names, true);
4554 if (ret) {
4555 goto error;
4556 }
4557
4558 ret = assign_name_to_implicit_component(&implicit_muxer_args,
4559 "muxer", all_names, NULL, false);
4560 if (ret) {
4561 goto error;
4562 }
4563
4564 ret = assign_name_to_implicit_component(&implicit_trimmer_args,
4565 "trimmer", all_names, NULL, false);
4566 if (ret) {
4567 goto error;
4568 }
4569
4570 ret = assign_name_to_implicit_component(&implicit_debug_info_args,
4571 "debug-info", all_names, NULL, false);
4572 if (ret) {
4573 goto error;
4574 }
4575
4576 /* Make sure there's at least one source and one sink */
4577 if (!source_names) {
4578 printf_err("No source component\n");
4579 goto error;
4580 }
4581
4582 if (!sink_names) {
4583 printf_err("No sink component\n");
4584 goto error;
4585 }
4586
4587 /*
4588 * Prepend the muxer, the trimmer, and the debug info to the
4589 * filter chain so that we have:
4590 *
4591 * sources -> muxer -> [trimmer] -> [debug info] ->
4592 * [user filters] -> sinks
4593 */
4594 if (implicit_debug_info_args.exists) {
4595 if (g_list_prepend_gstring(&filter_names,
4596 implicit_debug_info_args.name_arg->str)) {
4597 goto error;
4598 }
4599 }
4600
4601 if (implicit_trimmer_args.exists) {
4602 if (g_list_prepend_gstring(&filter_names,
4603 implicit_trimmer_args.name_arg->str)) {
4604 goto error;
4605 }
4606 }
4607
4608 if (g_list_prepend_gstring(&filter_names,
4609 implicit_muxer_args.name_arg->str)) {
4610 goto error;
4611 }
4612
4613 /*
4614 * Append the equivalent run arguments for the implicit
4615 * components.
4616 */
4617 for (i = 0; i < implicit_ctf_inputs_args->len; i++) {
4618 struct implicit_component_args *impl_args =
4619 g_ptr_array_index(implicit_ctf_inputs_args, i);
4620
4621 ret = append_run_args_for_implicit_component(impl_args,
4622 run_args);
4623 if (ret) {
4624 goto error;
4625 }
4626 }
4627
4628 ret = append_run_args_for_implicit_component(&implicit_lttng_live_args,
4629 run_args);
4630 if (ret) {
4631 goto error;
4632 }
4633
4634 ret = append_run_args_for_implicit_component(&implicit_text_args,
4635 run_args);
4636 if (ret) {
4637 goto error;
4638 }
4639
4640 ret = append_run_args_for_implicit_component(&implicit_ctf_output_args,
4641 run_args);
4642 if (ret) {
4643 goto error;
4644 }
4645
4646 ret = append_run_args_for_implicit_component(&implicit_dummy_args,
4647 run_args);
4648 if (ret) {
4649 goto error;
4650 }
4651
4652 ret = append_run_args_for_implicit_component(&implicit_muxer_args,
4653 run_args);
4654 if (ret) {
4655 goto error;
4656 }
4657
4658 ret = append_run_args_for_implicit_component(&implicit_trimmer_args,
4659 run_args);
4660 if (ret) {
4661 goto error;
4662 }
4663
4664 ret = append_run_args_for_implicit_component(&implicit_debug_info_args,
4665 run_args);
4666 if (ret) {
4667 goto error;
4668 }
4669
4670 /* Auto-connect components */
4671 ret = convert_auto_connect(run_args, source_names, filter_names,
4672 sink_names);
4673 if (ret) {
4674 printf_err("Cannot auto-connect components\n");
4675 goto error;
4676 }
4677
4678 /*
4679 * We have all the run command arguments now. Depending on
4680 * --run-args, we pass this to the run command or print them
4681 * here.
4682 */
4683 if (print_run_args || print_run_args_0) {
4684 if (stream_intersection_mode) {
4685 printf_err("Cannot specify --stream-intersection with --run-args or --run-args-0\n");
4686 goto error;
4687 }
4688
4689 for (i = 0; i < bt_value_array_get_size(
4690 bt_private_value_borrow_value(run_args)); i++) {
4691 struct bt_value *arg_value =
4692 bt_value_array_borrow_element_by_index(
4693 bt_private_value_borrow_value(run_args),
4694 i);
4695 const char *arg;
4696 GString *quoted = NULL;
4697 const char *arg_to_print;
4698
4699 BT_ASSERT(arg_value);
4700 arg = bt_value_string_get(arg_value);
4701
4702 if (print_run_args) {
4703 quoted = bt_common_shell_quote(arg, true);
4704 if (!quoted) {
4705 goto error;
4706 }
4707
4708 arg_to_print = quoted->str;
4709 } else {
4710 arg_to_print = arg;
4711 }
4712
4713 printf("%s", arg_to_print);
4714
4715 if (quoted) {
4716 g_string_free(quoted, TRUE);
4717 }
4718
4719 if (i < bt_value_array_get_size(
4720 bt_private_value_borrow_value(run_args)) - 1) {
4721 if (print_run_args) {
4722 putchar(' ');
4723 } else {
4724 putchar('\0');
4725 }
4726 }
4727 }
4728
4729 *retcode = -1;
4730 BT_OBJECT_PUT_REF_AND_RESET(cfg);
4731 goto end;
4732 }
4733
4734 cfg = bt_config_run_from_args_array(
4735 bt_private_value_borrow_value(run_args), retcode,
4736 force_omit_system_plugin_path, force_omit_home_plugin_path,
4737 initial_plugin_paths);
4738 if (!cfg) {
4739 goto error;
4740 }
4741
4742 cfg->cmd_data.run.stream_intersection_mode = stream_intersection_mode;
4743 goto end;
4744
4745 error:
4746 *retcode = 1;
4747 BT_OBJECT_PUT_REF_AND_RESET(cfg);
4748
4749 end:
4750 if (pc) {
4751 poptFreeContext(pc);
4752 }
4753
4754 free(arg);
4755 free(output);
4756
4757 if (cur_name) {
4758 g_string_free(cur_name, TRUE);
4759 }
4760
4761 if (cur_name_prefix) {
4762 g_string_free(cur_name_prefix, TRUE);
4763 }
4764
4765 if (implicit_ctf_inputs_args) {
4766 g_ptr_array_free(implicit_ctf_inputs_args, TRUE);
4767 }
4768
4769 bt_object_put_ref(run_args);
4770 bt_object_put_ref(all_names);
4771 destroy_glist_of_gstring(source_names);
4772 destroy_glist_of_gstring(filter_names);
4773 destroy_glist_of_gstring(sink_names);
4774 destroy_glist_of_gstring(leftovers);
4775 finalize_implicit_component_args(&base_implicit_ctf_input_args);
4776 finalize_implicit_component_args(&implicit_ctf_output_args);
4777 finalize_implicit_component_args(&implicit_lttng_live_args);
4778 finalize_implicit_component_args(&implicit_dummy_args);
4779 finalize_implicit_component_args(&implicit_text_args);
4780 finalize_implicit_component_args(&implicit_debug_info_args);
4781 finalize_implicit_component_args(&implicit_muxer_args);
4782 finalize_implicit_component_args(&implicit_trimmer_args);
4783 bt_object_put_ref(plugin_paths);
4784 bt_common_destroy_lttng_live_url_parts(&lttng_live_url_parts);
4785 return cfg;
4786 }
4787
4788 /*
4789 * Prints the Babeltrace 2.x general usage.
4790 */
4791 static
4792 void print_gen_usage(FILE *fp)
4793 {
4794 fprintf(fp, "Usage: babeltrace [GENERAL OPTIONS] [COMMAND] [COMMAND ARGUMENTS]\n");
4795 fprintf(fp, "\n");
4796 fprintf(fp, "General options:\n");
4797 fprintf(fp, "\n");
4798 fprintf(fp, " -d, --debug Enable debug mode (same as --log-level=V)\n");
4799 fprintf(fp, " -h, --help Show this help and quit\n");
4800 fprintf(fp, " -l, --log-level=LVL Set all log levels to LVL (`N`, `V`, `D`,\n");
4801 fprintf(fp, " `I`, `W` (default), `E`, or `F`)\n");
4802 fprintf(fp, " -v, --verbose Enable verbose mode (same as --log-level=I)\n");
4803 fprintf(fp, " -V, --version Show version and quit\n");
4804 fprintf(fp, "\n");
4805 fprintf(fp, "Available commands:\n");
4806 fprintf(fp, "\n");
4807 fprintf(fp, " convert Convert and trim traces (default)\n");
4808 fprintf(fp, " help Get help for a plugin or a component class\n");
4809 fprintf(fp, " list-plugins List available plugins and their content\n");
4810 fprintf(fp, " query Query objects from a component class\n");
4811 fprintf(fp, " run Build a processing graph and run it\n");
4812 fprintf(fp, "\n");
4813 fprintf(fp, "Use `babeltrace COMMAND --help` to show the help of COMMAND.\n");
4814 }
4815
4816 static
4817 char log_level_from_arg(const char *arg)
4818 {
4819 char level = 'U';
4820
4821 if (strcmp(arg, "VERBOSE") == 0 ||
4822 strcmp(arg, "V") == 0) {
4823 level = 'V';
4824 } else if (strcmp(arg, "DEBUG") == 0 ||
4825 strcmp(arg, "D") == 0) {
4826 level = 'D';
4827 } else if (strcmp(arg, "INFO") == 0 ||
4828 strcmp(arg, "I") == 0) {
4829 level = 'I';
4830 } else if (strcmp(arg, "WARN") == 0 ||
4831 strcmp(arg, "WARNING") == 0 ||
4832 strcmp(arg, "W") == 0) {
4833 level = 'W';
4834 } else if (strcmp(arg, "ERROR") == 0 ||
4835 strcmp(arg, "E") == 0) {
4836 level = 'E';
4837 } else if (strcmp(arg, "FATAL") == 0 ||
4838 strcmp(arg, "F") == 0) {
4839 level = 'F';
4840 } else if (strcmp(arg, "NONE") == 0 ||
4841 strcmp(arg, "N") == 0) {
4842 level = 'N';
4843 }
4844
4845 return level;
4846 }
4847
4848 struct bt_config *bt_config_cli_args_create(int argc, const char *argv[],
4849 int *retcode, bool force_omit_system_plugin_path,
4850 bool force_omit_home_plugin_path,
4851 struct bt_private_value *initial_plugin_paths)
4852 {
4853 struct bt_config *config = NULL;
4854 int i;
4855 const char **command_argv = NULL;
4856 int command_argc = -1;
4857 const char *command_name = NULL;
4858 char log_level = 'U';
4859
4860 enum command_type {
4861 COMMAND_TYPE_NONE = -1,
4862 COMMAND_TYPE_RUN = 0,
4863 COMMAND_TYPE_CONVERT,
4864 COMMAND_TYPE_LIST_PLUGINS,
4865 COMMAND_TYPE_HELP,
4866 COMMAND_TYPE_QUERY,
4867 } command_type = COMMAND_TYPE_NONE;
4868
4869 *retcode = -1;
4870
4871 if (!initial_plugin_paths) {
4872 initial_plugin_paths = bt_private_value_array_create();
4873 if (!initial_plugin_paths) {
4874 *retcode = 1;
4875 goto end;
4876 }
4877 } else {
4878 bt_object_get_ref(initial_plugin_paths);
4879 }
4880
4881 if (argc <= 1) {
4882 print_version();
4883 puts("");
4884 print_gen_usage(stdout);
4885 goto end;
4886 }
4887
4888 for (i = 1; i < argc; i++) {
4889 const char *cur_arg = argv[i];
4890 const char *next_arg = i == (argc - 1) ? NULL : argv[i + 1];
4891
4892 if (strcmp(cur_arg, "-d") == 0 ||
4893 strcmp(cur_arg, "--debug") == 0) {
4894 log_level = 'V';
4895 } else if (strcmp(cur_arg, "-v") == 0 ||
4896 strcmp(cur_arg, "--verbose") == 0) {
4897 if (log_level != 'V' && log_level != 'D') {
4898 /*
4899 * Legacy: do not override a previous
4900 * --debug because --verbose and --debug
4901 * can be specified together (in this
4902 * case we want the lowest log level to
4903 * apply, VERBOSE).
4904 */
4905 log_level = 'I';
4906 }
4907 } else if (strcmp(cur_arg, "--log-level") == 0 ||
4908 strcmp(cur_arg, "-l") == 0) {
4909 if (!next_arg) {
4910 printf_err("Missing log level value for --log-level option\n");
4911 *retcode = 1;
4912 goto end;
4913 }
4914
4915 log_level = log_level_from_arg(next_arg);
4916 if (log_level == 'U') {
4917 printf_err("Invalid argument for --log-level option:\n %s\n",
4918 next_arg);
4919 *retcode = 1;
4920 goto end;
4921 }
4922
4923 i++;
4924 } else if (strncmp(cur_arg, "--log-level=", 12) == 0) {
4925 const char *arg = &cur_arg[12];
4926
4927 log_level = log_level_from_arg(arg);
4928 if (log_level == 'U') {
4929 printf_err("Invalid argument for --log-level option:\n %s\n",
4930 arg);
4931 *retcode = 1;
4932 goto end;
4933 }
4934 } else if (strncmp(cur_arg, "-l", 2) == 0) {
4935 const char *arg = &cur_arg[2];
4936
4937 log_level = log_level_from_arg(arg);
4938 if (log_level == 'U') {
4939 printf_err("Invalid argument for --log-level option:\n %s\n",
4940 arg);
4941 *retcode = 1;
4942 goto end;
4943 }
4944 } else if (strcmp(cur_arg, "-V") == 0 ||
4945 strcmp(cur_arg, "--version") == 0) {
4946 print_version();
4947 goto end;
4948 } else if (strcmp(cur_arg, "-h") == 0 ||
4949 strcmp(cur_arg, "--help") == 0) {
4950 print_gen_usage(stdout);
4951 goto end;
4952 } else {
4953 /*
4954 * First unknown argument: is it a known command
4955 * name?
4956 */
4957 command_argv = &argv[i];
4958 command_argc = argc - i;
4959
4960 if (strcmp(cur_arg, "convert") == 0) {
4961 command_type = COMMAND_TYPE_CONVERT;
4962 } else if (strcmp(cur_arg, "list-plugins") == 0) {
4963 command_type = COMMAND_TYPE_LIST_PLUGINS;
4964 } else if (strcmp(cur_arg, "help") == 0) {
4965 command_type = COMMAND_TYPE_HELP;
4966 } else if (strcmp(cur_arg, "query") == 0) {
4967 command_type = COMMAND_TYPE_QUERY;
4968 } else if (strcmp(cur_arg, "run") == 0) {
4969 command_type = COMMAND_TYPE_RUN;
4970 } else {
4971 /*
4972 * Unknown argument, but not a known
4973 * command name: assume the default
4974 * `convert` command.
4975 */
4976 command_type = COMMAND_TYPE_CONVERT;
4977 command_name = "convert";
4978 command_argv = &argv[i - 1];
4979 command_argc = argc - i + 1;
4980 }
4981 break;
4982 }
4983 }
4984
4985 if (command_type == COMMAND_TYPE_NONE) {
4986 /*
4987 * We only got non-help, non-version general options
4988 * like --verbose and --debug, without any other
4989 * arguments, so we can't do anything useful: print the
4990 * usage and quit.
4991 */
4992 print_gen_usage(stdout);
4993 goto end;
4994 }
4995
4996 BT_ASSERT(command_argv);
4997 BT_ASSERT(command_argc >= 0);
4998
4999 switch (command_type) {
5000 case COMMAND_TYPE_RUN:
5001 config = bt_config_run_from_args(command_argc, command_argv,
5002 retcode, force_omit_system_plugin_path,
5003 force_omit_home_plugin_path, initial_plugin_paths);
5004 break;
5005 case COMMAND_TYPE_CONVERT:
5006 config = bt_config_convert_from_args(command_argc, command_argv,
5007 retcode, force_omit_system_plugin_path,
5008 force_omit_home_plugin_path,
5009 initial_plugin_paths, &log_level);
5010 break;
5011 case COMMAND_TYPE_LIST_PLUGINS:
5012 config = bt_config_list_plugins_from_args(command_argc,
5013 command_argv, retcode, force_omit_system_plugin_path,
5014 force_omit_home_plugin_path, initial_plugin_paths);
5015 break;
5016 case COMMAND_TYPE_HELP:
5017 config = bt_config_help_from_args(command_argc,
5018 command_argv, retcode, force_omit_system_plugin_path,
5019 force_omit_home_plugin_path, initial_plugin_paths);
5020 break;
5021 case COMMAND_TYPE_QUERY:
5022 config = bt_config_query_from_args(command_argc,
5023 command_argv, retcode, force_omit_system_plugin_path,
5024 force_omit_home_plugin_path, initial_plugin_paths);
5025 break;
5026 default:
5027 abort();
5028 }
5029
5030 if (config) {
5031 if (log_level == 'U') {
5032 log_level = 'W';
5033 }
5034
5035 config->log_level = log_level;
5036 config->command_name = command_name;
5037 }
5038
5039 end:
5040 bt_object_put_ref(initial_plugin_paths);
5041 return config;
5042 }
This page took 0.174324 seconds and 4 git commands to generate.