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