gdb_tips

gdb useful tips:

break point with condition

ex:
break foo.c:123 if i == 3

commands list:

ex:
commands 1
silent # which will tell gdb to silent
print "i = %d\n", i
continue
end

watch point:

ex:
watch i
watch (i | j > 12) && i > 20 && strlen(str) > 6
(gdb) p &var1
$1 = (int *) 0x41523c0
(gdb) watch *(int *)0x41523c0
Hardware watchpoint 1: *(int *)0x41523c0

display command:
automatically print the item each time there is a pause in execution.

ex:
(gdb) disp *tmp
1: *tmp = {val = 12, left = 0x8049698, right = 0x0}
(gdb) c
Continuing.
Breakpoint 1, insert (btp=0x804967c, x=5) at bintree.c:37
37
if (x < tmp->val) {
1: *tmp = {val = 8, left = 0x0, right = 0x0}

Multithread backtrace

In a multi-threaded program, gdb by default shows the backtrace only for the current thread. To display the backtrace for several or all of the threads, use the command thread apply (see thread apply). For example, if you type thread apply all backtrace, gdb will display the backtrace for all the threads; this is handy when you debug a core dump of a multi-threaded program.

@Scott:
1. 例一中,用 "watch -l var1" 即可 (Stackoverflow 中路人的建議不見得是最好的)
2. 你 watchpoints 與 commands 都有在用的話,可考慮打開 gdb global history: $HOME/.gdbinit
set history save on
set history size 4096
set history filename /home/fcamel/.gdb_history
然後像 ipython 一樣,撈 history 寫成 gdb script.

gdb watch point (watch -l python script)
import gdb                                                                                                                                                                                                   

def _watch_location(expr, watch_point_class):
    # gdb.parse_and_eval will return gdb.Value
    # which is the internal gdb structure which can be access by python
    # http://sourceware.org/gdb/current/onlinedocs/gdb/Values-From-Inferior.html#Values-From-Inferior
    l = gdb.parse_and_eval(expr).address
    wp_str = '*(%(type)s)(%(address)s)' % dict(type=l.type, address=l)

    # http://sourceware.org/gdb/onlinedocs/gdb/Breakpoints-In-Python.html
    # call gdb to set breakpoint
    gdb.Breakpoint(wp_str, gdb.BP_WATCHPOINT, watch_point_class)

# to create your own gdb command
# http://sourceware.org/gdb/current/onlinedocs/gdb/Commands-In-Python.html#Commands-In-Python
class _WatchLocationCommand(gdb.Command):
    'Like "watch -l" in gdb 7.4+'
    # initialize the command's name, command_class, complete_class
    def __init__(self):        
        gdb.Command.__init__(self, 'watch-l', gdb.COMMAND_BREAKPOINTS, gdb.COMPLETE_SYMBOL)

    def invoke(self, arg, from_tty):
        _watch_location(arg, gdb.WP_WRITE)

class _RWatchLocationCommand(gdb.Command):
    'Like "rwatch -l" in gdb 7.4+'
    def __init__(self):        
        gdb.Command.__init__(self, 'rwatch-l', gdb.COMMAND_BREAKPOINTS, gdb.COMPLETE_SYMBOL)

    def invoke(self, arg, from_tty):
        _watch_location(arg, gdb.WP_READ)

class _AWatchLocationCommand(gdb.Command):
    'Like "awatch -l" in gdb 7.4+'
    def __init__(self):        
        gdb.Command.__init__(self, 'awatch-l', gdb.COMMAND_BREAKPOINTS, gdb.COMPLETE_SYMBOL)

    def invoke(self, arg, from_tty):
        _watch_location(arg, gdb.WP_ACCESS)

_WatchLocationCommand()
_RWatchLocationCommand()
_AWatchLocationCommand()
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License