1、启动你的程序,可以按照你的自定义的要求随心所欲的运行程序。
2、可让被调试的程序在你所指定的调置的断点处停住。(断点可以是条件表达式)
3、当程序被停住时,可以检查此时你的程序中所发生的事。
4、动态的改变你程序的执行环境。
调试的程序如下;
[root@localhost ~]# cat tst.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 = 0; i<= 100; i++) { result +=i; } printf("result[1-100] = %d n", result); printf("result[1-250] = %d n", func(250)); }
gcc -g tst.c -o tst 其中的-g参数用于保存调试的信息。
进入gdb调试界面:
[root@localhost ~]# gdb tst GNU gdb (GDB) CentOS (7.0.1-45.el5.centos) Copyright (C) 2009 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 "i386-redhat-linux-gnu". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>... Reading symbols from /root/tst...done. (gdb)
输入l命令来显示源码:
(gdb) l 7 { 8 sum += i; 9 } 10 return sum; 11 } 12 13 main() 14 { 15 int i; 16 long result = 0; (gdb)