如何在gdb中打印wstring

时间:2020-03-06 14:27:51  来源:igfitidea点击:

如何在gdb中打印wstring?

解决方案

假设我们有一个std :: wstring str。以下应在gdb中工作:

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

(printf中的-l选项使它成为一个长字符串,并且我相信我们需要"`call"语句,因为普通的gdb printf不喜欢该选项。)

我做了一些研究,这是gdb PR716,PR1998,PR2264. 显然,这是一个尚未实现的经常请求的功能。

call printf%ls有时只能工作,但是要使其在gdb 6.3中完全起作用,我们需要显示如下的void强制转换和换行\ n

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

这是可以放在.gdbinit中的更可靠的命令,该命令还显示非ASCII代码点:

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

只需使用std :: wstring或者wchar_t *输入wc(wc_print`的缩写)。

有关更多详细信息,请访问http://www.firstobject.com/wchar_t-gdb.htm