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