lib: make bt_value_map_foreach_entry_{const_}func() return a status code
[babeltrace.git] / src / cli / babeltrace2.c
CommitLineData
34ac0e6c 1/*
64fa3fec
MD
2 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
3 *
4 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
34ac0e6c
MD
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.
c462e188
MD
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.
34ac0e6c 23 */
4c8bfb7e 24
ea9f8b29
PP
25#define BT_LOG_TAG "CLI"
26#include "logging.h"
27
3fadfbc0 28#include <babeltrace2/babeltrace.h>
578e048b 29#include "common/common.h"
a8ff38ef 30#include <unistd.h>
34ac0e6c 31#include <stdlib.h>
7f26a816
PP
32#include <string.h>
33#include <stdio.h>
33b34c43 34#include <glib.h>
dc3fffef 35#include <inttypes.h>
7cdc2bab 36#include <unistd.h>
5401f780 37#include <signal.h>
ec2c5e50
MJ
38#include "babeltrace2-cfg.h"
39#include "babeltrace2-cfg-cli-args.h"
40#include "babeltrace2-cfg-cli-args-default.h"
ebddaeb8 41#include "babeltrace2-log-level.h"
743138a3 42#include "babeltrace2-plugins.h"
3c729b9a 43#include "babeltrace2-query.h"
9009cc24
PP
44
45#define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH"
75a2cb9b 46#define NSEC_PER_SEC 1000000000LL
34ac0e6c 47
b5cd7d65
FD
48enum bt_cmd_status {
49 BT_CMD_STATUS_OK = 0,
50 BT_CMD_STATUS_ERROR = -1,
51 BT_CMD_STATUS_INTERRUPTED = -2,
52};
53
54static
55const char *bt_cmd_status_string(enum bt_cmd_status cmd_status)
56{
57 switch (cmd_status) {
58 case BT_CMD_STATUS_OK:
59 return "OK";
60 case BT_CMD_STATUS_ERROR:
61 return "ERROR";
62 case BT_CMD_STATUS_INTERRUPTED:
63 return "INTERRUPTED";
64 default:
65 bt_common_abort();
66 }
67}
68
9b4f9b42
PP
69/* Application's interrupter (owned by this) */
70static bt_interrupter *the_interrupter;
5401f780 71
65d3198f 72#ifdef __MINGW32__
a2d06fd5 73
65d3198f
MJ
74#include <windows.h>
75
76static
77BOOL WINAPI signal_handler(DWORD signal) {
9b4f9b42
PP
78 if (the_interrupter) {
79 bt_interrupter_set(the_interrupter);
65d3198f
MJ
80 }
81
65d3198f
MJ
82 return TRUE;
83}
84
5401f780 85static
65d3198f
MJ
86void set_signal_handler(void)
87{
88 if (!SetConsoleCtrlHandler(signal_handler, TRUE)) {
3f7d4d90 89 BT_LOGE("Failed to set the Ctrl+C handler.");
65d3198f
MJ
90 }
91}
a2d06fd5 92
65d3198f 93#else /* __MINGW32__ */
a2d06fd5 94
65d3198f
MJ
95static
96void signal_handler(int signum)
5401f780
PP
97{
98 if (signum != SIGINT) {
99 return;
100 }
101
9b4f9b42
PP
102 if (the_interrupter) {
103 bt_interrupter_set(the_interrupter);
c7eee084 104 }
5401f780
PP
105}
106
65d3198f
MJ
107static
108void set_signal_handler(void)
109{
110 struct sigaction new_action, old_action;
111
112 new_action.sa_handler = signal_handler;
113 sigemptyset(&new_action.sa_mask);
114 new_action.sa_flags = 0;
115 sigaction(SIGINT, NULL, &old_action);
116
117 if (old_action.sa_handler != SIG_IGN) {
118 sigaction(SIGINT, &new_action, NULL);
119 }
120}
a2d06fd5 121
65d3198f
MJ
122#endif /* __MINGW32__ */
123
c7eee084 124static
f4e38e70
PP
125int query(struct bt_config *cfg, const bt_component_class *comp_cls,
126 const char *obj, const bt_value *params,
127 const bt_value **user_result, const char **fail_reason)
c7eee084 128{
3c729b9a
PP
129 return cli_query(comp_cls, obj, params, cfg->log_level,
130 the_interrupter, user_result, fail_reason);
c7eee084
PP
131}
132
0d72b8c3 133typedef const void *(*plugin_borrow_comp_cls_func_t)(
b19ff26f 134 const bt_plugin *, const char *);
d94d92ac 135
33b34c43 136static
0d72b8c3 137const void *find_component_class_from_plugin(const char *plugin_name,
33b34c43 138 const char *comp_class_name,
d94d92ac 139 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func)
33b34c43 140{
0d72b8c3 141 const void *comp_class = NULL;
b19ff26f 142 const bt_plugin *plugin;
7213a328 143
3f7d4d90 144 BT_LOGI("Finding component class: plugin-name=\"%s\", "
d94d92ac 145 "comp-cls-name=\"%s\"", plugin_name, comp_class_name);
7213a328 146
743138a3 147 plugin = find_loaded_plugin(plugin_name);
33b34c43
PP
148 if (!plugin) {
149 goto end;
150 }
151
398454ed
PP
152 comp_class = plugin_borrow_comp_cls_func(plugin, comp_class_name);
153 bt_object_get_ref(comp_class);
c5b9b441 154 BT_PLUGIN_PUT_REF_AND_RESET(plugin);
7213a328 155
33b34c43 156end:
3f7d4d90
PP
157 if (comp_class) {
158 BT_LOGI("Found component class: plugin-name=\"%s\", "
159 "comp-cls-name=\"%s\"", plugin_name, comp_class_name);
160 } else {
161 BT_LOGI("Cannot find source component class: "
162 "plugin-name=\"%s\", comp-cls-name=\"%s\"",
163 plugin_name, comp_class_name);
7213a328
PP
164 }
165
33b34c43
PP
166 return comp_class;
167}
6c2f3ee5 168
d94d92ac 169static
b19ff26f 170const bt_component_class_source *find_source_component_class(
d94d92ac
PP
171 const char *plugin_name, const char *comp_class_name)
172{
0d72b8c3
PP
173 return (const void *) find_component_class_from_plugin(
174 plugin_name, comp_class_name,
d94d92ac 175 (plugin_borrow_comp_cls_func_t)
92fed4e1 176 bt_plugin_borrow_source_component_class_by_name_const);
d94d92ac
PP
177}
178
179static
b19ff26f 180const bt_component_class_filter *find_filter_component_class(
d94d92ac
PP
181 const char *plugin_name, const char *comp_class_name)
182{
0d72b8c3
PP
183 return (const void *) find_component_class_from_plugin(
184 plugin_name, comp_class_name,
d94d92ac 185 (plugin_borrow_comp_cls_func_t)
92fed4e1 186 bt_plugin_borrow_filter_component_class_by_name_const);
d94d92ac
PP
187}
188
189static
b19ff26f 190const bt_component_class_sink *find_sink_component_class(
d94d92ac
PP
191 const char *plugin_name, const char *comp_class_name)
192{
0d72b8c3 193 return (const void *) find_component_class_from_plugin(plugin_name,
d94d92ac
PP
194 comp_class_name,
195 (plugin_borrow_comp_cls_func_t)
92fed4e1 196 bt_plugin_borrow_sink_component_class_by_name_const);
d94d92ac
PP
197}
198
199static
b19ff26f 200const bt_component_class *find_component_class(const char *plugin_name,
d94d92ac 201 const char *comp_class_name,
4cdfc5e8 202 bt_component_class_type comp_class_type)
d94d92ac 203{
b19ff26f 204 const bt_component_class *comp_cls = NULL;
d94d92ac
PP
205
206 switch (comp_class_type) {
207 case BT_COMPONENT_CLASS_TYPE_SOURCE:
0d72b8c3 208 comp_cls = bt_component_class_source_as_component_class_const(find_source_component_class(plugin_name, comp_class_name));
d94d92ac
PP
209 break;
210 case BT_COMPONENT_CLASS_TYPE_FILTER:
0d72b8c3 211 comp_cls = bt_component_class_filter_as_component_class_const(find_filter_component_class(plugin_name, comp_class_name));
d94d92ac
PP
212 break;
213 case BT_COMPONENT_CLASS_TYPE_SINK:
0d72b8c3 214 comp_cls = bt_component_class_sink_as_component_class_const(find_sink_component_class(plugin_name, comp_class_name));
d94d92ac
PP
215 break;
216 default:
498e7994 217 bt_common_abort();
d94d92ac
PP
218 }
219
220 return comp_cls;
221}
222
c42c79ea 223static
7213a328 224void print_indent(FILE *fp, size_t indent)
c42c79ea
PP
225{
226 size_t i;
227
228 for (i = 0; i < indent; i++) {
7213a328 229 fprintf(fp, " ");
c42c79ea
PP
230 }
231}
232
87796884 233static
4cdfc5e8 234const char *component_type_str(bt_component_class_type type)
87796884
PP
235{
236 switch (type) {
237 case BT_COMPONENT_CLASS_TYPE_SOURCE:
238 return "source";
239 case BT_COMPONENT_CLASS_TYPE_SINK:
240 return "sink";
241 case BT_COMPONENT_CLASS_TYPE_FILTER:
242 return "filter";
87796884 243 default:
fd5f8053 244 return "(unknown)";
87796884
PP
245 }
246}
247
9009cc24
PP
248static
249void print_plugin_comp_cls_opt(FILE *fh, const char *plugin_name,
4cdfc5e8 250 const char *comp_cls_name, bt_component_class_type type)
87796884 251{
9009cc24
PP
252 GString *shell_plugin_name = NULL;
253 GString *shell_comp_cls_name = NULL;
87796884 254
f393c19b
PP
255 if (plugin_name) {
256 shell_plugin_name = bt_common_shell_quote(plugin_name, false);
257 if (!shell_plugin_name) {
258 goto end;
259 }
87796884
PP
260 }
261
9009cc24 262 shell_comp_cls_name = bt_common_shell_quote(comp_cls_name, false);
87796884
PP
263 if (!shell_comp_cls_name) {
264 goto end;
265 }
266
f393c19b 267 fprintf(fh, "'%s%s%s%s",
87796884 268 bt_common_color_bold(),
04609487 269 bt_common_color_fg_bright_cyan(),
87796884 270 component_type_str(type),
f393c19b
PP
271 bt_common_color_fg_default());
272
273 if (shell_plugin_name) {
274 fprintf(fh, ".%s%s%s",
275 bt_common_color_fg_blue(),
276 shell_plugin_name->str,
277 bt_common_color_fg_default());
278 }
279
280 fprintf(fh, ".%s%s%s'",
87796884 281 bt_common_color_fg_yellow(),
9009cc24 282 shell_comp_cls_name->str,
87796884
PP
283 bt_common_color_reset());
284
285end:
9009cc24
PP
286 if (shell_plugin_name) {
287 g_string_free(shell_plugin_name, TRUE);
288 }
289
290 if (shell_comp_cls_name) {
291 g_string_free(shell_comp_cls_name, TRUE);
292 }
87796884
PP
293}
294
c42c79ea 295static
b19ff26f 296void print_value(FILE *, const bt_value *, size_t);
c42c79ea 297
c1081aa6 298static
b19ff26f 299void print_value_rec(FILE *, const bt_value *, size_t);
7213a328 300
c42c79ea 301static
13763113
SM
302void print_map_value(const char *key, const bt_value *object, FILE *fp,
303 size_t indent)
c42c79ea 304{
13763113
SM
305 print_indent(fp, indent);
306 fprintf(fp, "%s: ", key);
f6ccaed9 307 BT_ASSERT(object);
290725f7
PP
308
309 if (bt_value_is_array(object) &&
310 bt_value_array_is_empty(object)) {
13763113
SM
311 fprintf(fp, "[ ]\n");
312 goto end;
290725f7
PP
313 }
314
315 if (bt_value_is_map(object) &&
316 bt_value_map_is_empty(object)) {
13763113
SM
317 fprintf(fp, "{ }\n");
318 goto end;
290725f7 319 }
c42c79ea 320
290725f7
PP
321 if (bt_value_is_array(object) ||
322 bt_value_is_map(object)) {
13763113 323 fprintf(fp, "\n");
290725f7 324 }
c42c79ea 325
13763113
SM
326 print_value_rec(fp, object, indent + 2);
327
328end:
329 return;
330}
331
332static
27c61ce8
PP
333bt_value_map_foreach_entry_const_func_status collect_map_keys(
334 const char *key, const bt_value *object, void *data)
13763113
SM
335{
336 GPtrArray *map_keys = data;
337
338 g_ptr_array_add(map_keys, (gpointer *) key);
339
27c61ce8 340 return BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_OK;
c42c79ea
PP
341}
342
13763113
SM
343static
344gint g_ptr_array_sort_strings(gconstpointer a, gconstpointer b) {
345 const char *s1 = *((const char **) a);
346 const char *s2 = *((const char **) b);
347
348 return g_strcmp0(s1, s2);
349}
350
c42c79ea 351static
b19ff26f 352void print_value_rec(FILE *fp, const bt_value *value, size_t indent)
c42c79ea 353{
c55a9f58 354 bt_bool bool_val;
c42c79ea 355 int64_t int_val;
fdd3a2da 356 uint64_t uint_val;
c42c79ea
PP
357 double dbl_val;
358 const char *str_val;
13763113 359 GPtrArray *map_keys = NULL;
c42c79ea 360
3eb16918 361 BT_ASSERT(value);
c42c79ea 362
c42c79ea
PP
363 switch (bt_value_get_type(value)) {
364 case BT_VALUE_TYPE_NULL:
7213a328 365 fprintf(fp, "%snull%s\n", bt_common_color_bold(),
c1081aa6 366 bt_common_color_reset());
c42c79ea
PP
367 break;
368 case BT_VALUE_TYPE_BOOL:
601b0d3c 369 bool_val = bt_value_bool_get(value);
7213a328 370 fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(),
04609487 371 bt_common_color_fg_bright_cyan(), bool_val ? "yes" : "no",
c1081aa6 372 bt_common_color_reset());
c42c79ea 373 break;
fdd3a2da 374 case BT_VALUE_TYPE_UNSIGNED_INTEGER:
9c08c816 375 uint_val = bt_value_integer_unsigned_get(value);
fdd3a2da 376 fprintf(fp, "%s%s%" PRIu64 "%s\n", bt_common_color_bold(),
04609487 377 bt_common_color_fg_bright_red(), uint_val,
fdd3a2da
PP
378 bt_common_color_reset());
379 break;
380 case BT_VALUE_TYPE_SIGNED_INTEGER:
9c08c816 381 int_val = bt_value_integer_signed_get(value);
7213a328 382 fprintf(fp, "%s%s%" PRId64 "%s\n", bt_common_color_bold(),
04609487 383 bt_common_color_fg_bright_red(), int_val,
c1081aa6 384 bt_common_color_reset());
c42c79ea 385 break;
a373bf69 386 case BT_VALUE_TYPE_REAL:
601b0d3c 387 dbl_val = bt_value_real_get(value);
7213a328 388 fprintf(fp, "%s%s%lf%s\n", bt_common_color_bold(),
04609487 389 bt_common_color_fg_bright_red(), dbl_val,
c1081aa6 390 bt_common_color_reset());
c42c79ea
PP
391 break;
392 case BT_VALUE_TYPE_STRING:
601b0d3c 393 str_val = bt_value_string_get(value);
7213a328 394 fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(),
04609487 395 bt_common_color_fg_bright_green(), str_val,
c1081aa6 396 bt_common_color_reset());
c42c79ea
PP
397 break;
398 case BT_VALUE_TYPE_ARRAY:
50233236 399 {
f80e9ec1 400 uint64_t i, size;
393729a6 401 size = bt_value_array_get_length(value);
290725f7 402 if (size == 0) {
7213a328
PP
403 print_indent(fp, indent);
404 fprintf(fp, "[ ]\n");
290725f7
PP
405 break;
406 }
c42c79ea
PP
407
408 for (i = 0; i < size; i++) {
b19ff26f 409 const bt_value *element =
05e21286 410 bt_value_array_borrow_element_by_index_const(
07208d85 411 value, i);
c42c79ea 412
7213a328
PP
413 print_indent(fp, indent);
414 fprintf(fp, "- ");
290725f7
PP
415
416 if (bt_value_is_array(element) &&
417 bt_value_array_is_empty(element)) {
7213a328 418 fprintf(fp, "[ ]\n");
290725f7
PP
419 continue;
420 }
421
422 if (bt_value_is_map(element) &&
423 bt_value_map_is_empty(element)) {
7213a328 424 fprintf(fp, "{ }\n");
290725f7
PP
425 continue;
426 }
427
428 if (bt_value_is_array(element) ||
429 bt_value_is_map(element)) {
7213a328 430 fprintf(fp, "\n");
290725f7
PP
431 }
432
7213a328 433 print_value_rec(fp, element, indent + 2);
c42c79ea 434 }
c42c79ea 435 break;
50233236 436 }
c42c79ea 437 case BT_VALUE_TYPE_MAP:
7213a328 438 {
13763113
SM
439 guint i;
440 bt_value_map_foreach_entry_const_status foreach_status;
7213a328 441
c42c79ea 442 if (bt_value_map_is_empty(value)) {
7213a328
PP
443 print_indent(fp, indent);
444 fprintf(fp, "{ }\n");
290725f7 445 break;
c42c79ea
PP
446 }
447
13763113
SM
448 map_keys = g_ptr_array_new();
449 if (!map_keys) {
450 BT_CLI_LOGE_APPEND_CAUSE("Failed to allocated on GPtrArray.");
451 goto end;
452 }
453
454 /*
455 * We want to print the map entries in a stable order. Collect
456 * all the map's keys in a GPtrArray, sort it, then print the
457 * entries in that order.
458 */
459 foreach_status = bt_value_map_foreach_entry_const(value,
460 collect_map_keys, map_keys);
461 if (foreach_status != BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_OK) {
462 BT_CLI_LOGE_APPEND_CAUSE("Failed to iterator on map value.");
463 goto end;
464 }
465
466 g_ptr_array_sort(map_keys, g_ptr_array_sort_strings);
467
468 for (i = 0; i < map_keys->len; i++) {
469 const char *map_key = g_ptr_array_index(map_keys, i);
470 const bt_value *map_value;
471
472 map_value = bt_value_map_borrow_entry_value_const(value, map_key);
473 BT_ASSERT(map_value);
474
475 print_map_value(map_key, map_value, fp, indent);
476 }
477
c42c79ea 478 break;
7213a328 479 }
c42c79ea 480 default:
498e7994 481 bt_common_abort();
c42c79ea 482 }
13763113
SM
483
484 goto end;
60a97734 485
13763113
SM
486end:
487 if (map_keys) {
488 g_ptr_array_free(map_keys, TRUE);
489 }
c42c79ea
PP
490}
491
c1081aa6 492static
b19ff26f 493void print_value(FILE *fp, const bt_value *value, size_t indent)
c1081aa6
PP
494{
495 if (!bt_value_is_array(value) && !bt_value_is_map(value)) {
7213a328 496 print_indent(fp, indent);
c1081aa6
PP
497 }
498
7213a328 499 print_value_rec(fp, value, indent);
c1081aa6
PP
500}
501
c42c79ea
PP
502static
503void print_bt_config_component(struct bt_config_component *bt_config_component)
504{
7213a328
PP
505 fprintf(stderr, " ");
506 print_plugin_comp_cls_opt(stderr, bt_config_component->plugin_name->str,
db0f160a 507 bt_config_component->comp_cls_name->str,
87796884 508 bt_config_component->type);
7213a328 509 fprintf(stderr, ":\n");
3b6cfcc5
PP
510
511 if (bt_config_component->instance_name->len > 0) {
7213a328 512 fprintf(stderr, " Name: %s\n",
3b6cfcc5
PP
513 bt_config_component->instance_name->str);
514 }
515
7213a328 516 fprintf(stderr, " Parameters:\n");
05e21286 517 print_value(stderr, bt_config_component->params, 8);
c42c79ea
PP
518}
519
520static
521void print_bt_config_components(GPtrArray *array)
522{
523 size_t i;
524
525 for (i = 0; i < array->len; i++) {
526 struct bt_config_component *cfg_component =
e5bc7f81 527 bt_config_get_component(array, i);
c42c79ea 528 print_bt_config_component(cfg_component);
65300d60 529 BT_OBJECT_PUT_REF_AND_RESET(cfg_component);
c42c79ea
PP
530 }
531}
532
290725f7 533static
b19ff26f 534void print_plugin_paths(const bt_value *plugin_paths)
290725f7 535{
7213a328
PP
536 fprintf(stderr, " Plugin paths:\n");
537 print_value(stderr, plugin_paths, 4);
290725f7
PP
538}
539
540static
db0f160a 541void print_cfg_run(struct bt_config *cfg)
290725f7 542{
ebba3338
PP
543 size_t i;
544
05e21286 545 print_plugin_paths(cfg->plugin_paths);
7213a328 546 fprintf(stderr, " Source component instances:\n");
db0f160a 547 print_bt_config_components(cfg->cmd_data.run.sources);
ebba3338 548
db0f160a 549 if (cfg->cmd_data.run.filters->len > 0) {
7213a328 550 fprintf(stderr, " Filter component instances:\n");
db0f160a 551 print_bt_config_components(cfg->cmd_data.run.filters);
ebba3338
PP
552 }
553
7213a328 554 fprintf(stderr, " Sink component instances:\n");
db0f160a 555 print_bt_config_components(cfg->cmd_data.run.sinks);
7213a328 556 fprintf(stderr, " Connections:\n");
ebba3338 557
db0f160a 558 for (i = 0; i < cfg->cmd_data.run.connections->len; i++) {
ebba3338 559 struct bt_config_connection *cfg_connection =
db0f160a 560 g_ptr_array_index(cfg->cmd_data.run.connections,
ebba3338
PP
561 i);
562
7213a328 563 fprintf(stderr, " %s%s%s -> %s%s%s\n",
9009cc24
PP
564 cfg_connection->upstream_comp_name->str,
565 cfg_connection->upstream_port_glob->len > 0 ? "." : "",
566 cfg_connection->upstream_port_glob->str,
567 cfg_connection->downstream_comp_name->str,
568 cfg_connection->downstream_port_glob->len > 0 ? "." : "",
569 cfg_connection->downstream_port_glob->str);
ebba3338 570 }
290725f7
PP
571}
572
573static
574void print_cfg_list_plugins(struct bt_config *cfg)
575{
05e21286 576 print_plugin_paths(cfg->plugin_paths);
290725f7
PP
577}
578
c1081aa6
PP
579static
580void print_cfg_help(struct bt_config *cfg)
581{
05e21286 582 print_plugin_paths(cfg->plugin_paths);
db0f160a
PP
583}
584
585static
586void print_cfg_print_ctf_metadata(struct bt_config *cfg)
587{
05e21286 588 print_plugin_paths(cfg->plugin_paths);
7213a328
PP
589 fprintf(stderr, " Path: %s\n",
590 cfg->cmd_data.print_ctf_metadata.path->str);
db0f160a
PP
591}
592
593static
594void print_cfg_print_lttng_live_sessions(struct bt_config *cfg)
595{
05e21286 596 print_plugin_paths(cfg->plugin_paths);
7213a328
PP
597 fprintf(stderr, " URL: %s\n",
598 cfg->cmd_data.print_lttng_live_sessions.url->str);
c1081aa6
PP
599}
600
601static
a67681c1 602void print_cfg_query(struct bt_config *cfg)
c1081aa6 603{
05e21286 604 print_plugin_paths(cfg->plugin_paths);
7213a328
PP
605 fprintf(stderr, " Object: `%s`\n", cfg->cmd_data.query.object->str);
606 fprintf(stderr, " Component class:\n");
a67681c1 607 print_bt_config_component(cfg->cmd_data.query.cfg_component);
c1081aa6
PP
608}
609
c42c79ea
PP
610static
611void print_cfg(struct bt_config *cfg)
612{
7213a328 613 if (!BT_LOG_ON_INFO) {
00447e45
PP
614 return;
615 }
616
3f7d4d90 617 BT_LOGI_STR("CLI configuration:");
290725f7
PP
618
619 switch (cfg->command) {
db0f160a
PP
620 case BT_CONFIG_COMMAND_RUN:
621 print_cfg_run(cfg);
290725f7
PP
622 break;
623 case BT_CONFIG_COMMAND_LIST_PLUGINS:
624 print_cfg_list_plugins(cfg);
c1081aa6
PP
625 break;
626 case BT_CONFIG_COMMAND_HELP:
627 print_cfg_help(cfg);
628 break;
a67681c1
PP
629 case BT_CONFIG_COMMAND_QUERY:
630 print_cfg_query(cfg);
290725f7 631 break;
db0f160a
PP
632 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA:
633 print_cfg_print_ctf_metadata(cfg);
634 break;
635 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS:
636 print_cfg_print_lttng_live_sessions(cfg);
637 break;
290725f7 638 default:
498e7994 639 bt_common_abort();
290725f7 640 }
c42c79ea
PP
641}
642
9009cc24 643static
b19ff26f 644void print_plugin_info(const bt_plugin *plugin)
22e22462
PP
645{
646 unsigned int major, minor, patch;
647 const char *extra;
4cdfc5e8 648 bt_property_availability version_avail;
22e22462
PP
649 const char *plugin_name;
650 const char *path;
651 const char *author;
652 const char *license;
653 const char *plugin_description;
654
655 plugin_name = bt_plugin_get_name(plugin);
656 path = bt_plugin_get_path(plugin);
657 author = bt_plugin_get_author(plugin);
658 license = bt_plugin_get_license(plugin);
659 plugin_description = bt_plugin_get_description(plugin);
9724cce9 660 version_avail = bt_plugin_get_version(plugin, &major, &minor,
22e22462
PP
661 &patch, &extra);
662 printf("%s%s%s%s:\n", bt_common_color_bold(),
04609487 663 bt_common_color_fg_bright_blue(), plugin_name,
22e22462 664 bt_common_color_reset());
a157fea9
MJ
665 if (path) {
666 printf(" %sPath%s: %s\n", bt_common_color_bold(),
667 bt_common_color_reset(), path);
668 } else {
669 puts(" Built-in");
670 }
22e22462 671
9724cce9 672 if (version_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE) {
22e22462
PP
673 printf(" %sVersion%s: %u.%u.%u",
674 bt_common_color_bold(), bt_common_color_reset(),
675 major, minor, patch);
676
677 if (extra) {
678 printf("%s", extra);
679 }
680
681 printf("\n");
682 }
683
684 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
685 bt_common_color_reset(),
686 plugin_description ? plugin_description : "(None)");
687 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
688 bt_common_color_reset(), author ? author : "(Unknown)");
689 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
690 bt_common_color_reset(),
691 license ? license : "(Unknown)");
692}
693
9009cc24 694static
b5cd7d65 695enum bt_cmd_status cmd_query(struct bt_config *cfg)
63ce0e1d 696{
db95fa29 697 int ret = 0;
b5cd7d65 698 enum bt_cmd_status cmd_status;
b19ff26f
PP
699 const bt_component_class *comp_cls = NULL;
700 const bt_value *results = NULL;
c7eee084 701 const char *fail_reason = NULL;
63ce0e1d 702
d94d92ac
PP
703 comp_cls = find_component_class(
704 cfg->cmd_data.query.cfg_component->plugin_name->str,
db0f160a 705 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
a67681c1 706 cfg->cmd_data.query.cfg_component->type);
63ce0e1d 707 if (!comp_cls) {
cee05105
PP
708 BT_CLI_LOGE_APPEND_CAUSE(
709 "Cannot find component class: plugin-name=\"%s\", "
7213a328
PP
710 "comp-cls-name=\"%s\", comp-cls-type=%d",
711 cfg->cmd_data.query.cfg_component->plugin_name->str,
712 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
713 cfg->cmd_data.query.cfg_component->type);
b5cd7d65 714 goto error;
63ce0e1d
PP
715 }
716
f4e38e70 717 ret = query(cfg, comp_cls, cfg->cmd_data.query.object->str,
05e21286 718 cfg->cmd_data.query.cfg_component->params,
da91b29a 719 &results, &fail_reason);
c7eee084 720 if (ret) {
16cbfe32
FD
721 BT_CLI_LOGE_APPEND_CAUSE(
722 "Failed to query component class: %s: plugin-name=\"%s\", "
723 "comp-cls-name=\"%s\", comp-cls-type=%d "
724 "object=\"%s\"", fail_reason,
725 cfg->cmd_data.query.cfg_component->plugin_name->str,
726 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
727 cfg->cmd_data.query.cfg_component->type,
728 cfg->cmd_data.query.object->str);
729 goto error;
63ce0e1d
PP
730 }
731
7213a328 732 print_value(stdout, results, 0);
b5cd7d65 733 cmd_status = BT_CMD_STATUS_OK;
c7eee084
PP
734 goto end;
735
b5cd7d65
FD
736error:
737 cmd_status = BT_CMD_STATUS_ERROR;
63ce0e1d
PP
738
739end:
c5b9b441
PP
740 bt_component_class_put_ref(comp_cls);
741 bt_value_put_ref(results);
b5cd7d65 742 return cmd_status;
63ce0e1d
PP
743}
744
d94d92ac
PP
745static
746void print_component_class_help(const char *plugin_name,
b19ff26f 747 const bt_component_class *comp_cls)
d94d92ac
PP
748{
749 const char *comp_class_name =
750 bt_component_class_get_name(comp_cls);
751 const char *comp_class_description =
752 bt_component_class_get_description(comp_cls);
753 const char *comp_class_help =
754 bt_component_class_get_help(comp_cls);
4cdfc5e8 755 bt_component_class_type type =
d94d92ac
PP
756 bt_component_class_get_type(comp_cls);
757
758 print_plugin_comp_cls_opt(stdout, plugin_name, comp_class_name, type);
759 printf("\n");
760 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
761 bt_common_color_reset(),
762 comp_class_description ? comp_class_description : "(None)");
763
764 if (comp_class_help) {
765 printf("\n%s\n", comp_class_help);
766 }
767}
768
9009cc24 769static
b5cd7d65 770enum bt_cmd_status cmd_help(struct bt_config *cfg)
22e22462 771{
b5cd7d65 772 enum bt_cmd_status cmd_status;
b19ff26f
PP
773 const bt_plugin *plugin = NULL;
774 const bt_component_class *needed_comp_cls = NULL;
22e22462 775
743138a3 776 plugin = find_loaded_plugin(cfg->cmd_data.help.cfg_component->plugin_name->str);
22e22462 777 if (!plugin) {
cee05105
PP
778 BT_CLI_LOGE_APPEND_CAUSE(
779 "Cannot find plugin: plugin-name=\"%s\"",
7213a328 780 cfg->cmd_data.help.cfg_component->plugin_name->str);
b5cd7d65 781 goto error;
22e22462
PP
782 }
783
784 print_plugin_info(plugin);
d94d92ac
PP
785 printf(" %sSource component classes%s: %d\n",
786 bt_common_color_bold(),
787 bt_common_color_reset(),
788 (int) bt_plugin_get_source_component_class_count(plugin));
789 printf(" %sFilter component classes%s: %d\n",
22e22462
PP
790 bt_common_color_bold(),
791 bt_common_color_reset(),
d94d92ac
PP
792 (int) bt_plugin_get_filter_component_class_count(plugin));
793 printf(" %sSink component classes%s: %d\n",
794 bt_common_color_bold(),
795 bt_common_color_reset(),
796 (int) bt_plugin_get_sink_component_class_count(plugin));
22e22462 797
d94d92ac
PP
798 if (strlen(cfg->cmd_data.help.cfg_component->comp_cls_name->str) == 0) {
799 /* Plugin help only */
b5cd7d65 800 cmd_status = BT_CMD_STATUS_OK;
d94d92ac
PP
801 goto end;
802 }
22e22462 803
d94d92ac
PP
804 needed_comp_cls = find_component_class(
805 cfg->cmd_data.help.cfg_component->plugin_name->str,
806 cfg->cmd_data.help.cfg_component->comp_cls_name->str,
807 cfg->cmd_data.help.cfg_component->type);
808 if (!needed_comp_cls) {
cee05105
PP
809 BT_CLI_LOGE_APPEND_CAUSE(
810 "Cannot find component class: plugin-name=\"%s\", "
d94d92ac
PP
811 "comp-cls-name=\"%s\", comp-cls-type=%d",
812 cfg->cmd_data.help.cfg_component->plugin_name->str,
813 cfg->cmd_data.help.cfg_component->comp_cls_name->str,
814 cfg->cmd_data.help.cfg_component->type);
b5cd7d65 815 goto error;
d94d92ac 816 }
22e22462 817
d94d92ac
PP
818 printf("\n");
819 print_component_class_help(
820 cfg->cmd_data.help.cfg_component->plugin_name->str,
821 needed_comp_cls);
b5cd7d65
FD
822 cmd_status = BT_CMD_STATUS_OK;
823 goto end;
824
825error:
826 cmd_status = BT_CMD_STATUS_ERROR;
d94d92ac
PP
827
828end:
c5b9b441
PP
829 bt_component_class_put_ref(needed_comp_cls);
830 bt_plugin_put_ref(plugin);
b5cd7d65 831 return cmd_status;
d94d92ac
PP
832}
833
b19ff26f 834typedef void *(* plugin_borrow_comp_cls_by_index_func_t)(const bt_plugin *,
d94d92ac 835 uint64_t);
b19ff26f 836typedef const bt_component_class *(* spec_comp_cls_borrow_comp_cls_func_t)(
d94d92ac
PP
837 void *);
838
7c7301d5 839static
b19ff26f 840void cmd_list_plugins_print_component_classes(const bt_plugin *plugin,
d94d92ac
PP
841 const char *cc_type_name, uint64_t count,
842 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func,
843 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func)
844{
845 uint64_t i;
22e22462 846
d94d92ac 847 if (count == 0) {
8a7c9d06 848 printf(" %s%s component classes%s: (none)\n",
d94d92ac 849 bt_common_color_bold(),
8a7c9d06 850 cc_type_name,
d94d92ac
PP
851 bt_common_color_reset());
852 goto end;
853 } else {
8a7c9d06 854 printf(" %s%s component classes%s:\n",
d94d92ac 855 bt_common_color_bold(),
8a7c9d06 856 cc_type_name,
d94d92ac 857 bt_common_color_reset());
22e22462
PP
858 }
859
d94d92ac 860 for (i = 0; i < count; i++) {
b19ff26f 861 const bt_component_class *comp_class =
d94d92ac
PP
862 spec_comp_cls_borrow_comp_cls_func(
863 borrow_comp_cls_by_index_func(plugin, i));
22e22462 864 const char *comp_class_name =
d94d92ac 865 bt_component_class_get_name(comp_class);
22e22462 866 const char *comp_class_description =
d94d92ac 867 bt_component_class_get_description(comp_class);
4cdfc5e8 868 bt_component_class_type type =
d94d92ac 869 bt_component_class_get_type(comp_class);
22e22462 870
d94d92ac 871 printf(" ");
22e22462 872 print_plugin_comp_cls_opt(stdout,
d94d92ac 873 bt_plugin_get_name(plugin), comp_class_name,
22e22462 874 type);
22e22462 875
d94d92ac
PP
876 if (comp_class_description) {
877 printf(": %s", comp_class_description);
22e22462
PP
878 }
879
d94d92ac 880 printf("\n");
22e22462
PP
881 }
882
883end:
d94d92ac 884 return;
22e22462
PP
885}
886
9009cc24 887static
b5cd7d65 888enum bt_cmd_status cmd_list_plugins(struct bt_config *cfg)
290725f7 889{
290725f7
PP
890 int plugins_count, component_classes_count = 0, i;
891
22e22462 892 printf("From the following plugin paths:\n\n");
05e21286 893 print_value(stdout, cfg->plugin_paths, 2);
22e22462 894 printf("\n");
743138a3 895 plugins_count = get_loaded_plugins_count();
290725f7 896 if (plugins_count == 0) {
7213a328 897 printf("No plugins found.\n");
56a1cced
JG
898 goto end;
899 }
900
290725f7 901 for (i = 0; i < plugins_count; i++) {
743138a3 902 const bt_plugin *plugin = borrow_loaded_plugin(i);
290725f7 903
d94d92ac
PP
904 component_classes_count +=
905 bt_plugin_get_source_component_class_count(plugin) +
906 bt_plugin_get_filter_component_class_count(plugin) +
907 bt_plugin_get_sink_component_class_count(plugin);
290725f7 908 }
33bceaf8 909
290725f7
PP
910 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
911 bt_common_color_bold(),
912 component_classes_count,
913 bt_common_color_reset(),
914 bt_common_color_bold(),
915 plugins_count,
916 bt_common_color_reset());
917
918 for (i = 0; i < plugins_count; i++) {
743138a3 919 const bt_plugin *plugin = borrow_loaded_plugin(i);
290725f7 920
22e22462
PP
921 printf("\n");
922 print_plugin_info(plugin);
d94d92ac
PP
923 cmd_list_plugins_print_component_classes(plugin, "Source",
924 bt_plugin_get_source_component_class_count(plugin),
925 (plugin_borrow_comp_cls_by_index_func_t)
c3dd43ec 926 bt_plugin_borrow_source_component_class_by_index_const,
d94d92ac 927 (spec_comp_cls_borrow_comp_cls_func_t)
707b7d35 928 bt_component_class_source_as_component_class);
d94d92ac
PP
929 cmd_list_plugins_print_component_classes(plugin, "Filter",
930 bt_plugin_get_filter_component_class_count(plugin),
931 (plugin_borrow_comp_cls_by_index_func_t)
c3dd43ec 932 bt_plugin_borrow_filter_component_class_by_index_const,
d94d92ac 933 (spec_comp_cls_borrow_comp_cls_func_t)
707b7d35 934 bt_component_class_filter_as_component_class);
d94d92ac
PP
935 cmd_list_plugins_print_component_classes(plugin, "Sink",
936 bt_plugin_get_sink_component_class_count(plugin),
937 (plugin_borrow_comp_cls_by_index_func_t)
c3dd43ec 938 bt_plugin_borrow_sink_component_class_by_index_const,
d94d92ac 939 (spec_comp_cls_borrow_comp_cls_func_t)
707b7d35 940 bt_component_class_sink_as_component_class);
290725f7
PP
941 }
942
943end:
b5cd7d65 944 return BT_CMD_STATUS_OK;
290725f7
PP
945}
946
9009cc24 947static
b5cd7d65 948enum bt_cmd_status cmd_print_lttng_live_sessions(struct bt_config *cfg)
db0f160a 949{
96e8c7e1 950 int ret = 0;
b5cd7d65 951 enum bt_cmd_status cmd_status;
b19ff26f
PP
952 const bt_component_class *comp_cls = NULL;
953 const bt_value *results = NULL;
954 bt_value *params = NULL;
955 const bt_value *map = NULL;
956 const bt_value *v = NULL;
96e8c7e1
MD
957 static const char * const plugin_name = "ctf";
958 static const char * const comp_cls_name = "lttng-live";
4cdfc5e8 959 static const bt_component_class_type comp_cls_type =
96e8c7e1 960 BT_COMPONENT_CLASS_TYPE_SOURCE;
f80e9ec1 961 uint64_t array_size, i;
c7eee084 962 const char *fail_reason = NULL;
c327e427 963 FILE *out_stream = stdout;
96e8c7e1 964
f6ccaed9 965 BT_ASSERT(cfg->cmd_data.print_lttng_live_sessions.url);
96e8c7e1
MD
966 comp_cls = find_component_class(plugin_name, comp_cls_name,
967 comp_cls_type);
968 if (!comp_cls) {
cee05105
PP
969 BT_CLI_LOGE_APPEND_CAUSE(
970 "Cannot find component class: plugin-name=\"%s\", "
96e8c7e1
MD
971 "comp-cls-name=\"%s\", comp-cls-type=%d",
972 plugin_name, comp_cls_name,
973 BT_COMPONENT_CLASS_TYPE_SOURCE);
96e8c7e1
MD
974 goto error;
975 }
976
05e21286 977 params = bt_value_map_create();
96e8c7e1
MD
978 if (!params) {
979 goto error;
980 }
981
05e21286 982 ret = bt_value_map_insert_string_entry(params, "url",
96e8c7e1
MD
983 cfg->cmd_data.print_lttng_live_sessions.url->str);
984 if (ret) {
985 goto error;
986 }
987
f4e38e70 988 ret = query(cfg, comp_cls, "sessions", params,
05e21286 989 &results, &fail_reason);
c7eee084 990 if (ret) {
16cbfe32
FD
991 BT_CLI_LOGE_APPEND_CAUSE("Failed to query `sessions` object: %s",
992 fail_reason);
993 goto error;
96e8c7e1
MD
994 }
995
f6ccaed9
PP
996 BT_ASSERT(results);
997
96e8c7e1 998 if (!bt_value_is_array(results)) {
cee05105
PP
999 BT_CLI_LOGE_APPEND_CAUSE(
1000 "Expecting an array for LTTng live `sessions` query.");
96e8c7e1
MD
1001 goto error;
1002 }
1003
c327e427
PP
1004 if (cfg->cmd_data.print_lttng_live_sessions.output_path->len > 0) {
1005 out_stream =
1006 fopen(cfg->cmd_data.print_lttng_live_sessions.output_path->str,
1007 "w");
1008 if (!out_stream) {
c327e427
PP
1009 BT_LOGE_ERRNO("Cannot open file for writing",
1010 ": path=\"%s\"",
1011 cfg->cmd_data.print_lttng_live_sessions.output_path->str);
cee05105
PP
1012 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
1013 "Babeltrace CLI",
1014 "Cannot open file for writing: path=\"%s\"",
1015 cfg->cmd_data.print_lttng_live_sessions.output_path->str);
b5cd7d65 1016 goto error;
c327e427
PP
1017 }
1018 }
1019
393729a6 1020 array_size = bt_value_array_get_length(results);
96e8c7e1
MD
1021 for (i = 0; i < array_size; i++) {
1022 const char *url_text;
1023 int64_t timer_us, streams, clients;
1024
05e21286 1025 map = bt_value_array_borrow_element_by_index_const(results, i);
96e8c7e1 1026 if (!bt_value_is_map(map)) {
cee05105 1027 BT_CLI_LOGE_APPEND_CAUSE("Unexpected entry type.");
96e8c7e1
MD
1028 goto error;
1029 }
1030
05e21286 1031 v = bt_value_map_borrow_entry_value_const(map, "url");
96e8c7e1 1032 if (!v) {
cee05105 1033 BT_CLI_LOGE_APPEND_CAUSE("Missing `url` entry.");
96e8c7e1
MD
1034 goto error;
1035 }
601b0d3c 1036 url_text = bt_value_string_get(v);
c327e427 1037 fprintf(out_stream, "%s", url_text);
05e21286 1038 v = bt_value_map_borrow_entry_value_const(map, "timer-us");
96e8c7e1 1039 if (!v) {
cee05105 1040 BT_CLI_LOGE_APPEND_CAUSE("Missing `timer-us` entry.");
96e8c7e1
MD
1041 goto error;
1042 }
a91cb83e 1043 timer_us = bt_value_integer_unsigned_get(v);
c327e427 1044 fprintf(out_stream, " (timer = %" PRIu64 ", ", timer_us);
05e21286 1045 v = bt_value_map_borrow_entry_value_const(map, "stream-count");
96e8c7e1 1046 if (!v) {
cee05105
PP
1047 BT_CLI_LOGE_APPEND_CAUSE(
1048 "Missing `stream-count` entry.");
96e8c7e1
MD
1049 goto error;
1050 }
a91cb83e 1051 streams = bt_value_integer_unsigned_get(v);
c327e427 1052 fprintf(out_stream, "%" PRIu64 " stream(s), ", streams);
05e21286 1053 v = bt_value_map_borrow_entry_value_const(map, "client-count");
96e8c7e1 1054 if (!v) {
cee05105
PP
1055 BT_CLI_LOGE_APPEND_CAUSE(
1056 "Missing `client-count` entry.");
96e8c7e1
MD
1057 goto error;
1058 }
a91cb83e 1059 clients = bt_value_integer_unsigned_get(v);
c327e427 1060 fprintf(out_stream, "%" PRIu64 " client(s) connected)\n", clients);
96e8c7e1 1061 }
c7eee084 1062
b5cd7d65 1063 cmd_status = BT_CMD_STATUS_OK;
c7eee084
PP
1064 goto end;
1065
c7eee084 1066error:
b5cd7d65 1067 cmd_status = BT_CMD_STATUS_ERROR;
c7eee084 1068
96e8c7e1 1069end:
c5b9b441
PP
1070 bt_value_put_ref(results);
1071 bt_value_put_ref(params);
1072 bt_component_class_put_ref(comp_cls);
c327e427
PP
1073
1074 if (out_stream && out_stream != stdout) {
1075 int fclose_ret = fclose(out_stream);
1076
1077 if (fclose_ret) {
1078 BT_LOGE_ERRNO("Cannot close file stream",
1079 ": path=\"%s\"",
1080 cfg->cmd_data.print_lttng_live_sessions.output_path->str);
1081 }
1082 }
1083
b5cd7d65 1084 return cmd_status;
db0f160a
PP
1085}
1086
9009cc24 1087static
b5cd7d65 1088enum bt_cmd_status cmd_print_ctf_metadata(struct bt_config *cfg)
05a67631
PP
1089{
1090 int ret = 0;
b5cd7d65 1091 enum bt_cmd_status cmd_status;
b19ff26f
PP
1092 const bt_component_class *comp_cls = NULL;
1093 const bt_value *results = NULL;
1094 bt_value *params = NULL;
1095 const bt_value *metadata_text_value = NULL;
05a67631 1096 const char *metadata_text = NULL;
db0f160a
PP
1097 static const char * const plugin_name = "ctf";
1098 static const char * const comp_cls_name = "fs";
4cdfc5e8 1099 static const bt_component_class_type comp_cls_type =
db0f160a 1100 BT_COMPONENT_CLASS_TYPE_SOURCE;
c7eee084 1101 const char *fail_reason = NULL;
c327e427 1102 FILE *out_stream = stdout;
db0f160a 1103
f6ccaed9 1104 BT_ASSERT(cfg->cmd_data.print_ctf_metadata.path);
db0f160a
PP
1105 comp_cls = find_component_class(plugin_name, comp_cls_name,
1106 comp_cls_type);
05a67631 1107 if (!comp_cls) {
cee05105
PP
1108 BT_CLI_LOGE_APPEND_CAUSE(
1109 "Cannot find component class: plugin-name=\"%s\", "
7213a328
PP
1110 "comp-cls-name=\"%s\", comp-cls-type=%d",
1111 plugin_name, comp_cls_name,
1112 BT_COMPONENT_CLASS_TYPE_SOURCE);
b5cd7d65 1113 goto error;
05a67631
PP
1114 }
1115
05e21286 1116 params = bt_value_map_create();
05a67631 1117 if (!params) {
b5cd7d65 1118 goto error;
05a67631
PP
1119 }
1120
05e21286 1121 ret = bt_value_map_insert_string_entry(params, "path",
db0f160a 1122 cfg->cmd_data.print_ctf_metadata.path->str);
05a67631 1123 if (ret) {
b5cd7d65 1124 goto error;
05a67631
PP
1125 }
1126
f4e38e70 1127 ret = query(cfg, comp_cls, "metadata-info",
05e21286 1128 params, &results, &fail_reason);
c7eee084 1129 if (ret) {
16cbfe32
FD
1130 BT_CLI_LOGE_APPEND_CAUSE(
1131 "Failed to query `metadata-info` object: %s", fail_reason);
1132 goto error;
05a67631
PP
1133 }
1134
05e21286 1135 metadata_text_value = bt_value_map_borrow_entry_value_const(results,
cee05105 1136 "text");
05a67631 1137 if (!metadata_text_value) {
cee05105
PP
1138 BT_CLI_LOGE_APPEND_CAUSE(
1139 "Cannot find `text` string value in the resulting metadata info object.");
b5cd7d65 1140 goto error;
05a67631
PP
1141 }
1142
601b0d3c 1143 metadata_text = bt_value_string_get(metadata_text_value);
c327e427
PP
1144
1145 if (cfg->cmd_data.print_ctf_metadata.output_path->len > 0) {
1146 out_stream =
1147 fopen(cfg->cmd_data.print_ctf_metadata.output_path->str,
1148 "w");
1149 if (!out_stream) {
c327e427
PP
1150 BT_LOGE_ERRNO("Cannot open file for writing",
1151 ": path=\"%s\"",
1152 cfg->cmd_data.print_ctf_metadata.output_path->str);
cee05105
PP
1153 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
1154 "Babeltrace CLI",
1155 "Cannot open file for writing: path=\"%s\"",
1156 cfg->cmd_data.print_ctf_metadata.output_path->str);
b5cd7d65 1157 goto error;
c327e427
PP
1158 }
1159 }
1160
1161 ret = fprintf(out_stream, "%s\n", metadata_text);
1162 if (ret < 0) {
cee05105
PP
1163 BT_CLI_LOGE_APPEND_CAUSE(
1164 "Cannot write whole metadata text to output stream: "
c327e427 1165 "ret=%d", ret);
b5cd7d65 1166 goto error;
c327e427
PP
1167 }
1168
b5cd7d65 1169 cmd_status = BT_CMD_STATUS_OK;
c7eee084
PP
1170 goto end;
1171
b5cd7d65
FD
1172error:
1173 cmd_status = BT_CMD_STATUS_ERROR;
05a67631
PP
1174
1175end:
c5b9b441
PP
1176 bt_value_put_ref(results);
1177 bt_value_put_ref(params);
1178 bt_component_class_put_ref(comp_cls);
c327e427
PP
1179
1180 if (out_stream && out_stream != stdout) {
1181 int fclose_ret = fclose(out_stream);
1182
1183 if (fclose_ret) {
1184 BT_LOGE_ERRNO("Cannot close file stream",
1185 ": path=\"%s\"",
1186 cfg->cmd_data.print_ctf_metadata.output_path->str);
1187 }
1188 }
1189
b5cd7d65 1190 return cmd_status;
05a67631
PP
1191}
1192
75a2cb9b
JG
1193struct port_id {
1194 char *instance_name;
1195 char *port_name;
1196};
1197
1198struct trace_range {
1199 uint64_t intersection_range_begin_ns;
1200 uint64_t intersection_range_end_ns;
1201};
1202
1203static
1204guint port_id_hash(gconstpointer v)
1205{
1206 const struct port_id *id = v;
1207
f6ccaed9
PP
1208 BT_ASSERT(id->instance_name);
1209 BT_ASSERT(id->port_name);
75a2cb9b
JG
1210
1211 return g_str_hash(id->instance_name) ^ g_str_hash(id->port_name);
1212}
1213
1214static
1215gboolean port_id_equal(gconstpointer v1, gconstpointer v2)
1216{
1217 const struct port_id *id1 = v1;
1218 const struct port_id *id2 = v2;
1219
2242b43d
PP
1220 return strcmp(id1->instance_name, id2->instance_name) == 0 &&
1221 strcmp(id1->port_name, id2->port_name) == 0;
75a2cb9b
JG
1222}
1223
1224static
1225void port_id_destroy(gpointer data)
1226{
1227 struct port_id *id = data;
1228
1229 free(id->instance_name);
1230 free(id->port_name);
1231 free(id);
1232}
1233
1234static
1235void trace_range_destroy(gpointer data)
1236{
1237 free(data);
1238}
1239
9009cc24
PP
1240struct cmd_run_ctx {
1241 /* Owned by this */
d94d92ac
PP
1242 GHashTable *src_components;
1243
1244 /* Owned by this */
1245 GHashTable *flt_components;
1246
1247 /* Owned by this */
1248 GHashTable *sink_components;
9009cc24
PP
1249
1250 /* Owned by this */
b19ff26f 1251 bt_graph *graph;
9009cc24
PP
1252
1253 /* Weak */
1254 struct bt_config *cfg;
1255
1256 bool connect_ports;
75a2cb9b
JG
1257
1258 bool stream_intersection_mode;
1259
1260 /*
1261 * Association of struct port_id -> struct trace_range.
1262 */
1263 GHashTable *intersections;
9009cc24
PP
1264};
1265
75a2cb9b
JG
1266/* Returns a timestamp of the form "(-)s.ns" */
1267static
1268char *s_from_ns(int64_t ns)
1269{
1270 int ret;
1271 char *s_ret = NULL;
1272 bool is_negative;
1273 int64_t ts_sec_abs, ts_nsec_abs;
1274 int64_t ts_sec = ns / NSEC_PER_SEC;
1275 int64_t ts_nsec = ns % NSEC_PER_SEC;
1276
1277 if (ts_sec >= 0 && ts_nsec >= 0) {
1278 is_negative = false;
1279 ts_sec_abs = ts_sec;
1280 ts_nsec_abs = ts_nsec;
1281 } else if (ts_sec > 0 && ts_nsec < 0) {
1282 is_negative = false;
1283 ts_sec_abs = ts_sec - 1;
1284 ts_nsec_abs = NSEC_PER_SEC + ts_nsec;
1285 } else if (ts_sec == 0 && ts_nsec < 0) {
1286 is_negative = true;
1287 ts_sec_abs = ts_sec;
1288 ts_nsec_abs = -ts_nsec;
1289 } else if (ts_sec < 0 && ts_nsec > 0) {
1290 is_negative = true;
1291 ts_sec_abs = -(ts_sec + 1);
1292 ts_nsec_abs = NSEC_PER_SEC - ts_nsec;
1293 } else if (ts_sec < 0 && ts_nsec == 0) {
1294 is_negative = true;
1295 ts_sec_abs = -ts_sec;
1296 ts_nsec_abs = ts_nsec;
1297 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1298 is_negative = true;
1299 ts_sec_abs = -ts_sec;
1300 ts_nsec_abs = -ts_nsec;
1301 }
1302
1303 ret = asprintf(&s_ret, "%s%" PRId64 ".%09" PRId64,
1304 is_negative ? "-" : "", ts_sec_abs, ts_nsec_abs);
1305 if (ret < 0) {
1306 s_ret = NULL;
1307 }
1308 return s_ret;
1309}
1310
9009cc24
PP
1311static
1312int cmd_run_ctx_connect_upstream_port_to_downstream_component(
d94d92ac 1313 struct cmd_run_ctx *ctx,
b19ff26f
PP
1314 const bt_component *upstream_comp,
1315 const bt_port_output *out_upstream_port,
9009cc24 1316 struct bt_config_connection *cfg_conn)
290725f7 1317{
d94d92ac 1318 typedef uint64_t (*input_port_count_func_t)(void *);
b19ff26f 1319 typedef const bt_port_input *(*borrow_input_port_by_index_func_t)(
0d72b8c3 1320 const void *, uint64_t);
b19ff26f 1321 const bt_port *upstream_port =
0d72b8c3 1322 bt_port_output_as_port_const(out_upstream_port);
d94d92ac 1323
290725f7 1324 int ret = 0;
9009cc24 1325 GQuark downstreamp_comp_name_quark;
d94d92ac
PP
1326 void *downstream_comp;
1327 uint64_t downstream_port_count;
9009cc24 1328 uint64_t i;
d94d92ac
PP
1329 input_port_count_func_t port_count_fn;
1330 borrow_input_port_by_index_func_t port_by_index_fn;
d24d5663
PP
1331 bt_graph_connect_ports_status connect_ports_status =
1332 BT_GRAPH_CONNECT_PORTS_STATUS_OK;
75a2cb9b 1333 bool insert_trimmer = false;
b19ff26f 1334 bt_value *trimmer_params = NULL;
75a2cb9b
JG
1335 char *intersection_begin = NULL;
1336 char *intersection_end = NULL;
b19ff26f
PP
1337 const bt_component_filter *trimmer = NULL;
1338 const bt_component_class_filter *trimmer_class = NULL;
1339 const bt_port_input *trimmer_input = NULL;
1340 const bt_port_output *trimmer_output = NULL;
75a2cb9b
JG
1341
1342 if (ctx->intersections &&
1343 bt_component_get_class_type(upstream_comp) ==
1344 BT_COMPONENT_CLASS_TYPE_SOURCE) {
1345 struct trace_range *range;
1346 struct port_id port_id = {
1347 .instance_name = (char *) bt_component_get_name(upstream_comp),
1348 .port_name = (char *) bt_port_get_name(upstream_port)
1349 };
1350
1351 if (!port_id.instance_name || !port_id.port_name) {
1352 goto error;
1353 }
1354
1355 range = (struct trace_range *) g_hash_table_lookup(
1356 ctx->intersections, &port_id);
1357 if (range) {
d24d5663 1358 bt_value_map_insert_entry_status insert_status;
75a2cb9b
JG
1359
1360 intersection_begin = s_from_ns(
1361 range->intersection_range_begin_ns);
1362 intersection_end = s_from_ns(
1363 range->intersection_range_end_ns);
1364 if (!intersection_begin || !intersection_end) {
cee05105
PP
1365 BT_CLI_LOGE_APPEND_CAUSE(
1366 "Cannot create trimmer argument timestamp string.");
75a2cb9b
JG
1367 goto error;
1368 }
1369
1370 insert_trimmer = true;
05e21286 1371 trimmer_params = bt_value_map_create();
75a2cb9b
JG
1372 if (!trimmer_params) {
1373 goto error;
1374 }
1375
d24d5663 1376 insert_status = bt_value_map_insert_string_entry(
07208d85 1377 trimmer_params, "begin", intersection_begin);
d24d5663 1378 if (insert_status < 0) {
75a2cb9b
JG
1379 goto error;
1380 }
d24d5663 1381 insert_status = bt_value_map_insert_string_entry(
07208d85 1382 trimmer_params,
75a2cb9b 1383 "end", intersection_end);
d24d5663 1384 if (insert_status < 0) {
75a2cb9b
JG
1385 goto error;
1386 }
1387 }
1388
d94d92ac 1389 trimmer_class = find_filter_component_class("utils", "trimmer");
75a2cb9b
JG
1390 if (!trimmer_class) {
1391 goto error;
1392 }
1393 }
9009cc24 1394
7213a328
PP
1395 BT_LOGI("Connecting upstream port to the next available downstream port: "
1396 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1397 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1398 upstream_port, bt_port_get_name(upstream_port),
1399 cfg_conn->downstream_comp_name->str,
1400 cfg_conn->arg->str);
9009cc24
PP
1401 downstreamp_comp_name_quark = g_quark_from_string(
1402 cfg_conn->downstream_comp_name->str);
f6ccaed9 1403 BT_ASSERT(downstreamp_comp_name_quark > 0);
d94d92ac 1404 downstream_comp = g_hash_table_lookup(ctx->flt_components,
71c7c95f 1405 GUINT_TO_POINTER(downstreamp_comp_name_quark));
d94d92ac
PP
1406 port_count_fn = (input_port_count_func_t)
1407 bt_component_filter_get_input_port_count;
1408 port_by_index_fn = (borrow_input_port_by_index_func_t)
0d72b8c3 1409 bt_component_filter_borrow_input_port_by_index_const;
d94d92ac
PP
1410
1411 if (!downstream_comp) {
1412 downstream_comp = g_hash_table_lookup(ctx->sink_components,
1413 GUINT_TO_POINTER(downstreamp_comp_name_quark));
1414 port_count_fn = (input_port_count_func_t)
1415 bt_component_sink_get_input_port_count;
1416 port_by_index_fn = (borrow_input_port_by_index_func_t)
0d72b8c3 1417 bt_component_sink_borrow_input_port_by_index_const;
d94d92ac
PP
1418 }
1419
9009cc24 1420 if (!downstream_comp) {
cee05105
PP
1421 BT_CLI_LOGE_APPEND_CAUSE("Cannot find downstream component: "
1422 "comp-name=\"%s\", conn-arg=\"%s\"",
1423 cfg_conn->downstream_comp_name->str,
9009cc24
PP
1424 cfg_conn->arg->str);
1425 goto error;
1426 }
1427
9009cc24 1428 downstream_port_count = port_count_fn(downstream_comp);
9009cc24
PP
1429
1430 for (i = 0; i < downstream_port_count; i++) {
b19ff26f 1431 const bt_port_input *in_downstream_port =
9009cc24 1432 port_by_index_fn(downstream_comp, i);
b19ff26f 1433 const bt_port *downstream_port =
0d72b8c3 1434 bt_port_input_as_port_const(in_downstream_port);
75a2cb9b 1435 const char *upstream_port_name;
9009cc24
PP
1436 const char *downstream_port_name;
1437
f6ccaed9 1438 BT_ASSERT(downstream_port);
9009cc24 1439
75a2cb9b 1440 /* Skip port if it's already connected. */
9009cc24 1441 if (bt_port_is_connected(downstream_port)) {
3f7d4d90 1442 BT_LOGI("Skipping downstream port: already connected: "
7213a328
PP
1443 "port-addr=%p, port-name=\"%s\"",
1444 downstream_port,
1445 bt_port_get_name(downstream_port));
9009cc24
PP
1446 continue;
1447 }
1448
1449 downstream_port_name = bt_port_get_name(downstream_port);
f6ccaed9 1450 BT_ASSERT(downstream_port_name);
75a2cb9b 1451 upstream_port_name = bt_port_get_name(upstream_port);
f6ccaed9 1452 BT_ASSERT(upstream_port_name);
9009cc24 1453
75a2cb9b 1454 if (!bt_common_star_glob_match(
1974687e
MJ
1455 cfg_conn->downstream_port_glob->str, SIZE_MAX,
1456 downstream_port_name, SIZE_MAX)) {
75a2cb9b
JG
1457 continue;
1458 }
1459
1460 if (insert_trimmer) {
1461 /*
d94d92ac
PP
1462 * In order to insert the trimmer between the
1463 * two components that were being connected, we
1464 * create a connection configuration entry which
1465 * describes a connection from the trimmer's
1466 * output to the original input that was being
1467 * connected.
75a2cb9b 1468 *
d94d92ac
PP
1469 * Hence, the creation of the trimmer will cause
1470 * the graph "new port" listener to establish
1471 * all downstream connections as its output port
1472 * is connected. We will then establish the
1473 * connection between the original upstream
1474 * source and the trimmer.
75a2cb9b
JG
1475 */
1476 char *trimmer_name = NULL;
d24d5663 1477 bt_graph_add_component_status add_comp_status;
75a2cb9b 1478
d94d92ac
PP
1479 ret = asprintf(&trimmer_name,
1480 "stream-intersection-trimmer-%s",
75a2cb9b
JG
1481 upstream_port_name);
1482 if (ret < 0) {
1483 goto error;
1484 }
1485 ret = 0;
1486
1487 ctx->connect_ports = false;
d24d5663 1488 add_comp_status = bt_graph_add_filter_component(
d94d92ac 1489 ctx->graph, trimmer_class, trimmer_name,
e874da19
PP
1490 trimmer_params, ctx->cfg->log_level,
1491 &trimmer);
75a2cb9b 1492 free(trimmer_name);
d24d5663
PP
1493 if (add_comp_status !=
1494 BT_GRAPH_ADD_COMPONENT_STATUS_OK) {
75a2cb9b
JG
1495 goto error;
1496 }
f6ccaed9 1497 BT_ASSERT(trimmer);
75a2cb9b
JG
1498
1499 trimmer_input =
0d72b8c3 1500 bt_component_filter_borrow_input_port_by_index_const(
75a2cb9b
JG
1501 trimmer, 0);
1502 if (!trimmer_input) {
1503 goto error;
1504 }
1505 trimmer_output =
0d72b8c3 1506 bt_component_filter_borrow_output_port_by_index_const(
75a2cb9b
JG
1507 trimmer, 0);
1508 if (!trimmer_output) {
9009cc24
PP
1509 goto error;
1510 }
1511
75a2cb9b
JG
1512 /*
1513 * Replace the current downstream port by the trimmer's
1514 * upstream port.
1515 */
d94d92ac
PP
1516 in_downstream_port = trimmer_input;
1517 downstream_port =
0d72b8c3 1518 bt_port_input_as_port_const(in_downstream_port);
75a2cb9b
JG
1519 downstream_port_name = bt_port_get_name(
1520 downstream_port);
d94d92ac 1521 BT_ASSERT(downstream_port_name);
75a2cb9b
JG
1522 }
1523
1524 /* We have a winner! */
d24d5663 1525 connect_ports_status = bt_graph_connect_ports(ctx->graph,
d94d92ac
PP
1526 out_upstream_port, in_downstream_port, NULL);
1527 downstream_port = NULL;
d24d5663
PP
1528 switch (connect_ports_status) {
1529 case BT_GRAPH_CONNECT_PORTS_STATUS_OK:
75a2cb9b 1530 break;
75a2cb9b 1531 default:
cee05105
PP
1532 BT_CLI_LOGE_APPEND_CAUSE(
1533 "Cannot create connection: graph refuses to connect ports: "
75a2cb9b
JG
1534 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1535 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1536 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1537 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1538 "conn-arg=\"%s\"",
1539 upstream_comp, bt_component_get_name(upstream_comp),
1540 upstream_port, bt_port_get_name(upstream_port),
1541 downstream_comp, cfg_conn->downstream_comp_name->str,
1542 downstream_port, downstream_port_name,
1543 cfg_conn->arg->str);
75a2cb9b 1544 goto error;
9009cc24
PP
1545 }
1546
75a2cb9b
JG
1547 BT_LOGI("Connected component ports: "
1548 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1549 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1550 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1551 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1552 "conn-arg=\"%s\"",
1553 upstream_comp, bt_component_get_name(upstream_comp),
1554 upstream_port, bt_port_get_name(upstream_port),
1555 downstream_comp, cfg_conn->downstream_comp_name->str,
1556 downstream_port, downstream_port_name,
1557 cfg_conn->arg->str);
1558
1559 if (insert_trimmer) {
1560 /*
1561 * The first connection, from the source to the trimmer,
1562 * has been done. We now connect the trimmer to the
1563 * original downstream port.
1564 */
1565 ret = cmd_run_ctx_connect_upstream_port_to_downstream_component(
d94d92ac 1566 ctx,
0d72b8c3 1567 bt_component_filter_as_component_const(trimmer),
d94d92ac 1568 trimmer_output, cfg_conn);
75a2cb9b
JG
1569 if (ret) {
1570 goto error;
1571 }
1572 ctx->connect_ports = true;
1573 }
9009cc24 1574
53bc54cd
PP
1575 /*
1576 * We found a matching downstream port: the search is
1577 * over.
1578 */
1579 goto end;
05a67631
PP
1580 }
1581
53bc54cd 1582 /* No downstream port found */
cee05105
PP
1583 BT_CLI_LOGE_APPEND_CAUSE(
1584 "Cannot create connection: cannot find a matching downstream port for upstream port: "
53bc54cd
PP
1585 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1586 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1587 upstream_port, bt_port_get_name(upstream_port),
1588 cfg_conn->downstream_comp_name->str,
1589 cfg_conn->arg->str);
9009cc24
PP
1590
1591error:
1592 ret = -1;
1593
1594end:
75a2cb9b
JG
1595 free(intersection_begin);
1596 free(intersection_end);
c5b9b441
PP
1597 BT_VALUE_PUT_REF_AND_RESET(trimmer_params);
1598 BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class);
1599 BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer);
9009cc24
PP
1600 return ret;
1601}
1602
1603static
1604int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx *ctx,
b19ff26f 1605 const bt_port_output *upstream_port)
9009cc24
PP
1606{
1607 int ret = 0;
1608 const char *upstream_port_name;
1609 const char *upstream_comp_name;
b19ff26f 1610 const bt_component *upstream_comp = NULL;
9009cc24
PP
1611 size_t i;
1612
f6ccaed9
PP
1613 BT_ASSERT(ctx);
1614 BT_ASSERT(upstream_port);
d94d92ac 1615 upstream_port_name = bt_port_get_name(
0d72b8c3 1616 bt_port_output_as_port_const(upstream_port));
f6ccaed9 1617 BT_ASSERT(upstream_port_name);
0d72b8c3
PP
1618 upstream_comp = bt_port_borrow_component_const(
1619 bt_port_output_as_port_const(upstream_port));
cee05105 1620 BT_ASSERT(upstream_comp);
9009cc24 1621 upstream_comp_name = bt_component_get_name(upstream_comp);
f6ccaed9 1622 BT_ASSERT(upstream_comp_name);
7213a328
PP
1623 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1624 "port-addr=%p, port-name=\"%s\"",
1625 upstream_comp, upstream_comp_name,
1626 upstream_port, upstream_port_name);
9009cc24
PP
1627
1628 for (i = 0; i < ctx->cfg->cmd_data.run.connections->len; i++) {
1629 struct bt_config_connection *cfg_conn =
1630 g_ptr_array_index(
1631 ctx->cfg->cmd_data.run.connections, i);
1632
1633 if (strcmp(cfg_conn->upstream_comp_name->str,
75a2cb9b
JG
1634 upstream_comp_name)) {
1635 continue;
1636 }
1637
1638 if (!bt_common_star_glob_match(
1639 cfg_conn->upstream_port_glob->str,
1974687e 1640 SIZE_MAX, upstream_port_name, SIZE_MAX)) {
75a2cb9b
JG
1641 continue;
1642 }
1643
1644 ret = cmd_run_ctx_connect_upstream_port_to_downstream_component(
1645 ctx, upstream_comp, upstream_port, cfg_conn);
1646 if (ret) {
cee05105
PP
1647 BT_CLI_LOGE_APPEND_CAUSE(
1648 "Cannot connect upstream port: "
75a2cb9b
JG
1649 "port-addr=%p, port-name=\"%s\"",
1650 upstream_port,
1651 upstream_port_name);
75a2cb9b 1652 goto error;
9009cc24 1653 }
75a2cb9b 1654 goto end;
9009cc24
PP
1655 }
1656
cee05105
PP
1657 BT_CLI_LOGE_APPEND_CAUSE(
1658 "Cannot connect upstream port: port does not match any connection argument: "
7213a328
PP
1659 "port-addr=%p, port-name=\"%s\"", upstream_port,
1660 upstream_port_name);
9009cc24
PP
1661
1662error:
1663 ret = -1;
1664
1665end:
9009cc24
PP
1666 return ret;
1667}
1668
1669static
d24d5663 1670bt_graph_listener_func_status
8cc56726 1671graph_output_port_added_listener(struct cmd_run_ctx *ctx,
b19ff26f 1672 const bt_port_output *out_port)
9009cc24 1673{
b19ff26f
PP
1674 const bt_component *comp;
1675 const bt_port *port = bt_port_output_as_port_const(out_port);
d24d5663
PP
1676 bt_graph_listener_func_status ret =
1677 BT_GRAPH_LISTENER_FUNC_STATUS_OK;
9009cc24 1678
0d72b8c3 1679 comp = bt_port_borrow_component_const(port);
e12720c0
PP
1680 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1681 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1682 comp, comp ? bt_component_get_name(comp) : "",
7213a328 1683 port, bt_port_get_name(port));
36712f1d
PP
1684
1685 if (!ctx->connect_ports) {
1686 goto end;
1687 }
1688
cee05105 1689 BT_ASSERT(comp);
7c7c0433 1690
e12720c0
PP
1691 if (bt_port_is_connected(port)) {
1692 BT_LOGW_STR("Port is already connected.");
7c7c0433
JG
1693 goto end;
1694 }
1695
d94d92ac 1696 if (cmd_run_ctx_connect_upstream_port(ctx, out_port)) {
cee05105 1697 BT_CLI_LOGE_APPEND_CAUSE("Cannot connect upstream port.");
d24d5663 1698 ret = BT_GRAPH_LISTENER_FUNC_STATUS_ERROR;
8cc56726 1699 goto end;
9009cc24
PP
1700 }
1701
1702end:
8cc56726 1703 return ret;
9009cc24
PP
1704}
1705
1706static
d24d5663 1707bt_graph_listener_func_status graph_source_output_port_added_listener(
b19ff26f
PP
1708 const bt_component_source *component,
1709 const bt_port_output *port, void *data)
9009cc24 1710{
8cc56726 1711 return graph_output_port_added_listener(data, port);
9009cc24
PP
1712}
1713
1714static
d24d5663 1715bt_graph_listener_func_status graph_filter_output_port_added_listener(
b19ff26f
PP
1716 const bt_component_filter *component,
1717 const bt_port_output *port, void *data)
9009cc24 1718{
8cc56726 1719 return graph_output_port_added_listener(data, port);
9009cc24
PP
1720}
1721
1722static
1723void cmd_run_ctx_destroy(struct cmd_run_ctx *ctx)
1724{
1725 if (!ctx) {
1726 return;
1727 }
1728
d94d92ac
PP
1729 if (ctx->src_components) {
1730 g_hash_table_destroy(ctx->src_components);
1731 ctx->src_components = NULL;
1732 }
1733
1734 if (ctx->flt_components) {
1735 g_hash_table_destroy(ctx->flt_components);
1736 ctx->flt_components = NULL;
1737 }
1738
1739 if (ctx->sink_components) {
1740 g_hash_table_destroy(ctx->sink_components);
1741 ctx->sink_components = NULL;
9009cc24
PP
1742 }
1743
75a2cb9b
JG
1744 if (ctx->intersections) {
1745 g_hash_table_destroy(ctx->intersections);
a5302548 1746 ctx->intersections = NULL;
75a2cb9b
JG
1747 }
1748
c5b9b441 1749 BT_GRAPH_PUT_REF_AND_RESET(ctx->graph);
9009cc24
PP
1750 ctx->cfg = NULL;
1751}
1752
5fea82b8
PP
1753static
1754int add_descriptor_to_component_descriptor_set(
1755 bt_component_descriptor_set *comp_descr_set,
1756 const char *plugin_name, const char *comp_cls_name,
1757 bt_component_class_type comp_cls_type,
1758 const bt_value *params)
1759{
1760 const bt_component_class *comp_cls;
1761 int status = 0;
1762
1763 comp_cls = find_component_class(plugin_name, comp_cls_name,
1764 comp_cls_type);
1765 if (!comp_cls) {
1766 BT_CLI_LOGE_APPEND_CAUSE(
1767 "Cannot find component class: plugin-name=\"%s\", "
1768 "comp-cls-name=\"%s\", comp-cls-type=%d",
1769 plugin_name, comp_cls_name, comp_cls_type);
1770 status = -1;
1771 goto end;
1772 }
1773
1774 status = bt_component_descriptor_set_add_descriptor(
1775 comp_descr_set, comp_cls, params);
1776 if (status != BT_COMPONENT_DESCRIPTOR_SET_ADD_DESCRIPTOR_STATUS_OK) {
1777 BT_CLI_LOGE_APPEND_CAUSE(
1778 "Cannot append descriptor to component descriptor set: "
1779 "status=%s", bt_common_func_status_string(status));
1780 goto end;
1781 }
1782
1783end:
1784 bt_component_class_put_ref(comp_cls);
1785 return status;
1786}
1787
1788static
1789int append_descriptors_from_bt_config_component_array(
1790 bt_component_descriptor_set *comp_descr_set,
1791 GPtrArray *component_configs)
1792{
1793 int ret = 0;
1794 uint64_t i;
1795
1796 for (i = 0; i < component_configs->len; i++) {
1797 struct bt_config_component *cfg_comp =
1798 component_configs->pdata[i];
1799
1800 ret = add_descriptor_to_component_descriptor_set(
1801 comp_descr_set,
1802 cfg_comp->plugin_name->str,
1803 cfg_comp->comp_cls_name->str,
1804 cfg_comp->type, cfg_comp->params);
1805 if (ret) {
1806 goto end;
1807 }
1808 }
1809
1810end:
1811 return ret;
1812}
1813
1814static
1815bt_get_greatest_operative_mip_version_status get_greatest_operative_mip_version(
1816 struct bt_config *cfg, uint64_t *mip_version)
1817{
1818 bt_get_greatest_operative_mip_version_status status =
1819 BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_OK;
1820 bt_component_descriptor_set *comp_descr_set = NULL;
1821 int ret;
1822
1823 BT_ASSERT(cfg);
1824 BT_ASSERT(mip_version);
1825 comp_descr_set = bt_component_descriptor_set_create();
1826 if (!comp_descr_set) {
1827 BT_CLI_LOGE_APPEND_CAUSE(
1828 "Failed to create a component descriptor set object.");
1829 status = BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_MEMORY_ERROR;
1830 goto end;
1831 }
1832
1833 ret = append_descriptors_from_bt_config_component_array(
1834 comp_descr_set, cfg->cmd_data.run.sources);
1835 if (ret) {
1836 status = BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR;
1837 goto end;
1838 }
1839
1840 ret = append_descriptors_from_bt_config_component_array(
1841 comp_descr_set, cfg->cmd_data.run.filters);
1842 if (ret) {
1843 status = BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR;
1844 goto end;
1845 }
1846
1847 ret = append_descriptors_from_bt_config_component_array(
1848 comp_descr_set, cfg->cmd_data.run.sinks);
1849 if (ret) {
1850 status = BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR;
1851 goto end;
1852 }
1853
1854 if (cfg->cmd_data.run.stream_intersection_mode) {
1855 /*
1856 * Stream intersection mode adds `flt.utils.trimmer`
1857 * components; we need to include this type of component
1858 * in the component descriptor set to get the real
1859 * greatest operative MIP version.
1860 */
1861 ret = add_descriptor_to_component_descriptor_set(
1862 comp_descr_set, "utils", "trimmer",
1863 BT_COMPONENT_CLASS_TYPE_FILTER, NULL);
1864 if (ret) {
1865 status = BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR;
1866 goto end;
1867 }
1868 }
1869
1870 status = bt_get_greatest_operative_mip_version(comp_descr_set,
1871 bt_cli_log_level, mip_version);
1872
1873end:
1874 bt_component_descriptor_set_put_ref(comp_descr_set);
1875 return status;
1876}
1877
9009cc24
PP
1878static
1879int cmd_run_ctx_init(struct cmd_run_ctx *ctx, struct bt_config *cfg)
1880{
1881 int ret = 0;
9275bef4 1882 bt_graph_add_listener_status add_listener_status;
5fea82b8
PP
1883 bt_get_greatest_operative_mip_version_status mip_version_status;
1884 uint64_t mip_version = UINT64_C(-1);
9009cc24
PP
1885
1886 ctx->cfg = cfg;
1887 ctx->connect_ports = false;
d94d92ac 1888 ctx->src_components = g_hash_table_new_full(g_direct_hash,
398454ed 1889 g_direct_equal, NULL, (GDestroyNotify) bt_object_put_ref);
d94d92ac
PP
1890 if (!ctx->src_components) {
1891 goto error;
1892 }
1893
1894 ctx->flt_components = g_hash_table_new_full(g_direct_hash,
398454ed 1895 g_direct_equal, NULL, (GDestroyNotify) bt_object_put_ref);
d94d92ac
PP
1896 if (!ctx->flt_components) {
1897 goto error;
1898 }
1899
1900 ctx->sink_components = g_hash_table_new_full(g_direct_hash,
398454ed 1901 g_direct_equal, NULL, (GDestroyNotify) bt_object_put_ref);
d94d92ac 1902 if (!ctx->sink_components) {
9009cc24
PP
1903 goto error;
1904 }
1905
75a2cb9b
JG
1906 if (cfg->cmd_data.run.stream_intersection_mode) {
1907 ctx->stream_intersection_mode = true;
1908 ctx->intersections = g_hash_table_new_full(port_id_hash,
1909 port_id_equal, port_id_destroy, trace_range_destroy);
1910 if (!ctx->intersections) {
1911 goto error;
1912 }
1913 }
1914
5fea82b8
PP
1915 /*
1916 * Get the greatest operative MIP version to use to configure
1917 * the graph to create.
1918 */
1919 mip_version_status = get_greatest_operative_mip_version(
1920 cfg, &mip_version);
1921 if (mip_version_status == BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_NO_MATCH) {
1922 BT_CLI_LOGE_APPEND_CAUSE(
1923 "Failed to find an operative message interchange "
1924 "protocol version to use to create the `run` command's "
1925 "graph (components are not interoperable).");
1926 goto error;
1927 } else if (mip_version_status < 0) {
1928 BT_CLI_LOGE_APPEND_CAUSE(
1929 "Cannot find an operative message interchange "
1930 "protocol version to use to create the `run` command's "
1931 "graph: status=%s",
1932 bt_common_func_status_string(mip_version_status));
1933 goto error;
1934 }
1935
1936 BT_ASSERT(mip_version_status == BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_OK);
1937 BT_LOGI("Found operative message interchange protocol version to "
1938 "configure the `run` command's graph: mip-version=%" PRIu64,
1939 mip_version);
1940 ctx->graph = bt_graph_create(mip_version);
9009cc24
PP
1941 if (!ctx->graph) {
1942 goto error;
1943 }
1944
9b4f9b42 1945 bt_graph_add_interrupter(ctx->graph, the_interrupter);
9275bef4 1946 add_listener_status = bt_graph_add_source_component_output_port_added_listener(
d94d92ac
PP
1947 ctx->graph, graph_source_output_port_added_listener, NULL, ctx,
1948 NULL);
9275bef4 1949 if (add_listener_status != BT_GRAPH_ADD_LISTENER_STATUS_OK) {
cee05105
PP
1950 BT_CLI_LOGE_APPEND_CAUSE(
1951 "Cannot add \"port added\" listener to graph.");
9009cc24
PP
1952 goto error;
1953 }
1954
9275bef4 1955 add_listener_status = bt_graph_add_filter_component_output_port_added_listener(
d94d92ac
PP
1956 ctx->graph, graph_filter_output_port_added_listener, NULL, ctx,
1957 NULL);
9275bef4 1958 if (add_listener_status != BT_GRAPH_ADD_LISTENER_STATUS_OK) {
cee05105
PP
1959 BT_CLI_LOGE_APPEND_CAUSE(
1960 "Cannot add \"port added\" listener to graph.");
9009cc24
PP
1961 goto error;
1962 }
1963
1964 goto end;
1965
1966error:
1967 cmd_run_ctx_destroy(ctx);
1968 ret = -1;
1969
1970end:
1971 return ret;
1972}
1973
30947af0
SM
1974/*
1975 * Compute the intersection of all streams in the array `streams`, write it
1976 * in `range`.
1977 */
1978
1979static
1980int compute_stream_intersection(const bt_value *streams,
1981 struct trace_range *range)
1982{
f80e9ec1 1983 uint64_t i, stream_count;
30947af0
SM
1984 int ret;
1985
1986 BT_ASSERT(bt_value_get_type(streams) == BT_VALUE_TYPE_ARRAY);
1987
1988 stream_count = bt_value_array_get_length(streams);
1989
1990 BT_ASSERT(stream_count > 0);
1991
1992 range->intersection_range_begin_ns = 0;
1993 range->intersection_range_end_ns = UINT64_MAX;
1994
1995 for (i = 0; i < stream_count; i++) {
1996 int64_t begin_ns, end_ns;
1997 uint64_t begin_ns_u, end_ns_u;
1998 const bt_value *stream_value;
1999 const bt_value *range_ns_value;
2000 const bt_value *begin_value;
2001 const bt_value *end_value;
2002
2003 stream_value = bt_value_array_borrow_element_by_index_const(streams, i);
2004 if (bt_value_get_type(stream_value) != BT_VALUE_TYPE_MAP) {
5f2a1585 2005 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
30947af0
SM
2006 "expected streams array element to be a map, got %s.",
2007 bt_common_value_type_string(bt_value_get_type(stream_value)));
2008 goto error;
2009 }
2010
2011 range_ns_value = bt_value_map_borrow_entry_value_const(
2012 stream_value, "range-ns");
2013 if (!range_ns_value) {
5f2a1585 2014 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
30947af0
SM
2015 "missing expected `range-ns` key in stream map.");
2016 goto error;
2017 }
2018
2019 if (bt_value_get_type(range_ns_value) != BT_VALUE_TYPE_MAP) {
5f2a1585 2020 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
30947af0
SM
2021 "expected `range-ns` entry value of stream map to be a map, got %s.",
2022 bt_common_value_type_string(bt_value_get_type(range_ns_value)));
2023 goto error;
2024 }
2025
2026 begin_value = bt_value_map_borrow_entry_value_const(range_ns_value, "begin");
2027 if (!begin_value) {
5f2a1585 2028 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
30947af0
SM
2029 "missing expected `begin` key in range-ns map.");
2030 goto error;
2031 }
2032
2033 if (bt_value_get_type(begin_value) != BT_VALUE_TYPE_SIGNED_INTEGER) {
5f2a1585 2034 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
30947af0
SM
2035 "expected `begin` entry value of range-ns map to be a signed integer, got %s.",
2036 bt_common_value_type_string(bt_value_get_type(range_ns_value)));
2037 goto error;
2038 }
2039
2040 end_value = bt_value_map_borrow_entry_value_const(range_ns_value, "end");
2041 if (!end_value) {
5f2a1585 2042 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
30947af0
SM
2043 "missing expected `end` key in range-ns map.");
2044 goto error;
2045 }
2046
2047 if (bt_value_get_type(end_value) != BT_VALUE_TYPE_SIGNED_INTEGER) {
5f2a1585 2048 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
30947af0
SM
2049 "expected `end` entry value of range-ns map to be a signed integer, got %s.",
2050 bt_common_value_type_string(bt_value_get_type(range_ns_value)));
2051 goto error;
2052 }
2053
2054 begin_ns = bt_value_integer_signed_get(begin_value);
2055 end_ns = bt_value_integer_signed_get(end_value);
2056
2057 if (begin_ns < 0 || end_ns < 0 || end_ns < begin_ns) {
2058 BT_CLI_LOGE_APPEND_CAUSE(
2059 "Invalid stream range values: "
2060 "range-ns:begin=%" PRId64 ", "
2061 "range-ns:end=%" PRId64,
2062 begin_ns, end_ns);
30947af0
SM
2063 goto error;
2064 }
2065
2066 begin_ns_u = begin_ns;
2067 end_ns_u = end_ns;
2068
2069 range->intersection_range_begin_ns =
2070 MAX(range->intersection_range_begin_ns, begin_ns_u);
2071 range->intersection_range_end_ns =
2072 MIN(range->intersection_range_end_ns, end_ns_u);
2073 }
2074
2075 ret = 0;
2076 goto end;
2077error:
2078 ret = -1;
2079
2080end:
2081 return ret;
2082}
2083
75a2cb9b
JG
2084static
2085int set_stream_intersections(struct cmd_run_ctx *ctx,
2086 struct bt_config_component *cfg_comp,
b19ff26f 2087 const bt_component_class_source *src_comp_cls)
75a2cb9b
JG
2088{
2089 int ret = 0;
2090 uint64_t trace_idx;
f80e9ec1 2091 uint64_t trace_count;
b19ff26f
PP
2092 const bt_value *query_result = NULL;
2093 const bt_value *trace_info = NULL;
b19ff26f
PP
2094 const bt_value *stream_infos = NULL;
2095 const bt_value *stream_info = NULL;
75a2cb9b 2096 struct port_id *port_id = NULL;
30947af0 2097 struct trace_range *stream_intersection = NULL;
c7eee084 2098 const char *fail_reason = NULL;
b19ff26f 2099 const bt_component_class *comp_cls =
0d72b8c3 2100 bt_component_class_source_as_component_class_const(src_comp_cls);
75a2cb9b 2101
5f2a1585 2102 ret = query(ctx->cfg, comp_cls, "babeltrace.trace-infos",
f280892e 2103 cfg_comp->params, &query_result,
c7eee084
PP
2104 &fail_reason);
2105 if (ret) {
d4fb887f 2106 BT_CLI_LOGE_APPEND_CAUSE("Failed to execute `babeltrace.trace-infos` query: %s: "
c7eee084 2107 "comp-class-name=\"%s\"", fail_reason,
75a2cb9b
JG
2108 bt_component_class_get_name(comp_cls));
2109 ret = -1;
8dfb232c 2110 goto end;
75a2cb9b
JG
2111 }
2112
f6ccaed9
PP
2113 BT_ASSERT(query_result);
2114
75a2cb9b 2115 if (!bt_value_is_array(query_result)) {
5f2a1585 2116 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: expecting result to be an array: "
8dfb232c
SM
2117 "component-class-name=%s, actual-type=%s",
2118 bt_component_class_get_name(comp_cls),
2119 bt_common_value_type_string(bt_value_get_type(query_result)));
75a2cb9b 2120 ret = -1;
8dfb232c 2121 goto end;
75a2cb9b
JG
2122 }
2123
393729a6 2124 trace_count = bt_value_array_get_length(query_result);
f80e9ec1 2125 if (trace_count == 0) {
5f2a1585 2126 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: result is empty: "
8dfb232c 2127 "component-class-name=%s", bt_component_class_get_name(comp_cls));
75a2cb9b 2128 ret = -1;
8dfb232c 2129 goto end;
75a2cb9b
JG
2130 }
2131
2132 for (trace_idx = 0; trace_idx < trace_count; trace_idx++) {
75a2cb9b
JG
2133 uint64_t stream_idx;
2134 int64_t stream_count;
30947af0 2135 struct trace_range trace_intersection;
75a2cb9b 2136
05e21286 2137 trace_info = bt_value_array_borrow_element_by_index_const(
07208d85 2138 query_result, trace_idx);
8dfb232c 2139 if (!bt_value_is_map(trace_info)) {
75a2cb9b 2140 ret = -1;
5f2a1585 2141 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: expecting element to be a map: "
8dfb232c
SM
2142 "component-class-name=%s, actual-type=%s",
2143 bt_component_class_get_name(comp_cls),
2144 bt_common_value_type_string(bt_value_get_type(trace_info)));
2145 goto end;
75a2cb9b
JG
2146 }
2147
8dfb232c 2148 stream_infos = bt_value_map_borrow_entry_value_const(
5f2a1585 2149 trace_info, "stream-infos");
8dfb232c 2150 if (!stream_infos) {
75a2cb9b 2151 ret = -1;
5f2a1585 2152 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: missing `streams` key in trace info map: "
8dfb232c
SM
2153 "component-class-name=%s",
2154 bt_component_class_get_name(comp_cls));
2155 goto end;
2156 }
2157
2158 if (!bt_value_is_array(stream_infos)) {
2159 ret = -1;
5f2a1585 2160 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: expecting `streams` entry of trace info map to be an array: "
8dfb232c
SM
2161 "component-class-name=%s, actual-type=%s",
2162 bt_component_class_get_name(comp_cls),
2163 bt_common_value_type_string(bt_value_get_type(stream_infos)));
2164 goto end;
75a2cb9b
JG
2165 }
2166
393729a6 2167 stream_count = bt_value_array_get_length(stream_infos);
f80e9ec1 2168 if (stream_count == 0) {
75a2cb9b 2169 ret = -1;
5f2a1585 2170 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: list of streams is empty: "
8dfb232c
SM
2171 "component-class-name=%s",
2172 bt_component_class_get_name(comp_cls));
2173 goto end;
75a2cb9b
JG
2174 }
2175
30947af0
SM
2176 ret = compute_stream_intersection(stream_infos, &trace_intersection);
2177 if (ret != 0) {
2178 BT_CLI_LOGE_APPEND_CAUSE("Failed to compute trace streams intersection.");
8dfb232c 2179 goto end;
30947af0
SM
2180 }
2181
75a2cb9b 2182 for (stream_idx = 0; stream_idx < stream_count; stream_idx++) {
a38d7650 2183 const bt_value *port_name;
75a2cb9b
JG
2184
2185 port_id = g_new0(struct port_id, 1);
2186 if (!port_id) {
2187 ret = -1;
cee05105
PP
2188 BT_CLI_LOGE_APPEND_CAUSE(
2189 "Cannot allocate memory for port_id structure.");
8dfb232c 2190 goto end;
75a2cb9b
JG
2191 }
2192 port_id->instance_name = strdup(cfg_comp->instance_name->str);
2193 if (!port_id->instance_name) {
2194 ret = -1;
cee05105
PP
2195 BT_CLI_LOGE_APPEND_CAUSE(
2196 "Cannot allocate memory for port_id component instance name.");
8dfb232c 2197 goto end;
75a2cb9b
JG
2198 }
2199
30947af0
SM
2200 stream_intersection = g_new0(struct trace_range, 1);
2201 if (!stream_intersection) {
75a2cb9b 2202 ret = -1;
cee05105
PP
2203 BT_CLI_LOGE_APPEND_CAUSE(
2204 "Cannot allocate memory for trace_range structure.");
8dfb232c 2205 goto end;
75a2cb9b 2206 }
30947af0
SM
2207
2208 *stream_intersection = trace_intersection;
75a2cb9b 2209
05e21286 2210 stream_info = bt_value_array_borrow_element_by_index_const(
07208d85 2211 stream_infos, stream_idx);
8dfb232c 2212 if (!bt_value_is_map(stream_info)) {
75a2cb9b 2213 ret = -1;
5f2a1585 2214 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: "
8dfb232c
SM
2215 "expecting element of stream list to be a map: "
2216 "component-class-name=%s, actual-type=%s",
2217 bt_component_class_get_name(comp_cls),
2218 bt_common_value_type_string(bt_value_get_type(stream_info)));
2219 goto end;
75a2cb9b
JG
2220 }
2221
a38d7650 2222 port_name = bt_value_map_borrow_entry_value_const(stream_info, "port-name");
8dfb232c 2223 if (!port_name) {
75a2cb9b 2224 ret = -1;
5f2a1585 2225 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: "
8dfb232c
SM
2226 "missing `port-name` key in stream info map: "
2227 "component-class-name=%s",
2228 bt_component_class_get_name(comp_cls));
2229 goto end;
2230 }
2231
2232 if (!bt_value_is_string(port_name)) {
2233 ret = -1;
5f2a1585 2234 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: "
8dfb232c
SM
2235 "expecting `port-name` entry of stream info map to be a string: "
2236 "component-class-name=%s, actual-type=%s",
2237 bt_component_class_get_name(comp_cls),
2238 bt_common_value_type_string(bt_value_get_type(port_name)));
2239 goto end;
75a2cb9b
JG
2240 }
2241
a38d7650 2242 port_id->port_name = g_strdup(bt_value_string_get(port_name));
75a2cb9b
JG
2243 if (!port_id->port_name) {
2244 ret = -1;
cee05105
PP
2245 BT_CLI_LOGE_APPEND_CAUSE(
2246 "Cannot allocate memory for port_id port_name.");
8dfb232c 2247 goto end;
75a2cb9b
JG
2248 }
2249
2250 BT_LOGD("Inserting stream intersection ");
2251
30947af0 2252 g_hash_table_insert(ctx->intersections, port_id, stream_intersection);
75a2cb9b
JG
2253
2254 port_id = NULL;
30947af0 2255 stream_intersection = NULL;
75a2cb9b 2256 }
75a2cb9b
JG
2257 }
2258
8dfb232c 2259 ret = 0;
cee05105 2260
75a2cb9b 2261end:
c5b9b441 2262 bt_value_put_ref(query_result);
75a2cb9b 2263 g_free(port_id);
30947af0 2264 g_free(stream_intersection);
75a2cb9b
JG
2265 return ret;
2266}
2267
9009cc24
PP
2268static
2269int cmd_run_ctx_create_components_from_config_components(
2270 struct cmd_run_ctx *ctx, GPtrArray *cfg_components)
2271{
2272 size_t i;
0d72b8c3
PP
2273 const void *comp_cls = NULL;
2274 const void *comp = NULL;
9009cc24
PP
2275 int ret = 0;
2276
2277 for (i = 0; i < cfg_components->len; i++) {
2278 struct bt_config_component *cfg_comp =
2279 g_ptr_array_index(cfg_components, i);
2280 GQuark quark;
2281
d94d92ac
PP
2282 switch (cfg_comp->type) {
2283 case BT_COMPONENT_CLASS_TYPE_SOURCE:
2284 comp_cls = find_source_component_class(
2285 cfg_comp->plugin_name->str,
2286 cfg_comp->comp_cls_name->str);
2287 break;
2288 case BT_COMPONENT_CLASS_TYPE_FILTER:
2289 comp_cls = find_filter_component_class(
2290 cfg_comp->plugin_name->str,
2291 cfg_comp->comp_cls_name->str);
2292 break;
2293 case BT_COMPONENT_CLASS_TYPE_SINK:
2294 comp_cls = find_sink_component_class(
2295 cfg_comp->plugin_name->str,
2296 cfg_comp->comp_cls_name->str);
2297 break;
2298 default:
498e7994 2299 bt_common_abort();
d94d92ac
PP
2300 }
2301
9009cc24 2302 if (!comp_cls) {
cee05105
PP
2303 BT_CLI_LOGE_APPEND_CAUSE(
2304 "Cannot find component class: plugin-name=\"%s\", "
7213a328
PP
2305 "comp-cls-name=\"%s\", comp-cls-type=%d",
2306 cfg_comp->plugin_name->str,
2307 cfg_comp->comp_cls_name->str,
2308 cfg_comp->type);
9009cc24
PP
2309 goto error;
2310 }
2311
ef267d12 2312 BT_ASSERT(cfg_comp->log_level >= BT_LOG_TRACE);
29da2ffc 2313
d94d92ac
PP
2314 switch (cfg_comp->type) {
2315 case BT_COMPONENT_CLASS_TYPE_SOURCE:
0d72b8c3 2316 ret = bt_graph_add_source_component(ctx->graph,
d94d92ac 2317 comp_cls, cfg_comp->instance_name->str,
29da2ffc 2318 cfg_comp->params, cfg_comp->log_level,
d94d92ac
PP
2319 (void *) &comp);
2320 break;
2321 case BT_COMPONENT_CLASS_TYPE_FILTER:
0d72b8c3 2322 ret = bt_graph_add_filter_component(ctx->graph,
d94d92ac 2323 comp_cls, cfg_comp->instance_name->str,
29da2ffc 2324 cfg_comp->params, cfg_comp->log_level,
d94d92ac
PP
2325 (void *) &comp);
2326 break;
2327 case BT_COMPONENT_CLASS_TYPE_SINK:
0d72b8c3 2328 ret = bt_graph_add_sink_component(ctx->graph,
d94d92ac 2329 comp_cls, cfg_comp->instance_name->str,
29da2ffc 2330 cfg_comp->params, cfg_comp->log_level,
d94d92ac
PP
2331 (void *) &comp);
2332 break;
2333 default:
498e7994 2334 bt_common_abort();
d94d92ac
PP
2335 }
2336
36712f1d 2337 if (ret) {
cee05105
PP
2338 BT_CLI_LOGE_APPEND_CAUSE(
2339 "Cannot create component: plugin-name=\"%s\", "
32e87ceb 2340 "comp-cls-name=\"%s\", comp-cls-type=%d, "
7213a328
PP
2341 "comp-name=\"%s\"",
2342 cfg_comp->plugin_name->str,
2343 cfg_comp->comp_cls_name->str,
2344 cfg_comp->type, cfg_comp->instance_name->str);
9009cc24
PP
2345 goto error;
2346 }
2347
75a2cb9b
JG
2348 if (ctx->stream_intersection_mode &&
2349 cfg_comp->type == BT_COMPONENT_CLASS_TYPE_SOURCE) {
2350 ret = set_stream_intersections(ctx, cfg_comp, comp_cls);
2351 if (ret) {
8dfb232c
SM
2352 BT_CLI_LOGE_APPEND_CAUSE(
2353 "Cannot determine stream intersection of trace.");
75a2cb9b
JG
2354 goto error;
2355 }
2356 }
2357
7213a328
PP
2358 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2359 comp, cfg_comp->instance_name->str);
9009cc24 2360 quark = g_quark_from_string(cfg_comp->instance_name->str);
f6ccaed9 2361 BT_ASSERT(quark > 0);
d94d92ac
PP
2362
2363 switch (cfg_comp->type) {
2364 case BT_COMPONENT_CLASS_TYPE_SOURCE:
2365 g_hash_table_insert(ctx->src_components,
0d72b8c3 2366 GUINT_TO_POINTER(quark), (void *) comp);
d94d92ac
PP
2367 break;
2368 case BT_COMPONENT_CLASS_TYPE_FILTER:
2369 g_hash_table_insert(ctx->flt_components,
0d72b8c3 2370 GUINT_TO_POINTER(quark), (void *) comp);
d94d92ac
PP
2371 break;
2372 case BT_COMPONENT_CLASS_TYPE_SINK:
2373 g_hash_table_insert(ctx->sink_components,
0d72b8c3 2374 GUINT_TO_POINTER(quark), (void *) comp);
d94d92ac
PP
2375 break;
2376 default:
498e7994 2377 bt_common_abort();
d94d92ac
PP
2378 }
2379
9009cc24 2380 comp = NULL;
65300d60 2381 BT_OBJECT_PUT_REF_AND_RESET(comp_cls);
9009cc24
PP
2382 }
2383
2384 goto end;
2385
2386error:
2387 ret = -1;
2388
2389end:
65300d60
PP
2390 bt_object_put_ref(comp);
2391 bt_object_put_ref(comp_cls);
9009cc24
PP
2392 return ret;
2393}
56a1cced 2394
9009cc24
PP
2395static
2396int cmd_run_ctx_create_components(struct cmd_run_ctx *ctx)
2397{
2398 int ret = 0;
2399
2400 /*
2401 * Make sure that, during this phase, our graph's "port added"
2402 * listener does not connect ports while we are creating the
2403 * components because we have a special, initial phase for
2404 * this.
2405 */
2406 ctx->connect_ports = false;
2407
2408 ret = cmd_run_ctx_create_components_from_config_components(
2409 ctx, ctx->cfg->cmd_data.run.sources);
2410 if (ret) {
7c7c0433 2411 ret = -1;
2e339de1
JG
2412 goto end;
2413 }
2414
9009cc24
PP
2415 ret = cmd_run_ctx_create_components_from_config_components(
2416 ctx, ctx->cfg->cmd_data.run.filters);
6c2f3ee5 2417 if (ret) {
290725f7 2418 ret = -1;
fec2a9f2
JG
2419 goto end;
2420 }
78586d8a 2421
9009cc24
PP
2422 ret = cmd_run_ctx_create_components_from_config_components(
2423 ctx, ctx->cfg->cmd_data.run.sinks);
2424 if (ret) {
2425 ret = -1;
2426 goto end;
2427 }
2428
2429end:
2430 return ret;
2431}
2432
0d72b8c3 2433typedef uint64_t (*output_port_count_func_t)(const void *);
b19ff26f 2434typedef const bt_port_output *(*borrow_output_port_by_index_func_t)(
0d72b8c3 2435 const void *, uint64_t);
d94d92ac 2436
9009cc24
PP
2437static
2438int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx *ctx,
d94d92ac
PP
2439 void *comp, output_port_count_func_t port_count_fn,
2440 borrow_output_port_by_index_func_t port_by_index_fn)
9009cc24
PP
2441{
2442 int ret = 0;
d94d92ac 2443 uint64_t count;
9009cc24
PP
2444 uint64_t i;
2445
2446 count = port_count_fn(comp);
9009cc24
PP
2447
2448 for (i = 0; i < count; i++) {
b19ff26f 2449 const bt_port_output *upstream_port = port_by_index_fn(comp, i);
9009cc24 2450
f6ccaed9 2451 BT_ASSERT(upstream_port);
9009cc24 2452 ret = cmd_run_ctx_connect_upstream_port(ctx, upstream_port);
9009cc24
PP
2453 if (ret) {
2454 goto end;
2455 }
2456 }
2457
2458end:
2459 return ret;
2460}
2461
2462static
2463int cmd_run_ctx_connect_ports(struct cmd_run_ctx *ctx)
2464{
2465 int ret = 0;
2466 GHashTableIter iter;
2467 gpointer g_name_quark, g_comp;
2468
2469 ctx->connect_ports = true;
d94d92ac 2470 g_hash_table_iter_init(&iter, ctx->src_components);
9009cc24
PP
2471
2472 while (g_hash_table_iter_next(&iter, &g_name_quark, &g_comp)) {
d94d92ac
PP
2473 ret = cmd_run_ctx_connect_comp_ports(ctx, g_comp,
2474 (output_port_count_func_t)
2475 bt_component_source_get_output_port_count,
2476 (borrow_output_port_by_index_func_t)
0d72b8c3 2477 bt_component_source_borrow_output_port_by_index_const);
d94d92ac
PP
2478 if (ret) {
2479 goto end;
9009cc24 2480 }
d94d92ac
PP
2481 }
2482
2483 g_hash_table_iter_init(&iter, ctx->flt_components);
9009cc24 2484
d94d92ac
PP
2485 while (g_hash_table_iter_next(&iter, &g_name_quark, &g_comp)) {
2486 ret = cmd_run_ctx_connect_comp_ports(ctx, g_comp,
2487 (output_port_count_func_t)
2488 bt_component_filter_get_output_port_count,
2489 (borrow_output_port_by_index_func_t)
0d72b8c3 2490 bt_component_filter_borrow_output_port_by_index_const);
9009cc24
PP
2491 if (ret) {
2492 goto end;
2493 }
2494 }
2495
2496end:
2497 return ret;
2498}
2499
2500static
b5cd7d65 2501enum bt_cmd_status cmd_run(struct bt_config *cfg)
9009cc24 2502{
b5cd7d65 2503 enum bt_cmd_status cmd_status;
9009cc24
PP
2504 struct cmd_run_ctx ctx = { 0 };
2505
9009cc24
PP
2506 /* Initialize the command's context and the graph object */
2507 if (cmd_run_ctx_init(&ctx, cfg)) {
cee05105
PP
2508 BT_CLI_LOGE_APPEND_CAUSE(
2509 "Cannot initialize the command's context.");
9009cc24
PP
2510 goto error;
2511 }
2512
aede8fc9 2513 if (bt_interrupter_is_set(the_interrupter)) {
63d355d2 2514 BT_CLI_LOGW_APPEND_CAUSE(
9b4f9b42 2515 "Interrupted by user before creating components.");
cc308374
PP
2516 goto error;
2517 }
2518
2519 BT_LOGI_STR("Creating components.");
2520
9009cc24
PP
2521 /* Create the requested component instances */
2522 if (cmd_run_ctx_create_components(&ctx)) {
cee05105 2523 BT_CLI_LOGE_APPEND_CAUSE("Cannot create components.");
9009cc24
PP
2524 goto error;
2525 }
2526
aede8fc9 2527 if (bt_interrupter_is_set(the_interrupter)) {
63d355d2 2528 BT_CLI_LOGW_APPEND_CAUSE(
9b4f9b42 2529 "Interrupted by user before connecting components.");
cc308374
PP
2530 goto error;
2531 }
2532
2533 BT_LOGI_STR("Connecting components.");
2534
9009cc24
PP
2535 /* Connect the initially visible component ports */
2536 if (cmd_run_ctx_connect_ports(&ctx)) {
cee05105
PP
2537 BT_CLI_LOGE_APPEND_CAUSE(
2538 "Cannot connect initial component ports.");
9009cc24
PP
2539 goto error;
2540 }
2541
7213a328
PP
2542 BT_LOGI_STR("Running the graph.");
2543
9009cc24 2544 /* Run the graph */
fec2a9f2 2545 while (true) {
d24d5663 2546 bt_graph_run_status run_status = bt_graph_run(ctx.graph);
61ddbc8a 2547
5669a3e7
PP
2548 /*
2549 * Reset console in case something messed with console
2550 * codes during the graph's execution.
2551 */
2552 printf("%s", bt_common_color_reset());
2553 fflush(stdout);
2554 fprintf(stderr, "%s", bt_common_color_reset());
ef267d12 2555 BT_LOGT("bt_graph_run() returned: status=%s",
d24d5663 2556 bt_common_func_status_string(run_status));
fd948396 2557
d24d5663
PP
2558 switch (run_status) {
2559 case BT_GRAPH_RUN_STATUS_OK:
b5cd7d65 2560 cmd_status = BT_CMD_STATUS_OK;
9669d693 2561 goto end;
d24d5663 2562 case BT_GRAPH_RUN_STATUS_AGAIN:
9b4f9b42 2563 if (bt_interrupter_is_set(the_interrupter)) {
b5cd7d65
FD
2564 /*
2565 * The graph was interrupted by a SIGINT.
2566 */
2567 cmd_status = BT_CMD_STATUS_INTERRUPTED;
2568 goto end;
5401f780
PP
2569 }
2570
9009cc24 2571 if (cfg->cmd_data.run.retry_duration_us > 0) {
d24d5663 2572 BT_LOGT("Got BT_GRAPH_RUN_STATUS_AGAIN: sleeping: "
7213a328
PP
2573 "time-us=%" PRIu64,
2574 cfg->cmd_data.run.retry_duration_us);
2575
9009cc24 2576 if (usleep(cfg->cmd_data.run.retry_duration_us)) {
9b4f9b42 2577 if (bt_interrupter_is_set(the_interrupter)) {
b5cd7d65
FD
2578 cmd_status = BT_CMD_STATUS_INTERRUPTED;
2579 goto end;
cfa4637b 2580 }
9009cc24
PP
2581 }
2582 }
78586d8a 2583 break;
fec2a9f2 2584 default:
9b4f9b42 2585 if (bt_interrupter_is_set(the_interrupter)) {
b5cd7d65
FD
2586 cmd_status = BT_CMD_STATUS_INTERRUPTED;
2587 goto end;
9b4f9b42
PP
2588 }
2589
cee05105
PP
2590 BT_CLI_LOGE_APPEND_CAUSE(
2591 "Graph failed to complete successfully");
9009cc24 2592 goto error;
78586d8a 2593 }
fec2a9f2 2594 }
9009cc24 2595error:
b5cd7d65 2596 cmd_status = BT_CMD_STATUS_ERROR;
9009cc24 2597
11e1d048 2598end:
9009cc24 2599 cmd_run_ctx_destroy(&ctx);
b5cd7d65 2600 return cmd_status;
290725f7
PP
2601}
2602
9009cc24
PP
2603static
2604void warn_command_name_and_directory_clash(struct bt_config *cfg)
290725f7 2605{
9009cc24
PP
2606 const char *env_clash;
2607
290725f7
PP
2608 if (!cfg->command_name) {
2609 return;
2610 }
2611
9009cc24
PP
2612 env_clash = getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH);
2613 if (env_clash && strcmp(env_clash, "0") == 0) {
2614 return;
2615 }
2616
290725f7
PP
2617 if (g_file_test(cfg->command_name,
2618 G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
cee05105 2619 _bt_log_write_d(_BT_LOG_SRCLOC_FUNCTION, __FILE__, __LINE__,
770538dd 2620 BT_LOG_WARNING, BT_LOG_TAG,
cee05105
PP
2621 "The `%s` command was executed. "
2622 "If you meant to convert a trace located in "
2623 "the local `%s` directory, please use:\n\n"
2624 " babeltrace2 convert %s [OPTIONS]",
2625 cfg->command_name, cfg->command_name,
2626 cfg->command_name);
290725f7
PP
2627 }
2628}
2629
7213a328
PP
2630static
2631void init_log_level(void)
2632{
c6d4d1ae 2633 bt_cli_log_level = bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL);
7213a328
PP
2634}
2635
f393c19b
PP
2636static
2637void print_error_causes(void)
2638{
2639 const bt_error *error = bt_current_thread_take_error();
2640 int64_t i;
2641 GString *folded = NULL;
2642 unsigned int columns;
2643
2644 if (!error || bt_error_get_cause_count(error) == 0) {
2645 fprintf(stderr, "%s%sUnknown command-line error.%s\n",
04609487 2646 bt_common_color_bold(), bt_common_color_fg_bright_red(),
f393c19b
PP
2647 bt_common_color_reset());
2648 goto end;
2649 }
2650
2651 /* Try to get terminal width to fold the error cause messages */
2652 if (bt_common_get_term_size(&columns, NULL) < 0) {
2653 /* Width not found: default to 80 */
2654 columns = 80;
2655 }
2656
2657 /*
2658 * This helps visually separate the error causes from the last
2659 * logging statement.
2660 */
2661 fprintf(stderr, "\n");
2662
2663 /* Reverse order: deepest (root) cause printed at the end */
2664 for (i = bt_error_get_cause_count(error) - 1; i >= 0; i--) {
2665 const bt_error_cause *cause =
2666 bt_error_borrow_cause_by_index(error, (uint64_t) i);
2667 const char *prefix_fmt =
2668 i == bt_error_get_cause_count(error) - 1 ?
2669 "%s%sERROR%s: " : "%s%sCAUSED BY%s ";
2670
2671 /* Print prefix */
2672 fprintf(stderr, prefix_fmt,
04609487 2673 bt_common_color_bold(), bt_common_color_fg_bright_red(),
f393c19b
PP
2674 bt_common_color_reset());
2675
2676 /* Print actor name */
2677 fprintf(stderr, "[");
2678 switch (bt_error_cause_get_actor_type(cause)) {
2679 case BT_ERROR_CAUSE_ACTOR_TYPE_UNKNOWN:
2680 fprintf(stderr, "%s%s%s",
2681 bt_common_color_bold(),
2682 bt_error_cause_get_module_name(cause),
2683 bt_common_color_reset());
2684 break;
2685 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT:
2686 fprintf(stderr, "%s%s%s: ",
2687 bt_common_color_bold(),
2688 bt_error_cause_component_actor_get_component_name(cause),
2689 bt_common_color_reset());
2690 print_plugin_comp_cls_opt(stderr,
2691 bt_error_cause_component_actor_get_plugin_name(cause),
2692 bt_error_cause_component_actor_get_component_class_name(cause),
2693 bt_error_cause_component_actor_get_component_class_type(cause));
2694 break;
2695 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS:
2696 print_plugin_comp_cls_opt(stderr,
2697 bt_error_cause_component_class_actor_get_plugin_name(cause),
2698 bt_error_cause_component_class_actor_get_component_class_name(cause),
2699 bt_error_cause_component_class_actor_get_component_class_type(cause));
2700 break;
2701 case BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR:
2702 fprintf(stderr, "%s%s%s (%s%s%s): ",
2703 bt_common_color_bold(),
2704 bt_error_cause_message_iterator_actor_get_component_name(cause),
2705 bt_common_color_reset(),
2706 bt_common_color_bold(),
2707 bt_error_cause_message_iterator_actor_get_component_output_port_name(cause),
2708 bt_common_color_reset());
2709 print_plugin_comp_cls_opt(stderr,
2710 bt_error_cause_message_iterator_actor_get_plugin_name(cause),
2711 bt_error_cause_message_iterator_actor_get_component_class_name(cause),
2712 bt_error_cause_message_iterator_actor_get_component_class_type(cause));
2713 break;
2714 default:
498e7994 2715 bt_common_abort();
f393c19b
PP
2716 }
2717
2718 /* Print file name and line number */
2719 fprintf(stderr, "] (%s%s%s%s:%s%" PRIu64 "%s)\n",
2720 bt_common_color_bold(),
04609487 2721 bt_common_color_fg_bright_magenta(),
f393c19b
PP
2722 bt_error_cause_get_file_name(cause),
2723 bt_common_color_reset(),
2724 bt_common_color_fg_green(),
2725 bt_error_cause_get_line_number(cause),
2726 bt_common_color_reset());
2727
2728 /* Print message */
2729 folded = bt_common_fold(bt_error_cause_get_message(cause),
2730 columns, 2);
2731 if (!folded) {
2732 BT_LOGE_STR("Could not fold string.");
2733 fprintf(stderr, "%s\n",
2734 bt_error_cause_get_message(cause));
2735 continue;
2736 }
2737
2738 fprintf(stderr, "%s\n", folded->str);
2739 g_string_free(folded, TRUE);
2740 folded = NULL;
2741 }
2742
2743end:
6c2baa60 2744 BT_ASSERT(!folded);
f393c19b
PP
2745
2746 if (error) {
2747 bt_error_release(error);
2748 }
2749}
2750
290725f7
PP
2751int main(int argc, const char **argv)
2752{
b5cd7d65
FD
2753 int ret, retcode;
2754 enum bt_cmd_status cmd_status;
3dae1685 2755 struct bt_config *cfg = NULL;
290725f7 2756
7213a328 2757 init_log_level();
65d3198f 2758 set_signal_handler();
743138a3 2759 init_loaded_plugins();
3dae1685
SM
2760
2761 BT_ASSERT(!the_interrupter);
2762 the_interrupter = bt_interrupter_create();
2763 if (!the_interrupter) {
2764 BT_CLI_LOGE_APPEND_CAUSE("Failed to create an interrupter object.");
2765 retcode = 1;
2766 goto end;
2767 }
2768
2769 cfg = bt_config_cli_args_create_with_default(argc, argv, &retcode,
2770 the_interrupter);
290725f7
PP
2771
2772 if (retcode < 0) {
2773 /* Quit without errors; typically usage/version */
2774 retcode = 0;
7213a328 2775 BT_LOGI_STR("Quitting without errors.");
290725f7
PP
2776 goto end;
2777 }
2778
2779 if (retcode > 0) {
cee05105
PP
2780 BT_CLI_LOGE_APPEND_CAUSE(
2781 "Command-line error: retcode=%d", retcode);
290725f7
PP
2782 goto end;
2783 }
2784
2785 if (!cfg) {
cee05105 2786 BT_CLI_LOGE_APPEND_CAUSE(
9442c9a2 2787 "Failed to create a valid Babeltrace CLI configuration.");
db0f160a 2788 retcode = 1;
290725f7
PP
2789 goto end;
2790 }
2791
290725f7
PP
2792 print_cfg(cfg);
2793
db0f160a 2794 if (cfg->command_needs_plugins) {
73760435 2795 ret = require_loaded_plugins(cfg->plugin_paths);
db0f160a 2796 if (ret) {
cee05105
PP
2797 BT_CLI_LOGE_APPEND_CAUSE(
2798 "Failed to load plugins: ret=%d", ret);
db0f160a
PP
2799 retcode = 1;
2800 goto end;
2801 }
2802 }
2803
7213a328
PP
2804 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2805 cfg->command, cfg->command_name);
2806
290725f7 2807 switch (cfg->command) {
db0f160a 2808 case BT_CONFIG_COMMAND_RUN:
b5cd7d65 2809 cmd_status = cmd_run(cfg);
290725f7
PP
2810 break;
2811 case BT_CONFIG_COMMAND_LIST_PLUGINS:
b5cd7d65 2812 cmd_status = cmd_list_plugins(cfg);
290725f7 2813 break;
22e22462 2814 case BT_CONFIG_COMMAND_HELP:
b5cd7d65 2815 cmd_status = cmd_help(cfg);
22e22462 2816 break;
a67681c1 2817 case BT_CONFIG_COMMAND_QUERY:
b5cd7d65 2818 cmd_status = cmd_query(cfg);
63ce0e1d 2819 break;
db0f160a 2820 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA:
b5cd7d65 2821 cmd_status = cmd_print_ctf_metadata(cfg);
db0f160a
PP
2822 break;
2823 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS:
b5cd7d65 2824 cmd_status = cmd_print_lttng_live_sessions(cfg);
db0f160a 2825 break;
290725f7 2826 default:
0fbb9a9f 2827 BT_LOGF("Invalid/unknown command: cmd=%d", cfg->command);
498e7994 2828 bt_common_abort();
290725f7
PP
2829 }
2830
b5cd7d65
FD
2831 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", command-status=\"%s\"",
2832 cfg->command, cfg->command_name, bt_cmd_status_string(cmd_status));
290725f7 2833 warn_command_name_and_directory_clash(cfg);
b5cd7d65
FD
2834
2835 switch (cmd_status) {
2836 case BT_CMD_STATUS_OK:
2837 retcode = 0;
2838 break;
2839 case BT_CMD_STATUS_ERROR:
2840 retcode = 1;
2841 print_error_causes();
2842 break;
2843 case BT_CMD_STATUS_INTERRUPTED:
2844 retcode = 2;
2845 break;
2846 default:
2847 BT_LOGF("Invalid command status: cmd-status=%d", cmd_status);
2848 bt_common_abort();
2849 }
290725f7
PP
2850
2851end:
65300d60 2852 BT_OBJECT_PUT_REF_AND_RESET(cfg);
743138a3 2853 fini_loaded_plugins();
9b4f9b42 2854 bt_interrupter_put_ref(the_interrupter);
0232a535
PP
2855
2856 /*
2857 * Clear current thread's error in case there is one to avoid a
2858 * memory leak.
2859 */
2860 bt_current_thread_clear_error();
290725f7 2861 return retcode;
4c8bfb7e 2862}
This page took 0.276417 seconds and 4 git commands to generate.