AVR GCC Debugging

From Wiki

Jump to: navigation, search

Return to avr-gcc main page

Contents

Introduction

This page discusses an easy way to set up a graphical debugging/emulation environment on GNU/Linux-based systems, for debugging AVR programs built with gcc-avr and avr-libc.

System Overview

In this walkthrough, we will be using the ddd graphical debugger, which is available on most Linux distributions. We will discuss the steps needed to run the gdb-avr debugger within ddd, and communicate with the simulavr avr simulator, and simplify this into a set of Makefile rules.

Credits

Much of this walkthrough is based on the excellent Psychogenic tutorial.

Preparation

Before starting this walkthrough, ensure that you have the following installed:

  • gcc-avr
  • binutils-avr
  • avr-libc
  • simulavr
  • gdb-avr
  • ddd

All these packages are available on most Linux distros

Setting up your Makefile

Add the following rules to your makefile (which assumes that the variable PRG is already defined and set to the name of your program. For instance, if your Makefile builds fred.elf, fred.hex etc, then make sure you have:

PRG = "fred"

defined early in the Makefile.

Also, ensure that any assembler files are compiled with -Wa,-gstabs arguments. For example,

ASMFLAGS = $(CFLAGS) -Wa,-gstabs

   ...

%.o: %.S
	$(CC) $(ASMFLAGS) -c $<

Now, here are the debugging-related Makefile targets:

# debugging-related targets

ddd: gdbinit
	ddd --debugger "avr-gdb -x $(GDBINITFILE)"

gdbserver: gdbinit
	simulavr --device $(MCU_TARGET) --gdbserver

gdbinit: $(GDBINITFILE)

$(GDBINITFILE): $(PRG).hex
	@echo "file $(PRG).elf" > $(GDBINITFILE)
	
	@echo "target remote localhost:1212" \
		                >> $(GDBINITFILE)
	
	@echo "load"        >> $(GDBINITFILE) 
	@echo "break main"  >> $(GDBINITFILE)
	@echo "continue"    >> $(GDBINITFILE)
	@echo
	@echo "Use 'avr-gdb -x $(GDBINITFILE)'"

These rules create two user-level targets in your makefile, 'ddd' and 'gdbserver'.

Note that with the makefile rules above, any changes in your program will cause the program to be rebuilt automatically prior to running executing the debugging Makefile targets.

Running the debugger

The debugger comes in two parts - the 'debug server', which consists of simulavr, and the 'debug client', which consists of gdb-avr running within ddd.

To perform a debugging session, you first launch the debug server, then launch the debug client to connect to that server.

Let's first start up the debug server (simulavr). Open one terminal window, and type:

$ make gdbserver

You should see simulavr start up, and make some noises about waiting for client connections.

Now, you're ready to start up the graphical debug client. Now, type:

$ make ddd

You should see the ddd graphical debugger start up as a 'debug client', and load in your program. By now, you should see your program's source code showing in the ddd main window, and the code halted at a breakpoint at the start of your main function. What could possibly be more convenient than that?

From here on in, you should be ok to continue on your own. ddd, while not quite as polished as MSVS and the like, is still an excellent graphical debugger. You can view your registers changing on the fly by opening up a registers window (Main menu: Status/Registers).

Personal tools