02.Linux下C语言程序的调试

你可以用 gcc -S test.c 命令生成test.c对应的汇编语言文件,是at&t格式的。

xumh@ubuntu:~/cpp$ gcc --o gdb gdb.c   //参数-g是为了产生debug信息
xumh@ubuntu:
~/cpp$ ls
gdb  gdb.c  myfirst  test1.c  test1.s
xumh@ubuntu:
~/cpp$ gdb gdb //用GDB命令调试刚才生成的gdb程序
GNU gdb 
6.8-debian
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 "x86_64-linux-gnu"
(gdb) list
1       //测试gdb实现对c语言源程序的调试
2
3       #include <stdio.h>
4       int main(int argc, char **argv)
5       {
6               int sum = 0;
7               int i=0;
8               for(; i<10; i++)
9                       sum += i;
10              printf("the value is:%d.\n",sum);
(gdb) 
break 9//还可以对function中断
Breakpoint 1 at 0x4004b7: file gdb.c, line 9.
(gdb) run
      //执行程序
Starting program: 
/home/xumh/cpp/gdb

Breakpoint 
1, main (argc=1, argv=0x7fff42873018) at gdb.c:9
9                       sum += i;
(gdb) watch sum  
//观察变量
Hardware watchpoint 
2: sum
(gdb) watch i
Hardware watchpoint 
3: i
(gdb) next   
//单步执行
8               for(; i<10; i++)
(gdb) next
Hardware watchpoint 
3: i

Old value 
= 0
New value 
= 1
0x00000000004004c1 in main (argc=1, argv=0x7fff42873018) at gdb.c:8
8               for(; i<10; i++)
(gdb) next

Breakpoint 
1, main (argc=1, argv=0x7fff42873018) at gdb.c:9
9                       sum += i;
(gdb) next
Hardware watchpoint 
2: sum

Old value 
= 0
New value 
= 1

main (argc
=1, argv=0x7fff42873018) at gdb.c:8
8               for(; i<10; i++)
(gdb) next
Hardware watchpoint 
3: i

Old value 
= 1
New value 
= 2
0x00000000004004c1 in main (argc=1, argv=0x7fff42873018) at gdb.c:8
8               for(; i<10; i++)
(gdb) next

Breakpoint 
1, main (argc=1, argv=0x7fff42873018) at gdb.c:9
9                       sum += i;
(gdb) next
Hardware watchpoint 
2: sum

Old value 
= 1
New value 
= 3
main (argc
=1, argv=0x7fff42873018) at gdb.c:8
8               for(; i<10; i++)
(gdb) next
Hardware watchpoint 
3: i

Old value 
= 2
New value 
= 3
0x00000000004004c1 in main (argc=1, argv=0x7fff42873018) at gdb.c:8
8               for(; i<10; i++)
(gdb) next

Breakpoint 
1, main (argc=1, argv=0x7fff42873018) at gdb.c:9
9                       sum += i;
(gdb) next
Hardware watchpoint 
2: sum

Old value 
= 3
New value 
= 6
main (argc
=1, argv=0x7fff42873018) at gdb.c:8
8               for(; i<10; i++)

(gdb)

原文地址:https://www.cnblogs.com/flaaash/p/1327285.html