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