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