[摘]gdb查看QString类型变量

来源:互联网 发布:儿童发音矫正软件 编辑:程序博客网 时间:2024/05/18 23:54
reprinted from:http://hi.baidu.com/yy73/blog/item/3aa83cad810a860c4a36d623.html#
 
A Printing routine for QString in GDB

GDBsupports the command print toprint out the content of variables. This works pretty well with allbasic types, but becomes annoying at best with complex types likeQString. GDB has its own macro language, so it is possible to writeroutines for printing these types.

WhenGDB starts it reads in the file $HOME/.gdbinit toload macros that should be available at runtime. In the followingtwo sections I show a macro to print QString objects. If you copythis macro into your .gdbinit fileit will be automatically available each time you call gdb.

Examplesession:

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

 

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

Theform printqstringmyString prints out a more readableversion.

Qt 3.x

 

Thismacro 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

 

Italready prints out each character of the QString onto a singleline.

A muchrefined version was posted by Arnaud de Muyser to the qt-interestlist 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

Qt4.x

 

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

So thisis my adapted version of the macro:

define printqstring printf "(QString)0x%x (length=%i):"",&$arg0,$arg0.d->size set $i=0while $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
0 0
原创粉丝点击