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