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