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