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