现在的位置: 首页 > 综合 > 正文

在gdb中显示unicode等几则调试技巧

2012年11月06日 ⁄ 综合 ⁄ 共 1450字 ⁄ 字号 评论关闭
 在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,中文仍然显示不了,不过总比没有强多了。

抱歉!评论已关闭.