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