Memory dump formatted like xxd from gdb

后端 未结 5 950
轻奢々
轻奢々 2020-12-23 02:10

I\'m trying to inspect a buffer which contains a binary formatted message, but also contains string data. As an example, I\'m using this C code:

int main (vo         


        
5条回答
  •  北海茫月
    2020-12-23 03:09

    My own contribution, from Employed Russian solution and Roger Lipscombe comments:

    • use xxd,
    • preserve the address (xxd -o)
    • size argument is optional
    • small documentation included

    The script (tested with gdb 7.8.1):

    define xxd
      if $argc < 2
        set $size = sizeof(*$arg0)
      else
        set $size = $arg1
      end
      dump binary memory dump.bin $arg0 ((void *)$arg0)+$size
      eval "shell xxd -o %d dump.bin; rm dump.bin", ((void *)$arg0)
    end
    document xxd
      Dump memory with xxd command (keep the address as offset)
    
      xxd addr [size]
        addr -- expression resolvable as an address
        size -- size (in byte) of memory to dump
                sizeof(*addr) is used by default
    end
    

    Examples:

    (gdb) p &m_data
    $1 = (data_t *) 0x200130dc 
    
    (gdb) p sizeof(m_data)
    $2 = 32
    
    (gdb) xxd &m_data 32
    200130dc: 0300 0000 e87c 0400 0000 0000 0100 0000  .....|..........
    200130ec: 0c01 0000 b831 0020 0100 0000 0100 0000  .....1. ........
    
    (gdb) xxd &m_data
    200130dc: 0300 0000 e87c 0400 0000 0000 0100 0000  .....|..........
    200130ec: 0c01 0000 b831 0020 0100 0000 0100 0000  .....1. ........
    
    (gdb) help xxd
      Dump memory with xxd command (keep the address as offset)
    
      xxd addr [size]
        addr -- expression resolvable as an address
        size -- size (in byte) of memory to dump
                sizeof(*addr) is used by default
    

提交回复
热议问题