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