lib: graph API: return borrowed references when adding to an object
[babeltrace.git] / src / cli / babeltrace2.c
CommitLineData
34ac0e6c 1/*
64fa3fec
MD
2 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
3 *
4 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
34ac0e6c
MD
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
c462e188
MD
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
34ac0e6c 23 */
4c8bfb7e 24
ea9f8b29
PP
25#define BT_LOG_TAG "CLI"
26#include "logging.h"
27
3fadfbc0 28#include <babeltrace2/babeltrace.h>
578e048b 29#include "common/common.h"
96c1b5fe 30#include "string-format/format-error.h"
ec532b44 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>
ec2c5e50
MJ
40#include "babeltrace2-cfg.h"
41#include "babeltrace2-cfg-cli-args.h"
42#include "babeltrace2-cfg-cli-args-default.h"
ebddaeb8 43#include "babeltrace2-log-level.h"
743138a3 44#include "babeltrace2-plugins.h"
3c729b9a 45#include "babeltrace2-query.h"
9009cc24
PP
46
47#define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH"
75a2cb9b 48#define NSEC_PER_SEC 1000000000LL
34ac0e6c 49
b5cd7d65
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
9b4f9b42
PP
71/* Application's interrupter (owned by this) */
72static bt_interrupter *the_interrupter;
5401f780 73
65d3198f 74#ifdef __MINGW32__
a2d06fd5 75
65d3198f
MJ
76#include <windows.h>
77
78static
79BOOL WINAPI signal_handler(DWORD signal) {
9b4f9b42
PP
80 if (the_interrupter) {
81 bt_interrupter_set(the_interrupter);
65d3198f
MJ
82 }
83
65d3198f
MJ
84 return TRUE;
85}
86
5401f780 87static
65d3198f
MJ
88void set_signal_handler(void)
89{
90 if (!SetConsoleCtrlHandler(signal_handler, TRUE)) {
3f7d4d90 91 BT_LOGE("Failed to set the Ctrl+C handler.");
65d3198f
MJ
92 }
93}
a2d06fd5 94
65d3198f 95#else /* __MINGW32__ */
a2d06fd5 96
65d3198f
MJ
97static
98void signal_handler(int signum)
5401f780
PP
99{
100 if (signum != SIGINT) {
101 return;
102 }
103
9b4f9b42
PP
104 if (the_interrupter) {
105 bt_interrupter_set(the_interrupter);
c7eee084 106 }
5401f780
PP
107}
108
65d3198f
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}
a2d06fd5 123
65d3198f
MJ
124#endif /* __MINGW32__ */
125
c7eee084 126static
f4e38e70
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)
c7eee084 130{
3c729b9a
PP
131 return cli_query(comp_cls, obj, params, cfg->log_level,
132 the_interrupter, user_result, fail_reason);
c7eee084
PP
133}
134
0d72b8c3 135typedef const void *(*plugin_borrow_comp_cls_func_t)(
b19ff26f 136 const bt_plugin *, const char *);
d94d92ac 137
33b34c43 138static
0d72b8c3 139const void *find_component_class_from_plugin(const char *plugin_name,
33b34c43 140 const char *comp_class_name,
d94d92ac 141 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func)
33b34c43 142{
0d72b8c3 143 const void *comp_class = NULL;
b19ff26f 144 const bt_plugin *plugin;
7213a328 145
3f7d4d90 146 BT_LOGI("Finding component class: plugin-name=\"%s\", "
d94d92ac 147 "comp-cls-name=\"%s\"", plugin_name, comp_class_name);
7213a328 148
97512754 149 plugin = borrow_loaded_plugin_by_name(plugin_name);
33b34c43
PP
150 if (!plugin) {
151 goto end;
152 }
153
398454ed
PP
154 comp_class = plugin_borrow_comp_cls_func(plugin, comp_class_name);
155 bt_object_get_ref(comp_class);
7213a328 156
33b34c43 157end:
3f7d4d90
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
d94d92ac 170static
b19ff26f 171const bt_component_class_source *find_source_component_class(
d94d92ac
PP
172 const char *plugin_name, const char *comp_class_name)
173{
0d72b8c3
PP
174 return (const void *) find_component_class_from_plugin(
175 plugin_name, comp_class_name,
d94d92ac 176 (plugin_borrow_comp_cls_func_t)
92fed4e1 177 bt_plugin_borrow_source_component_class_by_name_const);
d94d92ac
PP
178}
179
180static
b19ff26f 181const bt_component_class_filter *find_filter_component_class(
d94d92ac
PP
182 const char *plugin_name, const char *comp_class_name)
183{
0d72b8c3
PP
184 return (const void *) find_component_class_from_plugin(
185 plugin_name, comp_class_name,
d94d92ac 186 (plugin_borrow_comp_cls_func_t)
92fed4e1 187 bt_plugin_borrow_filter_component_class_by_name_const);
d94d92ac
PP
188}
189
190static
b19ff26f 191const bt_component_class_sink *find_sink_component_class(
d94d92ac
PP
192 const char *plugin_name, const char *comp_class_name)
193{
0d72b8c3 194 return (const void *) find_component_class_from_plugin(plugin_name,
d94d92ac
PP
195 comp_class_name,
196 (plugin_borrow_comp_cls_func_t)
92fed4e1 197 bt_plugin_borrow_sink_component_class_by_name_const);
d94d92ac
PP
198}
199
200static
b19ff26f 201const bt_component_class *find_component_class(const char *plugin_name,
d94d92ac 202 const char *comp_class_name,
4cdfc5e8 203 bt_component_class_type comp_class_type)
d94d92ac 204{
b19ff26f 205 const bt_component_class *comp_cls = NULL;
d94d92ac
PP
206
207 switch (comp_class_type) {
208 case BT_COMPONENT_CLASS_TYPE_SOURCE:
0d72b8c3 209 comp_cls = bt_component_class_source_as_component_class_const(find_source_component_class(plugin_name, comp_class_name));
d94d92ac
PP
210 break;
211 case BT_COMPONENT_CLASS_TYPE_FILTER:
0d72b8c3 212 comp_cls = bt_component_class_filter_as_component_class_const(find_filter_component_class(plugin_name, comp_class_name));
d94d92ac
PP
213 break;
214 case BT_COMPONENT_CLASS_TYPE_SINK:
0d72b8c3 215 comp_cls = bt_component_class_sink_as_component_class_const(find_sink_component_class(plugin_name, comp_class_name));
d94d92ac
PP
216 break;
217 default:
498e7994 218 bt_common_abort();
d94d92ac
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
b19ff26f 235void print_value(FILE *, const bt_value *, size_t);
c42c79ea 236
c1081aa6 237static
b19ff26f 238void print_value_rec(FILE *, const bt_value *, size_t);
7213a328 239
c42c79ea 240static
13763113
SM
241void print_map_value(const char *key, const bt_value *object, FILE *fp,
242 size_t indent)
c42c79ea 243{
13763113
SM
244 print_indent(fp, indent);
245 fprintf(fp, "%s: ", key);
f6ccaed9 246 BT_ASSERT(object);
290725f7
PP
247
248 if (bt_value_is_array(object) &&
249 bt_value_array_is_empty(object)) {
13763113
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)) {
13763113
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)) {
13763113 262 fprintf(fp, "\n");
290725f7 263 }
c42c79ea 264
13763113
SM
265 print_value_rec(fp, object, indent + 2);
266
267end:
268 return;
269}
270
271static
27c61ce8
PP
272bt_value_map_foreach_entry_const_func_status collect_map_keys(
273 const char *key, const bt_value *object, void *data)
13763113
SM
274{
275 GPtrArray *map_keys = data;
276
277 g_ptr_array_add(map_keys, (gpointer *) key);
278
27c61ce8 279 return BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_OK;
c42c79ea
PP
280}
281
13763113
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
b19ff26f 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;
fdd3a2da 295 uint64_t uint_val;
c42c79ea
PP
296 double dbl_val;
297 const char *str_val;
13763113 298 GPtrArray *map_keys = NULL;
c42c79ea 299
3eb16918 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:
601b0d3c 308 bool_val = bt_value_bool_get(value);
7213a328 309 fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(),
04609487 310 bt_common_color_fg_bright_cyan(), bool_val ? "yes" : "no",
c1081aa6 311 bt_common_color_reset());
c42c79ea 312 break;
fdd3a2da 313 case BT_VALUE_TYPE_UNSIGNED_INTEGER:
9c08c816 314 uint_val = bt_value_integer_unsigned_get(value);
fdd3a2da 315 fprintf(fp, "%s%s%" PRIu64 "%s\n", bt_common_color_bold(),
04609487 316 bt_common_color_fg_bright_red(), uint_val,
fdd3a2da
PP
317 bt_common_color_reset());
318 break;
319 case BT_VALUE_TYPE_SIGNED_INTEGER:
9c08c816 320 int_val = bt_value_integer_signed_get(value);
7213a328 321 fprintf(fp, "%s%s%" PRId64 "%s\n", bt_common_color_bold(),
04609487 322 bt_common_color_fg_bright_red(), int_val,
c1081aa6 323 bt_common_color_reset());
c42c79ea 324 break;
a373bf69 325 case BT_VALUE_TYPE_REAL:
601b0d3c 326 dbl_val = bt_value_real_get(value);
7213a328 327 fprintf(fp, "%s%s%lf%s\n", bt_common_color_bold(),
04609487 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:
601b0d3c 332 str_val = bt_value_string_get(value);
7213a328 333 fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(),
04609487 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:
50233236 338 {
f80e9ec1 339 uint64_t i, size;
393729a6 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++) {
b19ff26f 348 const bt_value *element =
05e21286 349 bt_value_array_borrow_element_by_index_const(
07208d85 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;
50233236 375 }
c42c79ea 376 case BT_VALUE_TYPE_MAP:
7213a328 377 {
13763113
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
13763113
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:
498e7994 420 bt_common_abort();
c42c79ea 421 }
13763113
SM
422
423 goto end;
60a97734 424
13763113
SM
425end:
426 if (map_keys) {
427 g_ptr_array_free(map_keys, TRUE);
428 }
c42c79ea
PP
429}
430
c1081aa6 431static
b19ff26f 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{
ec532b44
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,
ec532b44
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");
05e21286 461 print_value(stderr, bt_config_component->params, 8);
ec532b44
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);
65300d60 475 BT_OBJECT_PUT_REF_AND_RESET(cfg_component);
c42c79ea
PP
476 }
477}
478
290725f7 479static
b19ff26f 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
05e21286 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{
05e21286 522 print_plugin_paths(cfg->plugin_paths);
290725f7
PP
523}
524
c1081aa6
PP
525static
526void print_cfg_help(struct bt_config *cfg)
527{
05e21286 528 print_plugin_paths(cfg->plugin_paths);
db0f160a
PP
529}
530
531static
532void print_cfg_print_ctf_metadata(struct bt_config *cfg)
533{
05e21286 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{
05e21286 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{
05e21286 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
3f7d4d90 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:
498e7994 585 bt_common_abort();
290725f7 586 }
c42c79ea
PP
587}
588
9009cc24 589static
b19ff26f 590void print_plugin_info(const bt_plugin *plugin)
22e22462
PP
591{
592 unsigned int major, minor, patch;
593 const char *extra;
4cdfc5e8 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);
9724cce9 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(),
04609487 609 bt_common_color_fg_bright_blue(), plugin_name,
22e22462 610 bt_common_color_reset());
a157fea9
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
9724cce9 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
b5cd7d65 641enum bt_cmd_status cmd_query(struct bt_config *cfg)
63ce0e1d 642{
db95fa29 643 int ret = 0;
b5cd7d65 644 enum bt_cmd_status cmd_status;
b19ff26f
PP
645 const bt_component_class *comp_cls = NULL;
646 const bt_value *results = NULL;
c7eee084 647 const char *fail_reason = NULL;
63ce0e1d 648
d94d92ac
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) {
cee05105
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);
b5cd7d65 660 goto error;
63ce0e1d
PP
661 }
662
f4e38e70 663 ret = query(cfg, comp_cls, cfg->cmd_data.query.object->str,
05e21286 664 cfg->cmd_data.query.cfg_component->params,
da91b29a 665 &results, &fail_reason);
c7eee084 666 if (ret) {
16cbfe32
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);
b5cd7d65 679 cmd_status = BT_CMD_STATUS_OK;
c7eee084
PP
680 goto end;
681
b5cd7d65
FD
682error:
683 cmd_status = BT_CMD_STATUS_ERROR;
63ce0e1d
PP
684
685end:
c5b9b441
PP
686 bt_component_class_put_ref(comp_cls);
687 bt_value_put_ref(results);
b5cd7d65 688 return cmd_status;
63ce0e1d
PP
689}
690
d94d92ac
PP
691static
692void print_component_class_help(const char *plugin_name,
b19ff26f 693 const bt_component_class *comp_cls)
d94d92ac
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);
4cdfc5e8 701 bt_component_class_type type =
d94d92ac 702 bt_component_class_get_type(comp_cls);
ec532b44 703 gchar *comp_cls_str;
d94d92ac 704
ec532b44
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);
d94d92ac
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 }
ec532b44
SM
717
718 g_free(comp_cls_str);
d94d92ac
PP
719}
720
9009cc24 721static
b5cd7d65 722enum bt_cmd_status cmd_help(struct bt_config *cfg)
22e22462 723{
b5cd7d65 724 enum bt_cmd_status cmd_status;
b19ff26f
PP
725 const bt_plugin *plugin = NULL;
726 const bt_component_class *needed_comp_cls = NULL;
22e22462 727
97512754 728 plugin = borrow_loaded_plugin_by_name(cfg->cmd_data.help.cfg_component->plugin_name->str);
22e22462 729 if (!plugin) {
cee05105
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);
b5cd7d65 733 goto error;
22e22462
PP
734 }
735
736 print_plugin_info(plugin);
d94d92ac
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(),
d94d92ac
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
d94d92ac
PP
750 if (strlen(cfg->cmd_data.help.cfg_component->comp_cls_name->str) == 0) {
751 /* Plugin help only */
b5cd7d65 752 cmd_status = BT_CMD_STATUS_OK;
d94d92ac
PP
753 goto end;
754 }
22e22462 755
d94d92ac
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) {
cee05105
PP
761 BT_CLI_LOGE_APPEND_CAUSE(
762 "Cannot find component class: plugin-name=\"%s\", "
d94d92ac
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);
b5cd7d65 767 goto error;
d94d92ac 768 }
22e22462 769
d94d92ac
PP
770 printf("\n");
771 print_component_class_help(
772 cfg->cmd_data.help.cfg_component->plugin_name->str,
773 needed_comp_cls);
b5cd7d65
FD
774 cmd_status = BT_CMD_STATUS_OK;
775 goto end;
776
777error:
778 cmd_status = BT_CMD_STATUS_ERROR;
d94d92ac
PP
779
780end:
c5b9b441 781 bt_component_class_put_ref(needed_comp_cls);
b5cd7d65 782 return cmd_status;
d94d92ac
PP
783}
784
b19ff26f 785typedef void *(* plugin_borrow_comp_cls_by_index_func_t)(const bt_plugin *,
d94d92ac 786 uint64_t);
b19ff26f 787typedef const bt_component_class *(* spec_comp_cls_borrow_comp_cls_func_t)(
d94d92ac
PP
788 void *);
789
7c7301d5 790static
b19ff26f 791void cmd_list_plugins_print_component_classes(const bt_plugin *plugin,
d94d92ac
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;
ec532b44 797 gchar *comp_cls_str = NULL;
22e22462 798
d94d92ac 799 if (count == 0) {
8a7c9d06 800 printf(" %s%s component classes%s: (none)\n",
d94d92ac 801 bt_common_color_bold(),
8a7c9d06 802 cc_type_name,
d94d92ac
PP
803 bt_common_color_reset());
804 goto end;
805 } else {
8a7c9d06 806 printf(" %s%s component classes%s:\n",
d94d92ac 807 bt_common_color_bold(),
8a7c9d06 808 cc_type_name,
d94d92ac 809 bt_common_color_reset());
22e22462
PP
810 }
811
d94d92ac 812 for (i = 0; i < count; i++) {
b19ff26f 813 const bt_component_class *comp_class =
d94d92ac
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 =
d94d92ac 817 bt_component_class_get_name(comp_class);
22e22462 818 const char *comp_class_description =
d94d92ac 819 bt_component_class_get_description(comp_class);
4cdfc5e8 820 bt_component_class_type type =
d94d92ac 821 bt_component_class_get_type(comp_class);
22e22462 822
ec532b44
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
d94d92ac
PP
829 if (comp_class_description) {
830 printf(": %s", comp_class_description);
22e22462
PP
831 }
832
d94d92ac 833 printf("\n");
22e22462
PP
834 }
835
836end:
ec532b44 837 g_free(comp_cls_str);
22e22462
PP
838}
839
9009cc24 840static
b5cd7d65 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");
05e21286 846 print_value(stdout, cfg->plugin_paths, 2);
22e22462 847 printf("\n");
743138a3 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++) {
97512754 855 const bt_plugin *plugin = borrow_loaded_plugin_by_index(i);
290725f7 856
d94d92ac
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++) {
97512754 872 const bt_plugin *plugin = borrow_loaded_plugin_by_index(i);
290725f7 873
22e22462
PP
874 printf("\n");
875 print_plugin_info(plugin);
d94d92ac
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)
c3dd43ec 879 bt_plugin_borrow_source_component_class_by_index_const,
d94d92ac 880 (spec_comp_cls_borrow_comp_cls_func_t)
707b7d35 881 bt_component_class_source_as_component_class);
d94d92ac
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)
c3dd43ec 885 bt_plugin_borrow_filter_component_class_by_index_const,
d94d92ac 886 (spec_comp_cls_borrow_comp_cls_func_t)
707b7d35 887 bt_component_class_filter_as_component_class);
d94d92ac
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)
c3dd43ec 891 bt_plugin_borrow_sink_component_class_by_index_const,
d94d92ac 892 (spec_comp_cls_borrow_comp_cls_func_t)
707b7d35 893 bt_component_class_sink_as_component_class);
290725f7
PP
894 }
895
896end:
b5cd7d65 897 return BT_CMD_STATUS_OK;
290725f7
PP
898}
899
9009cc24 900static
b5cd7d65 901enum bt_cmd_status cmd_print_lttng_live_sessions(struct bt_config *cfg)
db0f160a 902{
96e8c7e1 903 int ret = 0;
b5cd7d65 904 enum bt_cmd_status cmd_status;
b19ff26f
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";
4cdfc5e8 912 static const bt_component_class_type comp_cls_type =
96e8c7e1 913 BT_COMPONENT_CLASS_TYPE_SOURCE;
f80e9ec1 914 uint64_t array_size, i;
c7eee084 915 const char *fail_reason = NULL;
c327e427 916 FILE *out_stream = stdout;
96e8c7e1 917
f6ccaed9 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) {
cee05105
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
05e21286 930 params = bt_value_map_create();
96e8c7e1
MD
931 if (!params) {
932 goto error;
933 }
934
05e21286 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
f4e38e70 941 ret = query(cfg, comp_cls, "sessions", params,
05e21286 942 &results, &fail_reason);
c7eee084 943 if (ret) {
16cbfe32
FD
944 BT_CLI_LOGE_APPEND_CAUSE("Failed to query `sessions` object: %s",
945 fail_reason);
946 goto error;
96e8c7e1
MD
947 }
948
f6ccaed9
PP
949 BT_ASSERT(results);
950
96e8c7e1 951 if (!bt_value_is_array(results)) {
cee05105
PP
952 BT_CLI_LOGE_APPEND_CAUSE(
953 "Expecting an array for LTTng live `sessions` query.");
96e8c7e1
MD
954 goto error;
955 }
956
c327e427
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) {
c327e427
PP
962 BT_LOGE_ERRNO("Cannot open file for writing",
963 ": path=\"%s\"",
964 cfg->cmd_data.print_lttng_live_sessions.output_path->str);
cee05105
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);
b5cd7d65 969 goto error;
c327e427
PP
970 }
971 }
972
393729a6 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
05e21286 978 map = bt_value_array_borrow_element_by_index_const(results, i);
96e8c7e1 979 if (!bt_value_is_map(map)) {
cee05105 980 BT_CLI_LOGE_APPEND_CAUSE("Unexpected entry type.");
96e8c7e1
MD
981 goto error;
982 }
983
05e21286 984 v = bt_value_map_borrow_entry_value_const(map, "url");
96e8c7e1 985 if (!v) {
cee05105 986 BT_CLI_LOGE_APPEND_CAUSE("Missing `url` entry.");
96e8c7e1
MD
987 goto error;
988 }
601b0d3c 989 url_text = bt_value_string_get(v);
c327e427 990 fprintf(out_stream, "%s", url_text);
05e21286 991 v = bt_value_map_borrow_entry_value_const(map, "timer-us");
96e8c7e1 992 if (!v) {
cee05105 993 BT_CLI_LOGE_APPEND_CAUSE("Missing `timer-us` entry.");
96e8c7e1
MD
994 goto error;
995 }
a91cb83e 996 timer_us = bt_value_integer_unsigned_get(v);
c327e427 997 fprintf(out_stream, " (timer = %" PRIu64 ", ", timer_us);
05e21286 998 v = bt_value_map_borrow_entry_value_const(map, "stream-count");
96e8c7e1 999 if (!v) {
cee05105
PP
1000 BT_CLI_LOGE_APPEND_CAUSE(
1001 "Missing `stream-count` entry.");
96e8c7e1
MD
1002 goto error;
1003 }
a91cb83e 1004 streams = bt_value_integer_unsigned_get(v);
c327e427 1005 fprintf(out_stream, "%" PRIu64 " stream(s), ", streams);
05e21286 1006 v = bt_value_map_borrow_entry_value_const(map, "client-count");
96e8c7e1 1007 if (!v) {
cee05105
PP
1008 BT_CLI_LOGE_APPEND_CAUSE(
1009 "Missing `client-count` entry.");
96e8c7e1
MD
1010 goto error;
1011 }
a91cb83e 1012 clients = bt_value_integer_unsigned_get(v);
c327e427 1013 fprintf(out_stream, "%" PRIu64 " client(s) connected)\n", clients);
96e8c7e1 1014 }
c7eee084 1015
b5cd7d65 1016 cmd_status = BT_CMD_STATUS_OK;
c7eee084
PP
1017 goto end;
1018
c7eee084 1019error:
b5cd7d65 1020 cmd_status = BT_CMD_STATUS_ERROR;
c7eee084 1021
96e8c7e1 1022end:
c5b9b441
PP
1023 bt_value_put_ref(results);
1024 bt_value_put_ref(params);
1025 bt_component_class_put_ref(comp_cls);
c327e427
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
b5cd7d65 1037 return cmd_status;
db0f160a
PP
1038}
1039
9009cc24 1040static
b5cd7d65 1041enum bt_cmd_status cmd_print_ctf_metadata(struct bt_config *cfg)
05a67631
PP
1042{
1043 int ret = 0;
b5cd7d65 1044 enum bt_cmd_status cmd_status;
b19ff26f
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";
4cdfc5e8 1052 static const bt_component_class_type comp_cls_type =
db0f160a 1053 BT_COMPONENT_CLASS_TYPE_SOURCE;
c7eee084 1054 const char *fail_reason = NULL;
c327e427 1055 FILE *out_stream = stdout;
db0f160a 1056
f6ccaed9 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) {
cee05105
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);
b5cd7d65 1066 goto error;
05a67631
PP
1067 }
1068
05e21286 1069 params = bt_value_map_create();
05a67631 1070 if (!params) {
b5cd7d65 1071 goto error;
05a67631
PP
1072 }
1073
05e21286 1074 ret = bt_value_map_insert_string_entry(params, "path",
db0f160a 1075 cfg->cmd_data.print_ctf_metadata.path->str);
05a67631 1076 if (ret) {
b5cd7d65 1077 goto error;
05a67631
PP
1078 }
1079
f4e38e70 1080 ret = query(cfg, comp_cls, "metadata-info",
05e21286 1081 params, &results, &fail_reason);
c7eee084 1082 if (ret) {
16cbfe32
FD
1083 BT_CLI_LOGE_APPEND_CAUSE(
1084 "Failed to query `metadata-info` object: %s", fail_reason);
1085 goto error;
05a67631
PP
1086 }
1087
05e21286 1088 metadata_text_value = bt_value_map_borrow_entry_value_const(results,
cee05105 1089 "text");
05a67631 1090 if (!metadata_text_value) {
cee05105
PP
1091 BT_CLI_LOGE_APPEND_CAUSE(
1092 "Cannot find `text` string value in the resulting metadata info object.");
b5cd7d65 1093 goto error;
05a67631
PP
1094 }
1095
601b0d3c 1096 metadata_text = bt_value_string_get(metadata_text_value);
c327e427
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) {
c327e427
PP
1103 BT_LOGE_ERRNO("Cannot open file for writing",
1104 ": path=\"%s\"",
1105 cfg->cmd_data.print_ctf_metadata.output_path->str);
cee05105
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);
b5cd7d65 1110 goto error;
c327e427
PP
1111 }
1112 }
1113
1114 ret = fprintf(out_stream, "%s\n", metadata_text);
1115 if (ret < 0) {
cee05105
PP
1116 BT_CLI_LOGE_APPEND_CAUSE(
1117 "Cannot write whole metadata text to output stream: "
c327e427 1118 "ret=%d", ret);
b5cd7d65 1119 goto error;
c327e427
PP
1120 }
1121
b5cd7d65 1122 cmd_status = BT_CMD_STATUS_OK;
c7eee084
PP
1123 goto end;
1124
b5cd7d65
FD
1125error:
1126 cmd_status = BT_CMD_STATUS_ERROR;
05a67631
PP
1127
1128end:
c5b9b441
PP
1129 bt_value_put_ref(results);
1130 bt_value_put_ref(params);
1131 bt_component_class_put_ref(comp_cls);
c327e427
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
b5cd7d65 1143 return cmd_status;
05a67631
PP
1144}
1145
75a2cb9b
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
f6ccaed9
PP
1161 BT_ASSERT(id->instance_name);
1162 BT_ASSERT(id->port_name);
75a2cb9b
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
2242b43d
PP
1173 return strcmp(id1->instance_name, id2->instance_name) == 0 &&
1174 strcmp(id1->port_name, id2->port_name) == 0;
75a2cb9b
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 */
d94d92ac
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 */
b19ff26f 1204 bt_graph *graph;
9009cc24
PP
1205
1206 /* Weak */
1207 struct bt_config *cfg;
1208
1209 bool connect_ports;
75a2cb9b
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
75a2cb9b
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(
d94d92ac 1266 struct cmd_run_ctx *ctx,
b19ff26f
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{
d94d92ac 1271 typedef uint64_t (*input_port_count_func_t)(void *);
b19ff26f 1272 typedef const bt_port_input *(*borrow_input_port_by_index_func_t)(
0d72b8c3 1273 const void *, uint64_t);
b19ff26f 1274 const bt_port *upstream_port =
0d72b8c3 1275 bt_port_output_as_port_const(out_upstream_port);
d94d92ac 1276
290725f7 1277 int ret = 0;
9009cc24 1278 GQuark downstreamp_comp_name_quark;
d94d92ac
PP
1279 void *downstream_comp;
1280 uint64_t downstream_port_count;
9009cc24 1281 uint64_t i;
d94d92ac
PP
1282 input_port_count_func_t port_count_fn;
1283 borrow_input_port_by_index_func_t port_by_index_fn;
d24d5663
PP
1284 bt_graph_connect_ports_status connect_ports_status =
1285 BT_GRAPH_CONNECT_PORTS_STATUS_OK;
75a2cb9b 1286 bool insert_trimmer = false;
b19ff26f 1287 bt_value *trimmer_params = NULL;
75a2cb9b
JG
1288 char *intersection_begin = NULL;
1289 char *intersection_end = NULL;
b19ff26f
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;
75a2cb9b
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) {
d24d5663 1311 bt_value_map_insert_entry_status insert_status;
75a2cb9b
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) {
cee05105
PP
1318 BT_CLI_LOGE_APPEND_CAUSE(
1319 "Cannot create trimmer argument timestamp string.");
75a2cb9b
JG
1320 goto error;
1321 }
1322
1323 insert_trimmer = true;
05e21286 1324 trimmer_params = bt_value_map_create();
75a2cb9b
JG
1325 if (!trimmer_params) {
1326 goto error;
1327 }
1328
d24d5663 1329 insert_status = bt_value_map_insert_string_entry(
07208d85 1330 trimmer_params, "begin", intersection_begin);
d24d5663 1331 if (insert_status < 0) {
75a2cb9b
JG
1332 goto error;
1333 }
d24d5663 1334 insert_status = bt_value_map_insert_string_entry(
07208d85 1335 trimmer_params,
75a2cb9b 1336 "end", intersection_end);
d24d5663 1337 if (insert_status < 0) {
75a2cb9b
JG
1338 goto error;
1339 }
1340 }
1341
d94d92ac 1342 trimmer_class = find_filter_component_class("utils", "trimmer");
75a2cb9b
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);
f6ccaed9 1356 BT_ASSERT(downstreamp_comp_name_quark > 0);
d94d92ac 1357 downstream_comp = g_hash_table_lookup(ctx->flt_components,
71c7c95f 1358 GUINT_TO_POINTER(downstreamp_comp_name_quark));
d94d92ac
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)
0d72b8c3 1362 bt_component_filter_borrow_input_port_by_index_const;
d94d92ac
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)
0d72b8c3 1370 bt_component_sink_borrow_input_port_by_index_const;
d94d92ac
PP
1371 }
1372
9009cc24 1373 if (!downstream_comp) {
cee05105
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++) {
b19ff26f 1384 const bt_port_input *in_downstream_port =
9009cc24 1385 port_by_index_fn(downstream_comp, i);
b19ff26f 1386 const bt_port *downstream_port =
0d72b8c3 1387 bt_port_input_as_port_const(in_downstream_port);
75a2cb9b 1388 const char *upstream_port_name;
9009cc24
PP
1389 const char *downstream_port_name;
1390
f6ccaed9 1391 BT_ASSERT(downstream_port);
9009cc24 1392
75a2cb9b 1393 /* Skip port if it's already connected. */
9009cc24 1394 if (bt_port_is_connected(downstream_port)) {
3f7d4d90 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);
f6ccaed9 1403 BT_ASSERT(downstream_port_name);
75a2cb9b 1404 upstream_port_name = bt_port_get_name(upstream_port);
f6ccaed9 1405 BT_ASSERT(upstream_port_name);
9009cc24 1406
75a2cb9b 1407 if (!bt_common_star_glob_match(
1974687e
MJ
1408 cfg_conn->downstream_port_glob->str, SIZE_MAX,
1409 downstream_port_name, SIZE_MAX)) {
75a2cb9b
JG
1410 continue;
1411 }
1412
1413 if (insert_trimmer) {
1414 /*
d94d92ac
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.
75a2cb9b 1421 *
d94d92ac
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.
75a2cb9b
JG
1428 */
1429 char *trimmer_name = NULL;
d24d5663 1430 bt_graph_add_component_status add_comp_status;
75a2cb9b 1431
d94d92ac
PP
1432 ret = asprintf(&trimmer_name,
1433 "stream-intersection-trimmer-%s",
75a2cb9b
JG
1434 upstream_port_name);
1435 if (ret < 0) {
1436 goto error;
1437 }
1438 ret = 0;
1439
1440 ctx->connect_ports = false;
d24d5663 1441 add_comp_status = bt_graph_add_filter_component(
d94d92ac 1442 ctx->graph, trimmer_class, trimmer_name,
e874da19
PP
1443 trimmer_params, ctx->cfg->log_level,
1444 &trimmer);
a91462c9 1445 bt_component_filter_get_ref(trimmer);
75a2cb9b 1446 free(trimmer_name);
d24d5663
PP
1447 if (add_comp_status !=
1448 BT_GRAPH_ADD_COMPONENT_STATUS_OK) {
75a2cb9b
JG
1449 goto error;
1450 }
f6ccaed9 1451 BT_ASSERT(trimmer);
75a2cb9b
JG
1452
1453 trimmer_input =
0d72b8c3 1454 bt_component_filter_borrow_input_port_by_index_const(
75a2cb9b
JG
1455 trimmer, 0);
1456 if (!trimmer_input) {
1457 goto error;
1458 }
1459 trimmer_output =
0d72b8c3 1460 bt_component_filter_borrow_output_port_by_index_const(
75a2cb9b
JG
1461 trimmer, 0);
1462 if (!trimmer_output) {
9009cc24
PP
1463 goto error;
1464 }
1465
75a2cb9b
JG
1466 /*
1467 * Replace the current downstream port by the trimmer's
1468 * upstream port.
1469 */
d94d92ac
PP
1470 in_downstream_port = trimmer_input;
1471 downstream_port =
0d72b8c3 1472 bt_port_input_as_port_const(in_downstream_port);
75a2cb9b
JG
1473 downstream_port_name = bt_port_get_name(
1474 downstream_port);
d94d92ac 1475 BT_ASSERT(downstream_port_name);
75a2cb9b
JG
1476 }
1477
1478 /* We have a winner! */
d24d5663 1479 connect_ports_status = bt_graph_connect_ports(ctx->graph,
d94d92ac
PP
1480 out_upstream_port, in_downstream_port, NULL);
1481 downstream_port = NULL;
d24d5663
PP
1482 switch (connect_ports_status) {
1483 case BT_GRAPH_CONNECT_PORTS_STATUS_OK:
75a2cb9b 1484 break;
75a2cb9b 1485 default:
cee05105
PP
1486 BT_CLI_LOGE_APPEND_CAUSE(
1487 "Cannot create connection: graph refuses to connect ports: "
75a2cb9b
JG
1488 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1489 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1490 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1491 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1492 "conn-arg=\"%s\"",
1493 upstream_comp, bt_component_get_name(upstream_comp),
1494 upstream_port, bt_port_get_name(upstream_port),
1495 downstream_comp, cfg_conn->downstream_comp_name->str,
1496 downstream_port, downstream_port_name,
1497 cfg_conn->arg->str);
75a2cb9b 1498 goto error;
9009cc24
PP
1499 }
1500
75a2cb9b
JG
1501 BT_LOGI("Connected component ports: "
1502 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1503 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1504 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1505 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1506 "conn-arg=\"%s\"",
1507 upstream_comp, bt_component_get_name(upstream_comp),
1508 upstream_port, bt_port_get_name(upstream_port),
1509 downstream_comp, cfg_conn->downstream_comp_name->str,
1510 downstream_port, downstream_port_name,
1511 cfg_conn->arg->str);
1512
1513 if (insert_trimmer) {
1514 /*
1515 * The first connection, from the source to the trimmer,
1516 * has been done. We now connect the trimmer to the
1517 * original downstream port.
1518 */
1519 ret = cmd_run_ctx_connect_upstream_port_to_downstream_component(
d94d92ac 1520 ctx,
0d72b8c3 1521 bt_component_filter_as_component_const(trimmer),
d94d92ac 1522 trimmer_output, cfg_conn);
75a2cb9b
JG
1523 if (ret) {
1524 goto error;
1525 }
1526 ctx->connect_ports = true;
1527 }
9009cc24 1528
53bc54cd
PP
1529 /*
1530 * We found a matching downstream port: the search is
1531 * over.
1532 */
1533 goto end;
05a67631
PP
1534 }
1535
53bc54cd 1536 /* No downstream port found */
cee05105
PP
1537 BT_CLI_LOGE_APPEND_CAUSE(
1538 "Cannot create connection: cannot find a matching downstream port for upstream port: "
53bc54cd
PP
1539 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1540 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1541 upstream_port, bt_port_get_name(upstream_port),
1542 cfg_conn->downstream_comp_name->str,
1543 cfg_conn->arg->str);
9009cc24
PP
1544
1545error:
1546 ret = -1;
1547
1548end:
75a2cb9b
JG
1549 free(intersection_begin);
1550 free(intersection_end);
c5b9b441
PP
1551 BT_VALUE_PUT_REF_AND_RESET(trimmer_params);
1552 BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class);
1553 BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer);
9009cc24
PP
1554 return ret;
1555}
1556
1557static
1558int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx *ctx,
b19ff26f 1559 const bt_port_output *upstream_port)
9009cc24
PP
1560{
1561 int ret = 0;
1562 const char *upstream_port_name;
1563 const char *upstream_comp_name;
b19ff26f 1564 const bt_component *upstream_comp = NULL;
9009cc24
PP
1565 size_t i;
1566
f6ccaed9
PP
1567 BT_ASSERT(ctx);
1568 BT_ASSERT(upstream_port);
d94d92ac 1569 upstream_port_name = bt_port_get_name(
0d72b8c3 1570 bt_port_output_as_port_const(upstream_port));
f6ccaed9 1571 BT_ASSERT(upstream_port_name);
0d72b8c3
PP
1572 upstream_comp = bt_port_borrow_component_const(
1573 bt_port_output_as_port_const(upstream_port));
cee05105 1574 BT_ASSERT(upstream_comp);
9009cc24 1575 upstream_comp_name = bt_component_get_name(upstream_comp);
f6ccaed9 1576 BT_ASSERT(upstream_comp_name);
7213a328
PP
1577 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1578 "port-addr=%p, port-name=\"%s\"",
1579 upstream_comp, upstream_comp_name,
1580 upstream_port, upstream_port_name);
9009cc24
PP
1581
1582 for (i = 0; i < ctx->cfg->cmd_data.run.connections->len; i++) {
1583 struct bt_config_connection *cfg_conn =
1584 g_ptr_array_index(
1585 ctx->cfg->cmd_data.run.connections, i);
1586
1587 if (strcmp(cfg_conn->upstream_comp_name->str,
75a2cb9b
JG
1588 upstream_comp_name)) {
1589 continue;
1590 }
1591
1592 if (!bt_common_star_glob_match(
1593 cfg_conn->upstream_port_glob->str,
1974687e 1594 SIZE_MAX, upstream_port_name, SIZE_MAX)) {
75a2cb9b
JG
1595 continue;
1596 }
1597
1598 ret = cmd_run_ctx_connect_upstream_port_to_downstream_component(
1599 ctx, upstream_comp, upstream_port, cfg_conn);
1600 if (ret) {
cee05105
PP
1601 BT_CLI_LOGE_APPEND_CAUSE(
1602 "Cannot connect upstream port: "
75a2cb9b
JG
1603 "port-addr=%p, port-name=\"%s\"",
1604 upstream_port,
1605 upstream_port_name);
75a2cb9b 1606 goto error;
9009cc24 1607 }
75a2cb9b 1608 goto end;
9009cc24
PP
1609 }
1610
cee05105
PP
1611 BT_CLI_LOGE_APPEND_CAUSE(
1612 "Cannot connect upstream port: port does not match any connection argument: "
7213a328
PP
1613 "port-addr=%p, port-name=\"%s\"", upstream_port,
1614 upstream_port_name);
9009cc24
PP
1615
1616error:
1617 ret = -1;
1618
1619end:
9009cc24
PP
1620 return ret;
1621}
1622
1623static
d24d5663 1624bt_graph_listener_func_status
8cc56726 1625graph_output_port_added_listener(struct cmd_run_ctx *ctx,
b19ff26f 1626 const bt_port_output *out_port)
9009cc24 1627{
b19ff26f
PP
1628 const bt_component *comp;
1629 const bt_port *port = bt_port_output_as_port_const(out_port);
d24d5663
PP
1630 bt_graph_listener_func_status ret =
1631 BT_GRAPH_LISTENER_FUNC_STATUS_OK;
9009cc24 1632
0d72b8c3 1633 comp = bt_port_borrow_component_const(port);
e12720c0
PP
1634 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1635 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1636 comp, comp ? bt_component_get_name(comp) : "",
7213a328 1637 port, bt_port_get_name(port));
36712f1d
PP
1638
1639 if (!ctx->connect_ports) {
1640 goto end;
1641 }
1642
cee05105 1643 BT_ASSERT(comp);
7c7c0433 1644
e12720c0
PP
1645 if (bt_port_is_connected(port)) {
1646 BT_LOGW_STR("Port is already connected.");
7c7c0433
JG
1647 goto end;
1648 }
1649
d94d92ac 1650 if (cmd_run_ctx_connect_upstream_port(ctx, out_port)) {
cee05105 1651 BT_CLI_LOGE_APPEND_CAUSE("Cannot connect upstream port.");
d24d5663 1652 ret = BT_GRAPH_LISTENER_FUNC_STATUS_ERROR;
8cc56726 1653 goto end;
9009cc24
PP
1654 }
1655
1656end:
8cc56726 1657 return ret;
9009cc24
PP
1658}
1659
1660static
d24d5663 1661bt_graph_listener_func_status graph_source_output_port_added_listener(
b19ff26f
PP
1662 const bt_component_source *component,
1663 const bt_port_output *port, void *data)
9009cc24 1664{
8cc56726 1665 return graph_output_port_added_listener(data, port);
9009cc24
PP
1666}
1667
1668static
d24d5663 1669bt_graph_listener_func_status graph_filter_output_port_added_listener(
b19ff26f
PP
1670 const bt_component_filter *component,
1671 const bt_port_output *port, void *data)
9009cc24 1672{
8cc56726 1673 return graph_output_port_added_listener(data, port);
9009cc24
PP
1674}
1675
1676static
1677void cmd_run_ctx_destroy(struct cmd_run_ctx *ctx)
1678{
1679 if (!ctx) {
1680 return;
1681 }
1682
d94d92ac
PP
1683 if (ctx->src_components) {
1684 g_hash_table_destroy(ctx->src_components);
1685 ctx->src_components = NULL;
1686 }
1687
1688 if (ctx->flt_components) {
1689 g_hash_table_destroy(ctx->flt_components);
1690 ctx->flt_components = NULL;
1691 }
1692
1693 if (ctx->sink_components) {
1694 g_hash_table_destroy(ctx->sink_components);
1695 ctx->sink_components = NULL;
9009cc24
PP
1696 }
1697
75a2cb9b
JG
1698 if (ctx->intersections) {
1699 g_hash_table_destroy(ctx->intersections);
a5302548 1700 ctx->intersections = NULL;
75a2cb9b
JG
1701 }
1702
c5b9b441 1703 BT_GRAPH_PUT_REF_AND_RESET(ctx->graph);
9009cc24
PP
1704 ctx->cfg = NULL;
1705}
1706
5fea82b8
PP
1707static
1708int add_descriptor_to_component_descriptor_set(
1709 bt_component_descriptor_set *comp_descr_set,
1710 const char *plugin_name, const char *comp_cls_name,
1711 bt_component_class_type comp_cls_type,
1712 const bt_value *params)
1713{
1714 const bt_component_class *comp_cls;
1715 int status = 0;
1716
1717 comp_cls = find_component_class(plugin_name, comp_cls_name,
1718 comp_cls_type);
1719 if (!comp_cls) {
1720 BT_CLI_LOGE_APPEND_CAUSE(
1721 "Cannot find component class: plugin-name=\"%s\", "
1722 "comp-cls-name=\"%s\", comp-cls-type=%d",
1723 plugin_name, comp_cls_name, comp_cls_type);
1724 status = -1;
1725 goto end;
1726 }
1727
1728 status = bt_component_descriptor_set_add_descriptor(
1729 comp_descr_set, comp_cls, params);
1730 if (status != BT_COMPONENT_DESCRIPTOR_SET_ADD_DESCRIPTOR_STATUS_OK) {
1731 BT_CLI_LOGE_APPEND_CAUSE(
1732 "Cannot append descriptor to component descriptor set: "
1733 "status=%s", bt_common_func_status_string(status));
1734 goto end;
1735 }
1736
1737end:
1738 bt_component_class_put_ref(comp_cls);
1739 return status;
1740}
1741
1742static
1743int append_descriptors_from_bt_config_component_array(
1744 bt_component_descriptor_set *comp_descr_set,
1745 GPtrArray *component_configs)
1746{
1747 int ret = 0;
1748 uint64_t i;
1749
1750 for (i = 0; i < component_configs->len; i++) {
1751 struct bt_config_component *cfg_comp =
1752 component_configs->pdata[i];
1753
1754 ret = add_descriptor_to_component_descriptor_set(
1755 comp_descr_set,
1756 cfg_comp->plugin_name->str,
1757 cfg_comp->comp_cls_name->str,
1758 cfg_comp->type, cfg_comp->params);
1759 if (ret) {
1760 goto end;
1761 }
1762 }
1763
1764end:
1765 return ret;
1766}
1767
1768static
1769bt_get_greatest_operative_mip_version_status get_greatest_operative_mip_version(
1770 struct bt_config *cfg, uint64_t *mip_version)
1771{
1772 bt_get_greatest_operative_mip_version_status status =
1773 BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_OK;
1774 bt_component_descriptor_set *comp_descr_set = NULL;
1775 int ret;
1776
1777 BT_ASSERT(cfg);
1778 BT_ASSERT(mip_version);
1779 comp_descr_set = bt_component_descriptor_set_create();
1780 if (!comp_descr_set) {
1781 BT_CLI_LOGE_APPEND_CAUSE(
1782 "Failed to create a component descriptor set object.");
1783 status = BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_MEMORY_ERROR;
1784 goto end;
1785 }
1786
1787 ret = append_descriptors_from_bt_config_component_array(
1788 comp_descr_set, cfg->cmd_data.run.sources);
1789 if (ret) {
1790 status = BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR;
1791 goto end;
1792 }
1793
1794 ret = append_descriptors_from_bt_config_component_array(
1795 comp_descr_set, cfg->cmd_data.run.filters);
1796 if (ret) {
1797 status = BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR;
1798 goto end;
1799 }
1800
1801 ret = append_descriptors_from_bt_config_component_array(
1802 comp_descr_set, cfg->cmd_data.run.sinks);
1803 if (ret) {
1804 status = BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR;
1805 goto end;
1806 }
1807
1808 if (cfg->cmd_data.run.stream_intersection_mode) {
1809 /*
1810 * Stream intersection mode adds `flt.utils.trimmer`
1811 * components; we need to include this type of component
1812 * in the component descriptor set to get the real
1813 * greatest operative MIP version.
1814 */
1815 ret = add_descriptor_to_component_descriptor_set(
1816 comp_descr_set, "utils", "trimmer",
1817 BT_COMPONENT_CLASS_TYPE_FILTER, NULL);
1818 if (ret) {
1819 status = BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR;
1820 goto end;
1821 }
1822 }
1823
1824 status = bt_get_greatest_operative_mip_version(comp_descr_set,
1825 bt_cli_log_level, mip_version);
1826
1827end:
1828 bt_component_descriptor_set_put_ref(comp_descr_set);
1829 return status;
1830}
1831
9009cc24
PP
1832static
1833int cmd_run_ctx_init(struct cmd_run_ctx *ctx, struct bt_config *cfg)
1834{
1835 int ret = 0;
9275bef4 1836 bt_graph_add_listener_status add_listener_status;
5fea82b8
PP
1837 bt_get_greatest_operative_mip_version_status mip_version_status;
1838 uint64_t mip_version = UINT64_C(-1);
9009cc24
PP
1839
1840 ctx->cfg = cfg;
1841 ctx->connect_ports = false;
d94d92ac 1842 ctx->src_components = g_hash_table_new_full(g_direct_hash,
398454ed 1843 g_direct_equal, NULL, (GDestroyNotify) bt_object_put_ref);
d94d92ac
PP
1844 if (!ctx->src_components) {
1845 goto error;
1846 }
1847
1848 ctx->flt_components = g_hash_table_new_full(g_direct_hash,
398454ed 1849 g_direct_equal, NULL, (GDestroyNotify) bt_object_put_ref);
d94d92ac
PP
1850 if (!ctx->flt_components) {
1851 goto error;
1852 }
1853
1854 ctx->sink_components = g_hash_table_new_full(g_direct_hash,
398454ed 1855 g_direct_equal, NULL, (GDestroyNotify) bt_object_put_ref);
d94d92ac 1856 if (!ctx->sink_components) {
9009cc24
PP
1857 goto error;
1858 }
1859
75a2cb9b
JG
1860 if (cfg->cmd_data.run.stream_intersection_mode) {
1861 ctx->stream_intersection_mode = true;
1862 ctx->intersections = g_hash_table_new_full(port_id_hash,
1863 port_id_equal, port_id_destroy, trace_range_destroy);
1864 if (!ctx->intersections) {
1865 goto error;
1866 }
1867 }
1868
5fea82b8
PP
1869 /*
1870 * Get the greatest operative MIP version to use to configure
1871 * the graph to create.
1872 */
1873 mip_version_status = get_greatest_operative_mip_version(
1874 cfg, &mip_version);
1875 if (mip_version_status == BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_NO_MATCH) {
1876 BT_CLI_LOGE_APPEND_CAUSE(
1877 "Failed to find an operative message interchange "
1878 "protocol version to use to create the `run` command's "
1879 "graph (components are not interoperable).");
1880 goto error;
1881 } else if (mip_version_status < 0) {
1882 BT_CLI_LOGE_APPEND_CAUSE(
1883 "Cannot find an operative message interchange "
1884 "protocol version to use to create the `run` command's "
1885 "graph: status=%s",
1886 bt_common_func_status_string(mip_version_status));
1887 goto error;
1888 }
1889
1890 BT_ASSERT(mip_version_status == BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_OK);
1891 BT_LOGI("Found operative message interchange protocol version to "
1892 "configure the `run` command's graph: mip-version=%" PRIu64,
1893 mip_version);
1894 ctx->graph = bt_graph_create(mip_version);
9009cc24
PP
1895 if (!ctx->graph) {
1896 goto error;
1897 }
1898
9b4f9b42 1899 bt_graph_add_interrupter(ctx->graph, the_interrupter);
9275bef4 1900 add_listener_status = bt_graph_add_source_component_output_port_added_listener(
f3d6b4c2 1901 ctx->graph, graph_source_output_port_added_listener, ctx,
d94d92ac 1902 NULL);
9275bef4 1903 if (add_listener_status != BT_GRAPH_ADD_LISTENER_STATUS_OK) {
cee05105
PP
1904 BT_CLI_LOGE_APPEND_CAUSE(
1905 "Cannot add \"port added\" listener to graph.");
9009cc24
PP
1906 goto error;
1907 }
1908
9275bef4 1909 add_listener_status = bt_graph_add_filter_component_output_port_added_listener(
f3d6b4c2 1910 ctx->graph, graph_filter_output_port_added_listener, ctx,
d94d92ac 1911 NULL);
9275bef4 1912 if (add_listener_status != BT_GRAPH_ADD_LISTENER_STATUS_OK) {
cee05105
PP
1913 BT_CLI_LOGE_APPEND_CAUSE(
1914 "Cannot add \"port added\" listener to graph.");
9009cc24
PP
1915 goto error;
1916 }
1917
1918 goto end;
1919
1920error:
1921 cmd_run_ctx_destroy(ctx);
1922 ret = -1;
1923
1924end:
1925 return ret;
1926}
1927
30947af0
SM
1928/*
1929 * Compute the intersection of all streams in the array `streams`, write it
1930 * in `range`.
1931 */
1932
1933static
1934int compute_stream_intersection(const bt_value *streams,
1935 struct trace_range *range)
1936{
f80e9ec1 1937 uint64_t i, stream_count;
30947af0
SM
1938 int ret;
1939
1940 BT_ASSERT(bt_value_get_type(streams) == BT_VALUE_TYPE_ARRAY);
1941
1942 stream_count = bt_value_array_get_length(streams);
1943
1944 BT_ASSERT(stream_count > 0);
1945
1946 range->intersection_range_begin_ns = 0;
1947 range->intersection_range_end_ns = UINT64_MAX;
1948
1949 for (i = 0; i < stream_count; i++) {
1950 int64_t begin_ns, end_ns;
1951 uint64_t begin_ns_u, end_ns_u;
1952 const bt_value *stream_value;
1953 const bt_value *range_ns_value;
1954 const bt_value *begin_value;
1955 const bt_value *end_value;
1956
1957 stream_value = bt_value_array_borrow_element_by_index_const(streams, i);
1958 if (bt_value_get_type(stream_value) != BT_VALUE_TYPE_MAP) {
5f2a1585 1959 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
30947af0
SM
1960 "expected streams array element to be a map, got %s.",
1961 bt_common_value_type_string(bt_value_get_type(stream_value)));
1962 goto error;
1963 }
1964
1965 range_ns_value = bt_value_map_borrow_entry_value_const(
1966 stream_value, "range-ns");
1967 if (!range_ns_value) {
5f2a1585 1968 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
30947af0
SM
1969 "missing expected `range-ns` key in stream map.");
1970 goto error;
1971 }
1972
1973 if (bt_value_get_type(range_ns_value) != BT_VALUE_TYPE_MAP) {
5f2a1585 1974 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
30947af0
SM
1975 "expected `range-ns` entry value of stream map to be a map, got %s.",
1976 bt_common_value_type_string(bt_value_get_type(range_ns_value)));
1977 goto error;
1978 }
1979
1980 begin_value = bt_value_map_borrow_entry_value_const(range_ns_value, "begin");
1981 if (!begin_value) {
5f2a1585 1982 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
30947af0
SM
1983 "missing expected `begin` key in range-ns map.");
1984 goto error;
1985 }
1986
1987 if (bt_value_get_type(begin_value) != BT_VALUE_TYPE_SIGNED_INTEGER) {
5f2a1585 1988 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
30947af0
SM
1989 "expected `begin` entry value of range-ns map to be a signed integer, got %s.",
1990 bt_common_value_type_string(bt_value_get_type(range_ns_value)));
1991 goto error;
1992 }
1993
1994 end_value = bt_value_map_borrow_entry_value_const(range_ns_value, "end");
1995 if (!end_value) {
5f2a1585 1996 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
30947af0
SM
1997 "missing expected `end` key in range-ns map.");
1998 goto error;
1999 }
2000
2001 if (bt_value_get_type(end_value) != BT_VALUE_TYPE_SIGNED_INTEGER) {
5f2a1585 2002 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
30947af0
SM
2003 "expected `end` entry value of range-ns map to be a signed integer, got %s.",
2004 bt_common_value_type_string(bt_value_get_type(range_ns_value)));
2005 goto error;
2006 }
2007
2008 begin_ns = bt_value_integer_signed_get(begin_value);
2009 end_ns = bt_value_integer_signed_get(end_value);
2010
2011 if (begin_ns < 0 || end_ns < 0 || end_ns < begin_ns) {
2012 BT_CLI_LOGE_APPEND_CAUSE(
2013 "Invalid stream range values: "
2014 "range-ns:begin=%" PRId64 ", "
2015 "range-ns:end=%" PRId64,
2016 begin_ns, end_ns);
30947af0
SM
2017 goto error;
2018 }
2019
2020 begin_ns_u = begin_ns;
2021 end_ns_u = end_ns;
2022
2023 range->intersection_range_begin_ns =
2024 MAX(range->intersection_range_begin_ns, begin_ns_u);
2025 range->intersection_range_end_ns =
2026 MIN(range->intersection_range_end_ns, end_ns_u);
2027 }
2028
2029 ret = 0;
2030 goto end;
2031error:
2032 ret = -1;
2033
2034end:
2035 return ret;
2036}
2037
75a2cb9b
JG
2038static
2039int set_stream_intersections(struct cmd_run_ctx *ctx,
2040 struct bt_config_component *cfg_comp,
b19ff26f 2041 const bt_component_class_source *src_comp_cls)
75a2cb9b
JG
2042{
2043 int ret = 0;
2044 uint64_t trace_idx;
f80e9ec1 2045 uint64_t trace_count;
b19ff26f
PP
2046 const bt_value *query_result = NULL;
2047 const bt_value *trace_info = NULL;
b19ff26f
PP
2048 const bt_value *stream_infos = NULL;
2049 const bt_value *stream_info = NULL;
75a2cb9b 2050 struct port_id *port_id = NULL;
30947af0 2051 struct trace_range *stream_intersection = NULL;
c7eee084 2052 const char *fail_reason = NULL;
b19ff26f 2053 const bt_component_class *comp_cls =
0d72b8c3 2054 bt_component_class_source_as_component_class_const(src_comp_cls);
75a2cb9b 2055
5f2a1585 2056 ret = query(ctx->cfg, comp_cls, "babeltrace.trace-infos",
f280892e 2057 cfg_comp->params, &query_result,
c7eee084
PP
2058 &fail_reason);
2059 if (ret) {
d4fb887f 2060 BT_CLI_LOGE_APPEND_CAUSE("Failed to execute `babeltrace.trace-infos` query: %s: "
c7eee084 2061 "comp-class-name=\"%s\"", fail_reason,
75a2cb9b
JG
2062 bt_component_class_get_name(comp_cls));
2063 ret = -1;
8dfb232c 2064 goto end;
75a2cb9b
JG
2065 }
2066
f6ccaed9
PP
2067 BT_ASSERT(query_result);
2068
75a2cb9b 2069 if (!bt_value_is_array(query_result)) {
5f2a1585 2070 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: expecting result to be an array: "
8dfb232c
SM
2071 "component-class-name=%s, actual-type=%s",
2072 bt_component_class_get_name(comp_cls),
2073 bt_common_value_type_string(bt_value_get_type(query_result)));
75a2cb9b 2074 ret = -1;
8dfb232c 2075 goto end;
75a2cb9b
JG
2076 }
2077
393729a6 2078 trace_count = bt_value_array_get_length(query_result);
f80e9ec1 2079 if (trace_count == 0) {
5f2a1585 2080 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: result is empty: "
8dfb232c 2081 "component-class-name=%s", bt_component_class_get_name(comp_cls));
75a2cb9b 2082 ret = -1;
8dfb232c 2083 goto end;
75a2cb9b
JG
2084 }
2085
2086 for (trace_idx = 0; trace_idx < trace_count; trace_idx++) {
75a2cb9b
JG
2087 uint64_t stream_idx;
2088 int64_t stream_count;
30947af0 2089 struct trace_range trace_intersection;
75a2cb9b 2090
05e21286 2091 trace_info = bt_value_array_borrow_element_by_index_const(
07208d85 2092 query_result, trace_idx);
8dfb232c 2093 if (!bt_value_is_map(trace_info)) {
75a2cb9b 2094 ret = -1;
5f2a1585 2095 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: expecting element to be a map: "
8dfb232c
SM
2096 "component-class-name=%s, actual-type=%s",
2097 bt_component_class_get_name(comp_cls),
2098 bt_common_value_type_string(bt_value_get_type(trace_info)));
2099 goto end;
75a2cb9b
JG
2100 }
2101
8dfb232c 2102 stream_infos = bt_value_map_borrow_entry_value_const(
5f2a1585 2103 trace_info, "stream-infos");
8dfb232c 2104 if (!stream_infos) {
75a2cb9b 2105 ret = -1;
5f2a1585 2106 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: missing `streams` key in trace info map: "
8dfb232c
SM
2107 "component-class-name=%s",
2108 bt_component_class_get_name(comp_cls));
2109 goto end;
2110 }
2111
2112 if (!bt_value_is_array(stream_infos)) {
2113 ret = -1;
5f2a1585 2114 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: expecting `streams` entry of trace info map to be an array: "
8dfb232c
SM
2115 "component-class-name=%s, actual-type=%s",
2116 bt_component_class_get_name(comp_cls),
2117 bt_common_value_type_string(bt_value_get_type(stream_infos)));
2118 goto end;
75a2cb9b
JG
2119 }
2120
393729a6 2121 stream_count = bt_value_array_get_length(stream_infos);
f80e9ec1 2122 if (stream_count == 0) {
75a2cb9b 2123 ret = -1;
5f2a1585 2124 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: list of streams is empty: "
8dfb232c
SM
2125 "component-class-name=%s",
2126 bt_component_class_get_name(comp_cls));
2127 goto end;
75a2cb9b
JG
2128 }
2129
30947af0
SM
2130 ret = compute_stream_intersection(stream_infos, &trace_intersection);
2131 if (ret != 0) {
2132 BT_CLI_LOGE_APPEND_CAUSE("Failed to compute trace streams intersection.");
8dfb232c 2133 goto end;
30947af0
SM
2134 }
2135
75a2cb9b 2136 for (stream_idx = 0; stream_idx < stream_count; stream_idx++) {
a38d7650 2137 const bt_value *port_name;
75a2cb9b
JG
2138
2139 port_id = g_new0(struct port_id, 1);
2140 if (!port_id) {
2141 ret = -1;
cee05105
PP
2142 BT_CLI_LOGE_APPEND_CAUSE(
2143 "Cannot allocate memory for port_id structure.");
8dfb232c 2144 goto end;
75a2cb9b
JG
2145 }
2146 port_id->instance_name = strdup(cfg_comp->instance_name->str);
2147 if (!port_id->instance_name) {
2148 ret = -1;
cee05105
PP
2149 BT_CLI_LOGE_APPEND_CAUSE(
2150 "Cannot allocate memory for port_id component instance name.");
8dfb232c 2151 goto end;
75a2cb9b
JG
2152 }
2153
30947af0
SM
2154 stream_intersection = g_new0(struct trace_range, 1);
2155 if (!stream_intersection) {
75a2cb9b 2156 ret = -1;
cee05105
PP
2157 BT_CLI_LOGE_APPEND_CAUSE(
2158 "Cannot allocate memory for trace_range structure.");
8dfb232c 2159 goto end;
75a2cb9b 2160 }
30947af0
SM
2161
2162 *stream_intersection = trace_intersection;
75a2cb9b 2163
05e21286 2164 stream_info = bt_value_array_borrow_element_by_index_const(
07208d85 2165 stream_infos, stream_idx);
8dfb232c 2166 if (!bt_value_is_map(stream_info)) {
75a2cb9b 2167 ret = -1;
5f2a1585 2168 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: "
8dfb232c
SM
2169 "expecting element of stream list to be a map: "
2170 "component-class-name=%s, actual-type=%s",
2171 bt_component_class_get_name(comp_cls),
2172 bt_common_value_type_string(bt_value_get_type(stream_info)));
2173 goto end;
75a2cb9b
JG
2174 }
2175
a38d7650 2176 port_name = bt_value_map_borrow_entry_value_const(stream_info, "port-name");
8dfb232c 2177 if (!port_name) {
75a2cb9b 2178 ret = -1;
5f2a1585 2179 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: "
8dfb232c
SM
2180 "missing `port-name` key in stream info map: "
2181 "component-class-name=%s",
2182 bt_component_class_get_name(comp_cls));
2183 goto end;
2184 }
2185
2186 if (!bt_value_is_string(port_name)) {
2187 ret = -1;
5f2a1585 2188 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: "
8dfb232c
SM
2189 "expecting `port-name` entry of stream info map to be a string: "
2190 "component-class-name=%s, actual-type=%s",
2191 bt_component_class_get_name(comp_cls),
2192 bt_common_value_type_string(bt_value_get_type(port_name)));
2193 goto end;
75a2cb9b
JG
2194 }
2195
a38d7650 2196 port_id->port_name = g_strdup(bt_value_string_get(port_name));
75a2cb9b
JG
2197 if (!port_id->port_name) {
2198 ret = -1;
cee05105
PP
2199 BT_CLI_LOGE_APPEND_CAUSE(
2200 "Cannot allocate memory for port_id port_name.");
8dfb232c 2201 goto end;
75a2cb9b
JG
2202 }
2203
2204 BT_LOGD("Inserting stream intersection ");
2205
30947af0 2206 g_hash_table_insert(ctx->intersections, port_id, stream_intersection);
75a2cb9b
JG
2207
2208 port_id = NULL;
30947af0 2209 stream_intersection = NULL;
75a2cb9b 2210 }
75a2cb9b
JG
2211 }
2212
8dfb232c 2213 ret = 0;
cee05105 2214
75a2cb9b 2215end:
c5b9b441 2216 bt_value_put_ref(query_result);
75a2cb9b 2217 g_free(port_id);
30947af0 2218 g_free(stream_intersection);
75a2cb9b
JG
2219 return ret;
2220}
2221
9009cc24
PP
2222static
2223int cmd_run_ctx_create_components_from_config_components(
2224 struct cmd_run_ctx *ctx, GPtrArray *cfg_components)
2225{
2226 size_t i;
0d72b8c3
PP
2227 const void *comp_cls = NULL;
2228 const void *comp = NULL;
9009cc24
PP
2229 int ret = 0;
2230
2231 for (i = 0; i < cfg_components->len; i++) {
2232 struct bt_config_component *cfg_comp =
2233 g_ptr_array_index(cfg_components, i);
2234 GQuark quark;
2235
d94d92ac
PP
2236 switch (cfg_comp->type) {
2237 case BT_COMPONENT_CLASS_TYPE_SOURCE:
2238 comp_cls = find_source_component_class(
2239 cfg_comp->plugin_name->str,
2240 cfg_comp->comp_cls_name->str);
2241 break;
2242 case BT_COMPONENT_CLASS_TYPE_FILTER:
2243 comp_cls = find_filter_component_class(
2244 cfg_comp->plugin_name->str,
2245 cfg_comp->comp_cls_name->str);
2246 break;
2247 case BT_COMPONENT_CLASS_TYPE_SINK:
2248 comp_cls = find_sink_component_class(
2249 cfg_comp->plugin_name->str,
2250 cfg_comp->comp_cls_name->str);
2251 break;
2252 default:
498e7994 2253 bt_common_abort();
d94d92ac
PP
2254 }
2255
9009cc24 2256 if (!comp_cls) {
cee05105
PP
2257 BT_CLI_LOGE_APPEND_CAUSE(
2258 "Cannot find component class: plugin-name=\"%s\", "
7213a328
PP
2259 "comp-cls-name=\"%s\", comp-cls-type=%d",
2260 cfg_comp->plugin_name->str,
2261 cfg_comp->comp_cls_name->str,
2262 cfg_comp->type);
9009cc24
PP
2263 goto error;
2264 }
2265
ef267d12 2266 BT_ASSERT(cfg_comp->log_level >= BT_LOG_TRACE);
29da2ffc 2267
d94d92ac
PP
2268 switch (cfg_comp->type) {
2269 case BT_COMPONENT_CLASS_TYPE_SOURCE:
0d72b8c3 2270 ret = bt_graph_add_source_component(ctx->graph,
d94d92ac 2271 comp_cls, cfg_comp->instance_name->str,
29da2ffc 2272 cfg_comp->params, cfg_comp->log_level,
d94d92ac 2273 (void *) &comp);
a91462c9 2274 bt_component_source_get_ref(comp);
d94d92ac
PP
2275 break;
2276 case BT_COMPONENT_CLASS_TYPE_FILTER:
0d72b8c3 2277 ret = bt_graph_add_filter_component(ctx->graph,
d94d92ac 2278 comp_cls, cfg_comp->instance_name->str,
29da2ffc 2279 cfg_comp->params, cfg_comp->log_level,
d94d92ac 2280 (void *) &comp);
a91462c9 2281 bt_component_filter_get_ref(comp);
d94d92ac
PP
2282 break;
2283 case BT_COMPONENT_CLASS_TYPE_SINK:
0d72b8c3 2284 ret = bt_graph_add_sink_component(ctx->graph,
d94d92ac 2285 comp_cls, cfg_comp->instance_name->str,
29da2ffc 2286 cfg_comp->params, cfg_comp->log_level,
d94d92ac 2287 (void *) &comp);
a91462c9 2288 bt_component_sink_get_ref(comp);
d94d92ac
PP
2289 break;
2290 default:
498e7994 2291 bt_common_abort();
d94d92ac
PP
2292 }
2293
36712f1d 2294 if (ret) {
cee05105
PP
2295 BT_CLI_LOGE_APPEND_CAUSE(
2296 "Cannot create component: plugin-name=\"%s\", "
32e87ceb 2297 "comp-cls-name=\"%s\", comp-cls-type=%d, "
7213a328
PP
2298 "comp-name=\"%s\"",
2299 cfg_comp->plugin_name->str,
2300 cfg_comp->comp_cls_name->str,
2301 cfg_comp->type, cfg_comp->instance_name->str);
9009cc24
PP
2302 goto error;
2303 }
2304
75a2cb9b
JG
2305 if (ctx->stream_intersection_mode &&
2306 cfg_comp->type == BT_COMPONENT_CLASS_TYPE_SOURCE) {
2307 ret = set_stream_intersections(ctx, cfg_comp, comp_cls);
2308 if (ret) {
8dfb232c
SM
2309 BT_CLI_LOGE_APPEND_CAUSE(
2310 "Cannot determine stream intersection of trace.");
75a2cb9b
JG
2311 goto error;
2312 }
2313 }
2314
7213a328
PP
2315 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2316 comp, cfg_comp->instance_name->str);
9009cc24 2317 quark = g_quark_from_string(cfg_comp->instance_name->str);
f6ccaed9 2318 BT_ASSERT(quark > 0);
d94d92ac
PP
2319
2320 switch (cfg_comp->type) {
2321 case BT_COMPONENT_CLASS_TYPE_SOURCE:
2322 g_hash_table_insert(ctx->src_components,
0d72b8c3 2323 GUINT_TO_POINTER(quark), (void *) comp);
d94d92ac
PP
2324 break;
2325 case BT_COMPONENT_CLASS_TYPE_FILTER:
2326 g_hash_table_insert(ctx->flt_components,
0d72b8c3 2327 GUINT_TO_POINTER(quark), (void *) comp);
d94d92ac
PP
2328 break;
2329 case BT_COMPONENT_CLASS_TYPE_SINK:
2330 g_hash_table_insert(ctx->sink_components,
0d72b8c3 2331 GUINT_TO_POINTER(quark), (void *) comp);
d94d92ac
PP
2332 break;
2333 default:
498e7994 2334 bt_common_abort();
d94d92ac
PP
2335 }
2336
9009cc24 2337 comp = NULL;
65300d60 2338 BT_OBJECT_PUT_REF_AND_RESET(comp_cls);
9009cc24
PP
2339 }
2340
2341 goto end;
2342
2343error:
2344 ret = -1;
2345
2346end:
65300d60
PP
2347 bt_object_put_ref(comp);
2348 bt_object_put_ref(comp_cls);
9009cc24
PP
2349 return ret;
2350}
56a1cced 2351
9009cc24
PP
2352static
2353int cmd_run_ctx_create_components(struct cmd_run_ctx *ctx)
2354{
2355 int ret = 0;
2356
2357 /*
2358 * Make sure that, during this phase, our graph's "port added"
2359 * listener does not connect ports while we are creating the
2360 * components because we have a special, initial phase for
2361 * this.
2362 */
2363 ctx->connect_ports = false;
2364
2365 ret = cmd_run_ctx_create_components_from_config_components(
2366 ctx, ctx->cfg->cmd_data.run.sources);
2367 if (ret) {
7c7c0433 2368 ret = -1;
2e339de1
JG
2369 goto end;
2370 }
2371
9009cc24
PP
2372 ret = cmd_run_ctx_create_components_from_config_components(
2373 ctx, ctx->cfg->cmd_data.run.filters);
6c2f3ee5 2374 if (ret) {
290725f7 2375 ret = -1;
fec2a9f2
JG
2376 goto end;
2377 }
78586d8a 2378
9009cc24
PP
2379 ret = cmd_run_ctx_create_components_from_config_components(
2380 ctx, ctx->cfg->cmd_data.run.sinks);
2381 if (ret) {
2382 ret = -1;
2383 goto end;
2384 }
2385
2386end:
2387 return ret;
2388}
2389
0d72b8c3 2390typedef uint64_t (*output_port_count_func_t)(const void *);
b19ff26f 2391typedef const bt_port_output *(*borrow_output_port_by_index_func_t)(
0d72b8c3 2392 const void *, uint64_t);
d94d92ac 2393
9009cc24
PP
2394static
2395int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx *ctx,
d94d92ac
PP
2396 void *comp, output_port_count_func_t port_count_fn,
2397 borrow_output_port_by_index_func_t port_by_index_fn)
9009cc24
PP
2398{
2399 int ret = 0;
d94d92ac 2400 uint64_t count;
9009cc24
PP
2401 uint64_t i;
2402
2403 count = port_count_fn(comp);
9009cc24
PP
2404
2405 for (i = 0; i < count; i++) {
b19ff26f 2406 const bt_port_output *upstream_port = port_by_index_fn(comp, i);
9009cc24 2407
f6ccaed9 2408 BT_ASSERT(upstream_port);
9009cc24 2409 ret = cmd_run_ctx_connect_upstream_port(ctx, upstream_port);
9009cc24
PP
2410 if (ret) {
2411 goto end;
2412 }
2413 }
2414
2415end:
2416 return ret;
2417}
2418
2419static
2420int cmd_run_ctx_connect_ports(struct cmd_run_ctx *ctx)
2421{
2422 int ret = 0;
2423 GHashTableIter iter;
2424 gpointer g_name_quark, g_comp;
2425
2426 ctx->connect_ports = true;
d94d92ac 2427 g_hash_table_iter_init(&iter, ctx->src_components);
9009cc24
PP
2428
2429 while (g_hash_table_iter_next(&iter, &g_name_quark, &g_comp)) {
d94d92ac
PP
2430 ret = cmd_run_ctx_connect_comp_ports(ctx, g_comp,
2431 (output_port_count_func_t)
2432 bt_component_source_get_output_port_count,
2433 (borrow_output_port_by_index_func_t)
0d72b8c3 2434 bt_component_source_borrow_output_port_by_index_const);
d94d92ac
PP
2435 if (ret) {
2436 goto end;
9009cc24 2437 }
d94d92ac
PP
2438 }
2439
2440 g_hash_table_iter_init(&iter, ctx->flt_components);
9009cc24 2441
d94d92ac
PP
2442 while (g_hash_table_iter_next(&iter, &g_name_quark, &g_comp)) {
2443 ret = cmd_run_ctx_connect_comp_ports(ctx, g_comp,
2444 (output_port_count_func_t)
2445 bt_component_filter_get_output_port_count,
2446 (borrow_output_port_by_index_func_t)
0d72b8c3 2447 bt_component_filter_borrow_output_port_by_index_const);
9009cc24
PP
2448 if (ret) {
2449 goto end;
2450 }
2451 }
2452
2453end:
2454 return ret;
2455}
2456
2457static
b5cd7d65 2458enum bt_cmd_status cmd_run(struct bt_config *cfg)
9009cc24 2459{
b5cd7d65 2460 enum bt_cmd_status cmd_status;
9009cc24
PP
2461 struct cmd_run_ctx ctx = { 0 };
2462
9009cc24
PP
2463 /* Initialize the command's context and the graph object */
2464 if (cmd_run_ctx_init(&ctx, cfg)) {
cee05105
PP
2465 BT_CLI_LOGE_APPEND_CAUSE(
2466 "Cannot initialize the command's context.");
9009cc24
PP
2467 goto error;
2468 }
2469
aede8fc9 2470 if (bt_interrupter_is_set(the_interrupter)) {
63d355d2 2471 BT_CLI_LOGW_APPEND_CAUSE(
9b4f9b42 2472 "Interrupted by user before creating components.");
cc308374
PP
2473 goto error;
2474 }
2475
2476 BT_LOGI_STR("Creating components.");
2477
9009cc24
PP
2478 /* Create the requested component instances */
2479 if (cmd_run_ctx_create_components(&ctx)) {
cee05105 2480 BT_CLI_LOGE_APPEND_CAUSE("Cannot create components.");
9009cc24
PP
2481 goto error;
2482 }
2483
aede8fc9 2484 if (bt_interrupter_is_set(the_interrupter)) {
63d355d2 2485 BT_CLI_LOGW_APPEND_CAUSE(
9b4f9b42 2486 "Interrupted by user before connecting components.");
cc308374
PP
2487 goto error;
2488 }
2489
2490 BT_LOGI_STR("Connecting components.");
2491
9009cc24
PP
2492 /* Connect the initially visible component ports */
2493 if (cmd_run_ctx_connect_ports(&ctx)) {
cee05105
PP
2494 BT_CLI_LOGE_APPEND_CAUSE(
2495 "Cannot connect initial component ports.");
9009cc24
PP
2496 goto error;
2497 }
2498
7213a328
PP
2499 BT_LOGI_STR("Running the graph.");
2500
9009cc24 2501 /* Run the graph */
fec2a9f2 2502 while (true) {
d24d5663 2503 bt_graph_run_status run_status = bt_graph_run(ctx.graph);
61ddbc8a 2504
5669a3e7
PP
2505 /*
2506 * Reset console in case something messed with console
2507 * codes during the graph's execution.
2508 */
2509 printf("%s", bt_common_color_reset());
2510 fflush(stdout);
2511 fprintf(stderr, "%s", bt_common_color_reset());
ef267d12 2512 BT_LOGT("bt_graph_run() returned: status=%s",
d24d5663 2513 bt_common_func_status_string(run_status));
fd948396 2514
d24d5663
PP
2515 switch (run_status) {
2516 case BT_GRAPH_RUN_STATUS_OK:
b5cd7d65 2517 cmd_status = BT_CMD_STATUS_OK;
9669d693 2518 goto end;
d24d5663 2519 case BT_GRAPH_RUN_STATUS_AGAIN:
9b4f9b42 2520 if (bt_interrupter_is_set(the_interrupter)) {
b5cd7d65
FD
2521 /*
2522 * The graph was interrupted by a SIGINT.
2523 */
2524 cmd_status = BT_CMD_STATUS_INTERRUPTED;
2525 goto end;
5401f780
PP
2526 }
2527
9009cc24 2528 if (cfg->cmd_data.run.retry_duration_us > 0) {
d24d5663 2529 BT_LOGT("Got BT_GRAPH_RUN_STATUS_AGAIN: sleeping: "
7213a328
PP
2530 "time-us=%" PRIu64,
2531 cfg->cmd_data.run.retry_duration_us);
2532
9009cc24 2533 if (usleep(cfg->cmd_data.run.retry_duration_us)) {
9b4f9b42 2534 if (bt_interrupter_is_set(the_interrupter)) {
b5cd7d65
FD
2535 cmd_status = BT_CMD_STATUS_INTERRUPTED;
2536 goto end;
cfa4637b 2537 }
9009cc24
PP
2538 }
2539 }
78586d8a 2540 break;
fec2a9f2 2541 default:
9b4f9b42 2542 if (bt_interrupter_is_set(the_interrupter)) {
b5cd7d65
FD
2543 cmd_status = BT_CMD_STATUS_INTERRUPTED;
2544 goto end;
9b4f9b42
PP
2545 }
2546
cee05105
PP
2547 BT_CLI_LOGE_APPEND_CAUSE(
2548 "Graph failed to complete successfully");
9009cc24 2549 goto error;
78586d8a 2550 }
fec2a9f2 2551 }
9009cc24 2552error:
b5cd7d65 2553 cmd_status = BT_CMD_STATUS_ERROR;
9009cc24 2554
11e1d048 2555end:
9009cc24 2556 cmd_run_ctx_destroy(&ctx);
b5cd7d65 2557 return cmd_status;
290725f7
PP
2558}
2559
9009cc24
PP
2560static
2561void warn_command_name_and_directory_clash(struct bt_config *cfg)
290725f7 2562{
9009cc24
PP
2563 const char *env_clash;
2564
290725f7
PP
2565 if (!cfg->command_name) {
2566 return;
2567 }
2568
9009cc24
PP
2569 env_clash = getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH);
2570 if (env_clash && strcmp(env_clash, "0") == 0) {
2571 return;
2572 }
2573
290725f7
PP
2574 if (g_file_test(cfg->command_name,
2575 G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
cee05105 2576 _bt_log_write_d(_BT_LOG_SRCLOC_FUNCTION, __FILE__, __LINE__,
770538dd 2577 BT_LOG_WARNING, BT_LOG_TAG,
cee05105
PP
2578 "The `%s` command was executed. "
2579 "If you meant to convert a trace located in "
2580 "the local `%s` directory, please use:\n\n"
2581 " babeltrace2 convert %s [OPTIONS]",
2582 cfg->command_name, cfg->command_name,
2583 cfg->command_name);
290725f7
PP
2584 }
2585}
2586
7213a328
PP
2587static
2588void init_log_level(void)
2589{
c6d4d1ae 2590 bt_cli_log_level = bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL);
7213a328
PP
2591}
2592
f393c19b
PP
2593static
2594void print_error_causes(void)
2595{
2596 const bt_error *error = bt_current_thread_take_error();
f393c19b 2597 unsigned int columns;
96c1b5fe 2598 gchar *error_str = NULL;
f393c19b
PP
2599
2600 if (!error || bt_error_get_cause_count(error) == 0) {
2601 fprintf(stderr, "%s%sUnknown command-line error.%s\n",
04609487 2602 bt_common_color_bold(), bt_common_color_fg_bright_red(),
f393c19b
PP
2603 bt_common_color_reset());
2604 goto end;
2605 }
2606
2607 /* Try to get terminal width to fold the error cause messages */
2608 if (bt_common_get_term_size(&columns, NULL) < 0) {
2609 /* Width not found: default to 80 */
2610 columns = 80;
2611 }
2612
2613 /*
2614 * This helps visually separate the error causes from the last
2615 * logging statement.
2616 */
96c1b5fe 2617 fputc('\n', stderr);
f393c19b 2618
96c1b5fe
SM
2619 error_str = format_bt_error(error, columns, bt_cli_log_level,
2620 BT_COMMON_COLOR_WHEN_AUTO);
2621 BT_ASSERT(error_str);
f393c19b 2622
96c1b5fe 2623 fprintf(stderr, "%s\n", error_str);
f393c19b
PP
2624
2625end:
f393c19b
PP
2626 if (error) {
2627 bt_error_release(error);
2628 }
ec532b44 2629
96c1b5fe 2630 g_free(error_str);
f393c19b
PP
2631}
2632
290725f7
PP
2633int main(int argc, const char **argv)
2634{
b5cd7d65
FD
2635 int ret, retcode;
2636 enum bt_cmd_status cmd_status;
3dae1685 2637 struct bt_config *cfg = NULL;
290725f7 2638
7213a328 2639 init_log_level();
65d3198f 2640 set_signal_handler();
743138a3 2641 init_loaded_plugins();
3dae1685
SM
2642
2643 BT_ASSERT(!the_interrupter);
2644 the_interrupter = bt_interrupter_create();
2645 if (!the_interrupter) {
2646 BT_CLI_LOGE_APPEND_CAUSE("Failed to create an interrupter object.");
2647 retcode = 1;
2648 goto end;
2649 }
2650
2651 cfg = bt_config_cli_args_create_with_default(argc, argv, &retcode,
2652 the_interrupter);
290725f7
PP
2653
2654 if (retcode < 0) {
2655 /* Quit without errors; typically usage/version */
2656 retcode = 0;
7213a328 2657 BT_LOGI_STR("Quitting without errors.");
290725f7
PP
2658 goto end;
2659 }
2660
2661 if (retcode > 0) {
cee05105
PP
2662 BT_CLI_LOGE_APPEND_CAUSE(
2663 "Command-line error: retcode=%d", retcode);
290725f7
PP
2664 goto end;
2665 }
2666
2667 if (!cfg) {
cee05105 2668 BT_CLI_LOGE_APPEND_CAUSE(
9442c9a2 2669 "Failed to create a valid Babeltrace CLI configuration.");
db0f160a 2670 retcode = 1;
290725f7
PP
2671 goto end;
2672 }
2673
290725f7
PP
2674 print_cfg(cfg);
2675
db0f160a 2676 if (cfg->command_needs_plugins) {
73760435 2677 ret = require_loaded_plugins(cfg->plugin_paths);
db0f160a 2678 if (ret) {
cee05105
PP
2679 BT_CLI_LOGE_APPEND_CAUSE(
2680 "Failed to load plugins: ret=%d", ret);
db0f160a
PP
2681 retcode = 1;
2682 goto end;
2683 }
2684 }
2685
7213a328
PP
2686 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2687 cfg->command, cfg->command_name);
2688
290725f7 2689 switch (cfg->command) {
db0f160a 2690 case BT_CONFIG_COMMAND_RUN:
b5cd7d65 2691 cmd_status = cmd_run(cfg);
290725f7
PP
2692 break;
2693 case BT_CONFIG_COMMAND_LIST_PLUGINS:
b5cd7d65 2694 cmd_status = cmd_list_plugins(cfg);
290725f7 2695 break;
22e22462 2696 case BT_CONFIG_COMMAND_HELP:
b5cd7d65 2697 cmd_status = cmd_help(cfg);
22e22462 2698 break;
a67681c1 2699 case BT_CONFIG_COMMAND_QUERY:
b5cd7d65 2700 cmd_status = cmd_query(cfg);
63ce0e1d 2701 break;
db0f160a 2702 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA:
b5cd7d65 2703 cmd_status = cmd_print_ctf_metadata(cfg);
db0f160a
PP
2704 break;
2705 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS:
b5cd7d65 2706 cmd_status = cmd_print_lttng_live_sessions(cfg);
db0f160a 2707 break;
290725f7 2708 default:
0fbb9a9f 2709 BT_LOGF("Invalid/unknown command: cmd=%d", cfg->command);
498e7994 2710 bt_common_abort();
290725f7
PP
2711 }
2712
b5cd7d65
FD
2713 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", command-status=\"%s\"",
2714 cfg->command, cfg->command_name, bt_cmd_status_string(cmd_status));
290725f7 2715 warn_command_name_and_directory_clash(cfg);
b5cd7d65
FD
2716
2717 switch (cmd_status) {
2718 case BT_CMD_STATUS_OK:
2719 retcode = 0;
2720 break;
2721 case BT_CMD_STATUS_ERROR:
2722 retcode = 1;
b5cd7d65
FD
2723 break;
2724 case BT_CMD_STATUS_INTERRUPTED:
2725 retcode = 2;
2726 break;
2727 default:
2728 BT_LOGF("Invalid command status: cmd-status=%d", cmd_status);
2729 bt_common_abort();
2730 }
290725f7
PP
2731
2732end:
927c0693
SM
2733 if (retcode == 1) {
2734 print_error_causes();
2735 }
2736
65300d60 2737 BT_OBJECT_PUT_REF_AND_RESET(cfg);
743138a3 2738 fini_loaded_plugins();
9b4f9b42 2739 bt_interrupter_put_ref(the_interrupter);
0232a535
PP
2740
2741 /*
2742 * Clear current thread's error in case there is one to avoid a
2743 * memory leak.
2744 */
2745 bt_current_thread_clear_error();
290725f7 2746 return retcode;
4c8bfb7e 2747}
This page took 0.298504 seconds and 4 git commands to generate.