GDB修改内存的内容

GDB修改内存的内容

  • 向变量中写入内容
    gdb中的set命令可以用来向内存地址写入内容。但使用set命令有一些语法需要注意。
    If the beginning of the argument string of the set command appears identical to a set subcommand, use the set variable command instead of just set. This command is identical to set except for its lack of subcommands. For example, if your program has a variable width, you get an error if you try to set a new value with just ‘set width=13’, because GDB has the command set width:
    因为GDB的set命令参数有可能与你定义的变量重名,写入的时候会提示表达式无效。
(gdb) whatis width
type = double
(gdb) p width
$4 = 13
(gdb) set width=47
Invalid syntax in expression.

这里无效的表达式就是“=47”,想要向变量写入内容,正确的写法是在变量名前加上variable,可以简写为var

(gdb) set var width=47

因为该set命令有许多子命令可能与程序变量的名称冲突,所以最好使用该 set variable命令而不是set。例如,如果你的程序定义有变量g,仅使用’set g = 4’在运行的时候就会出错,因为GDB具有命令set gnutarget,缩写为set g:

(gdb) whatis g
type = double
(gdb) p g
$1 = 1
(gdb) set g=4
(gdb) p g
$2 = 1
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/smith/cc_progs/a.out
"/home/smith/cc_progs/a.out": can't open to read symbols:
                                 Invalid bfd target.
(gdb) show g
The current BFD target is "=4".

程序变量g未更改,并且为无效值。为了将内容写入变量 g,使用

(gdb) set var g=4
  • 向任意内存地址中写入内容
    GDB比C允许更多的隐式转换。你可以将整数值自由地存储到指针变量中,反之亦然,并且可以将任何结构转换为长度相同或更短的任何其他结构。
    要将值存储到内存中的任意位置,可以使用’{…}'结构,以在指定的地址生成指定类型的值。

例如,{int}0x83040将内存位置0x83040表示为整数(表示内存中的特定大小和表示形式),然后

set {int}0x83040 = 4

将值4存储到该存储位置。

发布了42 篇原创文章 · 获赞 18 · 访问量 7566

猜你喜欢

转载自blog.csdn.net/weixin_44395686/article/details/104728628
GDB