Linux下gdb用法简单介绍 -电脑资料
makefile的一些用法:再了解makefile之前,希望大家好好了解一下vi的用法,有兴趣也可了解一下Emacs的高级用法,掌握一下GCC的用法,
Linux下gdb用法简单介绍
。还有由于linux的shell命令很多也不想一一讲解。在此之前我先深入浅出的介绍一下GDB的用法。uuHuuH
我们先看一个例子(">"先用这个符号代表注释部分):uuH
uuH
//源代码uuH
#include
uuH
int func(int n)uuH
{uuH
int sum = 0,i;uuH
for(i = 0; i < n; i++)uuH
{uuH
sum += i;uuH
}uuH
return sum;uuH
}uuH
uuH
main()uuH
{uuH
int i;uuH
long result = 0;uuH
for(i = 1; i <= 100; i++)uuH
{uuH
result += 1;uuH
}uuH
printf("result[1-100] = %d n ",result);uuH
printf("result[1-250] = %d n ",func(250));uuH
}uuH
uuH
>我们先进行编译uuH
[root@localhost opencTM]# vi test.cuuH
[root@localhost opencTM]# gcc -Werror -g test.c -o testuuH
[root@localhost opencTM]#uuH
uuH
>然后我们进行调试一下uuH
[root@localhost opencTM]# gdbuuH
GNU gdb Everest Linux (6.4-2)uuH
Copyright 2005 Free Software Foundation, Inc.uuH
GDB is free software, covered by the GNU General Public License, and you areuuH
welcome to change it and/or distribute copIEs of it under certain conditions.uuH
Type "show copying" to see the conditions.uuH
There is absolutely no warranty for GDB.Type "show warranty" for details.uuH
This GDB was configured as "i686-pc-linux-gnu".uuH
(gdb)uuH
uuH
>载入文件testuuH
(gdb) file testuuH
Reading symbols from /root/My Documents/opencTM/test...done.uuH
Using host libthread_db library "/lib/liBThread_db.so.1".uuH
(gdb)uuH
uuH
>列出部分代码uuH
(gdb) listuuH
5int sum = 0,i;uuH
6for(i = 0; i < n; i++)uuH
7{uuH
8sum += i;uuH
9}uuH
10return sum;uuH
11}uuH
12uuH
13main()uuH
14{uuH
(gdb)uuH
uuH
>直接按回车继续上一个命令uuH
(gdb)uuH
15int i;uuH
16long result = 0;uuH
17for(i = 1; i <= 100; i++)uuH
18{uuH
19result += 1;uuH
20}uuH
21printf("result[1-100] = %d n ",result);uuH
22printf("result[1-250] = %d n ",func(250));uuH
23}uuH
24uuH
(gdb)uuH
uuH
>设置断点,分别在主函数和func入口处,
电脑资料
《Linux下gdb用法简单介绍》()。uuH(gdb) break mainuuH
Breakpoint 1 at 0x80483a1: file test.c, line 14.uuH
(gdb) break funcuuH
Breakpoint 2 at 0x804837a: file test.c, li
ne 5.uuH(gdb)uuH
uuH
>查看断点信息uuH
(gdb) info breakuuH
Num TypeDisp Enb AddressWhatuuH
1breakpointkeep y0x080483a1 in main at test.c:14uuH
2breakpointkeep y0x0804837a in func at test.c:5uuH
(gdb)uuH
uuH
>运行程序uuH
(gdb) runuuH
Starting program: /root/My Documents/opencTM/testuuH
uuH
Breakpoint 1, main () at test.c:14uuH
14{uuH
(gdb) uuH
uuH
>由于我不想从断点main处运行,想从func函数开始运行我们来个跳转uuH
(gdb) jump funcuuH
Line 5 is not in `main'.Jump anyway? (y or n) yuuH
Continuing at 0x804837a.uuH
uuH
Breakpoint 2, func (n=134513569) at test.c:5uuH
5int sum = 0,i;uuH
(gdb) uuH
uuH
>发现一个错误,可能这里没有基础的人看不明白,此处n=134513569根本不是我们想要的值,这是怎么回事,等等再告诉大家这个jump跳转在跳转的同时可能会出现非比寻常的错误,此处就是一个在n还没有发生累加同时系统随机给n赋值产生错误,我们回过头来再调试!uuH
uuH
(gdb) clearuuH
Deleted breakpoint 2uuH
(gdb) break funcuuH
Breakpoint 3 at 0x804837a: file test.c, line 5.uuH
(gdb) runuuH
Starting program: /root/My Documents/opencTM/testuuH
result[1-100] = 100uuH
uuH
Breakpoint 1, func (n=250) at test.c:5uuH
5int sum = 0,i;uuH
(gdb)uuH
uuH
>这回对了,n=250。。。我们开始没条语句进行调试。。。别忘了,直接按回车进行上一次命令重复!uuH
uuH
(gdb) nextuuH
6for(i = 0; i < n; i++)uuH
(gdb)uuH
8sum += i;uuH
(gdb)uuH
6for(i = 0; i < n; i++)
[1][2][3]下一页