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