[PLUG] gdb tip you should know

Galen Seitz galens at seitzassoc.com
Fri Oct 18 16:54:54 UTC 2019


I occasionally have the need to use gdb for debugging, but not often
enough to be a gdb expert.  Typically this debugging is on embedded
systems that have microcontrollers with many peripherals that need to be
controlled, such as AVR or Cortex M devices.  The peripheral register
addresses and the bits within them are often defined using C
preprocessor macros.  This is fine for coding, but when debugging,
information regarding these macros is typically lost.  That means if I
want to examine a hardware register, such as a GPIO port, I have to look 
up the appropriate peripheral address, and then do an examine memory 
command at that address:

# Read port E output data register
(gdb) x/x 0x40021014
0x40021014:	0x0000007f

This morning I was looking for a script that would turn preprocessor
macros into gdb macros, in hopes of simplifying this.  That's when I 
discovered you can tell gcc to add preprocessor macros to the debug 
symbols.  When compiling for debugging, instead of passing '-g', you 
pass '-ggdb3'.  Now you can do this:

# Read port E output data register
(gdb) p/x GPIOE->ODR
$1 = 0x7f

This is far, far easier than having to look up addresses and bit 
positions.  The only hitch is that when you halt your code, you have to 
be stopped in a context where the preprocessor macros were present. 
Most of the time this won't be a problem.

Even if you're not debugging code that talks to hardware, it can be 
quite useful to print out the value of a macro, particularly if it's one 
that involves a calculation or another macro.

// A define in my code.
#define ADC_HV_DIVISOR	((499 + 200e3) / 499)

# What's that value?
(gdb) p ADC_HV_DIVISOR
$2 = 401.80160320641284


There are some other useful tips here.  This particular tip is #5 in the 
article.
<https://blogs.oracle.com/linux/8-gdb-tricks-you-should-know-v2>


galen
-- 
Galen Seitz
galens at seitzassoc.com



More information about the PLUG mailing list