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