打印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"