查看内存地址的值
- 命令examine/x
Examine memory: x/FMT ADDRESS.
ADDRESS is an expression for the memory address to examine.
FMT is a repeat count followed by a format letter and a size letter.
Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),
t(binary), f(float), a(address), i(instruction), c(char), s(string)
and z(hex, zero padded on the left).
Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).
The specified number of objects of the specified size are printed
according to the format. If a negative number is specified, memory is
examined backward from the address.
Defaults for format and size letters are those previously used.
Default count is 1. Default address is following last thing printed
with this command or “print”.
格式x/nfu addr
- n:输出单元的个数
- f:是输出格式。比如x是以16进制形式输出,o是以8进制形式输出
- u:标明一个单元的长度。b是一个byte,h是两个byte(halfword),w是四个byte(word),g是八个byte(giant word)
以下面代码为例,查看
#include<iostream>
#include<string>
using namespace std;
int main() {
int t[] = {1,2,3,4,5,6,7,8,9,10,11,12,1024,14,15,16,17,18,19,20};
cout << t << endl;
}
(gdb) list
1 #include<iostream>
2 #include<string>
3 using namespace std;
4 int main() {
5 int t[] = {1,2,3,4,5,6,7,8,9,10,11,12,1024,14,15,16,17,18,19,20};
6 cout << t << endl;
7 }
8
(gdb) b main
Breakpoint 1 at 0x902: file test.cpp, line 4.
(gdb) r
Starting program: /home/liguo/code/gdb/a.out
Breakpoint 1, main () at test.cpp:4
4 int main() {
(gdb) n
5 int t[] = {1,2,3,4,5,6,7,8,9,10,11,12,1024,14,15,16,17,18,19,20};
(gdb) n
6 cout << t << endl;
(gdb) n
0x7ffffffee3b0
7 }
(gdb) x/20x 0x7ffffffee3b0
0x7ffffffee3b0: 0x00000001 0x00000002 0x00000003 0x00000004
0x7ffffffee3c0: 0x00000005 0x00000006 0x00000007 0x00000008
0x7ffffffee3d0: 0x00000009 0x0000000a 0x0000000b 0x0000000c
0x7ffffffee3e0: 0x00000400 0x0000000e 0x0000000f 0x00000010
0x7ffffffee3f0: 0x00000011 0x00000012 0x00000013 0x00000014
(gdb) x/20d 0x7ffffffee3b0
0x7ffffffee3b0: 1 2 3 4
0x7ffffffee3c0: 5 6 7 8
0x7ffffffee3d0: 9 10 11 12
0x7ffffffee3e0: 1024 14 15 16
0x7ffffffee3f0: 17 18 19 20