Getting Started with the "C Language" GDB debugger

Source: Internet
Author: User
Tags gdb debugger
Preface

As a computer department students, contact programming has been two years, a deeper feeling: The light code is far from enough, the ability to debug the code is very important.
After often students say, this I know the idea, the code also played out, why ran out of the results are always less than I want. Code 1 hours, debugging 1 days this happens often.
Of course, this is not the ability to code, and then good programmers for various reasons to play Bug program, Debug is a potluck.
For more than two years, people around me (including me) have mostly used "print" in debug--adding a lot of output statements to the source code to see what happens during code execution. Admittedly, this method is simple and easy, for a small number of code, this method is more convenient, but once the code increase, the program complex, it is easy to mess up chaos.
Recently contacted the GDB debugger, the function is very powerful, I hope in the actual combat can be familiar with this tool, use it to complete debugging. GDB main features monitor changes in the value of variables set breakpoints to suspend execution on a specified line of code to facilitate observation of problems with the use of core files produced by a single Step Code analysis crash program

1. When stepping, do not go inside the standard library function, execute the following commands in GDB:

Show Debug-file-directory
#显示路径为/usr/lib/debug
set debug-file-directory
#将路径设置为空
Show Debug-file-directory
#显示路径为空
#每一次进入gdb都需要重新设置一次
#单步执行时, you can use next, do not go inside the function

2. Debug with GDB, use GCC compile-time plus-g option, add debug information to program:

Gcc-ggdb3-o Test test.c
#-ggdb3 contains debug information to the fullest extent
Debugging Steps

1. Call GDB

GDB test

2. Set breakpoints and debug

(GDB) Break main
#在main函数内第一个非简单赋值语句处中断
(gdb) Break line number
#break可用 ' B ' instead of
(GDB) Run
#执行程序
(GDB)
step #单步执行, which can be replaced with ' s ', will display the line number + the line code (code to be executed next)
#类似的有next, but next does not trace to the function internal
(GDB) print expression
(gdb) Print variable = The expression
(gdb) print start expression @ The amount of contiguous memory space to print
#打印表达式or变量的值, numbered and stored in the history. The $n represents the nth Print command, and the $ $n represents the N+1
(gdb) display expression #每当运行到断点 from the current number
to display the value of an expression
(gdb) quit
#退出gdb
(gdb) Finish
#强制退出调试
Display data Commands
#1. Display and Print commands
#2. Memory Check command
x/format address
#format: <n/f/u>, representing the number of display units, format, number of bytes.
#如x/2c: Displays 2 cells, char, 4 bytes (the U parameter defaults to 4)
#3. printf command
#4. Set command: Display and modify data
Set Variable height=200
Set Breakpoints
Break test:21   #文件test的第21行 break
printmessage  #函数printmessage处设置断点
continue    # The recovery program runs (to the next breakpoint or end)
cont 2  #忽略断点2次
condition 1 checksum>700    #当表达式满足时, the 1th breakpoint is interrupted
tbreak   Set breakpoints at 41 lines, break the
enable breakpoint number only once #恢复暂时失效的断点
disable breakpoint number    #使断点失效
Delete  breakpoint number    #删除断点
Clear   Breakpoint line number    #清除断点
Set Watch Window
Watch counter>15    #当counter >15, the counter value needs to be in scope
View stack Information
#使用backtrace或者bt
frame <n>   #frame 0 represents the top of the stack, and frame 1 represents the first layer of the stack
#info args: Displays the parameter names and values of the current function
#info Locals: Displays all local variables and values in the current function
View Source program
List <linenum>  #显示第linenum行周围的源程序
list <function> #显示函数的源程序
Search Source Code
Forward-search <regexp> #向前搜索, RegExp is a regular expression
reverse-search <regexp> #向后搜索
View Assembly Code
disassemble func    #查看函数func的汇编代码
change the execution of a program
Set var width =  #改变变量的取值
jump <linespec> #跳转到某个运行点
jump <address>  #跳转到某个内存地址
# Jump does not change the contents of the current stack, use caution. return
<expression> #强制函数返回
program crashes: Core dump analysis
GDB Test core.****  #将core装载进gdb to find the cause and location
of  the crash bt #打印栈桢指针 (you know the last line that each function is executed)
Conclusion

These are just some of my notes in the learning process, there is no specific example to illustrate, more abstract. Suggested that beginners according to the knowledge point, find an example step-by-step actual exercise, practice makes perfect, learn also very relaxed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.