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