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