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

AI助手已提取文章相关产品:

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

转载时请注明出处和作者联系方式:http://blog.youkuaiyun.com/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,中文仍然显示不了,不过总比没有强多了。

~~end~~

您可能感兴趣的与本文相关内容

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值