How to modify memory contents using GDB?

bits picture bits · Jul 22, 2010 · Viewed 136.3k times · Source

I know that we can use several commands to access and read memory: for example, print, p, x...

But how can I change the contents of memory at any specific location (while debugging in GDB)?

Answer

Nikolai Fetissov picture Nikolai Fetissov · Jul 22, 2010

The easiest is setting a program variable (see GDB: assignment):

(gdb) l
6       {
7           int i;
8           struct file *f, *ftmp;
9
(gdb) set variable i = 10
(gdb) p i
$1 = 10

Or you can just update arbitrary (writable) location by address:

(gdb) set {int}0x83040 = 4

There's more. Read the manual.