Can I prevent debugger from stepping into Boost or

2020-02-08 07:29发布

问题:

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.

回答1:

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



回答2:

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



回答3:

Instead of doing s (step), you can
b on first line of your function where you want to stop (b Class::method, or b file.cpp:line),
then c.

gdb will bypass the boost code and break at the point given in b, where you want it

this works but can seem tedious. it's matter of habit. becomes easier with repetition.

msvc behaves similar to gdb



回答4:

From https://stackoverflow.com/a/31629136/5155476:

I had this same need. I extended the 'skip' command in gdb to support a new type 'dir'. I can now do this in gdb:

skip dir /usr

and then I'm never stopped in any of my 3rd party headers.

Here's a webpage w/ this info + the patch if it helps anyone: info & patch to skip directories in GDB