编译时
#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/