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