Using GDB effectively to debug programs

Posted: September 18, 2011 in Background Information

Let’s discuss a few ways of effectively using GDB while debugging programs. Though DDD aims at providing a reasonable UI for GDB, it still uses the command line interface of GDB for it’s work. So let’s see what options are available in GDB. There are a lot of tutorials which can be found online regarding the same, but I wish to keep this a bit naive for beginners.

This is just a note on my experiences with gdb. You can look at tutorials from betterexplained.com and RMS for more features.

  • What are the prerequisites to be done before starting to debug the program using gdb?
    • Firstly, you have to compile the program with the ‘-g’ option to provide debugging symbols for gdb. This can be done by:
      • user@host:~$ gcc program.c -o program_executable -g
    • Next, you have to invoke the executable using ‘gdb’. In my Ubuntu system, I normally do by:
      • user@host:~$ gdb program_executable
  • How do I set breakpoints in the program?
    • In case of setting breakpoints in source code:
      • (gdb) break program.c:29    (set breakpoint at line number 29 of source file program.c)
      • (gdb) b program.c:29
      • (gdb) b 29 (This case is when there is only one C file compiled)
    • In case of setting breakpoints at specific addresses:
      • (gdb) break *0x8048255
      • (gdb) b *0x8048255
  • How do I disassemble only particular addresses within a program using gdb?
    • In case of disassembly of a function using it’s name:
      • (gdb) disas <name>
    • In case of disassembly using addresses:
      • (gdb) info line <name_of_function> (optional)
      • (gdb) disas <start_addr> <end_addr>
  • How do I print the value of a register?
    • To see values in all registers:
      • info registers
    • To see value in individual registers:
      • print (or) x/bwx <register> (register=$eax/$ebx/$ecx/$edx/$esp/$ebp/$eip/$edi/$esi)
  • In what ways can I view the value of memory address/variable?
    • HEX:
      • x/x <var>           (only 1 byte)
      • x/2x <var>         (2 bytes)
      • x/2bwx <var>   (2 words)
    • As string:
      • x/s <var>
    • As character:
      • x/c <var>
  • How do dump the stack?
    • Get 32 words from stack:
      • x/32bwx $esp
  • How can I disassemble a particular instruction at a particular address?
    • At any address:
      • x/i <address>
    • The next instruction:
      • x/i $esp
  • How do I step into functions?
    • Step by source lines:
      • step (OR) s
    • Step by instructions:
      • stepi (OR) si
  • How do I step over functions?
    • Step over source lines:
      • next (OR) n
    • Step over instructions:
      • nexti (OR) ni
  • How do I continue till I meet another breakpoint?
    • continue (OR) c
  • How do I perform multiple operations with one command?
    • (gdb) define function1
    • > stepi
    • > echo “ebp = “
    • > x/bwx $esp
    • > end
    • (gdb) function1
    • esp = 0xbfffd89c
    • (gdb)

There are a lot more advanced features that can be used, I’ll keep adding new stuff as and when I encounter them. 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s