在 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    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(L"abc");    returntest(pIntf); } | 
 
存为main.c,然后编译生成test.exe
gcc -g main.cpp -lstdc++ -o test.exe
 
在gdb下运行,并在test函数中设置断点:
(gdb) b test
Breakpoint 1 at 0x80484: 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+8>
 
ZTV4Impl 是pIntf的虚表指针,它暗示其实现类为Impl。按下列方式我们就可以显示其具体实例的信息:
(gdb) p *(Impl*)pIntf
$2 = {
 
2. 查看unicode字符串。
(gdb) x /6ch ((Impl*)pIntf)->m_str
0x8048834 <_ZTV4Intf+20>: 97 'a' 0 '/0' 98 'b' 0 '/0' 99 'c' 0 '/0'
 
其中6表示要显示的长度。
这种方式只能显示英文的unicode,中文仍然显示不了,不过总比没有强多了。
 
~~end~~