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