本文尝试使用gdb的dump命令和restore命令。
dump命令用来将内存中的数据保存到文件中。
restore命令用来将文件中的数据加载到内容中。
1.示例代码
#include <iostream>
using namespace std;
int main() {
string a = "Hello";
float b = 3.9999;
float c = 2.4;
cout << a << endl;
cout << b << endl;
cout << c << endl;
return 0;
}
2.gdb命令使用演示
Breakpoint 1, main () at main1.cc:8
8 cout << a << endl;
(gdb) p b
$1 = 3.9999001
(gdb) p c
$2 = 2.4000001
(gdb) dump binary memory bb (char*)&b (char*)&b+sizeof(b)-1
(gdb) restore bb binary (char*)&c 0 sizeof(c)-1
Restoring binary file bb into memory (0x7fffffffdd1c to 0x7fffffffdd1f)
(gdb) p b
$3 = 3.9999001
(gdb) p c
$4 = 3.9999001