GDB 内存断点
2014-08-26 08:54
281 查看
内存断点可以帮助我们查找一些内存的问题, 而内存问题往往都是比较棘手的问题。所以掌握GDB的内存调试还是很有用的,下来我们就开始GDB的内存断点之旅。
我们通过一个程序来学些这个方法:
注意涉及的指令就是rwatch、watch和awatch,分别表示读、写、读写。需要注意的是我用的这个版本直接用地址是断不下来的,必须用 *(char*) 之类来进行强制类型转换 ,不知道其它版本会不会也有同样的问题。
代码如下:
//main.cpp gdb memory break test code
#include
#include
int main()
{
char buf[1024];
char* pp = buf;
printf("add buf = 0x%x/r/n",buf);
for(int i=0;i
{
printf("addr = 0x%x ~~ 0x%x/r/n",pp+i*10,pp+i*10+9);
memset(pp+i*10,0,10);
}
printf("end");
}
过程如下,我用的是cygwin模拟环境:
xxxx@97190913124F402 /cygdrive/f
$ gcc -g main.cpp -o main.out //编译,注意-g
xxxx @97190913124F402 /cygdrive/f
$ gdb main.out –nw //-nw表示用文本模式
GNU gdb 5.0 (20010428-3)
Copyright 2001 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i686-pc-cygwin"...
(gdb) list
1 #include
2 #include
3
4 int main()
5 {
6 char buf[1024];
7
8 char* pp = buf;
9 printf("add buf = 0x%x/r/n",buf);
10
(gdb) break 9 //第9行打断点
Breakpoint 1 at 0x40108a: file main.cpp, line 9.
(gdb) run //运行
Starting program: /cygdrive/f/main.out
Breakpoint 1, main () at main.cpp:9
9 printf("add buf = 0x%x/r/n",buf);
Current language: auto; currently c++
(gdb) display &buf //查看buf的地址
1: &buf = (char (*)[1024]) 0x23f994
(gdb) watch *(int*)0x23fA00 //写断点buf地址后的一点,保证其几个循环后被改写
Watchpoint 2: *(int *) 2357760
(gdb) c //继续运行
Continuing.
add buf = 0x23f994
addr = 0x23f994 ~~ 0x23f99d
addr = 0x23f99e ~~ 0x23f9a7
……
addr = 0x23f9ee ~~ 0x23f9f7
addr = 0x23f9f8 ~~ 0x23fa01
Watchpoint 2: *(int *) 2357760 //断点触发
Old value = 2357808
New value = 2357760
0x6108bee3 in _libkernel32_a_iname ()
(gdb) info breakpoints //删掉这些断点
Num Type Disp Enb Address What
1 breakpoint keep y 0x0040108a in main at main.cpp:9
breakpoint already hit 1 time
2 watchpoint keep y *(int *) 2357760
breakpoint already hit 1 time
(gdb) delete 1
(gdb) delete 2
(gdb) info breakpoints
No breakpoints or watchpoints.
(gdb) c //继续运行
Continuing.
addr = 0x23fa02 ~~ 0x23fa0b
……
addr = 0x23fd72 ~~ 0x23fd7b
end
Program exited normally.
(gdb)
转载地址http://blog.csdn.net/wei801004/article/details/4253886
我们通过一个程序来学些这个方法:
注意涉及的指令就是rwatch、watch和awatch,分别表示读、写、读写。需要注意的是我用的这个版本直接用地址是断不下来的,必须用 *(char*) 之类来进行强制类型转换 ,不知道其它版本会不会也有同样的问题。
代码如下:
//main.cpp gdb memory break test code
#include
#include
int main()
{
char buf[1024];
char* pp = buf;
printf("add buf = 0x%x/r/n",buf);
for(int i=0;i
{
printf("addr = 0x%x ~~ 0x%x/r/n",pp+i*10,pp+i*10+9);
memset(pp+i*10,0,10);
}
printf("end");
}
过程如下,我用的是cygwin模拟环境:
xxxx@97190913124F402 /cygdrive/f
$ gcc -g main.cpp -o main.out //编译,注意-g
xxxx @97190913124F402 /cygdrive/f
$ gdb main.out –nw //-nw表示用文本模式
GNU gdb 5.0 (20010428-3)
Copyright 2001 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i686-pc-cygwin"...
(gdb) list
1 #include
2 #include
3
4 int main()
5 {
6 char buf[1024];
7
8 char* pp = buf;
9 printf("add buf = 0x%x/r/n",buf);
10
(gdb) break 9 //第9行打断点
Breakpoint 1 at 0x40108a: file main.cpp, line 9.
(gdb) run //运行
Starting program: /cygdrive/f/main.out
Breakpoint 1, main () at main.cpp:9
9 printf("add buf = 0x%x/r/n",buf);
Current language: auto; currently c++
(gdb) display &buf //查看buf的地址
1: &buf = (char (*)[1024]) 0x23f994
(gdb) watch *(int*)0x23fA00 //写断点buf地址后的一点,保证其几个循环后被改写
Watchpoint 2: *(int *) 2357760
(gdb) c //继续运行
Continuing.
add buf = 0x23f994
addr = 0x23f994 ~~ 0x23f99d
addr = 0x23f99e ~~ 0x23f9a7
……
addr = 0x23f9ee ~~ 0x23f9f7
addr = 0x23f9f8 ~~ 0x23fa01
Watchpoint 2: *(int *) 2357760 //断点触发
Old value = 2357808
New value = 2357760
0x6108bee3 in _libkernel32_a_iname ()
(gdb) info breakpoints //删掉这些断点
Num Type Disp Enb Address What
1 breakpoint keep y 0x0040108a in main at main.cpp:9
breakpoint already hit 1 time
2 watchpoint keep y *(int *) 2357760
breakpoint already hit 1 time
(gdb) delete 1
(gdb) delete 2
(gdb) info breakpoints
No breakpoints or watchpoints.
(gdb) c //继续运行
Continuing.
addr = 0x23fa02 ~~ 0x23fa0b
……
addr = 0x23fd72 ~~ 0x23fd7b
end
Program exited normally.
(gdb)
转载地址http://blog.csdn.net/wei801004/article/details/4253886
相关文章推荐
- GDB内存断点(Memory break)的使用举例
- gdb 内存断点watch 的使用
- GDB 内存断点
- gdb内存断点功能(转)
- GDB 内存断点
- GDB内存断点(Memory break)的使用举例
- 程序由多个原文件组成,gdb时如何在指定的某个文件中设置断点?
- gdb 断点设置方法
- gdb调试C语言--单步调试、断点调试
- OD笔记2 内存断点与硬件断点
- gdb设置条件断点
- Linux gdb设置和管理断点
- gdb设置条件断点
- gdb中断点的使用
- 内存检查强力组合: valgrind和gdb
- VS内存断点
- linux gdb 查看内存
- vs2008 无法设置断点&设置内存断点的方法
- 运行断点GDB 调试汇编程序的方法-java教程
- GDB笔记(二):条件断点、命令列表、监视点