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