Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I prevent debugger from stepping into Boost or STL header files?

I'm using Qt Creator with gdb to debug my C++ code on a Linux Platform. Whenever I use a boost::shared_ptr or the like, the debugger steps into the header files containing the boost implementation (i.e. /usr/include/boost/shared_ptr.hpp). I would like to ignore these files in terms of debugging and simply step over them. I know that I can step out as soon as it reaches one of these files, but it would be much easier to debug without doing so several times per debugging session.

I'm using the gcc compiler (g++), running on OpenSuSE Linux 11.2 with QtCreator 2.2 (which uses gdb as the debugger.)

Edit to add: The question is geared toward Boost files, but could also apply toward STL files as well.

like image 300
Chance Avatar asked Jun 02 '11 19:06

Chance


2 Answers

GDB without stepping into STL and all other libraries in /usr:

Put the following in your .gdbinit file. It searches through the sources that gdb has loaded or will potentially load (gdb command info sources), and skips them when their absolute path starts with "/usr". It's hooked to the run command, because symbols might get reloaded when executing it.

# skip all STL source files
define skipstl
python
# get all sources loadable by gdb
def GetSources():
    sources = []
    for line in gdb.execute('info sources',to_string=True).splitlines():
        if line.startswith("/"):
            sources += [source.strip() for source in line.split(",")]
    return sources

# skip files of which the (absolute) path begins with 'dir'
def SkipDir(dir):
    sources = GetSources()
    for source in sources:
        if source.startswith(dir):
            gdb.execute('skip file %s' % source, to_string=True)

# apply only for c++
if 'c++' in gdb.execute('show language', to_string=True):
    SkipDir("/usr")
end
end

define hookpost-run
    skipstl
end

To check the list of files to be skipped, set a breakpoint somewhere (e.g., break main) and run gdb (e.g., run), then check with info sources upon reaching the breakpoint:

(gdb) info skip
Num     Type           Enb What
1       file           y   /usr/include/c++/5/bits/unordered_map.h
2       file           y   /usr/include/c++/5/bits/stl_set.h
3       file           y   /usr/include/c++/5/bits/stl_map.h
4       file           y   /usr/include/c++/5/bits/stl_vector.h
...

Its easy to extend this to skip other directories as well by adding a call to SkipDir(<some/absolute/path>).

like image 98
gospes Avatar answered Oct 19 '22 21:10

gospes


gdb is scriptable. it has while, if, variables, shell subcommands, user-defined functions (define) etc etc. it has python interface for scriptability.

With a bit of work, you can to make gdb script along these lines:

define step-bypass-boost
  step
  while 1
    use "info source", put current source file into variable
    if source file does not match */boost/* then
        break-loop
    end
    step
  end
end

or find whether somebody already made such script

like image 29
Andrei Avatar answered Oct 19 '22 23:10

Andrei