Fix: notif-iter.c: handle single/implicit SC/EC correctly
[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,
05e21286 173 const struct bt_value *params, const struct bt_value **user_result,
c7eee084
PP
174 const char **fail_reason)
175{
05e21286 176 const 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
05e21286 456void print_value(FILE *, const struct bt_value *, size_t);
c42c79ea 457
c1081aa6 458static
05e21286 459void print_value_rec(FILE *, const struct bt_value *, size_t);
7213a328
PP
460
461struct print_map_value_data {
462 size_t indent;
463 FILE *fp;
464};
c1081aa6 465
c42c79ea 466static
05e21286
PP
467bt_bool print_map_value(const char *key, const struct bt_value *object,
468 void *data)
c42c79ea 469{
7213a328 470 struct print_map_value_data *print_map_value_data = data;
290725f7 471
7213a328
PP
472 print_indent(print_map_value_data->fp, print_map_value_data->indent);
473 fprintf(print_map_value_data->fp, "%s: ", key);
f6ccaed9 474 BT_ASSERT(object);
290725f7
PP
475
476 if (bt_value_is_array(object) &&
477 bt_value_array_is_empty(object)) {
7213a328 478 fprintf(print_map_value_data->fp, "[ ]\n");
290725f7
PP
479 return true;
480 }
481
482 if (bt_value_is_map(object) &&
483 bt_value_map_is_empty(object)) {
7213a328 484 fprintf(print_map_value_data->fp, "{ }\n");
290725f7
PP
485 return true;
486 }
c42c79ea 487
290725f7
PP
488 if (bt_value_is_array(object) ||
489 bt_value_is_map(object)) {
7213a328 490 fprintf(print_map_value_data->fp, "\n");
290725f7 491 }
c42c79ea 492
7213a328
PP
493 print_value_rec(print_map_value_data->fp, object,
494 print_map_value_data->indent + 2);
c55a9f58 495 return BT_TRUE;
c42c79ea
PP
496}
497
498static
05e21286 499void print_value_rec(FILE *fp, const struct bt_value *value, size_t indent)
c42c79ea 500{
c55a9f58 501 bt_bool bool_val;
c42c79ea
PP
502 int64_t int_val;
503 double dbl_val;
504 const char *str_val;
505 int size;
506 int i;
507
508 if (!value) {
509 return;
510 }
511
c42c79ea
PP
512 switch (bt_value_get_type(value)) {
513 case BT_VALUE_TYPE_NULL:
7213a328 514 fprintf(fp, "%snull%s\n", bt_common_color_bold(),
c1081aa6 515 bt_common_color_reset());
c42c79ea
PP
516 break;
517 case BT_VALUE_TYPE_BOOL:
601b0d3c 518 bool_val = bt_value_bool_get(value);
7213a328 519 fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(),
c1081aa6
PP
520 bt_common_color_fg_cyan(), bool_val ? "yes" : "no",
521 bt_common_color_reset());
c42c79ea
PP
522 break;
523 case BT_VALUE_TYPE_INTEGER:
601b0d3c 524 int_val = bt_value_integer_get(value);
7213a328 525 fprintf(fp, "%s%s%" PRId64 "%s\n", bt_common_color_bold(),
c1081aa6
PP
526 bt_common_color_fg_red(), int_val,
527 bt_common_color_reset());
c42c79ea 528 break;
a373bf69 529 case BT_VALUE_TYPE_REAL:
601b0d3c 530 dbl_val = bt_value_real_get(value);
7213a328 531 fprintf(fp, "%s%s%lf%s\n", bt_common_color_bold(),
c1081aa6
PP
532 bt_common_color_fg_red(), dbl_val,
533 bt_common_color_reset());
c42c79ea
PP
534 break;
535 case BT_VALUE_TYPE_STRING:
601b0d3c 536 str_val = bt_value_string_get(value);
7213a328 537 fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(),
c1081aa6
PP
538 bt_common_color_fg_green(), str_val,
539 bt_common_color_reset());
c42c79ea
PP
540 break;
541 case BT_VALUE_TYPE_ARRAY:
07208d85 542 size = bt_value_array_get_size(value);
60a97734
MD
543 if (size < 0) {
544 goto error;
545 }
290725f7
PP
546
547 if (size == 0) {
7213a328
PP
548 print_indent(fp, indent);
549 fprintf(fp, "[ ]\n");
290725f7
PP
550 break;
551 }
c42c79ea
PP
552
553 for (i = 0; i < size; i++) {
05e21286
PP
554 const struct bt_value *element =
555 bt_value_array_borrow_element_by_index_const(
07208d85 556 value, i);
c42c79ea 557
60a97734
MD
558 if (!element) {
559 goto error;
560 }
7213a328
PP
561 print_indent(fp, indent);
562 fprintf(fp, "- ");
290725f7
PP
563
564 if (bt_value_is_array(element) &&
565 bt_value_array_is_empty(element)) {
7213a328 566 fprintf(fp, "[ ]\n");
290725f7
PP
567 continue;
568 }
569
570 if (bt_value_is_map(element) &&
571 bt_value_map_is_empty(element)) {
7213a328 572 fprintf(fp, "{ }\n");
290725f7
PP
573 continue;
574 }
575
576 if (bt_value_is_array(element) ||
577 bt_value_is_map(element)) {
7213a328 578 fprintf(fp, "\n");
290725f7
PP
579 }
580
7213a328 581 print_value_rec(fp, element, indent + 2);
c42c79ea 582 }
c42c79ea
PP
583 break;
584 case BT_VALUE_TYPE_MAP:
7213a328
PP
585 {
586 struct print_map_value_data data = {
587 .indent = indent,
588 .fp = fp,
589 };
590
c42c79ea 591 if (bt_value_map_is_empty(value)) {
7213a328
PP
592 print_indent(fp, indent);
593 fprintf(fp, "{ }\n");
290725f7 594 break;
c42c79ea
PP
595 }
596
05e21286 597 bt_value_map_foreach_entry_const(value, print_map_value, &data);
c42c79ea 598 break;
7213a328 599 }
c42c79ea 600 default:
0fbb9a9f 601 abort();
c42c79ea 602 }
60a97734
MD
603 return;
604
605error:
606 BT_LOGE("Error printing value of type %s.",
da91b29a 607 bt_common_value_type_string(bt_value_get_type(value)));
c42c79ea
PP
608}
609
c1081aa6 610static
05e21286 611void print_value(FILE *fp, const struct bt_value *value, size_t indent)
c1081aa6
PP
612{
613 if (!bt_value_is_array(value) && !bt_value_is_map(value)) {
7213a328 614 print_indent(fp, indent);
c1081aa6
PP
615 }
616
7213a328 617 print_value_rec(fp, value, indent);
c1081aa6
PP
618}
619
c42c79ea
PP
620static
621void print_bt_config_component(struct bt_config_component *bt_config_component)
622{
7213a328
PP
623 fprintf(stderr, " ");
624 print_plugin_comp_cls_opt(stderr, bt_config_component->plugin_name->str,
db0f160a 625 bt_config_component->comp_cls_name->str,
87796884 626 bt_config_component->type);
7213a328 627 fprintf(stderr, ":\n");
3b6cfcc5
PP
628
629 if (bt_config_component->instance_name->len > 0) {
7213a328 630 fprintf(stderr, " Name: %s\n",
3b6cfcc5
PP
631 bt_config_component->instance_name->str);
632 }
633
7213a328 634 fprintf(stderr, " Parameters:\n");
05e21286 635 print_value(stderr, 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 651static
05e21286 652void print_plugin_paths(const struct bt_value *plugin_paths)
290725f7 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
05e21286 663 print_plugin_paths(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{
05e21286 694 print_plugin_paths(cfg->plugin_paths);
290725f7
PP
695}
696
c1081aa6
PP
697static
698void print_cfg_help(struct bt_config *cfg)
699{
05e21286 700 print_plugin_paths(cfg->plugin_paths);
db0f160a
PP
701}
702
703static
704void print_cfg_print_ctf_metadata(struct bt_config *cfg)
705{
05e21286 706 print_plugin_paths(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{
05e21286 714 print_plugin_paths(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{
05e21286 722 print_plugin_paths(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
05e21286 799int load_dynamic_plugins(const 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 812 for (i = 0; i < nr_paths; i++) {
05e21286 813 const struct bt_value *plugin_path_value = NULL;
98ecef32 814 const char *plugin_path;
a8ff38ef 815 struct bt_plugin_set *plugin_set;
98ecef32 816
05e21286
PP
817 plugin_path_value =
818 bt_value_array_borrow_element_by_index_const(
819 plugin_paths, i);
601b0d3c 820 plugin_path = bt_value_string_get(plugin_path_value);
50ad9320
PP
821
822 /*
823 * Skip this if the directory does not exist because
824 * bt_plugin_create_all_from_dir() expects an existing
825 * directory.
826 */
827 if (!g_file_test(plugin_path, G_FILE_TEST_IS_DIR)) {
828 BT_LOGV("Skipping nonexistent directory path: "
829 "path=\"%s\"", plugin_path);
50ad9320
PP
830 continue;
831 }
832
a8ff38ef
PP
833 plugin_set = bt_plugin_create_all_from_dir(plugin_path, false);
834 if (!plugin_set) {
7213a328 835 BT_LOGD("Unable to load dynamic plugins: path=\"%s\"",
98ecef32 836 plugin_path);
33b34c43 837 continue;
98ecef32 838 }
33b34c43 839
a8ff38ef 840 add_to_loaded_plugins(plugin_set);
65300d60 841 bt_object_put_ref(plugin_set);
98ecef32 842 }
33b34c43
PP
843end:
844 return ret;
845}
846
847static
848int load_static_plugins(void)
849{
850 int ret = 0;
a8ff38ef 851 struct bt_plugin_set *plugin_set;
33b34c43 852
7213a328 853 BT_LOGI("Loading static plugins.");
a8ff38ef
PP
854 plugin_set = bt_plugin_create_all_from_static();
855 if (!plugin_set) {
7213a328 856 BT_LOGE("Unable to load static plugins.");
33b34c43
PP
857 ret = -1;
858 goto end;
859 }
860
a8ff38ef 861 add_to_loaded_plugins(plugin_set);
65300d60 862 bt_object_put_ref(plugin_set);
33b34c43
PP
863end:
864 return ret;
98ecef32
MD
865}
866
9009cc24 867static
05e21286 868int load_all_plugins(const struct bt_value *plugin_paths)
290725f7
PP
869{
870 int ret = 0;
33b34c43 871
290725f7 872 if (load_dynamic_plugins(plugin_paths)) {
290725f7 873 ret = -1;
c1870f57
JG
874 goto end;
875 }
876
290725f7 877 if (load_static_plugins()) {
290725f7 878 ret = -1;
c1870f57
JG
879 goto end;
880 }
881
7213a328
PP
882 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins->len);
883
290725f7
PP
884end:
885 return ret;
886}
887
9009cc24
PP
888static
889void print_plugin_info(struct bt_plugin *plugin)
22e22462
PP
890{
891 unsigned int major, minor, patch;
892 const char *extra;
893 enum bt_plugin_status version_status;
894 const char *plugin_name;
895 const char *path;
896 const char *author;
897 const char *license;
898 const char *plugin_description;
899
900 plugin_name = bt_plugin_get_name(plugin);
901 path = bt_plugin_get_path(plugin);
902 author = bt_plugin_get_author(plugin);
903 license = bt_plugin_get_license(plugin);
904 plugin_description = bt_plugin_get_description(plugin);
905 version_status = bt_plugin_get_version(plugin, &major, &minor,
906 &patch, &extra);
907 printf("%s%s%s%s:\n", bt_common_color_bold(),
908 bt_common_color_fg_blue(), plugin_name,
909 bt_common_color_reset());
a157fea9
MJ
910 if (path) {
911 printf(" %sPath%s: %s\n", bt_common_color_bold(),
912 bt_common_color_reset(), path);
913 } else {
914 puts(" Built-in");
915 }
22e22462
PP
916
917 if (version_status == BT_PLUGIN_STATUS_OK) {
918 printf(" %sVersion%s: %u.%u.%u",
919 bt_common_color_bold(), bt_common_color_reset(),
920 major, minor, patch);
921
922 if (extra) {
923 printf("%s", extra);
924 }
925
926 printf("\n");
927 }
928
929 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
930 bt_common_color_reset(),
931 plugin_description ? plugin_description : "(None)");
932 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
933 bt_common_color_reset(), author ? author : "(Unknown)");
934 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
935 bt_common_color_reset(),
936 license ? license : "(Unknown)");
937}
938
9009cc24
PP
939static
940int cmd_query(struct bt_config *cfg)
63ce0e1d 941{
db95fa29 942 int ret = 0;
63ce0e1d 943 struct bt_component_class *comp_cls = NULL;
05e21286 944 const struct bt_value *results = NULL;
c7eee084 945 const char *fail_reason = NULL;
63ce0e1d 946
d94d92ac
PP
947 comp_cls = find_component_class(
948 cfg->cmd_data.query.cfg_component->plugin_name->str,
db0f160a 949 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
a67681c1 950 cfg->cmd_data.query.cfg_component->type);
63ce0e1d 951 if (!comp_cls) {
7213a328
PP
952 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
953 "comp-cls-name=\"%s\", comp-cls-type=%d",
954 cfg->cmd_data.query.cfg_component->plugin_name->str,
955 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
956 cfg->cmd_data.query.cfg_component->type);
63ce0e1d
PP
957 fprintf(stderr, "%s%sCannot find component class %s",
958 bt_common_color_bold(),
959 bt_common_color_fg_red(),
960 bt_common_color_reset());
961 print_plugin_comp_cls_opt(stderr,
a67681c1 962 cfg->cmd_data.query.cfg_component->plugin_name->str,
db0f160a 963 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
a67681c1 964 cfg->cmd_data.query.cfg_component->type);
63ce0e1d
PP
965 fprintf(stderr, "\n");
966 ret = -1;
967 goto end;
968 }
969
c7eee084 970 ret = query(comp_cls, cfg->cmd_data.query.object->str,
05e21286 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");
05e21286 1164 print_value(stdout, 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;
05e21286
PP
1223 const struct bt_value *results = NULL;
1224 struct bt_value *params = NULL;
1225 const struct bt_value *map = NULL;
1226 const struct bt_value *v = NULL;
96e8c7e1
MD
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
05e21286 1253 params = bt_value_map_create();
96e8c7e1
MD
1254 if (!params) {
1255 goto error;
1256 }
1257
05e21286 1258 ret = bt_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
05e21286
PP
1264 ret = query(comp_cls, "sessions", params,
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
05e21286 1299 map = bt_value_array_borrow_element_by_index_const(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
05e21286 1309 v = bt_value_map_borrow_entry_value_const(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);
05e21286 1316 v = bt_value_map_borrow_entry_value_const(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);
05e21286 1323 v = bt_value_map_borrow_entry_value_const(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);
05e21286 1330 v = bt_value_map_borrow_entry_value_const(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;
05e21286
PP
1375 const struct bt_value *results = NULL;
1376 struct bt_value *params = NULL;
1377 const struct bt_value *metadata_text_value = NULL;
05a67631 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
05e21286 1405 params = bt_value_map_create();
05a67631
PP
1406 if (!params) {
1407 ret = -1;
1408 goto end;
1409 }
1410
05e21286 1411 ret = bt_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",
05e21286 1419 params, &results, &fail_reason);
c7eee084
PP
1420 if (ret) {
1421 goto failed;
05a67631
PP
1422 }
1423
05e21286
PP
1424 metadata_text_value = bt_value_map_borrow_entry_value_const(results,
1425 "text");
05a67631 1426 if (!metadata_text_value) {
7213a328 1427 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
05a67631
PP
1428 ret = -1;
1429 goto end;
1430 }
1431
601b0d3c 1432 metadata_text = bt_value_string_get(metadata_text_value);
c327e427
PP
1433
1434 if (cfg->cmd_data.print_ctf_metadata.output_path->len > 0) {
1435 out_stream =
1436 fopen(cfg->cmd_data.print_ctf_metadata.output_path->str,
1437 "w");
1438 if (!out_stream) {
1439 ret = -1;
1440 BT_LOGE_ERRNO("Cannot open file for writing",
1441 ": path=\"%s\"",
1442 cfg->cmd_data.print_ctf_metadata.output_path->str);
1443 goto end;
1444 }
1445 }
1446
1447 ret = fprintf(out_stream, "%s\n", metadata_text);
1448 if (ret < 0) {
1449 BT_LOGE("Cannot write whole metadata text to output stream: "
1450 "ret=%d", ret);
1451 }
1452
c7eee084
PP
1453 goto end;
1454
1455failed:
1456 ret = -1;
1457 BT_LOGE("Failed to query for metadata info: %s", fail_reason);
1458 fprintf(stderr, "%s%sFailed to request metadata info: %s%s\n",
1459 bt_common_color_bold(),
1460 bt_common_color_fg_red(),
1461 fail_reason,
1462 bt_common_color_reset());
05a67631
PP
1463
1464end:
65300d60
PP
1465 bt_object_put_ref(results);
1466 bt_object_put_ref(params);
1467 bt_object_put_ref(comp_cls);
c327e427
PP
1468
1469 if (out_stream && out_stream != stdout) {
1470 int fclose_ret = fclose(out_stream);
1471
1472 if (fclose_ret) {
1473 BT_LOGE_ERRNO("Cannot close file stream",
1474 ": path=\"%s\"",
1475 cfg->cmd_data.print_ctf_metadata.output_path->str);
1476 }
1477 }
1478
05a67631
PP
1479 return 0;
1480}
1481
75a2cb9b
JG
1482struct port_id {
1483 char *instance_name;
1484 char *port_name;
1485};
1486
1487struct trace_range {
1488 uint64_t intersection_range_begin_ns;
1489 uint64_t intersection_range_end_ns;
1490};
1491
1492static
1493guint port_id_hash(gconstpointer v)
1494{
1495 const struct port_id *id = v;
1496
f6ccaed9
PP
1497 BT_ASSERT(id->instance_name);
1498 BT_ASSERT(id->port_name);
75a2cb9b
JG
1499
1500 return g_str_hash(id->instance_name) ^ g_str_hash(id->port_name);
1501}
1502
1503static
1504gboolean port_id_equal(gconstpointer v1, gconstpointer v2)
1505{
1506 const struct port_id *id1 = v1;
1507 const struct port_id *id2 = v2;
1508
1509 return !strcmp(id1->instance_name, id2->instance_name) &&
1510 !strcmp(id1->port_name, id2->port_name);
1511}
1512
1513static
1514void port_id_destroy(gpointer data)
1515{
1516 struct port_id *id = data;
1517
1518 free(id->instance_name);
1519 free(id->port_name);
1520 free(id);
1521}
1522
1523static
1524void trace_range_destroy(gpointer data)
1525{
1526 free(data);
1527}
1528
9009cc24
PP
1529struct cmd_run_ctx {
1530 /* Owned by this */
d94d92ac
PP
1531 GHashTable *src_components;
1532
1533 /* Owned by this */
1534 GHashTable *flt_components;
1535
1536 /* Owned by this */
1537 GHashTable *sink_components;
9009cc24
PP
1538
1539 /* Owned by this */
a2d06fd5 1540 struct bt_private_graph *graph;
9009cc24
PP
1541
1542 /* Weak */
1543 struct bt_config *cfg;
1544
1545 bool connect_ports;
75a2cb9b
JG
1546
1547 bool stream_intersection_mode;
1548
1549 /*
1550 * Association of struct port_id -> struct trace_range.
1551 */
1552 GHashTable *intersections;
9009cc24
PP
1553};
1554
75a2cb9b
JG
1555/* Returns a timestamp of the form "(-)s.ns" */
1556static
1557char *s_from_ns(int64_t ns)
1558{
1559 int ret;
1560 char *s_ret = NULL;
1561 bool is_negative;
1562 int64_t ts_sec_abs, ts_nsec_abs;
1563 int64_t ts_sec = ns / NSEC_PER_SEC;
1564 int64_t ts_nsec = ns % NSEC_PER_SEC;
1565
1566 if (ts_sec >= 0 && ts_nsec >= 0) {
1567 is_negative = false;
1568 ts_sec_abs = ts_sec;
1569 ts_nsec_abs = ts_nsec;
1570 } else if (ts_sec > 0 && ts_nsec < 0) {
1571 is_negative = false;
1572 ts_sec_abs = ts_sec - 1;
1573 ts_nsec_abs = NSEC_PER_SEC + ts_nsec;
1574 } else if (ts_sec == 0 && ts_nsec < 0) {
1575 is_negative = true;
1576 ts_sec_abs = ts_sec;
1577 ts_nsec_abs = -ts_nsec;
1578 } else if (ts_sec < 0 && ts_nsec > 0) {
1579 is_negative = true;
1580 ts_sec_abs = -(ts_sec + 1);
1581 ts_nsec_abs = NSEC_PER_SEC - ts_nsec;
1582 } else if (ts_sec < 0 && ts_nsec == 0) {
1583 is_negative = true;
1584 ts_sec_abs = -ts_sec;
1585 ts_nsec_abs = ts_nsec;
1586 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1587 is_negative = true;
1588 ts_sec_abs = -ts_sec;
1589 ts_nsec_abs = -ts_nsec;
1590 }
1591
1592 ret = asprintf(&s_ret, "%s%" PRId64 ".%09" PRId64,
1593 is_negative ? "-" : "", ts_sec_abs, ts_nsec_abs);
1594 if (ret < 0) {
1595 s_ret = NULL;
1596 }
1597 return s_ret;
1598}
1599
9009cc24
PP
1600static
1601int cmd_run_ctx_connect_upstream_port_to_downstream_component(
d94d92ac
PP
1602 struct cmd_run_ctx *ctx,
1603 struct bt_component *upstream_comp,
1604 struct bt_port_output *out_upstream_port,
9009cc24 1605 struct bt_config_connection *cfg_conn)
290725f7 1606{
d94d92ac
PP
1607 typedef uint64_t (*input_port_count_func_t)(void *);
1608 typedef struct bt_port_input *(*borrow_input_port_by_index_func_t)(
1609 void *, uint64_t);
1610 struct bt_port *upstream_port =
707b7d35 1611 bt_port_output_as_port(out_upstream_port);
d94d92ac 1612
290725f7 1613 int ret = 0;
9009cc24 1614 GQuark downstreamp_comp_name_quark;
d94d92ac
PP
1615 void *downstream_comp;
1616 uint64_t downstream_port_count;
9009cc24 1617 uint64_t i;
d94d92ac
PP
1618 input_port_count_func_t port_count_fn;
1619 borrow_input_port_by_index_func_t port_by_index_fn;
a256a42d 1620 enum bt_graph_status status = BT_GRAPH_STATUS_ERROR;
75a2cb9b 1621 bool insert_trimmer = false;
05e21286 1622 struct bt_value *trimmer_params = NULL;
75a2cb9b
JG
1623 char *intersection_begin = NULL;
1624 char *intersection_end = NULL;
d94d92ac
PP
1625 struct bt_component_filter *trimmer = NULL;
1626 struct bt_component_class_filter *trimmer_class = NULL;
1627 struct bt_port_input *trimmer_input = NULL;
1628 struct bt_port_output *trimmer_output = NULL;
75a2cb9b
JG
1629
1630 if (ctx->intersections &&
1631 bt_component_get_class_type(upstream_comp) ==
1632 BT_COMPONENT_CLASS_TYPE_SOURCE) {
1633 struct trace_range *range;
1634 struct port_id port_id = {
1635 .instance_name = (char *) bt_component_get_name(upstream_comp),
1636 .port_name = (char *) bt_port_get_name(upstream_port)
1637 };
1638
1639 if (!port_id.instance_name || !port_id.port_name) {
1640 goto error;
1641 }
1642
1643 range = (struct trace_range *) g_hash_table_lookup(
1644 ctx->intersections, &port_id);
1645 if (range) {
1646 enum bt_value_status status;
1647
1648 intersection_begin = s_from_ns(
1649 range->intersection_range_begin_ns);
1650 intersection_end = s_from_ns(
1651 range->intersection_range_end_ns);
1652 if (!intersection_begin || !intersection_end) {
1653 BT_LOGE_STR("Cannot create trimmer argument timestamp string.");
1654 goto error;
1655 }
1656
1657 insert_trimmer = true;
05e21286 1658 trimmer_params = bt_value_map_create();
75a2cb9b
JG
1659 if (!trimmer_params) {
1660 goto error;
1661 }
1662
05e21286 1663 status = bt_value_map_insert_string_entry(
07208d85 1664 trimmer_params, "begin", intersection_begin);
75a2cb9b
JG
1665 if (status != BT_VALUE_STATUS_OK) {
1666 goto error;
1667 }
05e21286 1668 status = bt_value_map_insert_string_entry(
07208d85 1669 trimmer_params,
75a2cb9b
JG
1670 "end", intersection_end);
1671 if (status != BT_VALUE_STATUS_OK) {
1672 goto error;
1673 }
1674 }
1675
d94d92ac 1676 trimmer_class = find_filter_component_class("utils", "trimmer");
75a2cb9b
JG
1677 if (!trimmer_class) {
1678 goto error;
1679 }
1680 }
9009cc24 1681
7213a328
PP
1682 BT_LOGI("Connecting upstream port to the next available downstream port: "
1683 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1684 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1685 upstream_port, bt_port_get_name(upstream_port),
1686 cfg_conn->downstream_comp_name->str,
1687 cfg_conn->arg->str);
9009cc24
PP
1688 downstreamp_comp_name_quark = g_quark_from_string(
1689 cfg_conn->downstream_comp_name->str);
f6ccaed9 1690 BT_ASSERT(downstreamp_comp_name_quark > 0);
d94d92ac 1691 downstream_comp = g_hash_table_lookup(ctx->flt_components,
71c7c95f 1692 GUINT_TO_POINTER(downstreamp_comp_name_quark));
d94d92ac
PP
1693 port_count_fn = (input_port_count_func_t)
1694 bt_component_filter_get_input_port_count;
1695 port_by_index_fn = (borrow_input_port_by_index_func_t)
1696 bt_component_filter_borrow_input_port_by_index;
1697
1698 if (!downstream_comp) {
1699 downstream_comp = g_hash_table_lookup(ctx->sink_components,
1700 GUINT_TO_POINTER(downstreamp_comp_name_quark));
1701 port_count_fn = (input_port_count_func_t)
1702 bt_component_sink_get_input_port_count;
1703 port_by_index_fn = (borrow_input_port_by_index_func_t)
1704 bt_component_sink_borrow_input_port_by_index;
1705 }
1706
9009cc24 1707 if (!downstream_comp) {
7213a328
PP
1708 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1709 "conn-arg=\"%s\"", cfg_conn->downstream_comp_name->str,
1710 cfg_conn->arg->str);
9009cc24
PP
1711 fprintf(stderr, "Cannot create connection: cannot find downstream component: %s\n",
1712 cfg_conn->arg->str);
1713 goto error;
1714 }
1715
9009cc24 1716 downstream_port_count = port_count_fn(downstream_comp);
f6ccaed9 1717 BT_ASSERT(downstream_port_count >= 0);
9009cc24
PP
1718
1719 for (i = 0; i < downstream_port_count; i++) {
d94d92ac 1720 struct bt_port_input *in_downstream_port =
9009cc24 1721 port_by_index_fn(downstream_comp, i);
d94d92ac 1722 struct bt_port *downstream_port =
707b7d35 1723 bt_port_input_as_port(in_downstream_port);
75a2cb9b 1724 const char *upstream_port_name;
9009cc24
PP
1725 const char *downstream_port_name;
1726
f6ccaed9 1727 BT_ASSERT(downstream_port);
9009cc24 1728
75a2cb9b 1729 /* Skip port if it's already connected. */
9009cc24 1730 if (bt_port_is_connected(downstream_port)) {
7213a328
PP
1731 BT_LOGD("Skipping downstream port: already connected: "
1732 "port-addr=%p, port-name=\"%s\"",
1733 downstream_port,
1734 bt_port_get_name(downstream_port));
9009cc24
PP
1735 continue;
1736 }
1737
1738 downstream_port_name = bt_port_get_name(downstream_port);
f6ccaed9 1739 BT_ASSERT(downstream_port_name);
75a2cb9b 1740 upstream_port_name = bt_port_get_name(upstream_port);
f6ccaed9 1741 BT_ASSERT(upstream_port_name);
9009cc24 1742
75a2cb9b 1743 if (!bt_common_star_glob_match(
1974687e
MJ
1744 cfg_conn->downstream_port_glob->str, SIZE_MAX,
1745 downstream_port_name, SIZE_MAX)) {
75a2cb9b
JG
1746 continue;
1747 }
1748
1749 if (insert_trimmer) {
1750 /*
d94d92ac
PP
1751 * In order to insert the trimmer between the
1752 * two components that were being connected, we
1753 * create a connection configuration entry which
1754 * describes a connection from the trimmer's
1755 * output to the original input that was being
1756 * connected.
75a2cb9b 1757 *
d94d92ac
PP
1758 * Hence, the creation of the trimmer will cause
1759 * the graph "new port" listener to establish
1760 * all downstream connections as its output port
1761 * is connected. We will then establish the
1762 * connection between the original upstream
1763 * source and the trimmer.
75a2cb9b
JG
1764 */
1765 char *trimmer_name = NULL;
1766 enum bt_graph_status graph_status;
1767
d94d92ac
PP
1768 ret = asprintf(&trimmer_name,
1769 "stream-intersection-trimmer-%s",
75a2cb9b
JG
1770 upstream_port_name);
1771 if (ret < 0) {
1772 goto error;
1773 }
1774 ret = 0;
1775
1776 ctx->connect_ports = false;
d94d92ac
PP
1777 graph_status = bt_private_graph_add_filter_component(
1778 ctx->graph, trimmer_class, trimmer_name,
05e21286 1779 trimmer_params,
75a2cb9b
JG
1780 &trimmer);
1781 free(trimmer_name);
1782 if (graph_status != BT_GRAPH_STATUS_OK) {
1783 goto error;
1784 }
f6ccaed9 1785 BT_ASSERT(trimmer);
75a2cb9b
JG
1786
1787 trimmer_input =
d94d92ac 1788 bt_component_filter_borrow_input_port_by_index(
75a2cb9b
JG
1789 trimmer, 0);
1790 if (!trimmer_input) {
1791 goto error;
1792 }
1793 trimmer_output =
d94d92ac 1794 bt_component_filter_borrow_output_port_by_index(
75a2cb9b
JG
1795 trimmer, 0);
1796 if (!trimmer_output) {
9009cc24
PP
1797 goto error;
1798 }
1799
75a2cb9b
JG
1800 /*
1801 * Replace the current downstream port by the trimmer's
1802 * upstream port.
1803 */
d94d92ac
PP
1804 in_downstream_port = trimmer_input;
1805 downstream_port =
707b7d35 1806 bt_port_input_as_port(in_downstream_port);
75a2cb9b
JG
1807 downstream_port_name = bt_port_get_name(
1808 downstream_port);
d94d92ac 1809 BT_ASSERT(downstream_port_name);
75a2cb9b
JG
1810 }
1811
1812 /* We have a winner! */
a2d06fd5 1813 status = bt_private_graph_connect_ports(ctx->graph,
d94d92ac
PP
1814 out_upstream_port, in_downstream_port, NULL);
1815 downstream_port = NULL;
75a2cb9b
JG
1816 switch (status) {
1817 case BT_GRAPH_STATUS_OK:
1818 break;
1819 case BT_GRAPH_STATUS_CANCELED:
1820 BT_LOGI_STR("Graph was canceled by user.");
1821 status = BT_GRAPH_STATUS_OK;
1822 break;
1823 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION:
1824 BT_LOGE("A component refused a connection to one of its ports: "
7213a328
PP
1825 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1826 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1827 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1828 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1829 "conn-arg=\"%s\"",
1830 upstream_comp, bt_component_get_name(upstream_comp),
1831 upstream_port, bt_port_get_name(upstream_port),
1832 downstream_comp, cfg_conn->downstream_comp_name->str,
1833 downstream_port, downstream_port_name,
1834 cfg_conn->arg->str);
75a2cb9b
JG
1835 fprintf(stderr,
1836 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1837 bt_port_get_name(upstream_port),
1838 downstream_port_name,
1839 cfg_conn->arg->str);
1840 break;
1841 default:
1842 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1843 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1844 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1845 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1846 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1847 "conn-arg=\"%s\"",
1848 upstream_comp, bt_component_get_name(upstream_comp),
1849 upstream_port, bt_port_get_name(upstream_port),
1850 downstream_comp, cfg_conn->downstream_comp_name->str,
1851 downstream_port, downstream_port_name,
1852 cfg_conn->arg->str);
1853 fprintf(stderr,
1854 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1855 bt_port_get_name(upstream_port),
1856 downstream_port_name,
1857 cfg_conn->arg->str);
1858 goto error;
9009cc24
PP
1859 }
1860
75a2cb9b
JG
1861 BT_LOGI("Connected component ports: "
1862 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1863 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1864 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1865 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1866 "conn-arg=\"%s\"",
1867 upstream_comp, bt_component_get_name(upstream_comp),
1868 upstream_port, bt_port_get_name(upstream_port),
1869 downstream_comp, cfg_conn->downstream_comp_name->str,
1870 downstream_port, downstream_port_name,
1871 cfg_conn->arg->str);
1872
1873 if (insert_trimmer) {
1874 /*
1875 * The first connection, from the source to the trimmer,
1876 * has been done. We now connect the trimmer to the
1877 * original downstream port.
1878 */
1879 ret = cmd_run_ctx_connect_upstream_port_to_downstream_component(
d94d92ac 1880 ctx,
707b7d35 1881 bt_component_filter_as_component(trimmer),
d94d92ac 1882 trimmer_output, cfg_conn);
75a2cb9b
JG
1883 if (ret) {
1884 goto error;
1885 }
1886 ctx->connect_ports = true;
1887 }
9009cc24 1888
53bc54cd
PP
1889 /*
1890 * We found a matching downstream port: the search is
1891 * over.
1892 */
1893 goto end;
05a67631
PP
1894 }
1895
53bc54cd
PP
1896 /* No downstream port found */
1897 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1898 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1899 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1900 upstream_port, bt_port_get_name(upstream_port),
1901 cfg_conn->downstream_comp_name->str,
1902 cfg_conn->arg->str);
1903 fprintf(stderr,
1904 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1905 bt_port_get_name(upstream_port), cfg_conn->arg->str);
9009cc24
PP
1906
1907error:
1908 ret = -1;
1909
1910end:
75a2cb9b
JG
1911 free(intersection_begin);
1912 free(intersection_end);
65300d60
PP
1913 BT_OBJECT_PUT_REF_AND_RESET(trimmer_params);
1914 BT_OBJECT_PUT_REF_AND_RESET(trimmer_class);
1915 BT_OBJECT_PUT_REF_AND_RESET(trimmer);
9009cc24
PP
1916 return ret;
1917}
1918
1919static
1920int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx *ctx,
d94d92ac 1921 struct bt_port_output *upstream_port)
9009cc24
PP
1922{
1923 int ret = 0;
1924 const char *upstream_port_name;
1925 const char *upstream_comp_name;
1926 struct bt_component *upstream_comp = NULL;
1927 size_t i;
1928
f6ccaed9
PP
1929 BT_ASSERT(ctx);
1930 BT_ASSERT(upstream_port);
d94d92ac 1931 upstream_port_name = bt_port_get_name(
707b7d35 1932 bt_port_output_as_port(upstream_port));
f6ccaed9 1933 BT_ASSERT(upstream_port_name);
d94d92ac 1934 upstream_comp = bt_port_borrow_component(
707b7d35 1935 bt_port_output_as_port(upstream_port));
9009cc24 1936 if (!upstream_comp) {
7213a328
PP
1937 BT_LOGW("Upstream port to connect is not part of a component: "
1938 "port-addr=%p, port-name=\"%s\"",
1939 upstream_port, upstream_port_name);
98ecef32
MD
1940 ret = -1;
1941 goto end;
33bceaf8
JG
1942 }
1943
9009cc24 1944 upstream_comp_name = bt_component_get_name(upstream_comp);
f6ccaed9 1945 BT_ASSERT(upstream_comp_name);
7213a328
PP
1946 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1947 "port-addr=%p, port-name=\"%s\"",
1948 upstream_comp, upstream_comp_name,
1949 upstream_port, upstream_port_name);
9009cc24
PP
1950
1951 for (i = 0; i < ctx->cfg->cmd_data.run.connections->len; i++) {
1952 struct bt_config_connection *cfg_conn =
1953 g_ptr_array_index(
1954 ctx->cfg->cmd_data.run.connections, i);
1955
1956 if (strcmp(cfg_conn->upstream_comp_name->str,
75a2cb9b
JG
1957 upstream_comp_name)) {
1958 continue;
1959 }
1960
1961 if (!bt_common_star_glob_match(
1962 cfg_conn->upstream_port_glob->str,
1974687e 1963 SIZE_MAX, upstream_port_name, SIZE_MAX)) {
75a2cb9b
JG
1964 continue;
1965 }
1966
1967 ret = cmd_run_ctx_connect_upstream_port_to_downstream_component(
1968 ctx, upstream_comp, upstream_port, cfg_conn);
1969 if (ret) {
1970 BT_LOGE("Cannot connect upstream port: "
1971 "port-addr=%p, port-name=\"%s\"",
1972 upstream_port,
1973 upstream_port_name);
1974 fprintf(stderr,
1975 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1976 upstream_port_name,
1977 upstream_comp_name,
1978 cfg_conn->arg->str);
1979 goto error;
9009cc24 1980 }
75a2cb9b 1981 goto end;
9009cc24
PP
1982 }
1983
7213a328
PP
1984 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1985 "port-addr=%p, port-name=\"%s\"", upstream_port,
1986 upstream_port_name);
9009cc24
PP
1987 fprintf(stderr,
1988 "Cannot create connection: upstream port `%s` does not match any connection\n",
7213a328 1989 upstream_port_name);
9009cc24
PP
1990
1991error:
1992 ret = -1;
1993
1994end:
9009cc24
PP
1995 return ret;
1996}
1997
1998static
d94d92ac
PP
1999void graph_output_port_added_listener(struct cmd_run_ctx *ctx,
2000 struct bt_port_output *out_port)
9009cc24 2001{
d94d92ac 2002 struct bt_component *comp;
707b7d35 2003 struct bt_port *port = bt_port_output_as_port(out_port);
9009cc24 2004
d94d92ac 2005 comp = bt_port_borrow_component(port);
e12720c0
PP
2006 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
2007 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
2008 comp, comp ? bt_component_get_name(comp) : "",
7213a328 2009 port, bt_port_get_name(port));
36712f1d
PP
2010
2011 if (!ctx->connect_ports) {
2012 goto end;
2013 }
2014
e12720c0
PP
2015 if (!comp) {
2016 BT_LOGW_STR("Port has no component.");
56a1cced
JG
2017 goto end;
2018 }
7c7c0433 2019
e12720c0
PP
2020 if (bt_port_is_connected(port)) {
2021 BT_LOGW_STR("Port is already connected.");
7c7c0433
JG
2022 goto end;
2023 }
2024
d94d92ac 2025 if (cmd_run_ctx_connect_upstream_port(ctx, out_port)) {
7213a328 2026 BT_LOGF_STR("Cannot connect upstream port.");
9009cc24
PP
2027 fprintf(stderr, "Added port could not be connected: aborting\n");
2028 abort();
2029 }
2030
2031end:
9009cc24
PP
2032 return;
2033}
2034
2035static
d94d92ac
PP
2036void graph_source_output_port_added_listener(
2037 struct bt_component_source *component,
2038 struct bt_port_output *port, void *data)
9009cc24 2039{
d94d92ac 2040 graph_output_port_added_listener(data, port);
9009cc24
PP
2041}
2042
2043static
d94d92ac
PP
2044void graph_filter_output_port_added_listener(
2045 struct bt_component_filter *component,
2046 struct bt_port_output *port, void *data)
9009cc24 2047{
d94d92ac 2048 graph_output_port_added_listener(data, port);
9009cc24
PP
2049}
2050
2051static
2052void cmd_run_ctx_destroy(struct cmd_run_ctx *ctx)
2053{
2054 if (!ctx) {
2055 return;
2056 }
2057
d94d92ac
PP
2058 if (ctx->src_components) {
2059 g_hash_table_destroy(ctx->src_components);
2060 ctx->src_components = NULL;
2061 }
2062
2063 if (ctx->flt_components) {
2064 g_hash_table_destroy(ctx->flt_components);
2065 ctx->flt_components = NULL;
2066 }
2067
2068 if (ctx->sink_components) {
2069 g_hash_table_destroy(ctx->sink_components);
2070 ctx->sink_components = NULL;
9009cc24
PP
2071 }
2072
75a2cb9b
JG
2073 if (ctx->intersections) {
2074 g_hash_table_destroy(ctx->intersections);
a5302548 2075 ctx->intersections = NULL;
75a2cb9b
JG
2076 }
2077
65300d60 2078 BT_OBJECT_PUT_REF_AND_RESET(ctx->graph);
5401f780 2079 the_graph = NULL;
9009cc24
PP
2080 ctx->cfg = NULL;
2081}
2082
2083static
2084int cmd_run_ctx_init(struct cmd_run_ctx *ctx, struct bt_config *cfg)
2085{
2086 int ret = 0;
d94d92ac 2087 enum bt_graph_status status;
9009cc24
PP
2088
2089 ctx->cfg = cfg;
2090 ctx->connect_ports = false;
d94d92ac 2091 ctx->src_components = g_hash_table_new_full(g_direct_hash,
398454ed 2092 g_direct_equal, NULL, (GDestroyNotify) bt_object_put_ref);
d94d92ac
PP
2093 if (!ctx->src_components) {
2094 goto error;
2095 }
2096
2097 ctx->flt_components = g_hash_table_new_full(g_direct_hash,
398454ed 2098 g_direct_equal, NULL, (GDestroyNotify) bt_object_put_ref);
d94d92ac
PP
2099 if (!ctx->flt_components) {
2100 goto error;
2101 }
2102
2103 ctx->sink_components = g_hash_table_new_full(g_direct_hash,
398454ed 2104 g_direct_equal, NULL, (GDestroyNotify) bt_object_put_ref);
d94d92ac 2105 if (!ctx->sink_components) {
9009cc24
PP
2106 goto error;
2107 }
2108
75a2cb9b
JG
2109 if (cfg->cmd_data.run.stream_intersection_mode) {
2110 ctx->stream_intersection_mode = true;
2111 ctx->intersections = g_hash_table_new_full(port_id_hash,
2112 port_id_equal, port_id_destroy, trace_range_destroy);
2113 if (!ctx->intersections) {
2114 goto error;
2115 }
2116 }
2117
a2d06fd5 2118 ctx->graph = bt_private_graph_create();
9009cc24
PP
2119 if (!ctx->graph) {
2120 goto error;
2121 }
2122
5401f780 2123 the_graph = ctx->graph;
d94d92ac
PP
2124 status = bt_private_graph_add_source_component_output_port_added_listener(
2125 ctx->graph, graph_source_output_port_added_listener, NULL, ctx,
2126 NULL);
2127 if (status != BT_GRAPH_STATUS_OK) {
0d107cdd 2128 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
9009cc24
PP
2129 goto error;
2130 }
2131
d94d92ac
PP
2132 status = bt_private_graph_add_filter_component_output_port_added_listener(
2133 ctx->graph, graph_filter_output_port_added_listener, NULL, ctx,
2134 NULL);
2135 if (status != BT_GRAPH_STATUS_OK) {
2136 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
9009cc24
PP
2137 goto error;
2138 }
2139
2140 goto end;
2141
2142error:
2143 cmd_run_ctx_destroy(ctx);
2144 ret = -1;
2145
2146end:
2147 return ret;
2148}
2149
75a2cb9b
JG
2150static
2151int set_stream_intersections(struct cmd_run_ctx *ctx,
2152 struct bt_config_component *cfg_comp,
d94d92ac 2153 struct bt_component_class_source *src_comp_cls)
75a2cb9b
JG
2154{
2155 int ret = 0;
2156 uint64_t trace_idx;
2157 int64_t trace_count;
2158 enum bt_value_status value_status;
2159 const char *path = NULL;
05e21286
PP
2160 const struct bt_value *component_path_value = NULL;
2161 struct bt_value *query_params = NULL;
2162 const struct bt_value *query_result = NULL;
2163 const struct bt_value *trace_info = NULL;
2164 const struct bt_value *intersection_range = NULL;
2165 const struct bt_value *intersection_begin = NULL;
2166 const struct bt_value *intersection_end = NULL;
2167 const struct bt_value *stream_path_value = NULL;
2168 const struct bt_value *stream_paths = NULL;
2169 const struct bt_value *stream_infos = NULL;
2170 const struct bt_value *stream_info = NULL;
75a2cb9b
JG
2171 struct port_id *port_id = NULL;
2172 struct trace_range *trace_range = NULL;
c7eee084 2173 const char *fail_reason = NULL;
d94d92ac 2174 struct bt_component_class *comp_cls =
707b7d35 2175 bt_component_class_source_as_component_class(src_comp_cls);
75a2cb9b 2176
05e21286
PP
2177 component_path_value = bt_value_map_borrow_entry_value(cfg_comp->params,
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);
05e21286 2187 query_params = bt_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
05e21286
PP
2194 value_status = bt_value_map_insert_string_entry(query_params, "path",
2195 path);
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",
05e21286 2203 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
05e21286 2234 trace_info = bt_value_array_borrow_element_by_index_const(
07208d85 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
05e21286
PP
2242 intersection_range = bt_value_map_borrow_entry_value_const(
2243 trace_info, "intersection-range-ns");
75a2cb9b
JG
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
05e21286
PP
2250 intersection_begin = bt_value_map_borrow_entry_value_const(intersection_range,
2251 "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
05e21286
PP
2258 intersection_end = bt_value_map_borrow_entry_value_const(intersection_range,
2259 "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
05e21286
PP
2278 stream_infos = bt_value_map_borrow_entry_value_const(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
05e21286 2329 stream_info = bt_value_array_borrow_element_by_index_const(
07208d85 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
05e21286
PP
2337 stream_paths = bt_value_map_borrow_entry_value_const(stream_info,
2338 "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 2345 stream_path_value =
05e21286 2346 bt_value_array_borrow_element_by_index_const(
07208d85 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,
05e21286 2444 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,
05e21286 2450 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,
05e21286 2456 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) {
05e21286 2958 ret = load_all_plugins(cfg->plugin_paths);
db0f160a 2959 if (ret) {
7213a328 2960 BT_LOGE("Failed to load plugins: ret=%d", ret);
db0f160a
PP
2961 retcode = 1;
2962 goto end;
2963 }
2964 }
2965
7213a328
PP
2966 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2967 cfg->command, cfg->command_name);
2968
290725f7 2969 switch (cfg->command) {
db0f160a
PP
2970 case BT_CONFIG_COMMAND_RUN:
2971 ret = cmd_run(cfg);
290725f7
PP
2972 break;
2973 case BT_CONFIG_COMMAND_LIST_PLUGINS:
2974 ret = cmd_list_plugins(cfg);
2975 break;
22e22462
PP
2976 case BT_CONFIG_COMMAND_HELP:
2977 ret = cmd_help(cfg);
2978 break;
a67681c1
PP
2979 case BT_CONFIG_COMMAND_QUERY:
2980 ret = cmd_query(cfg);
63ce0e1d 2981 break;
db0f160a
PP
2982 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA:
2983 ret = cmd_print_ctf_metadata(cfg);
2984 break;
2985 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS:
2986 ret = cmd_print_lttng_live_sessions(cfg);
2987 break;
290725f7 2988 default:
0fbb9a9f
PP
2989 BT_LOGF("Invalid/unknown command: cmd=%d", cfg->command);
2990 abort();
290725f7
PP
2991 }
2992
d26ef3e3
PP
2993 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2994 cfg->command, cfg->command_name, ret);
290725f7
PP
2995 warn_command_name_and_directory_clash(cfg);
2996 retcode = ret ? 1 : 0;
2997
2998end:
65300d60 2999 BT_OBJECT_PUT_REF_AND_RESET(cfg);
9009cc24 3000 fini_static_data();
290725f7 3001 return retcode;
4c8bfb7e 3002}
This page took 0.207387 seconds and 4 git commands to generate.