GDB – Debugging In Assembly

If you haven’t debugged in assembly yet, you haven’t debugged enough :-)  Debugging assembly in gdb can be tricky at times.

A normal debugging session involving assembly language consists of the following steps –

  1. Launch the program in gdb and set the required breakpoint(s).
  2. When the breakpoint is hit, use the disassemble command to view the assembly language of the current frame.
  3. Use the frame command and figure out the program counter and thus your location in the assembly that gdb produced in the last step.
  4. Use the si and ni commands to step in and step over the instructions in assembly language.

The problem is that this may require a bit of scrolling to do if the disassembled function is very long.  The display command can be used to solve this problem.

The display command in gdb allows a user to configure the variables that should show up each time the program is suspended.

(gdb) help display
Print value of expression EXP each time the program stops.
/FMT may be used before EXP as in the “print” command.
/FMT “i” or “s” or including a size-letter is allowed,
as in the “x” command, and then EXP is used to get the address to examine
and examining is done as in the “x” command.

With no argument, display all currently requested auto-display expressions.
Use “undisplay” to cancel display requests previously made.

One can use the display command to view the next instruction that is about to be executed.  The pointer to the instruction yet to be executed is stored in the computer’s program counter (also known as the instruction pointer).   The following command will show the next 3 instructions that are pointed to by the program counter.

display /3i $pc

The /3i above causes three instructions to be printed and $pc references the program counter register.  A sample output is shown below.

1: x/3i $pc

0x401175 <main+37>:     call   0x4103f0 <__main>
0x40117a <main+42>:     lea    0xffffffe8(%ebp),%eax
0x40117d <main+45>:     mov    %eax,(%esp)


2 thoughts on “GDB – Debugging In Assembly

  1. Pingback: gdb | bluegene

Leave a Reply

Your email address will not be published. Required fields are marked *