I have recently started to try OpenOCD and GDB to debug a Arduino Zero (with the built-in debugger chip sketch. I am using a desktop PC with a i5 processor @ 3.30GHz and have 32GB ram running Win 10 Pro. I am getting very inconsistent results. I have 1 function call (my custom) within the sketch plus Delay, Serial.ptintln, and DigitalWrite. Examples of what I see as inconsistent: I set a breakpoint at void "loop" then do next numerous times. It only goes to the next line 2, 3, maybe 4 times then it goes to "loop" breakpoint again rather than continuing to go to the next line. loop has 22 lines, most contain various forms of variable read and writes. Another example of what I see as inconsistent: When I do a "watch" GDB only stops at one or two of that variable write. There are 6 writes to that variable in "loop". I also tried "awatch" and "rwatch" with similar results. I would like to find out what I am doing wrong. Does anyone know of a good OpenOCD/GDB tutorial for Arduino? Sketch attached.
Any help would be greatly appreciated!
GrandpaMac
Compiler optimization can result in some confusing results in the debugger. For this reason, Arduino CLI and Arduino Pro IDE have an "optimize for debug" option.
You can see compiler flags used by Arduino SAMD Boards depending whether it's in the "release" (used for normal compilations) or the "debug" (used for compiling the sketch for the purposes of debugging) mode here:
If I understand this correctly, I need to add:
"compiler.optimization_flags.release=-Os
compiler.optimization_flags.debug=-Og -g3"
to "ArduinoCore-samd/platform.txt". But I can't find a ArduinoCore-samd/platform.txt. The only platform.txt I can find is in:
" C:\Program Files (x86)\Arduino\hardware\arduino\avr". Yet I can successfully compile and run a Zero sketch. Please help!
Thank You,
GrandpaVic
GrandPaVic:
If I understand this correctly, I need to add:
"compiler.optimization_flags.release=-Os
compiler.optimization_flags.debug=-Og -g3"
to "ArduinoCore-samd/platform.txt".
No. It's already in there. I copy/pasted that from the latest release of Arduino SAMD Boards. The problem is that the Arduino IDE doesn't provide the ability to change this setting, so your sketch is always compiled in "release" mode when you use the Arduino IDE.
GrandPaVic:
But I can't find a ArduinoCore-samd/platform.txt.
GrandPaVic:
The only platform.txt I can find is in:
" C:\Program Files (x86)\Arduino\hardware\arduino\avr".
It's because Windows explorer hides the C:\Users{username}\AppData by default. You have to configure Windows Explorer to show hidden folders to see it.
"arduino-cli compile --optimize-for-debug" will compile the sketch with the debug optimization settings.
Another option is to use Arduino Pro IDE, which has a built-in debugger and a Sketch > Optimize for debugging setting:
but you should be aware that the Pro IDE is still in alpha development phase, so it's only intended for beta testing use and you should expect to regularly encounter bugs when using that program.
If you're set on continuing to use the Arduino IDE, I can provide instructions for modifying the compilation settings.
It cleared up almost everything for me. I still have one question: When I try to "display" a couple of my static variables it says "optimized-out". What does that mean? Can you point me towards a website that is helpful?
GrandpaVic
The truth is that I'm not at all knowledgeable on the topic of debugging. I own a debugger, in addition to the Arduino Zero with its integrated debugger, and I've tried the Arduino CLI and Arduino Pro IDE out with some simple "Blink" type sketches, and thought it was really cool, but I haven't delved into the subject at all beyond that. Hopefully someone else here with more knowledge on the subject will step in and give some nice information.
One thing I will say is that there is really not much of anything "Arduino-specific" about this process. So if you're using the "arduino" keyword in your searches while doing research and not getting very good results, I would try some searches without "arduino". The buld of the existing information about Arduino was written by people using the AVR boards. Due to the excellent portability of Arduino, that information is usually useful for any board. However, my experiences with trying to get started debugging AVR using open source tools was not fun at all and completely unsuccessful. It seemed like the tools that were available had been abandoned long ago. So there is not so much Arduino-specific information about debugging, but a huge amount of general information about debugging for embedded systems.
GrandPaVic:
It cleared up almost everything for me. I still have one question: When I try to "display" a couple of my static variables it says "optimized-out". What does that mean? Can you point me towards a website that is helpful?
GrandpaVic
some elementary optimization is always done, like removing unused variables or replacing not changing variables of basic types with their value
Without knowing better, for test purposes, I added lots of variables and did little with some of them except assign values. Examples: I would do a "x++;" followed by a "x--;", the compiler was smart enough to skip both. x would be "optimized-out" . In fact when doing repeated "next" in DBG it would not even stop on those lines. Even the code was "optimized-out". Another example "z = 34;" followed by "z = x:", "z = 34;" was also "optimized-out". After I added code to use the variables like "analogWright(13, x);" after "x++;" and "analogWright(13, z);" after "z = 34;" the variables and the code were not "optimized-out".
Thanks! Both of you,
GrandpaVic