How to modify memory contents using GDB?

后端 未结 3 775
终归单人心
终归单人心 2020-12-04 08:12

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 (

3条回答
  •  無奈伤痛
    2020-12-04 08:27

    As Nikolai has said you can use the gdb 'set' command to change the value of a variable.

    You can also use the 'set' command to change memory locations. eg. Expanding on Nikolai's example:

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

    This should work for any valid pointer, and can be cast to any appropriate data type.

提交回复
热议问题