dankeboy36 / esp-exception-decoder

ESP8266/ESP32 Exception Decoder Extension for the Arduino IDE
MIT License
62 stars 2 forks source link

Line numbers of main .ino file seem to be off by one? #13

Closed php4fan closed 7 months ago

php4fan commented 7 months ago

I have this sketch:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <string>

const int SERIAL_BAUD_RATE = 74880;

const int LED_PIN = 5; // Thing's onboard, green LED

const int DOWNLOAD_MAX_TIME = 180;
const int DOWNLOAD_MAX_TIME_NODATA = 60;

const int DISPLAY_WIDTH=800;
const int DISPLAY_HEIGHT=480;

String client_ssid="Sixty-Seven";
String client_password="8791oetifiw";

uint8_t output_black_buffer[DISPLAY_WIDTH / 8] = {0}; 
uint8_t output_red_buffer[DISPLAY_WIDTH / 8] = {0}; 

int currentIdx = 0;
int out_idx = 0;

void setup() 
{
  Serial.begin(SERIAL_BAUD_RATE);
  Serial.println("=============== Setup =====================");
  WiFi.mode(WIFI_OFF);
  WiFi.persistent(false);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, HIGH);

  for (int i = 0; i < 96000; i++) {
    usePixels();
    if (i % 128 == 127) {
      ESP.wdtFeed();
      Serial.println(".");
    }
  }
}

void loop() {

}

void usePixels() {
  if (currentIdx % 2 == 0) {
    output_black_buffer[out_idx] = 1;
  }
  else {
    output_red_buffer[out_idx++] = 1; 
    if ((currentIdx / 2) == DISPLAY_WIDTH / 8 - 1) {

      out_idx = 0;
    }
  }

   currentIdx++;
}

which for reason that I haven't figured out yet, causes a LoadStoreAlignmentCause exception:

Fatal exception 9(LoadStoreAlignmentCause):
epc1=0x402017ef, epc2=0x00000000, epc3=0x00000000, excvaddr=0x01010105, depc=0x00000000

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

Exception (9):
epc1=0x402017ef epc2=0x00000000 epc3=0x00000000 excvaddr=0x01010105 depc=0x00000000

>>>stack>>>

ctx: cont
sp: 3ffffe30 end: 3fffffd0 offset: 0150
3fffff80:  00017700 3ffee750 3ffee750 402019d8  
3fffff90:  3fffdad0 3ffee750 0000027f 402010e5  
3fffffa0:  feefeffe feefeffe feefeffe 3ffee8c4  
3fffffb0:  3fffdad0 00000000 3ffee898 40202568  
3fffffc0:  feefeffe feefeffe 3fffdab0 40101179  
<<<stack<<<

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

The decoder decodes it as:

Exception 9: LoadStoreAlignmentCause: Load or store to an unaligned address
PC: 0x402017ef:  is in Print::write(char const*) (/home/teo/.arduino15/packages/esp8266/hardware/esp8266/3.1.2/cores/esp8266/Print.h:59).
EXCVADDR: 0x01010105

Decoding stack results
0x402019d8:  is in Print::println(char const*) (/home/teo/.arduino15/packages/esp8266/hardware/esp8266/3.1.2/cores/esp8266/Print.cpp:238).
0x402010e5: setup() at /home/teo/Documents/xxxx/xxxx.ino:33
0x40202568: loop_wrapper() at /home/teo/.arduino15/packages/esp8266/hardware/esp8266/3.1.2/cores/esp8266/core_esp8266_main.cpp:255

Note where it says xxxx.ino:33. That is the main .ino file of the sketch (code above). These are lines 33 and 34 respectively, as counted by the Arduino IDE:

33   for (int i = 0; i < 96000; i++) {
34     usePixels();

I don't think a for loop can itself be an entry in the stack, right? The function usePixels() seems much more likely.

Maybe you are counting line numbers starting from 0?

dankeboy36 commented 7 months ago

Thanks for giving this extension a try and submitting a quality bug report.

I looked into it and could reproduce this mysterious line 33 issue, but this is not a decoder issue. I am open to further investigation, though.

Maybe you are counting line numbers starting from 0?

There is neither line number counting logic in the decoder code nor special handling of the raw gdb output. You see in the decoder terminal what is from gdb. (With some additional ANSI coloring). The easiest way to see the decoder output is to check the logs inside the Arduino IDE. (See details on how to switch between the output channels: https://github.com/dankeboy36/esp-exception-decoder/issues/10#issuecomment-1727811557)

https://github.com/dankeboy36/esp-exception-decoder/assets/111981763/63a88942-98fb-45fe-a7ba-a19d09cefa21

In my case, it was:

parseGDBLine, failed: 
parseGDBLine, fallback: {"address":"0x4020177f","line":" is in Print::write(char const*) (/Users/akos.kitta/Library/Arduino15/packages/esp8266/hardware/esp8266/3.1.2/cores/esp8266/Print.h:59)."}
parseGDBLine, failed: 59                return write((const uint8_t *) str, strlen_P(str));
parseGDBLine, fallback: {"address":"0x40201868","line":" is in Print::println(char const*) (/Users/akos.kitta/Library/Arduino15/packages/esp8266/hardware/esp8266/3.1.2/cores/esp8266/Print.cpp:238)."}
parseGDBLine, failed: 238   template<typename T, typename... P> inline size_t Print::_println(T v, P... args)
parseGDBLine, OK: {"address":"0x402010e5","method":"setup()","file":"/Users/akos.kitta/Documents/Arduino/gh_13/gh_13.ino","line":"33"}
parseGDBLine, failed: 33      for (int i = 0; i < 96000; i++) {
parseGDBLine, OK: {"address":"0x40202050","method":"loop_wrapper()","file":"/Users/akos.kitta/Library/Arduino15/packages/esp8266/hardware/esp8266/3.1.2/cores/esp8266/core_esp8266_main.cpp","line":"255"}
parseGDBLine, failed: 255           setup_done = true;
parseGDBLine, failed: 0x40100e35 is at /Users/akos.kitta/Library/Arduino15/packages/esp8266/hardware/esp8266/3.1.2/cores/esp8266/cont.S:81.
parseGDBLine, failed: 81        movi    a2, cont_norm

I agree that something is odd with the decoder output, but it's not here:

{"address":"0x402010e5","method":"setup()","file":"/Users/akos.kitta/Documents/Arduino/gh_13/gh_13.ino","line":"33"}
parseGDBLine, failed: 33      for (int i = 0; i < 96000; i++) {

You can ask at https://github.com/arduino/arduino-builder or use the Arduino CLI forum, why there is this line number problem: https://forum.arduino.cc/c/software/arduino-cli/89