esp_core_dump.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef ESP_CORE_DUMP_H_
  7. #define ESP_CORE_DUMP_H_
  8. #include "sdkconfig.h"
  9. #include <stddef.h>
  10. #include "esp_err.h"
  11. #include "esp_private/panic_internal.h"
  12. #include "esp_core_dump_summary_port.h"
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. #define APP_ELF_SHA256_SZ (CONFIG_APP_RETRIEVE_LEN_ELF_SHA + 1)
  17. #if CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH && CONFIG_ESP_COREDUMP_DATA_FORMAT_ELF
  18. /**
  19. * @brief Core dump summary, Most meaningful contents of the core dump
  20. * are accommodated in this structure
  21. */
  22. typedef struct {
  23. uint32_t exc_tcb; /*!< TCB pointer to the task causing exception */
  24. char exc_task[16]; /*!< Name of the task that caused exception */
  25. uint32_t exc_pc; /*!< Program counter for exception */
  26. esp_core_dump_bt_info_t exc_bt_info; /*!< Backtrace information for task causing exception */
  27. uint32_t core_dump_version; /*!< Core dump version */
  28. uint8_t app_elf_sha256[APP_ELF_SHA256_SZ]; /*!< Crashing application's SHA256 sum as a string */
  29. esp_core_dump_summary_extra_info_t ex_info; /*!< Architecture specific extra data */
  30. } esp_core_dump_summary_t;
  31. #endif /* CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH && CONFIG_ESP_COREDUMP_DATA_FORMAT_ELF */
  32. /**************************************************************************************/
  33. /******************************** EXCEPTION MODE API **********************************/
  34. /**************************************************************************************/
  35. /**
  36. * @brief Initializes core dump module internal data.
  37. *
  38. * @note Should be called at system startup.
  39. */
  40. void esp_core_dump_init(void);
  41. /**
  42. * @brief Saves core dump to flash.
  43. *
  44. * The structure of data stored in flash is as follows:
  45. *
  46. * | TOTAL_LEN | VERSION | TASKS_NUM | TCB_SIZE |
  47. * | TCB_ADDR_1 | STACK_TOP_1 | STACK_END_1 | TCB_1 | STACK_1 |
  48. * . . . .
  49. * . . . .
  50. * | TCB_ADDR_N | STACK_TOP_N | STACK_END_N | TCB_N | STACK_N |
  51. * | CHECKSUM |
  52. *
  53. * Core dump in flash consists of header and data for every task in the system at the moment of crash.
  54. * For flash data integrity, a checksum is used at the end of core the dump data.
  55. * The structure of core dump data is described below in details.
  56. * 1) Core dump starts with header:
  57. * 1.1) TOTAL_LEN is total length of core dump data in flash including the checksum. Size is 4 bytes.
  58. * 1.2) VERSION field keeps 4 byte version of core dump.
  59. * 1.2) TASKS_NUM is the number of tasks for which data are stored. Size is 4 bytes.
  60. * 1.3) TCB_SIZE is the size of task's TCB structure. Size is 4 bytes.
  61. * 2) Core dump header is followed by the data for every task in the system.
  62. * Task data are started with task header:
  63. * 2.1) TCB_ADDR is the address of TCB in memory. Size is 4 bytes.
  64. * 2.2) STACK_TOP is the top of task's stack (address of the topmost stack item). Size is 4 bytes.
  65. * 2.2) STACK_END is the end of task's stack (address from which task's stack starts). Size is 4 bytes.
  66. * 3) Task header is followed by TCB data. Size is TCB_SIZE bytes.
  67. * 4) Task's stack is placed after TCB data. Size is (STACK_END - STACK_TOP) bytes.
  68. * 5) The checksum is placed at the end of the data.
  69. */
  70. void esp_core_dump_to_flash(panic_info_t *info);
  71. /**
  72. * @brief Print base64-encoded core dump to UART.
  73. *
  74. * The structure of core dump data is the same as for data stored in flash (@see esp_core_dump_to_flash) with some notes:
  75. * 1) The checksum is not present in core dump printed to UART.
  76. * 2) Since checksum is omitted TOTAL_LEN does not include its size.
  77. * 3) Printed base64 data are surrounded with special messages to help user recognize the start and end of actual data.
  78. */
  79. void esp_core_dump_to_uart(panic_info_t *info);
  80. /**************************************************************************************/
  81. /*********************************** USER MODE API ************************************/
  82. /**************************************************************************************/
  83. /**
  84. * @brief Check integrity of coredump data in flash.
  85. * This function reads the coredump data while calculating their checksum. If it
  86. * doesn't match the checksum written on flash, it means data are corrupted,
  87. * an error will be returned. Else, ESP_OK is returned.
  88. *
  89. * @return `ESP_OK` if core dump is present and valid, `ESP_ERR_NOT_FOUND` if no core dump
  90. * is stored in the partition, `ESP_ERR_INVALID_SIZE` or `ESP_ERR_INVALID_CRC`
  91. * if the core dump is corrupted, other errors when unable to access flash, in that
  92. * case please refer to \see esp_err_t
  93. */
  94. esp_err_t esp_core_dump_image_check(void);
  95. /**
  96. * @brief Retrieves address and size of coredump data in flash.
  97. * This function is always available, even when core dump is disabled in menuconfig.
  98. *
  99. * @param out_addr pointer to store image address in flash.
  100. * @param out_size pointer to store image size in flash (including checksum). In bytes.
  101. *
  102. * @return ESP_OK on success, otherwise \see esp_err_t
  103. */
  104. esp_err_t esp_core_dump_image_get(size_t* out_addr, size_t *out_size);
  105. /**
  106. * @brief Erases coredump data in flash. esp_core_dump_image_get() will then return
  107. * ESP_ERR_NOT_FOUND. Can be used after a coredump has been transmitted successfully.
  108. * This function is always available, even when core dump is disabled in menuconfig.
  109. *
  110. * @return ESP_OK on success, otherwise \see esp_err_t
  111. */
  112. esp_err_t esp_core_dump_image_erase(void);
  113. #if CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH && CONFIG_ESP_COREDUMP_DATA_FORMAT_ELF
  114. /**
  115. * @brief Get panic reason from the core dump.
  116. *
  117. * This function retrieves the panic reason from the core dump data and copies it to the provided buffer.
  118. *
  119. * @param[in,out] reason_buffer Pointer to the buffer where the panic reason will be copied.
  120. * @param[in] buffer_size Size of the destination buffer in bytes.
  121. * @return
  122. * - ESP_OK if the panic reason was successfully copied.
  123. * - ESP_ERR_INVALID_ARG if reason_buffer is NULL or buffer_size is 0.
  124. * - Other error codes indicating the outcome of the core dump retrieval.
  125. * - ESP_ERR_NOT_FOUND if the panic reason is not found in the core dump.
  126. *
  127. * Example usage:
  128. * @code{c}
  129. char panic_reason[200];
  130. esp_err_t err = esp_core_dump_get_panic_reason(panic_reason, sizeof(panic_reason));
  131. if (err == ESP_OK) {
  132. ESP_LOGW(TAG, "%s", panic_reason);
  133. }
  134. * @endcode
  135. */
  136. esp_err_t esp_core_dump_get_panic_reason(char *reason_buffer, size_t buffer_size);
  137. /**
  138. * @brief Get the summary of a core dump.
  139. *
  140. * @param summary Summary of the core dump
  141. *
  142. * @return ESP_OK on success, otherwise \see esp_err_t
  143. *
  144. * @note This function works only if coredump is stored in flash and in ELF format
  145. *
  146. * Example usage:
  147. * @code{c}
  148. * esp_core_dump_summary_t *summary = malloc(sizeof(esp_core_dump_summary_t));
  149. * if (summary) {
  150. * if (esp_core_dump_get_summary(summary) == ESP_OK) {
  151. * // Do stuff
  152. * }
  153. * }
  154. * free(summary);
  155. * @endcode
  156. */
  157. esp_err_t esp_core_dump_get_summary(esp_core_dump_summary_t *summary);
  158. #endif /* CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH && CONFIG_ESP_COREDUMP_DATA_FORMAT_ELF */
  159. #ifdef __cplusplus
  160. }
  161. #endif
  162. #endif