Commit | Line | Data |
---|---|---|
c42c79ea 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/values.h> | |
34 | #include <popt.h> | |
35 | #include <glib.h> | |
36 | #include "babeltrace-cfg.h" | |
37 | ||
38 | /* | |
39 | * Error printf() macro which prepends "Error: " the first time it's | |
40 | * called. This gives a nicer feel than having a bunch of error prefixes | |
41 | * (since the following lines usually describe the error and possible | |
42 | * solutions), or the error prefix just at the end. | |
43 | */ | |
44 | #define printf_err(fmt, args...) \ | |
45 | do { \ | |
46 | if (is_first_error) { \ | |
47 | fprintf(stderr, "Error: "); \ | |
48 | is_first_error = false; \ | |
49 | } \ | |
50 | fprintf(stderr, fmt, ##args); \ | |
51 | } while (0) | |
52 | ||
53 | static bool is_first_error = true; | |
54 | ||
55 | /* INI-style parsing FSM states */ | |
56 | enum ini_parsing_fsm_state { | |
57 | /* Expect a map key (identifier) */ | |
58 | INI_EXPECT_MAP_KEY, | |
59 | ||
60 | /* Expect an equal character ('=') */ | |
61 | INI_EXPECT_EQUAL, | |
62 | ||
63 | /* Expect a value */ | |
64 | INI_EXPECT_VALUE, | |
65 | ||
66 | /* Expect a negative number value */ | |
67 | INI_EXPECT_VALUE_NUMBER_NEG, | |
68 | ||
69 | /* Expect a comma character (',') */ | |
70 | INI_EXPECT_COMMA, | |
71 | }; | |
72 | ||
73 | /* INI-style parsing state variables */ | |
74 | struct ini_parsing_state { | |
75 | /* Lexical scanner (owned by this) */ | |
76 | GScanner *scanner; | |
77 | ||
78 | /* Output map value object being filled (owned by this) */ | |
79 | struct bt_value *params; | |
80 | ||
81 | /* Next expected FSM state */ | |
82 | enum ini_parsing_fsm_state expecting; | |
83 | ||
84 | /* Last decoded map key (owned by this) */ | |
85 | char *last_map_key; | |
86 | ||
87 | /* Complete INI-style string to parse (not owned by this) */ | |
88 | const char *arg; | |
89 | ||
90 | /* Error buffer (not owned by this) */ | |
91 | GString *ini_error; | |
92 | }; | |
93 | ||
94 | /* Offset option with "is set" boolean */ | |
95 | struct offset_opt { | |
96 | int64_t value; | |
97 | bool is_set; | |
98 | }; | |
99 | ||
100 | /* Legacy "ctf"/"lttng-live" format options */ | |
101 | struct ctf_legacy_opts { | |
102 | struct offset_opt offset_s; | |
103 | struct offset_opt offset_ns; | |
104 | bool stream_intersection; | |
105 | }; | |
106 | ||
107 | /* Legacy "text" format options */ | |
108 | struct text_legacy_opts { | |
109 | /* | |
110 | * output, dbg_info_dir, dbg_info_target_prefix, names, | |
111 | * and fields are owned by this. | |
112 | */ | |
113 | GString *output; | |
114 | GString *dbg_info_dir; | |
115 | GString *dbg_info_target_prefix; | |
116 | struct bt_value *names; | |
117 | struct bt_value *fields; | |
118 | ||
119 | /* Flags */ | |
120 | bool no_delta; | |
121 | bool clock_cycles; | |
122 | bool clock_seconds; | |
123 | bool clock_date; | |
124 | bool clock_gmt; | |
125 | bool dbg_info_full_path; | |
126 | }; | |
127 | ||
128 | /* Legacy input format format */ | |
129 | enum legacy_input_format { | |
130 | LEGACY_INPUT_FORMAT_NONE = 0, | |
131 | LEGACY_INPUT_FORMAT_CTF, | |
132 | LEGACY_INPUT_FORMAT_LTTNG_LIVE, | |
133 | }; | |
134 | ||
135 | /* Legacy output format format */ | |
136 | enum legacy_output_format { | |
137 | LEGACY_OUTPUT_FORMAT_NONE = 0, | |
138 | LEGACY_OUTPUT_FORMAT_TEXT, | |
139 | LEGACY_OUTPUT_FORMAT_CTF_METADATA, | |
140 | LEGACY_OUTPUT_FORMAT_DUMMY, | |
141 | }; | |
142 | ||
143 | /* | |
144 | * Prints the "out of memory" error. | |
145 | */ | |
146 | static | |
147 | void print_err_oom(void) | |
148 | { | |
149 | printf_err("Out of memory\n"); | |
150 | } | |
151 | ||
152 | /* | |
153 | * Prints duplicate legacy output format error. | |
154 | */ | |
155 | static | |
156 | void print_err_dup_legacy_output(void) | |
157 | { | |
158 | printf_err("More than one legacy output format specified\n"); | |
159 | } | |
160 | ||
161 | /* | |
162 | * Prints duplicate legacy input format error. | |
163 | */ | |
164 | static | |
165 | void print_err_dup_legacy_input(void) | |
166 | { | |
167 | printf_err("More than one legacy input format specified\n"); | |
168 | } | |
169 | ||
170 | /* | |
171 | * Checks if any of the "text" legacy options is set. | |
172 | */ | |
173 | static | |
174 | bool text_legacy_opts_is_any_set(struct text_legacy_opts *opts) | |
175 | { | |
176 | return (opts->output && opts->output->len > 0) || | |
177 | (opts->dbg_info_dir && opts->dbg_info_dir->len > 0) || | |
178 | (opts->dbg_info_target_prefix && | |
179 | opts->dbg_info_target_prefix->len > 0) || | |
180 | bt_value_array_size(opts->names) > 0 || | |
181 | bt_value_array_size(opts->fields) > 0 || | |
182 | opts->no_delta || opts->clock_cycles || opts->clock_seconds || | |
183 | opts->clock_date || opts->clock_gmt || | |
184 | opts->dbg_info_full_path; | |
185 | } | |
186 | ||
187 | /* | |
188 | * Checks if any of the "ctf" legacy options is set. | |
189 | */ | |
190 | static | |
191 | bool ctf_legacy_opts_is_any_set(struct ctf_legacy_opts *opts) | |
192 | { | |
193 | return opts->offset_s.is_set || opts->offset_ns.is_set || | |
194 | opts->stream_intersection; | |
195 | } | |
196 | ||
197 | /* | |
198 | * Appends an "expecting token" error to the INI-style parsing state's | |
199 | * error buffer. | |
200 | */ | |
201 | static | |
202 | void ini_append_error_expecting(struct ini_parsing_state *state, | |
203 | GScanner *scanner, const char *expecting) | |
204 | { | |
205 | size_t i; | |
206 | size_t pos; | |
207 | ||
208 | g_string_append_printf(state->ini_error, "Expecting %s:\n", expecting); | |
209 | ||
210 | /* Only print error if there's one line */ | |
211 | if (strchr(state->arg, '\n') != NULL || strlen(state->arg) == 0) { | |
212 | return; | |
213 | } | |
214 | ||
215 | g_string_append_printf(state->ini_error, "\n %s\n", state->arg); | |
216 | pos = g_scanner_cur_position(scanner) + 4; | |
217 | ||
218 | if (!g_scanner_eof(scanner)) { | |
219 | pos--; | |
220 | } | |
221 | ||
222 | for (i = 0; i < pos; ++i) { | |
223 | g_string_append_printf(state->ini_error, " "); | |
224 | } | |
225 | ||
226 | g_string_append_printf(state->ini_error, "^\n\n"); | |
227 | } | |
228 | ||
229 | static | |
230 | int ini_handle_state(struct ini_parsing_state *state) | |
231 | { | |
232 | int ret = 0; | |
233 | GTokenType token_type; | |
234 | struct bt_value *value = NULL; | |
235 | ||
236 | token_type = g_scanner_get_next_token(state->scanner); | |
237 | if (token_type == G_TOKEN_EOF) { | |
238 | if (state->expecting != INI_EXPECT_COMMA) { | |
239 | switch (state->expecting) { | |
240 | case INI_EXPECT_EQUAL: | |
241 | ini_append_error_expecting(state, | |
242 | state->scanner, "'='"); | |
243 | break; | |
244 | case INI_EXPECT_VALUE: | |
245 | case INI_EXPECT_VALUE_NUMBER_NEG: | |
246 | ini_append_error_expecting(state, | |
247 | state->scanner, "value"); | |
248 | break; | |
249 | case INI_EXPECT_MAP_KEY: | |
250 | ini_append_error_expecting(state, | |
251 | state->scanner, "unquoted map key"); | |
252 | break; | |
253 | default: | |
254 | break; | |
255 | } | |
256 | goto error; | |
257 | } | |
258 | ||
259 | /* We're done! */ | |
260 | ret = 1; | |
261 | goto success; | |
262 | } | |
263 | ||
264 | switch (state->expecting) { | |
265 | case INI_EXPECT_MAP_KEY: | |
266 | if (token_type != G_TOKEN_IDENTIFIER) { | |
267 | ini_append_error_expecting(state, state->scanner, | |
268 | "unquoted map key"); | |
269 | goto error; | |
270 | } | |
271 | ||
272 | free(state->last_map_key); | |
273 | state->last_map_key = | |
274 | strdup(state->scanner->value.v_identifier); | |
275 | if (!state->last_map_key) { | |
276 | g_string_append(state->ini_error, | |
277 | "Out of memory\n"); | |
278 | goto error; | |
279 | } | |
280 | ||
281 | if (bt_value_map_has_key(state->params, state->last_map_key)) { | |
282 | g_string_append_printf(state->ini_error, | |
6d1d5711 | 283 | "Duplicate parameter key: `%s`\n", |
c42c79ea PP |
284 | state->last_map_key); |
285 | goto error; | |
286 | } | |
287 | ||
288 | state->expecting = INI_EXPECT_EQUAL; | |
289 | goto success; | |
290 | case INI_EXPECT_EQUAL: | |
291 | if (token_type != G_TOKEN_CHAR) { | |
292 | ini_append_error_expecting(state, | |
293 | state->scanner, "'='"); | |
294 | goto error; | |
295 | } | |
296 | ||
297 | if (state->scanner->value.v_char != '=') { | |
298 | ini_append_error_expecting(state, | |
299 | state->scanner, "'='"); | |
300 | goto error; | |
301 | } | |
302 | ||
303 | state->expecting = INI_EXPECT_VALUE; | |
304 | goto success; | |
305 | case INI_EXPECT_VALUE: | |
306 | { | |
307 | switch (token_type) { | |
308 | case G_TOKEN_CHAR: | |
309 | if (state->scanner->value.v_char == '-') { | |
310 | /* Negative number */ | |
311 | state->expecting = | |
312 | INI_EXPECT_VALUE_NUMBER_NEG; | |
313 | goto success; | |
314 | } else { | |
315 | ini_append_error_expecting(state, | |
316 | state->scanner, "value"); | |
317 | goto error; | |
318 | } | |
319 | break; | |
320 | case G_TOKEN_INT: | |
321 | { | |
322 | /* Positive integer */ | |
323 | uint64_t int_val = state->scanner->value.v_int64; | |
324 | ||
325 | if (int_val > (1ULL << 63) - 1) { | |
326 | g_string_append_printf(state->ini_error, | |
327 | "Integer value %" PRIu64 " is outside the range of a 64-bit signed integer\n", | |
328 | int_val); | |
329 | goto error; | |
330 | } | |
331 | ||
332 | value = bt_value_integer_create_init( | |
333 | (int64_t) int_val); | |
334 | break; | |
335 | } | |
336 | case G_TOKEN_FLOAT: | |
337 | /* Positive floating point number */ | |
338 | value = bt_value_float_create_init( | |
339 | state->scanner->value.v_float); | |
340 | break; | |
341 | case G_TOKEN_STRING: | |
342 | /* Quoted string */ | |
343 | value = bt_value_string_create_init( | |
344 | state->scanner->value.v_string); | |
345 | break; | |
346 | case G_TOKEN_IDENTIFIER: | |
347 | { | |
348 | /* | |
349 | * Using symbols would be appropriate here, | |
350 | * but said symbols are allowed as map key, | |
351 | * so it's easier to consider everything an | |
352 | * identifier. | |
353 | * | |
354 | * If one of the known symbols is not | |
355 | * recognized here, then fall back to creating | |
356 | * a string value. | |
357 | */ | |
358 | const char *id = state->scanner->value.v_identifier; | |
359 | ||
360 | if (!strcmp(id, "null") || !strcmp(id, "NULL") || | |
361 | !strcmp(id, "nul")) { | |
362 | value = bt_value_null; | |
363 | } else if (!strcmp(id, "true") || !strcmp(id, "TRUE") || | |
364 | !strcmp(id, "yes") || | |
365 | !strcmp(id, "YES")) { | |
366 | value = bt_value_bool_create_init(true); | |
367 | } else if (!strcmp(id, "false") || | |
368 | !strcmp(id, "FALSE") || | |
369 | !strcmp(id, "no") || | |
370 | !strcmp(id, "NO")) { | |
371 | value = bt_value_bool_create_init(false); | |
372 | } else { | |
373 | value = bt_value_string_create_init(id); | |
374 | } | |
375 | break; | |
376 | } | |
377 | default: | |
378 | /* Unset value variable will trigger the error */ | |
379 | break; | |
380 | } | |
381 | ||
382 | if (!value) { | |
383 | ini_append_error_expecting(state, | |
384 | state->scanner, "value"); | |
385 | goto error; | |
386 | } | |
387 | ||
388 | state->expecting = INI_EXPECT_COMMA; | |
389 | goto success; | |
390 | } | |
391 | case INI_EXPECT_VALUE_NUMBER_NEG: | |
392 | { | |
393 | switch (token_type) { | |
394 | case G_TOKEN_INT: | |
395 | { | |
396 | /* Negative integer */ | |
397 | uint64_t int_val = state->scanner->value.v_int64; | |
398 | ||
399 | if (int_val > (1ULL << 63) - 1) { | |
400 | g_string_append_printf(state->ini_error, | |
401 | "Integer value -%" PRIu64 " is outside the range of a 64-bit signed integer\n", | |
402 | int_val); | |
403 | goto error; | |
404 | } | |
405 | ||
406 | value = bt_value_integer_create_init( | |
407 | -((int64_t) int_val)); | |
408 | break; | |
409 | } | |
410 | case G_TOKEN_FLOAT: | |
411 | /* Negative floating point number */ | |
412 | value = bt_value_float_create_init( | |
413 | -state->scanner->value.v_float); | |
414 | break; | |
415 | default: | |
416 | /* Unset value variable will trigger the error */ | |
417 | break; | |
418 | } | |
419 | ||
420 | if (!value) { | |
421 | ini_append_error_expecting(state, | |
422 | state->scanner, "value"); | |
423 | goto error; | |
424 | } | |
425 | ||
426 | state->expecting = INI_EXPECT_COMMA; | |
427 | goto success; | |
428 | } | |
429 | case INI_EXPECT_COMMA: | |
430 | if (token_type != G_TOKEN_CHAR) { | |
431 | ini_append_error_expecting(state, | |
432 | state->scanner, "','"); | |
433 | goto error; | |
434 | } | |
435 | ||
436 | if (state->scanner->value.v_char != ',') { | |
437 | ini_append_error_expecting(state, | |
438 | state->scanner, "','"); | |
439 | goto error; | |
440 | } | |
441 | ||
442 | state->expecting = INI_EXPECT_MAP_KEY; | |
443 | goto success; | |
444 | default: | |
445 | assert(false); | |
446 | } | |
447 | ||
448 | error: | |
449 | ret = -1; | |
450 | goto end; | |
451 | ||
452 | success: | |
453 | if (value) { | |
454 | if (bt_value_map_insert(state->params, | |
455 | state->last_map_key, value)) { | |
456 | /* Only override return value on error */ | |
457 | ret = -1; | |
458 | } | |
459 | } | |
460 | ||
461 | end: | |
462 | BT_PUT(value); | |
463 | return ret; | |
464 | } | |
465 | ||
466 | /* | |
467 | * Converts an INI-style argument to an equivalent map value object. | |
468 | * | |
469 | * Return value is owned by the caller. | |
470 | */ | |
471 | static | |
472 | struct bt_value *bt_value_from_ini(const char *arg, GString *ini_error) | |
473 | { | |
474 | /* Lexical scanner configuration */ | |
475 | GScannerConfig scanner_config = { | |
476 | /* Skip whitespaces */ | |
477 | .cset_skip_characters = " \t\n", | |
478 | ||
479 | /* Identifier syntax is: [a-zA-Z_][a-zA-Z0-9_.:-]* */ | |
480 | .cset_identifier_first = | |
481 | G_CSET_a_2_z | |
482 | "_" | |
483 | G_CSET_A_2_Z, | |
484 | .cset_identifier_nth = | |
485 | G_CSET_a_2_z | |
486 | "_0123456789-.:" | |
487 | G_CSET_A_2_Z, | |
488 | ||
489 | /* "hello" and "Hello" two different keys */ | |
490 | .case_sensitive = TRUE, | |
491 | ||
492 | /* No comments */ | |
493 | .cpair_comment_single = NULL, | |
494 | .skip_comment_multi = TRUE, | |
495 | .skip_comment_single = TRUE, | |
496 | .scan_comment_multi = FALSE, | |
497 | ||
498 | /* | |
499 | * Do scan identifiers, including 1-char identifiers, | |
500 | * but NULL is a normal identifier. | |
501 | */ | |
502 | .scan_identifier = TRUE, | |
503 | .scan_identifier_1char = TRUE, | |
504 | .scan_identifier_NULL = FALSE, | |
505 | ||
506 | /* | |
507 | * No specific symbols: null and boolean "symbols" are | |
508 | * scanned as plain identifiers. | |
509 | */ | |
510 | .scan_symbols = FALSE, | |
511 | .symbol_2_token = FALSE, | |
512 | .scope_0_fallback = FALSE, | |
513 | ||
514 | /* | |
515 | * Scan "0b"-, "0"-, and "0x"-prefixed integers, but not | |
516 | * integers prefixed with "$". | |
517 | */ | |
518 | .scan_binary = TRUE, | |
519 | .scan_octal = TRUE, | |
520 | .scan_float = TRUE, | |
521 | .scan_hex = TRUE, | |
522 | .scan_hex_dollar = FALSE, | |
523 | ||
524 | /* Convert scanned numbers to integer tokens */ | |
525 | .numbers_2_int = TRUE, | |
526 | ||
527 | /* Support both integers and floating-point numbers */ | |
528 | .int_2_float = FALSE, | |
529 | ||
530 | /* Scan integers as 64-bit signed integers */ | |
531 | .store_int64 = TRUE, | |
532 | ||
533 | /* Only scan double-quoted strings */ | |
534 | .scan_string_sq = FALSE, | |
535 | .scan_string_dq = TRUE, | |
536 | ||
537 | /* Do not converter identifiers to string tokens */ | |
538 | .identifier_2_string = FALSE, | |
539 | ||
540 | /* Scan characters as G_TOKEN_CHAR token */ | |
541 | .char_2_token = FALSE, | |
542 | }; | |
543 | struct ini_parsing_state state = { | |
544 | .scanner = NULL, | |
545 | .params = NULL, | |
546 | .expecting = INI_EXPECT_MAP_KEY, | |
547 | .arg = arg, | |
548 | .ini_error = ini_error, | |
549 | }; | |
550 | ||
551 | state.params = bt_value_map_create(); | |
552 | if (!state.params) { | |
553 | goto error; | |
554 | } | |
555 | ||
556 | state.scanner = g_scanner_new(&scanner_config); | |
557 | if (!state.scanner) { | |
558 | goto error; | |
559 | } | |
560 | ||
561 | /* Let the scan begin */ | |
562 | g_scanner_input_text(state.scanner, arg, strlen(arg)); | |
563 | ||
564 | while (true) { | |
565 | int ret = ini_handle_state(&state); | |
566 | ||
567 | if (ret < 0) { | |
568 | /* Error */ | |
569 | goto error; | |
570 | } else if (ret > 0) { | |
571 | /* Done */ | |
572 | break; | |
573 | } | |
574 | } | |
575 | ||
576 | goto end; | |
577 | ||
578 | error: | |
579 | BT_PUT(state.params); | |
580 | ||
581 | end: | |
582 | if (state.scanner) { | |
583 | g_scanner_destroy(state.scanner); | |
584 | } | |
585 | ||
586 | free(state.last_map_key); | |
587 | return state.params; | |
588 | } | |
589 | ||
590 | /* | |
591 | * Returns the parameters map value object from a command-line | |
bdc61c70 | 592 | * parameter option's argument. |
c42c79ea PP |
593 | * |
594 | * Return value is owned by the caller. | |
595 | */ | |
596 | static | |
597 | struct bt_value *bt_value_from_arg(const char *arg) | |
598 | { | |
599 | struct bt_value *params = NULL; | |
c42c79ea PP |
600 | GString *ini_error = NULL; |
601 | ||
c42c79ea PP |
602 | ini_error = g_string_new(NULL); |
603 | if (!ini_error) { | |
604 | print_err_oom(); | |
605 | goto end; | |
606 | } | |
607 | ||
608 | /* Try INI-style parsing */ | |
bdc61c70 | 609 | params = bt_value_from_ini(arg, ini_error); |
c42c79ea PP |
610 | if (!params) { |
611 | printf_err("%s", ini_error->str); | |
612 | goto end; | |
613 | } | |
614 | ||
615 | end: | |
616 | if (ini_error) { | |
617 | g_string_free(ini_error, TRUE); | |
618 | } | |
619 | return params; | |
620 | } | |
621 | ||
622 | /* | |
623 | * Returns the plugin and component names from a command-line | |
bdc61c70 PP |
624 | * source/sink option's argument. arg must have the following format: |
625 | * | |
626 | * PLUGIN.COMPONENT | |
627 | * | |
628 | * where PLUGIN is the plugin name, and COMPONENT is the component | |
629 | * name. | |
c42c79ea PP |
630 | * |
631 | * On success, both *plugin and *component are not NULL. *plugin | |
632 | * and *component are owned by the caller. | |
633 | */ | |
634 | static | |
635 | void plugin_component_names_from_arg(const char *arg, char **plugin, | |
636 | char **component) | |
637 | { | |
638 | const char *dot; | |
bdc61c70 | 639 | const char *end; |
c42c79ea PP |
640 | size_t plugin_len; |
641 | size_t component_len; | |
642 | ||
bdc61c70 | 643 | /* Initialize both return values to NULL: not found */ |
c42c79ea PP |
644 | *plugin = NULL; |
645 | *component = NULL; | |
646 | ||
647 | dot = strchr(arg, '.'); | |
bdc61c70 PP |
648 | if (!dot) { |
649 | /* No dot */ | |
c42c79ea PP |
650 | goto end; |
651 | } | |
652 | ||
bdc61c70 | 653 | end = arg + strlen(arg); |
c42c79ea | 654 | plugin_len = dot - arg; |
bdc61c70 | 655 | component_len = end - dot - 1; |
c42c79ea PP |
656 | if (plugin_len == 0 || component_len == 0) { |
657 | goto end; | |
658 | } | |
659 | ||
bdc61c70 | 660 | *plugin = g_malloc0(plugin_len + 1); |
c42c79ea PP |
661 | if (!*plugin) { |
662 | print_err_oom(); | |
663 | goto end; | |
664 | } | |
665 | ||
bdc61c70 PP |
666 | g_strlcpy(*plugin, arg, plugin_len + 1); |
667 | *component = g_malloc0(component_len + 1); | |
c42c79ea PP |
668 | if (!*component) { |
669 | print_err_oom(); | |
670 | goto end; | |
671 | } | |
672 | ||
bdc61c70 | 673 | g_strlcpy(*component, dot + 1, component_len + 1); |
c42c79ea PP |
674 | |
675 | end: | |
676 | return; | |
677 | } | |
678 | ||
679 | /* | |
680 | * Prints the Babeltrace version. | |
681 | */ | |
682 | static | |
683 | void print_version(void) | |
684 | { | |
685 | puts("Babeltrace " VERSION); | |
686 | } | |
687 | ||
688 | /* | |
689 | * Prints the legacy, Babeltrace 1.x command usage. Those options are | |
690 | * still compatible in Babeltrace 2.x, but it is recommended to use | |
691 | * the more generic plugin/component parameters instead of those | |
692 | * hard-coded option names. | |
693 | */ | |
694 | static | |
695 | void print_legacy_usage(FILE *fp) | |
696 | { | |
697 | fprintf(fp, "Usage: babeltrace [OPTIONS] INPUT...\n"); | |
698 | fprintf(fp, "\n"); | |
699 | fprintf(fp, "The following options are compatible with the Babeltrace 1.x options:\n"); | |
700 | fprintf(fp, "\n"); | |
701 | fprintf(fp, " --help-legacy Show this help\n"); | |
702 | fprintf(fp, " -V, --version Show version\n"); | |
703 | fprintf(fp, " --clock-force-correlate Assume that clocks are inherently correlated\n"); | |
704 | fprintf(fp, " across traces\n"); | |
705 | fprintf(fp, " -d, --debug Enable debug mode\n"); | |
706 | fprintf(fp, " -i, --input-format=FORMAT Input trace format (default: ctf)\n"); | |
707 | fprintf(fp, " -l, --list List available formats\n"); | |
708 | fprintf(fp, " -o, --output-format=FORMAT Output trace format (default: text)\n"); | |
709 | fprintf(fp, " -v, --verbose Enable verbose output\n"); | |
710 | fprintf(fp, "\n"); | |
711 | fprintf(fp, " Available input formats: ctf, lttng-live, ctf-metadata\n"); | |
712 | fprintf(fp, " Available output formats: text, dummy\n"); | |
713 | fprintf(fp, "\n"); | |
714 | fprintf(fp, "Input formats specific options:\n"); | |
715 | fprintf(fp, "\n"); | |
716 | fprintf(fp, " INPUT... Input trace file(s), directory(ies), or URLs\n"); | |
717 | fprintf(fp, " --clock-offset=SEC Set clock offset to SEC seconds\n"); | |
718 | fprintf(fp, " --clock-offset-ns=NS Set clock offset to NS nanoseconds\n"); | |
719 | fprintf(fp, " --stream-intersection Only process events when all streams are active\n"); | |
720 | fprintf(fp, "\n"); | |
721 | fprintf(fp, "text output format specific options:\n"); | |
722 | fprintf(fp, " \n"); | |
723 | fprintf(fp, " --clock-cycles Print timestamps in clock cycles\n"); | |
724 | fprintf(fp, " --clock-date Print timestamp dates\n"); | |
725 | fprintf(fp, " --clock-gmt Print timestamps in GMT time zone\n"); | |
726 | fprintf(fp, " (default: local time zone)\n"); | |
727 | fprintf(fp, " --clock-seconds Print the timestamps as [SEC.NS]\n"); | |
728 | fprintf(fp, " (default format: [HH:MM:SS.NS])\n"); | |
729 | fprintf(fp, " --debug-info-dir=DIR Search for debug info in directory DIR\n"); | |
6d1d5711 | 730 | fprintf(fp, " (default: `/usr/lib/debug`)\n"); |
c42c79ea PP |
731 | fprintf(fp, " --debug-info-full-path Show full debug info source and binary paths\n"); |
732 | fprintf(fp, " --debug-info-target-prefix=DIR Use directory DIR as a prefix when looking\n"); | |
733 | fprintf(fp, " up executables during debug info analysis\n"); | |
6d1d5711 | 734 | fprintf(fp, " (default: `/usr/lib/debug`)\n"); |
c42c79ea PP |
735 | fprintf(fp, " -f, --fields=NAME[,NAME]... Print additional fields:\n"); |
736 | fprintf(fp, " all, trace, trace:hostname, trace:domain,\n"); | |
737 | fprintf(fp, " trace:procname, trace:vpid, loglevel, emf,\n"); | |
738 | fprintf(fp, " callsite\n"); | |
739 | fprintf(fp, " (default: trace:hostname, trace:procname,\n"); | |
740 | fprintf(fp, " trace:vpid)\n"); | |
741 | fprintf(fp, " -n, --names=NAME[,NAME]... Print field names:\n"); | |
742 | fprintf(fp, " payload (or arg or args)\n"); | |
743 | fprintf(fp, " none, all, scope, header, context (or ctx)\n"); | |
744 | fprintf(fp, " (default: payload, context)\n"); | |
745 | fprintf(fp, " --no-delta Do not print time delta between consecutive\n"); | |
746 | fprintf(fp, " events\n"); | |
747 | fprintf(fp, " -w, --output=PATH Write output to PATH (default: standard output)\n"); | |
748 | } | |
749 | ||
750 | /* | |
751 | * Prints the Babeltrace 2.x usage. | |
752 | */ | |
753 | static | |
754 | void print_usage(FILE *fp) | |
755 | { | |
756 | fprintf(fp, "Usage: babeltrace [OPTIONS]\n"); | |
757 | fprintf(fp, "\n"); | |
015cee23 PP |
758 | fprintf(fp, " -B, --base-begin-ns=NS Set NS as the current base beginning\n"); |
759 | fprintf(fp, " timestamp of the following source component\n"); | |
760 | fprintf(fp, " instances\n"); | |
761 | fprintf(fp, " -E, --base-end-ns=NS Set NS as the current base end timestamp\n"); | |
762 | fprintf(fp, " of the following source component instances\n"); | |
b07ffa28 PP |
763 | fprintf(fp, " -b, --base-params=PARAMS Set PARAMS as the current base parameters\n"); |
764 | fprintf(fp, " of the following source and sink component\n"); | |
765 | fprintf(fp, " instances (see the exact format of PARAMS\n"); | |
766 | fprintf(fp, " below)\n"); | |
015cee23 PP |
767 | fprintf(fp, " --begin-ns=NS Set the beginning timestamp of the latest\n"); |
768 | fprintf(fp, " source component instance to NS\n"); | |
c42c79ea | 769 | fprintf(fp, " -d, --debug Enable debug mode\n"); |
015cee23 PP |
770 | fprintf(fp, " --end-ns=NS Set the end timestamp of the latest source\n"); |
771 | fprintf(fp, " component instance to NS\n"); | |
c42c79ea | 772 | fprintf(fp, " -l, --list List available plugins and their components\n"); |
bdc61c70 | 773 | fprintf(fp, " -p, --params=PARAMS Set the parameters of the latest source or\n"); |
b07ffa28 PP |
774 | fprintf(fp, " sink component instance (in command-line \n"); |
775 | fprintf(fp, " order) to PARAMS (see the exact format of\n"); | |
776 | fprintf(fp, " PARAMS below)\n"); | |
015cee23 PP |
777 | fprintf(fp, " -P, --plugin-path=PATH[:PATH]... Set paths from which dynamic plugins can be\n"); |
778 | fprintf(fp, " loaded to PATH\n"); | |
779 | fprintf(fp, " --reset-base-begin-ns Reset the current base beginning timestamp\n"); | |
780 | fprintf(fp, " of the following source component instances\n"); | |
781 | fprintf(fp, " --reset-base-end-ns Reset the current base end timestamp of the\n"); | |
782 | fprintf(fp, " following source component instances\n"); | |
b07ffa28 PP |
783 | fprintf(fp, " -r, --reset-base-params Reset the current base parameters of the\n"); |
784 | fprintf(fp, " following source and sink component\n"); | |
785 | fprintf(fp, " instances to an empty map\n"); | |
015cee23 PP |
786 | fprintf(fp, " -o, --sink=PLUGIN.COMPCLS Instantiate a sink component from plugin\n"); |
787 | fprintf(fp, " PLUGIN and component class COMPCLS (may be\n"); | |
788 | fprintf(fp, " repeated)\n"); | |
789 | fprintf(fp, " -i, --source=PLUGIN.COMPCLS Instantiate a source component from plugin\n"); | |
790 | fprintf(fp, " PLUGIN and component class COMPCLS (may be\n"); | |
791 | fprintf(fp, " repeated)\n"); | |
bdc61c70 PP |
792 | fprintf(fp, " -h --help Show this help\n"); |
793 | fprintf(fp, " --help-legacy Show Babeltrace 1.x legacy options\n"); | |
c42c79ea PP |
794 | fprintf(fp, " -v, --verbose Enable verbose output\n"); |
795 | fprintf(fp, " -V, --version Show version\n"); | |
bdc61c70 | 796 | fprintf(fp, "\n\n"); |
b07ffa28 PP |
797 | fprintf(fp, "Format of PARAMS\n"); |
798 | fprintf(fp, "----------------\n"); | |
c42c79ea | 799 | fprintf(fp, "\n"); |
bdc61c70 | 800 | fprintf(fp, " PARAM=VALUE[,PARAM=VALUE]...\n"); |
c42c79ea | 801 | fprintf(fp, "\n"); |
bdc61c70 PP |
802 | fprintf(fp, "The parameter string is a comma-separated list of PARAM=VALUE assignments,\n"); |
803 | fprintf(fp, "where PARAM is the parameter name (C identifier plus [:.-] characters), and\n"); | |
804 | fprintf(fp, "VALUE can be one of:\n"); | |
c42c79ea | 805 | fprintf(fp, "\n"); |
bdc61c70 PP |
806 | fprintf(fp, "* `null`, `nul`, `NULL`: null value (no backticks).\n"); |
807 | fprintf(fp, "* `true`, `TRUE`, `yes`, `YES`: true boolean value (no backticks).\n"); | |
808 | fprintf(fp, "* `false`, `FALSE`, `no`, `NO`: false boolean value (no backticks).\n"); | |
809 | fprintf(fp, "* Binary (`0b` prefix), octal (`0` prefix), decimal, or hexadecimal\n"); | |
810 | fprintf(fp, " (`0x` prefix) signed 64-bit integer.\n"); | |
811 | fprintf(fp, "* Double precision floating point number (scientific notation is accepted).\n"); | |
812 | fprintf(fp, "* Unquoted string with no special characters, and not matching any of\n"); | |
813 | fprintf(fp, " the null and boolean value symbols above.\n"); | |
814 | fprintf(fp, "* Double-quoted string (accepts escape characters).\n"); | |
c42c79ea | 815 | fprintf(fp, "\n"); |
bdc61c70 | 816 | fprintf(fp, "Whitespaces are allowed around individual `=` and `,` tokens.\n"); |
c42c79ea | 817 | fprintf(fp, "\n"); |
bdc61c70 | 818 | fprintf(fp, "Example:\n"); |
c42c79ea | 819 | fprintf(fp, "\n"); |
bdc61c70 PP |
820 | fprintf(fp, " many=null, fresh=yes, condition=false, squirrel=-782329,\n"); |
821 | fprintf(fp, " observe=3.14, simple=beef, needs-quotes=\"some string\",\n"); | |
822 | fprintf(fp, " escape.chars-are:allowed=\"this is a \\\" double quote\"\n"); | |
c42c79ea | 823 | fprintf(fp, "\n"); |
bdc61c70 PP |
824 | fprintf(fp, "IMPORTANT: Make sure to single-quote the whole argument when you run babeltrace\n"); |
825 | fprintf(fp, "from a shell.\n"); | |
c42c79ea PP |
826 | } |
827 | ||
828 | /* | |
829 | * Destroys a component configuration. | |
830 | */ | |
831 | static | |
832 | void bt_config_component_destroy(struct bt_object *obj) | |
833 | { | |
834 | struct bt_config_component *bt_config_component = | |
835 | container_of(obj, struct bt_config_component, base); | |
836 | ||
837 | if (!obj) { | |
838 | goto end; | |
839 | } | |
840 | ||
841 | if (bt_config_component->plugin_name) { | |
842 | g_string_free(bt_config_component->plugin_name, TRUE); | |
843 | } | |
844 | ||
845 | if (bt_config_component->component_name) { | |
846 | g_string_free(bt_config_component->component_name, TRUE); | |
847 | } | |
848 | ||
849 | BT_PUT(bt_config_component->params); | |
850 | g_free(bt_config_component); | |
851 | ||
852 | end: | |
853 | return; | |
854 | } | |
855 | ||
856 | /* | |
bdc61c70 PP |
857 | * Creates a component configuration using the given plugin name and |
858 | * component name. plugin_name and component_name are copied (belong to | |
859 | * the return value). | |
c42c79ea PP |
860 | * |
861 | * Return value is owned by the caller. | |
862 | */ | |
863 | static | |
864 | struct bt_config_component *bt_config_component_create(const char *plugin_name, | |
bdc61c70 | 865 | const char *component_name) |
c42c79ea PP |
866 | { |
867 | struct bt_config_component *cfg_component = NULL; | |
868 | ||
869 | cfg_component = g_new0(struct bt_config_component, 1); | |
870 | if (!cfg_component) { | |
871 | print_err_oom(); | |
872 | goto error; | |
873 | } | |
874 | ||
875 | bt_object_init(cfg_component, bt_config_component_destroy); | |
876 | cfg_component->plugin_name = g_string_new(plugin_name); | |
877 | if (!cfg_component->plugin_name) { | |
878 | print_err_oom(); | |
879 | goto error; | |
880 | } | |
881 | ||
882 | cfg_component->component_name = g_string_new(component_name); | |
883 | if (!cfg_component->component_name) { | |
884 | print_err_oom(); | |
885 | goto error; | |
886 | } | |
887 | ||
bdc61c70 PP |
888 | /* Start with empty parameters */ |
889 | cfg_component->params = bt_value_map_create(); | |
890 | if (!cfg_component->params) { | |
891 | print_err_oom(); | |
892 | goto error; | |
893 | } | |
894 | ||
015cee23 PP |
895 | /* Begin/end timestamp: not set */ |
896 | cfg_component->begin_ns = -1ULL; | |
897 | cfg_component->end_ns = -1ULL; | |
898 | ||
c42c79ea PP |
899 | goto end; |
900 | ||
901 | error: | |
902 | BT_PUT(cfg_component); | |
903 | ||
904 | end: | |
905 | return cfg_component; | |
906 | } | |
907 | ||
908 | /* | |
909 | * Creates a component configuration from a command-line source/sink | |
bdc61c70 | 910 | * option's argument. |
c42c79ea PP |
911 | */ |
912 | static | |
913 | struct bt_config_component *bt_config_component_from_arg(const char *arg) | |
914 | { | |
915 | struct bt_config_component *bt_config_component = NULL; | |
916 | char *plugin_name; | |
917 | char *component_name; | |
c42c79ea PP |
918 | |
919 | plugin_component_names_from_arg(arg, &plugin_name, &component_name); | |
920 | if (!plugin_name || !component_name) { | |
49849a47 | 921 | printf_err("Cannot get plugin or component class name\n"); |
c42c79ea PP |
922 | goto error; |
923 | } | |
924 | ||
c42c79ea | 925 | bt_config_component = bt_config_component_create(plugin_name, |
bdc61c70 | 926 | component_name); |
c42c79ea PP |
927 | if (!bt_config_component) { |
928 | goto error; | |
929 | } | |
930 | ||
931 | goto end; | |
932 | ||
933 | error: | |
934 | BT_PUT(bt_config_component); | |
935 | ||
936 | end: | |
bdc61c70 PP |
937 | g_free(plugin_name); |
938 | g_free(component_name); | |
c42c79ea PP |
939 | return bt_config_component; |
940 | } | |
941 | ||
942 | /* | |
943 | * Destroys a configuration. | |
944 | */ | |
945 | static | |
946 | void bt_config_destroy(struct bt_object *obj) | |
947 | { | |
948 | struct bt_config *bt_config = | |
949 | container_of(obj, struct bt_config, base); | |
950 | ||
951 | if (!obj) { | |
952 | goto end; | |
953 | } | |
954 | ||
955 | if (bt_config->sources) { | |
956 | g_ptr_array_free(bt_config->sources, TRUE); | |
957 | } | |
958 | ||
959 | if (bt_config->sinks) { | |
960 | g_ptr_array_free(bt_config->sinks, TRUE); | |
961 | } | |
962 | ||
963 | BT_PUT(bt_config->plugin_paths); | |
964 | g_free(bt_config); | |
965 | ||
966 | end: | |
967 | return; | |
968 | } | |
969 | ||
970 | /* | |
971 | * Extracts the various paths from the string arg, delimited by ':', | |
972 | * and converts them to an array value object. | |
973 | * | |
974 | * Returned array value object is empty if arg is empty. | |
975 | * | |
976 | * Return value is owned by the caller. | |
977 | */ | |
978 | static | |
979 | struct bt_value *plugin_paths_from_arg(const char *arg) | |
980 | { | |
981 | struct bt_value *plugin_paths; | |
982 | const char *at = arg; | |
983 | const char *end = arg + strlen(arg); | |
984 | ||
985 | plugin_paths = bt_value_array_create(); | |
986 | if (!plugin_paths) { | |
987 | print_err_oom(); | |
988 | goto error; | |
989 | } | |
990 | ||
991 | while (at < end) { | |
992 | int ret; | |
993 | GString *path; | |
994 | const char *next_colon; | |
995 | ||
996 | next_colon = strchr(at, ':'); | |
997 | if (next_colon == at) { | |
998 | /* | |
999 | * Empty path: try next character (supported | |
1000 | * to conform to the typical parsing of $PATH). | |
1001 | */ | |
1002 | at++; | |
1003 | continue; | |
1004 | } else if (!next_colon) { | |
1005 | /* No more colon: use the remaining */ | |
1006 | next_colon = arg + strlen(arg); | |
1007 | } | |
1008 | ||
1009 | path = g_string_new(NULL); | |
1010 | if (!path) { | |
1011 | print_err_oom(); | |
1012 | goto error; | |
1013 | } | |
1014 | ||
1015 | g_string_append_len(path, at, next_colon - at); | |
1016 | at = next_colon + 1; | |
1017 | ret = bt_value_array_append_string(plugin_paths, path->str); | |
1018 | g_string_free(path, TRUE); | |
1019 | if (ret) { | |
1020 | print_err_oom(); | |
1021 | goto error; | |
1022 | } | |
1023 | } | |
1024 | ||
1025 | goto end; | |
1026 | ||
1027 | error: | |
1028 | BT_PUT(plugin_paths); | |
1029 | ||
1030 | end: | |
1031 | return plugin_paths; | |
1032 | } | |
1033 | ||
1034 | /* | |
1035 | * Creates a simple lexical scanner for parsing comma-delimited names | |
1036 | * and fields. | |
1037 | * | |
1038 | * Return value is owned by the caller. | |
1039 | */ | |
1040 | static | |
1041 | GScanner *create_csv_identifiers_scanner(void) | |
1042 | { | |
1043 | GScannerConfig scanner_config = { | |
1044 | .cset_skip_characters = " \t\n", | |
1045 | .cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z "_", | |
1046 | .cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z ":_-", | |
1047 | .case_sensitive = TRUE, | |
1048 | .cpair_comment_single = NULL, | |
1049 | .skip_comment_multi = TRUE, | |
1050 | .skip_comment_single = TRUE, | |
1051 | .scan_comment_multi = FALSE, | |
1052 | .scan_identifier = TRUE, | |
1053 | .scan_identifier_1char = TRUE, | |
1054 | .scan_identifier_NULL = FALSE, | |
1055 | .scan_symbols = FALSE, | |
1056 | .symbol_2_token = FALSE, | |
1057 | .scope_0_fallback = FALSE, | |
1058 | .scan_binary = FALSE, | |
1059 | .scan_octal = FALSE, | |
1060 | .scan_float = FALSE, | |
1061 | .scan_hex = FALSE, | |
1062 | .scan_hex_dollar = FALSE, | |
1063 | .numbers_2_int = FALSE, | |
1064 | .int_2_float = FALSE, | |
1065 | .store_int64 = FALSE, | |
1066 | .scan_string_sq = FALSE, | |
1067 | .scan_string_dq = FALSE, | |
1068 | .identifier_2_string = FALSE, | |
1069 | .char_2_token = TRUE, | |
1070 | }; | |
1071 | ||
1072 | return g_scanner_new(&scanner_config); | |
1073 | } | |
1074 | ||
6e1bc0df MD |
1075 | /* |
1076 | * Inserts a string (if exists and not empty) or null to a map value | |
1077 | * object. | |
1078 | */ | |
1079 | static | |
1080 | enum bt_value_status map_insert_string_or_null(struct bt_value *map, | |
1081 | const char *key, GString *string) | |
1082 | { | |
1083 | enum bt_value_status ret; | |
1084 | ||
1085 | if (string && string->len > 0) { | |
1086 | ret = bt_value_map_insert_string(map, key, string->str); | |
1087 | } else { | |
1088 | ret = bt_value_map_insert(map, key, bt_value_null); | |
1089 | } | |
1090 | return ret; | |
1091 | } | |
1092 | ||
c42c79ea PP |
1093 | /* |
1094 | * Converts a comma-delimited list of known names (--names option) to | |
1095 | * an array value object containing those names as string value objects. | |
1096 | * | |
1097 | * Return value is owned by the caller. | |
1098 | */ | |
1099 | static | |
1100 | struct bt_value *names_from_arg(const char *arg) | |
1101 | { | |
1102 | GScanner *scanner = NULL; | |
1103 | struct bt_value *names = NULL; | |
6e1bc0df | 1104 | bool found_all = false, found_none = false, found_item = false; |
c42c79ea PP |
1105 | |
1106 | names = bt_value_array_create(); | |
1107 | if (!names) { | |
1108 | print_err_oom(); | |
1109 | goto error; | |
1110 | } | |
1111 | ||
1112 | scanner = create_csv_identifiers_scanner(); | |
1113 | if (!scanner) { | |
1114 | print_err_oom(); | |
1115 | goto error; | |
1116 | } | |
1117 | ||
1118 | g_scanner_input_text(scanner, arg, strlen(arg)); | |
1119 | ||
1120 | while (true) { | |
1121 | GTokenType token_type = g_scanner_get_next_token(scanner); | |
1122 | ||
1123 | switch (token_type) { | |
1124 | case G_TOKEN_IDENTIFIER: | |
1125 | { | |
1126 | const char *identifier = scanner->value.v_identifier; | |
1127 | ||
1128 | if (!strcmp(identifier, "payload") || | |
1129 | !strcmp(identifier, "args") || | |
1130 | !strcmp(identifier, "arg")) { | |
6e1bc0df | 1131 | found_item = true; |
c42c79ea PP |
1132 | if (bt_value_array_append_string(names, |
1133 | "payload")) { | |
1134 | goto error; | |
1135 | } | |
1136 | } else if (!strcmp(identifier, "context") || | |
1137 | !strcmp(identifier, "ctx")) { | |
6e1bc0df | 1138 | found_item = true; |
c42c79ea PP |
1139 | if (bt_value_array_append_string(names, |
1140 | "context")) { | |
1141 | goto error; | |
1142 | } | |
1143 | } else if (!strcmp(identifier, "scope") || | |
1144 | !strcmp(identifier, "header")) { | |
6e1bc0df | 1145 | found_item = true; |
c42c79ea PP |
1146 | if (bt_value_array_append_string(names, |
1147 | identifier)) { | |
1148 | goto error; | |
1149 | } | |
6e1bc0df MD |
1150 | } else if (!strcmp(identifier, "all")) { |
1151 | found_all = true; | |
1152 | if (bt_value_array_append_string(names, | |
1153 | identifier)) { | |
c42c79ea PP |
1154 | goto error; |
1155 | } | |
6e1bc0df MD |
1156 | } else if (!strcmp(identifier, "none")) { |
1157 | found_none = true; | |
c42c79ea PP |
1158 | if (bt_value_array_append_string(names, |
1159 | identifier)) { | |
1160 | goto error; | |
1161 | } | |
c42c79ea | 1162 | } else { |
6d1d5711 | 1163 | printf_err("Unknown field name: `%s`\n", |
c42c79ea PP |
1164 | identifier); |
1165 | goto error; | |
1166 | } | |
1167 | break; | |
1168 | } | |
1169 | case G_TOKEN_COMMA: | |
1170 | continue; | |
1171 | case G_TOKEN_EOF: | |
1172 | goto end; | |
1173 | default: | |
1174 | goto error; | |
1175 | } | |
1176 | } | |
1177 | ||
6e1bc0df MD |
1178 | end: |
1179 | if (found_none && found_all) { | |
6d1d5711 | 1180 | printf_err("Only either `all` or `none` can be specified in the list given to the --names option, but not both.\n"); |
6e1bc0df MD |
1181 | goto error; |
1182 | } | |
1183 | /* | |
1184 | * Legacy behavior is to clear the defaults (show none) when at | |
1185 | * least one item is specified. | |
1186 | */ | |
1187 | if (found_item && !found_none && !found_all) { | |
1188 | if (bt_value_array_append_string(names, "none")) { | |
1189 | goto error; | |
1190 | } | |
1191 | } | |
1192 | if (scanner) { | |
1193 | g_scanner_destroy(scanner); | |
1194 | } | |
1195 | return names; | |
c42c79ea PP |
1196 | |
1197 | error: | |
1198 | BT_PUT(names); | |
c42c79ea PP |
1199 | if (scanner) { |
1200 | g_scanner_destroy(scanner); | |
1201 | } | |
1202 | return names; | |
1203 | } | |
1204 | ||
1205 | ||
1206 | /* | |
1207 | * Converts a comma-delimited list of known fields (--fields option) to | |
1208 | * an array value object containing those fields as string | |
1209 | * value objects. | |
1210 | * | |
1211 | * Return value is owned by the caller. | |
1212 | */ | |
1213 | static | |
1214 | struct bt_value *fields_from_arg(const char *arg) | |
1215 | { | |
1216 | GScanner *scanner = NULL; | |
1217 | struct bt_value *fields; | |
1218 | ||
1219 | fields = bt_value_array_create(); | |
1220 | if (!fields) { | |
1221 | print_err_oom(); | |
1222 | goto error; | |
1223 | } | |
1224 | ||
1225 | scanner = create_csv_identifiers_scanner(); | |
1226 | if (!scanner) { | |
1227 | print_err_oom(); | |
1228 | goto error; | |
1229 | } | |
1230 | ||
1231 | g_scanner_input_text(scanner, arg, strlen(arg)); | |
1232 | ||
1233 | while (true) { | |
1234 | GTokenType token_type = g_scanner_get_next_token(scanner); | |
1235 | ||
1236 | switch (token_type) { | |
1237 | case G_TOKEN_IDENTIFIER: | |
1238 | { | |
1239 | const char *identifier = scanner->value.v_identifier; | |
1240 | ||
1241 | if (!strcmp(identifier, "trace") || | |
1242 | !strcmp(identifier, "trace:hostname") || | |
1243 | !strcmp(identifier, "trace:domain") || | |
1244 | !strcmp(identifier, "trace:procname") || | |
1245 | !strcmp(identifier, "trace:vpid") || | |
1246 | !strcmp(identifier, "loglevel") || | |
1247 | !strcmp(identifier, "emf") || | |
6e1bc0df MD |
1248 | !strcmp(identifier, "callsite") || |
1249 | !strcmp(identifier, "all")) { | |
c42c79ea PP |
1250 | if (bt_value_array_append_string(fields, |
1251 | identifier)) { | |
1252 | goto error; | |
1253 | } | |
c42c79ea | 1254 | } else { |
6d1d5711 | 1255 | printf_err("Unknown field name: `%s`\n", |
c42c79ea PP |
1256 | identifier); |
1257 | goto error; | |
1258 | } | |
1259 | break; | |
1260 | } | |
1261 | case G_TOKEN_COMMA: | |
1262 | continue; | |
1263 | case G_TOKEN_EOF: | |
1264 | goto end; | |
1265 | default: | |
1266 | goto error; | |
1267 | } | |
1268 | } | |
1269 | ||
1270 | goto end; | |
1271 | ||
1272 | error: | |
1273 | BT_PUT(fields); | |
1274 | ||
1275 | end: | |
1276 | if (scanner) { | |
1277 | g_scanner_destroy(scanner); | |
1278 | } | |
1279 | return fields; | |
1280 | } | |
1281 | ||
1282 | /* | |
1283 | * Inserts the equivalent "prefix-name" true boolean value objects into | |
1284 | * map_obj where the names are in array_obj. | |
1285 | */ | |
1286 | static | |
1287 | int insert_flat_names_fields_from_array(struct bt_value *map_obj, | |
1288 | struct bt_value *array_obj, const char *prefix) | |
1289 | { | |
1290 | int ret = 0; | |
1291 | int i; | |
6e1bc0df | 1292 | GString *tmpstr = NULL, *default_value = NULL; |
c42c79ea PP |
1293 | |
1294 | /* | |
1295 | * array_obj may be NULL if no CLI options were specified to | |
1296 | * trigger its creation. | |
1297 | */ | |
1298 | if (!array_obj) { | |
1299 | goto end; | |
1300 | } | |
1301 | ||
1302 | tmpstr = g_string_new(NULL); | |
1303 | if (!tmpstr) { | |
1304 | print_err_oom(); | |
1305 | ret = -1; | |
1306 | goto end; | |
1307 | } | |
1308 | ||
6e1bc0df MD |
1309 | default_value = g_string_new(NULL); |
1310 | if (!default_value) { | |
1311 | print_err_oom(); | |
1312 | ret = -1; | |
1313 | goto end; | |
1314 | } | |
1315 | ||
c42c79ea PP |
1316 | for (i = 0; i < bt_value_array_size(array_obj); i++) { |
1317 | struct bt_value *str_obj = bt_value_array_get(array_obj, i); | |
1318 | const char *suffix; | |
6e1bc0df | 1319 | bool is_default = false; |
c42c79ea PP |
1320 | |
1321 | if (!str_obj) { | |
1322 | printf_err("Unexpected error\n"); | |
1323 | ret = -1; | |
1324 | goto end; | |
1325 | } | |
1326 | ||
1327 | ret = bt_value_string_get(str_obj, &suffix); | |
1328 | BT_PUT(str_obj); | |
1329 | if (ret) { | |
1330 | printf_err("Unexpected error\n"); | |
1331 | goto end; | |
1332 | } | |
1333 | ||
1334 | g_string_assign(tmpstr, prefix); | |
1335 | g_string_append(tmpstr, "-"); | |
6e1bc0df MD |
1336 | |
1337 | /* Special-case for "all" and "none". */ | |
1338 | if (!strcmp(suffix, "all")) { | |
1339 | is_default = true; | |
1340 | g_string_assign(default_value, "show"); | |
1341 | } else if (!strcmp(suffix, "none")) { | |
1342 | is_default = true; | |
1343 | g_string_assign(default_value, "hide"); | |
1344 | } | |
1345 | if (is_default) { | |
1346 | g_string_append(tmpstr, "default"); | |
1347 | ret = map_insert_string_or_null(map_obj, | |
1348 | tmpstr->str, | |
1349 | default_value); | |
1350 | if (ret) { | |
1351 | print_err_oom(); | |
1352 | goto end; | |
1353 | } | |
1354 | } else { | |
1355 | g_string_append(tmpstr, suffix); | |
1356 | ret = bt_value_map_insert_bool(map_obj, tmpstr->str, | |
1357 | true); | |
1358 | if (ret) { | |
1359 | print_err_oom(); | |
1360 | goto end; | |
1361 | } | |
c42c79ea PP |
1362 | } |
1363 | } | |
1364 | ||
1365 | end: | |
6e1bc0df MD |
1366 | if (default_value) { |
1367 | g_string_free(default_value, TRUE); | |
1368 | } | |
c42c79ea PP |
1369 | if (tmpstr) { |
1370 | g_string_free(tmpstr, TRUE); | |
1371 | } | |
1372 | ||
1373 | return ret; | |
1374 | } | |
1375 | ||
c42c79ea PP |
1376 | /* |
1377 | * Returns the parameters (map value object) corresponding to the | |
1378 | * legacy text format options. | |
1379 | * | |
1380 | * Return value is owned by the caller. | |
1381 | */ | |
1382 | static | |
1383 | struct bt_value *params_from_text_legacy_opts( | |
1384 | struct text_legacy_opts *text_legacy_opts) | |
1385 | { | |
1386 | struct bt_value *params; | |
1387 | ||
1388 | params = bt_value_map_create(); | |
1389 | if (!params) { | |
1390 | print_err_oom(); | |
1391 | goto error; | |
1392 | } | |
1393 | ||
1394 | if (map_insert_string_or_null(params, "output-path", | |
1395 | text_legacy_opts->output)) { | |
1396 | print_err_oom(); | |
1397 | goto error; | |
1398 | } | |
1399 | ||
1400 | if (map_insert_string_or_null(params, "debug-info-dir", | |
1401 | text_legacy_opts->dbg_info_dir)) { | |
1402 | print_err_oom(); | |
1403 | goto error; | |
1404 | } | |
1405 | ||
1406 | if (map_insert_string_or_null(params, "debug-info-target-prefix", | |
1407 | text_legacy_opts->dbg_info_target_prefix)) { | |
1408 | print_err_oom(); | |
1409 | goto error; | |
1410 | } | |
1411 | ||
1412 | if (bt_value_map_insert_bool(params, "debug-info-full-path", | |
1413 | text_legacy_opts->dbg_info_full_path)) { | |
1414 | print_err_oom(); | |
1415 | goto error; | |
1416 | } | |
1417 | ||
1418 | if (bt_value_map_insert_bool(params, "no-delta", | |
1419 | text_legacy_opts->no_delta)) { | |
1420 | print_err_oom(); | |
1421 | goto error; | |
1422 | } | |
1423 | ||
1424 | if (bt_value_map_insert_bool(params, "clock-cycles", | |
1425 | text_legacy_opts->clock_cycles)) { | |
1426 | print_err_oom(); | |
1427 | goto error; | |
1428 | } | |
1429 | ||
1430 | if (bt_value_map_insert_bool(params, "clock-seconds", | |
1431 | text_legacy_opts->clock_seconds)) { | |
1432 | print_err_oom(); | |
1433 | goto error; | |
1434 | } | |
1435 | ||
1436 | if (bt_value_map_insert_bool(params, "clock-date", | |
1437 | text_legacy_opts->clock_date)) { | |
1438 | print_err_oom(); | |
1439 | goto error; | |
1440 | } | |
1441 | ||
1442 | if (bt_value_map_insert_bool(params, "clock-gmt", | |
1443 | text_legacy_opts->clock_gmt)) { | |
1444 | print_err_oom(); | |
1445 | goto error; | |
1446 | } | |
1447 | ||
1448 | if (insert_flat_names_fields_from_array(params, | |
1449 | text_legacy_opts->names, "name")) { | |
1450 | goto error; | |
1451 | } | |
1452 | ||
1453 | if (insert_flat_names_fields_from_array(params, | |
1454 | text_legacy_opts->fields, "field")) { | |
1455 | goto error; | |
1456 | } | |
1457 | ||
1458 | goto end; | |
1459 | ||
1460 | error: | |
1461 | BT_PUT(params); | |
1462 | ||
1463 | end: | |
1464 | return params; | |
1465 | } | |
1466 | ||
1467 | static | |
1468 | int append_sinks_from_legacy_opts(GPtrArray *sinks, | |
1469 | enum legacy_output_format legacy_output_format, | |
1470 | struct text_legacy_opts *text_legacy_opts) | |
1471 | { | |
1472 | int ret = 0; | |
1473 | struct bt_value *params = NULL; | |
1474 | const char *plugin_name; | |
1475 | const char *component_name; | |
1476 | struct bt_config_component *bt_config_component = NULL; | |
1477 | ||
1478 | switch (legacy_output_format) { | |
1479 | case LEGACY_OUTPUT_FORMAT_TEXT: | |
1480 | plugin_name = "text"; | |
1481 | component_name = "text"; | |
1482 | break; | |
1483 | case LEGACY_OUTPUT_FORMAT_CTF_METADATA: | |
1484 | plugin_name = "ctf"; | |
1485 | component_name = "metadata-text"; | |
1486 | break; | |
1487 | case LEGACY_OUTPUT_FORMAT_DUMMY: | |
1488 | plugin_name = "dummy"; | |
1489 | component_name = "dummy"; | |
1490 | break; | |
1491 | default: | |
1492 | assert(false); | |
1493 | break; | |
1494 | } | |
1495 | ||
1496 | if (legacy_output_format == LEGACY_OUTPUT_FORMAT_TEXT) { | |
1497 | /* Legacy "text" output format has parameters */ | |
1498 | params = params_from_text_legacy_opts(text_legacy_opts); | |
1499 | if (!params) { | |
1500 | goto error; | |
1501 | } | |
1502 | } else { | |
1503 | /* | |
1504 | * Legacy "dummy" and "ctf-metadata" output formats do | |
1505 | * not have parameters. | |
1506 | */ | |
1507 | params = bt_value_map_create(); | |
1508 | if (!params) { | |
1509 | print_err_oom(); | |
1510 | goto error; | |
1511 | } | |
1512 | } | |
1513 | ||
1514 | /* Create a component configuration */ | |
1515 | bt_config_component = bt_config_component_create(plugin_name, | |
bdc61c70 | 1516 | component_name); |
c42c79ea PP |
1517 | if (!bt_config_component) { |
1518 | goto error; | |
1519 | } | |
1520 | ||
bdc61c70 PP |
1521 | BT_MOVE(bt_config_component->params, params); |
1522 | ||
c42c79ea PP |
1523 | /* Move created component configuration to the array */ |
1524 | g_ptr_array_add(sinks, bt_config_component); | |
1525 | ||
1526 | goto end; | |
1527 | ||
1528 | error: | |
1529 | ret = -1; | |
1530 | ||
1531 | end: | |
1532 | BT_PUT(params); | |
1533 | ||
1534 | return ret; | |
1535 | } | |
1536 | ||
1537 | /* | |
1538 | * Returns the parameters (map value object) corresponding to the | |
1539 | * given legacy CTF format options. | |
1540 | * | |
1541 | * Return value is owned by the caller. | |
1542 | */ | |
1543 | static | |
1544 | struct bt_value *params_from_ctf_legacy_opts( | |
1545 | struct ctf_legacy_opts *ctf_legacy_opts) | |
1546 | { | |
1547 | struct bt_value *params; | |
1548 | ||
1549 | params = bt_value_map_create(); | |
1550 | if (!params) { | |
1551 | print_err_oom(); | |
1552 | goto error; | |
1553 | } | |
1554 | ||
1555 | if (bt_value_map_insert_integer(params, "offset-s", | |
1556 | ctf_legacy_opts->offset_s.value)) { | |
1557 | print_err_oom(); | |
1558 | goto error; | |
1559 | } | |
1560 | ||
1561 | if (bt_value_map_insert_integer(params, "offset-ns", | |
1562 | ctf_legacy_opts->offset_ns.value)) { | |
1563 | print_err_oom(); | |
1564 | goto error; | |
1565 | } | |
1566 | ||
1567 | if (bt_value_map_insert_bool(params, "stream-intersection", | |
1568 | ctf_legacy_opts->stream_intersection)) { | |
1569 | print_err_oom(); | |
1570 | goto error; | |
1571 | } | |
1572 | ||
1573 | goto end; | |
1574 | ||
1575 | error: | |
1576 | BT_PUT(params); | |
1577 | ||
1578 | end: | |
1579 | return params; | |
1580 | } | |
1581 | ||
1582 | static | |
1583 | int append_sources_from_legacy_opts(GPtrArray *sources, | |
1584 | enum legacy_input_format legacy_input_format, | |
1585 | struct ctf_legacy_opts *ctf_legacy_opts, | |
015cee23 PP |
1586 | struct bt_value *legacy_input_paths, |
1587 | uint64_t begin_ns, uint64_t end_ns) | |
c42c79ea PP |
1588 | { |
1589 | int ret = 0; | |
1590 | int i; | |
1591 | struct bt_value *base_params; | |
1592 | struct bt_value *params = NULL; | |
1593 | struct bt_value *input_path = NULL; | |
1594 | struct bt_value *input_path_copy = NULL; | |
1595 | const char *input_key; | |
1596 | const char *component_name; | |
1597 | ||
1598 | switch (legacy_input_format) { | |
1599 | case LEGACY_INPUT_FORMAT_CTF: | |
1600 | input_key = "path"; | |
1601 | component_name = "fs"; | |
1602 | break; | |
1603 | case LEGACY_INPUT_FORMAT_LTTNG_LIVE: | |
1604 | input_key = "url"; | |
1605 | component_name = "lttng-live"; | |
1606 | break; | |
1607 | default: | |
1608 | assert(false); | |
1609 | break; | |
1610 | } | |
1611 | ||
1612 | base_params = params_from_ctf_legacy_opts(ctf_legacy_opts); | |
1613 | if (!base_params) { | |
1614 | goto error; | |
1615 | } | |
1616 | ||
1617 | for (i = 0; i < bt_value_array_size(legacy_input_paths); i++) { | |
1618 | struct bt_config_component *bt_config_component = NULL; | |
1619 | ||
1620 | /* Copy base parameters as current parameters */ | |
1621 | params = bt_value_copy(base_params); | |
1622 | if (!params) { | |
1623 | goto error; | |
1624 | } | |
1625 | ||
1626 | /* Get current input path string value object */ | |
1627 | input_path = bt_value_array_get(legacy_input_paths, i); | |
1628 | if (!input_path) { | |
1629 | goto error; | |
1630 | } | |
1631 | ||
1632 | /* Copy current input path value object */ | |
1633 | input_path_copy = bt_value_copy(input_path); | |
1634 | if (!input_path_copy) { | |
1635 | goto error; | |
1636 | } | |
1637 | ||
1638 | /* Insert input path value object into current parameters */ | |
1639 | ret = bt_value_map_insert(params, input_key, input_path_copy); | |
1640 | if (ret) { | |
1641 | goto error; | |
1642 | } | |
1643 | ||
1644 | /* Create a component configuration */ | |
1645 | bt_config_component = bt_config_component_create("ctf", | |
bdc61c70 | 1646 | component_name); |
c42c79ea PP |
1647 | if (!bt_config_component) { |
1648 | goto error; | |
1649 | } | |
1650 | ||
bdc61c70 | 1651 | BT_MOVE(bt_config_component->params, params); |
015cee23 PP |
1652 | bt_config_component->begin_ns = begin_ns; |
1653 | bt_config_component->end_ns = end_ns; | |
bdc61c70 | 1654 | |
c42c79ea PP |
1655 | /* Move created component configuration to the array */ |
1656 | g_ptr_array_add(sources, bt_config_component); | |
1657 | ||
1658 | /* Put current stuff */ | |
1659 | BT_PUT(input_path); | |
1660 | BT_PUT(input_path_copy); | |
c42c79ea PP |
1661 | } |
1662 | ||
1663 | goto end; | |
1664 | ||
1665 | error: | |
1666 | ret = -1; | |
1667 | ||
1668 | end: | |
1669 | BT_PUT(base_params); | |
1670 | BT_PUT(params); | |
1671 | BT_PUT(input_path); | |
1672 | BT_PUT(input_path_copy); | |
1673 | return ret; | |
1674 | } | |
1675 | ||
1676 | /* | |
1677 | * Escapes a string for the shell. The string is escaped knowing that | |
1678 | * it's a parameter string value (double-quoted), and that it will be | |
1679 | * entered between single quotes in the shell. | |
1680 | * | |
1681 | * Return value is owned by the caller. | |
1682 | */ | |
1683 | static | |
1684 | char *str_shell_escape(const char *input) | |
1685 | { | |
1686 | char *ret = NULL; | |
1687 | const char *at = input; | |
1688 | GString *str = g_string_new(NULL); | |
1689 | ||
1690 | if (!str) { | |
1691 | goto end; | |
1692 | } | |
1693 | ||
1694 | while (*at != '\0') { | |
1695 | switch (*at) { | |
1696 | case '\\': | |
1697 | g_string_append(str, "\\\\"); | |
1698 | break; | |
1699 | case '"': | |
1700 | g_string_append(str, "\\\""); | |
1701 | break; | |
1702 | case '\'': | |
1703 | g_string_append(str, "'\"'\"'"); | |
1704 | break; | |
1705 | case '\n': | |
1706 | g_string_append(str, "\\n"); | |
1707 | break; | |
1708 | case '\t': | |
1709 | g_string_append(str, "\\t"); | |
1710 | break; | |
1711 | default: | |
1712 | g_string_append_c(str, *at); | |
1713 | break; | |
1714 | } | |
1715 | ||
1716 | at++; | |
1717 | } | |
1718 | ||
1719 | end: | |
1720 | if (str) { | |
1721 | ret = str->str; | |
1722 | g_string_free(str, FALSE); | |
1723 | } | |
1724 | ||
1725 | return ret; | |
1726 | } | |
1727 | ||
1728 | static | |
1729 | int append_prefixed_flag_params(GString *str, struct bt_value *flags, | |
1730 | const char *prefix) | |
1731 | { | |
1732 | int ret = 0; | |
1733 | int i; | |
1734 | ||
1735 | if (!flags) { | |
1736 | goto end; | |
1737 | } | |
1738 | ||
1739 | for (i = 0; i < bt_value_array_size(flags); i++) { | |
1740 | struct bt_value *value = bt_value_array_get(flags, i); | |
1741 | const char *flag; | |
1742 | ||
1743 | if (!value) { | |
1744 | ret = -1; | |
1745 | goto end; | |
1746 | } | |
1747 | ||
1748 | if (bt_value_string_get(value, &flag)) { | |
1749 | BT_PUT(value); | |
1750 | ret = -1; | |
1751 | goto end; | |
1752 | } | |
1753 | ||
1754 | g_string_append_printf(str, ",%s-%s=true", prefix, flag); | |
1755 | BT_PUT(value); | |
1756 | } | |
1757 | ||
1758 | end: | |
1759 | return ret; | |
1760 | } | |
1761 | ||
1762 | /* | |
1763 | * Appends a boolean parameter string. | |
1764 | */ | |
1765 | static | |
1766 | void g_string_append_bool_param(GString *str, const char *name, bool value) | |
1767 | { | |
1768 | g_string_append_printf(str, ",%s=%s", name, value ? "true" : "false"); | |
1769 | } | |
1770 | ||
1771 | /* | |
1772 | * Appends a path parameter string, or null if it's empty. | |
1773 | */ | |
1774 | static | |
1775 | int g_string_append_string_path_param(GString *str, const char *name, | |
1776 | GString *path) | |
1777 | { | |
1778 | int ret = 0; | |
1779 | ||
1780 | if (path->len > 0) { | |
1781 | char *escaped_path = str_shell_escape(path->str); | |
1782 | ||
1783 | if (!escaped_path) { | |
1784 | print_err_oom(); | |
1785 | goto error; | |
1786 | } | |
1787 | ||
1788 | g_string_append_printf(str, "%s=\"%s\"", name, escaped_path); | |
1789 | free(escaped_path); | |
1790 | } else { | |
1791 | g_string_append_printf(str, "%s=null", name); | |
1792 | } | |
1793 | ||
1794 | goto end; | |
1795 | ||
1796 | error: | |
1797 | ret = -1; | |
1798 | ||
1799 | end: | |
1800 | return ret; | |
1801 | } | |
1802 | ||
1803 | /* | |
1804 | * Prints the non-legacy sink options equivalent to the specified | |
1805 | * legacy output format options. | |
1806 | */ | |
1807 | static | |
1808 | void print_output_legacy_to_sinks( | |
1809 | enum legacy_output_format legacy_output_format, | |
1810 | struct text_legacy_opts *text_legacy_opts) | |
1811 | { | |
1812 | const char *input_format; | |
1813 | GString *str = NULL; | |
1814 | ||
1815 | str = g_string_new(" "); | |
1816 | if (!str) { | |
1817 | print_err_oom(); | |
1818 | goto end; | |
1819 | } | |
1820 | ||
1821 | switch (legacy_output_format) { | |
1822 | case LEGACY_OUTPUT_FORMAT_TEXT: | |
1823 | input_format = "text"; | |
1824 | break; | |
1825 | case LEGACY_OUTPUT_FORMAT_CTF_METADATA: | |
1826 | input_format = "ctf-metadata"; | |
1827 | break; | |
1828 | case LEGACY_OUTPUT_FORMAT_DUMMY: | |
1829 | input_format = "dummy"; | |
1830 | break; | |
1831 | default: | |
1832 | assert(false); | |
1833 | } | |
1834 | ||
49849a47 | 1835 | printf_err("Both `%s` legacy output format and non-legacy sink component\ninstances(s) specified.\n\n", |
c42c79ea | 1836 | input_format); |
49849a47 | 1837 | printf_err("Specify the following non-legacy sink component instance instead of the\nlegacy `%s` output format options:\n\n", |
c42c79ea PP |
1838 | input_format); |
1839 | g_string_append(str, "-o "); | |
1840 | ||
1841 | switch (legacy_output_format) { | |
1842 | case LEGACY_OUTPUT_FORMAT_TEXT: | |
1843 | g_string_append(str, "text.text"); | |
1844 | break; | |
1845 | case LEGACY_OUTPUT_FORMAT_CTF_METADATA: | |
1846 | g_string_append(str, "ctf.metadata-text"); | |
1847 | break; | |
1848 | case LEGACY_OUTPUT_FORMAT_DUMMY: | |
1849 | g_string_append(str, "dummy.dummy"); | |
1850 | break; | |
1851 | default: | |
1852 | assert(false); | |
1853 | } | |
1854 | ||
1855 | if (legacy_output_format == LEGACY_OUTPUT_FORMAT_TEXT && | |
1856 | text_legacy_opts_is_any_set(text_legacy_opts)) { | |
1857 | int ret; | |
1858 | ||
bdc61c70 | 1859 | g_string_append(str, " -p '"); |
c42c79ea PP |
1860 | |
1861 | if (g_string_append_string_path_param(str, "output-path", | |
1862 | text_legacy_opts->output)) { | |
1863 | goto end; | |
1864 | } | |
1865 | ||
1866 | g_string_append(str, ","); | |
1867 | ||
1868 | if (g_string_append_string_path_param(str, "debug-info-dir", | |
1869 | text_legacy_opts->dbg_info_dir)) { | |
1870 | goto end; | |
1871 | } | |
1872 | ||
1873 | g_string_append(str, ","); | |
1874 | ||
1875 | if (g_string_append_string_path_param(str, | |
1876 | "debug-info-target-prefix", | |
1877 | text_legacy_opts->dbg_info_target_prefix)) { | |
1878 | goto end; | |
1879 | } | |
1880 | ||
1881 | g_string_append_bool_param(str, "no-delta", | |
1882 | text_legacy_opts->no_delta); | |
1883 | g_string_append_bool_param(str, "clock-cycles", | |
1884 | text_legacy_opts->clock_cycles); | |
1885 | g_string_append_bool_param(str, "clock-seconds", | |
1886 | text_legacy_opts->clock_seconds); | |
1887 | g_string_append_bool_param(str, "clock-date", | |
1888 | text_legacy_opts->clock_date); | |
1889 | g_string_append_bool_param(str, "clock-gmt", | |
1890 | text_legacy_opts->clock_gmt); | |
1891 | ret = append_prefixed_flag_params(str, text_legacy_opts->names, | |
1892 | "name"); | |
1893 | if (ret) { | |
1894 | goto end; | |
1895 | } | |
1896 | ||
1897 | ret = append_prefixed_flag_params(str, text_legacy_opts->fields, | |
1898 | "field"); | |
1899 | if (ret) { | |
1900 | goto end; | |
1901 | } | |
1902 | ||
1903 | /* Remove last comma and close single quote */ | |
1904 | g_string_append(str, "'"); | |
1905 | } | |
1906 | ||
1907 | printf_err("%s\n\n", str->str); | |
1908 | ||
1909 | end: | |
1910 | if (str) { | |
1911 | g_string_free(str, TRUE); | |
1912 | } | |
1913 | return; | |
1914 | } | |
1915 | ||
1916 | /* | |
1917 | * Prints the non-legacy source options equivalent to the specified | |
1918 | * legacy input format options. | |
1919 | */ | |
1920 | static | |
1921 | void print_input_legacy_to_sources(enum legacy_input_format legacy_input_format, | |
1922 | struct bt_value *legacy_input_paths, | |
1923 | struct ctf_legacy_opts *ctf_legacy_opts) | |
1924 | { | |
1925 | const char *input_format; | |
1926 | GString *str = NULL; | |
1927 | int i; | |
1928 | ||
1929 | str = g_string_new(" "); | |
1930 | if (!str) { | |
1931 | print_err_oom(); | |
1932 | goto end; | |
1933 | } | |
1934 | ||
1935 | switch (legacy_input_format) { | |
1936 | case LEGACY_INPUT_FORMAT_CTF: | |
1937 | input_format = "ctf"; | |
1938 | break; | |
1939 | case LEGACY_INPUT_FORMAT_LTTNG_LIVE: | |
1940 | input_format = "lttng-live"; | |
1941 | break; | |
1942 | default: | |
1943 | assert(false); | |
1944 | } | |
1945 | ||
49849a47 | 1946 | printf_err("Both `%s` legacy input format and non-legacy source component\ninstance(s) specified.\n\n", |
c42c79ea | 1947 | input_format); |
49849a47 | 1948 | printf_err("Specify the following non-legacy source component instance(s) instead of the\nlegacy `%s` input format options and positional arguments:\n\n", |
c42c79ea PP |
1949 | input_format); |
1950 | ||
1951 | for (i = 0; i < bt_value_array_size(legacy_input_paths); i++) { | |
1952 | struct bt_value *input_value = | |
1953 | bt_value_array_get(legacy_input_paths, i); | |
1954 | const char *input = NULL; | |
1955 | char *escaped_input; | |
1956 | int ret; | |
1957 | ||
1958 | assert(input_value); | |
1959 | ret = bt_value_string_get(input_value, &input); | |
1960 | BT_PUT(input_value); | |
1961 | assert(!ret && input); | |
1962 | escaped_input = str_shell_escape(input); | |
1963 | if (!escaped_input) { | |
1964 | print_err_oom(); | |
1965 | goto end; | |
1966 | } | |
1967 | ||
1968 | g_string_append(str, "-i ctf."); | |
1969 | ||
1970 | switch (legacy_input_format) { | |
1971 | case LEGACY_INPUT_FORMAT_CTF: | |
bdc61c70 | 1972 | g_string_append(str, "fs -p 'path=\""); |
c42c79ea PP |
1973 | break; |
1974 | case LEGACY_INPUT_FORMAT_LTTNG_LIVE: | |
bdc61c70 | 1975 | g_string_append(str, "lttng-live -p 'url=\""); |
c42c79ea PP |
1976 | break; |
1977 | default: | |
1978 | assert(false); | |
1979 | } | |
1980 | ||
1981 | g_string_append(str, escaped_input); | |
1982 | g_string_append(str, "\""); | |
1983 | g_string_append_printf(str, ",offset-s=%" PRId64, | |
1984 | ctf_legacy_opts->offset_s.value); | |
1985 | g_string_append_printf(str, ",offset-ns=%" PRId64, | |
1986 | ctf_legacy_opts->offset_ns.value); | |
1987 | g_string_append_bool_param(str, "stream-intersection", | |
1988 | ctf_legacy_opts->stream_intersection); | |
1989 | g_string_append(str, "' "); | |
1990 | g_free(escaped_input); | |
1991 | } | |
1992 | ||
1993 | printf_err("%s\n\n", str->str); | |
1994 | ||
1995 | end: | |
1996 | if (str) { | |
1997 | g_string_free(str, TRUE); | |
1998 | } | |
1999 | return; | |
2000 | } | |
2001 | ||
015cee23 PP |
2002 | /* |
2003 | * Validates a given source component configuration. | |
2004 | */ | |
2005 | static | |
2006 | bool validate_source_config_component(struct bt_config_component *cfg_comp) | |
2007 | { | |
2008 | bool valid = false; | |
2009 | ||
2010 | if (cfg_comp->begin_ns != -1ULL && cfg_comp->end_ns != -1ULL) { | |
2011 | if (cfg_comp->begin_ns > cfg_comp->end_ns) { | |
2012 | printf_err("Beginning timestamp (%" PRIu64 ") is greater than end timestamp (%" PRIu64 ")\n", | |
2013 | cfg_comp->begin_ns, cfg_comp->end_ns); | |
2014 | goto end; | |
2015 | } | |
2016 | } | |
2017 | ||
2018 | valid = true; | |
2019 | ||
2020 | end: | |
2021 | return valid; | |
2022 | } | |
2023 | ||
c42c79ea PP |
2024 | /* |
2025 | * Validates a given configuration, with optional legacy input and | |
2026 | * output formats options. Prints useful error messages if anything | |
2027 | * is wrong. | |
2028 | * | |
2029 | * Returns true when the configuration is valid. | |
2030 | */ | |
2031 | static | |
2032 | bool validate_cfg(struct bt_config *cfg, | |
2033 | enum legacy_input_format *legacy_input_format, | |
2034 | enum legacy_output_format *legacy_output_format, | |
2035 | struct bt_value *legacy_input_paths, | |
2036 | struct ctf_legacy_opts *ctf_legacy_opts, | |
2037 | struct text_legacy_opts *text_legacy_opts) | |
2038 | { | |
2039 | bool legacy_input = false; | |
2040 | bool legacy_output = false; | |
2041 | ||
2042 | /* Determine if the input and output should be legacy-style */ | |
2043 | if (*legacy_input_format != LEGACY_INPUT_FORMAT_NONE || | |
2044 | cfg->sources->len == 0 || | |
2045 | !bt_value_array_is_empty(legacy_input_paths) || | |
2046 | ctf_legacy_opts_is_any_set(ctf_legacy_opts)) { | |
2047 | legacy_input = true; | |
2048 | } | |
2049 | ||
2050 | if (*legacy_output_format != LEGACY_OUTPUT_FORMAT_NONE || | |
2051 | cfg->sinks->len == 0 || | |
2052 | text_legacy_opts_is_any_set(text_legacy_opts)) { | |
2053 | legacy_output = true; | |
2054 | } | |
2055 | ||
2056 | if (legacy_input) { | |
2057 | /* If no legacy input format was specified, default to CTF */ | |
2058 | if (*legacy_input_format == LEGACY_INPUT_FORMAT_NONE) { | |
2059 | *legacy_input_format = LEGACY_INPUT_FORMAT_CTF; | |
2060 | } | |
2061 | ||
2062 | /* Make sure at least one input path exists */ | |
2063 | if (bt_value_array_is_empty(legacy_input_paths)) { | |
2064 | switch (*legacy_input_format) { | |
2065 | case LEGACY_INPUT_FORMAT_CTF: | |
6d1d5711 | 2066 | printf_err("No input path specified for legacy `ctf` input format\n"); |
c42c79ea PP |
2067 | break; |
2068 | case LEGACY_INPUT_FORMAT_LTTNG_LIVE: | |
6d1d5711 | 2069 | printf_err("No URL specified for legacy `lttng-live` input format\n"); |
c42c79ea PP |
2070 | break; |
2071 | default: | |
2072 | assert(false); | |
2073 | } | |
2074 | goto error; | |
2075 | } | |
2076 | ||
2077 | /* Make sure no non-legacy sources are specified */ | |
2078 | if (cfg->sources->len != 0) { | |
2079 | print_input_legacy_to_sources(*legacy_input_format, | |
2080 | legacy_input_paths, ctf_legacy_opts); | |
2081 | goto error; | |
2082 | } | |
2083 | } | |
2084 | ||
2085 | if (legacy_output) { | |
2086 | /* | |
2087 | * If no legacy output format was specified, default to | |
2088 | * "text". | |
2089 | */ | |
2090 | if (*legacy_output_format == LEGACY_OUTPUT_FORMAT_NONE) { | |
2091 | *legacy_output_format = LEGACY_OUTPUT_FORMAT_TEXT; | |
2092 | } | |
2093 | ||
2094 | /* | |
2095 | * If any "text" option was specified, the output must be | |
2096 | * legacy "text". | |
2097 | */ | |
2098 | if (text_legacy_opts_is_any_set(text_legacy_opts) && | |
2099 | *legacy_output_format != | |
2100 | LEGACY_OUTPUT_FORMAT_TEXT) { | |
6d1d5711 | 2101 | printf_err("Options for legacy `text` output format specified with a different legacy output format\n"); |
c42c79ea PP |
2102 | goto error; |
2103 | } | |
2104 | ||
2105 | /* Make sure no non-legacy sinks are specified */ | |
2106 | if (cfg->sinks->len != 0) { | |
2107 | print_output_legacy_to_sinks(*legacy_output_format, | |
2108 | text_legacy_opts); | |
2109 | goto error; | |
2110 | } | |
2111 | } | |
2112 | ||
2113 | /* | |
2114 | * If the output is the legacy "ctf-metadata" format, then the | |
2115 | * input should be the legacy "ctf" input format. | |
2116 | */ | |
2117 | if (*legacy_output_format == LEGACY_OUTPUT_FORMAT_CTF_METADATA && | |
2118 | *legacy_input_format != LEGACY_INPUT_FORMAT_CTF) { | |
6d1d5711 | 2119 | printf_err("Legacy `ctf-metadata` output format requires using legacy `ctf` input format\n"); |
c42c79ea PP |
2120 | goto error; |
2121 | } | |
2122 | ||
2123 | return true; | |
2124 | ||
2125 | error: | |
2126 | return false; | |
2127 | } | |
2128 | ||
2129 | /* | |
2130 | * Parses a 64-bit signed integer. | |
2131 | * | |
2132 | * Returns a negative value if anything goes wrong. | |
2133 | */ | |
2134 | static | |
2135 | int parse_int64(const char *arg, int64_t *val) | |
2136 | { | |
2137 | char *endptr; | |
2138 | ||
2139 | errno = 0; | |
2140 | *val = strtoll(arg, &endptr, 0); | |
2141 | if (*endptr != '\0' || arg == endptr || errno != 0) { | |
2142 | return -1; | |
2143 | } | |
2144 | ||
2145 | return 0; | |
2146 | } | |
2147 | ||
015cee23 PP |
2148 | /* |
2149 | * Parses a time in nanoseconds. | |
2150 | * | |
2151 | * Returns a negative value if anything goes wrong. | |
2152 | */ | |
2153 | static | |
2154 | int ns_from_arg(const char *arg, uint64_t *ns) | |
2155 | { | |
2156 | int ret = 0; | |
2157 | int64_t value; | |
2158 | ||
2159 | if (parse_int64(arg, &value)) { | |
2160 | ret = -1; | |
2161 | goto end; | |
2162 | } | |
2163 | ||
2164 | if (value < 0) { | |
2165 | ret = -1; | |
2166 | goto end; | |
2167 | } | |
2168 | ||
2169 | *ns = (uint64_t) value; | |
2170 | ||
2171 | end: | |
2172 | return ret; | |
2173 | } | |
2174 | ||
c42c79ea PP |
2175 | /* popt options */ |
2176 | enum { | |
2177 | OPT_NONE = 0, | |
015cee23 PP |
2178 | OPT_BASE_BEGIN_NS, |
2179 | OPT_BASE_END_NS, | |
b07ffa28 | 2180 | OPT_BASE_PARAMS, |
015cee23 | 2181 | OPT_BEGIN_NS, |
c42c79ea PP |
2182 | OPT_CLOCK_CYCLES, |
2183 | OPT_CLOCK_DATE, | |
2184 | OPT_CLOCK_FORCE_CORRELATE, | |
2185 | OPT_CLOCK_GMT, | |
2186 | OPT_CLOCK_OFFSET, | |
2187 | OPT_CLOCK_OFFSET_NS, | |
2188 | OPT_CLOCK_SECONDS, | |
2189 | OPT_DEBUG, | |
2190 | OPT_DEBUG_INFO_DIR, | |
2191 | OPT_DEBUG_INFO_FULL_PATH, | |
2192 | OPT_DEBUG_INFO_TARGET_PREFIX, | |
015cee23 | 2193 | OPT_END_NS, |
c42c79ea PP |
2194 | OPT_FIELDS, |
2195 | OPT_HELP, | |
2196 | OPT_HELP_LEGACY, | |
2197 | OPT_INPUT_FORMAT, | |
2198 | OPT_LIST, | |
2199 | OPT_NAMES, | |
2200 | OPT_NO_DELTA, | |
2201 | OPT_OUTPUT_FORMAT, | |
2202 | OPT_OUTPUT_PATH, | |
bdc61c70 | 2203 | OPT_PARAMS, |
c42c79ea | 2204 | OPT_PLUGIN_PATH, |
015cee23 PP |
2205 | OPT_RESET_BASE_BEGIN_NS, |
2206 | OPT_RESET_BASE_END_NS, | |
b07ffa28 | 2207 | OPT_RESET_BASE_PARAMS, |
c42c79ea PP |
2208 | OPT_SINK, |
2209 | OPT_SOURCE, | |
2210 | OPT_STREAM_INTERSECTION, | |
2211 | OPT_VERBOSE, | |
2212 | OPT_VERSION, | |
2213 | }; | |
2214 | ||
2215 | /* popt long option descriptions */ | |
2216 | static struct poptOption long_options[] = { | |
2217 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
015cee23 PP |
2218 | { "base-begin-ns", 'B', POPT_ARG_STRING, NULL, OPT_BASE_BEGIN_NS, NULL, NULL }, |
2219 | { "base-end-ns", 'E', POPT_ARG_STRING, NULL, OPT_BASE_END_NS, NULL, NULL }, | |
b07ffa28 | 2220 | { "base-params", 'b', POPT_ARG_STRING, NULL, OPT_BASE_PARAMS, NULL, NULL }, |
015cee23 | 2221 | { "begin-ns", '\0', POPT_ARG_STRING, NULL, OPT_BEGIN_NS, NULL, NULL }, |
c42c79ea PP |
2222 | { "clock-cycles", '\0', POPT_ARG_NONE, NULL, OPT_CLOCK_CYCLES, NULL, NULL }, |
2223 | { "clock-date", '\0', POPT_ARG_NONE, NULL, OPT_CLOCK_DATE, NULL, NULL }, | |
2224 | { "clock-force-correlate", '\0', POPT_ARG_NONE, NULL, OPT_CLOCK_FORCE_CORRELATE, NULL, NULL }, | |
2225 | { "clock-gmt", '\0', POPT_ARG_NONE, NULL, OPT_CLOCK_GMT, NULL, NULL }, | |
2226 | { "clock-offset", '\0', POPT_ARG_STRING, NULL, OPT_CLOCK_OFFSET, NULL, NULL }, | |
2227 | { "clock-offset-ns", '\0', POPT_ARG_STRING, NULL, OPT_CLOCK_OFFSET_NS, NULL, NULL }, | |
2228 | { "clock-seconds", '\0', POPT_ARG_NONE, NULL, OPT_CLOCK_SECONDS, NULL, NULL }, | |
2229 | { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL }, | |
2230 | { "debug-info-dir", 0, POPT_ARG_STRING, NULL, OPT_DEBUG_INFO_DIR, NULL, NULL }, | |
2231 | { "debug-info-full-path", 0, POPT_ARG_NONE, NULL, OPT_DEBUG_INFO_FULL_PATH, NULL, NULL }, | |
2232 | { "debug-info-target-prefix", 0, POPT_ARG_STRING, NULL, OPT_DEBUG_INFO_TARGET_PREFIX, NULL, NULL }, | |
015cee23 | 2233 | { "end-ns", '\0', POPT_ARG_STRING, NULL, OPT_END_NS, NULL, NULL }, |
c42c79ea PP |
2234 | { "fields", 'f', POPT_ARG_STRING, NULL, OPT_FIELDS, NULL, NULL }, |
2235 | { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL }, | |
2236 | { "help-legacy", '\0', POPT_ARG_NONE, NULL, OPT_HELP_LEGACY, NULL, NULL }, | |
2237 | { "input-format", 'i', POPT_ARG_STRING, NULL, OPT_INPUT_FORMAT, NULL, NULL }, | |
2238 | { "list", 'l', POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL }, | |
2239 | { "names", 'n', POPT_ARG_STRING, NULL, OPT_NAMES, NULL, NULL }, | |
2240 | { "no-delta", '\0', POPT_ARG_NONE, NULL, OPT_NO_DELTA, NULL, NULL }, | |
2241 | { "output", 'w', POPT_ARG_STRING, NULL, OPT_OUTPUT_PATH, NULL, NULL }, | |
2242 | { "output-format", 'o', POPT_ARG_STRING, NULL, OPT_OUTPUT_FORMAT, NULL, NULL }, | |
bdc61c70 PP |
2243 | { "params", 'p', POPT_ARG_STRING, NULL, OPT_PARAMS, NULL, NULL }, |
2244 | { "plugin-path", 'P', POPT_ARG_STRING, NULL, OPT_PLUGIN_PATH, NULL, NULL }, | |
015cee23 PP |
2245 | { "reset-base-begin-ns", '\0', POPT_ARG_NONE, NULL, OPT_RESET_BASE_BEGIN_NS, NULL, NULL }, |
2246 | { "reset-base-end-ns", '\0', POPT_ARG_NONE, NULL, OPT_RESET_BASE_END_NS, NULL, NULL }, | |
b07ffa28 | 2247 | { "reset-base-params", 'r', POPT_ARG_NONE, NULL, OPT_RESET_BASE_PARAMS, NULL, NULL }, |
c42c79ea PP |
2248 | { "sink", '\0', POPT_ARG_STRING, NULL, OPT_SINK, NULL, NULL }, |
2249 | { "source", '\0', POPT_ARG_STRING, NULL, OPT_SOURCE, NULL, NULL }, | |
2250 | { "stream-intersection", '\0', POPT_ARG_NONE, NULL, OPT_STREAM_INTERSECTION, NULL, NULL }, | |
2251 | { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL }, | |
2252 | { "version", 'V', POPT_ARG_NONE, NULL, OPT_VERSION, NULL, NULL }, | |
2253 | { NULL, 0, 0, NULL, 0, NULL, NULL }, | |
2254 | }; | |
2255 | ||
2256 | /* | |
2257 | * Sets the value of a given legacy offset option and marks it as set. | |
2258 | */ | |
2259 | static void set_offset_value(struct offset_opt *offset_opt, int64_t value) | |
2260 | { | |
2261 | offset_opt->value = value; | |
2262 | offset_opt->is_set = true; | |
2263 | } | |
2264 | ||
bdc61c70 PP |
2265 | enum bt_config_component_dest { |
2266 | BT_CONFIG_COMPONENT_DEST_SOURCE, | |
2267 | BT_CONFIG_COMPONENT_DEST_SINK, | |
2268 | }; | |
2269 | ||
2270 | /* | |
2271 | * Adds a configuration component to the appropriate configuration | |
2272 | * array depending on the destination. | |
2273 | */ | |
2274 | static void add_cfg_comp(struct bt_config *cfg, | |
2275 | struct bt_config_component *cfg_comp, | |
2276 | enum bt_config_component_dest dest) | |
2277 | { | |
2278 | if (dest == BT_CONFIG_COMPONENT_DEST_SOURCE) { | |
2279 | g_ptr_array_add(cfg->sources, cfg_comp); | |
2280 | } else { | |
2281 | g_ptr_array_add(cfg->sinks, cfg_comp); | |
2282 | } | |
2283 | } | |
2284 | ||
c42c79ea PP |
2285 | /* |
2286 | * Returns a Babeltrace configuration, out of command-line arguments, | |
2287 | * containing everything that is needed to instanciate specific | |
2288 | * components with given parameters. | |
2289 | * | |
2290 | * *exit_code is set to the appropriate exit code to use as far as this | |
2291 | * function goes. | |
2292 | * | |
2293 | * Return value is NULL on error, otherwise it's owned by the caller. | |
2294 | */ | |
2295 | struct bt_config *bt_config_from_args(int argc, char *argv[], int *exit_code) | |
2296 | { | |
2297 | struct bt_config *cfg = NULL; | |
2298 | poptContext pc = NULL; | |
2299 | char *arg = NULL; | |
2300 | struct ctf_legacy_opts ctf_legacy_opts = { 0 }; | |
2301 | struct text_legacy_opts text_legacy_opts = { 0 }; | |
2302 | enum legacy_input_format legacy_input_format = LEGACY_INPUT_FORMAT_NONE; | |
2303 | enum legacy_output_format legacy_output_format = | |
2304 | LEGACY_OUTPUT_FORMAT_NONE; | |
2305 | struct bt_value *legacy_input_paths = NULL; | |
bdc61c70 PP |
2306 | struct bt_config_component *cur_cfg_comp = NULL; |
2307 | enum bt_config_component_dest cur_cfg_comp_dest = | |
2308 | BT_CONFIG_COMPONENT_DEST_SOURCE; | |
b07ffa28 | 2309 | struct bt_value *cur_base_params = NULL; |
015cee23 PP |
2310 | uint64_t cur_base_begin_ns = -1ULL; |
2311 | uint64_t cur_base_end_ns = -1ULL; | |
c42c79ea | 2312 | int opt; |
bdc61c70 | 2313 | bool cur_cfg_comp_params_set = false; |
015cee23 | 2314 | unsigned int i; |
c42c79ea PP |
2315 | |
2316 | *exit_code = 0; | |
2317 | ||
2318 | if (argc <= 1) { | |
2319 | print_usage(stdout); | |
2320 | goto end; | |
2321 | } | |
2322 | ||
2323 | text_legacy_opts.output = g_string_new(NULL); | |
2324 | if (!text_legacy_opts.output) { | |
2325 | print_err_oom(); | |
2326 | goto error; | |
2327 | } | |
2328 | ||
2329 | text_legacy_opts.dbg_info_dir = g_string_new(NULL); | |
2330 | if (!text_legacy_opts.dbg_info_dir) { | |
2331 | print_err_oom(); | |
2332 | goto error; | |
2333 | } | |
2334 | ||
2335 | text_legacy_opts.dbg_info_target_prefix = g_string_new(NULL); | |
2336 | if (!text_legacy_opts.dbg_info_target_prefix) { | |
2337 | print_err_oom(); | |
2338 | goto error; | |
2339 | } | |
2340 | ||
b07ffa28 PP |
2341 | cur_base_params = bt_value_map_create(); |
2342 | if (!cur_base_params) { | |
2343 | print_err_oom(); | |
2344 | goto error; | |
2345 | } | |
2346 | ||
c42c79ea PP |
2347 | /* Create config */ |
2348 | cfg = g_new0(struct bt_config, 1); | |
2349 | if (!cfg) { | |
2350 | print_err_oom(); | |
2351 | goto error; | |
2352 | } | |
2353 | ||
2354 | bt_object_init(cfg, bt_config_destroy); | |
2355 | cfg->sources = g_ptr_array_new_with_free_func((GDestroyNotify) bt_put); | |
2356 | if (!cfg->sources) { | |
2357 | print_err_oom(); | |
2358 | goto error; | |
2359 | } | |
2360 | ||
2361 | cfg->sinks = g_ptr_array_new_with_free_func((GDestroyNotify) bt_put); | |
2362 | if (!cfg->sinks) { | |
2363 | print_err_oom(); | |
2364 | goto error; | |
2365 | } | |
2366 | ||
2367 | legacy_input_paths = bt_value_array_create(); | |
2368 | if (!legacy_input_paths) { | |
2369 | print_err_oom(); | |
2370 | goto error; | |
2371 | } | |
2372 | ||
2373 | /* Parse options */ | |
2374 | pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0); | |
2375 | if (!pc) { | |
2376 | printf_err("Cannot get popt context\n"); | |
2377 | goto error; | |
2378 | } | |
2379 | ||
2380 | poptReadDefaultConfig(pc, 0); | |
2381 | ||
2382 | while ((opt = poptGetNextOpt(pc)) > 0) { | |
2383 | arg = poptGetOptArg(pc); | |
2384 | ||
2385 | switch (opt) { | |
2386 | case OPT_PLUGIN_PATH: | |
2387 | if (cfg->plugin_paths) { | |
2388 | printf_err("Duplicate --plugin-path option\n"); | |
2389 | goto error; | |
2390 | } | |
2391 | ||
2392 | cfg->plugin_paths = plugin_paths_from_arg(arg); | |
2393 | if (!cfg->plugin_paths) { | |
2394 | printf_err("Invalid --plugin-path option's argument\n"); | |
2395 | goto error; | |
2396 | } | |
2397 | break; | |
2398 | case OPT_OUTPUT_PATH: | |
2399 | if (text_legacy_opts.output->len > 0) { | |
2400 | printf_err("Duplicate --output option\n"); | |
2401 | goto error; | |
2402 | } | |
2403 | ||
2404 | g_string_assign(text_legacy_opts.output, arg); | |
2405 | break; | |
2406 | case OPT_DEBUG_INFO_DIR: | |
2407 | if (text_legacy_opts.dbg_info_dir->len > 0) { | |
2408 | printf_err("Duplicate --debug-info-dir option\n"); | |
2409 | goto error; | |
2410 | } | |
2411 | ||
2412 | g_string_assign(text_legacy_opts.dbg_info_dir, arg); | |
2413 | break; | |
2414 | case OPT_DEBUG_INFO_TARGET_PREFIX: | |
2415 | if (text_legacy_opts.dbg_info_target_prefix->len > 0) { | |
2416 | printf_err("Duplicate --debug-info-target-prefix option\n"); | |
2417 | goto error; | |
2418 | } | |
2419 | ||
2420 | g_string_assign(text_legacy_opts.dbg_info_target_prefix, arg); | |
2421 | break; | |
2422 | case OPT_INPUT_FORMAT: | |
2423 | case OPT_SOURCE: | |
2424 | { | |
c42c79ea PP |
2425 | if (opt == OPT_INPUT_FORMAT) { |
2426 | if (!strcmp(arg, "ctf")) { | |
2427 | /* Legacy CTF input format */ | |
2428 | if (legacy_input_format) { | |
2429 | print_err_dup_legacy_input(); | |
2430 | goto error; | |
2431 | } | |
2432 | ||
2433 | legacy_input_format = | |
2434 | LEGACY_INPUT_FORMAT_CTF; | |
2435 | break; | |
2436 | } else if (!strcmp(arg, "lttng-live")) { | |
2437 | /* Legacy LTTng-live input format */ | |
2438 | if (legacy_input_format) { | |
2439 | print_err_dup_legacy_input(); | |
2440 | goto error; | |
2441 | } | |
2442 | ||
2443 | legacy_input_format = | |
2444 | LEGACY_INPUT_FORMAT_LTTNG_LIVE; | |
2445 | break; | |
2446 | } | |
2447 | } | |
2448 | ||
2449 | /* Non-legacy: try to create a component config */ | |
bdc61c70 PP |
2450 | if (cur_cfg_comp) { |
2451 | add_cfg_comp(cfg, cur_cfg_comp, | |
2452 | cur_cfg_comp_dest); | |
2453 | } | |
2454 | ||
2455 | cur_cfg_comp = bt_config_component_from_arg(arg); | |
2456 | if (!cur_cfg_comp) { | |
49849a47 | 2457 | printf_err("Invalid format for --source option's argument:\n %s\n", |
c42c79ea PP |
2458 | arg); |
2459 | goto error; | |
2460 | } | |
2461 | ||
b07ffa28 PP |
2462 | assert(cur_base_params); |
2463 | bt_put(cur_cfg_comp->params); | |
c9313318 PP |
2464 | cur_cfg_comp->params = bt_value_copy(cur_base_params); |
2465 | if (!cur_cfg_comp) { | |
2466 | print_err_oom(); | |
2467 | goto end; | |
2468 | } | |
2469 | ||
015cee23 PP |
2470 | cur_cfg_comp->begin_ns = cur_base_begin_ns; |
2471 | cur_cfg_comp->end_ns = cur_base_end_ns; | |
bdc61c70 PP |
2472 | cur_cfg_comp_dest = BT_CONFIG_COMPONENT_DEST_SOURCE; |
2473 | cur_cfg_comp_params_set = false; | |
c42c79ea PP |
2474 | break; |
2475 | } | |
2476 | case OPT_OUTPUT_FORMAT: | |
2477 | case OPT_SINK: | |
2478 | { | |
c42c79ea PP |
2479 | if (opt == OPT_OUTPUT_FORMAT) { |
2480 | if (!strcmp(arg, "text")) { | |
2481 | /* Legacy CTF-text output format */ | |
2482 | if (legacy_output_format) { | |
2483 | print_err_dup_legacy_output(); | |
2484 | goto error; | |
2485 | } | |
2486 | ||
2487 | legacy_output_format = | |
2488 | LEGACY_OUTPUT_FORMAT_TEXT; | |
2489 | break; | |
2490 | } else if (!strcmp(arg, "dummy")) { | |
2491 | /* Legacy dummy output format */ | |
2492 | if (legacy_output_format) { | |
2493 | print_err_dup_legacy_output(); | |
2494 | goto error; | |
2495 | } | |
2496 | ||
2497 | legacy_output_format = | |
2498 | LEGACY_OUTPUT_FORMAT_DUMMY; | |
2499 | break; | |
2500 | } else if (!strcmp(arg, "ctf-metadata")) { | |
2501 | /* Legacy CTF-metadata output format */ | |
2502 | if (legacy_output_format) { | |
2503 | print_err_dup_legacy_output(); | |
2504 | goto error; | |
2505 | } | |
2506 | ||
2507 | legacy_output_format = | |
2508 | LEGACY_OUTPUT_FORMAT_CTF_METADATA; | |
2509 | break; | |
2510 | } | |
2511 | } | |
2512 | ||
2513 | /* Non-legacy: try to create a component config */ | |
bdc61c70 PP |
2514 | if (cur_cfg_comp) { |
2515 | add_cfg_comp(cfg, cur_cfg_comp, | |
2516 | cur_cfg_comp_dest); | |
2517 | } | |
2518 | ||
2519 | cur_cfg_comp = bt_config_component_from_arg(arg); | |
2520 | if (!cur_cfg_comp) { | |
49849a47 | 2521 | printf_err("Invalid format for --sink option's argument:\n %s\n", |
c42c79ea PP |
2522 | arg); |
2523 | goto error; | |
2524 | } | |
2525 | ||
b07ffa28 PP |
2526 | assert(cur_base_params); |
2527 | bt_put(cur_cfg_comp->params); | |
c9313318 PP |
2528 | cur_cfg_comp->params = bt_value_copy(cur_base_params); |
2529 | if (!cur_cfg_comp) { | |
2530 | print_err_oom(); | |
2531 | goto end; | |
2532 | } | |
2533 | ||
bdc61c70 PP |
2534 | cur_cfg_comp_dest = BT_CONFIG_COMPONENT_DEST_SINK; |
2535 | cur_cfg_comp_params_set = false; | |
2536 | break; | |
2537 | } | |
2538 | case OPT_PARAMS: | |
2539 | { | |
2540 | struct bt_value *params; | |
b07ffa28 | 2541 | struct bt_value *params_to_set; |
bdc61c70 PP |
2542 | |
2543 | if (!cur_cfg_comp) { | |
2544 | printf_err("--params option must follow a --source or --sink option\n"); | |
2545 | goto error; | |
2546 | } | |
2547 | ||
2548 | if (cur_cfg_comp_params_set) { | |
49849a47 | 2549 | printf_err("Duplicate --params option for the same current component\ninstance (class %s.%s)\n", |
bdc61c70 PP |
2550 | cur_cfg_comp->plugin_name->str, |
2551 | cur_cfg_comp->component_name->str); | |
2552 | goto error; | |
2553 | } | |
2554 | ||
2555 | params = bt_value_from_arg(arg); | |
2556 | if (!params) { | |
2557 | printf_err("Invalid format for --params option's argument:\n %s\n", | |
2558 | arg); | |
2559 | goto error; | |
2560 | } | |
2561 | ||
b07ffa28 PP |
2562 | params_to_set = bt_value_map_extend(cur_base_params, |
2563 | params); | |
2564 | BT_PUT(params); | |
2565 | if (!params_to_set) { | |
2566 | printf_err("Cannot extend current base parameters with --params option's argument:\n %s\n", | |
2567 | arg); | |
2568 | goto error; | |
2569 | } | |
2570 | ||
2571 | BT_MOVE(cur_cfg_comp->params, params_to_set); | |
bdc61c70 | 2572 | cur_cfg_comp_params_set = true; |
c42c79ea PP |
2573 | break; |
2574 | } | |
b07ffa28 PP |
2575 | case OPT_BASE_PARAMS: |
2576 | { | |
2577 | struct bt_value *params = bt_value_from_arg(arg); | |
2578 | ||
2579 | if (!params) { | |
2580 | printf_err("Invalid format for --base-params option's argument:\n %s\n", | |
2581 | arg); | |
2582 | goto error; | |
2583 | } | |
2584 | ||
2585 | BT_MOVE(cur_base_params, params); | |
2586 | break; | |
2587 | } | |
2588 | case OPT_RESET_BASE_PARAMS: | |
2589 | BT_PUT(cur_base_params); | |
2590 | cur_base_params = bt_value_map_create(); | |
2591 | if (!cur_base_params) { | |
2592 | print_err_oom(); | |
2593 | goto error; | |
2594 | } | |
2595 | break; | |
015cee23 PP |
2596 | case OPT_BASE_BEGIN_NS: |
2597 | if (ns_from_arg(arg, &cur_base_begin_ns)) { | |
2598 | printf_err("Invalid --base-begin-ns option's argument:\n %s\n", | |
2599 | arg); | |
2600 | goto error; | |
2601 | } | |
2602 | break; | |
2603 | case OPT_BASE_END_NS: | |
2604 | if (ns_from_arg(arg, &cur_base_end_ns)) { | |
2605 | printf_err("Invalid --base-end-ns option's argument:\n %s\n", | |
2606 | arg); | |
2607 | goto error; | |
2608 | } | |
2609 | break; | |
2610 | case OPT_RESET_BASE_BEGIN_NS: | |
2611 | cur_base_begin_ns = -1ULL; | |
2612 | break; | |
2613 | case OPT_RESET_BASE_END_NS: | |
2614 | cur_base_end_ns = -1ULL; | |
2615 | break; | |
2616 | case OPT_BEGIN_NS: | |
2617 | if (!cur_cfg_comp || cur_cfg_comp_dest == | |
2618 | BT_CONFIG_COMPONENT_DEST_SINK) { | |
2619 | printf_err("--begin-ns option must follow a --source option\n"); | |
2620 | goto error; | |
2621 | } | |
2622 | ||
2623 | if (ns_from_arg(arg, &cur_cfg_comp->begin_ns)) { | |
2624 | printf_err("Invalid --begin-ns option's argument:\n %s\n", | |
2625 | arg); | |
2626 | goto error; | |
2627 | } | |
2628 | break; | |
2629 | case OPT_END_NS: | |
2630 | if (!cur_cfg_comp || cur_cfg_comp_dest == | |
2631 | BT_CONFIG_COMPONENT_DEST_SINK) { | |
2632 | printf_err("--end-ns option must follow a --source option\n"); | |
2633 | goto error; | |
2634 | } | |
2635 | ||
2636 | if (ns_from_arg(arg, &cur_cfg_comp->end_ns)) { | |
2637 | printf_err("Invalid --end-ns option's argument:\n %s\n", | |
2638 | arg); | |
2639 | goto error; | |
2640 | } | |
2641 | break; | |
c42c79ea PP |
2642 | case OPT_NAMES: |
2643 | if (text_legacy_opts.names) { | |
2644 | printf_err("Duplicate --names option\n"); | |
2645 | goto error; | |
2646 | } | |
2647 | ||
2648 | text_legacy_opts.names = names_from_arg(arg); | |
2649 | if (!text_legacy_opts.names) { | |
2650 | printf_err("Invalid --names option's argument\n"); | |
2651 | goto error; | |
2652 | } | |
2653 | break; | |
2654 | case OPT_FIELDS: | |
2655 | if (text_legacy_opts.fields) { | |
2656 | printf_err("Duplicate --fields option\n"); | |
2657 | goto error; | |
2658 | } | |
2659 | ||
2660 | text_legacy_opts.fields = fields_from_arg(arg); | |
2661 | if (!text_legacy_opts.fields) { | |
2662 | printf_err("Invalid --fields option's argument\n"); | |
2663 | goto error; | |
2664 | } | |
2665 | break; | |
2666 | case OPT_NO_DELTA: | |
2667 | text_legacy_opts.no_delta = true; | |
2668 | break; | |
2669 | case OPT_CLOCK_CYCLES: | |
2670 | text_legacy_opts.clock_cycles = true; | |
2671 | break; | |
2672 | case OPT_CLOCK_SECONDS: | |
2673 | text_legacy_opts.clock_seconds = true; | |
2674 | break; | |
2675 | case OPT_CLOCK_DATE: | |
2676 | text_legacy_opts.clock_date = true; | |
2677 | break; | |
2678 | case OPT_CLOCK_GMT: | |
2679 | text_legacy_opts.clock_gmt = true; | |
2680 | break; | |
2681 | case OPT_DEBUG_INFO_FULL_PATH: | |
2682 | text_legacy_opts.dbg_info_full_path = true; | |
2683 | break; | |
2684 | case OPT_CLOCK_OFFSET: | |
2685 | { | |
2686 | int64_t val; | |
2687 | ||
2688 | if (ctf_legacy_opts.offset_s.is_set) { | |
2689 | printf_err("Duplicate --clock-offset option\n"); | |
2690 | goto error; | |
2691 | } | |
2692 | ||
2693 | if (parse_int64(arg, &val)) { | |
2694 | printf_err("Invalid --clock-offset option's argument\n"); | |
2695 | goto error; | |
2696 | } | |
2697 | ||
2698 | set_offset_value(&ctf_legacy_opts.offset_s, val); | |
2699 | break; | |
2700 | } | |
2701 | case OPT_CLOCK_OFFSET_NS: | |
2702 | { | |
2703 | int64_t val; | |
2704 | ||
2705 | if (ctf_legacy_opts.offset_ns.is_set) { | |
2706 | printf_err("Duplicate --clock-offset-ns option\n"); | |
2707 | goto error; | |
2708 | } | |
2709 | ||
2710 | if (parse_int64(arg, &val)) { | |
2711 | printf_err("Invalid --clock-offset-ns option's argument\n"); | |
2712 | goto error; | |
2713 | } | |
2714 | ||
2715 | set_offset_value(&ctf_legacy_opts.offset_ns, val); | |
2716 | break; | |
2717 | } | |
2718 | case OPT_STREAM_INTERSECTION: | |
2719 | ctf_legacy_opts.stream_intersection = true; | |
2720 | break; | |
2721 | case OPT_CLOCK_FORCE_CORRELATE: | |
2722 | cfg->force_correlate = true; | |
2723 | break; | |
2724 | case OPT_HELP: | |
2725 | BT_PUT(cfg); | |
2726 | print_usage(stdout); | |
2727 | goto end; | |
2728 | case OPT_HELP_LEGACY: | |
2729 | BT_PUT(cfg); | |
2730 | print_legacy_usage(stdout); | |
2731 | goto end; | |
2732 | case OPT_VERSION: | |
2733 | BT_PUT(cfg); | |
2734 | print_version(); | |
2735 | goto end; | |
2736 | case OPT_LIST: | |
2737 | cfg->do_list = true; | |
2738 | goto end; | |
2739 | case OPT_VERBOSE: | |
2740 | cfg->verbose = true; | |
2741 | break; | |
2742 | case OPT_DEBUG: | |
2743 | cfg->debug = true; | |
2744 | break; | |
2745 | default: | |
2746 | printf_err("Unknown command-line option specified (option code %d)\n", | |
2747 | opt); | |
2748 | goto error; | |
2749 | } | |
2750 | ||
2751 | free(arg); | |
2752 | arg = NULL; | |
2753 | } | |
2754 | ||
bdc61c70 PP |
2755 | /* Append current component configuration, if any */ |
2756 | if (cur_cfg_comp) { | |
2757 | add_cfg_comp(cfg, cur_cfg_comp, cur_cfg_comp_dest); | |
2758 | cur_cfg_comp = NULL; | |
2759 | } | |
2760 | ||
c42c79ea PP |
2761 | /* Check for option parsing error */ |
2762 | if (opt < -1) { | |
2763 | printf_err("While parsing command-line options, at option %s: %s\n", | |
2764 | poptBadOption(pc, 0), poptStrerror(opt)); | |
2765 | goto error; | |
2766 | } | |
2767 | ||
2768 | /* Consume leftover arguments as legacy input paths */ | |
2769 | while (true) { | |
2770 | const char *input_path = poptGetArg(pc); | |
2771 | ||
2772 | if (!input_path) { | |
2773 | break; | |
2774 | } | |
2775 | ||
2776 | if (bt_value_array_append_string(legacy_input_paths, | |
2777 | input_path)) { | |
2778 | print_err_oom(); | |
2779 | goto error; | |
2780 | } | |
2781 | } | |
2782 | ||
2783 | /* Validate legacy/non-legacy options */ | |
2784 | if (!validate_cfg(cfg, &legacy_input_format, &legacy_output_format, | |
2785 | legacy_input_paths, &ctf_legacy_opts, | |
2786 | &text_legacy_opts)) { | |
2787 | printf_err("Command-line options form an invalid configuration\n"); | |
2788 | goto error; | |
2789 | } | |
2790 | ||
2791 | /* | |
2792 | * If there's a legacy input format, convert it to source | |
2793 | * component configurations. | |
2794 | */ | |
2795 | if (legacy_input_format) { | |
2796 | if (append_sources_from_legacy_opts(cfg->sources, | |
2797 | legacy_input_format, &ctf_legacy_opts, | |
015cee23 PP |
2798 | legacy_input_paths, cur_base_begin_ns, |
2799 | cur_base_end_ns)) { | |
49849a47 | 2800 | printf_err("Cannot convert legacy input format options to source component instance(s)\n"); |
c42c79ea PP |
2801 | goto error; |
2802 | } | |
2803 | } | |
2804 | ||
2805 | /* | |
2806 | * If there's a legacy output format, convert it to sink | |
2807 | * component configurations. | |
2808 | */ | |
2809 | if (legacy_output_format) { | |
2810 | if (append_sinks_from_legacy_opts(cfg->sinks, | |
2811 | legacy_output_format, &text_legacy_opts)) { | |
49849a47 | 2812 | printf_err("Cannot convert legacy output format options to sink component instance(s)\n"); |
c42c79ea PP |
2813 | goto error; |
2814 | } | |
2815 | } | |
2816 | ||
015cee23 PP |
2817 | for (i = 0; i < cfg->sources->len; i++) { |
2818 | struct bt_config_component *cfg_component = | |
2819 | bt_config_get_component(cfg->sources, i); | |
2820 | ||
2821 | /* Only peek */ | |
2822 | bt_put(cfg_component); | |
2823 | ||
2824 | if (!validate_source_config_component(cfg_component)) { | |
49849a47 | 2825 | printf_err("Invalid source component instance (class %s.%s)\n", |
015cee23 PP |
2826 | cfg_component->plugin_name->str, |
2827 | cfg_component->component_name->str); | |
2828 | goto error; | |
2829 | } | |
2830 | } | |
2831 | ||
c42c79ea PP |
2832 | goto end; |
2833 | ||
2834 | error: | |
2835 | BT_PUT(cfg); | |
2836 | cfg = NULL; | |
2837 | *exit_code = 1; | |
2838 | ||
2839 | end: | |
2840 | if (pc) { | |
2841 | poptFreeContext(pc); | |
2842 | } | |
2843 | ||
2844 | if (text_legacy_opts.output) { | |
2845 | g_string_free(text_legacy_opts.output, TRUE); | |
2846 | } | |
2847 | ||
2848 | if (text_legacy_opts.dbg_info_dir) { | |
2849 | g_string_free(text_legacy_opts.dbg_info_dir, TRUE); | |
2850 | } | |
2851 | ||
2852 | if (text_legacy_opts.dbg_info_target_prefix) { | |
2853 | g_string_free(text_legacy_opts.dbg_info_target_prefix, TRUE); | |
2854 | } | |
2855 | ||
2856 | free(arg); | |
bdc61c70 | 2857 | BT_PUT(cur_cfg_comp); |
b07ffa28 | 2858 | BT_PUT(cur_base_params); |
c42c79ea PP |
2859 | BT_PUT(text_legacy_opts.names); |
2860 | BT_PUT(text_legacy_opts.fields); | |
2861 | BT_PUT(legacy_input_paths); | |
2862 | return cfg; | |
2863 | } |