看到一篇GDB 的不错的文章,跟大家分享一下。
GDB Tutorial: Advanced Debugging Tips For C/C++ Programmers
以下是我做的一点笔记。
add debug option -g when compile the source code
basic:
b
c
step
n
list
bt
run
where
watch
Run A Program With Arguments
There are two ways you can feed arguments to your program
1. gdb --args executablename arg1 arg2 arg3
--args says thateverything that follows is the command and arguments, just as you'd normallytype them at the commandline prompt
2. gdb programe
r arg1 arg2 ....
1. breakpoint(断点)
b linenumber
b filename:linenumber
b function
b filename:function
Get information aboutall breakpoints
info breakpoint
delete an existing breakpoint
delete breakpoints 1
set a temporary breakpoint
tbreak powere
If you have a program without debug symbols then you can’tuse any of the above option. Instead, the gdb allows you to specify a breakpoint for memory addresses
break upon matching memory address
b *(memory address)
break after a condition
b linenumber if variable==1
2.get the information about the arguments passed to afunction (查看函数传入参数)
info args
3. get the information about the local variables
info locals
4.how to execute a function to the end after breakpoint, itwill run through the entire function
finsh
5.how to print the line number in GDB wihile debugging
frame
6.add watchers
Adding watch points is same like telling the debugger togive an dynamic analysis of changes to the variables.And it's easy to add awatch point in your code.
watch variable
7.How to skip/ignore breakpoints
while you are runningthrough a loop in your code and wouldn't want to pause for every break, thenignore command can help.
Here is how you can skip a breakpoint the number of timesyou want.
First check the index of the break point which you want toignore. Use the info breakpoints command.
Then run the following command in the gdb debugger, say wewant to ignore the break for 1000 times
ignore 1 10000
8.debug dynamic libs need to set
set breakpoint pending on
otherwise it will report the error:forexample
(gdb) b mps_guide_db.c:1699
No source file named mps_guide_db.c.
Make breakpoint pending on future shared library load? (y or[n]) [answered N; input not from terminal]