| 1 | /* |
| 2 | * Copyright (c) 2019 Philippe Proulx <pproulx@efficios.com> |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | * of this software and associated documentation files (the "Software"), to deal |
| 6 | * in the Software without restriction, including without limitation the rights |
| 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | * copies of the Software, and to permit persons to whom the Software is |
| 9 | * furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | * SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #define BT_LOG_TAG "LIB/ERROR" |
| 24 | #include "lib/logging.h" |
| 25 | |
| 26 | #include <stdlib.h> |
| 27 | #include <stdint.h> |
| 28 | #include <babeltrace2/babeltrace.h> |
| 29 | |
| 30 | #include "error.h" |
| 31 | #include "graph/message/iterator.h" |
| 32 | #include "graph/component.h" |
| 33 | #include "graph/component-class.h" |
| 34 | #include "common/assert.h" |
| 35 | #include "lib/assert-pre.h" |
| 36 | #include "lib/func-status.h" |
| 37 | |
| 38 | #define BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(_cause, _exp_type) \ |
| 39 | BT_ASSERT_PRE(((const struct bt_error_cause *) (_cause))->actor_type == _exp_type, \ |
| 40 | "Unexpected error cause's actor type: type=%s, exp-type=%s", \ |
| 41 | bt_error_cause_actor_type_string(((const struct bt_error_cause *) (_cause))->actor_type), \ |
| 42 | bt_error_cause_actor_type_string(_exp_type)) |
| 43 | |
| 44 | static |
| 45 | void fini_component_class_id( |
| 46 | struct bt_error_cause_component_class_id *comp_class_id) |
| 47 | { |
| 48 | BT_ASSERT(comp_class_id); |
| 49 | |
| 50 | if (comp_class_id->name) { |
| 51 | g_string_free(comp_class_id->name, TRUE); |
| 52 | comp_class_id->name = NULL; |
| 53 | } |
| 54 | |
| 55 | if (comp_class_id->plugin_name) { |
| 56 | g_string_free(comp_class_id->plugin_name, TRUE); |
| 57 | comp_class_id->plugin_name = NULL; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | static |
| 62 | void fini_error_cause(struct bt_error_cause *cause) |
| 63 | { |
| 64 | BT_ASSERT(cause); |
| 65 | BT_LIB_LOGD("Finalizing error cause: %!+r", cause); |
| 66 | |
| 67 | if (cause->module_name) { |
| 68 | g_string_free(cause->module_name, TRUE); |
| 69 | cause->module_name = NULL; |
| 70 | } |
| 71 | |
| 72 | if (cause->file_name) { |
| 73 | g_string_free(cause->file_name, TRUE); |
| 74 | cause->file_name = NULL; |
| 75 | } |
| 76 | |
| 77 | if (cause->message) { |
| 78 | g_string_free(cause->message, TRUE); |
| 79 | cause->message = NULL; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | static |
| 84 | void destroy_error_cause(struct bt_error_cause *cause) |
| 85 | { |
| 86 | if (!cause) { |
| 87 | goto end; |
| 88 | } |
| 89 | |
| 90 | BT_LIB_LOGD("Destroying error cause: %!+r", cause); |
| 91 | |
| 92 | switch (cause->actor_type) { |
| 93 | case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT: |
| 94 | { |
| 95 | struct bt_error_cause_component_actor *spec_cause = |
| 96 | (void *) cause; |
| 97 | |
| 98 | if (spec_cause->comp_name) { |
| 99 | g_string_free(spec_cause->comp_name, TRUE); |
| 100 | spec_cause->comp_name = NULL; |
| 101 | } |
| 102 | |
| 103 | fini_component_class_id(&spec_cause->comp_class_id); |
| 104 | break; |
| 105 | } |
| 106 | case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS: |
| 107 | { |
| 108 | struct bt_error_cause_component_class_actor *spec_cause = |
| 109 | (void *) cause; |
| 110 | |
| 111 | fini_component_class_id(&spec_cause->comp_class_id); |
| 112 | break; |
| 113 | } |
| 114 | case BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR: |
| 115 | { |
| 116 | struct bt_error_cause_message_iterator_actor *spec_cause = |
| 117 | (void *) cause; |
| 118 | |
| 119 | if (spec_cause->comp_name) { |
| 120 | g_string_free(spec_cause->comp_name, TRUE); |
| 121 | spec_cause->comp_name = NULL; |
| 122 | } |
| 123 | |
| 124 | if (spec_cause->output_port_name) { |
| 125 | g_string_free(spec_cause->output_port_name, TRUE); |
| 126 | spec_cause->output_port_name = NULL; |
| 127 | } |
| 128 | |
| 129 | fini_component_class_id(&spec_cause->comp_class_id); |
| 130 | break; |
| 131 | } |
| 132 | default: |
| 133 | break; |
| 134 | } |
| 135 | |
| 136 | fini_error_cause(cause); |
| 137 | g_free(cause); |
| 138 | |
| 139 | end: |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | static |
| 144 | int init_error_cause(struct bt_error_cause *cause, |
| 145 | enum bt_error_cause_actor_type actor_type) |
| 146 | { |
| 147 | int ret = 0; |
| 148 | |
| 149 | BT_ASSERT(cause); |
| 150 | BT_LIB_LOGD("Initializing error cause: %!+r", cause); |
| 151 | cause->actor_type = actor_type; |
| 152 | cause->module_name = g_string_new(NULL); |
| 153 | if (!cause->module_name) { |
| 154 | BT_LOGE_STR("Failed to allocate one GString."); |
| 155 | ret = -1; |
| 156 | goto end; |
| 157 | } |
| 158 | |
| 159 | cause->message = g_string_new(NULL); |
| 160 | if (!cause->message) { |
| 161 | BT_LOGE_STR("Failed to allocate one GString."); |
| 162 | ret = -1; |
| 163 | goto end; |
| 164 | } |
| 165 | |
| 166 | cause->file_name = g_string_new(NULL); |
| 167 | if (!cause->file_name) { |
| 168 | BT_LOGE_STR("Failed to allocate one GString."); |
| 169 | ret = -1; |
| 170 | goto end; |
| 171 | } |
| 172 | |
| 173 | BT_LIB_LOGD("Initialized error cause: %!+r", cause); |
| 174 | |
| 175 | end: |
| 176 | return ret; |
| 177 | } |
| 178 | |
| 179 | static |
| 180 | int init_component_class_id( |
| 181 | struct bt_error_cause_component_class_id *comp_class_id, |
| 182 | struct bt_component_class *comp_cls) |
| 183 | { |
| 184 | int ret = 0; |
| 185 | |
| 186 | BT_ASSERT(comp_class_id); |
| 187 | comp_class_id->type = comp_cls->type; |
| 188 | comp_class_id->name = g_string_new(comp_cls->name->str); |
| 189 | if (!comp_class_id->name) { |
| 190 | BT_LOGE_STR("Failed to allocate one GString."); |
| 191 | ret = -1; |
| 192 | goto end; |
| 193 | } |
| 194 | |
| 195 | comp_class_id->plugin_name = g_string_new(comp_cls->plugin_name->str); |
| 196 | if (!comp_class_id->plugin_name) { |
| 197 | BT_LOGE_STR("Failed to allocate one GString."); |
| 198 | ret = -1; |
| 199 | goto end; |
| 200 | } |
| 201 | |
| 202 | end: |
| 203 | return ret; |
| 204 | } |
| 205 | |
| 206 | static |
| 207 | void set_error_cause_props(struct bt_error_cause *cause, |
| 208 | const char *file_name, uint64_t line_no) |
| 209 | { |
| 210 | BT_ASSERT(cause); |
| 211 | g_string_assign(cause->file_name, file_name); |
| 212 | cause->line_no = line_no; |
| 213 | } |
| 214 | |
| 215 | static |
| 216 | struct bt_error_cause *create_error_cause(const char *module_name, |
| 217 | const char *file_name, uint64_t line_no) |
| 218 | { |
| 219 | struct bt_error_cause *cause = g_new0(struct bt_error_cause, 1); |
| 220 | int ret; |
| 221 | |
| 222 | BT_LOGD_STR("Creating error cause (unknown actor)."); |
| 223 | |
| 224 | if (!cause) { |
| 225 | BT_LOGE_STR("Failed to allocate one error cause."); |
| 226 | goto error; |
| 227 | } |
| 228 | |
| 229 | ret = init_error_cause(cause, BT_ERROR_CAUSE_ACTOR_TYPE_UNKNOWN); |
| 230 | if (ret) { |
| 231 | goto error; |
| 232 | } |
| 233 | |
| 234 | g_string_assign(cause->module_name, module_name); |
| 235 | set_error_cause_props(cause, file_name, line_no); |
| 236 | BT_LIB_LOGD("Created error cause: %!+r", cause); |
| 237 | goto end; |
| 238 | |
| 239 | error: |
| 240 | destroy_error_cause(cause); |
| 241 | cause = NULL; |
| 242 | |
| 243 | end: |
| 244 | return cause; |
| 245 | } |
| 246 | |
| 247 | static |
| 248 | void append_component_class_id_str(GString *str, |
| 249 | struct bt_error_cause_component_class_id *comp_class_id) |
| 250 | { |
| 251 | const char *type_str = NULL; |
| 252 | |
| 253 | switch (comp_class_id->type) { |
| 254 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 255 | type_str = "src"; |
| 256 | break; |
| 257 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 258 | type_str = "flt"; |
| 259 | break; |
| 260 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 261 | type_str = "sink"; |
| 262 | break; |
| 263 | default: |
| 264 | abort(); |
| 265 | } |
| 266 | |
| 267 | if (comp_class_id->plugin_name->len > 0) { |
| 268 | g_string_append_printf(str, "%s.%s.%s", |
| 269 | type_str, comp_class_id->plugin_name->str, |
| 270 | comp_class_id->name->str); |
| 271 | } else { |
| 272 | g_string_append_printf(str, "%s.%s", |
| 273 | type_str, comp_class_id->name->str); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | static |
| 278 | struct bt_error_cause_component_actor *create_error_cause_component_actor( |
| 279 | struct bt_component *comp, const char *file_name, |
| 280 | uint64_t line_no) |
| 281 | { |
| 282 | struct bt_error_cause_component_actor *cause = |
| 283 | g_new0(struct bt_error_cause_component_actor, 1); |
| 284 | int ret; |
| 285 | |
| 286 | BT_LOGD_STR("Creating error cause object (component actor)."); |
| 287 | |
| 288 | if (!cause) { |
| 289 | goto error; |
| 290 | } |
| 291 | |
| 292 | ret = init_error_cause(&cause->base, |
| 293 | BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT); |
| 294 | if (ret) { |
| 295 | goto error; |
| 296 | } |
| 297 | |
| 298 | set_error_cause_props(&cause->base, file_name, line_no); |
| 299 | cause->comp_name = g_string_new(comp->name->str); |
| 300 | if (!cause->comp_name) { |
| 301 | BT_LOGE_STR("Failed to allocate one GString."); |
| 302 | goto error; |
| 303 | } |
| 304 | |
| 305 | ret = init_component_class_id(&cause->comp_class_id, comp->class); |
| 306 | if (ret) { |
| 307 | goto error; |
| 308 | } |
| 309 | |
| 310 | g_string_append_printf(cause->base.module_name, "%s: ", |
| 311 | comp->name->str); |
| 312 | append_component_class_id_str(cause->base.module_name, |
| 313 | &cause->comp_class_id); |
| 314 | BT_LIB_LOGD("Created error cause object: %!+r", cause); |
| 315 | goto end; |
| 316 | |
| 317 | error: |
| 318 | destroy_error_cause(&cause->base); |
| 319 | cause = NULL; |
| 320 | |
| 321 | end: |
| 322 | return cause; |
| 323 | } |
| 324 | |
| 325 | static |
| 326 | struct bt_error_cause_component_class_actor * |
| 327 | create_error_cause_component_class_actor(struct bt_component_class *comp_cls, |
| 328 | const char *file_name, uint64_t line_no) |
| 329 | { |
| 330 | struct bt_error_cause_component_class_actor *cause = |
| 331 | g_new0(struct bt_error_cause_component_class_actor, 1); |
| 332 | int ret; |
| 333 | |
| 334 | BT_LOGD_STR("Creating error cause object (component class actor)."); |
| 335 | |
| 336 | if (!cause) { |
| 337 | BT_LOGE_STR("Failed to allocate one error cause object."); |
| 338 | goto error; |
| 339 | } |
| 340 | |
| 341 | ret = init_error_cause(&cause->base, |
| 342 | BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS); |
| 343 | if (ret) { |
| 344 | goto error; |
| 345 | } |
| 346 | |
| 347 | set_error_cause_props(&cause->base, file_name, line_no); |
| 348 | ret = init_component_class_id(&cause->comp_class_id, comp_cls); |
| 349 | if (ret) { |
| 350 | goto error; |
| 351 | } |
| 352 | |
| 353 | append_component_class_id_str(cause->base.module_name, |
| 354 | &cause->comp_class_id); |
| 355 | BT_LIB_LOGD("Created error cause object: %!+r", cause); |
| 356 | goto end; |
| 357 | |
| 358 | error: |
| 359 | destroy_error_cause(&cause->base); |
| 360 | cause = NULL; |
| 361 | |
| 362 | end: |
| 363 | return cause; |
| 364 | } |
| 365 | |
| 366 | struct bt_error_cause_message_iterator_actor * |
| 367 | create_error_cause_message_iterator_actor(struct bt_message_iterator *iter, |
| 368 | const char *file_name, uint64_t line_no) |
| 369 | { |
| 370 | struct bt_error_cause_message_iterator_actor *cause; |
| 371 | struct bt_self_component_port_input_message_iterator *input_port_iter; |
| 372 | int ret; |
| 373 | |
| 374 | BT_LOGD_STR("Creating error cause object (message iterator actor)."); |
| 375 | |
| 376 | /* |
| 377 | * This can only be created from within a graph, from a user |
| 378 | * message iterator, which is a self component port input |
| 379 | * message iterator. |
| 380 | */ |
| 381 | BT_ASSERT(iter->type == |
| 382 | BT_MESSAGE_ITERATOR_TYPE_SELF_COMPONENT_PORT_INPUT); |
| 383 | input_port_iter = (void *) iter; |
| 384 | cause = g_new0(struct bt_error_cause_message_iterator_actor, 1); |
| 385 | if (!cause) { |
| 386 | BT_LOGE_STR("Failed to allocate one error cause object."); |
| 387 | goto error; |
| 388 | } |
| 389 | |
| 390 | ret = init_error_cause(&cause->base, |
| 391 | BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR); |
| 392 | if (ret) { |
| 393 | goto error; |
| 394 | } |
| 395 | |
| 396 | set_error_cause_props(&cause->base, file_name, line_no); |
| 397 | cause->comp_name = g_string_new( |
| 398 | input_port_iter->upstream_component->name->str); |
| 399 | if (!cause->comp_name) { |
| 400 | BT_LOGE_STR("Failed to allocate one GString."); |
| 401 | goto error; |
| 402 | } |
| 403 | |
| 404 | cause->output_port_name = g_string_new( |
| 405 | input_port_iter->upstream_port->name->str); |
| 406 | if (!cause->output_port_name) { |
| 407 | BT_LOGE_STR("Failed to allocate one GString."); |
| 408 | goto error; |
| 409 | } |
| 410 | |
| 411 | ret = init_component_class_id(&cause->comp_class_id, |
| 412 | input_port_iter->upstream_component->class); |
| 413 | if (ret) { |
| 414 | goto error; |
| 415 | } |
| 416 | |
| 417 | g_string_append_printf(cause->base.module_name, "%s (%s): ", |
| 418 | input_port_iter->upstream_component->name->str, |
| 419 | input_port_iter->upstream_port->name->str); |
| 420 | append_component_class_id_str(cause->base.module_name, |
| 421 | &cause->comp_class_id); |
| 422 | BT_LIB_LOGD("Created error cause object: %!+r", cause); |
| 423 | goto end; |
| 424 | |
| 425 | error: |
| 426 | destroy_error_cause(&cause->base); |
| 427 | cause = NULL; |
| 428 | |
| 429 | end: |
| 430 | return cause; |
| 431 | } |
| 432 | |
| 433 | BT_HIDDEN |
| 434 | struct bt_error *bt_error_create(void) |
| 435 | { |
| 436 | struct bt_error *error; |
| 437 | |
| 438 | BT_LOGD_STR("Creating error object."); |
| 439 | error = g_new0(struct bt_error, 1); |
| 440 | if (!error) { |
| 441 | BT_LOGE_STR("Failed to allocate one error object."); |
| 442 | goto error; |
| 443 | } |
| 444 | |
| 445 | error->causes = g_ptr_array_new_with_free_func( |
| 446 | (GDestroyNotify) destroy_error_cause); |
| 447 | if (!error->causes) { |
| 448 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
| 449 | goto error; |
| 450 | } |
| 451 | |
| 452 | BT_LOGD("Created error object: addr=%p", error); |
| 453 | goto end; |
| 454 | |
| 455 | error: |
| 456 | bt_error_destroy(error); |
| 457 | error = NULL; |
| 458 | |
| 459 | end: |
| 460 | return error; |
| 461 | } |
| 462 | |
| 463 | BT_HIDDEN |
| 464 | void bt_error_destroy(struct bt_error *error) |
| 465 | { |
| 466 | if (!error) { |
| 467 | goto end; |
| 468 | } |
| 469 | |
| 470 | if (error->causes) { |
| 471 | g_ptr_array_free(error->causes, TRUE); |
| 472 | error->causes = NULL; |
| 473 | } |
| 474 | |
| 475 | g_free(error); |
| 476 | |
| 477 | end: |
| 478 | return; |
| 479 | } |
| 480 | |
| 481 | BT_HIDDEN |
| 482 | int bt_error_append_cause_from_unknown(struct bt_error *error, |
| 483 | const char *module_name, const char *file_name, |
| 484 | uint64_t line_no, const char *msg_fmt, va_list args) |
| 485 | { |
| 486 | struct bt_error_cause *cause = NULL; |
| 487 | int status = BT_FUNC_STATUS_OK; |
| 488 | |
| 489 | BT_ASSERT(error); |
| 490 | BT_ASSERT(module_name); |
| 491 | BT_ASSERT(file_name); |
| 492 | BT_ASSERT(msg_fmt); |
| 493 | BT_LOGD("Appending error cause from unknown actor: " |
| 494 | "module-name=\"%s\", func-name=\"%s\", line-no=%" PRIu64, |
| 495 | module_name, file_name, line_no); |
| 496 | cause = create_error_cause(module_name, file_name, line_no); |
| 497 | if (!cause) { |
| 498 | /* create_error_cause() logs errors */ |
| 499 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 500 | goto end; |
| 501 | } |
| 502 | |
| 503 | g_string_append_vprintf(cause->message, msg_fmt, args); |
| 504 | g_ptr_array_add(error->causes, cause); |
| 505 | BT_LIB_LOGD("Appended error cause: %!+r", cause); |
| 506 | cause = NULL; |
| 507 | |
| 508 | end: |
| 509 | destroy_error_cause(cause); |
| 510 | return status; |
| 511 | } |
| 512 | |
| 513 | BT_HIDDEN |
| 514 | int bt_error_append_cause_from_component( |
| 515 | struct bt_error *error, bt_self_component *self_comp, |
| 516 | const char *file_name, uint64_t line_no, |
| 517 | const char *msg_fmt, va_list args) |
| 518 | { |
| 519 | struct bt_error_cause_component_actor *cause = NULL; |
| 520 | int status = BT_FUNC_STATUS_OK; |
| 521 | |
| 522 | BT_ASSERT(error); |
| 523 | BT_ASSERT(self_comp); |
| 524 | BT_ASSERT(file_name); |
| 525 | BT_ASSERT(msg_fmt); |
| 526 | BT_LIB_LOGD("Appending error cause from component actor: %![comp-]+c", |
| 527 | self_comp); |
| 528 | cause = create_error_cause_component_actor((void *) self_comp, |
| 529 | file_name, line_no); |
| 530 | if (!cause) { |
| 531 | /* create_error_cause_component_actor() logs errors */ |
| 532 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 533 | goto end; |
| 534 | } |
| 535 | |
| 536 | g_string_append_vprintf(cause->base.message, msg_fmt, args); |
| 537 | g_ptr_array_add(error->causes, cause); |
| 538 | BT_LIB_LOGD("Appended error cause: %!+r", cause); |
| 539 | cause = NULL; |
| 540 | |
| 541 | end: |
| 542 | destroy_error_cause(&cause->base); |
| 543 | return status; |
| 544 | } |
| 545 | |
| 546 | BT_HIDDEN |
| 547 | int bt_error_append_cause_from_component_class( |
| 548 | struct bt_error *error, |
| 549 | bt_self_component_class *self_comp_class, |
| 550 | const char *file_name, uint64_t line_no, |
| 551 | const char *msg_fmt, va_list args) |
| 552 | { |
| 553 | struct bt_error_cause_component_class_actor *cause = NULL; |
| 554 | int status = BT_FUNC_STATUS_OK; |
| 555 | |
| 556 | BT_ASSERT(error); |
| 557 | BT_ASSERT(self_comp_class); |
| 558 | BT_ASSERT(file_name); |
| 559 | BT_ASSERT(msg_fmt); |
| 560 | BT_LIB_LOGD("Appending error cause from component class actor: " |
| 561 | "%![comp-cls-]+C", self_comp_class); |
| 562 | cause = create_error_cause_component_class_actor( |
| 563 | (void *) self_comp_class, file_name, line_no); |
| 564 | if (!cause) { |
| 565 | /* create_error_cause_component_class_actor() logs errors */ |
| 566 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 567 | goto end; |
| 568 | } |
| 569 | |
| 570 | g_string_append_vprintf(cause->base.message, msg_fmt, args); |
| 571 | g_ptr_array_add(error->causes, cause); |
| 572 | BT_LIB_LOGD("Appended error cause: %!+r", cause); |
| 573 | cause = NULL; |
| 574 | |
| 575 | end: |
| 576 | destroy_error_cause(&cause->base); |
| 577 | return status; |
| 578 | } |
| 579 | |
| 580 | BT_HIDDEN |
| 581 | int bt_error_append_cause_from_message_iterator( |
| 582 | struct bt_error *error, bt_self_message_iterator *self_iter, |
| 583 | const char *file_name, uint64_t line_no, |
| 584 | const char *msg_fmt, va_list args) |
| 585 | { |
| 586 | struct bt_error_cause_message_iterator_actor *cause = NULL; |
| 587 | int status = BT_FUNC_STATUS_OK; |
| 588 | |
| 589 | BT_ASSERT(error); |
| 590 | BT_ASSERT(self_iter); |
| 591 | BT_ASSERT(file_name); |
| 592 | BT_ASSERT(msg_fmt); |
| 593 | BT_LIB_LOGD("Appending error cause from message iterator actor: " |
| 594 | "%![comp-]+i", self_iter); |
| 595 | cause = create_error_cause_message_iterator_actor( |
| 596 | (void *) self_iter, file_name, line_no); |
| 597 | if (!cause) { |
| 598 | /* create_error_cause_message_iterator_actor() logs errors */ |
| 599 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 600 | goto end; |
| 601 | } |
| 602 | |
| 603 | g_string_append_vprintf(cause->base.message, msg_fmt, args); |
| 604 | g_ptr_array_add(error->causes, cause); |
| 605 | BT_LIB_LOGD("Appended error cause: %!+r", cause); |
| 606 | cause = NULL; |
| 607 | |
| 608 | end: |
| 609 | destroy_error_cause(&cause->base); |
| 610 | return status; |
| 611 | } |
| 612 | |
| 613 | static |
| 614 | uint64_t error_cause_count(const bt_error *error) |
| 615 | { |
| 616 | return error->causes ? error->causes->len : 0; |
| 617 | } |
| 618 | |
| 619 | uint64_t bt_error_get_cause_count(const bt_error *error) |
| 620 | { |
| 621 | BT_ASSERT_PRE_NON_NULL(error, "Error"); |
| 622 | return error_cause_count(error); |
| 623 | } |
| 624 | |
| 625 | void bt_error_release(const struct bt_error *error) |
| 626 | { |
| 627 | BT_ASSERT_PRE_NON_NULL(error, "Error"); |
| 628 | bt_error_destroy((void *) error); |
| 629 | } |
| 630 | |
| 631 | const struct bt_error_cause *bt_error_borrow_cause_by_index( |
| 632 | const bt_error *error, uint64_t index) |
| 633 | { |
| 634 | BT_ASSERT_PRE_NON_NULL(error, "Error"); |
| 635 | BT_ASSERT_PRE_VALID_INDEX(index, error_cause_count(error)); |
| 636 | return error->causes->pdata[index]; |
| 637 | } |
| 638 | |
| 639 | enum bt_error_cause_actor_type bt_error_cause_get_actor_type( |
| 640 | const struct bt_error_cause *cause) |
| 641 | { |
| 642 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); |
| 643 | return cause->actor_type; |
| 644 | } |
| 645 | |
| 646 | const char *bt_error_cause_get_message(const struct bt_error_cause *cause) |
| 647 | { |
| 648 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); |
| 649 | return cause->message->str; |
| 650 | } |
| 651 | |
| 652 | const char *bt_error_cause_get_module_name(const struct bt_error_cause *cause) |
| 653 | { |
| 654 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); |
| 655 | return cause->module_name->str; |
| 656 | } |
| 657 | |
| 658 | const char *bt_error_cause_get_file_name(const struct bt_error_cause *cause) |
| 659 | { |
| 660 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); |
| 661 | return cause->file_name->str; |
| 662 | } |
| 663 | |
| 664 | uint64_t bt_error_cause_get_line_number(const bt_error_cause *cause) |
| 665 | { |
| 666 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); |
| 667 | return cause->line_no; |
| 668 | } |
| 669 | |
| 670 | const char *bt_error_cause_component_actor_get_component_name( |
| 671 | const struct bt_error_cause *cause) |
| 672 | { |
| 673 | const struct bt_error_cause_component_actor *spec_cause = |
| 674 | (const void *) cause; |
| 675 | |
| 676 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); |
| 677 | BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause, |
| 678 | BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT); |
| 679 | return spec_cause->comp_name->str; |
| 680 | } |
| 681 | |
| 682 | bt_component_class_type bt_error_cause_component_actor_get_component_class_type( |
| 683 | const struct bt_error_cause *cause) |
| 684 | { |
| 685 | const struct bt_error_cause_component_actor *spec_cause = |
| 686 | (const void *) cause; |
| 687 | |
| 688 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); |
| 689 | BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause, |
| 690 | BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT); |
| 691 | return spec_cause->comp_class_id.type; |
| 692 | } |
| 693 | |
| 694 | const char *bt_error_cause_component_actor_get_component_class_name( |
| 695 | const struct bt_error_cause *cause) |
| 696 | { |
| 697 | const struct bt_error_cause_component_actor *spec_cause = |
| 698 | (const void *) cause; |
| 699 | |
| 700 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); |
| 701 | BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause, |
| 702 | BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT); |
| 703 | return spec_cause->comp_class_id.name->str; |
| 704 | } |
| 705 | |
| 706 | const char *bt_error_cause_component_actor_get_plugin_name( |
| 707 | const struct bt_error_cause *cause) |
| 708 | { |
| 709 | const struct bt_error_cause_component_actor *spec_cause = |
| 710 | (const void *) cause; |
| 711 | |
| 712 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); |
| 713 | BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause, |
| 714 | BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT); |
| 715 | return spec_cause->comp_class_id.plugin_name->len > 0 ? |
| 716 | spec_cause->comp_class_id.plugin_name->str : NULL; |
| 717 | } |
| 718 | |
| 719 | bt_component_class_type |
| 720 | bt_error_cause_component_class_actor_get_component_class_type( |
| 721 | const struct bt_error_cause *cause) |
| 722 | { |
| 723 | const struct bt_error_cause_component_class_actor *spec_cause = |
| 724 | (const void *) cause; |
| 725 | |
| 726 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); |
| 727 | BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause, |
| 728 | BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS); |
| 729 | return spec_cause->comp_class_id.type; |
| 730 | } |
| 731 | |
| 732 | const char *bt_error_cause_component_class_actor_get_component_class_name( |
| 733 | const struct bt_error_cause *cause) |
| 734 | { |
| 735 | const struct bt_error_cause_component_class_actor *spec_cause = |
| 736 | (const void *) cause; |
| 737 | |
| 738 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); |
| 739 | BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause, |
| 740 | BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS); |
| 741 | return spec_cause->comp_class_id.name->str; |
| 742 | } |
| 743 | |
| 744 | const char *bt_error_cause_component_class_actor_get_plugin_name( |
| 745 | const struct bt_error_cause *cause) |
| 746 | { |
| 747 | const struct bt_error_cause_component_class_actor *spec_cause = |
| 748 | (const void *) cause; |
| 749 | |
| 750 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); |
| 751 | BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause, |
| 752 | BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS); |
| 753 | return spec_cause->comp_class_id.plugin_name->len > 0 ? |
| 754 | spec_cause->comp_class_id.plugin_name->str : NULL; |
| 755 | } |
| 756 | |
| 757 | const char *bt_error_cause_message_iterator_actor_get_component_name( |
| 758 | const struct bt_error_cause *cause) |
| 759 | { |
| 760 | const struct bt_error_cause_message_iterator_actor *spec_cause = |
| 761 | (const void *) cause; |
| 762 | |
| 763 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); |
| 764 | BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause, |
| 765 | BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR); |
| 766 | return spec_cause->comp_name->str; |
| 767 | } |
| 768 | |
| 769 | const char * |
| 770 | bt_error_cause_message_iterator_actor_get_component_output_port_name( |
| 771 | const struct bt_error_cause *cause) |
| 772 | { |
| 773 | const struct bt_error_cause_message_iterator_actor *spec_cause = |
| 774 | (const void *) cause; |
| 775 | |
| 776 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); |
| 777 | BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause, |
| 778 | BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR); |
| 779 | return spec_cause->output_port_name->str; |
| 780 | } |
| 781 | |
| 782 | bt_component_class_type |
| 783 | bt_error_cause_message_iterator_actor_get_component_class_type( |
| 784 | const struct bt_error_cause *cause) |
| 785 | { |
| 786 | const struct bt_error_cause_message_iterator_actor *spec_cause = |
| 787 | (const void *) cause; |
| 788 | |
| 789 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); |
| 790 | BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause, |
| 791 | BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR); |
| 792 | return spec_cause->comp_class_id.type; |
| 793 | } |
| 794 | |
| 795 | const char *bt_error_cause_message_iterator_actor_get_component_class_name( |
| 796 | const struct bt_error_cause *cause) |
| 797 | { |
| 798 | const struct bt_error_cause_message_iterator_actor *spec_cause = |
| 799 | (const void *) cause; |
| 800 | |
| 801 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); |
| 802 | BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause, |
| 803 | BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR); |
| 804 | return spec_cause->comp_class_id.name->str; |
| 805 | } |
| 806 | |
| 807 | const char *bt_error_cause_message_iterator_actor_get_plugin_name( |
| 808 | const struct bt_error_cause *cause) |
| 809 | { |
| 810 | const struct bt_error_cause_message_iterator_actor *spec_cause = |
| 811 | (const void *) cause; |
| 812 | |
| 813 | BT_ASSERT_PRE_NON_NULL(cause, "Error cause"); |
| 814 | BT_ASSERT_PRE_CAUSE_HAS_ACTOR_TYPE(cause, |
| 815 | BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR); |
| 816 | return spec_cause->comp_class_id.plugin_name->len > 0 ? |
| 817 | spec_cause->comp_class_id.plugin_name->str : NULL; |
| 818 | } |