identify line number which caused panic

06userit
Posts: 5
Joined: Tue Jan 19, 2021 6:02 am

identify line number which caused panic

Postby 06userit » Mon Feb 26, 2024 1:17 pm

(for those interested)
My requirement was to be able to identify line number which caused panic, and store information in a log file, when ESP32 reboots. I do not have access to the various ESP32s, which are owned by customers. So the panic could have taken place 1 day or several days ago. By storing the "right" information in a log file (on a SD card for instance), it is possible to do "cold" analysis and identify line number which caused panic.

My first idea was to read the contents of coredump partition and store it in the log file for analysis with espcoredump. But various attempts were unsuccessful (espcoredump.py correctly starts to read the coredump partition but then fails with error " current_thread_id = result['current-thread-id'] TypeError: 'NoneType' object is not subscriptable").

So instead of getting the full contents of coredump partition, it is possible to rely on the coredump partition summary. The summary contains the PC register, which allows to identify line number with python script "addr2line".

See code below and output.

The panic occurs at line 32 :
25void loop() {
26 for (int i = 0; i < 5; i++)
27 { ESP_LOGI("","i=%d panic in %d", i, 5-i);
28 delay(1000);
29 }
30 int a = 0;
31 int b = 4;
32 ESP_LOGI("","%d\n", b / a); // panic
33}

The PC register is visible in coredump summary, its value is "0x420018fa" as shown in the output :
13:42:47.277 -> [ 3103][][coredumpaddr.ino:16] setup(): [] summary->exc_bt_info.bt[] 0x420018fa i 0x0

The script addr2line allows with the PC register and elf file to identify the line number which caused panic (32 in that case) :
xtensa-esp32-elf-addr2line -pfiaC -e D:\USER\ESP32_core2.0\coredumpaddr\coredumpaddr.ino.elf 0x420018fa
0x420018fa: loop() at D:\USER\ESP32_core2.0\coredumpaddr/coredumpaddr.ino:32

Conclusion
It is possible to get the line number where panic occured with the summary of coredump partition.

Configuration
ESP32-S3
Arduino IDE 1.8.19
arduino-esp32 core 2.0.14 (based on ESP-IDF v4.4.6)
Windows 10

Code: Select all

coredumpaddr.ino
/*
 * coredumpaddr
 * display PC register value of dump core, to retrieve panic line number (using addr2line)
 */

 #include <esp_core_dump.h>

 void setup() {
  delay(3000);
  Serial.begin(115200);

  esp_core_dump_summary_t *summary = (esp_core_dump_summary_t *)malloc(sizeof(esp_core_dump_summary_t)); // get dump core summary 
   if (summary) {
       if (esp_core_dump_get_summary(summary) == ESP_OK) {
		   ESP_LOGI(""," exc_bt_info.depth %d", summary->exc_bt_info.depth); // Backtrace information for task
          for (int i=0; i<summary->exc_bt_info.depth;i++) ESP_LOGI("","summary->exc_bt_info.bt[] 0x%x i 0x%x", summary->exc_bt_info.bt[i],i);                
       }
       else ESP_LOGE("","ERROR : esp_core_dump_get_summary KO");
   }
   else ESP_LOGE("","ERROR : summary KO");
   free(summary);
   //while(true); // uncomment to see value 
 }

void loop() {
  for (int i = 0; i < 5; i++)
  { ESP_LOGI("","i=%d panic in %d", i, 5-i);
    delay(1000);
  }
  int a = 0;
  int b = 4;
  ESP_LOGI("","%d\n", b / a); // panic
}
Output
13:42:44.134 -> Rebooting...
13:42:44.134 -> ESP-ROM:esp32s3-20210327
13:42:44.134 -> Build:Mar 27 2021
13:42:44.134 -> rst:0xc (RTC_SW_CPU_RST),boot:0x28 (SPI_FAST_FLASH_BOOT)
13:42:44.134 -> Saved PC:0x42021ee6
13:42:44.134 -> SPIWP:0xee
13:42:44.134 -> mode:DIO, clock div:1
13:42:44.181 -> load:0x3fce3808,len:0x44c
13:42:44.181 -> load:0x403c9700,len:0xbd8
13:42:44.181 -> load:0x403cc700,len:0x2a80
13:42:44.181 -> entry 0x403c98d0
13:42:44.227 -> [ 94][esp32-hal-psram.c:96] psramInit(): PSRAM enabled
13:42:47.277 -> [ 3103][coredumpaddr.ino:15] setup(): [] exc_bt_info.depth 2
13:42:47.277 -> [ 3103][coredumpaddr.ino:16] setup(): [] summary->exc_bt_info.bt[] 0x420018fa i 0x0
13:42:47.277 -> [ 3105][coredumpaddr.ino:16] setup(): [] summary->exc_bt_info.bt[] 0x420025e9 i 0x1
13:42:47.277 -> [ 3113][coredumpaddr.ino:27] loop(): [] i=0 panic in 5
13:42:48.250 -> [ 4118][coredumpaddr.ino:27] loop(): [] i=1 panic in 4
13:42:49.274 -> [ 5118][coredumpaddr.ino:27] loop(): [] i=2 panic in 3
13:42:50.292 -> [ 6118][coredumpaddr.ino:27] loop(): [] i=3 panic in 2
13:42:51.259 -> [ 7118][coredumpaddr.ino:27] loop(): [] i=4 panic in 1
13:42:52.460 -> Guru Meditation Error: Core 1 panic'ed (Unhandled debug exception).
13:42:52.460 -> Debug exception reason: BREAK instr
13:42:52.460 -> Core 1 register dump:
13:42:52.460 -> PC : 0x420018fd PS : 0x00060636 A0 : 0x820025ec A1 : 0x3fcebd50
13:42:52.460 -> A2 : 0x00000005 A3 : 0x3c030120 A4 : 0x3c030146 A5 : 0x3c032ca2
13:42:52.460 -> A6 : 0x3c030120 A7 : 0x3fcec6bc A8 : 0x820018fd A9 : 0x3fcebd30
13:42:52.460 -> A10 : 0x3c030146 A11 : 0x00000000 A12 : 0x3c030146 A13 : 0x0000001b
13:42:52.460 -> A14 : 0x3c03026d A15 : 0x3c032ca2 SAR : 0x00000004 EXCCAUSE: 0x00000001
13:42:52.460 -> EXCVADDR: 0x00000000 LBEG : 0x400556d5 LEND : 0x400556e5 LCOUNT : 0xffffffff
13:42:52.460 ->
13:42:52.460 ->
13:42:52.460 -> Backtrace: 0x420018fa:0x3fcebd50 0x420025e9:0x3fcebd90
13:42:52.460 ->
13:42:52.460 ->
13:42:52.460 ->
13:42:52.460 ->
13:42:52.460 -> ELF file SHA256: 1b016c253fa30eac
13:42:52.460 ->

Who is online

Users browsing this forum: No registered users and 145 guests