c++ - 使用 clang 时在 gdb 中评估 libc++ 的方法

标签 c++ debugging gdb clang inline

编译时

#include <vector>
#include <stdio.h>
int main()
{
    std::vector<int> foo;
    foo.push_back( 1 );
    printf( "%zu\n", foo.size() );
}

使用 clang++ foo.cpp -stdlib=libc++ -g,在 gdb 中运行 a.out 并尝试显示 foo.size() 的结果时,gdb说“无法计算函数——可能是内联的”。

有没有办法避免编译器在 Debug模式下内联?我可以使用 libstdc++,但是当它需要进入模板内部时会非常痛苦(许多子调用和缩进有时基于空格,有时基于制表符)。

我正在使用带有 clang 3.8 的 libc++-dev v3.5(也尝试使用 clang 5.0,结果相同)和 gdb 7.12 运行 Debian 9(stretch)。

最佳答案

libstdc++ 实现所谓的 Python xmethods,参见 documentation :

Xmethods are additional methods or replacements for existing methods of a C++ class. This feature is useful for those cases where a method defined in C++ source code could be inlined or optimized out by the compiler, making it unavailable to GDB. For such cases, one can define an xmethod to serve as a replacement for the method defined in the C++ source code. GDB will then invoke the xmethod, instead of the C++ method, to evaluate expressions. One can also use xmethods when debugging with core files. Moreover, when debugging live programs, invoking an xmethod need not involve running the inferior (which can potentially perturb its state). Hence, even if the C++ method is available, it is better to use its replacement xmethod if one is defined.

这就是为什么您可以调用模拟 foo.size(),即使真正的 foo.size() 在使用 libstdc++ 时已被编译器内联。据我所知,libc++ 没有类似的 xmethod 实现。

关于c++ - 使用 clang 时在 gdb 中评估 libc++ 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49128998/

相关文章:

c++ - 套接字服务器针对某些请求返回 tcp 错误 [RST, ACK]

android - Azure DevOps gradle 构建失败

c++简单地用自己的线程启动一个函数

c++ - 在高性能金融应用程序中缓存

c++ - 我们如何调试在 C++ 应用程序中使用的 matlab 制作的 DLL?

c - 尝试单步执行代码时 gdbserver 锁定

c - 在没有断点的情况下暂停 gdb

c++ - 调试:跟踪(和区分)同一程序的两个版本的函数调用树

c++ - 在 C 中截断 double 而不进行舍入

ios - 我如何修复 safari 6 网络检查器,使其与 iOS 设备或模拟器一起工作?