assert.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2021 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <string.h>
  15. #include <sys/param.h>
  16. #include "esp_system.h"
  17. #include "esp_spi_flash.h"
  18. #include "soc/soc_memory_layout.h"
  19. #define ASSERT_STR "assert failed: "
  20. #define CACHE_DISABLED_STR "<cached disabled>"
  21. #if __XTENSA__
  22. #define INST_LEN 3
  23. #elif __riscv
  24. #define INST_LEN 4
  25. #endif
  26. static inline void ra_to_str(char *addr)
  27. {
  28. addr[0] = '0';
  29. addr[1] = 'x';
  30. itoa((uint32_t)(__builtin_return_address(0) - INST_LEN), addr + 2, 16);
  31. }
  32. /* Overriding assert function so that whenever assert is called from critical section,
  33. * it does not lead to a crash of its own.
  34. */
  35. void __attribute__((noreturn)) __assert_func(const char *file, int line, const char *func, const char *expr)
  36. {
  37. #if CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT
  38. char buff[sizeof(ASSERT_STR) + 11 + 1] = ASSERT_STR;
  39. ra_to_str(&buff[sizeof(ASSERT_STR) - 1]);
  40. esp_system_abort(buff);
  41. #else
  42. char addr[11] = { 0 };
  43. char buff[200];
  44. char lbuf[5];
  45. uint32_t rem_len = sizeof(buff) - 1;
  46. uint32_t off = 0;
  47. itoa(line, lbuf, 10);
  48. if (!spi_flash_cache_enabled()) {
  49. if (esp_ptr_in_drom(file)) {
  50. file = CACHE_DISABLED_STR;
  51. }
  52. if (esp_ptr_in_drom(func)) {
  53. ra_to_str(addr);
  54. func = addr;
  55. }
  56. if (esp_ptr_in_drom(expr)) {
  57. expr = CACHE_DISABLED_STR;
  58. }
  59. }
  60. const char *str[] = {ASSERT_STR, func ? func : "\b", " ", file, ":", lbuf, " (", expr, ")"};
  61. for (int i = 0; i < sizeof(str) / sizeof(str[0]); i++) {
  62. uint32_t len = strlen(str[i]);
  63. uint32_t cpy_len = MIN(len, rem_len);
  64. memcpy(buff + off, str[i], cpy_len);
  65. rem_len -= cpy_len;
  66. off += cpy_len;
  67. if (rem_len == 0) {
  68. break;
  69. }
  70. }
  71. buff[off] = '\0';
  72. esp_system_abort(buff);
  73. #endif /* CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT */
  74. }
  75. void __attribute__((noreturn)) __assert(const char *file, int line, const char *failedexpr)
  76. {
  77. __assert_func(file, line, NULL, failedexpr);
  78. }
  79. /* No-op function, used to force linker to include these changes */
  80. void newlib_include_assert_impl(void)
  81. {
  82. }