Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print wstring in gdb

Tags:

wstring

gdb

How can I print wstring in gdb?

like image 792
sanxiyn Avatar asked Sep 19 '08 21:09

sanxiyn


2 Answers

call printf %ls only works sometimes, but to get it to work at all in gdb 6.3 you need the void cast and linefeed \n shown here:

call (void)printf("\"%ls\"\n",str.c_str())

here is a more reliable command you can put in your .gdbinit that also shows non-ASCII code points:

define wc_print
echo "
set $c = (wchar_t*)$arg0
while ( *$c )
  if ( *$c > 0x7f )
    printf "[%x]", *$c
  else
    printf "%c", *$c
  end
  set $c++
end
echo "\n
end

just enter wc (short for wc_print) with either a std::wstring or wchar_t*.

More detail at http://www.firstobject.com/wchar_t-gdb.htm

like image 82
Ben Bryant Avatar answered Sep 20 '22 10:09

Ben Bryant


Suppose you've got a std::wstring str. The following should work in gdb:

call printf("%ls", str._M_data())

(The -l option in printf makes it a long string, and I believe you need the "call" statement because the ordinary gdb printf doesn't like that option.)

like image 33
Jesse Beder Avatar answered Sep 17 '22 10:09

Jesse Beder