windows下gdb调试

原创
2012/09/15 22:20
阅读数 1.1W

c文件源码

 

#include <stdio.h>
int func(int n){
	int sum=0,i;
	for (i=0;i<n ;i++ )
	{
		sum+=i;
	}
	return sum;
}
main(){
	int i;
	long result=0;
	for (i=1;i<100 ;i++ )
	{
		result+=i;
	}
	printf("result[1-100]=%d\n",result);
	printf("result[1-50]=%d\n",func(50));
}

windows下gdb调试

 

G:\C\gccDemo>gdb test<------------------------------调试test.exe
GNU gdb 6.8
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-pc-mingw32"...
(gdb) L<----------------------------列出代码植入断点
L
1	#include <stdio.h>
2	int func(int n){
3		int sum=0,i;
4		for (i=0;i<n ;i++ )
5		{
6			sum+=i;
7		}
8		return sum;
9	}
10	main(){
(gdb) <--------------------------回车继续L

11		int i;
12		long result=0;
13		for (i=1;i<100 ;i++ )
14		{
15			result+=i;
16		}
17		printf("result[1-100]=%d\n",result);
18		printf("result[1-50]=%d\n",func(50));
19	}(gdb) 

Line number 20 out of range; test.c has 19 lines.
(gdb) Break 6<----------------定位第六行
B 6
 6
Breakpoint 1 at 0x40132e: file test.c, line 6.
(gdb) info break<-------------------植入断点
inf B
 B
Num     Type           Disp Enb Address    What
1       breakpoint     keep y   0x0040132e in func at test.c:6
(gdb) r
r<----------------------------------------run运行

Starting program: G:\C\gccDemo/test.exe 
[New thread 3768.0x15ac]
[New thread 3768.0x17cc]
result[1-100]=4950

Breakpoint 1, func (n=50) at test.c:6
6			sum+=i;
(gdb) n<---------------next
n
4		for (i=0;i<n ;i++ )
(gdb) n
n


Breakpoint 1, func (n=50) at test.c:6
6			sum+=i;
(gdb) n
n
4		for (i=0;i<n ;i++ )
(gdb) n
n

Breakpoint 1, func (n=50) at test.c:6
6			sum+=i;
(gdb) n
n
4		for (i=0;i<n ;i++ )
(gdb) n
n

Breakpoint 1, func (n=50) at test.c:6
6			sum+=i;
(gdb) n
n
4		for (i=0;i<n ;i++ )
(gdb) p i<---------------------------------print i,打印i的值
p i
$1 = 3
(gdb) up<--------------------------------上一堆栈
up
#1  0x00401397 in main () at test.c:18
18		printf("result[1-50]=%d\n",func(50));
(gdb) bt
bt
#0  func (n=50) at test.c:4
#1  0x00401397 in main () at test.c:18
(gdb) p &i<-----------------打印i的地址
p &i
i
$2 = (int *) 0x22ff48
(gdb) q
q
The program is running.  Exit anyway? (y or n) n
Not confirmed.
(gdb) r
r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: G:\C\gccDemo/test.exe 
[New thread 2884.0xebc]
[New thread 2884.0xc84]
result[1-100]=4950

Breakpoint 1, func (n=50) at test.c:6
6			sum+=i;
(gdb) 

(gdb) 

(gdb) r
r
The program being debugged has been started already.
Start it from the beginning? (y or n) n
Program not restarted.
(gdb) q<-----------------退出调试
q
The program is running.  Exit anyway? (y or n) y

G:\C\gccDemo>

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部