本博客(http://blog.csdn.net/livelylittlefish)贴出作者(三二一@小鱼)相关研究、学习内容所做的笔记,欢迎广大朋友指正!
GDB内存断点(Memory break)的使用举例
本文是一篇使用GDB设置内存断点的例子。
1. 源程序
文件名:testMemBreak.c
#include <stdio.h>
#include <string.h>
int main()
{
int i,j;
char buf[256]={0};
char* pp = buf;
printf("buf addr= 0x%x/r/n",buf);
for(i=0;i<16;i++)
{
printf("addr = 0x%x ~ 0x%x/r/n",pp+i*16,pp+i*16+15);
for(j=0;j<16;j++)
*(pp+i*16+j)=i*16+j;
}
printf("ASCII table:/n");
for(i=0;i<16;i++)
{
for(j=0;j<16;j++)
printf("%c ", *(pp+i*16+j));
printf("/n");
}
return 0;
}
即完成ASCII码的初始化并打印出来。
要使用的GDB命令,如下。
(gdb) help watch
Set a watchpoint for an expression.
A watchpoint stops execution of your program whenever the value of
an expression changes.
(gdb) help rwatch
Set a read watchpoint for an expression.
A watchpoint stops execution of your program whenever the value of
an expression is read.
(gdb) help awatch
Set a watchpoint for an expression.
A watchpoint stops execution of your program whenever the value of
an expression is either read or written.
(gdb)
2. 调试过程
2.1 设置断点并启动程序完成初始化
启动程序对其进行初始化,并使用display自动显示buf的地址。
$ gcc -g -o membreak testMemBreak.c
$ gdb ./membreak.exe
GNU gdb 6.3.50_2004-12-28-cvs (cygwin-special)
Copyright 2004 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.