How to see registers in gdb when using gcc
2006-04-07 by Ned Konz
You probably already know this, but I just figured it out and thought that maybe there's someone else out there who hadn't yet... If you're using gcc, the way that register names are defined means that the compiler doesn't know about them. That is, the preprocessor rewrites the code before it gets to the compiler, so that a reference to a register like PORTA = 3; becomes something like (*(volatile unsigned char *)0x3B) = 3; So the PORTA name is not available to the compiler, linker, or debugger. However, the GNU ld linker is very flexible, so you can give it the missing names. Just make a text file with the definitions of the registers in it (remember that these are memory addresses in data space, more or less, so that you have to add 0x800000 or 0x800020 to them (depending on whether they're in memory space or I/O space, respectively)). If they're defined in the avr/io*.h as _SFR_IO8 or _SFR_IO16 , add 0x800020 to them. Otherwise add 0x800000 to them. Another way to tell is if their address is less than 0x60: if it is, they're in I/O space. Each register definition should look like a C assignment expression: /* SFR memory addresses */ ACSR = 0x800028; ADC = 0x800024; ADCH = 0x800025; ADCL = 0x800024; ADCSR = 0x800026; ADCSRA = 0x800026; ADCW = 0x800024; ADMUX = 0x800027; ASSR = 0x800050; DDRA = 0x80003a; DDRB = 0x800037; DDRC = 0x800034; etc. Mine for the Atmega128 is attached; I generated this with a Perl script. I called mine "iom128.a"; the linker seems to like "*.a" files just fine. Then just list the file on the linker command line, along with your object files: avr-gcc -mmcu=atmega128 howToDoThings.o iom128.a -o howToDoThings.elf Just remember that you've just defined some symbols; you haven't really told the debugger what they meant yet. But the debugger seems to believe that they're 8-bit wide items. (gdb) whatis PORTA type = <variable (not text or data), no debug info> (gdb) p &PORTA $1 = (<variable (not text or data), no debug info> *) 0x80003b "" Still it works pretty well: (gdb) x/20xb &PINC "display 20 hex bytes of memory (examine) at address of PINC" 0x800033: 0x27 0x00 0x00 0xf9 0x00 0x00 0x3f 0x00 0x80003b: 0x00 0x00 0x00 0x00 0x0a 0x00 0x00 0x00 0x800043: 0x00 0x00 0x00 0x00 (gdb) x &PINC "display memory (examine) at address of PINC" 0x800033: 0x27 (gdb) p PINC "display PINC contents (print it)" $6 = 39 '\'' (gdb) p/x PINC "display it in hex" $7 = 0x27 (gdb) p PINC=0xff "set location" $8 = -1 'ΓΏ' (gdb) p/x PINC "display it as hex" $9 = 0xff (gdb) p PINC=0 "set it to 0" $10 = 0 '\0' (gdb) p PINC $12 = 0 '\0'