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