在gdb中显示unicode等几则调试技巧
http://blog.csdn.net/absurd
作者联系方式:li xianjing
更新时间:2007-3-21
这几天调试mozilla时,有两个问题一直困扰着我:一是无法从接口指针指向的实例得到具体实例的信息。二是无法显示unicode。今天在mozilla网站上找到了这个问题的解决方法,这里做个笔记。
为了便于说明,我写了个小程序:
#include
classintf
{
public:
intf(){};
virtual ~intf(){};
virtualintdo() = 0;
};
classimpl: publicintf
{
public:
impl(constwchar_t* str);
~impl();
intdo();
private:
constwchar_t* m_str;
};
impl::impl(constwchar_t* str)
{
m_str = str;
}
impl::~impl()
{
}
intimpl::do(void)
{
return 0;
}
inttest(intf* pintf)
{
returnpintf->do();
}
intmain(intargc, char* argv[])
{
intf* pintf = newimpl(labc);
returntest(pintf);
}
存为main.c,然后编译生成test.exe
gcc -g main.cpp -lstdc++ -o test.exe
在gdb下运行,并在test函数中设置断点:
(gdb) b test
breakpoint 1 at 0x8048644: file main.cpp, line 40.
(gdb) r
starting program: /work/test/gdb/test.exe
reading symbols from shared object read from target memory...done.
loaded system supplied dso at 0xb83000
breakpoint 1, test (pintf=0x8a3e008) at main.cpp:40
40 return pintf->do();
(gdb) p *pintf
$1 = {_vptr.intf = 0x8048810}
1. 查看pintf的实现。
(gdb) x /wa pintf
0x8a3e008: 0x8048810
ztv4impl 是pintf的虚表指针,它暗示其实现类为impl。按下列方式我们就可以显示其具体实例的信息:
(gdb) p *(impl*)pintf
$2 = { = {_vptr.intf = 0x8048810}, m_str = 0x8048834}
2. 查看unicode字符串。
(gdb) x /6ch ((impl*)pintf)->m_str
0x8048834 : 97 'a' 0 '/0' 98 'b' 0 '/0' 99 'c' 0 '/0'
其中6表示要显示的长度。
这种方式只能显示英文的unicode,中文仍然显示不了,不过总比没有强多了。
~~end~~
