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 | ||
bf403eb2 | 25 | #define BT_LOG_TAG "CLI/CFG-CLI-ARGS" |
f4f9e43b PP |
26 | #include "logging.h" |
27 | ||
9009cc24 PP |
28 | #include <errno.h> |
29 | #include <stdlib.h> | |
30 | #include <string.h> | |
57952005 | 31 | #include "common/assert.h" |
9009cc24 PP |
32 | #include <stdio.h> |
33 | #include <stdbool.h> | |
34 | #include <inttypes.h> | |
71c5da58 | 35 | #include <babeltrace2/babeltrace.h> |
57952005 | 36 | #include "common/common.h" |
9009cc24 PP |
37 | #include <glib.h> |
38 | #include <sys/types.h> | |
091cc3eb | 39 | #include "argpar/argpar.h" |
142ac9b0 MJ |
40 | #include "babeltrace2-cfg.h" |
41 | #include "babeltrace2-cfg-cli-args.h" | |
42 | #include "babeltrace2-cfg-cli-args-connect.h" | |
87c4d6ca | 43 | #include "babeltrace2-cfg-cli-params-arg.h" |
d9029925 | 44 | #include "babeltrace2-log-level.h" |
a1040187 | 45 | #include "babeltrace2-plugins.h" |
77a5caaf SM |
46 | #include "babeltrace2-query.h" |
47 | #include "autodisc/autodisc.h" | |
57952005 | 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) */ | |
8eee8ea2 | 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; | |
8eee8ea2 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 | ||
4b39e5c9 | 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 | |
75a1a799 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, | |
ee78f405 | 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 | ||
8b45963b 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)) { | |
4b39e5c9 | 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') { | |
4b39e5c9 | 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 { | |
4b39e5c9 | 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') { | |
4b39e5c9 | 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) { | |
4b39e5c9 | 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 | { | |
b6a0d2d9 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 | |
8eee8ea2 | 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 | ||
8c6884d9 | 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( | |
ee78f405 | 333 | bt_component_class_type type, |
c4f81dc9 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) { | |
4b39e5c9 | 341 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
342 | goto error; |
343 | } | |
344 | ||
1d7bf349 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) { | |
4b39e5c9 | 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) { | |
4b39e5c9 | 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) { | |
4b39e5c9 | 362 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
363 | goto error; |
364 | } | |
365 | ||
c4f81dc9 PP |
366 | cfg_component->log_level = init_log_level; |
367 | ||
9009cc24 | 368 | /* Start with empty parameters */ |
ce141536 | 369 | cfg_component->params = bt_value_map_create(); |
9009cc24 | 370 | if (!cfg_component->params) { |
4b39e5c9 | 371 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
372 | goto error; |
373 | } | |
374 | ||
375 | goto end; | |
376 | ||
377 | error: | |
8138bfe1 | 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 | |
c4f81dc9 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; | |
ee78f405 | 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 | ||
c4f81dc9 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: | |
8138bfe1 | 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 | |
8eee8ea2 | 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 | ||
8c6884d9 | 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: | |
8138bfe1 | 462 | BT_OBJECT_PUT_REF_AND_RESET(cfg->cmd_data.help.cfg_component); |
9009cc24 PP |
463 | break; |
464 | case BT_CONFIG_COMMAND_QUERY: | |
8138bfe1 | 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); | |
a107deea 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); | |
a107deea 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 | ||
8e01f2d9 | 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) { | |
4b39e5c9 | 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 | |
8eee8ea2 | 570 | bt_value *names_from_arg(const char *arg) |
9009cc24 PP |
571 | { |
572 | GScanner *scanner = NULL; | |
8eee8ea2 | 573 | bt_value *names = NULL; |
9009cc24 PP |
574 | bool found_all = false, found_none = false, found_item = false; |
575 | ||
ce141536 | 576 | names = bt_value_array_create(); |
9009cc24 | 577 | if (!names) { |
4b39e5c9 | 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 | ||
acfa8112 PP |
597 | if (strcmp(identifier, "payload") == 0 || |
598 | strcmp(identifier, "args") == 0 || | |
599 | strcmp(identifier, "arg") == 0) { | |
9009cc24 | 600 | found_item = true; |
ce141536 | 601 | if (bt_value_array_append_string_element(names, |
9009cc24 PP |
602 | "payload")) { |
603 | goto error; | |
604 | } | |
acfa8112 PP |
605 | } else if (strcmp(identifier, "context") == 0 || |
606 | strcmp(identifier, "ctx") == 0) { | |
9009cc24 | 607 | found_item = true; |
ce141536 | 608 | if (bt_value_array_append_string_element(names, |
9009cc24 PP |
609 | "context")) { |
610 | goto error; | |
611 | } | |
acfa8112 PP |
612 | } else if (strcmp(identifier, "scope") == 0 || |
613 | strcmp(identifier, "header") == 0) { | |
9009cc24 | 614 | found_item = true; |
ce141536 | 615 | if (bt_value_array_append_string_element(names, |
9009cc24 PP |
616 | identifier)) { |
617 | goto error; | |
618 | } | |
acfa8112 | 619 | } else if (strcmp(identifier, "all") == 0) { |
9009cc24 | 620 | found_all = true; |
ce141536 | 621 | if (bt_value_array_append_string_element(names, |
9009cc24 PP |
622 | identifier)) { |
623 | goto error; | |
624 | } | |
acfa8112 | 625 | } else if (strcmp(identifier, "none") == 0) { |
9009cc24 | 626 | found_none = true; |
ce141536 | 627 | if (bt_value_array_append_string_element(names, |
9009cc24 PP |
628 | identifier)) { |
629 | goto error; | |
630 | } | |
631 | } else { | |
4b39e5c9 | 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) { | |
4b39e5c9 | 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) { | |
ce141536 | 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: | |
8c6884d9 | 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 | |
8eee8ea2 | 682 | bt_value *fields_from_arg(const char *arg) |
9009cc24 PP |
683 | { |
684 | GScanner *scanner = NULL; | |
8eee8ea2 | 685 | bt_value *fields; |
9009cc24 | 686 | |
ce141536 | 687 | fields = bt_value_array_create(); |
9009cc24 | 688 | if (!fields) { |
4b39e5c9 | 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 | ||
acfa8112 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) { | |
ce141536 | 717 | if (bt_value_array_append_string_element(fields, |
9009cc24 PP |
718 | identifier)) { |
719 | goto error; | |
720 | } | |
721 | } else { | |
4b39e5c9 | 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: | |
8c6884d9 | 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 | { | |
8b45963b 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, | |
8eee8ea2 | 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) { | |
4b39e5c9 | 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) { | |
4b39e5c9 | 795 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
796 | ret = -1; |
797 | goto end; | |
798 | } | |
799 | ||
1270d0e9 | 800 | for (i = 0; i < bt_value_array_get_length(names_array); i++) { |
8eee8ea2 | 801 | const bt_value *str_obj = |
ce141536 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) { | |
4b39e5c9 | 808 | BT_CLI_LOGE_APPEND_CAUSE("Unexpected error."); |
9009cc24 PP |
809 | ret = -1; |
810 | goto end; | |
811 | } | |
812 | ||
b5cdc106 | 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". */ | |
acfa8112 | 819 | if (strcmp(suffix, "all") == 0) { |
9009cc24 PP |
820 | is_default = true; |
821 | g_string_assign(default_value, "show"); | |
acfa8112 | 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 | ||
34c83b89 | 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, | |
8e765000 | 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, |
c4f81dc9 | 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, |
091cc3eb | 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 | { | |
8138bfe1 | 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, | |
8eee8ea2 | 939 | bt_value *instance_names) |
9009cc24 PP |
940 | { |
941 | int ret = 0; | |
942 | ||
943 | if (cfg_comp->instance_name->len == 0) { | |
4b39e5c9 | 944 | BT_CLI_LOGE_APPEND_CAUSE("Found an unnamed component."); |
9009cc24 PP |
945 | ret = -1; |
946 | goto end; | |
947 | } | |
948 | ||
ce141536 PP |
949 | if (bt_value_map_has_entry(instance_names, |
950 | cfg_comp->instance_name->str)) { | |
4b39e5c9 | 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 | ||
ce141536 | 957 | if (bt_value_map_insert_entry(instance_names, |
9009cc24 | 958 | cfg_comp->instance_name->str, bt_value_null)) { |
4b39e5c9 | 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 | |
8eee8ea2 | 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()) { | |
f4f9e43b | 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) { | |
4b39e5c9 | 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 | |
8eee8ea2 | 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()) { | |
f4f9e43b | 1004 | BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary."); |
9009cc24 | 1005 | } else { |
c864e29a 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) { | |
4b39e5c9 | 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())) { | |
4b39e5c9 | 1025 | BT_CLI_LOGE_APPEND_CAUSE("Invalid system plugin path."); |
9009cc24 PP |
1026 | goto error; |
1027 | } | |
1028 | } | |
1029 | return 0; | |
1030 | error: | |
4b39e5c9 | 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, | |
d36258d0 | 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) { | |
4b39e5c9 | 1044 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
1045 | goto error; |
1046 | } | |
1047 | ||
1d7bf349 | 1048 | bt_object_init_shared(&cfg->base, bt_config_destroy); |
9009cc24 PP |
1049 | cfg->command = command; |
1050 | cfg->command_needs_plugins = needs_plugins; | |
1051 | ||
d36258d0 PP |
1052 | if (plugin_paths) { |
1053 | bt_value *plugin_paths_copy; | |
ce141536 | 1054 | |
d36258d0 PP |
1055 | (void) bt_value_copy(plugin_paths, |
1056 | &plugin_paths_copy); | |
1057 | cfg->plugin_paths = plugin_paths_copy; | |
9009cc24 | 1058 | } else { |
ce141536 | 1059 | cfg->plugin_paths = bt_value_array_create(); |
9009cc24 | 1060 | if (!cfg->plugin_paths) { |
4b39e5c9 | 1061 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
1062 | goto error; |
1063 | } | |
1064 | } | |
1065 | ||
1066 | goto end; | |
1067 | ||
1068 | error: | |
8138bfe1 | 1069 | BT_OBJECT_PUT_REF_AND_RESET(cfg); |
9009cc24 PP |
1070 | |
1071 | end: | |
1072 | return cfg; | |
1073 | } | |
1074 | ||
1075 | static | |
d36258d0 | 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, | |
d36258d0 | 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( | |
8138bfe1 | 1088 | (GDestroyNotify) bt_object_put_ref); |
9009cc24 | 1089 | if (!cfg->cmd_data.run.sources) { |
4b39e5c9 | 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( | |
8138bfe1 | 1095 | (GDestroyNotify) bt_object_put_ref); |
9009cc24 | 1096 | if (!cfg->cmd_data.run.filters) { |
4b39e5c9 | 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( | |
8138bfe1 | 1102 | (GDestroyNotify) bt_object_put_ref); |
9009cc24 | 1103 | if (!cfg->cmd_data.run.sinks) { |
4b39e5c9 | 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) { | |
4b39e5c9 | 1111 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
1112 | goto error; |
1113 | } | |
1114 | ||
1115 | goto end; | |
1116 | ||
1117 | error: | |
8138bfe1 | 1118 | BT_OBJECT_PUT_REF_AND_RESET(cfg); |
9009cc24 PP |
1119 | |
1120 | end: | |
1121 | return cfg; | |
1122 | } | |
1123 | ||
1124 | static | |
d36258d0 | 1125 | struct bt_config *bt_config_list_plugins_create(const bt_value *plugin_paths) |
9009cc24 | 1126 | { |
d36258d0 PP |
1127 | return bt_config_base_create(BT_CONFIG_COMMAND_LIST_PLUGINS, |
1128 | plugin_paths, true); | |
9009cc24 PP |
1129 | } |
1130 | ||
1131 | static | |
d36258d0 | 1132 | struct bt_config *bt_config_help_create(const bt_value *plugin_paths, |
c4f81dc9 | 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, | |
d36258d0 | 1139 | plugin_paths, true); |
9009cc24 PP |
1140 | if (!cfg) { |
1141 | goto error; | |
1142 | } | |
1143 | ||
1144 | cfg->cmd_data.help.cfg_component = | |
c4f81dc9 | 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: | |
8138bfe1 | 1153 | BT_OBJECT_PUT_REF_AND_RESET(cfg); |
9009cc24 PP |
1154 | |
1155 | end: | |
1156 | return cfg; | |
1157 | } | |
1158 | ||
1159 | static | |
d36258d0 | 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, | |
d36258d0 | 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) { | |
4b39e5c9 | 1173 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
1174 | goto error; |
1175 | } | |
1176 | ||
1177 | goto end; | |
1178 | ||
1179 | error: | |
8138bfe1 | 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( | |
d36258d0 | 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, | |
d36258d0 | 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) { | |
4b39e5c9 | 1201 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
1202 | goto error; |
1203 | } | |
1204 | ||
a107deea PP |
1205 | cfg->cmd_data.print_ctf_metadata.output_path = g_string_new(NULL); |
1206 | if (!cfg->cmd_data.print_ctf_metadata.output_path) { | |
4b39e5c9 | 1207 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
a107deea PP |
1208 | goto error; |
1209 | } | |
1210 | ||
9009cc24 PP |
1211 | goto end; |
1212 | ||
1213 | error: | |
8138bfe1 | 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( | |
d36258d0 | 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, | |
d36258d0 | 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) { | |
4b39e5c9 | 1235 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
1236 | goto error; |
1237 | } | |
1238 | ||
a107deea 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) { | |
4b39e5c9 | 1242 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
a107deea PP |
1243 | goto error; |
1244 | } | |
1245 | ||
9009cc24 PP |
1246 | goto end; |
1247 | ||
1248 | error: | |
8138bfe1 | 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( | |
8eee8ea2 | 1257 | bt_value *plugin_paths, const char *arg) |
9009cc24 PP |
1258 | { |
1259 | int ret = 0; | |
1260 | ||
1261 | if (bt_common_is_setuid_setgid()) { | |
f4f9e43b | 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)) { | |
4b39e5c9 | 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"); | |
68d9d039 | 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"); | |
7b6a2143 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"); | |
c54abe78 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"); | |
68d9d039 | 1311 | fprintf(fp, " play=+23, observe=3.14, simple=beef, needs-quotes=\"some string\",\n"); |
7b6a2143 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"); | |
142ac9b0 | 1316 | fprintf(fp, "babeltrace2 from a shell.\n"); |
9009cc24 PP |
1317 | } |
1318 | ||
9d1872e7 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 | { | |
142ac9b0 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"); | |
d36258d0 | 1356 | fprintf(fp, " -h, --help Show this help and quit\n"); |
9009cc24 | 1357 | fprintf(fp, "\n"); |
142ac9b0 | 1358 | fprintf(fp, "See `babeltrace2 --help` for the list of general options.\n"); |
9009cc24 | 1359 | fprintf(fp, "\n"); |
142ac9b0 | 1360 | fprintf(fp, "Use `babeltrace2 list-plugins` to show the list of available plugins.\n"); |
9009cc24 PP |
1361 | } |
1362 | ||
1363 | static | |
7551de33 SM |
1364 | const struct bt_argpar_opt_descr help_options[] = { |
1365 | /* id, short_name, long_name, with_arg */ | |
1366 | { OPT_HELP, 'h', "help", false }, | |
7551de33 | 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[], | |
d36258d0 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; |
b70ecec9 | 1383 | struct bt_argpar_parse_ret argpar_parse_ret = { 0 }; |
d36258d0 | 1384 | struct bt_argpar_item_non_opt *non_opt; |
9009cc24 PP |
1385 | |
1386 | *retcode = 0; | |
d36258d0 | 1387 | cfg = bt_config_help_create(plugin_paths, default_log_level); |
9009cc24 PP |
1388 | if (!cfg) { |
1389 | goto error; | |
1390 | } | |
1391 | ||
9009cc24 | 1392 | /* Parse options */ |
7551de33 SM |
1393 | argpar_parse_ret = bt_argpar_parse(argc, argv, help_options, true); |
1394 | if (argpar_parse_ret.error) { | |
1395 | BT_CLI_LOGE_APPEND_CAUSE( | |
1396 | "While parsing `help` command's command-line arguments: %s", | |
1397 | argpar_parse_ret.error->str); | |
9009cc24 PP |
1398 | goto error; |
1399 | } | |
1400 | ||
7551de33 SM |
1401 | if (help_option_is_specified(&argpar_parse_ret)) { |
1402 | print_help_usage(stdout); | |
1403 | *retcode = -1; | |
1404 | BT_OBJECT_PUT_REF_AND_RESET(cfg); | |
1405 | goto end; | |
1406 | } | |
9009cc24 | 1407 | |
d36258d0 PP |
1408 | if (argpar_parse_ret.items->len == 0) { |
1409 | BT_CLI_LOGE_APPEND_CAUSE( | |
1410 | "Missing plugin name or component class descriptor."); | |
1411 | goto error; | |
1412 | } else if (argpar_parse_ret.items->len > 1) { | |
1413 | /* | |
1414 | * At this point we know there are least two non-option | |
1415 | * arguments because we don't reach here with `--help`, | |
1416 | * the only option. | |
1417 | */ | |
1418 | non_opt = argpar_parse_ret.items->pdata[1]; | |
1419 | BT_CLI_LOGE_APPEND_CAUSE( | |
1420 | "Extraneous command-line argument specified to `help` command: `%s`.", | |
1421 | non_opt->arg); | |
1422 | goto error; | |
9009cc24 PP |
1423 | } |
1424 | ||
d36258d0 PP |
1425 | non_opt = argpar_parse_ret.items->pdata[0]; |
1426 | plugin_comp_cls_names(non_opt->arg, NULL, &plugin_name, &comp_cls_name, | |
1427 | &cfg->cmd_data.help.cfg_component->type); | |
1428 | if (plugin_name && comp_cls_name) { | |
1429 | /* Component class help */ | |
1430 | g_string_assign(cfg->cmd_data.help.cfg_component->plugin_name, | |
1431 | plugin_name); | |
1432 | g_string_assign(cfg->cmd_data.help.cfg_component->comp_cls_name, | |
1433 | comp_cls_name); | |
75a1a799 | 1434 | } else { |
d36258d0 PP |
1435 | /* Fall back to plugin help */ |
1436 | g_string_assign(cfg->cmd_data.help.cfg_component->plugin_name, | |
1437 | non_opt->arg); | |
9009cc24 PP |
1438 | } |
1439 | ||
1440 | goto end; | |
1441 | ||
1442 | error: | |
1443 | *retcode = 1; | |
8138bfe1 | 1444 | BT_OBJECT_PUT_REF_AND_RESET(cfg); |
9009cc24 PP |
1445 | |
1446 | end: | |
9009cc24 PP |
1447 | g_free(plugin_name); |
1448 | g_free(comp_cls_name); | |
1449 | ||
7551de33 | 1450 | bt_argpar_parse_ret_fini(&argpar_parse_ret); |
9009cc24 | 1451 | |
9009cc24 PP |
1452 | return cfg; |
1453 | } | |
1454 | ||
1455 | /* | |
1456 | * Prints the help command usage. | |
1457 | */ | |
1458 | static | |
1459 | void print_query_usage(FILE *fp) | |
1460 | { | |
142ac9b0 | 1461 | fprintf(fp, "Usage: babeltrace2 [GEN OPTS] query [OPTS] TYPE.PLUGIN.CLS OBJECT\n"); |
9009cc24 PP |
1462 | fprintf(fp, "\n"); |
1463 | fprintf(fp, "Options:\n"); | |
1464 | fprintf(fp, "\n"); | |
d36258d0 PP |
1465 | fprintf(fp, " -p, --params=PARAMS Set the query parameters to PARAMS (see the expected\n"); |
1466 | fprintf(fp, " format of PARAMS below)\n"); | |
1467 | fprintf(fp, " -h, --help Show this help and quit\n"); | |
9009cc24 PP |
1468 | fprintf(fp, "\n\n"); |
1469 | print_expected_params_format(fp); | |
1470 | } | |
1471 | ||
1472 | static | |
113d8912 SM |
1473 | const struct bt_argpar_opt_descr query_options[] = { |
1474 | /* id, short_name, long_name, with_arg */ | |
1475 | { OPT_HELP, 'h', "help", false }, | |
113d8912 | 1476 | { OPT_PARAMS, 'p', "params", true }, |
113d8912 | 1477 | BT_ARGPAR_OPT_DESCR_SENTINEL |
9009cc24 PP |
1478 | }; |
1479 | ||
1480 | /* | |
1481 | * Creates a Babeltrace config object from the arguments of a query | |
1482 | * command. | |
1483 | * | |
1484 | * *retcode is set to the appropriate exit code to use. | |
1485 | */ | |
1486 | static | |
1487 | struct bt_config *bt_config_query_from_args(int argc, const char *argv[], | |
d36258d0 | 1488 | int *retcode, const bt_value *plugin_paths, |
c4f81dc9 | 1489 | int default_log_level) |
9009cc24 | 1490 | { |
d36258d0 | 1491 | int i; |
9009cc24 | 1492 | struct bt_config *cfg = NULL; |
113d8912 SM |
1493 | const char *component_class_spec = NULL; |
1494 | const char *query_object = NULL; | |
13041794 | 1495 | bt_value *params; |
87c4d6ca | 1496 | GString *error_str = NULL; |
b70ecec9 | 1497 | struct bt_argpar_parse_ret argpar_parse_ret = { 0 }; |
13041794 SM |
1498 | |
1499 | params = bt_value_null; | |
1500 | bt_value_get_ref(bt_value_null); | |
9009cc24 PP |
1501 | |
1502 | *retcode = 0; | |
d36258d0 | 1503 | cfg = bt_config_query_create(plugin_paths); |
9009cc24 PP |
1504 | if (!cfg) { |
1505 | goto error; | |
1506 | } | |
1507 | ||
87c4d6ca PP |
1508 | error_str = g_string_new(NULL); |
1509 | if (!error_str) { | |
4b39e5c9 | 1510 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
87c4d6ca PP |
1511 | goto error; |
1512 | } | |
1513 | ||
9009cc24 | 1514 | /* Parse options */ |
113d8912 SM |
1515 | argpar_parse_ret = bt_argpar_parse(argc, argv, query_options, true); |
1516 | if (argpar_parse_ret.error) { | |
1517 | BT_CLI_LOGE_APPEND_CAUSE( | |
1518 | "While parsing `query` command's command-line arguments: %s", | |
1519 | argpar_parse_ret.error->str); | |
9009cc24 PP |
1520 | goto error; |
1521 | } | |
1522 | ||
113d8912 SM |
1523 | if (help_option_is_specified(&argpar_parse_ret)) { |
1524 | print_query_usage(stdout); | |
1525 | *retcode = -1; | |
1526 | BT_OBJECT_PUT_REF_AND_RESET(cfg); | |
1527 | goto end; | |
1528 | } | |
9009cc24 | 1529 | |
113d8912 SM |
1530 | for (i = 0; i < argpar_parse_ret.items->len; i++) { |
1531 | struct bt_argpar_item *argpar_item = | |
1532 | g_ptr_array_index(argpar_parse_ret.items, i); | |
9009cc24 | 1533 | |
113d8912 SM |
1534 | if (argpar_item->type == BT_ARGPAR_ITEM_TYPE_OPT) { |
1535 | struct bt_argpar_item_opt *argpar_item_opt = | |
1536 | (struct bt_argpar_item_opt *) argpar_item; | |
1537 | const char *arg = argpar_item_opt->arg; | |
1538 | ||
1539 | switch (argpar_item_opt->descr->id) { | |
113d8912 SM |
1540 | case OPT_PARAMS: |
1541 | { | |
1542 | bt_value_put_ref(params); | |
1543 | params = cli_value_from_arg(arg, error_str); | |
1544 | if (!params) { | |
1545 | BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s", | |
1546 | error_str->str); | |
1547 | goto error; | |
1548 | } | |
1549 | break; | |
1550 | } | |
1551 | default: | |
1552 | BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).", | |
1553 | argpar_item_opt->descr->id); | |
9009cc24 PP |
1554 | goto error; |
1555 | } | |
113d8912 SM |
1556 | } else { |
1557 | struct bt_argpar_item_non_opt *argpar_item_non_opt | |
1558 | = (struct bt_argpar_item_non_opt *) argpar_item; | |
1559 | ||
1560 | /* | |
8d5247bd PP |
1561 | * We need exactly two non-option arguments |
1562 | * which are the mandatory component class | |
1563 | * specification and query object. | |
113d8912 SM |
1564 | */ |
1565 | if (!component_class_spec) { | |
1566 | component_class_spec = argpar_item_non_opt->arg; | |
1567 | } else if (!query_object) { | |
1568 | query_object = argpar_item_non_opt->arg; | |
1569 | } else { | |
1570 | BT_CLI_LOGE_APPEND_CAUSE("Extraneous command-line argument specified to `query` command: `%s`.", | |
1571 | argpar_item_non_opt->arg); | |
9009cc24 PP |
1572 | goto error; |
1573 | } | |
9009cc24 | 1574 | } |
9009cc24 PP |
1575 | } |
1576 | ||
113d8912 | 1577 | if (!component_class_spec || !query_object) { |
93c1364b PP |
1578 | print_query_usage(stdout); |
1579 | *retcode = -1; | |
8138bfe1 | 1580 | BT_OBJECT_PUT_REF_AND_RESET(cfg); |
93c1364b PP |
1581 | goto end; |
1582 | } | |
1583 | ||
113d8912 SM |
1584 | cfg->cmd_data.query.cfg_component = |
1585 | bt_config_component_from_arg(component_class_spec, | |
1586 | default_log_level); | |
1587 | if (!cfg->cmd_data.query.cfg_component) { | |
1588 | BT_CLI_LOGE_APPEND_CAUSE("Invalid format for component class specification:\n %s", | |
1589 | component_class_spec); | |
1590 | goto error; | |
9009cc24 PP |
1591 | } |
1592 | ||
113d8912 SM |
1593 | BT_ASSERT(params); |
1594 | BT_OBJECT_MOVE_REF(cfg->cmd_data.query.cfg_component->params, params); | |
1595 | ||
1596 | if (strlen(query_object) == 0) { | |
1597 | BT_CLI_LOGE_APPEND_CAUSE("Invalid empty object."); | |
9009cc24 PP |
1598 | goto error; |
1599 | } | |
1600 | ||
113d8912 | 1601 | g_string_assign(cfg->cmd_data.query.object, query_object); |
9009cc24 PP |
1602 | goto end; |
1603 | ||
1604 | error: | |
1605 | *retcode = 1; | |
8138bfe1 | 1606 | BT_OBJECT_PUT_REF_AND_RESET(cfg); |
9009cc24 PP |
1607 | |
1608 | end: | |
113d8912 | 1609 | bt_argpar_parse_ret_fini(&argpar_parse_ret); |
9009cc24 | 1610 | |
87c4d6ca PP |
1611 | if (error_str) { |
1612 | g_string_free(error_str, TRUE); | |
1613 | } | |
1614 | ||
8c6884d9 | 1615 | bt_value_put_ref(params); |
9009cc24 PP |
1616 | return cfg; |
1617 | } | |
1618 | ||
1619 | /* | |
1620 | * Prints the list-plugins command usage. | |
1621 | */ | |
1622 | static | |
1623 | void print_list_plugins_usage(FILE *fp) | |
1624 | { | |
142ac9b0 | 1625 | fprintf(fp, "Usage: babeltrace2 [GENERAL OPTIONS] list-plugins [OPTIONS]\n"); |
9009cc24 PP |
1626 | fprintf(fp, "\n"); |
1627 | fprintf(fp, "Options:\n"); | |
1628 | fprintf(fp, "\n"); | |
9e503aa9 | 1629 | fprintf(fp, " -h, --help Show this help and quit\n"); |
9009cc24 | 1630 | fprintf(fp, "\n"); |
142ac9b0 | 1631 | fprintf(fp, "See `babeltrace2 --help` for the list of general options.\n"); |
9009cc24 | 1632 | fprintf(fp, "\n"); |
142ac9b0 | 1633 | fprintf(fp, "Use `babeltrace2 help` to get help for a specific plugin or component class.\n"); |
9009cc24 PP |
1634 | } |
1635 | ||
1636 | static | |
9975997a SM |
1637 | const struct bt_argpar_opt_descr list_plugins_options[] = { |
1638 | /* id, short_name, long_name, with_arg */ | |
1639 | { OPT_HELP, 'h', "help", false }, | |
9975997a | 1640 | BT_ARGPAR_OPT_DESCR_SENTINEL |
9009cc24 PP |
1641 | }; |
1642 | ||
1643 | /* | |
1644 | * Creates a Babeltrace config object from the arguments of a | |
1645 | * list-plugins command. | |
1646 | * | |
1647 | * *retcode is set to the appropriate exit code to use. | |
1648 | */ | |
1649 | static | |
1650 | struct bt_config *bt_config_list_plugins_from_args(int argc, const char *argv[], | |
d36258d0 | 1651 | int *retcode, const bt_value *plugin_paths) |
9009cc24 | 1652 | { |
9009cc24 | 1653 | struct bt_config *cfg = NULL; |
9975997a | 1654 | struct bt_argpar_parse_ret argpar_parse_ret = { 0 }; |
9009cc24 PP |
1655 | |
1656 | *retcode = 0; | |
d36258d0 | 1657 | cfg = bt_config_list_plugins_create(plugin_paths); |
9009cc24 PP |
1658 | if (!cfg) { |
1659 | goto error; | |
1660 | } | |
1661 | ||
9009cc24 | 1662 | /* Parse options */ |
9975997a SM |
1663 | argpar_parse_ret = bt_argpar_parse(argc, argv, list_plugins_options, true); |
1664 | if (argpar_parse_ret.error) { | |
1665 | BT_CLI_LOGE_APPEND_CAUSE( | |
1666 | "While parsing `list-plugins` command's command-line arguments: %s", | |
1667 | argpar_parse_ret.error->str); | |
9009cc24 PP |
1668 | goto error; |
1669 | } | |
1670 | ||
9975997a SM |
1671 | if (help_option_is_specified(&argpar_parse_ret)) { |
1672 | print_list_plugins_usage(stdout); | |
1673 | *retcode = -1; | |
1674 | BT_OBJECT_PUT_REF_AND_RESET(cfg); | |
1675 | goto end; | |
1676 | } | |
9009cc24 | 1677 | |
d36258d0 PP |
1678 | if (argpar_parse_ret.items->len > 0) { |
1679 | /* | |
1680 | * At this point we know there's at least one non-option | |
1681 | * argument because we don't reach here with `--help`, | |
1682 | * the only option. | |
1683 | */ | |
1684 | struct bt_argpar_item_non_opt *non_opt = | |
1685 | argpar_parse_ret.items->pdata[0]; | |
9009cc24 | 1686 | |
d36258d0 PP |
1687 | BT_CLI_LOGE_APPEND_CAUSE( |
1688 | "Extraneous command-line argument specified to `list-plugins` command: `%s`.", | |
1689 | non_opt->arg); | |
9009cc24 PP |
1690 | goto error; |
1691 | } | |
1692 | ||
1693 | goto end; | |
1694 | ||
1695 | error: | |
1696 | *retcode = 1; | |
8138bfe1 | 1697 | BT_OBJECT_PUT_REF_AND_RESET(cfg); |
9009cc24 PP |
1698 | |
1699 | end: | |
9975997a | 1700 | bt_argpar_parse_ret_fini(&argpar_parse_ret); |
9009cc24 | 1701 | |
9009cc24 PP |
1702 | return cfg; |
1703 | } | |
1704 | ||
1705 | /* | |
1706 | * Prints the run command usage. | |
1707 | */ | |
1708 | static | |
1709 | void print_run_usage(FILE *fp) | |
1710 | { | |
142ac9b0 | 1711 | fprintf(fp, "Usage: babeltrace2 [GENERAL OPTIONS] run [OPTIONS]\n"); |
9009cc24 PP |
1712 | fprintf(fp, "\n"); |
1713 | fprintf(fp, "Options:\n"); | |
1714 | fprintf(fp, "\n"); | |
1715 | fprintf(fp, " -b, --base-params=PARAMS Set PARAMS as the current base parameters\n"); | |
1716 | fprintf(fp, " for all the following components until\n"); | |
1717 | fprintf(fp, " --reset-base-params is encountered\n"); | |
1718 | fprintf(fp, " (see the expected format of PARAMS below)\n"); | |
4186b466 | 1719 | fprintf(fp, " -c, --component=NAME:TYPE.PLUGIN.CLS\n"); |
fd5f8053 PP |
1720 | fprintf(fp, " Instantiate the component class CLS of type\n"); |
1721 | fprintf(fp, " TYPE (`source`, `filter`, or `sink`) found\n"); | |
1722 | fprintf(fp, " in the plugin PLUGIN, add it to the graph,\n"); | |
4186b466 | 1723 | fprintf(fp, " and name it NAME"); |
d5128b09 | 1724 | fprintf(fp, " -x, --connect=CONNECTION Connect two created components (see the\n"); |
9009cc24 | 1725 | fprintf(fp, " expected format of CONNECTION below)\n"); |
c4f81dc9 | 1726 | fprintf(fp, " -l, --log-level=LVL Set the log level of the current component to LVL\n"); |
983c486e | 1727 | fprintf(fp, " (`N`, `T`, `D`, `I`, `W`, `E`, or `F`)\n"); |
9009cc24 PP |
1728 | fprintf(fp, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n"); |
1729 | fprintf(fp, " current component (see the expected format\n"); | |
1730 | fprintf(fp, " of PARAMS below)\n"); | |
9009cc24 PP |
1731 | fprintf(fp, " -r, --reset-base-params Reset the current base parameters to an\n"); |
1732 | fprintf(fp, " empty map\n"); | |
142ac9b0 | 1733 | fprintf(fp, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n"); |
9009cc24 PP |
1734 | fprintf(fp, " the graph later, retry in DUR µs\n"); |
1735 | fprintf(fp, " (default: 100000)\n"); | |
9e503aa9 | 1736 | fprintf(fp, " -h, --help Show this help and quit\n"); |
9009cc24 | 1737 | fprintf(fp, "\n"); |
142ac9b0 | 1738 | fprintf(fp, "See `babeltrace2 --help` for the list of general options.\n"); |
9009cc24 PP |
1739 | fprintf(fp, "\n\n"); |
1740 | fprintf(fp, "Expected format of CONNECTION\n"); | |
1741 | fprintf(fp, "-----------------------------\n"); | |
1742 | fprintf(fp, "\n"); | |
1743 | fprintf(fp, " UPSTREAM[.UPSTREAM-PORT]:DOWNSTREAM[.DOWNSTREAM-PORT]\n"); | |
1744 | fprintf(fp, "\n"); | |
1745 | fprintf(fp, "UPSTREAM and DOWNSTREAM are names of the upstream and downstream\n"); | |
1746 | fprintf(fp, "components to connect together. You must escape the following characters\n\n"); | |
4186b466 SM |
1747 | fprintf(fp, "with `\\`: `\\`, `.`, and `:`. You must set the name of the current\n"); |
1748 | fprintf(fp, "component using the NAME prefix of the --component option.\n"); | |
9009cc24 PP |
1749 | fprintf(fp, "\n"); |
1750 | fprintf(fp, "UPSTREAM-PORT and DOWNSTREAM-PORT are optional globbing patterns to\n"); | |
1751 | fprintf(fp, "identify the upstream and downstream ports to use for the connection.\n"); | |
1752 | fprintf(fp, "When the port is not specified, `*` is used.\n"); | |
1753 | fprintf(fp, "\n"); | |
1754 | fprintf(fp, "When a component named UPSTREAM has an available port which matches the\n"); | |
1755 | fprintf(fp, "UPSTREAM-PORT globbing pattern, it is connected to the first port which\n"); | |
1756 | fprintf(fp, "matches the DOWNSTREAM-PORT globbing pattern of the component named\n"); | |
1757 | fprintf(fp, "DOWNSTREAM.\n"); | |
1758 | fprintf(fp, "\n"); | |
1759 | fprintf(fp, "The only special character in UPSTREAM-PORT and DOWNSTREAM-PORT is `*`\n"); | |
1760 | fprintf(fp, "which matches anything. You must escape the following characters\n"); | |
1761 | fprintf(fp, "with `\\`: `\\`, `*`, `?`, `[`, `.`, and `:`.\n"); | |
1762 | fprintf(fp, "\n"); | |
1763 | fprintf(fp, "You can connect a source component to a filter or sink component. You\n"); | |
1764 | fprintf(fp, "can connect a filter component to a sink component.\n"); | |
1765 | fprintf(fp, "\n"); | |
1766 | fprintf(fp, "Examples:\n"); | |
1767 | fprintf(fp, "\n"); | |
1768 | fprintf(fp, " my-src:my-sink\n"); | |
1769 | fprintf(fp, " ctf-fs.*stream*:utils-muxer:*\n"); | |
1770 | fprintf(fp, "\n"); | |
1771 | fprintf(fp, "IMPORTANT: Make sure to single-quote the whole argument when you run\n"); | |
142ac9b0 | 1772 | fprintf(fp, "babeltrace2 from a shell.\n"); |
9009cc24 PP |
1773 | fprintf(fp, "\n\n"); |
1774 | print_expected_params_format(fp); | |
1775 | } | |
1776 | ||
1777 | /* | |
1778 | * Creates a Babeltrace config object from the arguments of a run | |
1779 | * command. | |
1780 | * | |
1781 | * *retcode is set to the appropriate exit code to use. | |
1782 | */ | |
1783 | static | |
1784 | struct bt_config *bt_config_run_from_args(int argc, const char *argv[], | |
d36258d0 PP |
1785 | int *retcode, const bt_value *plugin_paths, |
1786 | int default_log_level) | |
9009cc24 | 1787 | { |
9009cc24 | 1788 | struct bt_config_component *cur_cfg_comp = NULL; |
8eee8ea2 | 1789 | bt_value *cur_base_params = NULL; |
9d1872e7 | 1790 | int ret = 0; |
9009cc24 | 1791 | struct bt_config *cfg = NULL; |
8eee8ea2 PP |
1792 | bt_value *instance_names = NULL; |
1793 | bt_value *connection_args = NULL; | |
9009cc24 | 1794 | char error_buf[256] = { 0 }; |
738302b8 | 1795 | long retry_duration = -1; |
fb25b9e3 | 1796 | bt_value_map_extend_status extend_status; |
87c4d6ca | 1797 | GString *error_str = NULL; |
9d1872e7 SM |
1798 | struct bt_argpar_parse_ret argpar_parse_ret = { 0 }; |
1799 | int i; | |
1800 | ||
1801 | static const struct bt_argpar_opt_descr run_options[] = { | |
1802 | { OPT_BASE_PARAMS, 'b', "base-params", true }, | |
1803 | { OPT_COMPONENT, 'c', "component", true }, | |
1804 | { OPT_CONNECT, 'x', "connect", true }, | |
1805 | { OPT_HELP, 'h', "help", false }, | |
1806 | { OPT_LOG_LEVEL, 'l', "log-level", true }, | |
9d1872e7 | 1807 | { OPT_PARAMS, 'p', "params", true }, |
9d1872e7 SM |
1808 | { OPT_RESET_BASE_PARAMS, 'r', "reset-base-params", false }, |
1809 | { OPT_RETRY_DURATION, '\0', "retry-duration", true }, | |
1810 | BT_ARGPAR_OPT_DESCR_SENTINEL | |
9009cc24 PP |
1811 | }; |
1812 | ||
1813 | *retcode = 0; | |
9009cc24 | 1814 | |
87c4d6ca PP |
1815 | error_str = g_string_new(NULL); |
1816 | if (!error_str) { | |
4b39e5c9 | 1817 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
87c4d6ca PP |
1818 | goto error; |
1819 | } | |
1820 | ||
091cc3eb | 1821 | if (argc < 1) { |
9009cc24 PP |
1822 | print_run_usage(stdout); |
1823 | *retcode = -1; | |
1824 | goto end; | |
1825 | } | |
1826 | ||
d36258d0 | 1827 | cfg = bt_config_run_create(plugin_paths); |
9009cc24 PP |
1828 | if (!cfg) { |
1829 | goto error; | |
1830 | } | |
1831 | ||
1832 | cfg->cmd_data.run.retry_duration_us = 100000; | |
ce141536 | 1833 | cur_base_params = bt_value_map_create(); |
9009cc24 | 1834 | if (!cur_base_params) { |
4b39e5c9 | 1835 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
1836 | goto error; |
1837 | } | |
1838 | ||
ce141536 | 1839 | instance_names = bt_value_map_create(); |
9009cc24 | 1840 | if (!instance_names) { |
4b39e5c9 | 1841 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
1842 | goto error; |
1843 | } | |
1844 | ||
ce141536 | 1845 | connection_args = bt_value_array_create(); |
9009cc24 | 1846 | if (!connection_args) { |
4b39e5c9 | 1847 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
1848 | goto error; |
1849 | } | |
1850 | ||
9009cc24 | 1851 | /* Parse options */ |
9d1872e7 SM |
1852 | argpar_parse_ret = bt_argpar_parse(argc, argv, run_options, true); |
1853 | if (argpar_parse_ret.error) { | |
1854 | BT_CLI_LOGE_APPEND_CAUSE( | |
1855 | "While parsing `run` command's command-line arguments: %s", | |
1856 | argpar_parse_ret.error->str); | |
9009cc24 PP |
1857 | goto error; |
1858 | } | |
1859 | ||
9d1872e7 SM |
1860 | if (help_option_is_specified(&argpar_parse_ret)) { |
1861 | print_run_usage(stdout); | |
1862 | *retcode = -1; | |
1863 | BT_OBJECT_PUT_REF_AND_RESET(cfg); | |
1864 | goto end; | |
1865 | } | |
9009cc24 | 1866 | |
9d1872e7 SM |
1867 | for (i = 0; i < argpar_parse_ret.items->len; i++) { |
1868 | struct bt_argpar_item *argpar_item = | |
1869 | g_ptr_array_index(argpar_parse_ret.items, i); | |
1870 | struct bt_argpar_item_opt *argpar_item_opt; | |
1871 | const char *arg; | |
9009cc24 | 1872 | |
8d5247bd | 1873 | /* This command does not accept non-option arguments.*/ |
9d1872e7 SM |
1874 | if (argpar_item->type == BT_ARGPAR_ITEM_TYPE_NON_OPT) { |
1875 | struct bt_argpar_item_non_opt *argpar_nonopt_item = | |
1876 | (struct bt_argpar_item_non_opt *) argpar_item; | |
1877 | ||
1878 | BT_CLI_LOGE_APPEND_CAUSE("Unexpected argument: `%s`", | |
1879 | argpar_nonopt_item->arg); | |
1880 | goto error; | |
1881 | } | |
1882 | ||
1883 | argpar_item_opt = (struct bt_argpar_item_opt *) argpar_item; | |
1884 | arg = argpar_item_opt->arg; | |
1885 | ||
1886 | switch (argpar_item_opt->descr->id) { | |
fd5f8053 | 1887 | case OPT_COMPONENT: |
9009cc24 | 1888 | { |
4186b466 | 1889 | enum bt_config_component_dest dest; |
9009cc24 | 1890 | |
4186b466 | 1891 | BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp); |
c4f81dc9 PP |
1892 | cur_cfg_comp = bt_config_component_from_arg(arg, |
1893 | default_log_level); | |
9009cc24 | 1894 | if (!cur_cfg_comp) { |
4b39e5c9 | 1895 | BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --component option's argument:\n %s", |
fd5f8053 | 1896 | arg); |
9009cc24 PP |
1897 | goto error; |
1898 | } | |
1899 | ||
fd5f8053 PP |
1900 | switch (cur_cfg_comp->type) { |
1901 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
4186b466 | 1902 | dest = BT_CONFIG_COMPONENT_DEST_SOURCE; |
fd5f8053 PP |
1903 | break; |
1904 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
4186b466 | 1905 | dest = BT_CONFIG_COMPONENT_DEST_FILTER; |
fd5f8053 PP |
1906 | break; |
1907 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
4186b466 | 1908 | dest = BT_CONFIG_COMPONENT_DEST_SINK; |
fd5f8053 PP |
1909 | break; |
1910 | default: | |
1911 | abort(); | |
1912 | } | |
1913 | ||
8b45963b | 1914 | BT_ASSERT(cur_base_params); |
8c6884d9 | 1915 | bt_value_put_ref(cur_cfg_comp->params); |
fb25b9e3 PP |
1916 | if (bt_value_copy(cur_base_params, |
1917 | &cur_cfg_comp->params) < 0) { | |
4b39e5c9 | 1918 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
1919 | goto error; |
1920 | } | |
1921 | ||
4186b466 SM |
1922 | ret = add_run_cfg_comp_check_name(cfg, |
1923 | cur_cfg_comp, dest, | |
1924 | instance_names); | |
1925 | if (ret) { | |
1926 | goto error; | |
1927 | } | |
1928 | ||
9009cc24 PP |
1929 | break; |
1930 | } | |
1931 | case OPT_PARAMS: | |
1932 | { | |
8eee8ea2 PP |
1933 | bt_value *params; |
1934 | bt_value *params_to_set; | |
9009cc24 PP |
1935 | |
1936 | if (!cur_cfg_comp) { | |
4b39e5c9 | 1937 | BT_CLI_LOGE_APPEND_CAUSE("Cannot add parameters to unavailable component:\n %s", |
9009cc24 PP |
1938 | arg); |
1939 | goto error; | |
1940 | } | |
1941 | ||
87c4d6ca | 1942 | params = cli_value_from_arg(arg, error_str); |
9009cc24 | 1943 | if (!params) { |
4b39e5c9 | 1944 | BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s", |
87c4d6ca | 1945 | error_str->str); |
9009cc24 PP |
1946 | goto error; |
1947 | } | |
1948 | ||
fb25b9e3 PP |
1949 | extend_status = bt_value_map_extend( |
1950 | cur_cfg_comp->params, params, ¶ms_to_set); | |
8c6884d9 | 1951 | BT_VALUE_PUT_REF_AND_RESET(params); |
fb25b9e3 | 1952 | if (extend_status != BT_VALUE_MAP_EXTEND_STATUS_OK) { |
4b39e5c9 | 1953 | BT_CLI_LOGE_APPEND_CAUSE("Cannot extend current component parameters with --params option's argument:\n %s", |
9009cc24 PP |
1954 | arg); |
1955 | goto error; | |
1956 | } | |
1957 | ||
8138bfe1 | 1958 | BT_OBJECT_MOVE_REF(cur_cfg_comp->params, params_to_set); |
9009cc24 PP |
1959 | break; |
1960 | } | |
c4f81dc9 PP |
1961 | case OPT_LOG_LEVEL: |
1962 | if (!cur_cfg_comp) { | |
4b39e5c9 | 1963 | BT_CLI_LOGE_APPEND_CAUSE("Cannot set the log level of unavailable component:\n %s", |
c4f81dc9 PP |
1964 | arg); |
1965 | goto error; | |
1966 | } | |
1967 | ||
1968 | cur_cfg_comp->log_level = | |
1969 | bt_log_get_level_from_string(arg); | |
1970 | if (cur_cfg_comp->log_level < 0) { | |
4b39e5c9 | 1971 | BT_CLI_LOGE_APPEND_CAUSE("Invalid argument for --log-level option:\n %s", |
c4f81dc9 PP |
1972 | arg); |
1973 | goto error; | |
1974 | } | |
1975 | break; | |
9009cc24 PP |
1976 | case OPT_BASE_PARAMS: |
1977 | { | |
87c4d6ca | 1978 | bt_value *params = cli_value_from_arg(arg, error_str); |
9009cc24 PP |
1979 | |
1980 | if (!params) { | |
4b39e5c9 | 1981 | BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --base-params option's argument:\n %s", |
87c4d6ca | 1982 | error_str->str); |
9009cc24 PP |
1983 | goto error; |
1984 | } | |
1985 | ||
8138bfe1 | 1986 | BT_OBJECT_MOVE_REF(cur_base_params, params); |
9009cc24 PP |
1987 | break; |
1988 | } | |
1989 | case OPT_RESET_BASE_PARAMS: | |
8c6884d9 | 1990 | BT_VALUE_PUT_REF_AND_RESET(cur_base_params); |
ce141536 | 1991 | cur_base_params = bt_value_map_create(); |
9009cc24 | 1992 | if (!cur_base_params) { |
4b39e5c9 | 1993 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
1994 | goto error; |
1995 | } | |
1996 | break; | |
1997 | case OPT_CONNECT: | |
ce141536 | 1998 | if (bt_value_array_append_string_element( |
44514773 | 1999 | connection_args, arg)) { |
4b39e5c9 | 2000 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
2001 | goto error; |
2002 | } | |
2003 | break; | |
9d1872e7 SM |
2004 | case OPT_RETRY_DURATION: { |
2005 | gchar *end; | |
2006 | size_t arg_len = strlen(argpar_item_opt->arg); | |
2007 | ||
2008 | retry_duration = g_ascii_strtoll(argpar_item_opt->arg, &end, 10); | |
2009 | ||
2010 | if (arg_len == 0 || end != (argpar_item_opt->arg + arg_len)) { | |
2011 | BT_CLI_LOGE_APPEND_CAUSE( | |
2012 | "Could not parse --retry-duration option's argument as an unsigned integer: `%s`", | |
2013 | argpar_item_opt->arg); | |
2014 | goto error; | |
2015 | } | |
2016 | ||
9009cc24 | 2017 | if (retry_duration < 0) { |
4b39e5c9 | 2018 | BT_CLI_LOGE_APPEND_CAUSE("--retry-duration option's argument must be positive or 0: %ld", |
9009cc24 PP |
2019 | retry_duration); |
2020 | goto error; | |
2021 | } | |
2022 | ||
2023 | cfg->cmd_data.run.retry_duration_us = | |
2024 | (uint64_t) retry_duration; | |
2025 | break; | |
9d1872e7 | 2026 | } |
9009cc24 | 2027 | default: |
4b39e5c9 | 2028 | BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).", |
9d1872e7 | 2029 | argpar_item_opt->descr->id); |
9009cc24 PP |
2030 | goto error; |
2031 | } | |
9009cc24 PP |
2032 | } |
2033 | ||
4186b466 | 2034 | BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp); |
9009cc24 PP |
2035 | |
2036 | if (cfg->cmd_data.run.sources->len == 0) { | |
4b39e5c9 | 2037 | BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no source component."); |
9009cc24 PP |
2038 | goto error; |
2039 | } | |
2040 | ||
2041 | if (cfg->cmd_data.run.sinks->len == 0) { | |
4b39e5c9 | 2042 | BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no sink component."); |
9009cc24 PP |
2043 | goto error; |
2044 | } | |
2045 | ||
17582c6d | 2046 | ret = bt_config_cli_args_create_connections(cfg, |
ce141536 | 2047 | connection_args, |
9009cc24 PP |
2048 | error_buf, 256); |
2049 | if (ret) { | |
4b39e5c9 | 2050 | BT_CLI_LOGE_APPEND_CAUSE("Cannot creation connections:\n%s", error_buf); |
9009cc24 PP |
2051 | goto error; |
2052 | } | |
2053 | ||
2054 | goto end; | |
2055 | ||
2056 | error: | |
2057 | *retcode = 1; | |
8138bfe1 | 2058 | BT_OBJECT_PUT_REF_AND_RESET(cfg); |
9009cc24 PP |
2059 | |
2060 | end: | |
87c4d6ca PP |
2061 | if (error_str) { |
2062 | g_string_free(error_str, TRUE); | |
2063 | } | |
2064 | ||
9d1872e7 | 2065 | bt_argpar_parse_ret_fini(&argpar_parse_ret); |
8138bfe1 | 2066 | BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp); |
8c6884d9 PP |
2067 | BT_VALUE_PUT_REF_AND_RESET(cur_base_params); |
2068 | BT_VALUE_PUT_REF_AND_RESET(instance_names); | |
2069 | BT_VALUE_PUT_REF_AND_RESET(connection_args); | |
9009cc24 PP |
2070 | return cfg; |
2071 | } | |
2072 | ||
2073 | static | |
8eee8ea2 | 2074 | struct bt_config *bt_config_run_from_args_array(const bt_value *run_args, |
d36258d0 PP |
2075 | int *retcode, const bt_value *plugin_paths, |
2076 | int default_log_level) | |
9009cc24 PP |
2077 | { |
2078 | struct bt_config *cfg = NULL; | |
2079 | const char **argv; | |
0d3e053b | 2080 | int64_t i, len; |
1270d0e9 | 2081 | const size_t argc = bt_value_array_get_length(run_args); |
9009cc24 PP |
2082 | |
2083 | argv = calloc(argc, sizeof(*argv)); | |
2084 | if (!argv) { | |
4b39e5c9 | 2085 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
2086 | goto end; |
2087 | } | |
2088 | ||
1270d0e9 | 2089 | len = bt_value_array_get_length(run_args); |
0d3e053b | 2090 | if (len < 0) { |
4b39e5c9 | 2091 | BT_CLI_LOGE_APPEND_CAUSE("Invalid executable arguments."); |
0d3e053b MD |
2092 | goto end; |
2093 | } | |
2094 | for (i = 0; i < len; i++) { | |
8eee8ea2 | 2095 | const bt_value *arg_value = |
ce141536 PP |
2096 | bt_value_array_borrow_element_by_index_const(run_args, |
2097 | i); | |
9009cc24 PP |
2098 | const char *arg; |
2099 | ||
8b45963b | 2100 | BT_ASSERT(arg_value); |
b5cdc106 | 2101 | arg = bt_value_string_get(arg_value); |
8b45963b | 2102 | BT_ASSERT(arg); |
091cc3eb | 2103 | argv[i] = arg; |
9009cc24 PP |
2104 | } |
2105 | ||
2106 | cfg = bt_config_run_from_args(argc, argv, retcode, | |
d36258d0 | 2107 | plugin_paths, default_log_level); |
9009cc24 PP |
2108 | |
2109 | end: | |
2110 | free(argv); | |
2111 | return cfg; | |
2112 | } | |
2113 | ||
2114 | /* | |
2115 | * Prints the convert command usage. | |
2116 | */ | |
2117 | static | |
2118 | void print_convert_usage(FILE *fp) | |
2119 | { | |
142ac9b0 | 2120 | fprintf(fp, "Usage: babeltrace2 [GENERAL OPTIONS] [convert] [OPTIONS] [PATH/URL]\n"); |
9009cc24 PP |
2121 | fprintf(fp, "\n"); |
2122 | fprintf(fp, "Options:\n"); | |
2123 | fprintf(fp, "\n"); | |
fd5f8053 PP |
2124 | fprintf(fp, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n"); |
2125 | fprintf(fp, " Instantiate the component class CLS of type\n"); | |
2126 | fprintf(fp, " TYPE (`source`, `filter`, or `sink`) found\n"); | |
2127 | fprintf(fp, " in the plugin PLUGIN, add it to the\n"); | |
2128 | fprintf(fp, " conversion graph, and optionally name it\n"); | |
4186b466 | 2129 | fprintf(fp, " NAME\n"); |
c4f81dc9 | 2130 | fprintf(fp, " -l, --log-level=LVL Set the log level of the current component to LVL\n"); |
983c486e | 2131 | fprintf(fp, " (`N`, `T`, `D`, `I`, `W`, `E`, or `F`)\n"); |
9009cc24 PP |
2132 | fprintf(fp, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n"); |
2133 | fprintf(fp, " current component (see the expected format\n"); | |
2134 | fprintf(fp, " of PARAMS below)\n"); | |
142ac9b0 | 2135 | fprintf(fp, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n"); |
9009cc24 PP |
2136 | fprintf(fp, " the graph later, retry in DUR µs\n"); |
2137 | fprintf(fp, " (default: 100000)\n"); | |
2138 | fprintf(fp, " dynamic plugins can be loaded\n"); | |
2139 | fprintf(fp, " --run-args Print the equivalent arguments for the\n"); | |
2140 | fprintf(fp, " `run` command to the standard output,\n"); | |
2141 | fprintf(fp, " formatted for a shell, and quit\n"); | |
2142 | fprintf(fp, " --run-args-0 Print the equivalent arguments for the\n"); | |
2143 | fprintf(fp, " `run` command to the standard output,\n"); | |
2144 | fprintf(fp, " formatted for `xargs -0`, and quit\n"); | |
8320cc93 PP |
2145 | fprintf(fp, " --stream-intersection Only process events when all streams\n"); |
2146 | fprintf(fp, " are active\n"); | |
9e503aa9 | 2147 | fprintf(fp, " -h, --help Show this help and quit\n"); |
9009cc24 | 2148 | fprintf(fp, "\n"); |
e7ad156c | 2149 | fprintf(fp, "Implicit `source.ctf.fs` component options:\n"); |
9009cc24 PP |
2150 | fprintf(fp, "\n"); |
2151 | fprintf(fp, " --clock-offset=SEC Set clock offset to SEC seconds\n"); | |
2152 | fprintf(fp, " --clock-offset-ns=NS Set clock offset to NS ns\n"); | |
9009cc24 | 2153 | fprintf(fp, "\n"); |
e7ad156c | 2154 | fprintf(fp, "Implicit `sink.text.pretty` component options:\n"); |
9009cc24 PP |
2155 | fprintf(fp, "\n"); |
2156 | fprintf(fp, " --clock-cycles Print timestamps in clock cycles\n"); | |
2157 | fprintf(fp, " --clock-date Print timestamp dates\n"); | |
2158 | fprintf(fp, " --clock-gmt Print and parse timestamps in the GMT\n"); | |
2159 | fprintf(fp, " time zone instead of the local time zone\n"); | |
2160 | fprintf(fp, " --clock-seconds Print the timestamps as `SEC.NS` instead\n"); | |
2161 | fprintf(fp, " of `hh:mm:ss.nnnnnnnnn`\n"); | |
2162 | fprintf(fp, " --color=(never | auto | always)\n"); | |
2163 | fprintf(fp, " Never, automatically, or always emit\n"); | |
2164 | fprintf(fp, " console color codes\n"); | |
2165 | fprintf(fp, " -f, --fields=FIELD[,FIELD]... Print additional fields; FIELD can be:\n"); | |
2166 | fprintf(fp, " `all`, `trace`, `trace:hostname`,\n"); | |
2167 | fprintf(fp, " `trace:domain`, `trace:procname`,\n"); | |
2168 | fprintf(fp, " `trace:vpid`, `loglevel`, `emf`\n"); | |
2169 | fprintf(fp, " -n, --names=NAME[,NAME]... Print field names; NAME can be:\n"); | |
2170 | fprintf(fp, " `payload` (or `arg` or `args`), `none`,\n"); | |
2171 | fprintf(fp, " `all`, `scope`, `header`, `context`\n"); | |
2172 | fprintf(fp, " (or `ctx`)\n"); | |
2173 | fprintf(fp, " --no-delta Do not print time delta between\n"); | |
2174 | fprintf(fp, " consecutive events\n"); | |
2175 | fprintf(fp, " -w, --output=PATH Write output text to PATH instead of\n"); | |
2176 | fprintf(fp, " the standard output\n"); | |
2177 | fprintf(fp, "\n"); | |
e7ad156c | 2178 | fprintf(fp, "Implicit `filter.utils.muxer` component options:\n"); |
9009cc24 PP |
2179 | fprintf(fp, "\n"); |
2180 | fprintf(fp, " --clock-force-correlate Assume that clocks are inherently\n"); | |
2181 | fprintf(fp, " correlated across traces\n"); | |
2182 | fprintf(fp, "\n"); | |
e7ad156c | 2183 | fprintf(fp, "Implicit `filter.utils.trimmer` component options:\n"); |
9009cc24 PP |
2184 | fprintf(fp, "\n"); |
2185 | fprintf(fp, " -b, --begin=BEGIN Set the beginning time of the conversion\n"); | |
2186 | fprintf(fp, " time range to BEGIN (see the format of\n"); | |
2187 | fprintf(fp, " BEGIN below)\n"); | |
2188 | fprintf(fp, " -e, --end=END Set the end time of the conversion time\n"); | |
2189 | fprintf(fp, " range to END (see the format of END below)\n"); | |
2190 | fprintf(fp, " -t, --timerange=TIMERANGE Set conversion time range to TIMERANGE:\n"); | |
2191 | fprintf(fp, " BEGIN,END or [BEGIN,END] (literally `[` and\n"); | |
2192 | fprintf(fp, " `]`) (see the format of BEGIN/END below)\n"); | |
2193 | fprintf(fp, "\n"); | |
e7ad156c | 2194 | fprintf(fp, "Implicit `filter.lttng-utils.debug-info` component options:\n"); |
9009cc24 | 2195 | fprintf(fp, "\n"); |
8e765000 PP |
2196 | fprintf(fp, " --debug-info Create an implicit\n"); |
2197 | fprintf(fp, " `filter.lttng-utils.debug-info` component\n"); | |
9009cc24 PP |
2198 | fprintf(fp, " --debug-info-dir=DIR Search for debug info in directory DIR\n"); |
2199 | fprintf(fp, " instead of `/usr/lib/debug`\n"); | |
2200 | fprintf(fp, " --debug-info-full-path Show full debug info source and\n"); | |
2201 | fprintf(fp, " binary paths instead of just names\n"); | |
2202 | fprintf(fp, " --debug-info-target-prefix=DIR\n"); | |
2203 | fprintf(fp, " Use directory DIR as a prefix when\n"); | |
2204 | fprintf(fp, " looking up executables during debug\n"); | |
2205 | fprintf(fp, " info analysis\n"); | |
9009cc24 PP |
2206 | fprintf(fp, "\n"); |
2207 | fprintf(fp, "Legacy options that still work:\n"); | |
2208 | fprintf(fp, "\n"); | |
2209 | fprintf(fp, " -i, --input-format=(ctf | lttng-live)\n"); | |
2210 | fprintf(fp, " `ctf`:\n"); | |
fd5f8053 | 2211 | fprintf(fp, " Create an implicit `source.ctf.fs`\n"); |
9009cc24 PP |
2212 | fprintf(fp, " component\n"); |
2213 | fprintf(fp, " `lttng-live`:\n"); | |
fd5f8053 PP |
2214 | fprintf(fp, " Create an implicit `source.ctf.lttng-live`\n"); |
2215 | fprintf(fp, " component\n"); | |
e7ad156c | 2216 | fprintf(fp, " -o, --output-format=(text | ctf | dummy | ctf-metadata)\n"); |
9009cc24 | 2217 | fprintf(fp, " `text`:\n"); |
fd5f8053 | 2218 | fprintf(fp, " Create an implicit `sink.text.pretty`\n"); |
9009cc24 | 2219 | fprintf(fp, " component\n"); |
8f4f40e7 | 2220 | fprintf(fp, " `ctf`:\n"); |
e7ad156c PP |
2221 | fprintf(fp, " Create an implicit `sink.ctf.fs`\n"); |
2222 | fprintf(fp, " component\n"); | |
9009cc24 | 2223 | fprintf(fp, " `dummy`:\n"); |
fd5f8053 | 2224 | fprintf(fp, " Create an implicit `sink.utils.dummy`\n"); |
9009cc24 PP |
2225 | fprintf(fp, " component\n"); |
2226 | fprintf(fp, " `ctf-metadata`:\n"); | |
fd5f8053 PP |
2227 | fprintf(fp, " Query the `source.ctf.fs` component class\n"); |
2228 | fprintf(fp, " for metadata text and quit\n"); | |
9009cc24 | 2229 | fprintf(fp, "\n"); |
142ac9b0 | 2230 | fprintf(fp, "See `babeltrace2 --help` for the list of general options.\n"); |
9009cc24 PP |
2231 | fprintf(fp, "\n\n"); |
2232 | fprintf(fp, "Format of BEGIN and END\n"); | |
2233 | fprintf(fp, "-----------------------\n"); | |
2234 | fprintf(fp, "\n"); | |
2235 | fprintf(fp, " [YYYY-MM-DD [hh:mm:]]ss[.nnnnnnnnn]\n"); | |
2236 | fprintf(fp, "\n\n"); | |
2237 | print_expected_params_format(fp); | |
2238 | } | |
2239 | ||
2240 | static | |
a87d2977 SM |
2241 | const struct bt_argpar_opt_descr convert_options[] = { |
2242 | /* id, short_name, long_name, with_arg */ | |
2243 | { OPT_BEGIN, 'b', "begin", true }, | |
2244 | { OPT_CLOCK_CYCLES, '\0', "clock-cycles", false }, | |
2245 | { OPT_CLOCK_DATE, '\0', "clock-date", false }, | |
2246 | { OPT_CLOCK_FORCE_CORRELATE, '\0', "clock-force-correlate", false }, | |
2247 | { OPT_CLOCK_GMT, '\0', "clock-gmt", false }, | |
2248 | { OPT_CLOCK_OFFSET, '\0', "clock-offset", true }, | |
2249 | { OPT_CLOCK_OFFSET_NS, '\0', "clock-offset-ns", true }, | |
2250 | { OPT_CLOCK_SECONDS, '\0', "clock-seconds", false }, | |
2251 | { OPT_COLOR, '\0', "color", true }, | |
2252 | { OPT_COMPONENT, 'c', "component", true }, | |
2253 | { OPT_DEBUG, 'd', "debug", false }, | |
2254 | { OPT_DEBUG_INFO_DIR, '\0', "debug-info-dir", true }, | |
2255 | { OPT_DEBUG_INFO_FULL_PATH, '\0', "debug-info-full-path", false }, | |
2256 | { OPT_DEBUG_INFO_TARGET_PREFIX, '\0', "debug-info-target-prefix", true }, | |
2257 | { OPT_END, 'e', "end", true }, | |
2258 | { OPT_FIELDS, 'f', "fields", true }, | |
2259 | { OPT_HELP, 'h', "help", false }, | |
2260 | { OPT_INPUT_FORMAT, 'i', "input-format", true }, | |
2261 | { OPT_LOG_LEVEL, 'l', "log-level", true }, | |
a87d2977 SM |
2262 | { OPT_NAMES, 'n', "names", true }, |
2263 | { OPT_DEBUG_INFO, '\0', "debug-info", false }, | |
2264 | { OPT_NO_DELTA, '\0', "no-delta", false }, | |
2265 | { OPT_OMIT_HOME_PLUGIN_PATH, '\0', "omit-home-plugin-path", false }, | |
2266 | { OPT_OMIT_SYSTEM_PLUGIN_PATH, '\0', "omit-system-plugin-path", false }, | |
2267 | { OPT_OUTPUT, 'w', "output", true }, | |
2268 | { OPT_OUTPUT_FORMAT, 'o', "output-format", true }, | |
2269 | { OPT_PARAMS, 'p', "params", true }, | |
a87d2977 SM |
2270 | { OPT_PLUGIN_PATH, '\0', "plugin-path", true }, |
2271 | { OPT_RETRY_DURATION, '\0', "retry-duration", true }, | |
2272 | { OPT_RUN_ARGS, '\0', "run-args", false }, | |
2273 | { OPT_RUN_ARGS_0, '\0', "run-args-0", false }, | |
2274 | { OPT_STREAM_INTERSECTION, '\0', "stream-intersection", false }, | |
2275 | { OPT_TIMERANGE, '\0', "timerange", true }, | |
a87d2977 SM |
2276 | { OPT_VERBOSE, 'v', "verbose", false }, |
2277 | BT_ARGPAR_OPT_DESCR_SENTINEL | |
9009cc24 PP |
2278 | }; |
2279 | ||
2280 | static | |
2281 | GString *get_component_auto_name(const char *prefix, | |
8eee8ea2 | 2282 | const bt_value *existing_names) |
9009cc24 PP |
2283 | { |
2284 | unsigned int i = 0; | |
2285 | GString *auto_name = g_string_new(NULL); | |
2286 | ||
2287 | if (!auto_name) { | |
4b39e5c9 | 2288 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
2289 | goto end; |
2290 | } | |
2291 | ||
44514773 | 2292 | if (!bt_value_map_has_entry(existing_names, prefix)) { |
9009cc24 PP |
2293 | g_string_assign(auto_name, prefix); |
2294 | goto end; | |
2295 | } | |
2296 | ||
2297 | do { | |
2298 | g_string_printf(auto_name, "%s-%d", prefix, i); | |
2299 | i++; | |
44514773 | 2300 | } while (bt_value_map_has_entry(existing_names, auto_name->str)); |
9009cc24 PP |
2301 | |
2302 | end: | |
2303 | return auto_name; | |
2304 | } | |
2305 | ||
2306 | struct implicit_component_args { | |
2307 | bool exists; | |
a1040187 SM |
2308 | |
2309 | /* The component class name (e.g. src.ctf.fs). */ | |
fd5f8053 | 2310 | GString *comp_arg; |
a1040187 SM |
2311 | |
2312 | /* The component instance name. */ | |
9009cc24 | 2313 | GString *name_arg; |
a1040187 | 2314 | |
9009cc24 | 2315 | GString *params_arg; |
8eee8ea2 | 2316 | bt_value *extra_params; |
9009cc24 PP |
2317 | }; |
2318 | ||
2319 | static | |
2320 | int assign_name_to_implicit_component(struct implicit_component_args *args, | |
8eee8ea2 | 2321 | const char *prefix, bt_value *existing_names, |
9009cc24 PP |
2322 | GList **comp_names, bool append_to_comp_names) |
2323 | { | |
2324 | int ret = 0; | |
2325 | GString *name = NULL; | |
2326 | ||
2327 | if (!args->exists) { | |
2328 | goto end; | |
2329 | } | |
2330 | ||
17582c6d | 2331 | name = get_component_auto_name(prefix, |
ce141536 | 2332 | existing_names); |
9009cc24 PP |
2333 | |
2334 | if (!name) { | |
2335 | ret = -1; | |
2336 | goto end; | |
2337 | } | |
2338 | ||
2339 | g_string_assign(args->name_arg, name->str); | |
2340 | ||
ce141536 | 2341 | if (bt_value_map_insert_entry(existing_names, name->str, |
9009cc24 | 2342 | bt_value_null)) { |
4b39e5c9 | 2343 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
2344 | ret = -1; |
2345 | goto end; | |
2346 | } | |
2347 | ||
2348 | if (append_to_comp_names) { | |
2349 | *comp_names = g_list_append(*comp_names, name); | |
2350 | name = NULL; | |
2351 | } | |
2352 | ||
2353 | end: | |
2354 | if (name) { | |
2355 | g_string_free(name, TRUE); | |
2356 | } | |
2357 | ||
2358 | return ret; | |
2359 | } | |
2360 | ||
2361 | static | |
2362 | int append_run_args_for_implicit_component( | |
9009cc24 | 2363 | struct implicit_component_args *impl_args, |
8eee8ea2 | 2364 | bt_value *run_args) |
9009cc24 PP |
2365 | { |
2366 | int ret = 0; | |
2367 | size_t i; | |
4186b466 | 2368 | GString *component_arg_for_run = NULL; |
9009cc24 PP |
2369 | |
2370 | if (!impl_args->exists) { | |
2371 | goto end; | |
2372 | } | |
2373 | ||
4186b466 SM |
2374 | component_arg_for_run = g_string_new(NULL); |
2375 | if (!component_arg_for_run) { | |
4b39e5c9 | 2376 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
fd5f8053 | 2377 | goto error; |
9009cc24 PP |
2378 | } |
2379 | ||
4186b466 SM |
2380 | /* Build the full `name:type.plugin.cls`. */ |
2381 | BT_ASSERT(!strchr(impl_args->name_arg->str, '\\')); | |
2382 | BT_ASSERT(!strchr(impl_args->name_arg->str, ':')); | |
2383 | g_string_printf(component_arg_for_run, "%s:%s", | |
2384 | impl_args->name_arg->str, impl_args->comp_arg->str); | |
9009cc24 | 2385 | |
4186b466 | 2386 | if (bt_value_array_append_string_element(run_args, "--component")) { |
4b39e5c9 | 2387 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
2388 | goto error; |
2389 | } | |
2390 | ||
4186b466 SM |
2391 | if (bt_value_array_append_string_element(run_args, |
2392 | component_arg_for_run->str)) { | |
4b39e5c9 | 2393 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
2394 | goto error; |
2395 | } | |
2396 | ||
2397 | if (impl_args->params_arg->len > 0) { | |
ce141536 | 2398 | if (bt_value_array_append_string_element(run_args, "--params")) { |
4b39e5c9 | 2399 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
2400 | goto error; |
2401 | } | |
2402 | ||
ce141536 | 2403 | if (bt_value_array_append_string_element(run_args, |
9009cc24 | 2404 | impl_args->params_arg->str)) { |
4b39e5c9 | 2405 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
2406 | goto error; |
2407 | } | |
2408 | } | |
2409 | ||
1270d0e9 | 2410 | for (i = 0; i < bt_value_array_get_length(impl_args->extra_params); |
17582c6d | 2411 | i++) { |
8eee8ea2 | 2412 | const bt_value *elem; |
9009cc24 PP |
2413 | const char *arg; |
2414 | ||
ce141536 PP |
2415 | elem = bt_value_array_borrow_element_by_index(impl_args->extra_params, |
2416 | i); | |
9009cc24 PP |
2417 | if (!elem) { |
2418 | goto error; | |
2419 | } | |
2420 | ||
8b45963b | 2421 | BT_ASSERT(bt_value_is_string(elem)); |
b5cdc106 | 2422 | arg = bt_value_string_get(elem); |
ce141536 | 2423 | ret = bt_value_array_append_string_element(run_args, arg); |
9009cc24 | 2424 | if (ret) { |
4b39e5c9 | 2425 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
2426 | goto error; |
2427 | } | |
2428 | } | |
2429 | ||
2430 | goto end; | |
2431 | ||
2432 | error: | |
2433 | ret = -1; | |
2434 | ||
2435 | end: | |
4186b466 SM |
2436 | if (component_arg_for_run) { |
2437 | g_string_free(component_arg_for_run, TRUE); | |
2438 | } | |
2439 | ||
9009cc24 PP |
2440 | return ret; |
2441 | } | |
2442 | ||
a1040187 SM |
2443 | /* Free the fields of a `struct implicit_component_args`. */ |
2444 | ||
9009cc24 | 2445 | static |
94023a1c | 2446 | void finalize_implicit_component_args(struct implicit_component_args *args) |
9009cc24 | 2447 | { |
8b45963b | 2448 | BT_ASSERT(args); |
9009cc24 | 2449 | |
fd5f8053 PP |
2450 | if (args->comp_arg) { |
2451 | g_string_free(args->comp_arg, TRUE); | |
9009cc24 PP |
2452 | } |
2453 | ||
2454 | if (args->name_arg) { | |
2455 | g_string_free(args->name_arg, TRUE); | |
2456 | } | |
2457 | ||
2458 | if (args->params_arg) { | |
2459 | g_string_free(args->params_arg, TRUE); | |
2460 | } | |
2461 | ||
8c6884d9 | 2462 | bt_value_put_ref(args->extra_params); |
9009cc24 PP |
2463 | } |
2464 | ||
a1040187 SM |
2465 | /* Destroy a dynamically-allocated `struct implicit_component_args`. */ |
2466 | ||
2467 | static | |
2468 | void destroy_implicit_component_args(struct implicit_component_args *args) | |
2469 | { | |
2470 | finalize_implicit_component_args(args); | |
2471 | g_free(args); | |
2472 | } | |
2473 | ||
2474 | /* Initialize the fields of an already allocated `struct implicit_component_args`. */ | |
2475 | ||
9009cc24 PP |
2476 | static |
2477 | int init_implicit_component_args(struct implicit_component_args *args, | |
fd5f8053 | 2478 | const char *comp_arg, bool exists) |
9009cc24 PP |
2479 | { |
2480 | int ret = 0; | |
2481 | ||
2482 | args->exists = exists; | |
fd5f8053 | 2483 | args->comp_arg = g_string_new(comp_arg); |
9009cc24 PP |
2484 | args->name_arg = g_string_new(NULL); |
2485 | args->params_arg = g_string_new(NULL); | |
ce141536 | 2486 | args->extra_params = bt_value_array_create(); |
9009cc24 | 2487 | |
fd5f8053 | 2488 | if (!args->comp_arg || !args->name_arg || |
e7ad156c | 2489 | !args->params_arg || !args->extra_params) { |
9009cc24 | 2490 | ret = -1; |
94023a1c | 2491 | finalize_implicit_component_args(args); |
4b39e5c9 | 2492 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
2493 | goto end; |
2494 | } | |
2495 | ||
2496 | end: | |
2497 | return ret; | |
2498 | } | |
2499 | ||
a1040187 SM |
2500 | /* Dynamically allocate and initialize a `struct implicit_component_args`. */ |
2501 | ||
2502 | static | |
2503 | struct implicit_component_args *create_implicit_component_args( | |
2504 | const char *comp_arg) | |
2505 | { | |
2506 | struct implicit_component_args *args; | |
2507 | int status; | |
2508 | ||
2509 | args = g_new(struct implicit_component_args, 1); | |
2510 | if (!args) { | |
2511 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); | |
2512 | goto end; | |
2513 | } | |
2514 | ||
2515 | status = init_implicit_component_args(args, comp_arg, true); | |
2516 | if (status != 0) { | |
2517 | g_free(args); | |
2518 | args = NULL; | |
2519 | } | |
2520 | ||
2521 | end: | |
2522 | return args; | |
2523 | } | |
2524 | ||
9009cc24 PP |
2525 | static |
2526 | void append_implicit_component_param(struct implicit_component_args *args, | |
2527 | const char *key, const char *value) | |
2528 | { | |
8b45963b PP |
2529 | BT_ASSERT(args); |
2530 | BT_ASSERT(key); | |
2531 | BT_ASSERT(value); | |
9009cc24 PP |
2532 | append_param_arg(args->params_arg, key, value); |
2533 | } | |
2534 | ||
a1040187 SM |
2535 | /* |
2536 | * Append the given parameter (`key=value`) to all component specifications | |
2537 | * in `implicit_comp_args` (an array of `struct implicit_component_args *`) | |
2538 | * which match `comp_arg`. | |
2539 | * | |
2540 | * Return the number of matching components. | |
2541 | */ | |
2542 | ||
2543 | static | |
2544 | int append_multiple_implicit_components_param(GPtrArray *implicit_comp_args, | |
2545 | const char *comp_arg, const char *key, const char *value) | |
2546 | { | |
2547 | int i; | |
2548 | int n = 0; | |
2549 | ||
2550 | for (i = 0; i < implicit_comp_args->len; i++) { | |
2551 | struct implicit_component_args *args = implicit_comp_args->pdata[i]; | |
2552 | ||
2553 | if (strcmp(args->comp_arg->str, comp_arg) == 0) { | |
2554 | append_implicit_component_param(args, key, value); | |
2555 | n++; | |
2556 | } | |
2557 | } | |
2558 | ||
2559 | return n; | |
2560 | } | |
2561 | ||
b2f834c9 | 2562 | /* Escape value to make it suitable to use as a string parameter value. */ |
9009cc24 | 2563 | static |
b2f834c9 | 2564 | gchar *escape_string_value(const char *value) |
9009cc24 | 2565 | { |
b2f834c9 SM |
2566 | GString *ret; |
2567 | const char *in; | |
2568 | ||
2569 | ret = g_string_new(NULL); | |
2570 | if (!ret) { | |
4b39e5c9 | 2571 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
b2f834c9 SM |
2572 | goto end; |
2573 | } | |
2574 | ||
2575 | in = value; | |
2576 | while (*in) { | |
2577 | switch (*in) { | |
2578 | case '"': | |
2579 | case '\\': | |
2580 | g_string_append_c(ret, '\\'); | |
2581 | break; | |
2582 | } | |
2583 | ||
2584 | g_string_append_c(ret, *in); | |
2585 | ||
2586 | in++; | |
2587 | } | |
2588 | ||
2589 | end: | |
2590 | return g_string_free(ret, FALSE); | |
2591 | } | |
9009cc24 | 2592 | |
242407a9 | 2593 | static |
bf5b7748 | 2594 | int bt_value_to_cli_param_value_append(const bt_value *value, GString *buf) |
242407a9 | 2595 | { |
bf5b7748 | 2596 | BT_ASSERT(buf); |
242407a9 | 2597 | |
bf5b7748 | 2598 | int ret = -1; |
242407a9 SM |
2599 | |
2600 | switch (bt_value_get_type(value)) { | |
2601 | case BT_VALUE_TYPE_STRING: | |
2602 | { | |
2603 | const char *str_value = bt_value_string_get(value); | |
2604 | gchar *escaped_str_value; | |
2605 | ||
2606 | escaped_str_value = escape_string_value(str_value); | |
2607 | if (!escaped_str_value) { | |
bf5b7748 | 2608 | goto end; |
242407a9 SM |
2609 | } |
2610 | ||
bf5b7748 | 2611 | g_string_append_printf(buf, "\"%s\"", escaped_str_value); |
242407a9 SM |
2612 | |
2613 | g_free(escaped_str_value); | |
2614 | break; | |
2615 | } | |
bf5b7748 SM |
2616 | case BT_VALUE_TYPE_ARRAY: { |
2617 | g_string_append_c(buf, '['); | |
1270d0e9 | 2618 | uint64_t sz = bt_value_array_get_length(value); |
bf5b7748 | 2619 | for (uint64_t i = 0; i < sz; i++) { |
58f62bf9 PP |
2620 | const bt_value *item; |
2621 | int ret; | |
2622 | ||
bf5b7748 | 2623 | if (i > 0) { |
58f62bf9 | 2624 | g_string_append(buf, ", "); |
bf5b7748 | 2625 | } |
58f62bf9 PP |
2626 | |
2627 | item = bt_value_array_borrow_element_by_index_const( | |
2628 | value, i); | |
2629 | ret = bt_value_to_cli_param_value_append(item, buf); | |
2630 | ||
bf5b7748 SM |
2631 | if (ret) { |
2632 | goto end; | |
2633 | } | |
2634 | } | |
2635 | g_string_append_c(buf, ']'); | |
2636 | break; | |
2637 | } | |
242407a9 SM |
2638 | default: |
2639 | abort(); | |
2640 | } | |
2641 | ||
bf5b7748 SM |
2642 | ret = 0; |
2643 | ||
2644 | end: | |
2645 | return ret; | |
2646 | } | |
2647 | ||
2648 | /* | |
2649 | * Convert `value` to its equivalent representation as a command line parameter | |
2650 | * value. | |
2651 | */ | |
2652 | ||
2653 | static | |
2654 | gchar *bt_value_to_cli_param_value(bt_value *value) | |
2655 | { | |
2656 | GString *buf; | |
2657 | gchar *result = NULL; | |
2658 | ||
2659 | buf = g_string_new(NULL); | |
2660 | if (!buf) { | |
4b39e5c9 | 2661 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
bf5b7748 SM |
2662 | goto error; |
2663 | } | |
2664 | ||
2665 | if (bt_value_to_cli_param_value_append(value, buf)) { | |
2666 | goto error; | |
2667 | } | |
2668 | ||
242407a9 SM |
2669 | result = g_string_free(buf, FALSE); |
2670 | buf = NULL; | |
2671 | ||
2672 | goto end; | |
2673 | ||
2674 | error: | |
2675 | if (buf) { | |
2676 | g_string_free(buf, TRUE); | |
2677 | } | |
2678 | ||
2679 | end: | |
2680 | return result; | |
2681 | } | |
2682 | ||
b2f834c9 | 2683 | static |
242407a9 | 2684 | int append_parameter_to_args(bt_value *args, const char *key, bt_value *value) |
b2f834c9 | 2685 | { |
8b45963b | 2686 | BT_ASSERT(args); |
b2f834c9 | 2687 | BT_ASSERT(bt_value_get_type(args) == BT_VALUE_TYPE_ARRAY); |
8b45963b PP |
2688 | BT_ASSERT(key); |
2689 | BT_ASSERT(value); | |
9009cc24 | 2690 | |
b2f834c9 | 2691 | int ret = 0; |
242407a9 | 2692 | gchar *str_value = NULL; |
b2f834c9 SM |
2693 | GString *parameter = NULL; |
2694 | ||
2695 | if (bt_value_array_append_string_element(args, "--params")) { | |
4b39e5c9 | 2696 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
2697 | ret = -1; |
2698 | goto end; | |
2699 | } | |
2700 | ||
242407a9 SM |
2701 | str_value = bt_value_to_cli_param_value(value); |
2702 | if (!str_value) { | |
9009cc24 PP |
2703 | ret = -1; |
2704 | goto end; | |
2705 | } | |
2706 | ||
b2f834c9 SM |
2707 | parameter = g_string_new(NULL); |
2708 | if (!parameter) { | |
4b39e5c9 | 2709 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
2710 | ret = -1; |
2711 | goto end; | |
2712 | } | |
2713 | ||
242407a9 | 2714 | g_string_printf(parameter, "%s=%s", key, str_value); |
b2f834c9 SM |
2715 | |
2716 | if (bt_value_array_append_string_element(args, parameter->str)) { | |
4b39e5c9 | 2717 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
2718 | ret = -1; |
2719 | goto end; | |
2720 | } | |
2721 | ||
2722 | end: | |
b2f834c9 SM |
2723 | if (parameter) { |
2724 | g_string_free(parameter, TRUE); | |
2725 | parameter = NULL; | |
2726 | } | |
2727 | ||
242407a9 SM |
2728 | if (str_value) { |
2729 | g_free(str_value); | |
2730 | str_value = NULL; | |
2731 | } | |
2732 | ||
2733 | return ret; | |
2734 | } | |
2735 | ||
2736 | static | |
2737 | int append_string_parameter_to_args(bt_value *args, const char *key, const char *value) | |
2738 | { | |
2739 | bt_value *str_value; | |
2740 | int ret; | |
2741 | ||
2742 | str_value = bt_value_string_create_init(value); | |
2743 | ||
2744 | if (!str_value) { | |
4b39e5c9 | 2745 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
242407a9 SM |
2746 | ret = -1; |
2747 | goto end; | |
2748 | } | |
2749 | ||
2750 | ret = append_parameter_to_args(args, key, str_value); | |
2751 | ||
2752 | end: | |
2753 | BT_VALUE_PUT_REF_AND_RESET(str_value); | |
9009cc24 PP |
2754 | return ret; |
2755 | } | |
2756 | ||
b2f834c9 SM |
2757 | static |
2758 | int append_implicit_component_extra_param(struct implicit_component_args *args, | |
2759 | const char *key, const char *value) | |
2760 | { | |
242407a9 | 2761 | return append_string_parameter_to_args(args->extra_params, key, value); |
b2f834c9 SM |
2762 | } |
2763 | ||
9009cc24 PP |
2764 | /* |
2765 | * Escapes `.`, `:`, and `\` of `input` with `\`. | |
2766 | */ | |
2767 | static | |
2768 | GString *escape_dot_colon(const char *input) | |
2769 | { | |
2770 | GString *output = g_string_new(NULL); | |
2771 | const char *ch; | |
2772 | ||
2773 | if (!output) { | |
4b39e5c9 | 2774 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
2775 | goto end; |
2776 | } | |
2777 | ||
2778 | for (ch = input; *ch != '\0'; ch++) { | |
2779 | if (*ch == '\\' || *ch == '.' || *ch == ':') { | |
2780 | g_string_append_c(output, '\\'); | |
2781 | } | |
2782 | ||
2783 | g_string_append_c(output, *ch); | |
2784 | } | |
2785 | ||
2786 | end: | |
2787 | return output; | |
2788 | } | |
2789 | ||
2790 | /* | |
2791 | * Appends a --connect option to a list of arguments. `upstream_name` | |
2792 | * and `downstream_name` are escaped with escape_dot_colon() in this | |
2793 | * function. | |
2794 | */ | |
2795 | static | |
8eee8ea2 | 2796 | int append_connect_arg(bt_value *run_args, |
9009cc24 PP |
2797 | const char *upstream_name, const char *downstream_name) |
2798 | { | |
2799 | int ret = 0; | |
2800 | GString *e_upstream_name = escape_dot_colon(upstream_name); | |
2801 | GString *e_downstream_name = escape_dot_colon(downstream_name); | |
2802 | GString *arg = g_string_new(NULL); | |
2803 | ||
2804 | if (!e_upstream_name || !e_downstream_name || !arg) { | |
4b39e5c9 | 2805 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
2806 | ret = -1; |
2807 | goto end; | |
2808 | } | |
2809 | ||
ce141536 | 2810 | ret = bt_value_array_append_string_element(run_args, "--connect"); |
9009cc24 | 2811 | if (ret) { |
4b39e5c9 | 2812 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
2813 | ret = -1; |
2814 | goto end; | |
2815 | } | |
2816 | ||
2817 | g_string_append(arg, e_upstream_name->str); | |
2818 | g_string_append_c(arg, ':'); | |
2819 | g_string_append(arg, e_downstream_name->str); | |
ce141536 | 2820 | ret = bt_value_array_append_string_element(run_args, arg->str); |
9009cc24 | 2821 | if (ret) { |
4b39e5c9 | 2822 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
2823 | ret = -1; |
2824 | goto end; | |
2825 | } | |
2826 | ||
2827 | end: | |
2828 | if (arg) { | |
2829 | g_string_free(arg, TRUE); | |
2830 | } | |
2831 | ||
2832 | if (e_upstream_name) { | |
2833 | g_string_free(e_upstream_name, TRUE); | |
2834 | } | |
2835 | ||
2836 | if (e_downstream_name) { | |
2837 | g_string_free(e_downstream_name, TRUE); | |
2838 | } | |
2839 | ||
2840 | return ret; | |
2841 | } | |
2842 | ||
2843 | /* | |
2844 | * Appends the run command's --connect options for the convert command. | |
2845 | */ | |
2846 | static | |
8eee8ea2 | 2847 | int convert_auto_connect(bt_value *run_args, |
9009cc24 PP |
2848 | GList *source_names, GList *filter_names, |
2849 | GList *sink_names) | |
2850 | { | |
2851 | int ret = 0; | |
2852 | GList *source_at = source_names; | |
2853 | GList *filter_at = filter_names; | |
2854 | GList *filter_prev; | |
2855 | GList *sink_at = sink_names; | |
2856 | ||
8b45963b PP |
2857 | BT_ASSERT(source_names); |
2858 | BT_ASSERT(filter_names); | |
2859 | BT_ASSERT(sink_names); | |
9009cc24 PP |
2860 | |
2861 | /* Connect all sources to the first filter */ | |
8e01f2d9 | 2862 | for (source_at = source_names; source_at; source_at = g_list_next(source_at)) { |
9009cc24 PP |
2863 | GString *source_name = source_at->data; |
2864 | GString *filter_name = filter_at->data; | |
2865 | ||
2866 | ret = append_connect_arg(run_args, source_name->str, | |
2867 | filter_name->str); | |
2868 | if (ret) { | |
2869 | goto error; | |
2870 | } | |
2871 | } | |
2872 | ||
2873 | filter_prev = filter_at; | |
2874 | filter_at = g_list_next(filter_at); | |
2875 | ||
2876 | /* Connect remaining filters */ | |
8e01f2d9 | 2877 | for (; filter_at; filter_prev = filter_at, filter_at = g_list_next(filter_at)) { |
9009cc24 PP |
2878 | GString *filter_name = filter_at->data; |
2879 | GString *filter_prev_name = filter_prev->data; | |
2880 | ||
2881 | ret = append_connect_arg(run_args, filter_prev_name->str, | |
2882 | filter_name->str); | |
2883 | if (ret) { | |
2884 | goto error; | |
2885 | } | |
2886 | } | |
2887 | ||
2888 | /* Connect last filter to all sinks */ | |
8e01f2d9 | 2889 | for (sink_at = sink_names; sink_at; sink_at = g_list_next(sink_at)) { |
9009cc24 PP |
2890 | GString *filter_name = filter_prev->data; |
2891 | GString *sink_name = sink_at->data; | |
2892 | ||
2893 | ret = append_connect_arg(run_args, filter_name->str, | |
2894 | sink_name->str); | |
2895 | if (ret) { | |
2896 | goto error; | |
2897 | } | |
2898 | } | |
2899 | ||
2900 | goto end; | |
2901 | ||
2902 | error: | |
2903 | ret = -1; | |
2904 | ||
2905 | end: | |
2906 | return ret; | |
2907 | } | |
2908 | ||
2909 | static | |
2910 | int split_timerange(const char *arg, char **begin, char **end) | |
2911 | { | |
2912 | int ret = 0; | |
2913 | const char *ch = arg; | |
2914 | size_t end_pos; | |
2915 | GString *g_begin = NULL; | |
2916 | GString *g_end = NULL; | |
2917 | ||
8b45963b | 2918 | BT_ASSERT(arg); |
9009cc24 PP |
2919 | |
2920 | if (*ch == '[') { | |
2921 | ch++; | |
2922 | } | |
2923 | ||
2924 | g_begin = bt_common_string_until(ch, "", ",", &end_pos); | |
2925 | if (!g_begin || ch[end_pos] != ',' || g_begin->len == 0) { | |
2926 | goto error; | |
2927 | } | |
2928 | ||
2929 | ch += end_pos + 1; | |
2930 | ||
2931 | g_end = bt_common_string_until(ch, "", "]", &end_pos); | |
2932 | if (!g_end || g_end->len == 0) { | |
2933 | goto error; | |
2934 | } | |
2935 | ||
8b45963b PP |
2936 | BT_ASSERT(begin); |
2937 | BT_ASSERT(end); | |
9009cc24 PP |
2938 | *begin = g_begin->str; |
2939 | *end = g_end->str; | |
2940 | g_string_free(g_begin, FALSE); | |
2941 | g_string_free(g_end, FALSE); | |
2942 | g_begin = NULL; | |
2943 | g_end = NULL; | |
2944 | goto end; | |
2945 | ||
2946 | error: | |
2947 | ret = -1; | |
2948 | ||
2949 | end: | |
2950 | if (g_begin) { | |
2951 | g_string_free(g_begin, TRUE); | |
2952 | } | |
2953 | ||
2954 | if (g_end) { | |
2955 | g_string_free(g_end, TRUE); | |
2956 | } | |
2957 | ||
2958 | return ret; | |
2959 | } | |
2960 | ||
2961 | static | |
2962 | int g_list_prepend_gstring(GList **list, const char *string) | |
2963 | { | |
2964 | int ret = 0; | |
2965 | GString *gs = g_string_new(string); | |
2966 | ||
8b45963b | 2967 | BT_ASSERT(list); |
9009cc24 PP |
2968 | |
2969 | if (!gs) { | |
4b39e5c9 | 2970 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
2971 | goto end; |
2972 | } | |
2973 | ||
2974 | *list = g_list_prepend(*list, gs); | |
2975 | ||
2976 | end: | |
2977 | return ret; | |
2978 | } | |
2979 | ||
a1040187 | 2980 | /* |
8d5247bd PP |
2981 | * Create `struct implicit_component_args` structures for each of the |
2982 | * source components we identified. Add them to `component_args`. | |
459f81ca | 2983 | * |
8d5247bd PP |
2984 | * `non_opt_params` is an array where each element is an array of |
2985 | * strings containing all the arguments to `--params` that apply to the | |
2986 | * non-option argument at the same index. For example, if, for a | |
2987 | * non-option argument, the following `--params` options applied: | |
459f81ca SM |
2988 | * |
2989 | * --params=a=2 --params=b=3,c=4 | |
2990 | * | |
8d5247bd | 2991 | * its entry in `non_opt_params` would contain |
459f81ca SM |
2992 | * |
2993 | * ["a=2", "b=3,c=4"] | |
a1040187 SM |
2994 | */ |
2995 | ||
2996 | static | |
459f81ca SM |
2997 | int create_implicit_component_args_from_auto_discovered_sources( |
2998 | const struct auto_source_discovery *auto_disc, | |
8d5247bd PP |
2999 | const bt_value *non_opt_params, |
3000 | const bt_value *non_opt_loglevels, | |
459f81ca | 3001 | GPtrArray *component_args) |
a1040187 SM |
3002 | { |
3003 | gchar *cc_name = NULL; | |
3004 | struct implicit_component_args *comp = NULL; | |
3005 | int status; | |
3006 | guint i, len; | |
3007 | ||
3008 | len = auto_disc->results->len; | |
3009 | ||
3010 | for (i = 0; i < len; i++) { | |
3011 | struct auto_source_discovery_result *res = | |
3012 | g_ptr_array_index(auto_disc->results, i); | |
459f81ca | 3013 | uint64_t orig_indices_i, orig_indices_count; |
a1040187 SM |
3014 | |
3015 | g_free(cc_name); | |
3016 | cc_name = g_strdup_printf("source.%s.%s", res->plugin_name, res->source_cc_name); | |
3017 | if (!cc_name) { | |
3018 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); | |
459f81ca | 3019 | goto error; |
a1040187 SM |
3020 | } |
3021 | ||
3022 | comp = create_implicit_component_args(cc_name); | |
3023 | if (!comp) { | |
459f81ca SM |
3024 | goto error; |
3025 | } | |
3026 | ||
3027 | /* | |
8d5247bd PP |
3028 | * Append parameters and log levels of all the |
3029 | * non-option arguments that contributed to this | |
3030 | * component instance coming into existence. | |
459f81ca | 3031 | */ |
1270d0e9 | 3032 | orig_indices_count = bt_value_array_get_length(res->original_input_indices); |
459f81ca SM |
3033 | for (orig_indices_i = 0; orig_indices_i < orig_indices_count; orig_indices_i++) { |
3034 | const bt_value *orig_idx_value = | |
3035 | bt_value_array_borrow_element_by_index( | |
3036 | res->original_input_indices, orig_indices_i); | |
3037 | uint64_t orig_idx = bt_value_integer_unsigned_get(orig_idx_value); | |
3038 | const bt_value *params_array = | |
3039 | bt_value_array_borrow_element_by_index_const( | |
8d5247bd | 3040 | non_opt_params, orig_idx); |
459f81ca | 3041 | uint64_t params_i, params_count; |
b6a0ac97 | 3042 | const bt_value *loglevel_value; |
459f81ca | 3043 | |
1270d0e9 | 3044 | params_count = bt_value_array_get_length(params_array); |
459f81ca SM |
3045 | for (params_i = 0; params_i < params_count; params_i++) { |
3046 | const bt_value *params_value = | |
3047 | bt_value_array_borrow_element_by_index_const( | |
3048 | params_array, params_i); | |
3049 | const char *params = bt_value_string_get(params_value); | |
3050 | bt_value_array_append_element_status append_status; | |
3051 | ||
3052 | append_status = bt_value_array_append_string_element( | |
3053 | comp->extra_params, "--params"); | |
3054 | if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { | |
3055 | BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element."); | |
3056 | goto error; | |
3057 | } | |
3058 | ||
3059 | append_status = bt_value_array_append_string_element( | |
3060 | comp->extra_params, params); | |
3061 | if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { | |
3062 | BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element."); | |
3063 | goto error; | |
3064 | } | |
3065 | } | |
b6a0ac97 SM |
3066 | |
3067 | loglevel_value = bt_value_array_borrow_element_by_index_const( | |
8d5247bd | 3068 | non_opt_loglevels, orig_idx); |
b6a0ac97 SM |
3069 | if (bt_value_get_type(loglevel_value) == BT_VALUE_TYPE_STRING) { |
3070 | const char *loglevel = bt_value_string_get(loglevel_value); | |
3071 | bt_value_array_append_element_status append_status; | |
3072 | ||
3073 | append_status = bt_value_array_append_string_element( | |
3074 | comp->extra_params, "--log-level"); | |
3075 | if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { | |
3076 | BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element."); | |
3077 | goto error; | |
3078 | } | |
3079 | ||
3080 | append_status = bt_value_array_append_string_element( | |
3081 | comp->extra_params, loglevel); | |
3082 | if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { | |
3083 | BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element."); | |
3084 | goto error; | |
3085 | } | |
3086 | } | |
a1040187 SM |
3087 | } |
3088 | ||
3089 | status = append_parameter_to_args(comp->extra_params, "inputs", res->inputs); | |
3090 | if (status != 0) { | |
459f81ca | 3091 | goto error; |
a1040187 SM |
3092 | } |
3093 | ||
3094 | g_ptr_array_add(component_args, comp); | |
3095 | comp = NULL; | |
3096 | } | |
3097 | ||
459f81ca SM |
3098 | status = 0; |
3099 | goto end; | |
3100 | ||
3101 | error: | |
3102 | status = -1; | |
3103 | ||
a1040187 SM |
3104 | end: |
3105 | g_free(cc_name); | |
3106 | ||
3107 | if (comp) { | |
3108 | destroy_implicit_component_args(comp); | |
3109 | } | |
459f81ca SM |
3110 | |
3111 | return status; | |
a1040187 SM |
3112 | } |
3113 | ||
4186b466 SM |
3114 | /* |
3115 | * As we iterate the arguments to the convert command, this tracks what is the | |
3116 | * type of the current item, to which some contextual options (e.g. --params) | |
3117 | * apply to. | |
3118 | */ | |
3119 | enum convert_current_item_type { | |
3120 | /* There is no current item. */ | |
3121 | CONVERT_CURRENT_ITEM_TYPE_NONE, | |
3122 | ||
3123 | /* Current item is a component. */ | |
3124 | CONVERT_CURRENT_ITEM_TYPE_COMPONENT, | |
551b5a52 | 3125 | |
8d5247bd PP |
3126 | /* Current item is a non-option argument. */ |
3127 | CONVERT_CURRENT_ITEM_TYPE_NON_OPT, | |
4186b466 SM |
3128 | }; |
3129 | ||
9009cc24 PP |
3130 | /* |
3131 | * Creates a Babeltrace config object from the arguments of a convert | |
3132 | * command. | |
3133 | * | |
3134 | * *retcode is set to the appropriate exit code to use. | |
3135 | */ | |
3136 | static | |
3137 | struct bt_config *bt_config_convert_from_args(int argc, const char *argv[], | |
d36258d0 | 3138 | int *retcode, const bt_value *plugin_paths, |
3f8644ec | 3139 | int *default_log_level, const bt_interrupter *interrupter) |
9009cc24 | 3140 | { |
4186b466 SM |
3141 | enum convert_current_item_type current_item_type = |
3142 | CONVERT_CURRENT_ITEM_TYPE_NONE; | |
a87d2977 | 3143 | int ret = 0; |
9009cc24 | 3144 | struct bt_config *cfg = NULL; |
9009cc24 PP |
3145 | bool got_input_format_opt = false; |
3146 | bool got_output_format_opt = false; | |
3147 | bool trimmer_has_begin = false; | |
3148 | bool trimmer_has_end = false; | |
b5fe3e00 | 3149 | bool stream_intersection_mode = false; |
9009cc24 PP |
3150 | bool print_run_args = false; |
3151 | bool print_run_args_0 = false; | |
3152 | bool print_ctf_metadata = false; | |
8eee8ea2 PP |
3153 | bt_value *run_args = NULL; |
3154 | bt_value *all_names = NULL; | |
9009cc24 PP |
3155 | GList *source_names = NULL; |
3156 | GList *filter_names = NULL; | |
3157 | GList *sink_names = NULL; | |
8d5247bd PP |
3158 | bt_value *non_opts = NULL; |
3159 | bt_value *non_opt_params = NULL; | |
3160 | bt_value *non_opt_loglevels = NULL; | |
e7ad156c | 3161 | struct implicit_component_args implicit_ctf_output_args = { 0 }; |
9009cc24 PP |
3162 | struct implicit_component_args implicit_lttng_live_args = { 0 }; |
3163 | struct implicit_component_args implicit_dummy_args = { 0 }; | |
3164 | struct implicit_component_args implicit_text_args = { 0 }; | |
3165 | struct implicit_component_args implicit_debug_info_args = { 0 }; | |
3166 | struct implicit_component_args implicit_muxer_args = { 0 }; | |
3167 | struct implicit_component_args implicit_trimmer_args = { 0 }; | |
9009cc24 PP |
3168 | char error_buf[256] = { 0 }; |
3169 | size_t i; | |
3170 | struct bt_common_lttng_live_url_parts lttng_live_url_parts = { 0 }; | |
e7ad156c | 3171 | char *output = NULL; |
a1040187 SM |
3172 | struct auto_source_discovery auto_disc = { NULL }; |
3173 | GString *auto_disc_comp_name = NULL; | |
a87d2977 | 3174 | struct bt_argpar_parse_ret argpar_parse_ret = { 0 }; |
4186b466 SM |
3175 | GString *name_gstr = NULL; |
3176 | GString *component_arg_for_run = NULL; | |
2df75e06 | 3177 | bt_value *live_inputs_array_val = NULL; |
a1040187 SM |
3178 | |
3179 | /* | |
3180 | * Array of `struct implicit_component_args *` created for the sources | |
3181 | * we have auto-discovered. | |
3182 | */ | |
3183 | GPtrArray *discovered_source_args = NULL; | |
3184 | ||
3185 | /* | |
3186 | * If set, restrict automatic source discovery to this component class | |
3187 | * of this plugin. | |
3188 | */ | |
3189 | const char *auto_source_discovery_restrict_plugin_name = NULL; | |
3190 | const char *auto_source_discovery_restrict_component_class_name = NULL; | |
3191 | ||
3192 | gchar *ctf_fs_source_clock_class_offset_arg = NULL; | |
3193 | gchar *ctf_fs_source_clock_class_offset_ns_arg = NULL; | |
9009cc24 PP |
3194 | *retcode = 0; |
3195 | ||
091cc3eb | 3196 | if (argc < 1) { |
9009cc24 PP |
3197 | print_convert_usage(stdout); |
3198 | *retcode = -1; | |
3199 | goto end; | |
3200 | } | |
3201 | ||
e7ad156c PP |
3202 | if (init_implicit_component_args(&implicit_ctf_output_args, |
3203 | "sink.ctf.fs", false)) { | |
3204 | goto error; | |
3205 | } | |
3206 | ||
9009cc24 | 3207 | if (init_implicit_component_args(&implicit_lttng_live_args, |
fd5f8053 | 3208 | "source.ctf.lttng-live", false)) { |
9009cc24 PP |
3209 | goto error; |
3210 | } | |
3211 | ||
fd5f8053 PP |
3212 | if (init_implicit_component_args(&implicit_text_args, |
3213 | "sink.text.pretty", false)) { | |
9009cc24 PP |
3214 | goto error; |
3215 | } | |
3216 | ||
fd5f8053 PP |
3217 | if (init_implicit_component_args(&implicit_dummy_args, |
3218 | "sink.utils.dummy", false)) { | |
9009cc24 PP |
3219 | goto error; |
3220 | } | |
3221 | ||
3222 | if (init_implicit_component_args(&implicit_debug_info_args, | |
8e765000 | 3223 | "filter.lttng-utils.debug-info", false)) { |
9009cc24 PP |
3224 | goto error; |
3225 | } | |
3226 | ||
fd5f8053 PP |
3227 | if (init_implicit_component_args(&implicit_muxer_args, |
3228 | "filter.utils.muxer", true)) { | |
9009cc24 PP |
3229 | goto error; |
3230 | } | |
3231 | ||
3232 | if (init_implicit_component_args(&implicit_trimmer_args, | |
fd5f8053 | 3233 | "filter.utils.trimmer", false)) { |
9009cc24 PP |
3234 | goto error; |
3235 | } | |
3236 | ||
ce141536 | 3237 | all_names = bt_value_map_create(); |
9009cc24 | 3238 | if (!all_names) { |
4b39e5c9 | 3239 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
3240 | goto error; |
3241 | } | |
3242 | ||
ce141536 | 3243 | run_args = bt_value_array_create(); |
9009cc24 | 3244 | if (!run_args) { |
4b39e5c9 | 3245 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
3246 | goto error; |
3247 | } | |
3248 | ||
4186b466 SM |
3249 | component_arg_for_run = g_string_new(NULL); |
3250 | if (!component_arg_for_run) { | |
4b39e5c9 | 3251 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
3252 | goto error; |
3253 | } | |
3254 | ||
8d5247bd PP |
3255 | non_opts = bt_value_array_create(); |
3256 | if (!non_opts) { | |
4b39e5c9 | 3257 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
bf5b7748 SM |
3258 | goto error; |
3259 | } | |
3260 | ||
8d5247bd PP |
3261 | non_opt_params = bt_value_array_create(); |
3262 | if (!non_opt_params) { | |
459f81ca SM |
3263 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
3264 | goto error; | |
3265 | } | |
3266 | ||
8d5247bd PP |
3267 | non_opt_loglevels = bt_value_array_create(); |
3268 | if (!non_opt_loglevels) { | |
b6a0ac97 SM |
3269 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
3270 | goto error; | |
3271 | } | |
3272 | ||
a1040187 SM |
3273 | if (auto_source_discovery_init(&auto_disc) != 0) { |
3274 | goto error; | |
3275 | } | |
3276 | ||
3277 | discovered_source_args = | |
3278 | g_ptr_array_new_with_free_func((GDestroyNotify) destroy_implicit_component_args); | |
3279 | if (!discovered_source_args) { | |
3280 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); | |
3281 | goto error; | |
3282 | } | |
3283 | ||
3284 | auto_disc_comp_name = g_string_new(NULL); | |
3285 | if (!auto_disc_comp_name) { | |
3286 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); | |
3287 | goto error; | |
3288 | } | |
3289 | ||
9009cc24 PP |
3290 | /* |
3291 | * First pass: collect all arguments which need to be passed | |
3292 | * as is to the run command. This pass can also add --name | |
3293 | * arguments if needed to automatically name unnamed component | |
99854915 | 3294 | * instances. |
9009cc24 | 3295 | */ |
a87d2977 SM |
3296 | argpar_parse_ret = bt_argpar_parse(argc, argv, convert_options, true); |
3297 | if (argpar_parse_ret.error) { | |
3298 | BT_CLI_LOGE_APPEND_CAUSE( | |
3299 | "While parsing `convert` command's command-line arguments: %s", | |
3300 | argpar_parse_ret.error->str); | |
9009cc24 PP |
3301 | goto error; |
3302 | } | |
3303 | ||
a87d2977 SM |
3304 | if (help_option_is_specified(&argpar_parse_ret)) { |
3305 | print_convert_usage(stdout); | |
3306 | *retcode = -1; | |
3307 | BT_OBJECT_PUT_REF_AND_RESET(cfg); | |
3308 | goto end; | |
3309 | } | |
9009cc24 | 3310 | |
a87d2977 SM |
3311 | for (i = 0; i < argpar_parse_ret.items->len; i++) { |
3312 | struct bt_argpar_item *argpar_item = | |
3313 | g_ptr_array_index(argpar_parse_ret.items, i); | |
3314 | struct bt_argpar_item_opt *argpar_item_opt; | |
9009cc24 PP |
3315 | char *name = NULL; |
3316 | char *plugin_name = NULL; | |
3317 | char *comp_cls_name = NULL; | |
a87d2977 | 3318 | const char *arg; |
9009cc24 | 3319 | |
551b5a52 SM |
3320 | if (argpar_item->type == BT_ARGPAR_ITEM_TYPE_OPT) { |
3321 | argpar_item_opt = (struct bt_argpar_item_opt *) argpar_item; | |
3322 | arg = argpar_item_opt->arg; | |
fd5f8053 | 3323 | |
551b5a52 SM |
3324 | switch (argpar_item_opt->descr->id) { |
3325 | case OPT_COMPONENT: | |
3326 | { | |
3327 | bt_component_class_type type; | |
9009cc24 | 3328 | |
551b5a52 | 3329 | current_item_type = CONVERT_CURRENT_ITEM_TYPE_COMPONENT; |
9009cc24 | 3330 | |
551b5a52 SM |
3331 | /* Parse the argument */ |
3332 | plugin_comp_cls_names(arg, &name, &plugin_name, | |
3333 | &comp_cls_name, &type); | |
3334 | if (!plugin_name || !comp_cls_name) { | |
4186b466 | 3335 | BT_CLI_LOGE_APPEND_CAUSE( |
551b5a52 SM |
3336 | "Invalid format for --component option's argument:\n %s", |
3337 | arg); | |
4186b466 SM |
3338 | goto error; |
3339 | } | |
3340 | ||
551b5a52 SM |
3341 | if (name) { |
3342 | /* | |
3343 | * Name was given by the user, verify it isn't | |
3344 | * taken. | |
3345 | */ | |
3346 | if (bt_value_map_has_entry(all_names, name)) { | |
3347 | BT_CLI_LOGE_APPEND_CAUSE( | |
3348 | "Duplicate component instance name:\n %s", | |
3349 | name); | |
3350 | goto error; | |
3351 | } | |
3352 | ||
3353 | name_gstr = g_string_new(name); | |
3354 | if (!name_gstr) { | |
3355 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); | |
3356 | goto error; | |
3357 | } | |
3358 | ||
3359 | g_string_assign(component_arg_for_run, arg); | |
3360 | } else { | |
3361 | /* Name not given by user, generate one. */ | |
3362 | name_gstr = get_component_auto_name(arg, all_names); | |
3363 | if (!name_gstr) { | |
3364 | goto error; | |
3365 | } | |
3366 | ||
3367 | g_string_printf(component_arg_for_run, "%s:%s", | |
3368 | name_gstr->str, arg); | |
3369 | } | |
3370 | ||
3371 | if (bt_value_array_append_string_element(run_args, | |
3372 | "--component")) { | |
4186b466 SM |
3373 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
3374 | goto error; | |
3375 | } | |
3376 | ||
551b5a52 SM |
3377 | if (bt_value_array_append_string_element(run_args, |
3378 | component_arg_for_run->str)) { | |
3379 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); | |
4186b466 SM |
3380 | goto error; |
3381 | } | |
9009cc24 | 3382 | |
551b5a52 SM |
3383 | /* |
3384 | * Remember this name globally, for the uniqueness of | |
3385 | * all component names. | |
3386 | */ | |
3387 | if (bt_value_map_insert_entry(all_names, | |
3388 | name_gstr->str, bt_value_null)) { | |
3389 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); | |
3390 | goto error; | |
3391 | } | |
fd5f8053 | 3392 | |
551b5a52 SM |
3393 | /* |
3394 | * Remember this name specifically for the type of the | |
3395 | * component. This is to create connection arguments. | |
3396 | * | |
3397 | * The list takes ownership of `name_gstr`. | |
3398 | */ | |
3399 | switch (type) { | |
3400 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
3401 | source_names = g_list_append(source_names, name_gstr); | |
3402 | break; | |
3403 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
3404 | filter_names = g_list_append(filter_names, name_gstr); | |
3405 | break; | |
3406 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
3407 | sink_names = g_list_append(sink_names, name_gstr); | |
3408 | break; | |
3409 | default: | |
3410 | abort(); | |
3411 | } | |
3412 | name_gstr = NULL; | |
3413 | ||
3414 | free(name); | |
3415 | free(plugin_name); | |
3416 | free(comp_cls_name); | |
3417 | name = NULL; | |
3418 | plugin_name = NULL; | |
3419 | comp_cls_name = NULL; | |
3420 | break; | |
4186b466 | 3421 | } |
551b5a52 | 3422 | case OPT_PARAMS: |
459f81ca SM |
3423 | if (current_item_type == CONVERT_CURRENT_ITEM_TYPE_COMPONENT) { |
3424 | /* | |
3425 | * The current item is a component (--component option), | |
3426 | * pass it directly to the run args. | |
3427 | */ | |
3428 | if (bt_value_array_append_string_element(run_args, | |
3429 | "--params")) { | |
3430 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); | |
3431 | goto error; | |
3432 | } | |
4186b466 | 3433 | |
459f81ca SM |
3434 | if (bt_value_array_append_string_element(run_args, arg)) { |
3435 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); | |
3436 | goto error; | |
3437 | } | |
8d5247bd PP |
3438 | } else if (current_item_type == CONVERT_CURRENT_ITEM_TYPE_NON_OPT) { |
3439 | /* | |
3440 | * The current item is a | |
3441 | * non-option argument, record | |
3442 | * it in `non_opt_params`. | |
3443 | */ | |
459f81ca | 3444 | bt_value *array; |
d98f6ad1 | 3445 | bt_value_array_append_element_status append_element_status; |
1270d0e9 | 3446 | uint64_t idx = bt_value_array_get_length(non_opt_params) - 1; |
9009cc24 | 3447 | |
8d5247bd | 3448 | array = bt_value_array_borrow_element_by_index(non_opt_params, idx); |
d98f6ad1 SM |
3449 | |
3450 | append_element_status = bt_value_array_append_string_element(array, arg); | |
3451 | if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { | |
3452 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); | |
3453 | goto error; | |
3454 | } | |
459f81ca SM |
3455 | } else { |
3456 | BT_CLI_LOGE_APPEND_CAUSE( | |
3457 | "No current component (--component option) or non-option argument of which to set parameters:\n %s", | |
3458 | arg); | |
551b5a52 SM |
3459 | goto error; |
3460 | } | |
4186b466 | 3461 | break; |
551b5a52 | 3462 | case OPT_LOG_LEVEL: |
b6a0ac97 SM |
3463 | if (current_item_type == CONVERT_CURRENT_ITEM_TYPE_COMPONENT) { |
3464 | if (bt_value_array_append_string_element(run_args, "--log-level")) { | |
3465 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); | |
3466 | goto error; | |
3467 | } | |
9009cc24 | 3468 | |
b6a0ac97 SM |
3469 | if (bt_value_array_append_string_element(run_args, arg)) { |
3470 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); | |
3471 | goto error; | |
3472 | } | |
8d5247bd | 3473 | } else if (current_item_type == CONVERT_CURRENT_ITEM_TYPE_NON_OPT) { |
1270d0e9 | 3474 | uint64_t idx = bt_value_array_get_length(non_opt_loglevels) - 1; |
b6a0ac97 | 3475 | bt_value *log_level_str_value; |
9009cc24 | 3476 | |
b6a0ac97 SM |
3477 | log_level_str_value = bt_value_string_create_init(arg); |
3478 | if (!log_level_str_value) { | |
3479 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); | |
3480 | goto error; | |
3481 | } | |
3482 | ||
8d5247bd | 3483 | if (bt_value_array_set_element_by_index(non_opt_loglevels, idx, |
b6a0ac97 SM |
3484 | log_level_str_value)) { |
3485 | bt_value_put_ref(log_level_str_value); | |
3486 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); | |
3487 | goto error; | |
3488 | } | |
3489 | } else { | |
3490 | BT_CLI_LOGE_APPEND_CAUSE( | |
3491 | "No current component (--component option) or non-option argument to assign a log level to:\n %s", | |
3492 | arg); | |
551b5a52 SM |
3493 | goto error; |
3494 | } | |
c4f81dc9 | 3495 | |
551b5a52 SM |
3496 | break; |
3497 | case OPT_RETRY_DURATION: | |
3498 | if (bt_value_array_append_string_element(run_args, | |
3499 | "--retry-duration")) { | |
3500 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); | |
3501 | goto error; | |
3502 | } | |
c4f81dc9 | 3503 | |
551b5a52 SM |
3504 | if (bt_value_array_append_string_element(run_args, arg)) { |
3505 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); | |
3506 | goto error; | |
3507 | } | |
3508 | break; | |
3509 | case OPT_BEGIN: | |
3510 | case OPT_CLOCK_CYCLES: | |
3511 | case OPT_CLOCK_DATE: | |
3512 | case OPT_CLOCK_FORCE_CORRELATE: | |
3513 | case OPT_CLOCK_GMT: | |
3514 | case OPT_CLOCK_OFFSET: | |
3515 | case OPT_CLOCK_OFFSET_NS: | |
3516 | case OPT_CLOCK_SECONDS: | |
3517 | case OPT_COLOR: | |
3518 | case OPT_DEBUG: | |
3519 | case OPT_DEBUG_INFO: | |
3520 | case OPT_DEBUG_INFO_DIR: | |
3521 | case OPT_DEBUG_INFO_FULL_PATH: | |
3522 | case OPT_DEBUG_INFO_TARGET_PREFIX: | |
3523 | case OPT_END: | |
3524 | case OPT_FIELDS: | |
3525 | case OPT_INPUT_FORMAT: | |
3526 | case OPT_NAMES: | |
3527 | case OPT_NO_DELTA: | |
3528 | case OPT_OUTPUT_FORMAT: | |
3529 | case OPT_OUTPUT: | |
3530 | case OPT_RUN_ARGS: | |
3531 | case OPT_RUN_ARGS_0: | |
3532 | case OPT_STREAM_INTERSECTION: | |
3533 | case OPT_TIMERANGE: | |
3534 | case OPT_VERBOSE: | |
3535 | /* Ignore in this pass */ | |
3536 | break; | |
3537 | default: | |
3538 | BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).", | |
3539 | argpar_item_opt->descr->id); | |
9009cc24 PP |
3540 | goto error; |
3541 | } | |
551b5a52 SM |
3542 | } else if (argpar_item->type == BT_ARGPAR_ITEM_TYPE_NON_OPT) { |
3543 | struct bt_argpar_item_non_opt *argpar_item_non_opt; | |
3544 | bt_value_array_append_element_status append_status; | |
9009cc24 | 3545 | |
8d5247bd | 3546 | current_item_type = CONVERT_CURRENT_ITEM_TYPE_NON_OPT; |
9009cc24 | 3547 | |
551b5a52 SM |
3548 | argpar_item_non_opt = (struct bt_argpar_item_non_opt *) argpar_item; |
3549 | ||
8d5247bd | 3550 | append_status = bt_value_array_append_string_element(non_opts, |
551b5a52 SM |
3551 | argpar_item_non_opt->arg); |
3552 | if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { | |
4b39e5c9 | 3553 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
3554 | goto error; |
3555 | } | |
459f81ca | 3556 | |
1b8e1443 PP |
3557 | append_status = bt_value_array_append_empty_array_element( |
3558 | non_opt_params, NULL); | |
459f81ca SM |
3559 | if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
3560 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); | |
3561 | goto error; | |
3562 | } | |
b6a0ac97 | 3563 | |
8d5247bd | 3564 | append_status = bt_value_array_append_element(non_opt_loglevels, bt_value_null); |
b6a0ac97 SM |
3565 | if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
3566 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); | |
3567 | goto error; | |
3568 | } | |
551b5a52 SM |
3569 | } else { |
3570 | abort(); | |
9009cc24 | 3571 | } |
9009cc24 PP |
3572 | } |
3573 | ||
9009cc24 PP |
3574 | /* |
3575 | * Second pass: transform the convert-specific options and | |
3576 | * arguments into implicit component instances for the run | |
3577 | * command. | |
3578 | */ | |
a87d2977 SM |
3579 | for (i = 0; i < argpar_parse_ret.items->len; i++) { |
3580 | struct bt_argpar_item *argpar_item = | |
3581 | g_ptr_array_index(argpar_parse_ret.items, i); | |
3582 | struct bt_argpar_item_opt *argpar_item_opt; | |
3583 | const char *arg; | |
9009cc24 | 3584 | |
a87d2977 SM |
3585 | if (argpar_item->type != BT_ARGPAR_ITEM_TYPE_OPT) { |
3586 | continue; | |
3587 | } | |
9009cc24 | 3588 | |
a87d2977 SM |
3589 | argpar_item_opt = (struct bt_argpar_item_opt *) argpar_item; |
3590 | arg = argpar_item_opt->arg; | |
9009cc24 | 3591 | |
a87d2977 | 3592 | switch (argpar_item_opt->descr->id) { |
9009cc24 PP |
3593 | case OPT_BEGIN: |
3594 | if (trimmer_has_begin) { | |
3595 | printf("At --begin option: --begin or --timerange option already specified\n %s\n", | |
3596 | arg); | |
3597 | goto error; | |
3598 | } | |
3599 | ||
3600 | trimmer_has_begin = true; | |
e7ad156c | 3601 | ret = append_implicit_component_extra_param( |
9009cc24 PP |
3602 | &implicit_trimmer_args, "begin", arg); |
3603 | implicit_trimmer_args.exists = true; | |
3604 | if (ret) { | |
3605 | goto error; | |
3606 | } | |
3607 | break; | |
3608 | case OPT_END: | |
3609 | if (trimmer_has_end) { | |
3610 | printf("At --end option: --end or --timerange option already specified\n %s\n", | |
3611 | arg); | |
3612 | goto error; | |
3613 | } | |
3614 | ||
3615 | trimmer_has_end = true; | |
e7ad156c | 3616 | ret = append_implicit_component_extra_param( |
9009cc24 PP |
3617 | &implicit_trimmer_args, "end", arg); |
3618 | implicit_trimmer_args.exists = true; | |
3619 | if (ret) { | |
3620 | goto error; | |
3621 | } | |
3622 | break; | |
3623 | case OPT_TIMERANGE: | |
3624 | { | |
3625 | char *begin; | |
3626 | char *end; | |
3627 | ||
3628 | if (trimmer_has_begin || trimmer_has_end) { | |
3629 | printf("At --timerange option: --begin, --end, or --timerange option already specified\n %s\n", | |
3630 | arg); | |
3631 | goto error; | |
3632 | } | |
3633 | ||
3634 | ret = split_timerange(arg, &begin, &end); | |
3635 | if (ret) { | |
4b39e5c9 | 3636 | BT_CLI_LOGE_APPEND_CAUSE("Invalid --timerange option's argument: expecting BEGIN,END or [BEGIN,END]:\n %s", |
9009cc24 PP |
3637 | arg); |
3638 | goto error; | |
3639 | } | |
3640 | ||
e7ad156c | 3641 | ret = append_implicit_component_extra_param( |
9009cc24 | 3642 | &implicit_trimmer_args, "begin", begin); |
e7ad156c | 3643 | ret |= append_implicit_component_extra_param( |
9009cc24 PP |
3644 | &implicit_trimmer_args, "end", end); |
3645 | implicit_trimmer_args.exists = true; | |
3646 | free(begin); | |
3647 | free(end); | |
3648 | if (ret) { | |
3649 | goto error; | |
3650 | } | |
3651 | break; | |
3652 | } | |
3653 | case OPT_CLOCK_CYCLES: | |
3654 | append_implicit_component_param( | |
3655 | &implicit_text_args, "clock-cycles", "yes"); | |
3656 | implicit_text_args.exists = true; | |
3657 | break; | |
3658 | case OPT_CLOCK_DATE: | |
3659 | append_implicit_component_param( | |
3660 | &implicit_text_args, "clock-date", "yes"); | |
3661 | implicit_text_args.exists = true; | |
3662 | break; | |
3663 | case OPT_CLOCK_FORCE_CORRELATE: | |
3664 | append_implicit_component_param( | |
53ac3428 PP |
3665 | &implicit_muxer_args, |
3666 | "assume-absolute-clock-classes", "yes"); | |
9009cc24 PP |
3667 | break; |
3668 | case OPT_CLOCK_GMT: | |
3669 | append_implicit_component_param( | |
3670 | &implicit_text_args, "clock-gmt", "yes"); | |
af47368e | 3671 | append_implicit_component_param( |
480c0b45 | 3672 | &implicit_trimmer_args, "gmt", "yes"); |
9009cc24 PP |
3673 | implicit_text_args.exists = true; |
3674 | break; | |
3675 | case OPT_CLOCK_OFFSET: | |
a1040187 SM |
3676 | if (ctf_fs_source_clock_class_offset_arg) { |
3677 | BT_CLI_LOGE_APPEND_CAUSE("Duplicate --clock-offset option\n"); | |
3678 | goto error; | |
3679 | } | |
3680 | ||
3681 | ctf_fs_source_clock_class_offset_arg = g_strdup(arg); | |
3682 | if (!ctf_fs_source_clock_class_offset_arg) { | |
3683 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); | |
3684 | goto error; | |
3685 | } | |
9009cc24 PP |
3686 | break; |
3687 | case OPT_CLOCK_OFFSET_NS: | |
a1040187 SM |
3688 | if (ctf_fs_source_clock_class_offset_ns_arg) { |
3689 | BT_CLI_LOGE_APPEND_CAUSE("Duplicate --clock-offset-ns option\n"); | |
3690 | goto error; | |
3691 | } | |
3692 | ||
3693 | ctf_fs_source_clock_class_offset_ns_arg = g_strdup(arg); | |
3694 | if (!ctf_fs_source_clock_class_offset_ns_arg) { | |
3695 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); | |
3696 | goto error; | |
3697 | } | |
9009cc24 PP |
3698 | break; |
3699 | case OPT_CLOCK_SECONDS: | |
3700 | append_implicit_component_param( | |
3701 | &implicit_text_args, "clock-seconds", "yes"); | |
3702 | implicit_text_args.exists = true; | |
3703 | break; | |
3704 | case OPT_COLOR: | |
9009cc24 | 3705 | implicit_text_args.exists = true; |
e7ad156c PP |
3706 | ret = append_implicit_component_extra_param( |
3707 | &implicit_text_args, "color", arg); | |
9009cc24 PP |
3708 | if (ret) { |
3709 | goto error; | |
3710 | } | |
3711 | break; | |
8e765000 PP |
3712 | case OPT_DEBUG_INFO: |
3713 | implicit_debug_info_args.exists = true; | |
9009cc24 PP |
3714 | break; |
3715 | case OPT_DEBUG_INFO_DIR: | |
e7ad156c PP |
3716 | implicit_debug_info_args.exists = true; |
3717 | ret = append_implicit_component_extra_param( | |
395a08d0 | 3718 | &implicit_debug_info_args, "debug-info-dir", arg); |
9009cc24 PP |
3719 | if (ret) { |
3720 | goto error; | |
3721 | } | |
3722 | break; | |
3723 | case OPT_DEBUG_INFO_FULL_PATH: | |
e7ad156c | 3724 | implicit_debug_info_args.exists = true; |
9009cc24 PP |
3725 | append_implicit_component_param( |
3726 | &implicit_debug_info_args, "full-path", "yes"); | |
3727 | break; | |
3728 | case OPT_DEBUG_INFO_TARGET_PREFIX: | |
e7ad156c PP |
3729 | implicit_debug_info_args.exists = true; |
3730 | ret = append_implicit_component_extra_param( | |
9009cc24 PP |
3731 | &implicit_debug_info_args, |
3732 | "target-prefix", arg); | |
3733 | if (ret) { | |
3734 | goto error; | |
3735 | } | |
3736 | break; | |
3737 | case OPT_FIELDS: | |
3738 | { | |
8eee8ea2 | 3739 | bt_value *fields = fields_from_arg(arg); |
9009cc24 PP |
3740 | |
3741 | if (!fields) { | |
3742 | goto error; | |
3743 | } | |
3744 | ||
e7ad156c | 3745 | implicit_text_args.exists = true; |
9009cc24 PP |
3746 | ret = insert_flat_params_from_array( |
3747 | implicit_text_args.params_arg, | |
ce141536 | 3748 | fields, "field"); |
8c6884d9 | 3749 | bt_value_put_ref(fields); |
9009cc24 PP |
3750 | if (ret) { |
3751 | goto error; | |
3752 | } | |
3753 | break; | |
3754 | } | |
3755 | case OPT_NAMES: | |
3756 | { | |
8eee8ea2 | 3757 | bt_value *names = names_from_arg(arg); |
9009cc24 PP |
3758 | |
3759 | if (!names) { | |
3760 | goto error; | |
3761 | } | |
3762 | ||
e7ad156c | 3763 | implicit_text_args.exists = true; |
9009cc24 PP |
3764 | ret = insert_flat_params_from_array( |
3765 | implicit_text_args.params_arg, | |
ce141536 | 3766 | names, "name"); |
8c6884d9 | 3767 | bt_value_put_ref(names); |
9009cc24 PP |
3768 | if (ret) { |
3769 | goto error; | |
3770 | } | |
3771 | break; | |
3772 | } | |
3773 | case OPT_NO_DELTA: | |
3774 | append_implicit_component_param( | |
3775 | &implicit_text_args, "no-delta", "yes"); | |
3776 | implicit_text_args.exists = true; | |
3777 | break; | |
3778 | case OPT_INPUT_FORMAT: | |
3779 | if (got_input_format_opt) { | |
4b39e5c9 | 3780 | BT_CLI_LOGE_APPEND_CAUSE("Duplicate --input-format option."); |
9009cc24 PP |
3781 | goto error; |
3782 | } | |
3783 | ||
3784 | got_input_format_opt = true; | |
3785 | ||
3786 | if (strcmp(arg, "ctf") == 0) { | |
a1040187 SM |
3787 | auto_source_discovery_restrict_plugin_name = "ctf"; |
3788 | auto_source_discovery_restrict_component_class_name = "fs"; | |
9009cc24 | 3789 | } else if (strcmp(arg, "lttng-live") == 0) { |
a1040187 SM |
3790 | auto_source_discovery_restrict_plugin_name = "ctf"; |
3791 | auto_source_discovery_restrict_component_class_name = "lttng-live"; | |
9009cc24 PP |
3792 | implicit_lttng_live_args.exists = true; |
3793 | } else { | |
4b39e5c9 | 3794 | BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy input format:\n %s", |
9009cc24 PP |
3795 | arg); |
3796 | goto error; | |
3797 | } | |
3798 | break; | |
3799 | case OPT_OUTPUT_FORMAT: | |
3800 | if (got_output_format_opt) { | |
4b39e5c9 | 3801 | BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output-format option."); |
9009cc24 PP |
3802 | goto error; |
3803 | } | |
3804 | ||
3805 | got_output_format_opt = true; | |
3806 | ||
3807 | if (strcmp(arg, "text") == 0) { | |
3808 | implicit_text_args.exists = true; | |
e7ad156c PP |
3809 | } else if (strcmp(arg, "ctf") == 0) { |
3810 | implicit_ctf_output_args.exists = true; | |
9009cc24 PP |
3811 | } else if (strcmp(arg, "dummy") == 0) { |
3812 | implicit_dummy_args.exists = true; | |
3813 | } else if (strcmp(arg, "ctf-metadata") == 0) { | |
3814 | print_ctf_metadata = true; | |
3815 | } else { | |
4b39e5c9 | 3816 | BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy output format:\n %s", |
9009cc24 PP |
3817 | arg); |
3818 | goto error; | |
3819 | } | |
3820 | break; | |
3821 | case OPT_OUTPUT: | |
e7ad156c | 3822 | if (output) { |
4b39e5c9 | 3823 | BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output option"); |
e7ad156c PP |
3824 | goto error; |
3825 | } | |
3826 | ||
3827 | output = strdup(arg); | |
3828 | if (!output) { | |
4b39e5c9 | 3829 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); |
9009cc24 PP |
3830 | goto error; |
3831 | } | |
3832 | break; | |
3833 | case OPT_RUN_ARGS: | |
3834 | if (print_run_args_0) { | |
4b39e5c9 | 3835 | BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0."); |
9009cc24 PP |
3836 | goto error; |
3837 | } | |
3838 | ||
3839 | print_run_args = true; | |
3840 | break; | |
3841 | case OPT_RUN_ARGS_0: | |
3842 | if (print_run_args) { | |
4b39e5c9 | 3843 | BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0."); |
9009cc24 PP |
3844 | goto error; |
3845 | } | |
3846 | ||
3847 | print_run_args_0 = true; | |
3848 | break; | |
3849 | case OPT_STREAM_INTERSECTION: | |
b5fe3e00 | 3850 | /* |
9e534aae PP |
3851 | * Applies to all traces implementing the |
3852 | * babeltrace.trace-info query. | |
b5fe3e00 JG |
3853 | */ |
3854 | stream_intersection_mode = true; | |
9009cc24 PP |
3855 | break; |
3856 | case OPT_VERBOSE: | |
d9029925 SM |
3857 | *default_log_level = |
3858 | logging_level_min(*default_log_level, BT_LOG_INFO); | |
9009cc24 PP |
3859 | break; |
3860 | case OPT_DEBUG: | |
d9029925 SM |
3861 | *default_log_level = |
3862 | logging_level_min(*default_log_level, BT_LOG_TRACE); | |
c4f81dc9 PP |
3863 | break; |
3864 | default: | |
9009cc24 PP |
3865 | break; |
3866 | } | |
9009cc24 PP |
3867 | } |
3868 | ||
d9029925 SM |
3869 | set_auto_log_levels(default_log_level); |
3870 | ||
9e503aa9 PP |
3871 | /* |
3872 | * Legacy behaviour: --verbose used to make the `text` output | |
3873 | * format print more information. --verbose is now equivalent to | |
8a46ff40 PP |
3874 | * the INFO log level, which is why we compare to `BT_LOG_INFO` |
3875 | * here. | |
9e503aa9 | 3876 | */ |
c4f81dc9 | 3877 | if (*default_log_level == BT_LOG_INFO) { |
9e503aa9 PP |
3878 | append_implicit_component_param(&implicit_text_args, |
3879 | "verbose", "yes"); | |
3880 | } | |
3881 | ||
9009cc24 PP |
3882 | /* Print CTF metadata or print LTTng live sessions */ |
3883 | if (print_ctf_metadata) { | |
8d5247bd | 3884 | const bt_value *bt_val_non_opt; |
94023a1c | 3885 | |
8d5247bd | 3886 | if (bt_value_array_is_empty(non_opts)) { |
4b39e5c9 | 3887 | BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf-metadata specified without a path."); |
9009cc24 PP |
3888 | goto error; |
3889 | } | |
3890 | ||
1270d0e9 | 3891 | if (bt_value_array_get_length(non_opts) > 1) { |
4b39e5c9 | 3892 | BT_CLI_LOGE_APPEND_CAUSE("Too many paths specified for --output-format=ctf-metadata."); |
94023a1c PP |
3893 | goto error; |
3894 | } | |
3895 | ||
9009cc24 PP |
3896 | cfg = bt_config_print_ctf_metadata_create(plugin_paths); |
3897 | if (!cfg) { | |
3898 | goto error; | |
3899 | } | |
3900 | ||
8d5247bd | 3901 | bt_val_non_opt = bt_value_array_borrow_element_by_index_const(non_opts, 0); |
9009cc24 | 3902 | g_string_assign(cfg->cmd_data.print_ctf_metadata.path, |
8d5247bd | 3903 | bt_value_string_get(bt_val_non_opt)); |
a107deea PP |
3904 | |
3905 | if (output) { | |
3906 | g_string_assign( | |
3907 | cfg->cmd_data.print_ctf_metadata.output_path, | |
3908 | output); | |
3909 | } | |
3910 | ||
9009cc24 PP |
3911 | goto end; |
3912 | } | |
3913 | ||
3914 | /* | |
e7ad156c PP |
3915 | * If -o ctf was specified, make sure an output path (--output) |
3916 | * was also specified. --output does not imply -o ctf because | |
3917 | * it's also used for the default, implicit -o text if -o ctf | |
3918 | * is not specified. | |
3919 | */ | |
3920 | if (implicit_ctf_output_args.exists) { | |
3921 | if (!output) { | |
4b39e5c9 | 3922 | BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf specified without --output (trace output path)."); |
e7ad156c PP |
3923 | goto error; |
3924 | } | |
3925 | ||
3926 | /* | |
3927 | * At this point we know that -o ctf AND --output were | |
3928 | * specified. Make sure that no options were specified | |
3929 | * which would imply -o text because --output would be | |
3930 | * ambiguous in this case. For example, this is wrong: | |
3931 | * | |
142ac9b0 | 3932 | * babeltrace2 --names=all -o ctf --output=/tmp/path my-trace |
e7ad156c PP |
3933 | * |
3934 | * because --names=all implies -o text, and --output | |
3935 | * could apply to both the sink.text.pretty and | |
3936 | * sink.ctf.fs implicit components. | |
3937 | */ | |
3938 | if (implicit_text_args.exists) { | |
4b39e5c9 | 3939 | BT_CLI_LOGE_APPEND_CAUSE("Ambiguous --output option: --output-format=ctf specified but another option implies --output-format=text."); |
e7ad156c PP |
3940 | goto error; |
3941 | } | |
3942 | } | |
3943 | ||
3944 | /* | |
3945 | * If -o dummy and -o ctf were not specified, and if there are | |
3946 | * no explicit sink components, then use an implicit | |
3947 | * `sink.text.pretty` component. | |
9009cc24 | 3948 | */ |
e7ad156c PP |
3949 | if (!implicit_dummy_args.exists && !implicit_ctf_output_args.exists && |
3950 | !sink_names) { | |
9009cc24 PP |
3951 | implicit_text_args.exists = true; |
3952 | } | |
3953 | ||
e7ad156c PP |
3954 | /* |
3955 | * Set implicit `sink.text.pretty` or `sink.ctf.fs` component's | |
3956 | * `path` parameter if --output was specified. | |
3957 | */ | |
3958 | if (output) { | |
3959 | if (implicit_text_args.exists) { | |
3960 | append_implicit_component_extra_param(&implicit_text_args, | |
3961 | "path", output); | |
3962 | } else if (implicit_ctf_output_args.exists) { | |
3963 | append_implicit_component_extra_param(&implicit_ctf_output_args, | |
3964 | "path", output); | |
3965 | } | |
3966 | } | |
3967 | ||
8d5247bd | 3968 | /* Decide where the non-option argument(s) go */ |
1270d0e9 | 3969 | if (bt_value_array_get_length(non_opts) > 0) { |
9009cc24 | 3970 | if (implicit_lttng_live_args.exists) { |
8d5247bd | 3971 | const bt_value *bt_val_non_opt; |
94023a1c | 3972 | |
1270d0e9 | 3973 | if (bt_value_array_get_length(non_opts) > 1) { |
4b39e5c9 | 3974 | BT_CLI_LOGE_APPEND_CAUSE("Too many URLs specified for --input-format=lttng-live."); |
94023a1c PP |
3975 | goto error; |
3976 | } | |
3977 | ||
8d5247bd | 3978 | bt_val_non_opt = bt_value_array_borrow_element_by_index_const(non_opts, 0); |
9009cc24 | 3979 | lttng_live_url_parts = |
8d5247bd | 3980 | bt_common_parse_lttng_live_url(bt_value_string_get(bt_val_non_opt), |
94b828f3 | 3981 | error_buf, sizeof(error_buf)); |
9009cc24 | 3982 | if (!lttng_live_url_parts.proto) { |
4b39e5c9 | 3983 | BT_CLI_LOGE_APPEND_CAUSE("Invalid LTTng live URL format: %s.", |
9009cc24 PP |
3984 | error_buf); |
3985 | goto error; | |
3986 | } | |
3987 | ||
3988 | if (!lttng_live_url_parts.session_name) { | |
3989 | /* Print LTTng live sessions */ | |
3990 | cfg = bt_config_print_lttng_live_sessions_create( | |
3991 | plugin_paths); | |
3992 | if (!cfg) { | |
3993 | goto error; | |
3994 | } | |
3995 | ||
9009cc24 | 3996 | g_string_assign(cfg->cmd_data.print_lttng_live_sessions.url, |
8d5247bd | 3997 | bt_value_string_get(bt_val_non_opt)); |
a107deea PP |
3998 | |
3999 | if (output) { | |
4000 | g_string_assign( | |
4001 | cfg->cmd_data.print_lttng_live_sessions.output_path, | |
4002 | output); | |
4003 | } | |
4004 | ||
9009cc24 PP |
4005 | goto end; |
4006 | } | |
4007 | ||
2df75e06 PP |
4008 | live_inputs_array_val = bt_value_array_create(); |
4009 | if (!live_inputs_array_val) { | |
4010 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); | |
4011 | goto error; | |
4012 | } | |
4013 | ||
4014 | if (bt_value_array_append_string_element( | |
4015 | live_inputs_array_val, | |
4016 | bt_value_string_get(bt_val_non_opt))) { | |
4017 | BT_CLI_LOGE_APPEND_CAUSE_OOM(); | |
4018 | goto error; | |
4019 | } | |
4020 | ||
4021 | ret = append_parameter_to_args( | |
4022 | implicit_lttng_live_args.extra_params, | |
4023 | "inputs", live_inputs_array_val); | |
9009cc24 PP |
4024 | if (ret) { |
4025 | goto error; | |
4026 | } | |
1e20174d FD |
4027 | |
4028 | ret = append_implicit_component_extra_param( | |
4029 | &implicit_lttng_live_args, | |
4030 | "session-not-found-action", "end"); | |
4031 | if (ret) { | |
4032 | goto error; | |
4033 | } | |
9009cc24 | 4034 | } else { |
a1040187 | 4035 | int status; |
77a5caaf SM |
4036 | size_t plugin_count; |
4037 | const bt_plugin **plugins; | |
4038 | const bt_plugin *plugin; | |
a1040187 | 4039 | |
77a5caaf SM |
4040 | status = require_loaded_plugins(plugin_paths); |
4041 | if (status != 0) { | |
4042 | goto error; | |
4043 | } | |
4044 | ||
4045 | if (auto_source_discovery_restrict_plugin_name) { | |
4046 | plugin_count = 1; | |
4047 | plugin = find_loaded_plugin(auto_source_discovery_restrict_plugin_name); | |
4048 | plugins = &plugin; | |
4049 | } else { | |
4050 | plugin_count = get_loaded_plugins_count(); | |
4051 | plugins = borrow_loaded_plugins(); | |
4052 | } | |
4053 | ||
4054 | status = auto_discover_source_components(non_opts, plugins, plugin_count, | |
a1040187 | 4055 | auto_source_discovery_restrict_component_class_name, |
3f8644ec | 4056 | *default_log_level, &auto_disc, interrupter); |
a1040187 SM |
4057 | |
4058 | if (status != 0) { | |
3f8644ec SM |
4059 | if (status == AUTO_SOURCE_DISCOVERY_STATUS_INTERRUPTED) { |
4060 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN( | |
4061 | "Babeltrace CLI", "Automatic source discovery interrupted by the user"); | |
4062 | } | |
9009cc24 PP |
4063 | goto error; |
4064 | } | |
a1040187 | 4065 | |
459f81ca | 4066 | status = create_implicit_component_args_from_auto_discovered_sources( |
8d5247bd | 4067 | &auto_disc, non_opt_params, non_opt_loglevels, |
b6a0ac97 | 4068 | discovered_source_args); |
459f81ca SM |
4069 | if (status != 0) { |
4070 | goto error; | |
4071 | } | |
9009cc24 PP |
4072 | } |
4073 | } | |
4074 | ||
a1040187 SM |
4075 | /* If --clock-offset was given, apply it to any src.ctf.fs component. */ |
4076 | if (ctf_fs_source_clock_class_offset_arg) { | |
4077 | int n; | |
4078 | ||
4079 | n = append_multiple_implicit_components_param( | |
4080 | discovered_source_args, "source.ctf.fs", "clock-class-offset-s", | |
4081 | ctf_fs_source_clock_class_offset_arg); | |
4082 | ||
4083 | if (n == 0) { | |
4084 | BT_CLI_LOGE_APPEND_CAUSE("--clock-offset specified, but no source.ctf.fs component instantiated."); | |
4085 | goto error; | |
4086 | } | |
9009cc24 PP |
4087 | } |
4088 | ||
a1040187 SM |
4089 | /* If --clock-offset-ns was given, apply it to any src.ctf.fs component. */ |
4090 | if (ctf_fs_source_clock_class_offset_ns_arg) { | |
4091 | int n; | |
4092 | ||
4093 | n = append_multiple_implicit_components_param( | |
4094 | discovered_source_args, "source.ctf.fs", "clock-class-offset-ns", | |
4095 | ctf_fs_source_clock_class_offset_ns_arg); | |
4096 | ||
4097 | if (n == 0) { | |
4098 | BT_CLI_LOGE_APPEND_CAUSE("--clock-offset-ns specified, but no source.ctf.fs component instantiated."); | |
4099 | goto error; | |
4100 | } | |
9009cc24 PP |
4101 | } |
4102 | ||
a1040187 | 4103 | /* |
8d5247bd PP |
4104 | * If the implicit `source.ctf.lttng-live` component exists, |
4105 | * make sure there's at least one non-option argument (which is | |
4106 | * the URL). | |
a1040187 | 4107 | */ |
8d5247bd | 4108 | if (implicit_lttng_live_args.exists && bt_value_array_is_empty(non_opts)) { |
4b39e5c9 | 4109 | BT_CLI_LOGE_APPEND_CAUSE("Missing URL for implicit `%s` component.", |
fd5f8053 | 4110 | implicit_lttng_live_args.comp_arg->str); |
9009cc24 PP |
4111 | goto error; |
4112 | } | |
4113 | ||
4114 | /* Assign names to implicit components */ | |
a1040187 SM |
4115 | for (i = 0; i < discovered_source_args->len; i++) { |
4116 | struct implicit_component_args *args; | |
4117 | int j; | |
4118 | ||
4119 | args = discovered_source_args->pdata[i]; | |
4120 | ||
4121 | g_string_printf(auto_disc_comp_name, "auto-disc-%s", args->comp_arg->str); | |
4122 | ||
4123 | /* Give it a name like `auto-disc-src-ctf-fs`. */ | |
4124 | for (j = 0; j < auto_disc_comp_name->len; j++) { | |
4125 | if (auto_disc_comp_name->str[j] == '.') { | |
4126 | auto_disc_comp_name->str[j] = '-'; | |
4127 | } | |
4128 | } | |
4129 | ||
4130 | ret = assign_name_to_implicit_component(args, | |
4131 | auto_disc_comp_name->str, all_names, &source_names, true); | |
4132 | if (ret) { | |
4133 | goto error; | |
4134 | } | |
9009cc24 PP |
4135 | } |
4136 | ||
4137 | ret = assign_name_to_implicit_component(&implicit_lttng_live_args, | |
4138 | "lttng-live", all_names, &source_names, true); | |
4139 | if (ret) { | |
4140 | goto error; | |
4141 | } | |
4142 | ||
4143 | ret = assign_name_to_implicit_component(&implicit_text_args, | |
4144 | "pretty", all_names, &sink_names, true); | |
4145 | if (ret) { | |
4146 | goto error; | |
4147 | } | |
4148 | ||
e7ad156c PP |
4149 | ret = assign_name_to_implicit_component(&implicit_ctf_output_args, |
4150 | "sink-ctf-fs", all_names, &sink_names, true); | |
4151 | if (ret) { | |
4152 | goto error; | |
4153 | } | |
4154 | ||
9009cc24 PP |
4155 | ret = assign_name_to_implicit_component(&implicit_dummy_args, |
4156 | "dummy", all_names, &sink_names, true); | |
4157 | if (ret) { | |
4158 | goto error; | |
4159 | } | |
4160 | ||
4161 | ret = assign_name_to_implicit_component(&implicit_muxer_args, | |
4162 | "muxer", all_names, NULL, false); | |
4163 | if (ret) { | |
4164 | goto error; | |
4165 | } | |
4166 | ||
4167 | ret = assign_name_to_implicit_component(&implicit_trimmer_args, | |
4168 | "trimmer", all_names, NULL, false); | |
4169 | if (ret) { | |
4170 | goto error; | |
4171 | } | |
4172 | ||
4173 | ret = assign_name_to_implicit_component(&implicit_debug_info_args, | |
4174 | "debug-info", all_names, NULL, false); | |
4175 | if (ret) { | |
4176 | goto error; | |
4177 | } | |
4178 | ||
4179 | /* Make sure there's at least one source and one sink */ | |
4180 | if (!source_names) { | |
4b39e5c9 | 4181 | BT_CLI_LOGE_APPEND_CAUSE("No source component."); |
9009cc24 PP |
4182 | goto error; |
4183 | } | |
4184 | ||
4185 | if (!sink_names) { | |
4b39e5c9 | 4186 | BT_CLI_LOGE_APPEND_CAUSE("No sink component."); |
9009cc24 PP |
4187 | goto error; |
4188 | } | |
4189 | ||
8e9c6210 PP |
4190 | /* Make sure there's a single sink component */ |
4191 | if (g_list_length(sink_names) != 1) { | |
4192 | BT_CLI_LOGE_APPEND_CAUSE( | |
4193 | "More than one sink component specified."); | |
4194 | goto error; | |
4195 | } | |
4196 | ||
9009cc24 PP |
4197 | /* |
4198 | * Prepend the muxer, the trimmer, and the debug info to the | |
4199 | * filter chain so that we have: | |
4200 | * | |
4201 | * sources -> muxer -> [trimmer] -> [debug info] -> | |
4202 | * [user filters] -> sinks | |
4203 | */ | |
4204 | if (implicit_debug_info_args.exists) { | |
4205 | if (g_list_prepend_gstring(&filter_names, | |
4206 | implicit_debug_info_args.name_arg->str)) { | |
4207 | goto error; | |
4208 | } | |
4209 | } | |
4210 | ||
4211 | if (implicit_trimmer_args.exists) { | |
4212 | if (g_list_prepend_gstring(&filter_names, | |
4213 | implicit_trimmer_args.name_arg->str)) { | |
4214 | goto error; | |
4215 | } | |
4216 | } | |
4217 | ||
4218 | if (g_list_prepend_gstring(&filter_names, | |
4219 | implicit_muxer_args.name_arg->str)) { | |
4220 | goto error; | |
4221 | } | |
4222 | ||
4223 | /* | |
4224 | * Append the equivalent run arguments for the implicit | |
4225 | * components. | |
4226 | */ | |
a1040187 SM |
4227 | for (i = 0; i < discovered_source_args->len; i++) { |
4228 | struct implicit_component_args *args = | |
4229 | discovered_source_args->pdata[i]; | |
4230 | ||
4231 | ret = append_run_args_for_implicit_component(args, run_args); | |
4232 | if (ret) { | |
4233 | goto error; | |
4234 | } | |
9009cc24 PP |
4235 | } |
4236 | ||
fd5f8053 | 4237 | ret = append_run_args_for_implicit_component(&implicit_lttng_live_args, |
9009cc24 PP |
4238 | run_args); |
4239 | if (ret) { | |
4240 | goto error; | |
4241 | } | |
4242 | ||
fd5f8053 PP |
4243 | ret = append_run_args_for_implicit_component(&implicit_text_args, |
4244 | run_args); | |
9009cc24 PP |
4245 | if (ret) { |
4246 | goto error; | |
4247 | } | |
4248 | ||
e7ad156c PP |
4249 | ret = append_run_args_for_implicit_component(&implicit_ctf_output_args, |
4250 | run_args); | |
4251 | if (ret) { | |
4252 | goto error; | |
4253 | } | |
4254 | ||
fd5f8053 PP |
4255 | ret = append_run_args_for_implicit_component(&implicit_dummy_args, |
4256 | run_args); | |
9009cc24 PP |
4257 | if (ret) { |
4258 | goto error; | |
4259 | } | |
4260 | ||
fd5f8053 PP |
4261 | ret = append_run_args_for_implicit_component(&implicit_muxer_args, |
4262 | run_args); | |
9009cc24 PP |
4263 | if (ret) { |
4264 | goto error; | |
4265 | } | |
4266 | ||
fd5f8053 | 4267 | ret = append_run_args_for_implicit_component(&implicit_trimmer_args, |
9009cc24 PP |
4268 | run_args); |
4269 | if (ret) { | |
4270 | goto error; | |
4271 | } | |
4272 | ||
fd5f8053 | 4273 | ret = append_run_args_for_implicit_component(&implicit_debug_info_args, |
9009cc24 PP |
4274 | run_args); |
4275 | if (ret) { | |
4276 | goto error; | |
4277 | } | |
4278 | ||
4279 | /* Auto-connect components */ | |
4280 | ret = convert_auto_connect(run_args, source_names, filter_names, | |
4281 | sink_names); | |
4282 | if (ret) { | |
4b39e5c9 | 4283 | BT_CLI_LOGE_APPEND_CAUSE("Cannot auto-connect components."); |
9009cc24 PP |
4284 | goto error; |
4285 | } | |
4286 | ||
4287 | /* | |
4288 | * We have all the run command arguments now. Depending on | |
4289 | * --run-args, we pass this to the run command or print them | |
4290 | * here. | |
4291 | */ | |
4292 | if (print_run_args || print_run_args_0) { | |
f9692f14 | 4293 | if (stream_intersection_mode) { |
4b39e5c9 | 4294 | BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --stream-intersection with --run-args or --run-args-0."); |
f9692f14 PP |
4295 | goto error; |
4296 | } | |
4297 | ||
1270d0e9 | 4298 | for (i = 0; i < bt_value_array_get_length(run_args); i++) { |
8eee8ea2 | 4299 | const bt_value *arg_value = |
ce141536 PP |
4300 | bt_value_array_borrow_element_by_index(run_args, |
4301 | i); | |
9009cc24 PP |
4302 | const char *arg; |
4303 | GString *quoted = NULL; | |
4304 | const char *arg_to_print; | |
4305 | ||
8b45963b | 4306 | BT_ASSERT(arg_value); |
b5cdc106 | 4307 | arg = bt_value_string_get(arg_value); |
9009cc24 PP |
4308 | |
4309 | if (print_run_args) { | |
4310 | quoted = bt_common_shell_quote(arg, true); | |
4311 | if (!quoted) { | |
4312 | goto error; | |
4313 | } | |
4314 | ||
4315 | arg_to_print = quoted->str; | |
4316 | } else { | |
4317 | arg_to_print = arg; | |
4318 | } | |
4319 | ||
4320 | printf("%s", arg_to_print); | |
4321 | ||
4322 | if (quoted) { | |
4323 | g_string_free(quoted, TRUE); | |
4324 | } | |
4325 | ||
1270d0e9 | 4326 | if (i < bt_value_array_get_length(run_args) - 1) { |
9009cc24 PP |
4327 | if (print_run_args) { |
4328 | putchar(' '); | |
4329 | } else { | |
4330 | putchar('\0'); | |
4331 | } | |
4332 | } | |
4333 | } | |
4334 | ||
4335 | *retcode = -1; | |
8138bfe1 | 4336 | BT_OBJECT_PUT_REF_AND_RESET(cfg); |
9009cc24 PP |
4337 | goto end; |
4338 | } | |
4339 | ||
ce141536 | 4340 | cfg = bt_config_run_from_args_array(run_args, retcode, |
d36258d0 | 4341 | plugin_paths, *default_log_level); |
fc11b6a6 PP |
4342 | if (!cfg) { |
4343 | goto error; | |
4344 | } | |
4345 | ||
b5fe3e00 | 4346 | cfg->cmd_data.run.stream_intersection_mode = stream_intersection_mode; |
9009cc24 PP |
4347 | goto end; |
4348 | ||
4349 | error: | |
4350 | *retcode = 1; | |
8138bfe1 | 4351 | BT_OBJECT_PUT_REF_AND_RESET(cfg); |
9009cc24 PP |
4352 | |
4353 | end: | |
a87d2977 | 4354 | bt_argpar_parse_ret_fini(&argpar_parse_ret); |
9009cc24 | 4355 | |
e7ad156c | 4356 | free(output); |
9009cc24 | 4357 | |
4186b466 SM |
4358 | if (component_arg_for_run) { |
4359 | g_string_free(component_arg_for_run, TRUE); | |
9009cc24 PP |
4360 | } |
4361 | ||
4186b466 SM |
4362 | if (name_gstr) { |
4363 | g_string_free(name_gstr, TRUE); | |
9009cc24 PP |
4364 | } |
4365 | ||
2df75e06 | 4366 | bt_value_put_ref(live_inputs_array_val); |
8c6884d9 PP |
4367 | bt_value_put_ref(run_args); |
4368 | bt_value_put_ref(all_names); | |
9009cc24 PP |
4369 | destroy_glist_of_gstring(source_names); |
4370 | destroy_glist_of_gstring(filter_names); | |
4371 | destroy_glist_of_gstring(sink_names); | |
8d5247bd PP |
4372 | bt_value_put_ref(non_opt_params); |
4373 | bt_value_put_ref(non_opt_loglevels); | |
4374 | bt_value_put_ref(non_opts); | |
94023a1c PP |
4375 | finalize_implicit_component_args(&implicit_ctf_output_args); |
4376 | finalize_implicit_component_args(&implicit_lttng_live_args); | |
4377 | finalize_implicit_component_args(&implicit_dummy_args); | |
4378 | finalize_implicit_component_args(&implicit_text_args); | |
4379 | finalize_implicit_component_args(&implicit_debug_info_args); | |
4380 | finalize_implicit_component_args(&implicit_muxer_args); | |
4381 | finalize_implicit_component_args(&implicit_trimmer_args); | |
9009cc24 | 4382 | bt_common_destroy_lttng_live_url_parts(<tng_live_url_parts); |
a1040187 SM |
4383 | auto_source_discovery_fini(&auto_disc); |
4384 | ||
4385 | if (discovered_source_args) { | |
4386 | g_ptr_array_free(discovered_source_args, TRUE); | |
4387 | } | |
4388 | ||
4389 | g_free(ctf_fs_source_clock_class_offset_arg); | |
4390 | g_free(ctf_fs_source_clock_class_offset_ns_arg); | |
4391 | ||
4392 | if (auto_disc_comp_name) { | |
4393 | g_string_free(auto_disc_comp_name, TRUE); | |
4394 | } | |
4395 | ||
9009cc24 PP |
4396 | return cfg; |
4397 | } | |
4398 | ||
4399 | /* | |
4400 | * Prints the Babeltrace 2.x general usage. | |
4401 | */ | |
4402 | static | |
4403 | void print_gen_usage(FILE *fp) | |
4404 | { | |
142ac9b0 | 4405 | fprintf(fp, "Usage: babeltrace2 [GENERAL OPTIONS] [COMMAND] [COMMAND ARGUMENTS]\n"); |
9009cc24 PP |
4406 | fprintf(fp, "\n"); |
4407 | fprintf(fp, "General options:\n"); | |
4408 | fprintf(fp, "\n"); | |
983c486e | 4409 | fprintf(fp, " -d, --debug Enable debug mode (same as --log-level=T)\n"); |
d36258d0 | 4410 | fprintf(fp, " -h, --help Show this help and quit\n"); |
983c486e | 4411 | fprintf(fp, " -l, --log-level=LVL Set the default log level to LVL (`N`, `T`, `D`,\n"); |
d36258d0 PP |
4412 | fprintf(fp, " `I`, `W` (default), `E`, or `F`)\n"); |
4413 | fprintf(fp, " --omit-home-plugin-path Omit home plugins from plugin search path\n"); | |
4414 | fprintf(fp, " (~/.local/lib/babeltrace2/plugins)\n"); | |
4415 | fprintf(fp, " --omit-system-plugin-path Omit system plugins from plugin search path\n"); | |
4416 | fprintf(fp, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n"); | |
4417 | fprintf(fp, " dynamic plugins can be loaded\n"); | |
4418 | fprintf(fp, " -v, --verbose Enable verbose mode (same as --log-level=I)\n"); | |
4419 | fprintf(fp, " -V, --version Show version and quit\n"); | |
9009cc24 PP |
4420 | fprintf(fp, "\n"); |
4421 | fprintf(fp, "Available commands:\n"); | |
4422 | fprintf(fp, "\n"); | |
4423 | fprintf(fp, " convert Convert and trim traces (default)\n"); | |
4424 | fprintf(fp, " help Get help for a plugin or a component class\n"); | |
4425 | fprintf(fp, " list-plugins List available plugins and their content\n"); | |
4426 | fprintf(fp, " query Query objects from a component class\n"); | |
4427 | fprintf(fp, " run Build a processing graph and run it\n"); | |
4428 | fprintf(fp, "\n"); | |
142ac9b0 | 4429 | fprintf(fp, "Use `babeltrace2 COMMAND --help` to show the help of COMMAND.\n"); |
9009cc24 PP |
4430 | } |
4431 | ||
4432 | struct bt_config *bt_config_cli_args_create(int argc, const char *argv[], | |
d36258d0 PP |
4433 | int *retcode, bool omit_system_plugin_path, |
4434 | bool omit_home_plugin_path, | |
3f8644ec SM |
4435 | const bt_value *initial_plugin_paths, |
4436 | const bt_interrupter *interrupter) | |
9009cc24 PP |
4437 | { |
4438 | struct bt_config *config = NULL; | |
9009cc24 | 4439 | int i; |
091cc3eb SM |
4440 | int top_level_argc; |
4441 | const char **top_level_argv; | |
9009cc24 | 4442 | int command_argc = -1; |
091cc3eb | 4443 | const char **command_argv = NULL; |
9009cc24 | 4444 | const char *command_name = NULL; |
c4f81dc9 | 4445 | int default_log_level = -1; |
091cc3eb | 4446 | struct bt_argpar_parse_ret argpar_parse_ret = { 0 }; |
d36258d0 | 4447 | bt_value *plugin_paths = NULL; |
091cc3eb SM |
4448 | |
4449 | /* Top-level option descriptions. */ | |
4450 | static const struct bt_argpar_opt_descr descrs[] = { | |
4451 | { OPT_DEBUG, 'd', "debug", false }, | |
4452 | { OPT_HELP, 'h', "help", false }, | |
4453 | { OPT_LOG_LEVEL, 'l', "log-level", true }, | |
4454 | { OPT_VERBOSE, 'v', "verbose", false }, | |
4455 | { OPT_VERSION, 'V', "version", false}, | |
d36258d0 PP |
4456 | { OPT_OMIT_HOME_PLUGIN_PATH, '\0', "omit-home-plugin-path", false }, |
4457 | { OPT_OMIT_SYSTEM_PLUGIN_PATH, '\0', "omit-system-plugin-path", false }, | |
4458 | { OPT_PLUGIN_PATH, '\0', "plugin-path", true }, | |
091cc3eb SM |
4459 | BT_ARGPAR_OPT_DESCR_SENTINEL |
4460 | }; | |
9009cc24 PP |
4461 | |
4462 | enum command_type { | |
4463 | COMMAND_TYPE_NONE = -1, | |
4464 | COMMAND_TYPE_RUN = 0, | |
4465 | COMMAND_TYPE_CONVERT, | |
4466 | COMMAND_TYPE_LIST_PLUGINS, | |
4467 | COMMAND_TYPE_HELP, | |
4468 | COMMAND_TYPE_QUERY, | |
4469 | } command_type = COMMAND_TYPE_NONE; | |
4470 | ||
4471 | *retcode = -1; | |
4472 | ||
4473 | if (!initial_plugin_paths) { | |
d36258d0 PP |
4474 | plugin_paths = bt_value_array_create(); |
4475 | if (!plugin_paths) { | |
4476 | goto error; | |
9009cc24 PP |
4477 | } |
4478 | } else { | |
d36258d0 PP |
4479 | bt_value_copy_status copy_status = bt_value_copy( |
4480 | initial_plugin_paths, &plugin_paths); | |
4481 | if (copy_status) { | |
4482 | goto error; | |
4483 | } | |
4484 | } | |
4485 | ||
4486 | BT_ASSERT(plugin_paths); | |
4487 | ||
4488 | /* | |
4489 | * The `BABELTRACE_PLUGIN_PATH` paths take precedence over the | |
4490 | * `--plugin-path` option's paths, so append it now before | |
4491 | * parsing the general options. | |
4492 | */ | |
4493 | if (append_env_var_plugin_paths(plugin_paths)) { | |
4494 | goto error; | |
9009cc24 PP |
4495 | } |
4496 | ||
4497 | if (argc <= 1) { | |
329e245b PP |
4498 | print_version(); |
4499 | puts(""); | |
9009cc24 PP |
4500 | print_gen_usage(stdout); |
4501 | goto end; | |
4502 | } | |
4503 | ||
091cc3eb SM |
4504 | /* Skip first argument, the name of the program. */ |
4505 | top_level_argc = argc - 1; | |
4506 | top_level_argv = argv + 1; | |
4507 | argpar_parse_ret = bt_argpar_parse(top_level_argc, top_level_argv, | |
4508 | descrs, false); | |
9e503aa9 | 4509 | |
091cc3eb SM |
4510 | if (argpar_parse_ret.error) { |
4511 | BT_CLI_LOGE_APPEND_CAUSE( | |
4512 | "While parsing command-line arguments: %s", | |
4513 | argpar_parse_ret.error->str); | |
4514 | goto error; | |
4515 | } | |
21b5b16a | 4516 | |
091cc3eb SM |
4517 | for (i = 0; i < argpar_parse_ret.items->len; i++) { |
4518 | struct bt_argpar_item *item; | |
4519 | ||
4520 | item = g_ptr_array_index(argpar_parse_ret.items, i); | |
4521 | ||
4522 | if (item->type == BT_ARGPAR_ITEM_TYPE_OPT) { | |
4523 | struct bt_argpar_item_opt *item_opt = | |
4524 | (struct bt_argpar_item_opt *) item; | |
4525 | ||
4526 | switch (item_opt->descr->id) { | |
4527 | case OPT_DEBUG: | |
d9029925 SM |
4528 | default_log_level = |
4529 | logging_level_min(default_log_level, BT_LOG_TRACE); | |
091cc3eb SM |
4530 | break; |
4531 | case OPT_VERBOSE: | |
d9029925 SM |
4532 | default_log_level = |
4533 | logging_level_min(default_log_level, BT_LOG_INFO); | |
091cc3eb SM |
4534 | break; |
4535 | case OPT_LOG_LEVEL: | |
d9029925 SM |
4536 | { |
4537 | int level = bt_log_get_level_from_string(item_opt->arg); | |
4538 | ||
4539 | if (level < 0) { | |
091cc3eb SM |
4540 | BT_CLI_LOGE_APPEND_CAUSE( |
4541 | "Invalid argument for --log-level option:\n %s", | |
4542 | item_opt->arg); | |
4543 | goto error; | |
4544 | } | |
d9029925 SM |
4545 | |
4546 | default_log_level = | |
4547 | logging_level_min(default_log_level, level); | |
091cc3eb | 4548 | break; |
d9029925 | 4549 | } |
d36258d0 PP |
4550 | case OPT_PLUGIN_PATH: |
4551 | if (bt_config_append_plugin_paths_check_setuid_setgid( | |
4552 | plugin_paths, item_opt->arg)) { | |
4553 | goto error; | |
4554 | } | |
4555 | break; | |
4556 | case OPT_OMIT_SYSTEM_PLUGIN_PATH: | |
4557 | omit_system_plugin_path = true; | |
4558 | break; | |
4559 | case OPT_OMIT_HOME_PLUGIN_PATH: | |
4560 | omit_home_plugin_path = true; | |
4561 | break; | |
091cc3eb SM |
4562 | case OPT_VERSION: |
4563 | print_version(); | |
4564 | goto end; | |
4565 | case OPT_HELP: | |
4566 | print_gen_usage(stdout); | |
4567 | goto end; | |
9e503aa9 | 4568 | } |
091cc3eb SM |
4569 | } else if (item->type == BT_ARGPAR_ITEM_TYPE_NON_OPT) { |
4570 | struct bt_argpar_item_non_opt *item_non_opt = | |
4571 | (struct bt_argpar_item_non_opt *) item; | |
9009cc24 PP |
4572 | /* |
4573 | * First unknown argument: is it a known command | |
4574 | * name? | |
4575 | */ | |
091cc3eb SM |
4576 | command_argc = |
4577 | top_level_argc - item_non_opt->orig_index - 1; | |
4578 | command_argv = | |
4579 | &top_level_argv[item_non_opt->orig_index + 1]; | |
9e503aa9 | 4580 | |
091cc3eb | 4581 | if (strcmp(item_non_opt->arg, "convert") == 0) { |
9009cc24 | 4582 | command_type = COMMAND_TYPE_CONVERT; |
091cc3eb | 4583 | } else if (strcmp(item_non_opt->arg, "list-plugins") == 0) { |
9009cc24 | 4584 | command_type = COMMAND_TYPE_LIST_PLUGINS; |
091cc3eb | 4585 | } else if (strcmp(item_non_opt->arg, "help") == 0) { |
9009cc24 | 4586 | command_type = COMMAND_TYPE_HELP; |
091cc3eb | 4587 | } else if (strcmp(item_non_opt->arg, "query") == 0) { |
9009cc24 | 4588 | command_type = COMMAND_TYPE_QUERY; |
091cc3eb | 4589 | } else if (strcmp(item_non_opt->arg, "run") == 0) { |
9009cc24 PP |
4590 | command_type = COMMAND_TYPE_RUN; |
4591 | } else { | |
4592 | /* | |
091cc3eb | 4593 | * Non-option argument, but not a known |
9e503aa9 PP |
4594 | * command name: assume the default |
4595 | * `convert` command. | |
9009cc24 PP |
4596 | */ |
4597 | command_type = COMMAND_TYPE_CONVERT; | |
9e503aa9 | 4598 | command_name = "convert"; |
091cc3eb SM |
4599 | command_argc++; |
4600 | command_argv--; | |
9009cc24 PP |
4601 | } |
4602 | break; | |
4603 | } | |
4604 | } | |
4605 | ||
4606 | if (command_type == COMMAND_TYPE_NONE) { | |
091cc3eb SM |
4607 | if (argpar_parse_ret.ingested_orig_args == top_level_argc) { |
4608 | /* | |
4609 | * We only got non-help, non-version general options | |
4610 | * like --verbose and --debug, without any other | |
4611 | * arguments, so we can't do anything useful: print the | |
4612 | * usage and quit. | |
4613 | */ | |
4614 | print_gen_usage(stdout); | |
4615 | goto end; | |
4616 | } | |
4617 | ||
9009cc24 | 4618 | /* |
091cc3eb SM |
4619 | * We stopped on an unknown option argument (and therefore |
4620 | * didn't see a command name). Assume `convert` command. | |
9009cc24 | 4621 | */ |
091cc3eb SM |
4622 | command_type = COMMAND_TYPE_CONVERT; |
4623 | command_name = "convert"; | |
4624 | command_argc = | |
4625 | top_level_argc - argpar_parse_ret.ingested_orig_args; | |
4626 | command_argv = | |
4627 | &top_level_argv[argpar_parse_ret.ingested_orig_args]; | |
9009cc24 PP |
4628 | } |
4629 | ||
8b45963b PP |
4630 | BT_ASSERT(command_argv); |
4631 | BT_ASSERT(command_argc >= 0); | |
9009cc24 | 4632 | |
c4f81dc9 | 4633 | /* |
d9029925 SM |
4634 | * For all commands other than `convert`, we now know the log level to |
4635 | * use, so we can apply it with `set_auto_log_levels`. | |
4636 | * | |
4637 | * The convert command has `--debug` and `--verbose` arguments that are | |
4638 | * equivalent to the top-level arguments of the same name. So after it | |
4639 | * has parsed its arguments, `bt_config_convert_from_args` calls | |
4640 | * `set_auto_log_levels` itself. | |
c4f81dc9 | 4641 | */ |
d9029925 SM |
4642 | if (command_type != COMMAND_TYPE_CONVERT) { |
4643 | set_auto_log_levels(&default_log_level); | |
c4f81dc9 PP |
4644 | } |
4645 | ||
d36258d0 PP |
4646 | /* |
4647 | * At this point, `plugin_paths` contains the initial plugin | |
4648 | * paths, the paths from the `BABELTRACE_PLUGIN_PATH` paths, and | |
4649 | * the paths from the `--plugin-path` option. | |
4650 | * | |
4651 | * Now append the user and system plugin paths. | |
4652 | */ | |
4653 | if (append_home_and_system_plugin_paths(plugin_paths, | |
4654 | omit_system_plugin_path, omit_home_plugin_path)) { | |
4655 | goto error; | |
4656 | } | |
4657 | ||
9009cc24 PP |
4658 | switch (command_type) { |
4659 | case COMMAND_TYPE_RUN: | |
4660 | config = bt_config_run_from_args(command_argc, command_argv, | |
d36258d0 | 4661 | retcode, plugin_paths, |
c4f81dc9 | 4662 | default_log_level); |
9009cc24 PP |
4663 | break; |
4664 | case COMMAND_TYPE_CONVERT: | |
4665 | config = bt_config_convert_from_args(command_argc, command_argv, | |
3f8644ec | 4666 | retcode, plugin_paths, &default_log_level, interrupter); |
9009cc24 PP |
4667 | break; |
4668 | case COMMAND_TYPE_LIST_PLUGINS: | |
4669 | config = bt_config_list_plugins_from_args(command_argc, | |
d36258d0 | 4670 | command_argv, retcode, plugin_paths); |
9009cc24 PP |
4671 | break; |
4672 | case COMMAND_TYPE_HELP: | |
4673 | config = bt_config_help_from_args(command_argc, | |
d36258d0 | 4674 | command_argv, retcode, plugin_paths, |
c4f81dc9 | 4675 | default_log_level); |
9009cc24 PP |
4676 | break; |
4677 | case COMMAND_TYPE_QUERY: | |
4678 | config = bt_config_query_from_args(command_argc, | |
d36258d0 | 4679 | command_argv, retcode, plugin_paths, |
c4f81dc9 | 4680 | default_log_level); |
9009cc24 PP |
4681 | break; |
4682 | default: | |
0fbb9a9f | 4683 | abort(); |
9009cc24 PP |
4684 | } |
4685 | ||
4686 | if (config) { | |
c9ecaa78 | 4687 | BT_ASSERT(default_log_level >= BT_LOG_TRACE); |
c4f81dc9 | 4688 | config->log_level = default_log_level; |
9009cc24 PP |
4689 | config->command_name = command_name; |
4690 | } | |
4691 | ||
091cc3eb SM |
4692 | goto end; |
4693 | ||
4694 | error: | |
4695 | *retcode = 1; | |
4696 | ||
9009cc24 | 4697 | end: |
091cc3eb | 4698 | bt_argpar_parse_ret_fini(&argpar_parse_ret); |
d36258d0 | 4699 | bt_value_put_ref(plugin_paths); |
9009cc24 PP |
4700 | return config; |
4701 | } |