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