[摘]gdb查看QString类型变量

 
A Printing routine for QString in GDB

GDB supports the command print to print out the content of variables. This works pretty well with all basic types, but becomes annoying at best with complex types like QString. GDB has its own macro language, so it is possible to write routines for printing these types.

When GDB starts it reads in the file $HOME/.gdbinit to load macros that should be available at runtime. In the following two sections I show a macro to print QString objects. If you copy this macro into your .gdbinit file it will be automatically available each time you call gdb.

Example session:

(gdb) print myString $2 = {static null = {<No data fields>}, static shared_null = {ref = {atomic = 39}, alloc = 0, size = 0, data = 0x82174ca, clean = 0, simpletext = 0, righttoleft = 0, asciiCache = 0, reserved = 0, array = {0}}, static shared_empty = {ref = {atomic = 1}, alloc = 0, size = 0, data = 0xf5f71aca, clean = 0, simpletext = 0, righttoleft = 0, asciiCache = 0, reserved = 0, array = {0}}, d = 0x8260b48, static codecForCStrings = 0x0}(gdb) printqstring myString (QString)0x8217658 (length=26): "this is an example QString"(gdb)

 

As you see above, print myString prints the QString as an object. Since the actual data is hidden in the element "d" it is not immediately visible - even print myString.d would not be very satisfactory, since QString stores its data in unicode format.

The form printqstring myString prints out a more readable version.

Qt 3.x

 

This macro was posted by David Faure to the KDE maillist in 2001:

define printqstring set $i=0 while $i < $arg0.d->len print $arg0.d->unicode[$i++].cl endend

 

It already prints out each character of the QString onto a single line.

A much refined version was posted by Arnaud de Muyser to the qt-interest list the same year:

 

 

 

define ps
    set $i=0
    set $unicode=$arg0.d->unicode
    while $i < $arg0.d->len
        set $c=$unicode[$i++].cl
        if $c < 32
          printf "\\0%o", $c
        else
          if $c <= 127
            printf "%c", $c
          else
            printf "\\0%o", $c
          end
        end
    end
    echo \n

end

Qt 4.x

 

The internal representation of QString changed for Qt 4.x: the length is now stored in d->size and it uses UCS-16 instead of UTF-8 for internal storage. The fact that QStrings are now implicitly shared does not matter in this context though.

So this is my adapted version of the macro:

define printqstring printf "(QString)0x%x (length=%i): "",&$arg0,$arg0.d->size set $i=0 while $i < $arg0.d->size set $c=$arg0.d->data[$i++] if $c < 32 || $c > 127 printf "\\u0xx", $c else printf "%c", (char)$c end end printf ""\n" end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值