1.基本语法
1. 启动gdb
start -- 只执行一步
n -- next
s -- step(单步) -- 可以进入到函数体内部
c - continue -- 直接停在断点的位置
2. 查看代码:
l -- list
l 10(函数名)
l filename:行号(函数名)
3. 设置断点:
设置当前文件断点:
b -- break
b 10(函数名)
b fileName:行号(函数名)
设置指定文件断点:
设置条件断点:
b 10 if value==19
删除断点:
delete -- del - d
d 断点的编号
获取编号: info -- i
info b
4. 查看设置的断点
5. 开始 执行gdb调试
执行一步操作:
继续执行:
执行多步, 直接停在断点处:
5. 单步调试
进入函数体内部: s
从函数体内部跳出: finish
不进入函数体内部:
n
退出当前循环: u
6. 查看变量的值: p -- print
7. 查看变量的类型: ptype 变量名
8. 设置变量的值: set var 变量名 = 赋值
9. 设置追踪变量
display
取消追踪变量
undisplay 编号
获取编号: info display
10. 退出gdb调试
Quit
2.demo
hchen/test> gdb tst <
GNU gdb
5.1.1
Copyright
2002 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 "i386-suse-linux"...
(gdb) l <
1
2
3 int func(int n)
4 {
5 int sum=
0,i;
6 for(i=
0; i<n; i++)
7 {
8 sum+=i;
9 }
10 return sum;
(gdb) <
11 }
12
13
14 main()
15 {
16 int i;
17 long
result =
0;
18 for(i=
1; i<=
100; i++)
19 {
20 result += i;
(gdb) break
16 <
Breakpoint
1 at 0x8048496:
file tst.c, line
16.
(gdb) break func <
Breakpoint
2 at 0x8048456:
file tst.c, line
5.
(gdb) info break <
Num Type Disp Enb Address What
1 breakpoint keep y
0x08048496 in main
at tst.c:
16
2 breakpoint keep y
0x08048456 in func
at tst.c:
5
(gdb) r <
Starting program: /home/hchen/test/tst
Breakpoint
1, main ()
at tst.c:
17 <
17 long
result =
0;
(gdb) n <
18 for(i=
1; i<=
100; i++)
(gdb) n
20 result += i;
(gdb) n
18 for(i=
1; i<=
100; i++)
(gdb) n
20 result += i;
(gdb) c <
Continuing.
result[
1-
100] =
5050 <
Breakpoint
2, func (n=
250)
at tst.c:
5
5 int sum=
0,i;
(gdb) n
6 for(i=
1; i<=n; i++)
(gdb) p i <
$
1 =
134513808
(gdb) n
8 sum+=i;
(gdb) n
6 for(i=
1; i<=n; i++)
(gdb) p sum
$
2 =
1
(gdb) n
8 sum+=i;
(gdb) p i
$
3 =
2
(gdb) n
6 for(i=
1; i<=n; i++)
(gdb) p sum
$
4 =
3
(gdb) bt <
(gdb) finish <
Run till
exit from
0x080484e4 in main ()
at tst.c:
24
24 printf(
"result[1-250] = %d /n", func(
250) );
Value returned
is $
6 =
31375
(gdb) c <
Continuing.
result[
1-
250] =
31375 <
Program exited
with code
027. <
(gdb) q <
hchen/test>