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