使用 clang 时在 gdb 中评估 libc++ 的方法
evaluating methods of libc++ in gdb when using clang
编译时
#include <vector>
#include <stdio.h>
int main()
{
std::vector<int> foo;
foo.push_back( 1 );
printf( "%zu\n", foo.size() );
}
with clang++ foo.cpp -stdlib=libc++ -g
,当 gdb 中 运行 a.out 并试图显示 foo.size()
的结果时,gdb 说 "Cannot evaluate function -- may be inlined".
有没有办法避免编译器在调试模式下内联?我可以使用 libstdc++,但是当它需要进入模板时会非常痛苦(许多子调用和缩进有时是基于 space 的,有时是基于制表符的)。
我是 运行 Debian 9(stretch),使用 libc++-dev v3.5 和 clang 3.8(也尝试使用 clang 5.0,结果相同)和 gdb 7.12。
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.
这就是为什么您可以调用 mock foo.size()
,即使真正的 foo.size()
在使用 libstdc++ 时已被编译器内联。据我所知,libc++ 没有类似的 xmethod 实现。
编译时
#include <vector>
#include <stdio.h>
int main()
{
std::vector<int> foo;
foo.push_back( 1 );
printf( "%zu\n", foo.size() );
}
with clang++ foo.cpp -stdlib=libc++ -g
,当 gdb 中 运行 a.out 并试图显示 foo.size()
的结果时,gdb 说 "Cannot evaluate function -- may be inlined".
有没有办法避免编译器在调试模式下内联?我可以使用 libstdc++,但是当它需要进入模板时会非常痛苦(许多子调用和缩进有时是基于 space 的,有时是基于制表符的)。
我是 运行 Debian 9(stretch),使用 libc++-dev v3.5 和 clang 3.8(也尝试使用 clang 5.0,结果相同)和 gdb 7.12。
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.
这就是为什么您可以调用 mock foo.size()
,即使真正的 foo.size()
在使用 libstdc++ 时已被编译器内联。据我所知,libc++ 没有类似的 xmethod 实现。