在gdb还没有使用python pretty printer之前,Codeblocks IDE(我想其他IDE应该也一样),使用gdb的一些脚本用于显示std::string的内容,例如 output s.c_str()[0]@s.size() 这个命令实际上调用了 std::string的2个函数,第一个c_str()获取了字符串的首地址,后面这个是字符串的长度。 但是,这个命令似乎在gcc4.7.x下不好使了,测试代码和错误如下: #include <iostream> using namespace std; int main() { string s = "Test"; cout << s << endl; return 0; } 运行output s.c_str()[0]@s.size() 结果:Cannot evaluate function -- may be inlined 但是如果是gcc4.6.x编译此代码,就一切正常。 于是我去gdb的irc上问了一下,jan(貌似就是gdb的开发者)回答得很好,摘录如下: ollydbg I have a problem under windows: gcc 4.7.x gdb-cvs, when try to show a std::string, I enter the command: output s.c_str(), but gdb response: Cannot evaluate function -- may be inlined ollydbg what cause this kind of problem? thanks ollydbg For the record, if gcc 4.6.x does not have such issue. jankratochvil <irc://freenode/jankratochvil,isnick> Try to use this function in the program, be sure to use -O0 -g (and not -O2 -g) and try -fkeep-inline-functions. ollydbg OK, I will try this soon -->| qeed (~qeed@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx) has joined #gdb <irc://freenode/%23gdb> ollydbg Hi, jan, thanks, it works. ollydbg In-fact, I'm forum Code::blocks forum, we use such script to show the std::string value: output s.c_str()[0]@s.size() ollydbg But, if the user does not expicit call the c_str() and size() in there code, gdb will report failure in running such script. ollydbg That's strange, does gcc changed something? ollydbg BTW: If I enable the python pretty printer, then I have no such issue, the str::string's content shown correctly either under gcc 4.7.x or gcc 4.6.x jankratochvil <irc://freenode/jankratochvil,isnick> Newer gcc has more optimizations, it has the right to do these optimizations. And sure Pretty Printers do not depend on inferior function calls, so that Pretty Printers can work also on core files. ollydbg Ok, thanks, I will forward your explanation to Code::blocks forum, many thanks. asmwarrior/ollydbg ollydbg So, it looks like gdb pretty-printer is the most suggest way to show std::string like contents, other gdb script are not suggest because they will cause such issue. 具体原因是这些函数(c_str(), size())如果在gcc4.7下没有被调用,gdb就找不到他们了。 如果你使用gdb的python pretty printer,则不存在此问题。 相关在codeblocks论坛上的讨论见: http://forums.codeblocks.org/index.php/topic,16772.msg114299.html#msg114299 asmwarrior