在 gdb 中将某段内存 dump 保存到文件中

http://it.taocms.org/08/4272.htm

我们会有这种需求,在 gdb 中将某段内存 dump 保存到文件中,比如一段无法在 gdb 中直接访问的二进制数据(一段私有格式的网络包什么的)。其实在 gdb 里这个命令就叫dump,这里仅给出一种简单的用法,其他的可以在gdbhelp

dump binary memory file start_addr end_addr 
前面就写dump binary memory,后面接文件名,接着是起始地址,然后是尾地址。

++++++++++++++++++++++++++++++++++++++++++++++++++

(gdb) help dump binary memory
Write contents of memory to a raw binary file.
Arguments are FILE START STOP.  Writes the contents of memory
within the range [START .. STOP) to the specified FILE in binary format.

(gdb) dump binary memory my_binary_file.bin 0x22fd8a 0x22fd8a+450

++++++++++++++++++++++++++++++++++++++++++++++++++


http://blog.csdn.net/zhangmiaoping23/article/details/40892261

dump [格式] memory 文件名 起始地址 结构地址 #   把指定内存段写到文件

 dump [格式] value 文件名 表达式                        #   把指定值写到文件
        格式包括:
            binary      原始二进制格式
            ihex        intel 16进制格式
            srec        S-recored格式
            tekhex      tektronix 16进制格式


 append [binary] memory 文件名 起始地址 结构地址 #   按2进制追加到文件
 append [binary] value 文件名 表达式             #   按2进制追加到文件
    

restore 文件名 [binary] bias 起始地址 结构地址 #   恢复文件中内容到内存.如果文件内容是原始二进制,需要指定binary参数,不然会gdb自动识别文件格式


目的:

在gdb调试过程中(甚至是在调试coredump时),将程序内存中的内容dump到指定文件中。


gdb命令:

(gdb) dump binary memory ./file START STOP

将 [START, STOP) 地址范围内的内存内容输出到文件 file 中


举例:

1)将 [$pc, $pc+450) 范围内的内存输出到./file 中

  1. (gdb) p $pc  
  2. $1 = (void (*)()) 0x4004a7 <main+11>  
  3. (gdb) p $pc + 450  
  4. $2 = (void (*)()) 0x400669  
  5. (gdb) dump binary memory ./file $1 $2 

  1. (gdb) p $pc  
  2. $1 = (void (*)()) 0x4004a7 <main+11>  
  3. (gdb) p $pc + 450  
  4. $2 = (void (*)()) 0x400669  
  5. (gdb) dump binary memory ./file $1 $2 

2)将字符串s1的前5个字节输出到./a中

  1. int main ()  
  2. {  
  3.         char s1[] = "abcdefghijklmnopqrstuvwxyz";  
  4.         char s2[] = "0123456789";  
  5.   
  6.   return 0;  

[root@ampcommons02 yasi]# gdb ./dump -q
Reading symbols from /home/yasi/s...done.
(gdb) b 6
Breakpoint 1 at 0x4005a4: file s.cpp, line 6.
(gdb) r
Starting program: /home/yasi/s

Breakpoint 1, main () at s.cpp:6
6         return 0;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.80.el6_3.6.x86_64 libgcc-4.4.6-4.el6.x86_64 libstdc++-4.4.6-4.el6.x86_64
(gdb) dump binary memory ./dump s1 s1+5


[root@ampcommons02 yasi]# cat ./dump
abcde









<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(67) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
原文地址:https://www.cnblogs.com/ztguang/p/12648994.html