Gdb debugger command learning summary notes

Source: Internet
Author: User
Tags gdb debugger

Linux contains a very useful debugging tool, GDB (GNU debuger), which can be used to debug C and C ++ programs. It is no less powerful than many graphical interface debugging tools in windows.

Like all common debugging tools, GDB provides the following functions:
# Value of the variable in the Monitoring Program
# Set breakpoints in a program
# One-Step Program Execution

Before using GDB, you must first load the executable file. for debugging, the file must contain debugging information, therefore, when compiling with GCC or CC, you need to use the-G parameter to open the debugging options of the program.

When Debugging starts, you must first load the program to be debugged. You can use either of the following methods:
* After GDB is started, run the following command:
File executable file path
* Load the program at GDB startup:
GDB executable file path

After the program is loaded, the next step is to set the breakpoint and add the variables to be monitored. Next, we will introduce the commands commonly used in this process one by one:
* List: display the code in the program. Common formats include:
List
Output 10 lines of program code after the last LIST command is called.
List-
Output 10 lines of program code starting from the last call of the list command.
List n
Output 10 lines of program code near line N.
List Function
Output 10 lines of program code before and after the function.
* Forward/search: searches for the program line that matches a string from the current row. Format:
Forward/search string
The searched row number is saved in the $ _ variable and can be viewed using the Print $ _ command.
* Reverse-Search: opposite to forward/search, search for strings forward. The same format is used.
* Break: sets a breakpoint in a program. When the program runs on a specified line, the execution is paused. Format:
The line number of the breakpoint to be set for the break.
* Tbreak: sets a temporary breakpoint, which takes effect only once after it is set. Format:
The line number of the temporary breakpoint to be set for tbreak
* Clear: opposite to break, clear is used to clear breakpoints. Format:
The row number of the breakpoint to be cleared.
* Run: start the program. parameters can be passed to the program being debugged after run.
* Awatch: Used to add an observation point (add Watch). The format is as follows:
Awatch variables or expressions
When the expression value changes or the expression value is read, the program stops running.
* Watch: similar to awatch, it is used to set observation points. However, the program stops running only when the expression value changes. Usage format:
Watch variable or expression
Note that both awatch and watch must set the observation point during the running of the program. You can only set it after running run.
* Commands: Set to execute specific commands when a breakpoint is encountered. The format is as follows:
Commands
Set the command to be executed when the last breakpoint is encountered
Commands n
Set the command to be executed when the breakpoint number N is encountered
Note that commands is followed by the breakpoint number, rather than the line number of the breakpoint.
After entering the command, you can enter the command to be executed after the breakpoint is met. Each command line contains one command. after entering the last command, enter end to end the input.
* Delete: Clear the breakpoint or automatically displayed expression. Format:
Delete breakpoint number
* Disable: disables a specified breakpoint. Format:
Disable Checkpoint number list
Use spaces to separate breakpoint numbers.
* Enable: opposite to disable, it restores a failed breakpoint. Format:
Enable breakpoint number list
* Ignore: Ignore breakpoints. Format:
Number of ignore checkpoint numbers ignored
* Condition: Set a breakpoint to take effect under certain conditions. Format:
Condition breakpoint number condition expression
* Cont/continue: enables the program to continue running after the breakpoint is paused. Format:
Cont
Skip the current breakpoint to continue running.
Cont n
Skip n breakpoints and continue running.
When N is 1, cont 1 is Cont.
* Jump: Jump the program to the specified line to start debugging. Format:
Jump row number
* Next: continue to execute the statement, but skip the call of the subroutine. Format:
Next
Execute a statement
Next n
Execute n statements
* Nexti: A statement executed in a single step. However, unlike next, nexti traces the subprogram, but does not print the statements in the subprogram. The same format is used.
* Step: similar to next, but it will track the interior of the subprogram and display the execution of the subprogram. The same format is used.
* Stepi: similar to step, but more detailed than step. It is a combination of nexti and step. The same format is used.
* Whatis: displays the Data Type of a variable or expression. Format:
Whatis variable or expression
* Ptype: similar to whatis, it is used to display data types, but it can also display types defined by typedef. Format:
Ptype variable or expression
* Set: set the value of the variable in the program. Format:
Set variable = expression
Set variable: = expression
* Display: adds an expression for displaying values. Format:
Display expression
* Info display: displays the expressions of all values to be displayed.
* Delete display/undisplay: Delete the expression of the value to be displayed. Format:
Delete display/undisplay expression no.
* Disable display: A value to be expressed is not displayed for the moment. Format:
Disable display expression no.
* Enable display: opposite to disable display, use an expression to restore display. Format:
Enable display expression number
* Print: print the value of a variable or expression. Format:
Print variable or expression
The expression has two special meanings: $ and $.
$ Indicates the first sequence number of a given sequence number. $ indicates the first two sequence numbers of a given sequence number.
If $ and $ are not followed by a number, the given sequence number is the current sequence number.
* Backtrace: prints a specified number of stack frames ). Format:
Number of Backtrace stack Frames
* Frame: prints the stack frame. Format:
Frame Number of the frame Stack
* Info frame: displays the details of the current stack frame.
* Select-frame: select a stack frame. After the stack frame is selected, you can use info frame to display stack frame information. Format:
Select-frame stack frame number
* Kill: End debugging of the current program.
* Quit: Exit GDB.

To view all gdb commands, enter the tab twice under GDB and run "Help Command" to view the detailed usage format of command.
This article only briefly summarizes the usage of some common commands during GDB debugging. For more information about GDB, see the official documentation of GDB:
Http://www.gnu.org/software/gdb/documentation/

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.