【问题标题】:Can I prevent debugger from stepping into Boost or STL header files?我可以阻止调试器进入 Boost 或 STL 头文件吗?
【发布时间】:2011-09-07 08:00:45
【问题描述】:

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

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

编辑添加:该问题针对 Boost 文件,但也适用于 STL 文件。

【问题讨论】:

  • @STATUS,谢谢。这个问题听起来像是不可能的,至少是自动的,除非目标代码和动态库代码之间存在差异。

标签: debugging gcc boost gdb qt-creator


【解决方案1】:

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>) 的调用,很容易扩展它以跳过其他目录。

【讨论】:

  • 这个解决方案适用于 gcc,但不幸的是不适用于 icc16。无论如何,谢谢!
【解决方案2】:

gdb 是可编写脚本的。它有 while、if、变量、shell 子命令、用户定义的函数(define)等。它有用于编写脚本的 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

或者查找是否有人已经制作了这样的脚本

【讨论】:

    【解决方案3】:

    您可以代替执行 s(步骤)
    b 在您要停止的函数的第一行(b Class::method 或 b file.cpp:line),
    然后c。

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

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

    msvc 的行为类似于 gdb

    【讨论】:

      【解决方案4】:

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

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

      skip dir /usr
      

      然后我从未停止过任何第 3 方标头。

      这是一个包含此信息的网页 + 补丁(如果它对任何人有帮助):info & patch to skip directories in GDB

      【讨论】:

        猜你喜欢
        • 2010-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-11
        • 1970-01-01
        • 2010-12-10
        • 2011-10-28
        • 1970-01-01
        相关资源
        最近更新 更多