当前位置:网站首页>How to use GDB debugging Foundation

How to use GDB debugging Foundation

2020-11-06 01:14:54 RioTian

Despite the current use of VS code You can use plug-ins to build and run programs with one click , but GDB As a debugging tool , It's worth the time to learn .

summary

GDB(GNU Debugger) It's a by GNU Open source organization released 、UNIX/LINUX Under the operating system 、 Command line based 、 Powerful program debugging tool .

Reference resources : gdb Debugging tools

Get into 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 to GDB in

In the folder where the execution file is located , Use gdb Command to enter GDB Debugging environment . Then use file filename
Or use gdb filename (filename Executable file name ).

Set the input parameters of the execution file

Use command set args parameter (parameter For the input parameter ), Load the parameters needed for the run . adopt show args Check the loading parameters .

example :

set args /home/hello.png 2016 12

Use ’show args’ after :

/home/hello.png 2016 12

GDB Internal use make

In case of modification of external documents , Directly in GDB Use in the environment “make” Compile .

File execution

Use r You can execute the file .

sign out GDB Debug environment

Use q Exit debugging environment .

Breakpoint settings

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 line or function names with file names and colons “filename:”.

example :

b hello.cpp:hello 
b hello.cpp:2017

Conditional breakpoints

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 debug

Print variables p

  • Print variable values :p variable example : “p x”
  • Print variable address :p &variable example : “p &x”
  • Print pointer content :p *point example : “p *pData”

Step by step debugging s

Be similar to step in

  • Step by step , Encountered function , Enter the function and execute it .
    s number Perform multi-step execution

Step by step n

similar step over perform , Encountered function , Don't go inside the function , Directly execute the function .

n number Perform multi-step execution

Carry on c

After the program encounters a breakpoint in execution , Use c Carry on

Execute the current function and print out the information finish

Use... Inside a function finish , After executing the current function, print the return information

Set variable value debugging

set var variable = x Set the variable to x After debugging

example :

set var tmp = 5

Put the variable tmp Set to 5,var Keywords make sure there is no conflict

Section error finding

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 seeing the wrong location , Use frame number Go inside the paragraph .
  • After entering the internal, you can print and debug local 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

  • Show 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++ The code needs to add the class name before the function name , Overloaded functions need to add parameter types to the function )
  • Display 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 
  • Display the code around the specified function in the specified file : l filename:function

example :

l hello.cpp:Util::hello    //  Show hello.cpp Under the document ,Util Class hello
  • Show start and end inline code : l fisrt,last

example :

l 1,30  //  From 0 The line shows up to 30 The content of the line 

About GDB Debugging related articles :https://www.cnblogs.com/acceptedzhs/p/13161213.html

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