在 gdb中显示unicode等几则调试技巧 http://blog.csdn.net/absurd 作者联系方式: Li XianJing xianjimli at hotmail dot com 更新时间: 2007-3-21 这几天调试 mozilla 时,有两个问题一直困扰着我:一是无法从接口指针指向的实例得到具体实例的信息。二是
在 gdb中显示unicode等几则调试技巧
http://blog.csdn.net/absurd
作者联系方式: Li XianJing
更新时间: 2007-3-21
这几天调试 mozilla 时,有两个问题一直困扰着我:一是无法从接口指针指向的实例得到具体实例的信息。二是无法显示 unicode 。今天在 mozilla 网站上找到了这个问题的解决方法,这里做个笔记。
为了便于说明,我写了个小程序:
#include
class Intf
{
public :
Intf (){};
virtual ~ Intf (){};
virtual int Do () = 0;
};
class Impl : public Intf
{
public :
Impl ( const wchar_t * str );
~ Impl ();
int Do ();
private :
const wchar_t * m_str ;
};
Impl :: Impl ( const wchar_t * str )
{
m_str = str ;
}
Impl ::~ Impl ()
{
}
int Impl :: Do ( void )
{
return 0;
}
int test ( Intf * pIntf )
{
return pIntf -> Do ();
}
int main ( int argc , char * argv [])
{
Intf * pIntf = new Impl (L "abc" );
return test ( 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~~
查看更多关于在gdb中显示unicode等几则调试技巧的详细内容...