我可以阻止调试器单步执行 Boost 或 STL 头文件吗?

发布于 2024-11-11 02:22:02 字数 383 浏览 3 评论 0原文

我使用 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 文件。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你的呼吸 2024-11-18 02:22:03

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

将以下内容放入 .gdbinit 文件中。它搜索 gdb 已加载或可能加载的源(gdb 命令 infosource),并在其绝对路径以“/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()的调用,可以轻松扩展它以跳过其他目录。

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

心舞飞扬 2024-11-18 02:22:03

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

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

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

妄断弥空 2024-11-18 02:22:03

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

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

skip dir /usr

然后我就不会在任何第 3 方标头中停止。

这是一个包含此信息+补丁的网页(如果它对任何人有帮助的话): info &跳过 GDB 中的目录的补丁

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

久伴你 2024-11-18 02:22:03

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

gdb 将绕过 boost 代码并在 b 中给出的点处中断,您希望它

可以工作,但看起来很乏味。这是习惯问题。通过重复变得更容易。

msvc 的行为与 gdb 类似

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文