gdb显示std::string的内容,看起来python pretty printer很重要

124 views
Skip to first unread message

asmwarrior

unread,
Sep 1, 2012, 10:53:04 AM9/1/12
to Code::Blocks中国开发组, hell...@freelists.org
在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的开发者)回答得很好,摘录如下:



ollydbgI 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

ollydbgwhat cause this kind of problem? thanks

ollydbgFor the record, if gcc 4.6.x does not have such issue.

jankratochvilTry to use this function in the program, be sure to use -O0 -g (and not -O2 -g) and try -fkeep-inline-functions.

ollydbgOK, I will try this soon

-->|qeed (~qeed@adsl-98-85-40-120.mco.bellsouth.net) has joined #gdb

ollydbgHi, jan, thanks, it works.

ollydbgIn-fact, I'm forum Code::blocks forum, we use such script to show the std::string value: output s.c_str()[0]@s.size()

ollydbgBut, if the user does not expicit call the c_str() and size() in there code, gdb will report failure in running such script.

ollydbgThat's strange, does gcc changed something?

ollydbgBTW: 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

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

ollydbgOk, thanks, I will forward your explanation to Code::blocks forum, many thanks. asmwarrior/ollydbg

ollydbgSo, 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



xunxun

unread,
Sep 1, 2012, 9:58:55 PM9/1/12
to chi...@googlegroups.com, asmwarrior
另外问一下,gdb irc的地址是?
-- 
Best Regards,
xunxun

asmwarrior

unread,
Sep 2, 2012, 5:04:12 AM9/2/12
to xunxun, chi...@googlegroups.com
On 2012-9-2 9:58, xunxun wrote:
另外问一下,gdb irc的地址是?
irc://irc.freenode.net/gdb

Reply all
Reply to author
Forward
0 new messages