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