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