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

后端 未结 4 2150
生来不讨喜
生来不讨喜 2020-12-08 21:28

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 fil

4条回答
  •  一整个雨季
    2020-12-08 21:39

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

提交回复
热议问题