当前位置:网站首页>Use of GDB
Use of GDB
2022-04-23 13:37:00 【Stack Overflow? Tan90】
GDB Use
Commonly used gdb Debugging command and usage
Command name | Command abbreviation | Command specification |
---|---|---|
run | r | Run a program |
continue | c | Let the suspended program continue |
break | b | Add Breakpoint |
tbreak | tb | Add temporary breakpoint |
backtrace | bt | View the call stack of the current thread |
frame | f | Switch to the top note of the current calling thread. |
info | info | View breakpoints 、 Thread and other information |
enable | enable | Enable a breakpoint |
disable | disable | Disable a breakpoint |
delete | del | Delete breakpoints |
list | l | Display code |
p | Display variable values , Such as :p + Variable name , Indicates the value of the display variable . You can also add functions :p + function , You can also add an expression | |
ptype | ptype | Look at the type of the variable |
thread | thread | Switch to the specified thread |
next | n | Run to next line |
step | s | Execute the current line statement , If the statement is a function call , Enter the function to execute the first statement Be careful : If the function is a library function or a function provided by a third party , use s I can't get in , Because there is no source code , If it is a custom function , As long as you have the source code, you can go in |
until | u | Run to the specified row , stop |
finish | fi | End the current call function , Function call to the upper level |
return | return | Ends the current calling function and returns the specified value. , Function call to the upper level |
jump | j | Jump the execution flow of the current program to the specified line or address |
disassemble | dis | Look at assembly code |
set args | Set the parameters of the main program Setup method : (gdb)set args + Parameters 1 + Parameters 2 |
|
set var | Set the value of the variable , Suppose the program has two variables : Such as int i;char arr[20]; set var i = 10; hold i Is set to 10; set var arr = “string”; hold arr Is set to ”string“, Notice that it's not strcpy |
|
show args | View the set command line parameters | |
watch | watch | Monitor whether the value of a variable or memory address has changed |
display | display | Monitored variable or memory address , Automatically output monitored variables or memory addresses after program interruption |
dir | dir | Redirect source file location |
Be careful
When debugging the program, you need to add -g Options , It is also recommended to turn off the optimization options for compiler programs , Compiler program optimization generally includes 5 A level , namely O0-O4, among O0 It means no optimization , from O1-O4, The optimization level is getting higher and higher . Recommended O0.
start-up gdb Debugging method
Use gdb There are generally three ways to debug a program :
-
Directly debug the target program : gdb + filename
-
attach To the process :gdb + attach + pid
- When a program has started , But I want to debug the program , You need to gdb attch + Process to be debugged id. In the use of attach After reaching the target process , The debugger will stop , Use... Directly at this time continue Command the program to continue running .
- If you want to end this debugging after debugging the program , And will not have any impact on the current process , Can be in gdb Command interface input detach Separation of command program and debugger .
-
debugging core file – Locate the process crash problem :gdb + filename corename
-
linux By default, the system will not start when the program runs out core File function , have access to ulimit -c To check whether the system turns on this function . If you find that core file size yes 0 Words , Indicates that the build is closed core File options , Can pass ulimit + Option name + Set the value To modify the . It can be changed to the maximum allowable size , Such as 1024, Indicates the generation of the maximum core File size is 1024KB; It can also be changed to infinity , Such as ulimit -c unlimited
-
Be careful : When the session is closed , This value will also become 0. If you want this option to be permanent , There are two ways to set :
-
stay /etc/security/limits.conf Add a line to the list :
#<domain> <type> <item> <value> * soft core unlimited
-
hold ulimit -c unlimited Add to /etc/profile In file , Just put it on the last line . Execute after successful modification
source /etc/profile
You can make the configuration take effect immediately . Of course, this only works for root user , If you want to use only one user , You can put ulimit -c unlimited Add to the corresponding of the user ~/.bashrc perhaps ~/.bash_profile In file -
Generated core The default file naming method is core.pid. The location is in the directory where the crash program is located .
-
-
If , When multiple programs crash at the same time , from core The name of the file corresponds to which service , How to solve it ?
-
Mode one : When the program starts, it is recorded pid
-
Mode two : Customize core Name and directory of the file
-
/proc/sys/kernel/core_uses_pid Can be controlled in the generated core Add to the file name pid As an extension , If added, the file content is 1, Otherwise 0
-
/proc/sys/kernel/core_pattern You can set the formatted core File save location or file name . The modification method is as follows :
echo "/corefile/core-%e-%p-%t" > /proc/sys/kernel/core_pattern
Parameter name Parameter meaning %p add to pid To core In the filename %u Add current uid To core In the filename %g Add current gid To core In the filename %s Adding results in core The signal to core In file %t add to core File generation time (UNIX) To core In file %h Add host name to core In the filename %e Add program name to core In the filename
-
-
-
gdb Debugging multithreaded methods
-
Use gdb After running the program , Use ctrl + c Interrupt the program , Use info threads Command to see how many threads there are in the current process .
-
have access to thread + Switch the thread number to the corresponding thread , Use bt View the call stack of the thread
-
Sometimes in the case of multithreading , You only want to debug a thread , Instead of switching to another thread , You can use the options to lock the top note of the debug thread.
scheduler-locking
This option has three values , Namely on( lock )、step( lock )、off( Release lock ). Use as followsset scheduler-locking on/step/off
- on and step difference : Set to on when , Current thread execution next、step、until、finish、return On command , Other threads will not execute . and step If and only if next、step The command locks the current thread , Use until、finish、return Wait for debugging commands in the thread ( Non single step debugging command ), Other threads have a chance to run .
gdb Debug multiprocess methods
-
Method 1 : Now? shell Use... In the window gdb Debug parent process , When a child process is fork After coming out , Opening a new one shell Window use gdbattach The order will gdb attach To child processes .
-
Mode two :gdb The debugger provides a follow-fork Options , adopt set follow-fork mode Set up a process fork Create new subprocesses to make ,gdb Continue debugging parent or child processes .
1. Debug parent process :set follow-fork-mode parent( default ) 2. Debug child :set follow-fork-mode child 3. Set debug mode :set detach-on-fork [on|off] 1. The default is on; Indicates when debugging the current process , Other processes continue to run 2. off: When debugging the current process , Other processes are gdb Hang up 4. View the process of debugging :info inferiors 5. Toggle the current debugging process :inferior + process ID 6. Be careful : To switch process debugging, you need to set the debugging mode to off
In this paper, the reference 《c++ The essence of server development 》
If this article helps you , Remember to connect three times with one button , One button three times laughing, ha ha , Excellent code capability !
My ability is limited , If there is a mistake , Don't hesitate to correct ; Originality is not easy. , Welcome to reprint , Reprint please indicate the source !
版权声明
本文为[Stack Overflow? Tan90]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231333555171.html
边栏推荐
- 面试官给我挖坑:单台服务器并发TCP连接数到底可以有多少 ?
- TIA博途中基于高速计数器触发中断OB40实现定点加工动作的具体方法示例
- Ding ~ your scholarship has arrived! C certified enterprise scholarship list released
- Filter and listener of three web components
- TCP 复位gongji原理和实战复现
- Cross carbon market and Web3 to achieve renewable transformation
- 【行走的笔记】
- Machine learning -- model optimization
- Unified task distribution scheduling execution framework
- [tensorflow] sharing mechanism
猜你喜欢
Machine learning -- PCA and LDA
[quick platoon] 215 The kth largest element in the array
QT calling external program
LeetCode_DFS_中等_695.岛屿的最大面积
EMMC / SD learning notes
Common types and basic usage of input plug-in of logstash data processing service
[point cloud series] neural opportunity point cloud (NOPC)
Solve the problem that Oracle needs to set IP every time in the virtual machine
Example interview | sun Guanghao: College Club grows and starts a business with me
AI21 Labs | Standing on the Shoulders of Giant Frozen Language Models(站在巨大的冷冻语言模型的肩膀上)
随机推荐
[walking notes]
Solve the problem of Oracle Chinese garbled code
榜样专访 | 孙光浩:高校俱乐部伴我成长并创业
Mui wechat payment pit
[point cloud series] relationship based point cloud completion
Servlet of three web components
TERSUS笔记员工信息516-Mysql查询(2个字段的时间段唯一性判断)
Summary of request and response and their ServletContext
在 pytorch 中加载和使用图像分类数据集 Fashion-MNIST
【快排】215. 数组中的第K个最大元素
软考系统集成项目管理工程师全真模拟题(含答案、解析)
Tangent space
Detailed explanation of ADB shell top command
Machine learning -- naive Bayes
5 tricky activity life cycle interview questions. After learning, go and hang the interviewer!
切线空间(tangent space)
为什么从事云原生开发需要学习容器技术
Armv8m (cortex M33) MPU actual combat
Analysis of the latest Android high frequency interview questions in 2020 (BAT TMD JD Xiaomi)
Explanation of input components in Chapter 16