gdb简单调试步骤:
新建一个小demoC程序:
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-250] = %d /n", func(250) );
}
开始gdb调试:
编译:gcc -g -o e e.c
进入gdb: gdb e
(gdb) start //开始运行
Temporary breakpoint 1 at 0x400563: file test.c, line 17.
Starting program: /home/qqq/cc/test
Temporary breakpoint 1, main () at test.c:17
17 long result = 0;
(gdb) n 1 //回车单步运行
18 for(i=1; i<=100; i++)
(gdb) n 1
20 result += i;
(gdb) b 20 //20行加断点
Breakpoint 2 at 0x400574: file test.c, line 20.
(gdb) c //继续运行
Continuing.
Breakpoint 2, main () at test.c:20
20 result += i;
(gdb) display result //显示printf输出结果
1: result = 1
(gdb) c
Continuing.
Breakpoint 2, main () at test.c:20
20 result += i;
1: result = 3
(gdb) quit //退出gdb