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