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