在gdb中顯示unicode等幾則調試技巧
轉自:http://blog.csdn.net/absurd/archive/2007/03/21/1536699.aspx
轉載時請註明出處和作者連絡方式:http://blog.csdn.net/absurd
作者連絡方式:Li XianJing <xianjimli at hotmail dot com>
更新時間:2007-3-21
這幾天調試mozilla時,有兩個問題一直困擾著我:一是無法從介面指標指向的執行個體得到具體執行個體的資訊。二是無法顯示unicode。今天在mozilla網站上找到了這個問題的解決方案,這裡做個筆記。
為了便於說明,我寫了個小程式:
#include <stdlib.h>
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+8>
ZTV4Impl 是pIntf的虛表指標,它暗示其實作類別為Impl。按下列方式我們就可以顯示其具體執行個體的資訊:
(gdb) p *(Impl*)pIntf
$2 = {<Intf> = {_vptr.Intf = 0x8048810}, m_str = 0x8048834}
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,中文仍然顯示不了,不過總比沒有強多了。