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