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