【gdb】打印ASCII和宽字符字符串

打印ASCII和宽字符字符串

1. 例子:

#include <stdio.h>
#include <wchar.h>

int main(void) 
{
  char str1[] = "abcd";
  wchar_t str2[] = L"abcd";

  return 0;
}

用gdb调试程序时,可以使用x/s命令打印ASCII字符串。以上面程序为例:

[root@node01 demo]# gdb ./a.out -q
Reading symbols from /root/demo/a.out...done.
(gdb) start
Temporary breakpoint 1 at 0x400511: file demo.cpp, line 6.
Starting program: /root/demo/./a.out 

Temporary breakpoint 1, main () at demo.cpp:6
6	  char str1[] = "abcd";
(gdb) n
7	  wchar_t str2[] = L"abcd";
(gdb) 
9	  return 0;
(gdb) x/s str1
0x7fffffffe480:	"abcd"

可以看到打印出了str1字符串的值。打印宽字符字符串时,要根据宽字符的长度决定如何打印。仍以上面程序为例:

[root@node01 demo]# gdb ./a.out -q
Reading symbols from /root/demo/a.out...done.
(gdb) start
Temporary breakpoint 1 at 0x400511: file demo.cpp, line 6.
Starting program: /root/demo/./a.out 

Temporary breakpoint 1, main () at demo.cpp:6
6	  char str1[] = "abcd";
(gdb) n
7	  wchar_t str2[] = L"abcd";
(gdb) 
9	  return 0;
(gdb) p sizeof(wchar_t)
$1 = 4
(gdb) x/ws str2
0x7fffffffe460:	U"abcd"

 

参考资料

1. gdb手册

2. 打印ASCII和宽字符字符串

posted @ 2023-10-15 18:28  苏格拉底的落泪  阅读(216)  评论(0编辑  收藏  举报