Debugger showing wrong variable values makes debugging nearly impossible
Hello,
I'm a bit surprised i don't find a previous discussion about this topic, as i am not the only one experiencing it in our company:
- STM32F765 (Cortex M7) based board, own design
- connected with ST-Link
- happens both with OpenOCD and gdb debugger configuration
- C++
When stepping through the code, variable values are changing to other values randomly, with the result that tracing errors becomes nearly impossible. I tried some workarounds like copying the interesting values to other variables in different scopes, or disabling optimization for the relevant functions (unfortunately i cannot disable optimization globally, as it does not fit in any more), or declaring variables as volatile, but with only little success. Here is an example (displayed variable values in the comments):
Buffer *buf1;
volatile Buffer *buf2;
Object *obj1;
volatile Object *obj2;
void __attribute__((optimize("O0"))) Object::receive(Buffer *packet) {
// this = 0x2005ddd4 (seems this is the correct pointer)
// packet = 0x20035d90
A *a = new A;
// this = 00x200193d4
buf1 = packet;
// buf1 = 0
buf2 = packet;
// buf2 = 0
obj1 = this;
// obj1 = 0
obj2 = this;
// obj2 = 0
Object *obj3 = this;
// obj3 = 0x2005ddd4 (original this)
a->foo();
// obj3 = 0x200193d4
...
}
Are there more people having this problem? Is this a bug, or a result from the compiler reusing the registers and the debugger doesn't notice it?
Is there a way to get rid of this problem (which would be the best), or at least are there techniques how to deal with it and still be able to debug more or less comfortable?
Kind regards