Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
889 views
in Technique[技术] by (71.8m points)

debugging - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...