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