cli: set all the log level of all known loggers with -v and -d
[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>
7c7c0433 33#include <babeltrace/plugin/plugin.h>
290725f7 34#include <babeltrace/common-internal.h>
b2e0c907
PP
35#include <babeltrace/graph/component.h>
36#include <babeltrace/graph/component-source.h>
37#include <babeltrace/graph/component-sink.h>
38#include <babeltrace/graph/component-filter.h>
39#include <babeltrace/graph/component-class.h>
40#include <babeltrace/graph/port.h>
41#include <babeltrace/graph/graph.h>
42#include <babeltrace/graph/connection.h>
43#include <babeltrace/graph/notification-iterator.h>
2e339de1
JG
44#include <babeltrace/ref.h>
45#include <babeltrace/values.h>
7213a328 46#include <babeltrace/logging.h>
a8ff38ef 47#include <unistd.h>
34ac0e6c 48#include <stdlib.h>
7f26a816
PP
49#include <popt.h>
50#include <string.h>
51#include <stdio.h>
33b34c43 52#include <glib.h>
dc3fffef 53#include <inttypes.h>
7cdc2bab 54#include <unistd.h>
5401f780 55#include <signal.h>
c42c79ea 56#include "babeltrace-cfg.h"
9009cc24
PP
57#include "babeltrace-cfg-cli-args.h"
58#include "babeltrace-cfg-cli-args-default.h"
59
60#define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH"
34ac0e6c 61
fc11b6a6
PP
62/*
63 * Known environment variable names for the log levels of the project's
64 * modules.
65 */
66static const char* log_level_env_var_names[] = {
67 "BABELTRACE_PLUGIN_CTF_BTR_LOG_LEVEL",
68 "BABELTRACE_PLUGIN_CTF_FS_SRC_LOG_LEVEL",
69 "BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_SRC_LOG_LEVEL",
70 "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL",
71 "BABELTRACE_PLUGIN_CTF_NOTIF_ITER_LOG_LEVEL",
72 "BABELTRACE_PYTHON_PLUGIN_PROVIDER_LOG_LEVEL",
73 NULL,
74};
75
5401f780
PP
76/* Application's processing graph (weak) */
77static struct bt_graph *the_graph;
78static bool canceled = false;
79
33b34c43
PP
80GPtrArray *loaded_plugins;
81
5401f780
PP
82static
83void sigint_handler(int signum)
84{
85 if (signum != SIGINT) {
86 return;
87 }
88
89 if (the_graph) {
90 bt_graph_cancel(the_graph);
91 }
92
93 canceled = true;
94}
95
33b34c43 96static
9009cc24 97void init_static_data(void)
33b34c43 98{
9009cc24 99 loaded_plugins = g_ptr_array_new_with_free_func(bt_put);
33b34c43
PP
100}
101
102static
9009cc24 103void fini_static_data(void)
33b34c43
PP
104{
105 g_ptr_array_free(loaded_plugins, TRUE);
106}
107
108static
109struct bt_plugin *find_plugin(const char *name)
110{
111 int i;
112 struct bt_plugin *plugin = NULL;
113
7213a328
PP
114 assert(name);
115 BT_LOGD("Finding plugin: name=\"%s\"", name);
116
33b34c43
PP
117 for (i = 0; i < loaded_plugins->len; i++) {
118 plugin = g_ptr_array_index(loaded_plugins, i);
119
120 if (strcmp(name, bt_plugin_get_name(plugin)) == 0) {
121 break;
122 }
123
124 plugin = NULL;
125 }
126
7213a328
PP
127 if (BT_LOG_ON_DEBUG) {
128 if (plugin) {
129 BT_LOGD("Found plugin: plugin-addr=%p", plugin);
130 } else {
131 BT_LOGD("Cannot find plugin.");
132 }
133 }
134
33b34c43
PP
135 return bt_get(plugin);
136}
137
138static
139struct bt_component_class *find_component_class(const char *plugin_name,
140 const char *comp_class_name,
d3e4dcd8 141 enum bt_component_class_type comp_class_type)
33b34c43
PP
142{
143 struct bt_component_class *comp_class = NULL;
7213a328
PP
144 struct bt_plugin *plugin;
145
146 BT_LOGD("Finding component class: plugin-name=\"%s\", "
147 "comp-cls-name=\"%s\", comp-cls-type=%d",
148 plugin_name, comp_class_name, comp_class_type);
149
150 plugin = find_plugin(plugin_name);
33b34c43
PP
151
152 if (!plugin) {
153 goto end;
154 }
155
156 comp_class = bt_plugin_get_component_class_by_name_and_type(plugin,
157 comp_class_name, comp_class_type);
158 BT_PUT(plugin);
7213a328 159
33b34c43 160end:
7213a328
PP
161 if (BT_LOG_ON_DEBUG) {
162 if (comp_class) {
163 BT_LOGD("Found component class: comp-cls-addr=%p",
164 comp_class);
165 } else {
166 BT_LOGD("Cannot find component class.");
167 }
168 }
169
33b34c43
PP
170 return comp_class;
171}
6c2f3ee5 172
c42c79ea 173static
7213a328 174void print_indent(FILE *fp, size_t indent)
c42c79ea
PP
175{
176 size_t i;
177
178 for (i = 0; i < indent; i++) {
7213a328 179 fprintf(fp, " ");
c42c79ea
PP
180 }
181}
182
87796884
PP
183static
184const char *component_type_str(enum bt_component_class_type type)
185{
186 switch (type) {
187 case BT_COMPONENT_CLASS_TYPE_SOURCE:
188 return "source";
189 case BT_COMPONENT_CLASS_TYPE_SINK:
190 return "sink";
191 case BT_COMPONENT_CLASS_TYPE_FILTER:
192 return "filter";
193 case BT_COMPONENT_CLASS_TYPE_UNKNOWN:
194 default:
fd5f8053 195 return "(unknown)";
87796884
PP
196 }
197}
198
9009cc24
PP
199static
200void print_plugin_comp_cls_opt(FILE *fh, const char *plugin_name,
87796884
PP
201 const char *comp_cls_name, enum bt_component_class_type type)
202{
9009cc24
PP
203 GString *shell_plugin_name = NULL;
204 GString *shell_comp_cls_name = NULL;
87796884 205
9009cc24 206 shell_plugin_name = bt_common_shell_quote(plugin_name, false);
87796884
PP
207 if (!shell_plugin_name) {
208 goto end;
209 }
210
9009cc24 211 shell_comp_cls_name = bt_common_shell_quote(comp_cls_name, false);
87796884
PP
212 if (!shell_comp_cls_name) {
213 goto end;
214 }
215
fd5f8053 216 fprintf(fh, "'%s%s%s%s.%s%s%s.%s%s%s'",
87796884
PP
217 bt_common_color_bold(),
218 bt_common_color_fg_cyan(),
219 component_type_str(type),
87796884 220 bt_common_color_fg_default(),
87796884 221 bt_common_color_fg_blue(),
9009cc24 222 shell_plugin_name->str,
87796884
PP
223 bt_common_color_fg_default(),
224 bt_common_color_fg_yellow(),
9009cc24 225 shell_comp_cls_name->str,
87796884
PP
226 bt_common_color_reset());
227
228end:
9009cc24
PP
229 if (shell_plugin_name) {
230 g_string_free(shell_plugin_name, TRUE);
231 }
232
233 if (shell_comp_cls_name) {
234 g_string_free(shell_comp_cls_name, TRUE);
235 }
87796884
PP
236}
237
c42c79ea 238static
7213a328 239void print_value(FILE *, struct bt_value *, size_t);
c42c79ea 240
c1081aa6 241static
7213a328
PP
242void print_value_rec(FILE *, struct bt_value *, size_t);
243
244struct print_map_value_data {
245 size_t indent;
246 FILE *fp;
247};
c1081aa6 248
c42c79ea 249static
c55a9f58 250bt_bool print_map_value(const char *key, struct bt_value *object, void *data)
c42c79ea 251{
7213a328 252 struct print_map_value_data *print_map_value_data = data;
290725f7 253
7213a328
PP
254 print_indent(print_map_value_data->fp, print_map_value_data->indent);
255 fprintf(print_map_value_data->fp, "%s: ", key);
290725f7
PP
256
257 if (bt_value_is_array(object) &&
258 bt_value_array_is_empty(object)) {
7213a328 259 fprintf(print_map_value_data->fp, "[ ]\n");
290725f7
PP
260 return true;
261 }
262
263 if (bt_value_is_map(object) &&
264 bt_value_map_is_empty(object)) {
7213a328 265 fprintf(print_map_value_data->fp, "{ }\n");
290725f7
PP
266 return true;
267 }
c42c79ea 268
290725f7
PP
269 if (bt_value_is_array(object) ||
270 bt_value_is_map(object)) {
7213a328 271 fprintf(print_map_value_data->fp, "\n");
290725f7 272 }
c42c79ea 273
7213a328
PP
274 print_value_rec(print_map_value_data->fp, object,
275 print_map_value_data->indent + 2);
c55a9f58 276 return BT_TRUE;
c42c79ea
PP
277}
278
279static
7213a328 280void print_value_rec(FILE *fp, struct bt_value *value, size_t indent)
c42c79ea 281{
c55a9f58 282 bt_bool bool_val;
c42c79ea
PP
283 int64_t int_val;
284 double dbl_val;
285 const char *str_val;
286 int size;
287 int i;
288
289 if (!value) {
290 return;
291 }
292
c42c79ea
PP
293 switch (bt_value_get_type(value)) {
294 case BT_VALUE_TYPE_NULL:
7213a328 295 fprintf(fp, "%snull%s\n", bt_common_color_bold(),
c1081aa6 296 bt_common_color_reset());
c42c79ea
PP
297 break;
298 case BT_VALUE_TYPE_BOOL:
299 bt_value_bool_get(value, &bool_val);
7213a328 300 fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(),
c1081aa6
PP
301 bt_common_color_fg_cyan(), bool_val ? "yes" : "no",
302 bt_common_color_reset());
c42c79ea
PP
303 break;
304 case BT_VALUE_TYPE_INTEGER:
305 bt_value_integer_get(value, &int_val);
7213a328 306 fprintf(fp, "%s%s%" PRId64 "%s\n", bt_common_color_bold(),
c1081aa6
PP
307 bt_common_color_fg_red(), int_val,
308 bt_common_color_reset());
c42c79ea
PP
309 break;
310 case BT_VALUE_TYPE_FLOAT:
311 bt_value_float_get(value, &dbl_val);
7213a328 312 fprintf(fp, "%s%s%lf%s\n", bt_common_color_bold(),
c1081aa6
PP
313 bt_common_color_fg_red(), dbl_val,
314 bt_common_color_reset());
c42c79ea
PP
315 break;
316 case BT_VALUE_TYPE_STRING:
317 bt_value_string_get(value, &str_val);
7213a328 318 fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(),
c1081aa6
PP
319 bt_common_color_fg_green(), str_val,
320 bt_common_color_reset());
c42c79ea
PP
321 break;
322 case BT_VALUE_TYPE_ARRAY:
323 size = bt_value_array_size(value);
290725f7
PP
324 assert(size >= 0);
325
326 if (size == 0) {
7213a328
PP
327 print_indent(fp, indent);
328 fprintf(fp, "[ ]\n");
290725f7
PP
329 break;
330 }
c42c79ea
PP
331
332 for (i = 0; i < size; i++) {
333 struct bt_value *element =
334 bt_value_array_get(value, i);
335
290725f7 336 assert(element);
7213a328
PP
337 print_indent(fp, indent);
338 fprintf(fp, "- ");
290725f7
PP
339
340 if (bt_value_is_array(element) &&
341 bt_value_array_is_empty(element)) {
7213a328 342 fprintf(fp, "[ ]\n");
290725f7
PP
343 continue;
344 }
345
346 if (bt_value_is_map(element) &&
347 bt_value_map_is_empty(element)) {
7213a328 348 fprintf(fp, "{ }\n");
290725f7
PP
349 continue;
350 }
351
352 if (bt_value_is_array(element) ||
353 bt_value_is_map(element)) {
7213a328 354 fprintf(fp, "\n");
290725f7
PP
355 }
356
7213a328 357 print_value_rec(fp, element, indent + 2);
c42c79ea
PP
358 BT_PUT(element);
359 }
c42c79ea
PP
360 break;
361 case BT_VALUE_TYPE_MAP:
7213a328
PP
362 {
363 struct print_map_value_data data = {
364 .indent = indent,
365 .fp = fp,
366 };
367
c42c79ea 368 if (bt_value_map_is_empty(value)) {
7213a328
PP
369 print_indent(fp, indent);
370 fprintf(fp, "{ }\n");
290725f7 371 break;
c42c79ea
PP
372 }
373
7213a328 374 bt_value_map_foreach(value, print_map_value, &data);
c42c79ea 375 break;
7213a328 376 }
c42c79ea 377 default:
0fbb9a9f 378 abort();
c42c79ea
PP
379 }
380}
381
c1081aa6 382static
7213a328 383void print_value(FILE *fp, struct bt_value *value, size_t indent)
c1081aa6
PP
384{
385 if (!bt_value_is_array(value) && !bt_value_is_map(value)) {
7213a328 386 print_indent(fp, indent);
c1081aa6
PP
387 }
388
7213a328 389 print_value_rec(fp, value, indent);
c1081aa6
PP
390}
391
c42c79ea
PP
392static
393void print_bt_config_component(struct bt_config_component *bt_config_component)
394{
7213a328
PP
395 fprintf(stderr, " ");
396 print_plugin_comp_cls_opt(stderr, bt_config_component->plugin_name->str,
db0f160a 397 bt_config_component->comp_cls_name->str,
87796884 398 bt_config_component->type);
7213a328 399 fprintf(stderr, ":\n");
3b6cfcc5
PP
400
401 if (bt_config_component->instance_name->len > 0) {
7213a328 402 fprintf(stderr, " Name: %s\n",
3b6cfcc5
PP
403 bt_config_component->instance_name->str);
404 }
405
7213a328
PP
406 fprintf(stderr, " Parameters:\n");
407 print_value(stderr, bt_config_component->params, 8);
c42c79ea
PP
408}
409
410static
411void print_bt_config_components(GPtrArray *array)
412{
413 size_t i;
414
415 for (i = 0; i < array->len; i++) {
416 struct bt_config_component *cfg_component =
e5bc7f81 417 bt_config_get_component(array, i);
c42c79ea
PP
418 print_bt_config_component(cfg_component);
419 BT_PUT(cfg_component);
420 }
421}
422
290725f7
PP
423static
424void print_plugin_paths(struct bt_value *plugin_paths)
425{
7213a328
PP
426 fprintf(stderr, " Plugin paths:\n");
427 print_value(stderr, plugin_paths, 4);
290725f7
PP
428}
429
430static
db0f160a 431void print_cfg_run(struct bt_config *cfg)
290725f7 432{
ebba3338
PP
433 size_t i;
434
db0f160a 435 print_plugin_paths(cfg->plugin_paths);
7213a328 436 fprintf(stderr, " Source component instances:\n");
db0f160a 437 print_bt_config_components(cfg->cmd_data.run.sources);
ebba3338 438
db0f160a 439 if (cfg->cmd_data.run.filters->len > 0) {
7213a328 440 fprintf(stderr, " Filter component instances:\n");
db0f160a 441 print_bt_config_components(cfg->cmd_data.run.filters);
ebba3338
PP
442 }
443
7213a328 444 fprintf(stderr, " Sink component instances:\n");
db0f160a 445 print_bt_config_components(cfg->cmd_data.run.sinks);
7213a328 446 fprintf(stderr, " Connections:\n");
ebba3338 447
db0f160a 448 for (i = 0; i < cfg->cmd_data.run.connections->len; i++) {
ebba3338 449 struct bt_config_connection *cfg_connection =
db0f160a 450 g_ptr_array_index(cfg->cmd_data.run.connections,
ebba3338
PP
451 i);
452
7213a328 453 fprintf(stderr, " %s%s%s -> %s%s%s\n",
9009cc24
PP
454 cfg_connection->upstream_comp_name->str,
455 cfg_connection->upstream_port_glob->len > 0 ? "." : "",
456 cfg_connection->upstream_port_glob->str,
457 cfg_connection->downstream_comp_name->str,
458 cfg_connection->downstream_port_glob->len > 0 ? "." : "",
459 cfg_connection->downstream_port_glob->str);
ebba3338 460 }
290725f7
PP
461}
462
463static
464void print_cfg_list_plugins(struct bt_config *cfg)
465{
db0f160a 466 print_plugin_paths(cfg->plugin_paths);
290725f7
PP
467}
468
c1081aa6
PP
469static
470void print_cfg_help(struct bt_config *cfg)
471{
db0f160a
PP
472 print_plugin_paths(cfg->plugin_paths);
473}
474
475static
476void print_cfg_print_ctf_metadata(struct bt_config *cfg)
477{
478 print_plugin_paths(cfg->plugin_paths);
7213a328
PP
479 fprintf(stderr, " Path: %s\n",
480 cfg->cmd_data.print_ctf_metadata.path->str);
db0f160a
PP
481}
482
483static
484void print_cfg_print_lttng_live_sessions(struct bt_config *cfg)
485{
486 print_plugin_paths(cfg->plugin_paths);
7213a328
PP
487 fprintf(stderr, " URL: %s\n",
488 cfg->cmd_data.print_lttng_live_sessions.url->str);
c1081aa6
PP
489}
490
491static
a67681c1 492void print_cfg_query(struct bt_config *cfg)
c1081aa6 493{
db0f160a 494 print_plugin_paths(cfg->plugin_paths);
7213a328
PP
495 fprintf(stderr, " Object: `%s`\n", cfg->cmd_data.query.object->str);
496 fprintf(stderr, " Component class:\n");
a67681c1 497 print_bt_config_component(cfg->cmd_data.query.cfg_component);
c1081aa6
PP
498}
499
c42c79ea
PP
500static
501void print_cfg(struct bt_config *cfg)
502{
7213a328 503 if (!BT_LOG_ON_INFO) {
00447e45
PP
504 return;
505 }
506
7213a328
PP
507 BT_LOGI_STR("Configuration:");
508 fprintf(stderr, " Debug mode: %s\n", cfg->debug ? "yes" : "no");
509 fprintf(stderr, " Verbose mode: %s\n", cfg->verbose ? "yes" : "no");
290725f7
PP
510
511 switch (cfg->command) {
db0f160a
PP
512 case BT_CONFIG_COMMAND_RUN:
513 print_cfg_run(cfg);
290725f7
PP
514 break;
515 case BT_CONFIG_COMMAND_LIST_PLUGINS:
516 print_cfg_list_plugins(cfg);
c1081aa6
PP
517 break;
518 case BT_CONFIG_COMMAND_HELP:
519 print_cfg_help(cfg);
520 break;
a67681c1
PP
521 case BT_CONFIG_COMMAND_QUERY:
522 print_cfg_query(cfg);
290725f7 523 break;
db0f160a
PP
524 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA:
525 print_cfg_print_ctf_metadata(cfg);
526 break;
527 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS:
528 print_cfg_print_lttng_live_sessions(cfg);
529 break;
290725f7 530 default:
0fbb9a9f 531 abort();
290725f7 532 }
c42c79ea
PP
533}
534
33b34c43 535static
a8ff38ef 536void add_to_loaded_plugins(struct bt_plugin_set *plugin_set)
98ecef32 537{
544d0515
PP
538 int64_t i;
539 int64_t count;
a8ff38ef
PP
540
541 count = bt_plugin_set_get_plugin_count(plugin_set);
542 assert(count >= 0);
543
544 for (i = 0; i < count; i++) {
545 struct bt_plugin *plugin =
546 bt_plugin_set_get_plugin(plugin_set, i);
33b34c43
PP
547 struct bt_plugin *loaded_plugin =
548 find_plugin(bt_plugin_get_name(plugin));
549
a8ff38ef
PP
550 assert(plugin);
551
33b34c43 552 if (loaded_plugin) {
7213a328
PP
553 BT_LOGI("Not using plugin: another one already exists with the same name: "
554 "plugin-name=\"%s\", plugin-path=\"%s\", "
555 "existing-plugin-path=\"%s\"",
556 bt_plugin_get_name(plugin),
557 bt_plugin_get_path(plugin),
558 bt_plugin_get_path(loaded_plugin));
a8ff38ef 559 bt_put(loaded_plugin);
33b34c43 560 } else {
a8ff38ef 561 /* Add to global array. */
7213a328
PP
562 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
563 bt_plugin_get_name(plugin));
a8ff38ef 564 g_ptr_array_add(loaded_plugins, bt_get(plugin));
33b34c43 565 }
a8ff38ef
PP
566
567 bt_put(plugin);
33b34c43
PP
568 }
569}
570
571static
290725f7 572int load_dynamic_plugins(struct bt_value *plugin_paths)
33b34c43
PP
573{
574 int nr_paths, i, ret = 0;
98ecef32 575
290725f7 576 nr_paths = bt_value_array_size(plugin_paths);
98ecef32 577 if (nr_paths < 0) {
7213a328 578 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
33b34c43
PP
579 ret = -1;
580 goto end;
98ecef32 581 }
33b34c43 582
7213a328
PP
583 BT_LOGI("Loading dynamic plugins.");
584
98ecef32
MD
585 for (i = 0; i < nr_paths; i++) {
586 struct bt_value *plugin_path_value = NULL;
587 const char *plugin_path;
a8ff38ef 588 struct bt_plugin_set *plugin_set;
98ecef32 589
290725f7 590 plugin_path_value = bt_value_array_get(plugin_paths, i);
7213a328
PP
591 bt_value_string_get(plugin_path_value, &plugin_path);
592 assert(plugin_path);
50ad9320
PP
593
594 /*
595 * Skip this if the directory does not exist because
596 * bt_plugin_create_all_from_dir() expects an existing
597 * directory.
598 */
599 if (!g_file_test(plugin_path, G_FILE_TEST_IS_DIR)) {
600 BT_LOGV("Skipping nonexistent directory path: "
601 "path=\"%s\"", plugin_path);
602 BT_PUT(plugin_path_value);
603 continue;
604 }
605
a8ff38ef
PP
606 plugin_set = bt_plugin_create_all_from_dir(plugin_path, false);
607 if (!plugin_set) {
7213a328 608 BT_LOGD("Unable to load dynamic plugins: path=\"%s\"",
98ecef32 609 plugin_path);
33b34c43
PP
610 BT_PUT(plugin_path_value);
611 continue;
98ecef32 612 }
33b34c43 613
a8ff38ef
PP
614 add_to_loaded_plugins(plugin_set);
615 bt_put(plugin_set);
98ecef32
MD
616 BT_PUT(plugin_path_value);
617 }
33b34c43
PP
618end:
619 return ret;
620}
621
622static
623int load_static_plugins(void)
624{
625 int ret = 0;
a8ff38ef 626 struct bt_plugin_set *plugin_set;
33b34c43 627
7213a328 628 BT_LOGI("Loading static plugins.");
a8ff38ef
PP
629 plugin_set = bt_plugin_create_all_from_static();
630 if (!plugin_set) {
7213a328 631 BT_LOGE("Unable to load static plugins.");
33b34c43
PP
632 ret = -1;
633 goto end;
634 }
635
a8ff38ef
PP
636 add_to_loaded_plugins(plugin_set);
637 bt_put(plugin_set);
33b34c43
PP
638end:
639 return ret;
98ecef32
MD
640}
641
9009cc24
PP
642static
643int load_all_plugins(struct bt_value *plugin_paths)
290725f7
PP
644{
645 int ret = 0;
33b34c43 646
290725f7 647 if (load_dynamic_plugins(plugin_paths)) {
290725f7 648 ret = -1;
c1870f57
JG
649 goto end;
650 }
651
290725f7 652 if (load_static_plugins()) {
290725f7 653 ret = -1;
c1870f57
JG
654 goto end;
655 }
656
7213a328
PP
657 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins->len);
658
290725f7
PP
659end:
660 return ret;
661}
662
9009cc24
PP
663static
664void print_plugin_info(struct bt_plugin *plugin)
22e22462
PP
665{
666 unsigned int major, minor, patch;
667 const char *extra;
668 enum bt_plugin_status version_status;
669 const char *plugin_name;
670 const char *path;
671 const char *author;
672 const char *license;
673 const char *plugin_description;
674
675 plugin_name = bt_plugin_get_name(plugin);
676 path = bt_plugin_get_path(plugin);
677 author = bt_plugin_get_author(plugin);
678 license = bt_plugin_get_license(plugin);
679 plugin_description = bt_plugin_get_description(plugin);
680 version_status = bt_plugin_get_version(plugin, &major, &minor,
681 &patch, &extra);
682 printf("%s%s%s%s:\n", bt_common_color_bold(),
683 bt_common_color_fg_blue(), plugin_name,
684 bt_common_color_reset());
685 printf(" %sPath%s: %s\n", bt_common_color_bold(),
686 bt_common_color_reset(), path ? path : "(None)");
687
688 if (version_status == BT_PLUGIN_STATUS_OK) {
689 printf(" %sVersion%s: %u.%u.%u",
690 bt_common_color_bold(), bt_common_color_reset(),
691 major, minor, patch);
692
693 if (extra) {
694 printf("%s", extra);
695 }
696
697 printf("\n");
698 }
699
700 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
701 bt_common_color_reset(),
702 plugin_description ? plugin_description : "(None)");
703 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
704 bt_common_color_reset(), author ? author : "(Unknown)");
705 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
706 bt_common_color_reset(),
707 license ? license : "(Unknown)");
708}
709
9009cc24
PP
710static
711int cmd_query(struct bt_config *cfg)
63ce0e1d 712{
db95fa29 713 int ret = 0;
63ce0e1d
PP
714 struct bt_component_class *comp_cls = NULL;
715 struct bt_value *results = NULL;
716
a67681c1 717 comp_cls = find_component_class(cfg->cmd_data.query.cfg_component->plugin_name->str,
db0f160a 718 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
a67681c1 719 cfg->cmd_data.query.cfg_component->type);
63ce0e1d 720 if (!comp_cls) {
7213a328
PP
721 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
722 "comp-cls-name=\"%s\", comp-cls-type=%d",
723 cfg->cmd_data.query.cfg_component->plugin_name->str,
724 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
725 cfg->cmd_data.query.cfg_component->type);
63ce0e1d
PP
726 fprintf(stderr, "%s%sCannot find component class %s",
727 bt_common_color_bold(),
728 bt_common_color_fg_red(),
729 bt_common_color_reset());
730 print_plugin_comp_cls_opt(stderr,
a67681c1 731 cfg->cmd_data.query.cfg_component->plugin_name->str,
db0f160a 732 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
a67681c1 733 cfg->cmd_data.query.cfg_component->type);
63ce0e1d
PP
734 fprintf(stderr, "\n");
735 ret = -1;
736 goto end;
737 }
738
a67681c1
PP
739 results = bt_component_class_query(comp_cls,
740 cfg->cmd_data.query.object->str,
741 cfg->cmd_data.query.cfg_component->params);
63ce0e1d 742 if (!results) {
7213a328
PP
743 BT_LOGE("Failed to query component class: plugin-name=\"%s\", "
744 "comp-cls-name=\"%s\", comp-cls-type=%d "
745 "object=\"%s\"",
746 cfg->cmd_data.query.cfg_component->plugin_name->str,
747 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
748 cfg->cmd_data.query.cfg_component->type,
749 cfg->cmd_data.query.object->str);
63ce0e1d
PP
750 fprintf(stderr, "%s%sFailed to query info to %s",
751 bt_common_color_bold(),
752 bt_common_color_fg_red(),
753 bt_common_color_reset());
754 print_plugin_comp_cls_opt(stderr,
a67681c1 755 cfg->cmd_data.query.cfg_component->plugin_name->str,
db0f160a 756 cfg->cmd_data.query.cfg_component->comp_cls_name->str,
a67681c1
PP
757 cfg->cmd_data.query.cfg_component->type);
758 fprintf(stderr, "%s%s with object `%s`%s\n",
63ce0e1d
PP
759 bt_common_color_bold(),
760 bt_common_color_fg_red(),
a67681c1 761 cfg->cmd_data.query.object->str,
63ce0e1d
PP
762 bt_common_color_reset());
763 ret = -1;
764 goto end;
765 }
766
7213a328 767 print_value(stdout, results, 0);
63ce0e1d
PP
768
769end:
770 bt_put(comp_cls);
771 bt_put(results);
772 return ret;
773}
774
9009cc24
PP
775static
776int cmd_help(struct bt_config *cfg)
22e22462 777{
db95fa29 778 int ret = 0;
22e22462
PP
779 struct bt_plugin *plugin = NULL;
780 size_t i;
781
90de159b 782 plugin = find_plugin(cfg->cmd_data.help.cfg_component->plugin_name->str);
22e22462 783 if (!plugin) {
7213a328
PP
784 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
785 cfg->cmd_data.help.cfg_component->plugin_name->str);
22e22462
PP
786 fprintf(stderr, "%s%sCannot find plugin %s%s%s\n",
787 bt_common_color_bold(), bt_common_color_fg_red(),
788 bt_common_color_fg_blue(),
90de159b 789 cfg->cmd_data.help.cfg_component->plugin_name->str,
22e22462
PP
790 bt_common_color_reset());
791 ret = -1;
792 goto end;
793 }
794
795 print_plugin_info(plugin);
796 printf(" %sComponent classes%s: %d\n",
797 bt_common_color_bold(),
798 bt_common_color_reset(),
544d0515 799 (int) bt_plugin_get_component_class_count(plugin));
22e22462
PP
800
801
90de159b 802 if (cfg->cmd_data.help.cfg_component->type !=
22e22462
PP
803 BT_COMPONENT_CLASS_TYPE_UNKNOWN) {
804 struct bt_component_class *needed_comp_cls =
805 find_component_class(
90de159b 806 cfg->cmd_data.help.cfg_component->plugin_name->str,
db0f160a 807 cfg->cmd_data.help.cfg_component->comp_cls_name->str,
90de159b 808 cfg->cmd_data.help.cfg_component->type);
22e22462
PP
809
810 if (!needed_comp_cls) {
7213a328
PP
811 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
812 "comp-cls-name=\"%s\", comp-cls-type=%d",
813 cfg->cmd_data.help.cfg_component->plugin_name->str,
814 cfg->cmd_data.help.cfg_component->comp_cls_name->str,
815 cfg->cmd_data.help.cfg_component->type);
22e22462
PP
816 fprintf(stderr, "\n%s%sCannot find component class %s",
817 bt_common_color_bold(),
818 bt_common_color_fg_red(),
819 bt_common_color_reset());
820 print_plugin_comp_cls_opt(stderr,
90de159b 821 cfg->cmd_data.help.cfg_component->plugin_name->str,
db0f160a 822 cfg->cmd_data.help.cfg_component->comp_cls_name->str,
90de159b 823 cfg->cmd_data.help.cfg_component->type);
22e22462
PP
824 fprintf(stderr, "\n");
825 ret = -1;
826 goto end;
827 }
828
829 bt_put(needed_comp_cls);
830 }
831
832 for (i = 0; i < bt_plugin_get_component_class_count(plugin); i++) {
833 struct bt_component_class *comp_cls =
9ac68eb1 834 bt_plugin_get_component_class_by_index(plugin, i);
22e22462
PP
835 const char *comp_class_name =
836 bt_component_class_get_name(comp_cls);
837 const char *comp_class_description =
838 bt_component_class_get_description(comp_cls);
839 const char *comp_class_help =
840 bt_component_class_get_help(comp_cls);
841 enum bt_component_class_type type =
842 bt_component_class_get_type(comp_cls);
843
844 assert(comp_cls);
845
90de159b 846 if (cfg->cmd_data.help.cfg_component->type !=
22e22462 847 BT_COMPONENT_CLASS_TYPE_UNKNOWN) {
db0f160a 848 if (strcmp(cfg->cmd_data.help.cfg_component->comp_cls_name->str,
22e22462
PP
849 comp_class_name) != 0 &&
850 type ==
90de159b 851 cfg->cmd_data.help.cfg_component->type) {
22e22462
PP
852 bt_put(comp_cls);
853 continue;
854 }
855 }
856
857 printf("\n");
858 print_plugin_comp_cls_opt(stdout,
90de159b 859 cfg->cmd_data.help.cfg_component->plugin_name->str,
22e22462
PP
860 comp_class_name,
861 type);
862 printf("\n");
863 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
864 bt_common_color_reset(),
865 comp_class_description ? comp_class_description : "(None)");
866
867 if (comp_class_help) {
868 printf("\n%s\n", comp_class_help);
869 }
870
871 bt_put(comp_cls);
872 }
873
874end:
875 bt_put(plugin);
876 return ret;
877}
878
9009cc24
PP
879static
880int cmd_list_plugins(struct bt_config *cfg)
290725f7 881{
7213a328 882 int ret = 0;
290725f7
PP
883 int plugins_count, component_classes_count = 0, i;
884
22e22462 885 printf("From the following plugin paths:\n\n");
7213a328 886 print_value(stdout, cfg->plugin_paths, 2);
22e22462 887 printf("\n");
290725f7
PP
888 plugins_count = loaded_plugins->len;
889 if (plugins_count == 0) {
7213a328 890 printf("No plugins found.\n");
56a1cced
JG
891 goto end;
892 }
893
290725f7
PP
894 for (i = 0; i < plugins_count; i++) {
895 struct bt_plugin *plugin = g_ptr_array_index(loaded_plugins, i);
896
897 component_classes_count += bt_plugin_get_component_class_count(plugin);
898 }
33bceaf8 899
290725f7
PP
900 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
901 bt_common_color_bold(),
902 component_classes_count,
903 bt_common_color_reset(),
904 bt_common_color_bold(),
905 plugins_count,
906 bt_common_color_reset());
907
908 for (i = 0; i < plugins_count; i++) {
909 int j;
910 struct bt_plugin *plugin = g_ptr_array_index(loaded_plugins, i);
290725f7
PP
911
912 component_classes_count =
913 bt_plugin_get_component_class_count(plugin);
22e22462
PP
914 printf("\n");
915 print_plugin_info(plugin);
290725f7
PP
916
917 if (component_classes_count == 0) {
9009cc24 918 printf(" %sComponent classes%s: (none)\n",
290725f7
PP
919 bt_common_color_bold(),
920 bt_common_color_reset());
921 } else {
922 printf(" %sComponent classes%s:\n",
923 bt_common_color_bold(),
924 bt_common_color_reset());
925 }
926
927 for (j = 0; j < component_classes_count; j++) {
928 struct bt_component_class *comp_class =
9ac68eb1
PP
929 bt_plugin_get_component_class_by_index(
930 plugin, j);
290725f7
PP
931 const char *comp_class_name =
932 bt_component_class_get_name(comp_class);
933 const char *comp_class_description =
934 bt_component_class_get_description(comp_class);
935 enum bt_component_class_type type =
936 bt_component_class_get_type(comp_class);
937
22e22462
PP
938 printf(" ");
939 print_plugin_comp_cls_opt(stdout,
940 bt_plugin_get_name(plugin), comp_class_name,
941 type);
290725f7
PP
942
943 if (comp_class_description) {
944 printf(": %s", comp_class_description);
945 }
946
947 printf("\n");
948 bt_put(comp_class);
949 }
950 }
951
952end:
953 return ret;
954}
955
9009cc24
PP
956static
957int cmd_print_lttng_live_sessions(struct bt_config *cfg)
db0f160a 958{
96e8c7e1
MD
959 int ret = 0;
960 struct bt_component_class *comp_cls = NULL;
961 struct bt_value *results = NULL;
962 struct bt_value *params = NULL;
963 struct bt_value *map = NULL;
964 struct bt_value *v = NULL;
965 static const char * const plugin_name = "ctf";
966 static const char * const comp_cls_name = "lttng-live";
967 static const enum bt_component_class_type comp_cls_type =
968 BT_COMPONENT_CLASS_TYPE_SOURCE;
969 int64_t array_size, i;
970
971 assert(cfg->cmd_data.print_lttng_live_sessions.url);
972 comp_cls = find_component_class(plugin_name, comp_cls_name,
973 comp_cls_type);
974 if (!comp_cls) {
975 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
976 "comp-cls-name=\"%s\", comp-cls-type=%d",
977 plugin_name, comp_cls_name,
978 BT_COMPONENT_CLASS_TYPE_SOURCE);
979 fprintf(stderr, "%s%sCannot find component class %s",
980 bt_common_color_bold(),
981 bt_common_color_fg_red(),
982 bt_common_color_reset());
983 print_plugin_comp_cls_opt(stderr, plugin_name,
984 comp_cls_name, comp_cls_type);
985 fprintf(stderr, "\n");
986 goto error;
987 }
988
989 params = bt_value_map_create();
990 if (!params) {
991 goto error;
992 }
993
994 ret = bt_value_map_insert_string(params, "url",
995 cfg->cmd_data.print_lttng_live_sessions.url->str);
996 if (ret) {
997 goto error;
998 }
999
1000 results = bt_component_class_query(comp_cls, "sessions",
1001 params);
1002 if (!results) {
1003 BT_LOGE_STR("Failed to query for sessions.");
1004 fprintf(stderr, "%s%sFailed to request sessions%s\n",
1005 bt_common_color_bold(),
1006 bt_common_color_fg_red(),
1007 bt_common_color_reset());
1008 goto error;
1009 }
1010
1011 if (!bt_value_is_array(results)) {
1012 BT_LOGE_STR("Expecting an array for sessions query.");
1013 fprintf(stderr, "%s%sUnexpected type returned by session query%s\n",
1014 bt_common_color_bold(),
1015 bt_common_color_fg_red(),
1016 bt_common_color_reset());
1017 goto error;
1018 }
1019
1020 array_size = bt_value_array_size(results);
1021 for (i = 0; i < array_size; i++) {
1022 const char *url_text;
1023 int64_t timer_us, streams, clients;
1024
1025 map = bt_value_array_get(results, i);
1026 if (!map) {
1027 BT_LOGE_STR("Unexpected empty array entry.");
1028 goto error;
1029 }
1030 if (!bt_value_is_map(map)) {
1031 BT_LOGE_STR("Unexpected entry type.");
1032 goto error;
1033 }
1034
1035 v = bt_value_map_get(map, "url");
1036 if (!v) {
1037 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1038 goto error;
1039 }
1040 ret = bt_value_string_get(v, &url_text);
1041 assert(ret == 0);
1042 printf("%s", url_text);
1043 BT_PUT(v);
1044
1045 v = bt_value_map_get(map, "timer-us");
1046 if (!v) {
1047 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1048 goto error;
1049 }
1050 ret = bt_value_integer_get(v, &timer_us);
1051 assert(ret == 0);
1052 printf(" (timer = %" PRIu64 ", ", timer_us);
1053 BT_PUT(v);
1054
1055 v = bt_value_map_get(map, "stream-count");
1056 if (!v) {
1057 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1058 goto error;
1059 }
1060 ret = bt_value_integer_get(v, &streams);
1061 assert(ret == 0);
1062 printf("%" PRIu64 " stream(s), ", streams);
1063 BT_PUT(v);
1064
1065 v = bt_value_map_get(map, "client-count");
1066 if (!v) {
1067 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1068 goto error;
1069 }
1070 ret = bt_value_integer_get(v, &clients);
1071 assert(ret == 0);
1072 printf("%" PRIu64 " client(s) connected)\n", clients);
1073 BT_PUT(v);
1074
1075 BT_PUT(map);
1076 }
1077end:
1078 bt_put(v);
1079 bt_put(map);
1080 bt_put(results);
1081 bt_put(params);
1082 bt_put(comp_cls);
1083 return 0;
1084
1085error:
1086 ret = -1;
1087 goto end;
db0f160a
PP
1088}
1089
9009cc24
PP
1090static
1091int cmd_print_ctf_metadata(struct bt_config *cfg)
05a67631
PP
1092{
1093 int ret = 0;
1094 struct bt_component_class *comp_cls = NULL;
05a67631 1095 struct bt_value *results = NULL;
05a67631
PP
1096 struct bt_value *params = NULL;
1097 struct bt_value *metadata_text_value = NULL;
1098 const char *metadata_text = NULL;
db0f160a
PP
1099 static const char * const plugin_name = "ctf";
1100 static const char * const comp_cls_name = "fs";
1101 static const enum bt_component_class_type comp_cls_type =
1102 BT_COMPONENT_CLASS_TYPE_SOURCE;
1103
1104 assert(cfg->cmd_data.print_ctf_metadata.path);
1105 comp_cls = find_component_class(plugin_name, comp_cls_name,
1106 comp_cls_type);
05a67631 1107 if (!comp_cls) {
7213a328
PP
1108 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1109 "comp-cls-name=\"%s\", comp-cls-type=%d",
1110 plugin_name, comp_cls_name,
1111 BT_COMPONENT_CLASS_TYPE_SOURCE);
05a67631
PP
1112 fprintf(stderr, "%s%sCannot find component class %s",
1113 bt_common_color_bold(),
1114 bt_common_color_fg_red(),
1115 bt_common_color_reset());
db0f160a
PP
1116 print_plugin_comp_cls_opt(stderr, plugin_name,
1117 comp_cls_name, comp_cls_type);
05a67631
PP
1118 fprintf(stderr, "\n");
1119 ret = -1;
1120 goto end;
1121 }
1122
05a67631
PP
1123 params = bt_value_map_create();
1124 if (!params) {
1125 ret = -1;
1126 goto end;
1127 }
1128
db0f160a
PP
1129 ret = bt_value_map_insert_string(params, "path",
1130 cfg->cmd_data.print_ctf_metadata.path->str);
05a67631
PP
1131 if (ret) {
1132 ret = -1;
1133 goto end;
1134 }
1135
a67681c1 1136 results = bt_component_class_query(comp_cls, "metadata-info",
05a67631
PP
1137 params);
1138 if (!results) {
1139 ret = -1;
7213a328 1140 BT_LOGE_STR("Failed to query for metadata info.");
a67681c1 1141 fprintf(stderr, "%s%sFailed to request metadata info%s\n",
05a67631
PP
1142 bt_common_color_bold(),
1143 bt_common_color_fg_red(),
1144 bt_common_color_reset());
1145 goto end;
1146 }
1147
1148 metadata_text_value = bt_value_map_get(results, "text");
1149 if (!metadata_text_value) {
7213a328 1150 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
05a67631
PP
1151 ret = -1;
1152 goto end;
1153 }
1154
1155 ret = bt_value_string_get(metadata_text_value, &metadata_text);
1156 assert(ret == 0);
1157 printf("%s\n", metadata_text);
1158
1159end:
1160 bt_put(results);
05a67631
PP
1161 bt_put(params);
1162 bt_put(metadata_text_value);
1163 bt_put(comp_cls);
05a67631
PP
1164 return 0;
1165}
1166
9009cc24
PP
1167struct cmd_run_ctx {
1168 /* Owned by this */
1169 GHashTable *components;
1170
1171 /* Owned by this */
1172 struct bt_graph *graph;
1173
1174 /* Weak */
1175 struct bt_config *cfg;
1176
1177 bool connect_ports;
1178};
1179
1180static
1181int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1182 struct cmd_run_ctx *ctx, struct bt_component *upstream_comp,
1183 struct bt_port *upstream_port,
1184 struct bt_config_connection *cfg_conn)
290725f7
PP
1185{
1186 int ret = 0;
9009cc24
PP
1187 GQuark downstreamp_comp_name_quark;
1188 struct bt_component *downstream_comp;
1189 int64_t downstream_port_count;
1190 uint64_t i;
1191 int64_t (*port_count_fn)(struct bt_component *);
1192 struct bt_port *(*port_by_index_fn)(struct bt_component *, uint64_t);
a256a42d 1193 enum bt_graph_status status = BT_GRAPH_STATUS_ERROR;
9009cc24 1194
7213a328
PP
1195 BT_LOGI("Connecting upstream port to the next available downstream port: "
1196 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1197 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1198 upstream_port, bt_port_get_name(upstream_port),
1199 cfg_conn->downstream_comp_name->str,
1200 cfg_conn->arg->str);
9009cc24
PP
1201 downstreamp_comp_name_quark = g_quark_from_string(
1202 cfg_conn->downstream_comp_name->str);
1203 assert(downstreamp_comp_name_quark > 0);
1204 downstream_comp = g_hash_table_lookup(ctx->components,
71c7c95f 1205 GUINT_TO_POINTER(downstreamp_comp_name_quark));
9009cc24 1206 if (!downstream_comp) {
7213a328
PP
1207 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1208 "conn-arg=\"%s\"", cfg_conn->downstream_comp_name->str,
1209 cfg_conn->arg->str);
9009cc24
PP
1210 fprintf(stderr, "Cannot create connection: cannot find downstream component: %s\n",
1211 cfg_conn->arg->str);
1212 goto error;
1213 }
1214
1215 if (bt_component_is_filter(downstream_comp)) {
1216 port_count_fn = bt_component_filter_get_input_port_count;
1217 port_by_index_fn = bt_component_filter_get_input_port_by_index;
1218 } else if (bt_component_is_sink(downstream_comp)) {
1219 port_count_fn = bt_component_sink_get_input_port_count;
1220 port_by_index_fn = bt_component_sink_get_input_port_by_index;
1221 } else {
1222 /*
1223 * Should never happen because the connections are
1224 * validated before we get here.
1225 */
7213a328
PP
1226 BT_LOGF("Invalid connection: downstream component is a source: "
1227 "conn-arg=\"%s\"", cfg_conn->arg->str);
0fbb9a9f 1228 abort();
9009cc24 1229 }
290725f7 1230
9009cc24
PP
1231 downstream_port_count = port_count_fn(downstream_comp);
1232 assert(downstream_port_count >= 0);
1233
1234 for (i = 0; i < downstream_port_count; i++) {
1235 struct bt_port *downstream_port =
1236 port_by_index_fn(downstream_comp, i);
1237 const char *downstream_port_name;
1238
1239 assert(downstream_port);
1240
1241 /* Skip port if it's already connected */
1242 if (bt_port_is_connected(downstream_port)) {
1243 bt_put(downstream_port);
7213a328
PP
1244 BT_LOGD("Skipping downstream port: already connected: "
1245 "port-addr=%p, port-name=\"%s\"",
1246 downstream_port,
1247 bt_port_get_name(downstream_port));
9009cc24
PP
1248 continue;
1249 }
1250
1251 downstream_port_name = bt_port_get_name(downstream_port);
1252 assert(downstream_port_name);
1253
1254 if (bt_common_star_glob_match(
1255 cfg_conn->downstream_port_glob->str, -1ULL,
1256 downstream_port_name, -1ULL)) {
1257 /* We have a winner! */
a256a42d
PP
1258 status = bt_graph_connect_ports(ctx->graph,
1259 upstream_port, downstream_port, NULL);
9009cc24 1260 bt_put(downstream_port);
a256a42d
PP
1261 switch (status) {
1262 case BT_GRAPH_STATUS_OK:
1263 break;
1264 case BT_GRAPH_STATUS_CANCELED:
1265 BT_LOGI_STR("Graph was canceled by user.");
1266 status = BT_GRAPH_STATUS_OK;
1267 break;
1268 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION:
1269 BT_LOGE("A component refused a connection to one of its ports: "
1270 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1271 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1272 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1273 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1274 "conn-arg=\"%s\"",
1275 upstream_comp, bt_component_get_name(upstream_comp),
1276 upstream_port, bt_port_get_name(upstream_port),
1277 downstream_comp, cfg_conn->downstream_comp_name->str,
1278 downstream_port, downstream_port_name,
1279 cfg_conn->arg->str);
1280 fprintf(stderr,
1281 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1282 bt_port_get_name(upstream_port),
1283 downstream_port_name,
1284 cfg_conn->arg->str);
1285 break;
1286 default:
7213a328
PP
1287 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1288 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1289 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1290 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1291 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1292 "conn-arg=\"%s\"",
1293 upstream_comp, bt_component_get_name(upstream_comp),
1294 upstream_port, bt_port_get_name(upstream_port),
1295 downstream_comp, cfg_conn->downstream_comp_name->str,
1296 downstream_port, downstream_port_name,
1297 cfg_conn->arg->str);
9009cc24
PP
1298 fprintf(stderr,
1299 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1300 bt_port_get_name(upstream_port),
1301 downstream_port_name,
1302 cfg_conn->arg->str);
1303 goto error;
1304 }
1305
7213a328
PP
1306 BT_LOGI("Connected component ports: "
1307 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1308 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1309 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1310 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1311 "conn-arg=\"%s\"",
1312 upstream_comp, bt_component_get_name(upstream_comp),
1313 upstream_port, bt_port_get_name(upstream_port),
1314 downstream_comp, cfg_conn->downstream_comp_name->str,
1315 downstream_port, downstream_port_name,
1316 cfg_conn->arg->str);
1317
9009cc24
PP
1318 goto end;
1319 }
1320
1321 bt_put(downstream_port);
1322 }
1323
a256a42d 1324 if (status != BT_GRAPH_STATUS_OK) {
7213a328
PP
1325 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1326 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1327 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1328 upstream_port, bt_port_get_name(upstream_port),
1329 cfg_conn->downstream_comp_name->str,
1330 cfg_conn->arg->str);
9009cc24
PP
1331 fprintf(stderr,
1332 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1333 bt_port_get_name(upstream_port), cfg_conn->arg->str);
1334 goto error;
05a67631
PP
1335 }
1336
9009cc24
PP
1337 goto end;
1338
1339error:
1340 ret = -1;
1341
1342end:
9009cc24
PP
1343 return ret;
1344}
1345
1346static
1347int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx *ctx,
1348 struct bt_port *upstream_port)
1349{
1350 int ret = 0;
1351 const char *upstream_port_name;
1352 const char *upstream_comp_name;
1353 struct bt_component *upstream_comp = NULL;
1354 size_t i;
1355
1356 assert(ctx);
1357 assert(upstream_port);
1358 upstream_port_name = bt_port_get_name(upstream_port);
1359 assert(upstream_port_name);
1360 upstream_comp = bt_port_get_component(upstream_port);
1361 if (!upstream_comp) {
7213a328
PP
1362 BT_LOGW("Upstream port to connect is not part of a component: "
1363 "port-addr=%p, port-name=\"%s\"",
1364 upstream_port, upstream_port_name);
98ecef32
MD
1365 ret = -1;
1366 goto end;
33bceaf8
JG
1367 }
1368
9009cc24
PP
1369 upstream_comp_name = bt_component_get_name(upstream_comp);
1370 assert(upstream_comp_name);
7213a328
PP
1371 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1372 "port-addr=%p, port-name=\"%s\"",
1373 upstream_comp, upstream_comp_name,
1374 upstream_port, upstream_port_name);
9009cc24
PP
1375
1376 for (i = 0; i < ctx->cfg->cmd_data.run.connections->len; i++) {
1377 struct bt_config_connection *cfg_conn =
1378 g_ptr_array_index(
1379 ctx->cfg->cmd_data.run.connections, i);
1380
1381 if (strcmp(cfg_conn->upstream_comp_name->str,
1382 upstream_comp_name) == 0) {
1383 if (bt_common_star_glob_match(
1384 cfg_conn->upstream_port_glob->str,
1385 -1ULL, upstream_port_name, -1ULL)) {
1386 ret = cmd_run_ctx_connect_upstream_port_to_downstream_component(
1387 ctx, upstream_comp, upstream_port,
1388 cfg_conn);
1389 if (ret) {
7213a328
PP
1390 BT_LOGE("Cannot connect upstream port: "
1391 "port-addr=%p, port-name=\"%s\"",
1392 upstream_port,
1393 upstream_port_name);
9009cc24
PP
1394 fprintf(stderr,
1395 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1396 upstream_port_name,
1397 upstream_comp_name,
1398 cfg_conn->arg->str);
1399 goto error;
1400 }
1401
1402 goto end;
1403 }
1404 }
1405 }
1406
7213a328
PP
1407 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1408 "port-addr=%p, port-name=\"%s\"", upstream_port,
1409 upstream_port_name);
9009cc24
PP
1410 fprintf(stderr,
1411 "Cannot create connection: upstream port `%s` does not match any connection\n",
7213a328 1412 upstream_port_name);
9009cc24
PP
1413
1414error:
1415 ret = -1;
1416
1417end:
1418 bt_put(upstream_comp);
1419 return ret;
1420}
1421
1422static
1423void graph_port_added_listener(struct bt_port *port, void *data)
1424{
1425 struct bt_component *comp = NULL;
1426 struct cmd_run_ctx *ctx = data;
1427
e12720c0
PP
1428 comp = bt_port_get_component(port);
1429 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1430 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1431 comp, comp ? bt_component_get_name(comp) : "",
7213a328 1432 port, bt_port_get_name(port));
36712f1d
PP
1433
1434 if (!ctx->connect_ports) {
1435 goto end;
1436 }
1437
e12720c0
PP
1438 if (!comp) {
1439 BT_LOGW_STR("Port has no component.");
56a1cced
JG
1440 goto end;
1441 }
7c7c0433 1442
e12720c0
PP
1443 if (bt_port_is_connected(port)) {
1444 BT_LOGW_STR("Port is already connected.");
7c7c0433
JG
1445 goto end;
1446 }
1447
9009cc24 1448 if (!bt_port_is_output(port)) {
7213a328 1449 BT_LOGI_STR("Skipping input port.");
61ddbc8a
JG
1450 goto end;
1451 }
1452
9009cc24 1453 if (cmd_run_ctx_connect_upstream_port(ctx, port)) {
7213a328 1454 BT_LOGF_STR("Cannot connect upstream port.");
9009cc24
PP
1455 fprintf(stderr, "Added port could not be connected: aborting\n");
1456 abort();
1457 }
1458
1459end:
1460 bt_put(comp);
1461 return;
1462}
1463
1464static
1465void graph_port_removed_listener(struct bt_component *component,
1466 struct bt_port *port, void *data)
1467{
7213a328
PP
1468 BT_LOGI("Port removed from a graph's component: comp-addr=%p, "
1469 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1470 component, bt_component_get_name(component),
1471 port, bt_port_get_name(port));
9009cc24
PP
1472}
1473
1474static
1475void graph_ports_connected_listener(struct bt_port *upstream_port,
1476 struct bt_port *downstream_port, void *data)
1477{
e12720c0
PP
1478 struct bt_component *upstream_comp = bt_port_get_component(upstream_port);
1479 struct bt_component *downstream_comp = bt_port_get_component(downstream_port);
1480
1481 assert(upstream_comp);
1482 assert(downstream_comp);
7213a328 1483 BT_LOGI("Graph's component ports connected: "
e12720c0 1484 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
7213a328 1485 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
e12720c0 1486 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
7213a328 1487 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
e12720c0 1488 upstream_comp, bt_component_get_name(upstream_comp),
7213a328 1489 upstream_port, bt_port_get_name(upstream_port),
e12720c0 1490 downstream_comp, bt_component_get_name(downstream_comp),
7213a328 1491 downstream_port, bt_port_get_name(downstream_port));
e12720c0
PP
1492 bt_put(upstream_comp);
1493 bt_put(downstream_comp);
9009cc24
PP
1494}
1495
1496static
1497void graph_ports_disconnected_listener(
1498 struct bt_component *upstream_component,
1499 struct bt_component *downstream_component,
1500 struct bt_port *upstream_port, struct bt_port *downstream_port,
1501 void *data)
1502{
7213a328
PP
1503 BT_LOGI("Graph's component ports disconnected: "
1504 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1505 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1506 upstream_port, bt_port_get_name(upstream_port),
1507 downstream_port, bt_port_get_name(downstream_port));
9009cc24
PP
1508}
1509
1510static
1511void cmd_run_ctx_destroy(struct cmd_run_ctx *ctx)
1512{
1513 if (!ctx) {
1514 return;
1515 }
1516
1517 if (ctx->components) {
1518 g_hash_table_destroy(ctx->components);
1519 ctx->components = NULL;
1520 }
1521
1522 BT_PUT(ctx->graph);
5401f780 1523 the_graph = NULL;
9009cc24
PP
1524 ctx->cfg = NULL;
1525}
1526
1527static
1528int cmd_run_ctx_init(struct cmd_run_ctx *ctx, struct bt_config *cfg)
1529{
1530 int ret = 0;
1531
1532 ctx->cfg = cfg;
1533 ctx->connect_ports = false;
1534 ctx->components = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1535 NULL, bt_put);
1536 if (!ctx->components) {
1537 goto error;
1538 }
1539
1540 ctx->graph = bt_graph_create();
1541 if (!ctx->graph) {
1542 goto error;
1543 }
1544
5401f780 1545 the_graph = ctx->graph;
9009cc24
PP
1546 ret = bt_graph_add_port_added_listener(ctx->graph,
1547 graph_port_added_listener, ctx);
0d107cdd
PP
1548 if (ret < 0) {
1549 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
9009cc24
PP
1550 goto error;
1551 }
1552
1553 ret = bt_graph_add_port_removed_listener(ctx->graph,
1554 graph_port_removed_listener, ctx);
0d107cdd
PP
1555 if (ret < 0) {
1556 BT_LOGE_STR("Cannot add \"port removed\" listener to graph.");
9009cc24
PP
1557 goto error;
1558 }
1559
1560 ret = bt_graph_add_ports_connected_listener(ctx->graph,
1561 graph_ports_connected_listener, ctx);
0d107cdd
PP
1562 if (ret < 0) {
1563 BT_LOGE_STR("Cannot add \"ports connected\" listener to graph.");
9009cc24
PP
1564 goto error;
1565 }
1566
1567 ret = bt_graph_add_ports_disconnected_listener(ctx->graph,
1568 graph_ports_disconnected_listener, ctx);
0d107cdd
PP
1569 if (ret < 0) {
1570 BT_LOGE_STR("Cannot add \"ports disconnected\" listener to graph.");
9009cc24
PP
1571 goto error;
1572 }
1573
1574 goto end;
1575
1576error:
1577 cmd_run_ctx_destroy(ctx);
1578 ret = -1;
1579
1580end:
1581 return ret;
1582}
1583
1584static
1585int cmd_run_ctx_create_components_from_config_components(
1586 struct cmd_run_ctx *ctx, GPtrArray *cfg_components)
1587{
1588 size_t i;
1589 struct bt_component_class *comp_cls = NULL;
1590 struct bt_component *comp = NULL;
1591 int ret = 0;
1592
1593 for (i = 0; i < cfg_components->len; i++) {
1594 struct bt_config_component *cfg_comp =
1595 g_ptr_array_index(cfg_components, i);
1596 GQuark quark;
1597
1598 comp_cls = find_component_class(cfg_comp->plugin_name->str,
1599 cfg_comp->comp_cls_name->str, cfg_comp->type);
1600 if (!comp_cls) {
7213a328
PP
1601 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1602 "comp-cls-name=\"%s\", comp-cls-type=%d",
1603 cfg_comp->plugin_name->str,
1604 cfg_comp->comp_cls_name->str,
1605 cfg_comp->type);
9009cc24
PP
1606 fprintf(stderr, "%s%sCannot find component class %s",
1607 bt_common_color_bold(),
1608 bt_common_color_fg_red(),
1609 bt_common_color_reset());
1610 print_plugin_comp_cls_opt(stderr,
1611 cfg_comp->plugin_name->str,
1612 cfg_comp->comp_cls_name->str,
1613 cfg_comp->type);
1614 fprintf(stderr, "\n");
1615 goto error;
1616 }
1617
36712f1d
PP
1618 ret = bt_graph_add_component(ctx->graph, comp_cls,
1619 cfg_comp->instance_name->str, cfg_comp->params, &comp);
1620 if (ret) {
7213a328 1621 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
32e87ceb 1622 "comp-cls-name=\"%s\", comp-cls-type=%d, "
7213a328
PP
1623 "comp-name=\"%s\"",
1624 cfg_comp->plugin_name->str,
1625 cfg_comp->comp_cls_name->str,
1626 cfg_comp->type, cfg_comp->instance_name->str);
9009cc24
PP
1627 fprintf(stderr, "%s%sCannot create component `%s`%s\n",
1628 bt_common_color_bold(),
1629 bt_common_color_fg_red(),
1630 cfg_comp->instance_name->str,
1631 bt_common_color_reset());
1632 goto error;
1633 }
1634
7213a328
PP
1635 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
1636 comp, cfg_comp->instance_name->str);
9009cc24
PP
1637 quark = g_quark_from_string(cfg_comp->instance_name->str);
1638 assert(quark > 0);
1639 g_hash_table_insert(ctx->components,
71c7c95f 1640 GUINT_TO_POINTER(quark), comp);
9009cc24
PP
1641 comp = NULL;
1642 BT_PUT(comp_cls);
1643 }
1644
1645 goto end;
1646
1647error:
1648 ret = -1;
1649
1650end:
1651 bt_put(comp);
1652 bt_put(comp_cls);
1653 return ret;
1654}
56a1cced 1655
9009cc24
PP
1656static
1657int cmd_run_ctx_create_components(struct cmd_run_ctx *ctx)
1658{
1659 int ret = 0;
1660
1661 /*
1662 * Make sure that, during this phase, our graph's "port added"
1663 * listener does not connect ports while we are creating the
1664 * components because we have a special, initial phase for
1665 * this.
1666 */
1667 ctx->connect_ports = false;
1668
1669 ret = cmd_run_ctx_create_components_from_config_components(
1670 ctx, ctx->cfg->cmd_data.run.sources);
1671 if (ret) {
7c7c0433 1672 ret = -1;
2e339de1
JG
1673 goto end;
1674 }
1675
9009cc24
PP
1676 ret = cmd_run_ctx_create_components_from_config_components(
1677 ctx, ctx->cfg->cmd_data.run.filters);
6c2f3ee5 1678 if (ret) {
290725f7 1679 ret = -1;
fec2a9f2
JG
1680 goto end;
1681 }
78586d8a 1682
9009cc24
PP
1683 ret = cmd_run_ctx_create_components_from_config_components(
1684 ctx, ctx->cfg->cmd_data.run.sinks);
1685 if (ret) {
1686 ret = -1;
1687 goto end;
1688 }
1689
1690end:
1691 return ret;
1692}
1693
1694static
1695int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx *ctx,
1696 struct bt_component *comp,
1697 int64_t (*port_count_fn)(struct bt_component *),
1698 struct bt_port *(*port_by_index_fn)(struct bt_component *, uint64_t))
1699{
1700 int ret = 0;
1701 int64_t count;
1702 uint64_t i;
1703
1704 count = port_count_fn(comp);
1705 assert(count >= 0);
1706
1707 for (i = 0; i < count; i++) {
1708 struct bt_port *upstream_port = port_by_index_fn(comp, i);
1709
1710 assert(upstream_port);
1711 ret = cmd_run_ctx_connect_upstream_port(ctx, upstream_port);
1712 bt_put(upstream_port);
1713 if (ret) {
1714 goto end;
1715 }
1716 }
1717
1718end:
1719 return ret;
1720}
1721
1722static
1723int cmd_run_ctx_connect_ports(struct cmd_run_ctx *ctx)
1724{
1725 int ret = 0;
1726 GHashTableIter iter;
1727 gpointer g_name_quark, g_comp;
1728
1729 ctx->connect_ports = true;
1730 g_hash_table_iter_init(&iter, ctx->components);
1731
1732 while (g_hash_table_iter_next(&iter, &g_name_quark, &g_comp)) {
1733 if (bt_component_is_source(g_comp)) {
1734 ret = cmd_run_ctx_connect_comp_ports(ctx,
1735 g_comp, bt_component_source_get_output_port_count,
1736 bt_component_source_get_output_port_by_index);
1737 } else if (bt_component_is_filter(g_comp)) {
1738 ret = cmd_run_ctx_connect_comp_ports(ctx,
1739 g_comp, bt_component_filter_get_output_port_count,
1740 bt_component_filter_get_output_port_by_index);
1741 }
1742
1743 if (ret) {
1744 goto end;
1745 }
1746 }
1747
1748end:
1749 return ret;
1750}
1751
fd948396
PP
1752static inline
1753const char *bt_graph_status_str(enum bt_graph_status status)
1754{
1755 switch (status) {
1756 case BT_GRAPH_STATUS_CANCELED:
1757 return "BT_GRAPH_STATUS_CANCELED";
1758 case BT_GRAPH_STATUS_AGAIN:
1759 return "BT_GRAPH_STATUS_AGAIN";
1760 case BT_GRAPH_STATUS_END:
1761 return "BT_GRAPH_STATUS_END";
1762 case BT_GRAPH_STATUS_OK:
1763 return "BT_GRAPH_STATUS_OK";
fd948396
PP
1764 case BT_GRAPH_STATUS_INVALID:
1765 return "BT_GRAPH_STATUS_INVALID";
1766 case BT_GRAPH_STATUS_NO_SINK:
1767 return "BT_GRAPH_STATUS_NO_SINK";
1768 case BT_GRAPH_STATUS_ERROR:
1769 return "BT_GRAPH_STATUS_ERROR";
1770 default:
1771 return "(unknown)";
1772 }
1773}
1774
9009cc24
PP
1775static
1776int cmd_run(struct bt_config *cfg)
1777{
1778 int ret = 0;
1779 struct cmd_run_ctx ctx = { 0 };
1780
9009cc24
PP
1781 /* Initialize the command's context and the graph object */
1782 if (cmd_run_ctx_init(&ctx, cfg)) {
7213a328 1783 BT_LOGE_STR("Cannot initialize the command's context.");
9009cc24
PP
1784 fprintf(stderr, "Cannot initialize the command's context\n");
1785 goto error;
1786 }
1787
cc308374
PP
1788 if (canceled) {
1789 BT_LOGI_STR("Canceled by user before creating components.");
1790 goto error;
1791 }
1792
1793 BT_LOGI_STR("Creating components.");
1794
9009cc24
PP
1795 /* Create the requested component instances */
1796 if (cmd_run_ctx_create_components(&ctx)) {
7213a328 1797 BT_LOGE_STR("Cannot create components.");
9009cc24
PP
1798 fprintf(stderr, "Cannot create components\n");
1799 goto error;
1800 }
1801
cc308374
PP
1802 if (canceled) {
1803 BT_LOGI_STR("Canceled by user before connecting components.");
1804 goto error;
1805 }
1806
1807 BT_LOGI_STR("Connecting components.");
1808
9009cc24
PP
1809 /* Connect the initially visible component ports */
1810 if (cmd_run_ctx_connect_ports(&ctx)) {
7213a328 1811 BT_LOGE_STR("Cannot connect initial component ports.");
9009cc24
PP
1812 fprintf(stderr, "Cannot connect initial component ports\n");
1813 goto error;
1814 }
1815
5401f780 1816 if (canceled) {
cc308374
PP
1817 BT_LOGI_STR("Canceled by user before running the graph.");
1818 goto error;
5401f780
PP
1819 }
1820
7213a328
PP
1821 BT_LOGI_STR("Running the graph.");
1822
9009cc24 1823 /* Run the graph */
fec2a9f2 1824 while (true) {
9009cc24 1825 enum bt_graph_status graph_status = bt_graph_run(ctx.graph);
61ddbc8a 1826
5669a3e7
PP
1827 /*
1828 * Reset console in case something messed with console
1829 * codes during the graph's execution.
1830 */
1831 printf("%s", bt_common_color_reset());
1832 fflush(stdout);
1833 fprintf(stderr, "%s", bt_common_color_reset());
fd948396
PP
1834 BT_LOGV("bt_graph_run() returned: status=%s",
1835 bt_graph_status_str(graph_status));
1836
61ddbc8a 1837 switch (graph_status) {
9009cc24
PP
1838 case BT_GRAPH_STATUS_OK:
1839 break;
5401f780 1840 case BT_GRAPH_STATUS_CANCELED:
fd948396 1841 BT_LOGI_STR("Graph was canceled by user.");
5401f780 1842 goto error;
61ddbc8a 1843 case BT_GRAPH_STATUS_AGAIN:
5401f780 1844 if (bt_graph_is_canceled(ctx.graph)) {
fd948396 1845 BT_LOGI_STR("Graph was canceled by user.");
5401f780
PP
1846 goto error;
1847 }
1848
9009cc24 1849 if (cfg->cmd_data.run.retry_duration_us > 0) {
7213a328
PP
1850 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
1851 "time-us=%" PRIu64,
1852 cfg->cmd_data.run.retry_duration_us);
1853
9009cc24 1854 if (usleep(cfg->cmd_data.run.retry_duration_us)) {
cfa4637b
PP
1855 if (bt_graph_is_canceled(ctx.graph)) {
1856 BT_LOGI_STR("Graph was canceled by user.");
1857 goto error;
1858 }
9009cc24
PP
1859 }
1860 }
78586d8a 1861 break;
fec2a9f2
JG
1862 case BT_COMPONENT_STATUS_END:
1863 goto end;
1864 default:
7213a328
PP
1865 BT_LOGE_STR("Graph failed to complete successfully");
1866 fprintf(stderr, "Graph failed to complete successfully\n");
9009cc24 1867 goto error;
78586d8a 1868 }
fec2a9f2 1869 }
290725f7 1870
9009cc24
PP
1871 goto end;
1872
1873error:
1874 if (ret == 0) {
1875 ret = -1;
1876 }
1877
11e1d048 1878end:
9009cc24 1879 cmd_run_ctx_destroy(&ctx);
290725f7
PP
1880 return ret;
1881}
1882
9009cc24
PP
1883static
1884void warn_command_name_and_directory_clash(struct bt_config *cfg)
290725f7 1885{
9009cc24
PP
1886 const char *env_clash;
1887
290725f7
PP
1888 if (!cfg->command_name) {
1889 return;
1890 }
1891
9009cc24
PP
1892 env_clash = getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH);
1893 if (env_clash && strcmp(env_clash, "0") == 0) {
1894 return;
1895 }
1896
290725f7
PP
1897 if (g_file_test(cfg->command_name,
1898 G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
1899 fprintf(stderr, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
1900 cfg->command_name);
1901 fprintf(stderr, "trace located in the local `%s` directory, please use:\n",
1902 cfg->command_name);
1903 fprintf(stderr, "\n");
1904 fprintf(stderr, " babeltrace convert %s [OPTIONS]\n",
1905 cfg->command_name);
1906 }
1907}
1908
7213a328
PP
1909static
1910void init_log_level(void)
1911{
373c938b 1912 bt_cli_log_level = bt_log_get_level_from_env("BABELTRACE_CLI_LOG_LEVEL");
7213a328
PP
1913}
1914
5401f780
PP
1915void set_sigint_handler(void)
1916{
1917 struct sigaction new_action, old_action;
1918
1919 new_action.sa_handler = sigint_handler;
1920 sigemptyset(&new_action.sa_mask);
1921 new_action.sa_flags = 0;
1922 sigaction(SIGINT, NULL, &old_action);
1923
1924 if (old_action.sa_handler != SIG_IGN) {
1925 sigaction(SIGINT, &new_action, NULL);
1926 }
1927}
1928
290725f7
PP
1929int main(int argc, const char **argv)
1930{
1931 int ret;
1932 int retcode;
1933 struct bt_config *cfg;
fc11b6a6 1934 const char **env_var_name;
290725f7 1935
7213a328 1936 init_log_level();
5401f780 1937 set_sigint_handler();
9009cc24
PP
1938 init_static_data();
1939 cfg = bt_config_cli_args_create_with_default(argc, argv, &retcode);
290725f7
PP
1940
1941 if (retcode < 0) {
1942 /* Quit without errors; typically usage/version */
1943 retcode = 0;
7213a328 1944 BT_LOGI_STR("Quitting without errors.");
290725f7
PP
1945 goto end;
1946 }
1947
1948 if (retcode > 0) {
7213a328 1949 BT_LOGE("Command-line error: retcode=%d", retcode);
290725f7
PP
1950 goto end;
1951 }
1952
1953 if (!cfg) {
7213a328 1954 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
290725f7 1955 fprintf(stderr, "Failed to create Babeltrace configuration\n");
db0f160a 1956 retcode = 1;
290725f7
PP
1957 goto end;
1958 }
1959
fc11b6a6
PP
1960 /*
1961 * Set log levels according to --debug or --verbose. For
1962 * backward compatibility, --debug is more verbose than
1963 * --verbose. So:
1964 *
1965 * --verbose: INFO log level
1966 * --debug: VERBOSE log level (includes DEBUG, which is
1967 * is less verbose than VERBOSE in the internal
1968 * logging framework)
1969 */
7213a328 1970 if (cfg->verbose) {
fc11b6a6
PP
1971 bt_cli_log_level = BT_LOGGING_LEVEL_INFO;
1972 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO);
1973 } else if (cfg->debug) {
7213a328
PP
1974 bt_cli_log_level = BT_LOGGING_LEVEL_VERBOSE;
1975 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE);
fc11b6a6
PP
1976 }
1977
1978 env_var_name = log_level_env_var_names;
1979
1980 while (*env_var_name) {
1981 if (cfg->verbose) {
1982 setenv(*env_var_name, "I", 1);
1983 } else if (cfg->debug) {
1984 setenv(*env_var_name, "V", 1);
1985 }
1986
1987 env_var_name++;
7213a328
PP
1988 }
1989
290725f7
PP
1990 babeltrace_debug = cfg->debug;
1991 babeltrace_verbose = cfg->verbose;
1992 print_cfg(cfg);
1993
db0f160a
PP
1994 if (cfg->command_needs_plugins) {
1995 ret = load_all_plugins(cfg->plugin_paths);
1996 if (ret) {
7213a328 1997 BT_LOGE("Failed to load plugins: ret=%d", ret);
db0f160a
PP
1998 retcode = 1;
1999 goto end;
2000 }
2001 }
2002
7213a328
PP
2003 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2004 cfg->command, cfg->command_name);
2005
290725f7 2006 switch (cfg->command) {
db0f160a
PP
2007 case BT_CONFIG_COMMAND_RUN:
2008 ret = cmd_run(cfg);
290725f7
PP
2009 break;
2010 case BT_CONFIG_COMMAND_LIST_PLUGINS:
2011 ret = cmd_list_plugins(cfg);
2012 break;
22e22462
PP
2013 case BT_CONFIG_COMMAND_HELP:
2014 ret = cmd_help(cfg);
2015 break;
a67681c1
PP
2016 case BT_CONFIG_COMMAND_QUERY:
2017 ret = cmd_query(cfg);
63ce0e1d 2018 break;
db0f160a
PP
2019 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA:
2020 ret = cmd_print_ctf_metadata(cfg);
2021 break;
2022 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS:
2023 ret = cmd_print_lttng_live_sessions(cfg);
2024 break;
290725f7 2025 default:
0fbb9a9f
PP
2026 BT_LOGF("Invalid/unknown command: cmd=%d", cfg->command);
2027 abort();
290725f7
PP
2028 }
2029
d26ef3e3
PP
2030 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2031 cfg->command, cfg->command_name, ret);
290725f7
PP
2032 warn_command_name_and_directory_clash(cfg);
2033 retcode = ret ? 1 : 0;
2034
2035end:
2036 BT_PUT(cfg);
9009cc24 2037 fini_static_data();
290725f7 2038 return retcode;
4c8bfb7e 2039}
This page took 0.144153 seconds and 4 git commands to generate.