Tuesday, April 20, 2010

GDB cheat sheet

Here is the cheat sheet for GDB in Linux
  • Enable the core file
% ulimit -a
% ulimit -c 500000

Set maximum core file size to 500K.

  • List all  proc information and function list
(gdb) info proc
(gdb) info functions

  • Disasseeble
(gdb) disass main

  • Display and clear breakpoints
(gdb) info breakpoints
(gdb) delete breakpoints 1
  • Watching it live
(gdb) break 188
(gdb) commands 1
print variable
continue
end

Note: break at 188 lines and then set a group of commands at break point 1.
  • Break when a specific address is accessed. Refer to this article.
(gdb)  watch *((int*)0xABCDEF)

As an simple example:
define cls
shell clear
end
document cls
clears screen for gdb
end

Example Macros for STL debugging


  • Handle Signal
(gdb) i handle
(gdb) i signal
(gdb) handle SIG32 nostop print pass

  • Multiple threads
(gdb) b foo.cpp:13 thread 28 if x>lim

  • Remote debugging
Server side: gdbserver:9999 -attach pid
Client side: gdb program
(gdb) handle SIGTRAP nonstop noprint pass
(gdb) target remote gdbserverip:9999

  • Store history
Convenience variables:
(gdb) set $table=*table_ptr
(gdb) show conv

Checkpoint
(gdb) checkpoint
(gdb) i checkpoint
(gdb) restart checkpoint-id


  • See Macros in program
g++ -gdwarf-2 -g3 a.cpp

  • Why the program is stopped
(gdb) i prog

  • exit a loop
(gdb) until line-no
  • print, info, show
print: print value of expression
info: show things about the program being debugged
show: show things about the debugger

No comments: