当前位置:网站首页>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
边栏推荐
- 缘结西安 | CSDN与西安思源学院签约,全面开启IT人才培养新篇章
- vscode小技巧
- RTOS mainstream assessment
- 数据仓库—什么是OLAP
- Hbuilderx + uniapp packaging IPA submission app store stepping on the pit
- ./gradlew: Permission denied
- 9419页最新一线互联网Android面试题解析大全
- [point cloud series] summary of papers related to implicit expression of point cloud
- SHA512 / 384 principle and C language implementation (with source code)
- Mui wechat payment pit
猜你喜欢
【动态规划】221. 最大正方形
SAP UI5 应用开发教程之七十二 - SAP UI5 页面路由的动画效果设置
[point cloud series] Introduction to scene recognition
LeetCode_DFS_中等_695.岛屿的最大面积
You and the 42W bonus pool are one short of the "Changsha bank Cup" Tencent yunqi innovation competition!
Machine learning -- PCA and LDA
Imx6ull QEMU bare metal tutorial 2: usdhc SD card
切线空间(tangent space)
[point cloud series] unsupervised multi task feature learning on point clouds
FatFs FAT32 learning notes
随机推荐
SSM整合之pom.xml
UEFI learning 01-arm aarch64 compilation, armplatformpripeicore (SEC)
Nodejs + websocket cycle small case
[point cloud series] Introduction to scene recognition
Introduction to metalama 4 Use fabric to manipulate items or namespaces
Office 2021 installation package download and activation tutorial
[point cloud series] summary of papers related to implicit expression of point cloud
“湘见”技术沙龙 | 程序员&CSDN的进阶之路
Part 3: docker installing MySQL container (custom port)
校园外卖系统 - 「农职邦」微信原生云开发小程序
9419 page analysis of the latest first-line Internet Android interview questions
playwright控制本地穀歌瀏覽打開,並下載文件
MySQL5. 5 installation tutorial
ESP32 VHCI架构传统蓝牙设置scan mode,让设备能被搜索到
You and the 42W bonus pool are one short of the "Changsha bank Cup" Tencent yunqi innovation competition!
切线空间(tangent space)
Common analog keys of ADB shell: keycode
5道刁钻的Activity生命周期面试题,学完去吊打面试官!
Stack protector under armcc / GCC
web三大组件之Servlet