GDB 内存操作
gdb查看内存数据
格式:
1 | x/nfu |
说明:
x
是 examine 的缩写,意思是检查n
表示要显示的内存单元的个数f
表示显示方式u
表示一个地址单元的长度:
显示方式 f 取值
1 | x 按十六进制格式显示变量。 |
地址单元长度 u 取值
1 | b表示单字节, |
example
1 | x/50xw 0x40451400 |
50
是数量,x
是16进制,w
是四字节
Examining Memory
You can use the command x
(for “examine”) to examine memory in any of several formats, independently of your program’s data types.
1 | x/nfu addr |
Use the x command to examine memory.
n
, f
, and u
are all optional parameters that specify how much memory to display and how to format it; addr is an expression giving the address where you want to start displaying memory. If you use defaults for nfu, you need not type the slash ‘/’. Several commands set convenient defaults for addr.
n, the repeat count
The repeat count is a decimal integer; the default is 1.
It specifies how much memory (counting by units u) to display.
If a negative number is specified, memory is examined backward from addr.
f, the display format
The display format is one of the formats used by print
(‘x’, ‘d’, ‘u’, ‘o’, ‘t’, ‘a’, ‘c’, ‘f’, ‘s’),
‘i’ (for machine instructions) and ‘m’ (for displaying memory tags).
The default is ‘x’ (hexadecimal) initially.
The default changes each time you use either x or print.
u, the unit size
The unit size is any of
1 | b |
- Each time you specify a unit size with x, that size becomes the default unit the next time you use x.
- For the ‘i’ format, the unit size is ignored and is normally not written.
- For the ‘s’ format, the unit size defaults to ‘b’, unless it is explicitly given.
- Use x /hs to display 16-bit char strings and x /ws to display 32-bit strings. The next use of x /s will again display 8-bit strings.
- Note that the results depend on the programming language of the current compilation unit.
- If the language is C, the ‘s’ modifier will use the UTF-16 encoding while ‘w’ will use UTF-32. The encoding is set by the programming language and cannot be altered.
modify memory
In order to set the variable g, use
1 | (gdb) set var g=4 |
GDB allows more implicit conversions in assignments than C; you can freely store an integer value into a pointer variable or vice versa,
and you can convert any structure to any other structure that is the same length or shorter.
To store values into arbitrary places in memory, use the ‘{…}’ construct to generate a value of specified type at a specified address (see Expressions).
For example, {int}0x83040 refers to memory location 0x83040 as an integer (which implies a certain size and representation in memory), and
1 | set {int}0x83040 = 4 |
stores the value 4 into that memory location.
This should work for any valid pointer, and can be cast to any appropriate data type.
1 | set *((int *) 0xbfbb0000) = 20 |
e.g.
1 |
|
or use
1 | set *0xbfbb0000=20 |
change memory locations directly.