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