| 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 | NULL, |
| 55 | }; |
| 56 | |
| 57 | /* Application's processing graph (weak) */ |
| 58 | static bt_graph *the_graph; |
| 59 | static bt_query_executor *the_query_executor; |
| 60 | static bool canceled = false; |
| 61 | |
| 62 | GPtrArray *loaded_plugins; |
| 63 | |
| 64 | #ifdef __MINGW32__ |
| 65 | |
| 66 | #include <windows.h> |
| 67 | |
| 68 | static |
| 69 | BOOL WINAPI signal_handler(DWORD signal) { |
| 70 | if (the_graph) { |
| 71 | bt_graph_cancel(the_graph); |
| 72 | } |
| 73 | |
| 74 | canceled = true; |
| 75 | |
| 76 | return TRUE; |
| 77 | } |
| 78 | |
| 79 | static |
| 80 | void set_signal_handler(void) |
| 81 | { |
| 82 | if (!SetConsoleCtrlHandler(signal_handler, TRUE)) { |
| 83 | BT_LOGE("Failed to set the Ctrl+C handler."); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | #else /* __MINGW32__ */ |
| 88 | |
| 89 | static |
| 90 | void signal_handler(int signum) |
| 91 | { |
| 92 | if (signum != SIGINT) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | if (the_graph) { |
| 97 | bt_graph_cancel(the_graph); |
| 98 | } |
| 99 | |
| 100 | if (the_query_executor) { |
| 101 | bt_query_executor_cancel(the_query_executor); |
| 102 | } |
| 103 | |
| 104 | canceled = true; |
| 105 | } |
| 106 | |
| 107 | static |
| 108 | void set_signal_handler(void) |
| 109 | { |
| 110 | struct sigaction new_action, old_action; |
| 111 | |
| 112 | new_action.sa_handler = signal_handler; |
| 113 | sigemptyset(&new_action.sa_mask); |
| 114 | new_action.sa_flags = 0; |
| 115 | sigaction(SIGINT, NULL, &old_action); |
| 116 | |
| 117 | if (old_action.sa_handler != SIG_IGN) { |
| 118 | sigaction(SIGINT, &new_action, NULL); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | #endif /* __MINGW32__ */ |
| 123 | |
| 124 | static |
| 125 | void init_static_data(void) |
| 126 | { |
| 127 | loaded_plugins = g_ptr_array_new_with_free_func( |
| 128 | (GDestroyNotify) bt_object_put_ref); |
| 129 | } |
| 130 | |
| 131 | static |
| 132 | void fini_static_data(void) |
| 133 | { |
| 134 | g_ptr_array_free(loaded_plugins, TRUE); |
| 135 | } |
| 136 | |
| 137 | static |
| 138 | int create_the_query_executor(void) |
| 139 | { |
| 140 | int ret = 0; |
| 141 | |
| 142 | the_query_executor = bt_query_executor_create(); |
| 143 | if (!the_query_executor) { |
| 144 | BT_LOGE_STR("Cannot create a query executor."); |
| 145 | ret = -1; |
| 146 | } |
| 147 | |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | static |
| 152 | void destroy_the_query_executor(void) |
| 153 | { |
| 154 | BT_QUERY_EXECUTOR_PUT_REF_AND_RESET(the_query_executor); |
| 155 | } |
| 156 | |
| 157 | static |
| 158 | int query(struct bt_config *cfg, const bt_component_class *comp_cls, |
| 159 | const char *obj, const bt_value *params, |
| 160 | const bt_value **user_result, const char **fail_reason) |
| 161 | { |
| 162 | const bt_value *result = NULL; |
| 163 | bt_query_executor_query_status query_status; |
| 164 | *fail_reason = "unknown error"; |
| 165 | int ret = 0; |
| 166 | |
| 167 | BT_ASSERT(fail_reason); |
| 168 | BT_ASSERT(user_result); |
| 169 | ret = create_the_query_executor(); |
| 170 | if (ret) { |
| 171 | /* create_the_query_executor() logs errors */ |
| 172 | goto end; |
| 173 | } |
| 174 | |
| 175 | if (canceled) { |
| 176 | BT_LOGI("Canceled by user before executing the query: " |
| 177 | "comp-cls-addr=%p, comp-cls-name=\"%s\", " |
| 178 | "query-obj=\"%s\"", comp_cls, |
| 179 | bt_component_class_get_name(comp_cls), obj); |
| 180 | *fail_reason = "canceled by user"; |
| 181 | goto error; |
| 182 | } |
| 183 | |
| 184 | while (true) { |
| 185 | query_status = bt_query_executor_query( |
| 186 | the_query_executor, comp_cls, obj, params, |
| 187 | cfg->log_level, &result); |
| 188 | switch (query_status) { |
| 189 | case BT_QUERY_EXECUTOR_QUERY_STATUS_OK: |
| 190 | goto ok; |
| 191 | case BT_QUERY_EXECUTOR_QUERY_STATUS_AGAIN: |
| 192 | { |
| 193 | const uint64_t sleep_time_us = 100000; |
| 194 | |
| 195 | /* Wait 100 ms and retry */ |
| 196 | BT_LOGD("Got BT_QUERY_EXECUTOR_QUERY_STATUS_AGAIN: sleeping: " |
| 197 | "time-us=%" PRIu64, sleep_time_us); |
| 198 | |
| 199 | if (usleep(sleep_time_us)) { |
| 200 | if (bt_query_executor_is_canceled(the_query_executor)) { |
| 201 | BT_LOGI("Query was canceled by user: " |
| 202 | "comp-cls-addr=%p, comp-cls-name=\"%s\", " |
| 203 | "query-obj=\"%s\"", comp_cls, |
| 204 | bt_component_class_get_name(comp_cls), |
| 205 | obj); |
| 206 | *fail_reason = "canceled by user"; |
| 207 | goto error; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | continue; |
| 212 | } |
| 213 | case BT_QUERY_EXECUTOR_QUERY_STATUS_CANCELED: |
| 214 | *fail_reason = "canceled by user"; |
| 215 | goto error; |
| 216 | case BT_QUERY_EXECUTOR_QUERY_STATUS_ERROR: |
| 217 | goto error; |
| 218 | case BT_QUERY_EXECUTOR_QUERY_STATUS_INVALID_OBJECT: |
| 219 | *fail_reason = "invalid or unknown query object"; |
| 220 | goto error; |
| 221 | case BT_QUERY_EXECUTOR_QUERY_STATUS_INVALID_PARAMS: |
| 222 | *fail_reason = "invalid query parameters"; |
| 223 | goto error; |
| 224 | case BT_QUERY_EXECUTOR_QUERY_STATUS_UNSUPPORTED: |
| 225 | *fail_reason = "unsupported action"; |
| 226 | goto error; |
| 227 | case BT_QUERY_EXECUTOR_QUERY_STATUS_MEMORY_ERROR: |
| 228 | *fail_reason = "not enough memory"; |
| 229 | goto error; |
| 230 | default: |
| 231 | BT_LOGF("Unknown query status: status=%s", |
| 232 | bt_common_func_status_string( |
| 233 | query_status)); |
| 234 | abort(); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | ok: |
| 239 | *user_result = result; |
| 240 | result = NULL; |
| 241 | goto end; |
| 242 | |
| 243 | error: |
| 244 | ret = -1; |
| 245 | |
| 246 | end: |
| 247 | destroy_the_query_executor(); |
| 248 | bt_value_put_ref(result); |
| 249 | return ret; |
| 250 | } |
| 251 | |
| 252 | static |
| 253 | const bt_plugin *find_plugin(const char *name) |
| 254 | { |
| 255 | int i; |
| 256 | const bt_plugin *plugin = NULL; |
| 257 | |
| 258 | BT_ASSERT(name); |
| 259 | BT_LOGI("Finding plugin: name=\"%s\"", name); |
| 260 | |
| 261 | for (i = 0; i < loaded_plugins->len; i++) { |
| 262 | plugin = g_ptr_array_index(loaded_plugins, i); |
| 263 | |
| 264 | if (strcmp(name, bt_plugin_get_name(plugin)) == 0) { |
| 265 | break; |
| 266 | } |
| 267 | |
| 268 | plugin = NULL; |
| 269 | } |
| 270 | |
| 271 | if (plugin) { |
| 272 | BT_LOGI("Found plugin: name=\"%s\", plugin-addr=%p", |
| 273 | name, plugin); |
| 274 | } else { |
| 275 | BT_LOGI("Cannot find plugin: name=\"%s\"", name); |
| 276 | } |
| 277 | |
| 278 | bt_plugin_get_ref(plugin); |
| 279 | return plugin; |
| 280 | } |
| 281 | |
| 282 | typedef const void *(*plugin_borrow_comp_cls_func_t)( |
| 283 | const bt_plugin *, const char *); |
| 284 | |
| 285 | static |
| 286 | const void *find_component_class_from_plugin(const char *plugin_name, |
| 287 | const char *comp_class_name, |
| 288 | plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func) |
| 289 | { |
| 290 | const void *comp_class = NULL; |
| 291 | const bt_plugin *plugin; |
| 292 | |
| 293 | BT_LOGI("Finding component class: plugin-name=\"%s\", " |
| 294 | "comp-cls-name=\"%s\"", plugin_name, comp_class_name); |
| 295 | |
| 296 | plugin = find_plugin(plugin_name); |
| 297 | if (!plugin) { |
| 298 | goto end; |
| 299 | } |
| 300 | |
| 301 | comp_class = plugin_borrow_comp_cls_func(plugin, comp_class_name); |
| 302 | bt_object_get_ref(comp_class); |
| 303 | BT_PLUGIN_PUT_REF_AND_RESET(plugin); |
| 304 | |
| 305 | end: |
| 306 | if (comp_class) { |
| 307 | BT_LOGI("Found component class: plugin-name=\"%s\", " |
| 308 | "comp-cls-name=\"%s\"", plugin_name, comp_class_name); |
| 309 | } else { |
| 310 | BT_LOGI("Cannot find source component class: " |
| 311 | "plugin-name=\"%s\", comp-cls-name=\"%s\"", |
| 312 | plugin_name, comp_class_name); |
| 313 | } |
| 314 | |
| 315 | return comp_class; |
| 316 | } |
| 317 | |
| 318 | static |
| 319 | const bt_component_class_source *find_source_component_class( |
| 320 | const char *plugin_name, const char *comp_class_name) |
| 321 | { |
| 322 | return (const void *) find_component_class_from_plugin( |
| 323 | plugin_name, comp_class_name, |
| 324 | (plugin_borrow_comp_cls_func_t) |
| 325 | bt_plugin_borrow_source_component_class_by_name_const); |
| 326 | } |
| 327 | |
| 328 | static |
| 329 | const bt_component_class_filter *find_filter_component_class( |
| 330 | const char *plugin_name, const char *comp_class_name) |
| 331 | { |
| 332 | return (const void *) find_component_class_from_plugin( |
| 333 | plugin_name, comp_class_name, |
| 334 | (plugin_borrow_comp_cls_func_t) |
| 335 | bt_plugin_borrow_filter_component_class_by_name_const); |
| 336 | } |
| 337 | |
| 338 | static |
| 339 | const bt_component_class_sink *find_sink_component_class( |
| 340 | const char *plugin_name, const char *comp_class_name) |
| 341 | { |
| 342 | return (const void *) find_component_class_from_plugin(plugin_name, |
| 343 | comp_class_name, |
| 344 | (plugin_borrow_comp_cls_func_t) |
| 345 | bt_plugin_borrow_sink_component_class_by_name_const); |
| 346 | } |
| 347 | |
| 348 | static |
| 349 | const bt_component_class *find_component_class(const char *plugin_name, |
| 350 | const char *comp_class_name, |
| 351 | bt_component_class_type comp_class_type) |
| 352 | { |
| 353 | const bt_component_class *comp_cls = NULL; |
| 354 | |
| 355 | switch (comp_class_type) { |
| 356 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 357 | comp_cls = bt_component_class_source_as_component_class_const(find_source_component_class(plugin_name, comp_class_name)); |
| 358 | break; |
| 359 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 360 | comp_cls = bt_component_class_filter_as_component_class_const(find_filter_component_class(plugin_name, comp_class_name)); |
| 361 | break; |
| 362 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 363 | comp_cls = bt_component_class_sink_as_component_class_const(find_sink_component_class(plugin_name, comp_class_name)); |
| 364 | break; |
| 365 | default: |
| 366 | abort(); |
| 367 | } |
| 368 | |
| 369 | return comp_cls; |
| 370 | } |
| 371 | |
| 372 | static |
| 373 | void print_indent(FILE *fp, size_t indent) |
| 374 | { |
| 375 | size_t i; |
| 376 | |
| 377 | for (i = 0; i < indent; i++) { |
| 378 | fprintf(fp, " "); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | static |
| 383 | const char *component_type_str(bt_component_class_type type) |
| 384 | { |
| 385 | switch (type) { |
| 386 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 387 | return "source"; |
| 388 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 389 | return "sink"; |
| 390 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 391 | return "filter"; |
| 392 | default: |
| 393 | return "(unknown)"; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | static |
| 398 | void print_plugin_comp_cls_opt(FILE *fh, const char *plugin_name, |
| 399 | const char *comp_cls_name, bt_component_class_type type) |
| 400 | { |
| 401 | GString *shell_plugin_name = NULL; |
| 402 | GString *shell_comp_cls_name = NULL; |
| 403 | |
| 404 | shell_plugin_name = bt_common_shell_quote(plugin_name, false); |
| 405 | if (!shell_plugin_name) { |
| 406 | goto end; |
| 407 | } |
| 408 | |
| 409 | shell_comp_cls_name = bt_common_shell_quote(comp_cls_name, false); |
| 410 | if (!shell_comp_cls_name) { |
| 411 | goto end; |
| 412 | } |
| 413 | |
| 414 | fprintf(fh, "'%s%s%s%s.%s%s%s.%s%s%s'", |
| 415 | bt_common_color_bold(), |
| 416 | bt_common_color_fg_cyan(), |
| 417 | component_type_str(type), |
| 418 | bt_common_color_fg_default(), |
| 419 | bt_common_color_fg_blue(), |
| 420 | shell_plugin_name->str, |
| 421 | bt_common_color_fg_default(), |
| 422 | bt_common_color_fg_yellow(), |
| 423 | shell_comp_cls_name->str, |
| 424 | bt_common_color_reset()); |
| 425 | |
| 426 | end: |
| 427 | if (shell_plugin_name) { |
| 428 | g_string_free(shell_plugin_name, TRUE); |
| 429 | } |
| 430 | |
| 431 | if (shell_comp_cls_name) { |
| 432 | g_string_free(shell_comp_cls_name, TRUE); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | static |
| 437 | void print_value(FILE *, const bt_value *, size_t); |
| 438 | |
| 439 | static |
| 440 | void print_value_rec(FILE *, const bt_value *, size_t); |
| 441 | |
| 442 | struct print_map_value_data { |
| 443 | size_t indent; |
| 444 | FILE *fp; |
| 445 | }; |
| 446 | |
| 447 | static |
| 448 | bt_bool print_map_value(const char *key, const bt_value *object, |
| 449 | void *data) |
| 450 | { |
| 451 | struct print_map_value_data *print_map_value_data = data; |
| 452 | |
| 453 | print_indent(print_map_value_data->fp, print_map_value_data->indent); |
| 454 | fprintf(print_map_value_data->fp, "%s: ", key); |
| 455 | BT_ASSERT(object); |
| 456 | |
| 457 | if (bt_value_is_array(object) && |
| 458 | bt_value_array_is_empty(object)) { |
| 459 | fprintf(print_map_value_data->fp, "[ ]\n"); |
| 460 | return true; |
| 461 | } |
| 462 | |
| 463 | if (bt_value_is_map(object) && |
| 464 | bt_value_map_is_empty(object)) { |
| 465 | fprintf(print_map_value_data->fp, "{ }\n"); |
| 466 | return true; |
| 467 | } |
| 468 | |
| 469 | if (bt_value_is_array(object) || |
| 470 | bt_value_is_map(object)) { |
| 471 | fprintf(print_map_value_data->fp, "\n"); |
| 472 | } |
| 473 | |
| 474 | print_value_rec(print_map_value_data->fp, object, |
| 475 | print_map_value_data->indent + 2); |
| 476 | return BT_TRUE; |
| 477 | } |
| 478 | |
| 479 | static |
| 480 | void print_value_rec(FILE *fp, const bt_value *value, size_t indent) |
| 481 | { |
| 482 | bt_bool bool_val; |
| 483 | int64_t int_val; |
| 484 | uint64_t uint_val; |
| 485 | double dbl_val; |
| 486 | const char *str_val; |
| 487 | int size; |
| 488 | int i; |
| 489 | |
| 490 | if (!value) { |
| 491 | return; |
| 492 | } |
| 493 | |
| 494 | switch (bt_value_get_type(value)) { |
| 495 | case BT_VALUE_TYPE_NULL: |
| 496 | fprintf(fp, "%snull%s\n", bt_common_color_bold(), |
| 497 | bt_common_color_reset()); |
| 498 | break; |
| 499 | case BT_VALUE_TYPE_BOOL: |
| 500 | bool_val = bt_value_bool_get(value); |
| 501 | fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(), |
| 502 | bt_common_color_fg_cyan(), bool_val ? "yes" : "no", |
| 503 | bt_common_color_reset()); |
| 504 | break; |
| 505 | case BT_VALUE_TYPE_UNSIGNED_INTEGER: |
| 506 | uint_val = bt_value_unsigned_integer_get(value); |
| 507 | fprintf(fp, "%s%s%" PRIu64 "%s\n", bt_common_color_bold(), |
| 508 | bt_common_color_fg_red(), uint_val, |
| 509 | bt_common_color_reset()); |
| 510 | break; |
| 511 | case BT_VALUE_TYPE_SIGNED_INTEGER: |
| 512 | int_val = bt_value_signed_integer_get(value); |
| 513 | fprintf(fp, "%s%s%" PRId64 "%s\n", bt_common_color_bold(), |
| 514 | bt_common_color_fg_red(), int_val, |
| 515 | bt_common_color_reset()); |
| 516 | break; |
| 517 | case BT_VALUE_TYPE_REAL: |
| 518 | dbl_val = bt_value_real_get(value); |
| 519 | fprintf(fp, "%s%s%lf%s\n", bt_common_color_bold(), |
| 520 | bt_common_color_fg_red(), dbl_val, |
| 521 | bt_common_color_reset()); |
| 522 | break; |
| 523 | case BT_VALUE_TYPE_STRING: |
| 524 | str_val = bt_value_string_get(value); |
| 525 | fprintf(fp, "%s%s%s%s\n", bt_common_color_bold(), |
| 526 | bt_common_color_fg_green(), str_val, |
| 527 | bt_common_color_reset()); |
| 528 | break; |
| 529 | case BT_VALUE_TYPE_ARRAY: |
| 530 | size = bt_value_array_get_size(value); |
| 531 | if (size < 0) { |
| 532 | goto error; |
| 533 | } |
| 534 | |
| 535 | if (size == 0) { |
| 536 | print_indent(fp, indent); |
| 537 | fprintf(fp, "[ ]\n"); |
| 538 | break; |
| 539 | } |
| 540 | |
| 541 | for (i = 0; i < size; i++) { |
| 542 | const bt_value *element = |
| 543 | bt_value_array_borrow_element_by_index_const( |
| 544 | value, i); |
| 545 | |
| 546 | if (!element) { |
| 547 | goto error; |
| 548 | } |
| 549 | print_indent(fp, indent); |
| 550 | fprintf(fp, "- "); |
| 551 | |
| 552 | if (bt_value_is_array(element) && |
| 553 | bt_value_array_is_empty(element)) { |
| 554 | fprintf(fp, "[ ]\n"); |
| 555 | continue; |
| 556 | } |
| 557 | |
| 558 | if (bt_value_is_map(element) && |
| 559 | bt_value_map_is_empty(element)) { |
| 560 | fprintf(fp, "{ }\n"); |
| 561 | continue; |
| 562 | } |
| 563 | |
| 564 | if (bt_value_is_array(element) || |
| 565 | bt_value_is_map(element)) { |
| 566 | fprintf(fp, "\n"); |
| 567 | } |
| 568 | |
| 569 | print_value_rec(fp, element, indent + 2); |
| 570 | } |
| 571 | break; |
| 572 | case BT_VALUE_TYPE_MAP: |
| 573 | { |
| 574 | struct print_map_value_data data = { |
| 575 | .indent = indent, |
| 576 | .fp = fp, |
| 577 | }; |
| 578 | |
| 579 | if (bt_value_map_is_empty(value)) { |
| 580 | print_indent(fp, indent); |
| 581 | fprintf(fp, "{ }\n"); |
| 582 | break; |
| 583 | } |
| 584 | |
| 585 | bt_value_map_foreach_entry_const(value, print_map_value, &data); |
| 586 | break; |
| 587 | } |
| 588 | default: |
| 589 | abort(); |
| 590 | } |
| 591 | return; |
| 592 | |
| 593 | error: |
| 594 | BT_LOGE("Error printing value of type %s.", |
| 595 | bt_common_value_type_string(bt_value_get_type(value))); |
| 596 | } |
| 597 | |
| 598 | static |
| 599 | void print_value(FILE *fp, const bt_value *value, size_t indent) |
| 600 | { |
| 601 | if (!bt_value_is_array(value) && !bt_value_is_map(value)) { |
| 602 | print_indent(fp, indent); |
| 603 | } |
| 604 | |
| 605 | print_value_rec(fp, value, indent); |
| 606 | } |
| 607 | |
| 608 | static |
| 609 | void print_bt_config_component(struct bt_config_component *bt_config_component) |
| 610 | { |
| 611 | fprintf(stderr, " "); |
| 612 | print_plugin_comp_cls_opt(stderr, bt_config_component->plugin_name->str, |
| 613 | bt_config_component->comp_cls_name->str, |
| 614 | bt_config_component->type); |
| 615 | fprintf(stderr, ":\n"); |
| 616 | |
| 617 | if (bt_config_component->instance_name->len > 0) { |
| 618 | fprintf(stderr, " Name: %s\n", |
| 619 | bt_config_component->instance_name->str); |
| 620 | } |
| 621 | |
| 622 | fprintf(stderr, " Parameters:\n"); |
| 623 | print_value(stderr, bt_config_component->params, 8); |
| 624 | } |
| 625 | |
| 626 | static |
| 627 | void print_bt_config_components(GPtrArray *array) |
| 628 | { |
| 629 | size_t i; |
| 630 | |
| 631 | for (i = 0; i < array->len; i++) { |
| 632 | struct bt_config_component *cfg_component = |
| 633 | bt_config_get_component(array, i); |
| 634 | print_bt_config_component(cfg_component); |
| 635 | BT_OBJECT_PUT_REF_AND_RESET(cfg_component); |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | static |
| 640 | void print_plugin_paths(const bt_value *plugin_paths) |
| 641 | { |
| 642 | fprintf(stderr, " Plugin paths:\n"); |
| 643 | print_value(stderr, plugin_paths, 4); |
| 644 | } |
| 645 | |
| 646 | static |
| 647 | void print_cfg_run(struct bt_config *cfg) |
| 648 | { |
| 649 | size_t i; |
| 650 | |
| 651 | print_plugin_paths(cfg->plugin_paths); |
| 652 | fprintf(stderr, " Source component instances:\n"); |
| 653 | print_bt_config_components(cfg->cmd_data.run.sources); |
| 654 | |
| 655 | if (cfg->cmd_data.run.filters->len > 0) { |
| 656 | fprintf(stderr, " Filter component instances:\n"); |
| 657 | print_bt_config_components(cfg->cmd_data.run.filters); |
| 658 | } |
| 659 | |
| 660 | fprintf(stderr, " Sink component instances:\n"); |
| 661 | print_bt_config_components(cfg->cmd_data.run.sinks); |
| 662 | fprintf(stderr, " Connections:\n"); |
| 663 | |
| 664 | for (i = 0; i < cfg->cmd_data.run.connections->len; i++) { |
| 665 | struct bt_config_connection *cfg_connection = |
| 666 | g_ptr_array_index(cfg->cmd_data.run.connections, |
| 667 | i); |
| 668 | |
| 669 | fprintf(stderr, " %s%s%s -> %s%s%s\n", |
| 670 | cfg_connection->upstream_comp_name->str, |
| 671 | cfg_connection->upstream_port_glob->len > 0 ? "." : "", |
| 672 | cfg_connection->upstream_port_glob->str, |
| 673 | cfg_connection->downstream_comp_name->str, |
| 674 | cfg_connection->downstream_port_glob->len > 0 ? "." : "", |
| 675 | cfg_connection->downstream_port_glob->str); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | static |
| 680 | void print_cfg_list_plugins(struct bt_config *cfg) |
| 681 | { |
| 682 | print_plugin_paths(cfg->plugin_paths); |
| 683 | } |
| 684 | |
| 685 | static |
| 686 | void print_cfg_help(struct bt_config *cfg) |
| 687 | { |
| 688 | print_plugin_paths(cfg->plugin_paths); |
| 689 | } |
| 690 | |
| 691 | static |
| 692 | void print_cfg_print_ctf_metadata(struct bt_config *cfg) |
| 693 | { |
| 694 | print_plugin_paths(cfg->plugin_paths); |
| 695 | fprintf(stderr, " Path: %s\n", |
| 696 | cfg->cmd_data.print_ctf_metadata.path->str); |
| 697 | } |
| 698 | |
| 699 | static |
| 700 | void print_cfg_print_lttng_live_sessions(struct bt_config *cfg) |
| 701 | { |
| 702 | print_plugin_paths(cfg->plugin_paths); |
| 703 | fprintf(stderr, " URL: %s\n", |
| 704 | cfg->cmd_data.print_lttng_live_sessions.url->str); |
| 705 | } |
| 706 | |
| 707 | static |
| 708 | void print_cfg_query(struct bt_config *cfg) |
| 709 | { |
| 710 | print_plugin_paths(cfg->plugin_paths); |
| 711 | fprintf(stderr, " Object: `%s`\n", cfg->cmd_data.query.object->str); |
| 712 | fprintf(stderr, " Component class:\n"); |
| 713 | print_bt_config_component(cfg->cmd_data.query.cfg_component); |
| 714 | } |
| 715 | |
| 716 | static |
| 717 | void print_cfg(struct bt_config *cfg) |
| 718 | { |
| 719 | if (!BT_LOG_ON_INFO) { |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | BT_LOGI_STR("CLI configuration:"); |
| 724 | BT_LOGI(" Debug mode: %s\n", cfg->debug ? "yes" : "no"); |
| 725 | BT_LOGI(" Verbose mode: %s\n", cfg->verbose ? "yes" : "no"); |
| 726 | |
| 727 | switch (cfg->command) { |
| 728 | case BT_CONFIG_COMMAND_RUN: |
| 729 | print_cfg_run(cfg); |
| 730 | break; |
| 731 | case BT_CONFIG_COMMAND_LIST_PLUGINS: |
| 732 | print_cfg_list_plugins(cfg); |
| 733 | break; |
| 734 | case BT_CONFIG_COMMAND_HELP: |
| 735 | print_cfg_help(cfg); |
| 736 | break; |
| 737 | case BT_CONFIG_COMMAND_QUERY: |
| 738 | print_cfg_query(cfg); |
| 739 | break; |
| 740 | case BT_CONFIG_COMMAND_PRINT_CTF_METADATA: |
| 741 | print_cfg_print_ctf_metadata(cfg); |
| 742 | break; |
| 743 | case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS: |
| 744 | print_cfg_print_lttng_live_sessions(cfg); |
| 745 | break; |
| 746 | default: |
| 747 | abort(); |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | static |
| 752 | void add_to_loaded_plugins(const bt_plugin_set *plugin_set) |
| 753 | { |
| 754 | int64_t i; |
| 755 | int64_t count; |
| 756 | |
| 757 | count = bt_plugin_set_get_plugin_count(plugin_set); |
| 758 | BT_ASSERT(count >= 0); |
| 759 | |
| 760 | for (i = 0; i < count; i++) { |
| 761 | const bt_plugin *plugin = |
| 762 | bt_plugin_set_borrow_plugin_by_index_const(plugin_set, i); |
| 763 | const bt_plugin *loaded_plugin = |
| 764 | find_plugin(bt_plugin_get_name(plugin)); |
| 765 | |
| 766 | BT_ASSERT(plugin); |
| 767 | |
| 768 | if (loaded_plugin) { |
| 769 | BT_LOGI("Not using plugin: another one already exists with the same name: " |
| 770 | "plugin-name=\"%s\", plugin-path=\"%s\", " |
| 771 | "existing-plugin-path=\"%s\"", |
| 772 | bt_plugin_get_name(plugin), |
| 773 | bt_plugin_get_path(plugin), |
| 774 | bt_plugin_get_path(loaded_plugin)); |
| 775 | bt_plugin_put_ref(loaded_plugin); |
| 776 | } else { |
| 777 | /* Add to global array. */ |
| 778 | BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"", |
| 779 | bt_plugin_get_name(plugin)); |
| 780 | bt_plugin_get_ref(plugin); |
| 781 | g_ptr_array_add(loaded_plugins, (void *) plugin); |
| 782 | } |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | static |
| 787 | int load_dynamic_plugins(const bt_value *plugin_paths) |
| 788 | { |
| 789 | int nr_paths, i, ret = 0; |
| 790 | |
| 791 | nr_paths = bt_value_array_get_size(plugin_paths); |
| 792 | if (nr_paths < 0) { |
| 793 | BT_LOGE_STR("Cannot load dynamic plugins: no plugin path."); |
| 794 | ret = -1; |
| 795 | goto end; |
| 796 | } |
| 797 | |
| 798 | BT_LOGI_STR("Loading dynamic plugins."); |
| 799 | |
| 800 | for (i = 0; i < nr_paths; i++) { |
| 801 | const bt_value *plugin_path_value = NULL; |
| 802 | const char *plugin_path; |
| 803 | const bt_plugin_set *plugin_set = NULL; |
| 804 | bt_plugin_find_all_from_dir_status status; |
| 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 | status = bt_plugin_find_all_from_dir(plugin_path, BT_FALSE, |
| 823 | BT_FALSE, &plugin_set); |
| 824 | if (status < 0) { |
| 825 | BT_LOGE("Unable to load dynamic plugins from directory: " |
| 826 | "path=\"%s\"", plugin_path); |
| 827 | continue; |
| 828 | } else if (status == |
| 829 | BT_PLUGIN_FIND_ALL_FROM_DIR_STATUS_NOT_FOUND) { |
| 830 | BT_LOGI("No plugins found in directory: path=\"%s\"", |
| 831 | plugin_path); |
| 832 | continue; |
| 833 | } |
| 834 | |
| 835 | BT_ASSERT(status == BT_PLUGIN_FIND_ALL_FROM_DIR_STATUS_OK); |
| 836 | BT_ASSERT(plugin_set); |
| 837 | add_to_loaded_plugins(plugin_set); |
| 838 | bt_plugin_set_put_ref(plugin_set); |
| 839 | } |
| 840 | end: |
| 841 | return ret; |
| 842 | } |
| 843 | |
| 844 | static |
| 845 | int load_static_plugins(void) |
| 846 | { |
| 847 | int ret = 0; |
| 848 | const bt_plugin_set *plugin_set; |
| 849 | bt_plugin_find_all_from_static_status status; |
| 850 | |
| 851 | BT_LOGI("Loading static plugins."); |
| 852 | status = bt_plugin_find_all_from_static(BT_FALSE, &plugin_set); |
| 853 | if (status < 0) { |
| 854 | BT_LOGE("Unable to load static plugins."); |
| 855 | ret = -1; |
| 856 | goto end; |
| 857 | } else if (status == |
| 858 | BT_PLUGIN_FIND_ALL_FROM_STATIC_STATUS_NOT_FOUND) { |
| 859 | BT_LOGI("No static plugins found."); |
| 860 | goto end; |
| 861 | } |
| 862 | |
| 863 | BT_ASSERT(status == BT_PLUGIN_FIND_ALL_FROM_STATIC_STATUS_OK); |
| 864 | BT_ASSERT(plugin_set); |
| 865 | add_to_loaded_plugins(plugin_set); |
| 866 | bt_plugin_set_put_ref(plugin_set); |
| 867 | |
| 868 | end: |
| 869 | return ret; |
| 870 | } |
| 871 | |
| 872 | static |
| 873 | int load_all_plugins(const bt_value *plugin_paths) |
| 874 | { |
| 875 | int ret = 0; |
| 876 | |
| 877 | if (load_dynamic_plugins(plugin_paths)) { |
| 878 | ret = -1; |
| 879 | goto end; |
| 880 | } |
| 881 | |
| 882 | if (load_static_plugins()) { |
| 883 | ret = -1; |
| 884 | goto end; |
| 885 | } |
| 886 | |
| 887 | BT_LOGI("Loaded all plugins: count=%u", loaded_plugins->len); |
| 888 | |
| 889 | end: |
| 890 | return ret; |
| 891 | } |
| 892 | |
| 893 | static |
| 894 | void print_plugin_info(const bt_plugin *plugin) |
| 895 | { |
| 896 | unsigned int major, minor, patch; |
| 897 | const char *extra; |
| 898 | bt_property_availability version_avail; |
| 899 | const char *plugin_name; |
| 900 | const char *path; |
| 901 | const char *author; |
| 902 | const char *license; |
| 903 | const char *plugin_description; |
| 904 | |
| 905 | plugin_name = bt_plugin_get_name(plugin); |
| 906 | path = bt_plugin_get_path(plugin); |
| 907 | author = bt_plugin_get_author(plugin); |
| 908 | license = bt_plugin_get_license(plugin); |
| 909 | plugin_description = bt_plugin_get_description(plugin); |
| 910 | version_avail = bt_plugin_get_version(plugin, &major, &minor, |
| 911 | &patch, &extra); |
| 912 | printf("%s%s%s%s:\n", bt_common_color_bold(), |
| 913 | bt_common_color_fg_blue(), plugin_name, |
| 914 | bt_common_color_reset()); |
| 915 | if (path) { |
| 916 | printf(" %sPath%s: %s\n", bt_common_color_bold(), |
| 917 | bt_common_color_reset(), path); |
| 918 | } else { |
| 919 | puts(" Built-in"); |
| 920 | } |
| 921 | |
| 922 | if (version_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE) { |
| 923 | printf(" %sVersion%s: %u.%u.%u", |
| 924 | bt_common_color_bold(), bt_common_color_reset(), |
| 925 | major, minor, patch); |
| 926 | |
| 927 | if (extra) { |
| 928 | printf("%s", extra); |
| 929 | } |
| 930 | |
| 931 | printf("\n"); |
| 932 | } |
| 933 | |
| 934 | printf(" %sDescription%s: %s\n", bt_common_color_bold(), |
| 935 | bt_common_color_reset(), |
| 936 | plugin_description ? plugin_description : "(None)"); |
| 937 | printf(" %sAuthor%s: %s\n", bt_common_color_bold(), |
| 938 | bt_common_color_reset(), author ? author : "(Unknown)"); |
| 939 | printf(" %sLicense%s: %s\n", bt_common_color_bold(), |
| 940 | bt_common_color_reset(), |
| 941 | license ? license : "(Unknown)"); |
| 942 | } |
| 943 | |
| 944 | static |
| 945 | int cmd_query(struct bt_config *cfg) |
| 946 | { |
| 947 | int ret = 0; |
| 948 | const bt_component_class *comp_cls = NULL; |
| 949 | const bt_value *results = NULL; |
| 950 | const char *fail_reason = NULL; |
| 951 | |
| 952 | comp_cls = find_component_class( |
| 953 | cfg->cmd_data.query.cfg_component->plugin_name->str, |
| 954 | cfg->cmd_data.query.cfg_component->comp_cls_name->str, |
| 955 | cfg->cmd_data.query.cfg_component->type); |
| 956 | if (!comp_cls) { |
| 957 | BT_LOGE("Cannot find component class: plugin-name=\"%s\", " |
| 958 | "comp-cls-name=\"%s\", comp-cls-type=%d", |
| 959 | cfg->cmd_data.query.cfg_component->plugin_name->str, |
| 960 | cfg->cmd_data.query.cfg_component->comp_cls_name->str, |
| 961 | cfg->cmd_data.query.cfg_component->type); |
| 962 | fprintf(stderr, "%s%sCannot find component class %s", |
| 963 | bt_common_color_bold(), |
| 964 | bt_common_color_fg_red(), |
| 965 | bt_common_color_reset()); |
| 966 | print_plugin_comp_cls_opt(stderr, |
| 967 | cfg->cmd_data.query.cfg_component->plugin_name->str, |
| 968 | cfg->cmd_data.query.cfg_component->comp_cls_name->str, |
| 969 | cfg->cmd_data.query.cfg_component->type); |
| 970 | fprintf(stderr, "\n"); |
| 971 | ret = -1; |
| 972 | goto end; |
| 973 | } |
| 974 | |
| 975 | ret = query(cfg, comp_cls, cfg->cmd_data.query.object->str, |
| 976 | cfg->cmd_data.query.cfg_component->params, |
| 977 | &results, &fail_reason); |
| 978 | if (ret) { |
| 979 | goto failed; |
| 980 | } |
| 981 | |
| 982 | print_value(stdout, results, 0); |
| 983 | goto end; |
| 984 | |
| 985 | failed: |
| 986 | BT_LOGE("Failed to query component class: %s: plugin-name=\"%s\", " |
| 987 | "comp-cls-name=\"%s\", comp-cls-type=%d " |
| 988 | "object=\"%s\"", fail_reason, |
| 989 | cfg->cmd_data.query.cfg_component->plugin_name->str, |
| 990 | cfg->cmd_data.query.cfg_component->comp_cls_name->str, |
| 991 | cfg->cmd_data.query.cfg_component->type, |
| 992 | cfg->cmd_data.query.object->str); |
| 993 | fprintf(stderr, "%s%sFailed to query info to %s", |
| 994 | bt_common_color_bold(), |
| 995 | bt_common_color_fg_red(), |
| 996 | bt_common_color_reset()); |
| 997 | print_plugin_comp_cls_opt(stderr, |
| 998 | cfg->cmd_data.query.cfg_component->plugin_name->str, |
| 999 | cfg->cmd_data.query.cfg_component->comp_cls_name->str, |
| 1000 | cfg->cmd_data.query.cfg_component->type); |
| 1001 | fprintf(stderr, "%s%s with object `%s`: %s%s\n", |
| 1002 | bt_common_color_bold(), |
| 1003 | bt_common_color_fg_red(), |
| 1004 | cfg->cmd_data.query.object->str, |
| 1005 | fail_reason, |
| 1006 | bt_common_color_reset()); |
| 1007 | ret = -1; |
| 1008 | |
| 1009 | end: |
| 1010 | bt_component_class_put_ref(comp_cls); |
| 1011 | bt_value_put_ref(results); |
| 1012 | return ret; |
| 1013 | } |
| 1014 | |
| 1015 | static |
| 1016 | void print_component_class_help(const char *plugin_name, |
| 1017 | const bt_component_class *comp_cls) |
| 1018 | { |
| 1019 | const char *comp_class_name = |
| 1020 | bt_component_class_get_name(comp_cls); |
| 1021 | const char *comp_class_description = |
| 1022 | bt_component_class_get_description(comp_cls); |
| 1023 | const char *comp_class_help = |
| 1024 | bt_component_class_get_help(comp_cls); |
| 1025 | bt_component_class_type type = |
| 1026 | bt_component_class_get_type(comp_cls); |
| 1027 | |
| 1028 | print_plugin_comp_cls_opt(stdout, plugin_name, comp_class_name, type); |
| 1029 | printf("\n"); |
| 1030 | printf(" %sDescription%s: %s\n", bt_common_color_bold(), |
| 1031 | bt_common_color_reset(), |
| 1032 | comp_class_description ? comp_class_description : "(None)"); |
| 1033 | |
| 1034 | if (comp_class_help) { |
| 1035 | printf("\n%s\n", comp_class_help); |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | static |
| 1040 | int cmd_help(struct bt_config *cfg) |
| 1041 | { |
| 1042 | int ret = 0; |
| 1043 | const bt_plugin *plugin = NULL; |
| 1044 | const bt_component_class *needed_comp_cls = NULL; |
| 1045 | |
| 1046 | plugin = find_plugin(cfg->cmd_data.help.cfg_component->plugin_name->str); |
| 1047 | if (!plugin) { |
| 1048 | BT_LOGE("Cannot find plugin: plugin-name=\"%s\"", |
| 1049 | cfg->cmd_data.help.cfg_component->plugin_name->str); |
| 1050 | fprintf(stderr, "%s%sCannot find plugin %s%s%s\n", |
| 1051 | bt_common_color_bold(), bt_common_color_fg_red(), |
| 1052 | bt_common_color_fg_blue(), |
| 1053 | cfg->cmd_data.help.cfg_component->plugin_name->str, |
| 1054 | bt_common_color_reset()); |
| 1055 | ret = -1; |
| 1056 | goto end; |
| 1057 | } |
| 1058 | |
| 1059 | print_plugin_info(plugin); |
| 1060 | printf(" %sSource component classes%s: %d\n", |
| 1061 | bt_common_color_bold(), |
| 1062 | bt_common_color_reset(), |
| 1063 | (int) bt_plugin_get_source_component_class_count(plugin)); |
| 1064 | printf(" %sFilter component classes%s: %d\n", |
| 1065 | bt_common_color_bold(), |
| 1066 | bt_common_color_reset(), |
| 1067 | (int) bt_plugin_get_filter_component_class_count(plugin)); |
| 1068 | printf(" %sSink component classes%s: %d\n", |
| 1069 | bt_common_color_bold(), |
| 1070 | bt_common_color_reset(), |
| 1071 | (int) bt_plugin_get_sink_component_class_count(plugin)); |
| 1072 | |
| 1073 | if (strlen(cfg->cmd_data.help.cfg_component->comp_cls_name->str) == 0) { |
| 1074 | /* Plugin help only */ |
| 1075 | goto end; |
| 1076 | } |
| 1077 | |
| 1078 | needed_comp_cls = find_component_class( |
| 1079 | cfg->cmd_data.help.cfg_component->plugin_name->str, |
| 1080 | cfg->cmd_data.help.cfg_component->comp_cls_name->str, |
| 1081 | cfg->cmd_data.help.cfg_component->type); |
| 1082 | if (!needed_comp_cls) { |
| 1083 | BT_LOGE("Cannot find component class: plugin-name=\"%s\", " |
| 1084 | "comp-cls-name=\"%s\", comp-cls-type=%d", |
| 1085 | cfg->cmd_data.help.cfg_component->plugin_name->str, |
| 1086 | cfg->cmd_data.help.cfg_component->comp_cls_name->str, |
| 1087 | cfg->cmd_data.help.cfg_component->type); |
| 1088 | fprintf(stderr, "\n%s%sCannot find component class %s", |
| 1089 | bt_common_color_bold(), |
| 1090 | bt_common_color_fg_red(), |
| 1091 | bt_common_color_reset()); |
| 1092 | print_plugin_comp_cls_opt(stderr, |
| 1093 | cfg->cmd_data.help.cfg_component->plugin_name->str, |
| 1094 | cfg->cmd_data.help.cfg_component->comp_cls_name->str, |
| 1095 | cfg->cmd_data.help.cfg_component->type); |
| 1096 | fprintf(stderr, "\n"); |
| 1097 | ret = -1; |
| 1098 | goto end; |
| 1099 | } |
| 1100 | |
| 1101 | printf("\n"); |
| 1102 | print_component_class_help( |
| 1103 | cfg->cmd_data.help.cfg_component->plugin_name->str, |
| 1104 | needed_comp_cls); |
| 1105 | |
| 1106 | end: |
| 1107 | bt_component_class_put_ref(needed_comp_cls); |
| 1108 | bt_plugin_put_ref(plugin); |
| 1109 | return ret; |
| 1110 | } |
| 1111 | |
| 1112 | typedef void *(* plugin_borrow_comp_cls_by_index_func_t)(const bt_plugin *, |
| 1113 | uint64_t); |
| 1114 | typedef const bt_component_class *(* spec_comp_cls_borrow_comp_cls_func_t)( |
| 1115 | void *); |
| 1116 | |
| 1117 | void cmd_list_plugins_print_component_classes(const bt_plugin *plugin, |
| 1118 | const char *cc_type_name, uint64_t count, |
| 1119 | plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func, |
| 1120 | spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func) |
| 1121 | { |
| 1122 | uint64_t i; |
| 1123 | |
| 1124 | if (count == 0) { |
| 1125 | printf(" %s%s component classes%s: (none)\n", |
| 1126 | bt_common_color_bold(), |
| 1127 | cc_type_name, |
| 1128 | bt_common_color_reset()); |
| 1129 | goto end; |
| 1130 | } else { |
| 1131 | printf(" %s%s component classes%s:\n", |
| 1132 | bt_common_color_bold(), |
| 1133 | cc_type_name, |
| 1134 | bt_common_color_reset()); |
| 1135 | } |
| 1136 | |
| 1137 | for (i = 0; i < count; i++) { |
| 1138 | const bt_component_class *comp_class = |
| 1139 | spec_comp_cls_borrow_comp_cls_func( |
| 1140 | borrow_comp_cls_by_index_func(plugin, i)); |
| 1141 | const char *comp_class_name = |
| 1142 | bt_component_class_get_name(comp_class); |
| 1143 | const char *comp_class_description = |
| 1144 | bt_component_class_get_description(comp_class); |
| 1145 | bt_component_class_type type = |
| 1146 | bt_component_class_get_type(comp_class); |
| 1147 | |
| 1148 | printf(" "); |
| 1149 | print_plugin_comp_cls_opt(stdout, |
| 1150 | bt_plugin_get_name(plugin), comp_class_name, |
| 1151 | type); |
| 1152 | |
| 1153 | if (comp_class_description) { |
| 1154 | printf(": %s", comp_class_description); |
| 1155 | } |
| 1156 | |
| 1157 | printf("\n"); |
| 1158 | } |
| 1159 | |
| 1160 | end: |
| 1161 | return; |
| 1162 | } |
| 1163 | |
| 1164 | static |
| 1165 | int cmd_list_plugins(struct bt_config *cfg) |
| 1166 | { |
| 1167 | int ret = 0; |
| 1168 | int plugins_count, component_classes_count = 0, i; |
| 1169 | |
| 1170 | printf("From the following plugin paths:\n\n"); |
| 1171 | print_value(stdout, cfg->plugin_paths, 2); |
| 1172 | printf("\n"); |
| 1173 | plugins_count = loaded_plugins->len; |
| 1174 | if (plugins_count == 0) { |
| 1175 | printf("No plugins found.\n"); |
| 1176 | goto end; |
| 1177 | } |
| 1178 | |
| 1179 | for (i = 0; i < plugins_count; i++) { |
| 1180 | const bt_plugin *plugin = g_ptr_array_index(loaded_plugins, i); |
| 1181 | |
| 1182 | component_classes_count += |
| 1183 | bt_plugin_get_source_component_class_count(plugin) + |
| 1184 | bt_plugin_get_filter_component_class_count(plugin) + |
| 1185 | bt_plugin_get_sink_component_class_count(plugin); |
| 1186 | } |
| 1187 | |
| 1188 | printf("Found %s%d%s component classes in %s%d%s plugins.\n", |
| 1189 | bt_common_color_bold(), |
| 1190 | component_classes_count, |
| 1191 | bt_common_color_reset(), |
| 1192 | bt_common_color_bold(), |
| 1193 | plugins_count, |
| 1194 | bt_common_color_reset()); |
| 1195 | |
| 1196 | for (i = 0; i < plugins_count; i++) { |
| 1197 | const bt_plugin *plugin = g_ptr_array_index(loaded_plugins, i); |
| 1198 | |
| 1199 | printf("\n"); |
| 1200 | print_plugin_info(plugin); |
| 1201 | cmd_list_plugins_print_component_classes(plugin, "Source", |
| 1202 | bt_plugin_get_source_component_class_count(plugin), |
| 1203 | (plugin_borrow_comp_cls_by_index_func_t) |
| 1204 | bt_plugin_borrow_source_component_class_by_index_const, |
| 1205 | (spec_comp_cls_borrow_comp_cls_func_t) |
| 1206 | bt_component_class_source_as_component_class); |
| 1207 | cmd_list_plugins_print_component_classes(plugin, "Filter", |
| 1208 | bt_plugin_get_filter_component_class_count(plugin), |
| 1209 | (plugin_borrow_comp_cls_by_index_func_t) |
| 1210 | bt_plugin_borrow_filter_component_class_by_index_const, |
| 1211 | (spec_comp_cls_borrow_comp_cls_func_t) |
| 1212 | bt_component_class_filter_as_component_class); |
| 1213 | cmd_list_plugins_print_component_classes(plugin, "Sink", |
| 1214 | bt_plugin_get_sink_component_class_count(plugin), |
| 1215 | (plugin_borrow_comp_cls_by_index_func_t) |
| 1216 | bt_plugin_borrow_sink_component_class_by_index_const, |
| 1217 | (spec_comp_cls_borrow_comp_cls_func_t) |
| 1218 | bt_component_class_sink_as_component_class); |
| 1219 | } |
| 1220 | |
| 1221 | end: |
| 1222 | return ret; |
| 1223 | } |
| 1224 | |
| 1225 | static |
| 1226 | int cmd_print_lttng_live_sessions(struct bt_config *cfg) |
| 1227 | { |
| 1228 | int ret = 0; |
| 1229 | const bt_component_class *comp_cls = NULL; |
| 1230 | const bt_value *results = NULL; |
| 1231 | bt_value *params = NULL; |
| 1232 | const bt_value *map = NULL; |
| 1233 | const bt_value *v = NULL; |
| 1234 | static const char * const plugin_name = "ctf"; |
| 1235 | static const char * const comp_cls_name = "lttng-live"; |
| 1236 | static const bt_component_class_type comp_cls_type = |
| 1237 | BT_COMPONENT_CLASS_TYPE_SOURCE; |
| 1238 | int64_t array_size, i; |
| 1239 | const char *fail_reason = NULL; |
| 1240 | FILE *out_stream = stdout; |
| 1241 | |
| 1242 | BT_ASSERT(cfg->cmd_data.print_lttng_live_sessions.url); |
| 1243 | comp_cls = find_component_class(plugin_name, comp_cls_name, |
| 1244 | comp_cls_type); |
| 1245 | if (!comp_cls) { |
| 1246 | BT_LOGE("Cannot find component class: plugin-name=\"%s\", " |
| 1247 | "comp-cls-name=\"%s\", comp-cls-type=%d", |
| 1248 | plugin_name, comp_cls_name, |
| 1249 | BT_COMPONENT_CLASS_TYPE_SOURCE); |
| 1250 | fprintf(stderr, "%s%sCannot find component class %s", |
| 1251 | bt_common_color_bold(), |
| 1252 | bt_common_color_fg_red(), |
| 1253 | bt_common_color_reset()); |
| 1254 | print_plugin_comp_cls_opt(stderr, plugin_name, |
| 1255 | comp_cls_name, comp_cls_type); |
| 1256 | fprintf(stderr, "\n"); |
| 1257 | goto error; |
| 1258 | } |
| 1259 | |
| 1260 | params = bt_value_map_create(); |
| 1261 | if (!params) { |
| 1262 | goto error; |
| 1263 | } |
| 1264 | |
| 1265 | ret = bt_value_map_insert_string_entry(params, "url", |
| 1266 | cfg->cmd_data.print_lttng_live_sessions.url->str); |
| 1267 | if (ret) { |
| 1268 | goto error; |
| 1269 | } |
| 1270 | |
| 1271 | ret = query(cfg, comp_cls, "sessions", params, |
| 1272 | &results, &fail_reason); |
| 1273 | if (ret) { |
| 1274 | goto failed; |
| 1275 | } |
| 1276 | |
| 1277 | BT_ASSERT(results); |
| 1278 | |
| 1279 | if (!bt_value_is_array(results)) { |
| 1280 | BT_LOGE_STR("Expecting an array for sessions query."); |
| 1281 | fprintf(stderr, "%s%sUnexpected type returned by session query%s\n", |
| 1282 | bt_common_color_bold(), |
| 1283 | bt_common_color_fg_red(), |
| 1284 | bt_common_color_reset()); |
| 1285 | goto error; |
| 1286 | } |
| 1287 | |
| 1288 | if (cfg->cmd_data.print_lttng_live_sessions.output_path->len > 0) { |
| 1289 | out_stream = |
| 1290 | fopen(cfg->cmd_data.print_lttng_live_sessions.output_path->str, |
| 1291 | "w"); |
| 1292 | if (!out_stream) { |
| 1293 | ret = -1; |
| 1294 | BT_LOGE_ERRNO("Cannot open file for writing", |
| 1295 | ": path=\"%s\"", |
| 1296 | cfg->cmd_data.print_lttng_live_sessions.output_path->str); |
| 1297 | goto end; |
| 1298 | } |
| 1299 | } |
| 1300 | |
| 1301 | array_size = bt_value_array_get_size(results); |
| 1302 | for (i = 0; i < array_size; i++) { |
| 1303 | const char *url_text; |
| 1304 | int64_t timer_us, streams, clients; |
| 1305 | |
| 1306 | map = bt_value_array_borrow_element_by_index_const(results, i); |
| 1307 | if (!map) { |
| 1308 | BT_LOGE_STR("Unexpected empty array entry."); |
| 1309 | goto error; |
| 1310 | } |
| 1311 | if (!bt_value_is_map(map)) { |
| 1312 | BT_LOGE_STR("Unexpected entry type."); |
| 1313 | goto error; |
| 1314 | } |
| 1315 | |
| 1316 | v = bt_value_map_borrow_entry_value_const(map, "url"); |
| 1317 | if (!v) { |
| 1318 | BT_LOGE_STR("Unexpected empty array \"url\" entry."); |
| 1319 | goto error; |
| 1320 | } |
| 1321 | url_text = bt_value_string_get(v); |
| 1322 | fprintf(out_stream, "%s", url_text); |
| 1323 | v = bt_value_map_borrow_entry_value_const(map, "timer-us"); |
| 1324 | if (!v) { |
| 1325 | BT_LOGE_STR("Unexpected empty array \"timer-us\" entry."); |
| 1326 | goto error; |
| 1327 | } |
| 1328 | timer_us = bt_value_signed_integer_get(v); |
| 1329 | fprintf(out_stream, " (timer = %" PRIu64 ", ", timer_us); |
| 1330 | v = bt_value_map_borrow_entry_value_const(map, "stream-count"); |
| 1331 | if (!v) { |
| 1332 | BT_LOGE_STR("Unexpected empty array \"stream-count\" entry."); |
| 1333 | goto error; |
| 1334 | } |
| 1335 | streams = bt_value_signed_integer_get(v); |
| 1336 | fprintf(out_stream, "%" PRIu64 " stream(s), ", streams); |
| 1337 | v = bt_value_map_borrow_entry_value_const(map, "client-count"); |
| 1338 | if (!v) { |
| 1339 | BT_LOGE_STR("Unexpected empty array \"client-count\" entry."); |
| 1340 | goto error; |
| 1341 | } |
| 1342 | clients = bt_value_signed_integer_get(v); |
| 1343 | fprintf(out_stream, "%" PRIu64 " client(s) connected)\n", clients); |
| 1344 | } |
| 1345 | |
| 1346 | goto end; |
| 1347 | |
| 1348 | failed: |
| 1349 | BT_LOGE("Failed to query for sessions: %s", fail_reason); |
| 1350 | fprintf(stderr, "%s%sFailed to request sessions: %s%s\n", |
| 1351 | bt_common_color_bold(), |
| 1352 | bt_common_color_fg_red(), |
| 1353 | fail_reason, |
| 1354 | bt_common_color_reset()); |
| 1355 | |
| 1356 | error: |
| 1357 | ret = -1; |
| 1358 | |
| 1359 | end: |
| 1360 | bt_value_put_ref(results); |
| 1361 | bt_value_put_ref(params); |
| 1362 | bt_component_class_put_ref(comp_cls); |
| 1363 | |
| 1364 | if (out_stream && out_stream != stdout) { |
| 1365 | int fclose_ret = fclose(out_stream); |
| 1366 | |
| 1367 | if (fclose_ret) { |
| 1368 | BT_LOGE_ERRNO("Cannot close file stream", |
| 1369 | ": path=\"%s\"", |
| 1370 | cfg->cmd_data.print_lttng_live_sessions.output_path->str); |
| 1371 | } |
| 1372 | } |
| 1373 | |
| 1374 | return ret; |
| 1375 | } |
| 1376 | |
| 1377 | static |
| 1378 | int cmd_print_ctf_metadata(struct bt_config *cfg) |
| 1379 | { |
| 1380 | int ret = 0; |
| 1381 | const bt_component_class *comp_cls = NULL; |
| 1382 | const bt_value *results = NULL; |
| 1383 | bt_value *params = NULL; |
| 1384 | const bt_value *metadata_text_value = NULL; |
| 1385 | const char *metadata_text = NULL; |
| 1386 | static const char * const plugin_name = "ctf"; |
| 1387 | static const char * const comp_cls_name = "fs"; |
| 1388 | static const bt_component_class_type comp_cls_type = |
| 1389 | BT_COMPONENT_CLASS_TYPE_SOURCE; |
| 1390 | const char *fail_reason = NULL; |
| 1391 | FILE *out_stream = stdout; |
| 1392 | |
| 1393 | BT_ASSERT(cfg->cmd_data.print_ctf_metadata.path); |
| 1394 | comp_cls = find_component_class(plugin_name, comp_cls_name, |
| 1395 | comp_cls_type); |
| 1396 | if (!comp_cls) { |
| 1397 | BT_LOGE("Cannot find component class: plugin-name=\"%s\", " |
| 1398 | "comp-cls-name=\"%s\", comp-cls-type=%d", |
| 1399 | plugin_name, comp_cls_name, |
| 1400 | BT_COMPONENT_CLASS_TYPE_SOURCE); |
| 1401 | fprintf(stderr, "%s%sCannot find component class %s", |
| 1402 | bt_common_color_bold(), |
| 1403 | bt_common_color_fg_red(), |
| 1404 | bt_common_color_reset()); |
| 1405 | print_plugin_comp_cls_opt(stderr, plugin_name, |
| 1406 | comp_cls_name, comp_cls_type); |
| 1407 | fprintf(stderr, "\n"); |
| 1408 | ret = -1; |
| 1409 | goto end; |
| 1410 | } |
| 1411 | |
| 1412 | params = bt_value_map_create(); |
| 1413 | if (!params) { |
| 1414 | ret = -1; |
| 1415 | goto end; |
| 1416 | } |
| 1417 | |
| 1418 | ret = bt_value_map_insert_string_entry(params, "path", |
| 1419 | cfg->cmd_data.print_ctf_metadata.path->str); |
| 1420 | if (ret) { |
| 1421 | ret = -1; |
| 1422 | goto end; |
| 1423 | } |
| 1424 | |
| 1425 | ret = query(cfg, comp_cls, "metadata-info", |
| 1426 | params, &results, &fail_reason); |
| 1427 | if (ret) { |
| 1428 | goto failed; |
| 1429 | } |
| 1430 | |
| 1431 | metadata_text_value = bt_value_map_borrow_entry_value_const(results, |
| 1432 | "text"); |
| 1433 | if (!metadata_text_value) { |
| 1434 | BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object."); |
| 1435 | ret = -1; |
| 1436 | goto end; |
| 1437 | } |
| 1438 | |
| 1439 | metadata_text = bt_value_string_get(metadata_text_value); |
| 1440 | |
| 1441 | if (cfg->cmd_data.print_ctf_metadata.output_path->len > 0) { |
| 1442 | out_stream = |
| 1443 | fopen(cfg->cmd_data.print_ctf_metadata.output_path->str, |
| 1444 | "w"); |
| 1445 | if (!out_stream) { |
| 1446 | ret = -1; |
| 1447 | BT_LOGE_ERRNO("Cannot open file for writing", |
| 1448 | ": path=\"%s\"", |
| 1449 | cfg->cmd_data.print_ctf_metadata.output_path->str); |
| 1450 | goto end; |
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | ret = fprintf(out_stream, "%s\n", metadata_text); |
| 1455 | if (ret < 0) { |
| 1456 | BT_LOGE("Cannot write whole metadata text to output stream: " |
| 1457 | "ret=%d", ret); |
| 1458 | goto end; |
| 1459 | } |
| 1460 | |
| 1461 | ret = 0; |
| 1462 | |
| 1463 | goto end; |
| 1464 | |
| 1465 | failed: |
| 1466 | ret = -1; |
| 1467 | BT_LOGE("Failed to query for metadata info: %s", fail_reason); |
| 1468 | fprintf(stderr, "%s%sFailed to request metadata info: %s%s\n", |
| 1469 | bt_common_color_bold(), |
| 1470 | bt_common_color_fg_red(), |
| 1471 | fail_reason, |
| 1472 | bt_common_color_reset()); |
| 1473 | |
| 1474 | end: |
| 1475 | bt_value_put_ref(results); |
| 1476 | bt_value_put_ref(params); |
| 1477 | bt_component_class_put_ref(comp_cls); |
| 1478 | |
| 1479 | if (out_stream && out_stream != stdout) { |
| 1480 | int fclose_ret = fclose(out_stream); |
| 1481 | |
| 1482 | if (fclose_ret) { |
| 1483 | BT_LOGE_ERRNO("Cannot close file stream", |
| 1484 | ": path=\"%s\"", |
| 1485 | cfg->cmd_data.print_ctf_metadata.output_path->str); |
| 1486 | } |
| 1487 | } |
| 1488 | |
| 1489 | return ret; |
| 1490 | } |
| 1491 | |
| 1492 | struct port_id { |
| 1493 | char *instance_name; |
| 1494 | char *port_name; |
| 1495 | }; |
| 1496 | |
| 1497 | struct trace_range { |
| 1498 | uint64_t intersection_range_begin_ns; |
| 1499 | uint64_t intersection_range_end_ns; |
| 1500 | }; |
| 1501 | |
| 1502 | static |
| 1503 | guint port_id_hash(gconstpointer v) |
| 1504 | { |
| 1505 | const struct port_id *id = v; |
| 1506 | |
| 1507 | BT_ASSERT(id->instance_name); |
| 1508 | BT_ASSERT(id->port_name); |
| 1509 | |
| 1510 | return g_str_hash(id->instance_name) ^ g_str_hash(id->port_name); |
| 1511 | } |
| 1512 | |
| 1513 | static |
| 1514 | gboolean port_id_equal(gconstpointer v1, gconstpointer v2) |
| 1515 | { |
| 1516 | const struct port_id *id1 = v1; |
| 1517 | const struct port_id *id2 = v2; |
| 1518 | |
| 1519 | return !strcmp(id1->instance_name, id2->instance_name) && |
| 1520 | !strcmp(id1->port_name, id2->port_name); |
| 1521 | } |
| 1522 | |
| 1523 | static |
| 1524 | void port_id_destroy(gpointer data) |
| 1525 | { |
| 1526 | struct port_id *id = data; |
| 1527 | |
| 1528 | free(id->instance_name); |
| 1529 | free(id->port_name); |
| 1530 | free(id); |
| 1531 | } |
| 1532 | |
| 1533 | static |
| 1534 | void trace_range_destroy(gpointer data) |
| 1535 | { |
| 1536 | free(data); |
| 1537 | } |
| 1538 | |
| 1539 | struct cmd_run_ctx { |
| 1540 | /* Owned by this */ |
| 1541 | GHashTable *src_components; |
| 1542 | |
| 1543 | /* Owned by this */ |
| 1544 | GHashTable *flt_components; |
| 1545 | |
| 1546 | /* Owned by this */ |
| 1547 | GHashTable *sink_components; |
| 1548 | |
| 1549 | /* Owned by this */ |
| 1550 | bt_graph *graph; |
| 1551 | |
| 1552 | /* Weak */ |
| 1553 | struct bt_config *cfg; |
| 1554 | |
| 1555 | bool connect_ports; |
| 1556 | |
| 1557 | bool stream_intersection_mode; |
| 1558 | |
| 1559 | /* |
| 1560 | * Association of struct port_id -> struct trace_range. |
| 1561 | */ |
| 1562 | GHashTable *intersections; |
| 1563 | }; |
| 1564 | |
| 1565 | /* Returns a timestamp of the form "(-)s.ns" */ |
| 1566 | static |
| 1567 | char *s_from_ns(int64_t ns) |
| 1568 | { |
| 1569 | int ret; |
| 1570 | char *s_ret = NULL; |
| 1571 | bool is_negative; |
| 1572 | int64_t ts_sec_abs, ts_nsec_abs; |
| 1573 | int64_t ts_sec = ns / NSEC_PER_SEC; |
| 1574 | int64_t ts_nsec = ns % NSEC_PER_SEC; |
| 1575 | |
| 1576 | if (ts_sec >= 0 && ts_nsec >= 0) { |
| 1577 | is_negative = false; |
| 1578 | ts_sec_abs = ts_sec; |
| 1579 | ts_nsec_abs = ts_nsec; |
| 1580 | } else if (ts_sec > 0 && ts_nsec < 0) { |
| 1581 | is_negative = false; |
| 1582 | ts_sec_abs = ts_sec - 1; |
| 1583 | ts_nsec_abs = NSEC_PER_SEC + ts_nsec; |
| 1584 | } else if (ts_sec == 0 && ts_nsec < 0) { |
| 1585 | is_negative = true; |
| 1586 | ts_sec_abs = ts_sec; |
| 1587 | ts_nsec_abs = -ts_nsec; |
| 1588 | } else if (ts_sec < 0 && ts_nsec > 0) { |
| 1589 | is_negative = true; |
| 1590 | ts_sec_abs = -(ts_sec + 1); |
| 1591 | ts_nsec_abs = NSEC_PER_SEC - ts_nsec; |
| 1592 | } else if (ts_sec < 0 && ts_nsec == 0) { |
| 1593 | is_negative = true; |
| 1594 | ts_sec_abs = -ts_sec; |
| 1595 | ts_nsec_abs = ts_nsec; |
| 1596 | } else { /* (ts_sec < 0 && ts_nsec < 0) */ |
| 1597 | is_negative = true; |
| 1598 | ts_sec_abs = -ts_sec; |
| 1599 | ts_nsec_abs = -ts_nsec; |
| 1600 | } |
| 1601 | |
| 1602 | ret = asprintf(&s_ret, "%s%" PRId64 ".%09" PRId64, |
| 1603 | is_negative ? "-" : "", ts_sec_abs, ts_nsec_abs); |
| 1604 | if (ret < 0) { |
| 1605 | s_ret = NULL; |
| 1606 | } |
| 1607 | return s_ret; |
| 1608 | } |
| 1609 | |
| 1610 | static |
| 1611 | int cmd_run_ctx_connect_upstream_port_to_downstream_component( |
| 1612 | struct cmd_run_ctx *ctx, |
| 1613 | const bt_component *upstream_comp, |
| 1614 | const bt_port_output *out_upstream_port, |
| 1615 | struct bt_config_connection *cfg_conn) |
| 1616 | { |
| 1617 | typedef uint64_t (*input_port_count_func_t)(void *); |
| 1618 | typedef const bt_port_input *(*borrow_input_port_by_index_func_t)( |
| 1619 | const void *, uint64_t); |
| 1620 | const bt_port *upstream_port = |
| 1621 | bt_port_output_as_port_const(out_upstream_port); |
| 1622 | |
| 1623 | int ret = 0; |
| 1624 | GQuark downstreamp_comp_name_quark; |
| 1625 | void *downstream_comp; |
| 1626 | uint64_t downstream_port_count; |
| 1627 | uint64_t i; |
| 1628 | input_port_count_func_t port_count_fn; |
| 1629 | borrow_input_port_by_index_func_t port_by_index_fn; |
| 1630 | bt_graph_connect_ports_status connect_ports_status = |
| 1631 | BT_GRAPH_CONNECT_PORTS_STATUS_OK; |
| 1632 | bool insert_trimmer = false; |
| 1633 | bt_value *trimmer_params = NULL; |
| 1634 | char *intersection_begin = NULL; |
| 1635 | char *intersection_end = NULL; |
| 1636 | const bt_component_filter *trimmer = NULL; |
| 1637 | const bt_component_class_filter *trimmer_class = NULL; |
| 1638 | const bt_port_input *trimmer_input = NULL; |
| 1639 | const bt_port_output *trimmer_output = NULL; |
| 1640 | |
| 1641 | if (ctx->intersections && |
| 1642 | bt_component_get_class_type(upstream_comp) == |
| 1643 | BT_COMPONENT_CLASS_TYPE_SOURCE) { |
| 1644 | struct trace_range *range; |
| 1645 | struct port_id port_id = { |
| 1646 | .instance_name = (char *) bt_component_get_name(upstream_comp), |
| 1647 | .port_name = (char *) bt_port_get_name(upstream_port) |
| 1648 | }; |
| 1649 | |
| 1650 | if (!port_id.instance_name || !port_id.port_name) { |
| 1651 | goto error; |
| 1652 | } |
| 1653 | |
| 1654 | range = (struct trace_range *) g_hash_table_lookup( |
| 1655 | ctx->intersections, &port_id); |
| 1656 | if (range) { |
| 1657 | bt_value_map_insert_entry_status insert_status; |
| 1658 | |
| 1659 | intersection_begin = s_from_ns( |
| 1660 | range->intersection_range_begin_ns); |
| 1661 | intersection_end = s_from_ns( |
| 1662 | range->intersection_range_end_ns); |
| 1663 | if (!intersection_begin || !intersection_end) { |
| 1664 | BT_LOGE_STR("Cannot create trimmer argument timestamp string."); |
| 1665 | goto error; |
| 1666 | } |
| 1667 | |
| 1668 | insert_trimmer = true; |
| 1669 | trimmer_params = bt_value_map_create(); |
| 1670 | if (!trimmer_params) { |
| 1671 | goto error; |
| 1672 | } |
| 1673 | |
| 1674 | insert_status = bt_value_map_insert_string_entry( |
| 1675 | trimmer_params, "begin", intersection_begin); |
| 1676 | if (insert_status < 0) { |
| 1677 | goto error; |
| 1678 | } |
| 1679 | insert_status = bt_value_map_insert_string_entry( |
| 1680 | trimmer_params, |
| 1681 | "end", intersection_end); |
| 1682 | if (insert_status < 0) { |
| 1683 | goto error; |
| 1684 | } |
| 1685 | } |
| 1686 | |
| 1687 | trimmer_class = find_filter_component_class("utils", "trimmer"); |
| 1688 | if (!trimmer_class) { |
| 1689 | goto error; |
| 1690 | } |
| 1691 | } |
| 1692 | |
| 1693 | BT_LOGI("Connecting upstream port to the next available downstream port: " |
| 1694 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " |
| 1695 | "downstream-comp-name=\"%s\", conn-arg=\"%s\"", |
| 1696 | upstream_port, bt_port_get_name(upstream_port), |
| 1697 | cfg_conn->downstream_comp_name->str, |
| 1698 | cfg_conn->arg->str); |
| 1699 | downstreamp_comp_name_quark = g_quark_from_string( |
| 1700 | cfg_conn->downstream_comp_name->str); |
| 1701 | BT_ASSERT(downstreamp_comp_name_quark > 0); |
| 1702 | downstream_comp = g_hash_table_lookup(ctx->flt_components, |
| 1703 | GUINT_TO_POINTER(downstreamp_comp_name_quark)); |
| 1704 | port_count_fn = (input_port_count_func_t) |
| 1705 | bt_component_filter_get_input_port_count; |
| 1706 | port_by_index_fn = (borrow_input_port_by_index_func_t) |
| 1707 | bt_component_filter_borrow_input_port_by_index_const; |
| 1708 | |
| 1709 | if (!downstream_comp) { |
| 1710 | downstream_comp = g_hash_table_lookup(ctx->sink_components, |
| 1711 | GUINT_TO_POINTER(downstreamp_comp_name_quark)); |
| 1712 | port_count_fn = (input_port_count_func_t) |
| 1713 | bt_component_sink_get_input_port_count; |
| 1714 | port_by_index_fn = (borrow_input_port_by_index_func_t) |
| 1715 | bt_component_sink_borrow_input_port_by_index_const; |
| 1716 | } |
| 1717 | |
| 1718 | if (!downstream_comp) { |
| 1719 | BT_LOGE("Cannot find downstream component: comp-name=\"%s\", " |
| 1720 | "conn-arg=\"%s\"", cfg_conn->downstream_comp_name->str, |
| 1721 | cfg_conn->arg->str); |
| 1722 | fprintf(stderr, "Cannot create connection: cannot find downstream component: %s\n", |
| 1723 | cfg_conn->arg->str); |
| 1724 | goto error; |
| 1725 | } |
| 1726 | |
| 1727 | downstream_port_count = port_count_fn(downstream_comp); |
| 1728 | |
| 1729 | for (i = 0; i < downstream_port_count; i++) { |
| 1730 | const bt_port_input *in_downstream_port = |
| 1731 | port_by_index_fn(downstream_comp, i); |
| 1732 | const bt_port *downstream_port = |
| 1733 | bt_port_input_as_port_const(in_downstream_port); |
| 1734 | const char *upstream_port_name; |
| 1735 | const char *downstream_port_name; |
| 1736 | |
| 1737 | BT_ASSERT(downstream_port); |
| 1738 | |
| 1739 | /* Skip port if it's already connected. */ |
| 1740 | if (bt_port_is_connected(downstream_port)) { |
| 1741 | BT_LOGI("Skipping downstream port: already connected: " |
| 1742 | "port-addr=%p, port-name=\"%s\"", |
| 1743 | downstream_port, |
| 1744 | bt_port_get_name(downstream_port)); |
| 1745 | continue; |
| 1746 | } |
| 1747 | |
| 1748 | downstream_port_name = bt_port_get_name(downstream_port); |
| 1749 | BT_ASSERT(downstream_port_name); |
| 1750 | upstream_port_name = bt_port_get_name(upstream_port); |
| 1751 | BT_ASSERT(upstream_port_name); |
| 1752 | |
| 1753 | if (!bt_common_star_glob_match( |
| 1754 | cfg_conn->downstream_port_glob->str, SIZE_MAX, |
| 1755 | downstream_port_name, SIZE_MAX)) { |
| 1756 | continue; |
| 1757 | } |
| 1758 | |
| 1759 | if (insert_trimmer) { |
| 1760 | /* |
| 1761 | * In order to insert the trimmer between the |
| 1762 | * two components that were being connected, we |
| 1763 | * create a connection configuration entry which |
| 1764 | * describes a connection from the trimmer's |
| 1765 | * output to the original input that was being |
| 1766 | * connected. |
| 1767 | * |
| 1768 | * Hence, the creation of the trimmer will cause |
| 1769 | * the graph "new port" listener to establish |
| 1770 | * all downstream connections as its output port |
| 1771 | * is connected. We will then establish the |
| 1772 | * connection between the original upstream |
| 1773 | * source and the trimmer. |
| 1774 | */ |
| 1775 | char *trimmer_name = NULL; |
| 1776 | bt_graph_add_component_status add_comp_status; |
| 1777 | |
| 1778 | ret = asprintf(&trimmer_name, |
| 1779 | "stream-intersection-trimmer-%s", |
| 1780 | upstream_port_name); |
| 1781 | if (ret < 0) { |
| 1782 | goto error; |
| 1783 | } |
| 1784 | ret = 0; |
| 1785 | |
| 1786 | ctx->connect_ports = false; |
| 1787 | add_comp_status = bt_graph_add_filter_component( |
| 1788 | ctx->graph, trimmer_class, trimmer_name, |
| 1789 | trimmer_params, ctx->cfg->log_level, |
| 1790 | &trimmer); |
| 1791 | free(trimmer_name); |
| 1792 | if (add_comp_status != |
| 1793 | BT_GRAPH_ADD_COMPONENT_STATUS_OK) { |
| 1794 | goto error; |
| 1795 | } |
| 1796 | BT_ASSERT(trimmer); |
| 1797 | |
| 1798 | trimmer_input = |
| 1799 | bt_component_filter_borrow_input_port_by_index_const( |
| 1800 | trimmer, 0); |
| 1801 | if (!trimmer_input) { |
| 1802 | goto error; |
| 1803 | } |
| 1804 | trimmer_output = |
| 1805 | bt_component_filter_borrow_output_port_by_index_const( |
| 1806 | trimmer, 0); |
| 1807 | if (!trimmer_output) { |
| 1808 | goto error; |
| 1809 | } |
| 1810 | |
| 1811 | /* |
| 1812 | * Replace the current downstream port by the trimmer's |
| 1813 | * upstream port. |
| 1814 | */ |
| 1815 | in_downstream_port = trimmer_input; |
| 1816 | downstream_port = |
| 1817 | bt_port_input_as_port_const(in_downstream_port); |
| 1818 | downstream_port_name = bt_port_get_name( |
| 1819 | downstream_port); |
| 1820 | BT_ASSERT(downstream_port_name); |
| 1821 | } |
| 1822 | |
| 1823 | /* We have a winner! */ |
| 1824 | connect_ports_status = bt_graph_connect_ports(ctx->graph, |
| 1825 | out_upstream_port, in_downstream_port, NULL); |
| 1826 | downstream_port = NULL; |
| 1827 | switch (connect_ports_status) { |
| 1828 | case BT_GRAPH_CONNECT_PORTS_STATUS_OK: |
| 1829 | break; |
| 1830 | case BT_GRAPH_CONNECT_PORTS_STATUS_CANCELED: |
| 1831 | BT_LOGI_STR("Graph was canceled by user."); |
| 1832 | break; |
| 1833 | default: |
| 1834 | BT_LOGE("Cannot create connection: graph refuses to connect ports: " |
| 1835 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " |
| 1836 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " |
| 1837 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\", " |
| 1838 | "downstream-port-addr=%p, downstream-port-name=\"%s\", " |
| 1839 | "conn-arg=\"%s\"", |
| 1840 | upstream_comp, bt_component_get_name(upstream_comp), |
| 1841 | upstream_port, bt_port_get_name(upstream_port), |
| 1842 | downstream_comp, cfg_conn->downstream_comp_name->str, |
| 1843 | downstream_port, downstream_port_name, |
| 1844 | cfg_conn->arg->str); |
| 1845 | fprintf(stderr, |
| 1846 | "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n", |
| 1847 | bt_port_get_name(upstream_port), |
| 1848 | downstream_port_name, |
| 1849 | cfg_conn->arg->str); |
| 1850 | goto error; |
| 1851 | } |
| 1852 | |
| 1853 | BT_LOGI("Connected component ports: " |
| 1854 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " |
| 1855 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " |
| 1856 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\", " |
| 1857 | "downstream-port-addr=%p, downstream-port-name=\"%s\", " |
| 1858 | "conn-arg=\"%s\"", |
| 1859 | upstream_comp, bt_component_get_name(upstream_comp), |
| 1860 | upstream_port, bt_port_get_name(upstream_port), |
| 1861 | downstream_comp, cfg_conn->downstream_comp_name->str, |
| 1862 | downstream_port, downstream_port_name, |
| 1863 | cfg_conn->arg->str); |
| 1864 | |
| 1865 | if (insert_trimmer) { |
| 1866 | /* |
| 1867 | * The first connection, from the source to the trimmer, |
| 1868 | * has been done. We now connect the trimmer to the |
| 1869 | * original downstream port. |
| 1870 | */ |
| 1871 | ret = cmd_run_ctx_connect_upstream_port_to_downstream_component( |
| 1872 | ctx, |
| 1873 | bt_component_filter_as_component_const(trimmer), |
| 1874 | trimmer_output, cfg_conn); |
| 1875 | if (ret) { |
| 1876 | goto error; |
| 1877 | } |
| 1878 | ctx->connect_ports = true; |
| 1879 | } |
| 1880 | |
| 1881 | /* |
| 1882 | * We found a matching downstream port: the search is |
| 1883 | * over. |
| 1884 | */ |
| 1885 | goto end; |
| 1886 | } |
| 1887 | |
| 1888 | /* No downstream port found */ |
| 1889 | BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: " |
| 1890 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " |
| 1891 | "downstream-comp-name=\"%s\", conn-arg=\"%s\"", |
| 1892 | upstream_port, bt_port_get_name(upstream_port), |
| 1893 | cfg_conn->downstream_comp_name->str, |
| 1894 | cfg_conn->arg->str); |
| 1895 | fprintf(stderr, |
| 1896 | "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n", |
| 1897 | bt_port_get_name(upstream_port), cfg_conn->arg->str); |
| 1898 | |
| 1899 | error: |
| 1900 | ret = -1; |
| 1901 | |
| 1902 | end: |
| 1903 | free(intersection_begin); |
| 1904 | free(intersection_end); |
| 1905 | BT_VALUE_PUT_REF_AND_RESET(trimmer_params); |
| 1906 | BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class); |
| 1907 | BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer); |
| 1908 | return ret; |
| 1909 | } |
| 1910 | |
| 1911 | static |
| 1912 | int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx *ctx, |
| 1913 | const bt_port_output *upstream_port) |
| 1914 | { |
| 1915 | int ret = 0; |
| 1916 | const char *upstream_port_name; |
| 1917 | const char *upstream_comp_name; |
| 1918 | const bt_component *upstream_comp = NULL; |
| 1919 | size_t i; |
| 1920 | |
| 1921 | BT_ASSERT(ctx); |
| 1922 | BT_ASSERT(upstream_port); |
| 1923 | upstream_port_name = bt_port_get_name( |
| 1924 | bt_port_output_as_port_const(upstream_port)); |
| 1925 | BT_ASSERT(upstream_port_name); |
| 1926 | upstream_comp = bt_port_borrow_component_const( |
| 1927 | bt_port_output_as_port_const(upstream_port)); |
| 1928 | if (!upstream_comp) { |
| 1929 | BT_LOGW("Upstream port to connect is not part of a component: " |
| 1930 | "port-addr=%p, port-name=\"%s\"", |
| 1931 | upstream_port, upstream_port_name); |
| 1932 | ret = -1; |
| 1933 | goto end; |
| 1934 | } |
| 1935 | |
| 1936 | upstream_comp_name = bt_component_get_name(upstream_comp); |
| 1937 | BT_ASSERT(upstream_comp_name); |
| 1938 | BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", " |
| 1939 | "port-addr=%p, port-name=\"%s\"", |
| 1940 | upstream_comp, upstream_comp_name, |
| 1941 | upstream_port, upstream_port_name); |
| 1942 | |
| 1943 | for (i = 0; i < ctx->cfg->cmd_data.run.connections->len; i++) { |
| 1944 | struct bt_config_connection *cfg_conn = |
| 1945 | g_ptr_array_index( |
| 1946 | ctx->cfg->cmd_data.run.connections, i); |
| 1947 | |
| 1948 | if (strcmp(cfg_conn->upstream_comp_name->str, |
| 1949 | upstream_comp_name)) { |
| 1950 | continue; |
| 1951 | } |
| 1952 | |
| 1953 | if (!bt_common_star_glob_match( |
| 1954 | cfg_conn->upstream_port_glob->str, |
| 1955 | SIZE_MAX, upstream_port_name, SIZE_MAX)) { |
| 1956 | continue; |
| 1957 | } |
| 1958 | |
| 1959 | ret = cmd_run_ctx_connect_upstream_port_to_downstream_component( |
| 1960 | ctx, upstream_comp, upstream_port, cfg_conn); |
| 1961 | if (ret) { |
| 1962 | BT_LOGE("Cannot connect upstream port: " |
| 1963 | "port-addr=%p, port-name=\"%s\"", |
| 1964 | upstream_port, |
| 1965 | upstream_port_name); |
| 1966 | fprintf(stderr, |
| 1967 | "Cannot connect port `%s` of component `%s` to a downstream port: %s\n", |
| 1968 | upstream_port_name, |
| 1969 | upstream_comp_name, |
| 1970 | cfg_conn->arg->str); |
| 1971 | goto error; |
| 1972 | } |
| 1973 | goto end; |
| 1974 | } |
| 1975 | |
| 1976 | BT_LOGE("Cannot connect upstream port: port does not match any connection argument: " |
| 1977 | "port-addr=%p, port-name=\"%s\"", upstream_port, |
| 1978 | upstream_port_name); |
| 1979 | fprintf(stderr, |
| 1980 | "Cannot create connection: upstream port `%s` does not match any connection\n", |
| 1981 | upstream_port_name); |
| 1982 | |
| 1983 | error: |
| 1984 | ret = -1; |
| 1985 | |
| 1986 | end: |
| 1987 | return ret; |
| 1988 | } |
| 1989 | |
| 1990 | static |
| 1991 | bt_graph_listener_func_status |
| 1992 | graph_output_port_added_listener(struct cmd_run_ctx *ctx, |
| 1993 | const bt_port_output *out_port) |
| 1994 | { |
| 1995 | const bt_component *comp; |
| 1996 | const bt_port *port = bt_port_output_as_port_const(out_port); |
| 1997 | bt_graph_listener_func_status ret = |
| 1998 | BT_GRAPH_LISTENER_FUNC_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_FUNC_STATUS_ERROR; |
| 2024 | goto end; |
| 2025 | } |
| 2026 | |
| 2027 | end: |
| 2028 | return ret; |
| 2029 | } |
| 2030 | |
| 2031 | static |
| 2032 | bt_graph_listener_func_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_func_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_add_component_status add_comp_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 | add_comp_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 (add_comp_status != BT_GRAPH_ADD_COMPONENT_STATUS_OK) { |
| 2124 | BT_LOGE_STR("Cannot add \"port added\" listener to graph."); |
| 2125 | goto error; |
| 2126 | } |
| 2127 | |
| 2128 | add_comp_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 (add_comp_status != BT_GRAPH_ADD_COMPONENT_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_TRACE); |
| 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 |
| 2572 | int cmd_run(struct bt_config *cfg) |
| 2573 | { |
| 2574 | int ret = 0; |
| 2575 | struct cmd_run_ctx ctx = { 0 }; |
| 2576 | |
| 2577 | /* Initialize the command's context and the graph object */ |
| 2578 | if (cmd_run_ctx_init(&ctx, cfg)) { |
| 2579 | BT_LOGE_STR("Cannot initialize the command's context."); |
| 2580 | fprintf(stderr, "Cannot initialize the command's context\n"); |
| 2581 | goto error; |
| 2582 | } |
| 2583 | |
| 2584 | if (canceled) { |
| 2585 | BT_LOGI_STR("Canceled by user before creating components."); |
| 2586 | goto error; |
| 2587 | } |
| 2588 | |
| 2589 | BT_LOGI_STR("Creating components."); |
| 2590 | |
| 2591 | /* Create the requested component instances */ |
| 2592 | if (cmd_run_ctx_create_components(&ctx)) { |
| 2593 | BT_LOGE_STR("Cannot create components."); |
| 2594 | fprintf(stderr, "Cannot create components\n"); |
| 2595 | goto error; |
| 2596 | } |
| 2597 | |
| 2598 | if (canceled) { |
| 2599 | BT_LOGI_STR("Canceled by user before connecting components."); |
| 2600 | goto error; |
| 2601 | } |
| 2602 | |
| 2603 | BT_LOGI_STR("Connecting components."); |
| 2604 | |
| 2605 | /* Connect the initially visible component ports */ |
| 2606 | if (cmd_run_ctx_connect_ports(&ctx)) { |
| 2607 | BT_LOGE_STR("Cannot connect initial component ports."); |
| 2608 | fprintf(stderr, "Cannot connect initial component ports\n"); |
| 2609 | goto error; |
| 2610 | } |
| 2611 | |
| 2612 | if (canceled) { |
| 2613 | BT_LOGI_STR("Canceled by user before running the graph."); |
| 2614 | goto error; |
| 2615 | } |
| 2616 | |
| 2617 | BT_LOGI_STR("Running the graph."); |
| 2618 | |
| 2619 | /* Run the graph */ |
| 2620 | while (true) { |
| 2621 | bt_graph_run_status run_status = bt_graph_run(ctx.graph); |
| 2622 | |
| 2623 | /* |
| 2624 | * Reset console in case something messed with console |
| 2625 | * codes during the graph's execution. |
| 2626 | */ |
| 2627 | printf("%s", bt_common_color_reset()); |
| 2628 | fflush(stdout); |
| 2629 | fprintf(stderr, "%s", bt_common_color_reset()); |
| 2630 | BT_LOGT("bt_graph_run() returned: status=%s", |
| 2631 | bt_common_func_status_string(run_status)); |
| 2632 | |
| 2633 | switch (run_status) { |
| 2634 | case BT_GRAPH_RUN_STATUS_OK: |
| 2635 | break; |
| 2636 | case BT_GRAPH_RUN_STATUS_CANCELED: |
| 2637 | BT_LOGI_STR("Graph was canceled by user."); |
| 2638 | goto error; |
| 2639 | case BT_GRAPH_RUN_STATUS_AGAIN: |
| 2640 | if (bt_graph_is_canceled(ctx.graph)) { |
| 2641 | BT_LOGI_STR("Graph was canceled by user."); |
| 2642 | goto error; |
| 2643 | } |
| 2644 | |
| 2645 | if (cfg->cmd_data.run.retry_duration_us > 0) { |
| 2646 | BT_LOGT("Got BT_GRAPH_RUN_STATUS_AGAIN: sleeping: " |
| 2647 | "time-us=%" PRIu64, |
| 2648 | cfg->cmd_data.run.retry_duration_us); |
| 2649 | |
| 2650 | if (usleep(cfg->cmd_data.run.retry_duration_us)) { |
| 2651 | if (bt_graph_is_canceled(ctx.graph)) { |
| 2652 | BT_LOGI_STR("Graph was canceled by user."); |
| 2653 | goto error; |
| 2654 | } |
| 2655 | } |
| 2656 | } |
| 2657 | break; |
| 2658 | case BT_GRAPH_RUN_STATUS_END: |
| 2659 | goto end; |
| 2660 | default: |
| 2661 | BT_LOGE_STR("Graph failed to complete successfully"); |
| 2662 | fprintf(stderr, "Graph failed to complete successfully\n"); |
| 2663 | goto error; |
| 2664 | } |
| 2665 | } |
| 2666 | |
| 2667 | goto end; |
| 2668 | |
| 2669 | error: |
| 2670 | if (ret == 0) { |
| 2671 | ret = -1; |
| 2672 | } |
| 2673 | |
| 2674 | end: |
| 2675 | cmd_run_ctx_destroy(&ctx); |
| 2676 | return ret; |
| 2677 | } |
| 2678 | |
| 2679 | static |
| 2680 | void warn_command_name_and_directory_clash(struct bt_config *cfg) |
| 2681 | { |
| 2682 | const char *env_clash; |
| 2683 | |
| 2684 | if (!cfg->command_name) { |
| 2685 | return; |
| 2686 | } |
| 2687 | |
| 2688 | env_clash = getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH); |
| 2689 | if (env_clash && strcmp(env_clash, "0") == 0) { |
| 2690 | return; |
| 2691 | } |
| 2692 | |
| 2693 | if (g_file_test(cfg->command_name, |
| 2694 | G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) { |
| 2695 | fprintf(stderr, "\nNOTE: The `%s` command was executed. If you meant to convert a\n", |
| 2696 | cfg->command_name); |
| 2697 | fprintf(stderr, "trace located in the local `%s` directory, please use:\n", |
| 2698 | cfg->command_name); |
| 2699 | fprintf(stderr, "\n"); |
| 2700 | fprintf(stderr, " babeltrace2 convert %s [OPTIONS]\n", |
| 2701 | cfg->command_name); |
| 2702 | } |
| 2703 | } |
| 2704 | |
| 2705 | static |
| 2706 | void init_log_level(void) |
| 2707 | { |
| 2708 | bt_cli_log_level = bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL); |
| 2709 | } |
| 2710 | |
| 2711 | static |
| 2712 | void set_auto_log_levels(struct bt_config *cfg) |
| 2713 | { |
| 2714 | const char **env_var_name; |
| 2715 | |
| 2716 | /* |
| 2717 | * Override the configuration's default log level if |
| 2718 | * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables |
| 2719 | * are found for backward compatibility with legacy Babetrace 1. |
| 2720 | */ |
| 2721 | if (getenv("BABELTRACE_DEBUG") && |
| 2722 | strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) { |
| 2723 | cfg->log_level = BT_LOG_TRACE; |
| 2724 | } else if (getenv("BABELTRACE_VERBOSE") && |
| 2725 | strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) { |
| 2726 | cfg->log_level = BT_LOG_INFO; |
| 2727 | } |
| 2728 | |
| 2729 | /* |
| 2730 | * Set log levels according to --debug or --verbose. For |
| 2731 | * backward compatibility, --debug is more verbose than |
| 2732 | * --verbose. So: |
| 2733 | * |
| 2734 | * --verbose: INFO log level |
| 2735 | * --debug: TRACE log level (includes DEBUG, which is |
| 2736 | * is less verbose than TRACE in the internal |
| 2737 | * logging framework) |
| 2738 | */ |
| 2739 | if (!getenv("LIBBABELTRACE2_INIT_LOG_LEVEL")) { |
| 2740 | if (cfg->verbose) { |
| 2741 | bt_logging_set_global_level(BT_LOG_INFO); |
| 2742 | } else if (cfg->debug) { |
| 2743 | bt_logging_set_global_level(BT_LOG_TRACE); |
| 2744 | } else { |
| 2745 | /* |
| 2746 | * Set library's default log level if not |
| 2747 | * explicitly specified. |
| 2748 | */ |
| 2749 | bt_logging_set_global_level(cfg->log_level); |
| 2750 | } |
| 2751 | } |
| 2752 | |
| 2753 | if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL)) { |
| 2754 | if (cfg->verbose) { |
| 2755 | bt_cli_log_level = BT_LOG_INFO; |
| 2756 | } else if (cfg->debug) { |
| 2757 | bt_cli_log_level = BT_LOG_TRACE; |
| 2758 | } else { |
| 2759 | /* |
| 2760 | * Set CLI's default log level if not explicitly |
| 2761 | * specified. |
| 2762 | */ |
| 2763 | bt_cli_log_level = cfg->log_level; |
| 2764 | } |
| 2765 | } |
| 2766 | |
| 2767 | env_var_name = log_level_env_var_names; |
| 2768 | |
| 2769 | while (*env_var_name) { |
| 2770 | if (!getenv(*env_var_name)) { |
| 2771 | if (cfg->verbose) { |
| 2772 | g_setenv(*env_var_name, "INFO", 1); |
| 2773 | } else if (cfg->debug) { |
| 2774 | g_setenv(*env_var_name, "TRACE", 1); |
| 2775 | } else { |
| 2776 | char val[2] = { 0 }; |
| 2777 | |
| 2778 | /* |
| 2779 | * Set module's default log level if not |
| 2780 | * explicitly specified. |
| 2781 | */ |
| 2782 | val[0] = bt_log_get_letter_from_level( |
| 2783 | cfg->log_level); |
| 2784 | g_setenv(*env_var_name, val, 1); |
| 2785 | } |
| 2786 | } |
| 2787 | |
| 2788 | env_var_name++; |
| 2789 | } |
| 2790 | } |
| 2791 | |
| 2792 | int main(int argc, const char **argv) |
| 2793 | { |
| 2794 | int ret; |
| 2795 | int retcode; |
| 2796 | struct bt_config *cfg; |
| 2797 | |
| 2798 | init_log_level(); |
| 2799 | set_signal_handler(); |
| 2800 | init_static_data(); |
| 2801 | cfg = bt_config_cli_args_create_with_default(argc, argv, &retcode); |
| 2802 | |
| 2803 | if (retcode < 0) { |
| 2804 | /* Quit without errors; typically usage/version */ |
| 2805 | retcode = 0; |
| 2806 | BT_LOGI_STR("Quitting without errors."); |
| 2807 | goto end; |
| 2808 | } |
| 2809 | |
| 2810 | if (retcode > 0) { |
| 2811 | BT_LOGE("Command-line error: retcode=%d", retcode); |
| 2812 | goto end; |
| 2813 | } |
| 2814 | |
| 2815 | if (!cfg) { |
| 2816 | BT_LOGE_STR("Failed to create a valid Babeltrace configuration."); |
| 2817 | fprintf(stderr, "Failed to create Babeltrace configuration\n"); |
| 2818 | retcode = 1; |
| 2819 | goto end; |
| 2820 | } |
| 2821 | |
| 2822 | set_auto_log_levels(cfg); |
| 2823 | print_cfg(cfg); |
| 2824 | |
| 2825 | if (cfg->command_needs_plugins) { |
| 2826 | ret = load_all_plugins(cfg->plugin_paths); |
| 2827 | if (ret) { |
| 2828 | BT_LOGE("Failed to load plugins: ret=%d", ret); |
| 2829 | retcode = 1; |
| 2830 | goto end; |
| 2831 | } |
| 2832 | } |
| 2833 | |
| 2834 | BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"", |
| 2835 | cfg->command, cfg->command_name); |
| 2836 | |
| 2837 | switch (cfg->command) { |
| 2838 | case BT_CONFIG_COMMAND_RUN: |
| 2839 | ret = cmd_run(cfg); |
| 2840 | break; |
| 2841 | case BT_CONFIG_COMMAND_LIST_PLUGINS: |
| 2842 | ret = cmd_list_plugins(cfg); |
| 2843 | break; |
| 2844 | case BT_CONFIG_COMMAND_HELP: |
| 2845 | ret = cmd_help(cfg); |
| 2846 | break; |
| 2847 | case BT_CONFIG_COMMAND_QUERY: |
| 2848 | ret = cmd_query(cfg); |
| 2849 | break; |
| 2850 | case BT_CONFIG_COMMAND_PRINT_CTF_METADATA: |
| 2851 | ret = cmd_print_ctf_metadata(cfg); |
| 2852 | break; |
| 2853 | case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS: |
| 2854 | ret = cmd_print_lttng_live_sessions(cfg); |
| 2855 | break; |
| 2856 | default: |
| 2857 | BT_LOGF("Invalid/unknown command: cmd=%d", cfg->command); |
| 2858 | abort(); |
| 2859 | } |
| 2860 | |
| 2861 | BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d", |
| 2862 | cfg->command, cfg->command_name, ret); |
| 2863 | warn_command_name_and_directory_clash(cfg); |
| 2864 | retcode = ret ? 1 : 0; |
| 2865 | |
| 2866 | end: |
| 2867 | BT_OBJECT_PUT_REF_AND_RESET(cfg); |
| 2868 | fini_static_data(); |
| 2869 | return retcode; |
| 2870 | } |