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