Where I provide hints on using avr-gdb and bloom to debug code on the ATmega328P.
- gdb Resources
- Bloom (replaces avarice)
- Microchip SNAP (replaces the Atmel Dragon)
- Atmel-ICE Debugger
- GDB Documentation
- Guide to Faster, Less Frustrating Debugging - Norman Matloff
- Check the version number on avr-gdb as of December 2021, it is 11.1. On Ubuntu, the latest version is 8.1 from 2018 which had quite a few bugs. See notes on this page as to how to ensure you have the latest version.
- Be sure to use target remote:1442 in gdb, if you are having connection issues.
- If you find your self executing a lengthy command quite a bit (such as “monitor reset init”), use “define” to create a shorter command for it. If you put it into the .gdbinit file (as I have), it will remember from session to session.
- gdb can be incredibly easy to use. Norman Matloff’s tutorial is great for illustrating the commands needed. I recommend reading and practicing with it, its well worth it!
- Another great tutorial is Beej’s Quick Guide to GDB, particularly for the gdb –tui guidance.
- Start with using gdb to replace printf statements. Run code, have it stop by using a “breakpoint function name” or “breakpoint line number”, then use “print” or “display” to get the values of variables. This will work the same as inserting print statements, however, it doesn’t affect your code.
- It took me less time to learn gdb, and write these two tutorials than the time I spent attempting to debug Visual Studio Code. Your mileage may vary, however, a simple gdb/code editor setup is fast and easy to learn and use.
- If your code ends, you will see “exit(status=0)….”. To restart the program, simply issue a monitor reset, which will reset the processor. Press “c” and the program will begin running again.
- gdb operates in at least two modes:
- command, indicated by (gdb) prompt. This is where you will enter all of the commands to interact with gdb
- execution, indicated by a lack of a (gdb) prompt. This means the program is running and hitting a breakpoint or watchpoint will return you back to command mode. Or you can hit Ctrl-C which will interrupt the process and put into command mode.
- Enter the command layout src or start by using gdb –tui and the window will divide into two horizontal windows. The top window will show 20 lines of source and the bottom window will continue to be the command window. This is a great way to view a breakpoint. For example, set a breakpoint on a function name, then “n(ext)” through the code to view execution. Once you have entered “n”, you can simply hit the return key and it will advance using the next command.
- If in tui, use info win to show the number of lines per window then use wh [window name] n to change the number of lines to n. For example, use wh src 40 to change the window showing the source code to 40 lines. Use fs [window name] to change the window focus. Typically, it is in the source (src) window, and to change the focus to the command window, use fs cmd.
- Note found elsewhere, I’ll link to it when I find it: “remember to use -g only for the image you pass to gdb, not the firmware you load into the device (if it can grok elf). Also another very critical prerequisite for C programs is -O0 (which is usually default). But if you use -Os or something, you can get very unpredictable debugging results. This btw. makes debugging some code on avrs for example almost impossible, as they often rely on -Os to fit the image in there. But.. it is very possible and comfy to debug simple optimized C programs in assembly, if you know roughly what to expect. Use objdump -dS on the optimized .elf file with debugging info and you’ll roughly see what assembly code was produced for what C code.”