1 指令说明
examine指令可以查看指定内存地址中的值。
examine的用法为:
examine /nuf addr
其中:
指令 | 说明 |
---|---|
n | 表示要打印的单元个数; |
u | 表示单元的大小; |
f | 表示打印格式。 |
u的值如下:
指令 | 说明 |
---|---|
b | 字节 |
h | 双字节 |
w | 四字节 |
g | 八字节 |
f的值如下:
指令 | 说明 |
---|---|
x | 16进制 |
d | 有符号的10进制,默认格式 |
u | 无符号的10进制 |
o | 8进制 |
t | 2进制 |
a | 地址 |
c | 字符 |
f | 浮点 |
s | 字符串 |
addr 是指内存地址
examine 可以简写为 x
2 代码准备
使用如下代码进行测试
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
int j = 0;
int test2()
{
char* s8Buf = NULL;
strcpy(s8Buf, "8888");
return 0;
}
int main()
{
int i = 0;
for (i = 0; i < 60; i++)
{
j++;
printf("-------->index %d\n", i);
sleep(1);
}
test2();
return 0;
}
3 指令测试
3.1 显示指定内存的值 x /4bx addr
如下,首先我们获取了变量j的内存地址为0x601044,然后单元个数为4,单元的大小为字节,单元的格式为16进制,因此使用命令“x /4bx 0x601044”
(gdb) break 23
Breakpoint 1 at 0x40059f: file test_gdb.c, line 23.
(gdb) r
Starting program: /home/test_demo/gdb/test_gdb
Breakpoint 1, main () at test_gdb.c:23
23 j++;
(gdb) print /a &j
$1 = 0x601044 <j>
(gdb) x /4bx 0x601044
0x601044 <j>: 0x00 0x00 0x00 0x00
(gdb) c
Continuing.
-------->index 0
Breakpoint 1, main () at test_gdb.c:23
23 j++;
(gdb) x /4bx 0x601044
0x601044 <j>: 0x01 0x00 0x00 0x00
(gdb) c
Continuing.
-------->index 1
Breakpoint 1, main () at test_gdb.c:23
23 j++;
(gdb) x /4bx 0x601044
0x601044 <j>: 0x02 0x00 0x00 0x00
(gdb)
我们也可以通过上面的方式来查看是大段存储还是小端存储的。