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