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