在gdb中顯示unicode等幾則調試技巧

來源:互聯網
上載者:User
 在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,中文仍然顯示不了,不過總比沒有強多了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.