2008年7月24日星期四

gdb调试

今天,学习gdb调试,每一天都要有自己的计划。
在用gcc编译的时候就应当添加-g这个选项的。比如:$ gcc -g -o useupper useupper.c
然后你就进入到了(gdb) 你就可以输入命令了,如输入list
当然,我们一般还有设置一些断点,看看我们的程序运行到那里有什么问题,用break命令。(gdb) help break就可以看到他的详细信息了。然后当然用到next和step的命令了,他们的对比如下:
(gdb) help next
Step program, proceeding through subroutine calls.
Like the "step" command as long as subroutine calls do not happen;
when they do, the call is treated as one instruction.
Argument N means do this N times (or till program stops for another reason).
(gdb) help step
Step program until it reaches a different source line.
Argument N means do this N times (or till program stops for another reason).
我们也可以不一步一步用next和step的命令执行我们的代码,而是要直接执行到n行为止,那我们就用until命令吧!用print命令可以查看变量的值。有时可用finish继续执行直到目前的函数结束。
(gdb) help finish
Execute until selected stack frame returns.
Upon return, the value returned is printed and put in the value history.
命令backtrace和bt和where是一样的
(gdb) help bt
Print backtrace of all stack frames, or innermost COUNT frames.
With a negative argument, print outermost -COUNT frames.
Use of the 'full' qualifier also prints the values of the local variables.
我们也可以用display命令告诉 gdb,在每次程序停止在断点位置时自动显示数组的内容。
(gdb) commands我们用这个命令可以修改断点设置,不在断点处停下而是显示,然后继续执行。
Type commands for when breakpoint 2 is hit, one per line.
End with a line saying just "end".
>cont
>end
这里还可以设置其他的一些,如为了打补丁,改变变量用set variable 变量
而cont命令是
Continue program being debugged, after signal or breakpoint.
If proceeding from breakpoint, a number N may be used as an argument,
which means to set the ignore count of that breakpoint to N - 1 (so that
the breakpoint won't break until the Nth time it is reached).
我们可以用info查看一些信息,比如说查看断点及display命令的内容。
我们可以用disable来禁用我们过去的设置。
其他调试工具:
ctags,cxref和cflow等就是一些静态分析程序,他们可以通过源文件提供有关函数调用和函数所在位置的有用信息。prof和gprog等就是一些动态分析程序,他们提供的信息包括已经执行了哪些函数以及这些函数的执行时间。

没有评论:

time