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