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