当前位置:网站首页>Basic usage of GDB debugging

Basic usage of GDB debugging

2020-11-06 01:35:40 itread01

> Despite the current use of VS code You can use the plug-in to build and run programs , but GDB As a tool for debugging , It's worth the time to learn . ** summary ** **GDB(GNU Debugger)** It's a matter of GNU Open source organizations release 、UNIX/LINUX Under the operating system 、 Based on the command line 、 Powerful debugging tool . Refer to : [gdb A good tool for debugging ](https://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/gdb.html) ## Enter GDB Environment debugging ### Load compilation options ” –g” In order to be in GDB Debugging environment can show the specific error location . example : ``` g++ filename.cpp –g –o filename ``` stay cmake Compiling , It can be done through optional `-DCMAKE_VERBOSE_MAKEFILE=1` , Show the compilation process , Guarantee -g Option is used during compilation . ### Load the executable file to GDB in In the folder where the execution file is located , Use `gdb` Order to enter GDB In the debugging environment . Later use `file filename` Or use `gdb filename` (filename Executable file name ). ### Set the input arguments for the execution file Use command `set args parameter` (parameter Is the input argument ), Load arguments required for execution . Through `show args` View loaded arguments . example : ``` set args /home/hello.png 2016 12 ``` Use ’show args’ After : ``` /home/hello.png 2016 12 ``` ### GDB Internal use make If the external file is modified , Directly in GDB Use... In the environment “make” Compile . ### File execution Use `r` The file can be executed . ### sign out GDB Debugging environment Use `q` Exit debugging environment . ## Breakpoint setting ### How to set breakpoints - Use line number :`b linenumber` example :“b 2017” - Use the function name `b function` example :“b hello” - Use the address `b *address` example :“b *0x404” > If you break points on different files , Prefix a line number or function name with a file name and colon “filename:”. example : ``` b hello.cpp:hello b hello.cpp:2017 ``` ### Conditional breakpoint stay 1 Add the condition after the small point method , Breakpoints stop when conditions hold . example : ``` b 12 if x > y // stay x > y Under the circumstances , Breakpoints work . ``` ### Breakpoint conditions change Modify the condition of the specified breakpoint number : ``` condition breakpointnumber expression ``` example : ``` condition 12 if x = y ``` > `condition breakpointnumber` Stop using conditions , Breakpoints are used unconditionally . example : ``` condition 12 ``` ### Breakpoint information view - View all breakpoint information : `info b` - View the breakpoint information of the specified breakpoint number : `info b number` ### The enabling of breakpoints - Stop that breakpoint . But not deleted : `dis breakpointnumber` - Enable to change the number of breakpoints : `enable breakpointnumber` ## The way to get rid of mistakes ### Print variables `p` - Print variable values :`p variable` example : “p x” - Print variable address :`p &variable` example : “p &x” - Print index content :`p *point` example : “p *pData” ### Single step debugging `s` Similar to **step in** - Step by step , Encountered function , Go inside the function to execute . `s number` Perform multi-step execution ### Step by step `n` Similar **step over** Execute , Encountered function , Don't go inside a function , Execute the function directly . `n number` Perform multi-step execution ### Continue execution `c` After the program runs into a breakpoint , Use `c` Continue execution ### Run the current function and print out the information `finish` Use... Inside a function `finish` , After the current function is printed, information is returned ### Set variable value to debug `set var variable = x` Set the variable to x After debugging example : ``` set var tmp = 5 ``` Change the variables tmp Set to 5,var Keywords make sure there is no conflict ## Segment error query ### Section backtracking `bt` - In the presence of segment fault When , Use the command to view segment errors . - You can get a function that's sitting in the wrong place . Like getting someone's home address . - You can also use `where` ### Enter error section - After looking at the wrong location , Use `frame number` Go inside the paragraph . - After entering the internal, you can print and debug regional variables . example : ``` frame 5 ``` Just go into the section 5. - Switch to the previous layer segment : `up` - Switch to the next layer segment : `down` ### Code shows - Displays the current code : `l` - Show current previous code : `l -` - Show the code around the line : `l number` - Show the code around the function : `l function` (C++ Code needs to precede the function name with the class name , Overload functions need to add argument types to the function ) - Displays the code around the specified line in the specified file : `l filename:linenumber` example : ``` l hello.cpp:1 // From hello.cpp The first line begins to show ``` - Displays the code around the specified function in the specified file : `l filename:function` example : ``` l hello.cpp:Util::hello // Display hello.cpp Under the file ,Util Class function hello ``` - Show start and end inline code : `l fisrt,last` example : ``` l 1,30 // From the 0 The line shows up to 30 The content of the line ``` --- About GDB Debugging related articles :https://www.cnblogs.com/acceptedzhs/p/13161

版权声明
本文为[itread01]所创,转载请带上原文链接,感谢