Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Esp32CoreDumpUtil.hxx
Go to the documentation of this file.
1
35#ifndef _FREERTOS_DRIVERS_ESP32_ESP32COREDUMPUTIL_HXX_
36#define _FREERTOS_DRIVERS_ESP32_ESP32COREDUMPUTIL_HXX_
37
38#include "sdkconfig.h"
39#include "os/Gpio.hxx"
40#include "utils/FileUtils.hxx"
41#include "utils/logging.h"
43
44#include <esp_core_dump.h>
45#include <esp_log.h>
46
47namespace openmrn_arduino
48{
49
61{
62public:
65 static bool is_present()
66 {
67 esp_log_level_set("esp_core_dump_flash", ESP_LOG_NONE);
68 esp_err_t res = esp_core_dump_image_check();
69 esp_log_level_set("esp_core_dump_flash", ESP_LOG_WARN);
70 return res == ESP_OK;
71 }
72
77 static void display(const char *output_path = nullptr)
78 {
79 if (is_present())
80 {
81 esp_core_dump_summary_t details;
82 if (esp_core_dump_get_summary(&details) == ESP_OK)
83 {
84 // Convert the core dump to a text file
85 string core_dump_summary =
86 StringPrintf("Task:%s (%d) crashed at PC %08x\n",
87 details.exc_task, details.exc_tcb, details.exc_pc);
88 core_dump_summary += StringPrintf("Registers:\n");
89 for (size_t idx = 0; idx < 16; idx += 4)
90 {
91 core_dump_summary +=
92 StringPrintf(
93 "A%02zu: 0x%08x A%02zu: 0x%08x A%02zu: 0x%08x A%02zu: 0x%08x\n",
94 idx, details.ex_info.exc_a[idx],
95 idx + 1, details.ex_info.exc_a[idx + 1],
96 idx + 2, details.ex_info.exc_a[idx + 2],
97 idx + 3, details.ex_info.exc_a[idx + 3]);
98 }
99 core_dump_summary +=
100 StringPrintf("EXCCAUSE: %08x EXCVADDR: %08x\n",
101 details.ex_info.exc_cause, details.ex_info.exc_vaddr);
102 if (details.ex_info.epcx_reg_bits)
103 {
104 core_dump_summary += "EPCX:";
105 for (size_t idx = 0; idx < 8; idx++)
106 {
107 if (details.ex_info.epcx_reg_bits & BIT(idx))
108 {
109 core_dump_summary +=
110 StringPrintf("%zu:%08x ", idx, details.ex_info.epcx[idx]);
111 }
112 }
113 core_dump_summary += "\n";
114 }
115 core_dump_summary += "Backtrace:";
116 for (size_t idx = 0; idx < details.exc_bt_info.depth; idx++)
117 {
118 core_dump_summary +=
119 StringPrintf(" 0x%08x", details.exc_bt_info.bt[idx]);
120 if (details.exc_bt_info.corrupted)
121 {
122 core_dump_summary += "(corrupted)";
123 }
124 }
125 core_dump_summary += "\n";
126 LOG_ERROR("Core dump:\n%s", core_dump_summary.c_str());
127 if (output_path)
128 {
129 write_string_to_file(output_path, core_dump_summary);
130 }
131 }
132 }
133 }
134
136 static void cleanup()
137 {
138 if (is_present())
139 {
140 ESP_ERROR_CHECK_WITHOUT_ABORT(esp_core_dump_image_erase());
141 }
142 }
143};
144
145} // namespace openmrn_arduino
146
148
149#endif // _FREERTOS_DRIVERS_ESP32_ESP32COREDUMPUTIL_HXX_
void write_string_to_file(const string &filename, const string &data)
Opens (or creates) a file, truncates it and overwrites the contents with what is given in a string.
Utility class containing methods related to esp32 core dumps.
static void display(const char *output_path=nullptr)
Utility method that displays a core dump (if present).
static void cleanup()
Utility method to cleanup a core dump.
static bool is_present()
Utility method to check if a core dump is present.
#define LOG_ERROR(message...)
Shorthand for LOG(LEVEL_ERROR, message...). See LOG.
Definition logging.h:124