我可以阻止调试器进入Boost或STL头文件吗?

时间:2011-06-02 19:13:27

标签: debugging gcc boost gdb qt-creator

我正在使用Qt Creator和gdb在Linux平台上调试我的C ++代码。每当我使用boost::shared_ptr之类的时候,调试器就会进入包含boost实现的头文件(即/ usr / include /boost/shared_ptr.hpp)。我想在调试方面忽略这些文件,然后简单地跳过它们。我知道我可以在它到达其中一个文件时立即退出,但是如果不在每个调试会话中多次这样做,那么调试会更容易。

我正在使用gcc编译器(g++),在带有QtCreator 2.2的OpenSuSE Linux 11.2上运行(使用gdb作为调试器。)

编辑添加:问题适用于Boost文件,但也可以应用于STL文件。

4 个答案:

答案 0 :(得分:5)

GDB没有进入STL和/ usr中的所有其他库

将以下内容放入.gdbinit文件中。它搜索gdb已加载或可能加载的源(gdb命令info sources),并在其绝对路径以“/ usr”开头时跳过它们。它挂钩到run命令,因为符号可能在执行时重新加载。

# 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

要检查要跳过的文件列表,请在某处设置断点(例如break main)并运行gdb(例如run),然后在到达时检查info sources断点:

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

通过添加对SkipDir(<some/absolute/path>)的调用,很容易将其扩展为跳过其他目录。

答案 1 :(得分:3)

gdb是可编写脚本的。它有while,if,变量,shell子命令,用户定义的函数(定义)等等,它有可编程性的python接口。

通过一些工作,你可以沿着这些方向制作gdb脚本:

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

或查找某人是否已制作此类剧本

答案 2 :(得分:0)

而不是s(步骤),你可以 b在你想要停止的函数的第一行(b Class :: method,或b file.cpp:line),
那么c。

gdb将绕过增强代码并在b中给出的点处中断,你需要它

这可行,但看起来很乏味。这是习惯问题。重复变得容易。

msvc的行为类似于gdb

答案 3 :(得分:0)

来自https://stackoverflow.com/a/31629136/5155476

我也有同样的需求。我在gdb中扩展了“ skip”命令,以支持新的“ dir”类型。我现在可以在gdb中执行此操作:

skip dir /usr

然后我再也不会在任何第三方标头中停留了。

这是一个包含此信息和补丁的网页,如果它可以帮助任何人:info & patch to skip directories in GDB