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