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