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