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