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