Program Debug Command GDB

Source: Internet
Author: User
Tags joins gdb debugger

Lock thread
Set scheduler-locking


1. To use this command, first compile the program with GCC-G, such as:
$GCC-G test.c-o test
Compile the TEST.C source program, enter the debug version of this program test

2. Debug program
$GDB Test//debug executable program test
$ (GDB) Start//Use program test to start running

Lists the source code for the specified function, or lists the source program starting with the specified line number
$ (GDB) List function name
$ (GDB) List line number

BackTrace (or BT) view function calls and parameters at all levels
Finish runs continuously until the current function returns, and then stops waiting for the command
FRAME (or F) Framing number Select stack frame
info (or i) Locals view the value of the current stack frame local variable
List (or L) lists the source code, followed by the last position, each column 10 rows
List line number lists source code starting from the first line
The list function names the source code of a function
Next (or N) executes the next line of statements
Print (or p) prints the value of the expression, through which you can modify the value of the variable or call the function
Quit (or Q) to exit the GDB debugging environment
Set Var modifies the value of a variable
Start to execute the program, stop waiting for the command in front of the first line of the main function
Step (or S) executes the next line of statements, and if there is a function call, enter into the function


Http://www.zeuux.org/science/learning-gdb.cn.html

GDB command-line arguments
Start GDB:

GDB Executable
GDB-E executable-c Core-file
GDB Executable-pid Process-id
(Use command ' PS-AUXW ' to view PID of the process)
Option meaning
--help
-H lists the command-line arguments.
--exec=file
-e file specifies an executable file.
--core=core-file
-C core-file indicates the core file.
--command=file
-X file reads the GDB command from the specified file.
--directory=directory
-D directory joins the specified directory to the source file search path.
--cd=directory runs GDB with the specified directory as the current path.
--nx
-N Do not execute commands in the. gdbinit file. By default, the commands in this file are executed after all command-line arguments have been processed.
--batch runs GDB in non-interactive mode. Commands are read from the file, so the-X option is required.
--symbols=file
-S file reads the symbol table from the specified files.
-write allows write operations to executables and core files.
--quiet
-Q Do not print the introduction and copyright information.
--TTY=DEVICE Specifies that device is the standard input output for running the program.
--pid=process-id
-P Process-id
Specifies the process ID to be attached.


GDB command
The commands used in GDB:


Command description
Help lists GDB assistance information.
Help topic lists the GDB commands in the related topics.
The help command lists the command description information.
Apropos Search-word search Related topics.
Info args
I args lists the command-line arguments that run the program.
Info breakpoints lists breakpoints.
Info Break lists the breakpoint number.
Info Break Breakpoint-number lists information for the specified breakpoint.
Info Watchpoints lists the Observer points.
Info registers lists the registers used.
Info threads lists the current thread.
The info set lists the options you can set.
Break and Watch

Break funtion
Break Line-number sets a breakpoint at the specified function or line number.
Break +offset
Break-offset set breakpoints at the front or several lines in the current place of stay.
Break File:func sets a breakpoint at the func in the specified file file.
Break File:nth sets a breakpoint on line nth in the specified file file.
Break *address sets a breakpoint at the specified address. Generally used when there is no source code.
Break Line-number If condition set a breakpoint at the specified location if the condition is met.
Break line thread Thread-number in the specified thread. Use info threads to display the thread number.
Tbreak set a temporary breakpoint. Breakpoints are deleted after a break.
Watch condition set the observer point when the condition is met.
Clear
Clear Func
Clear Nth clears the breakpoint at the function func.
Clears the breakpoint at line nth.
Delete
D Delete all breakpoints or observer points.
Delete Breakpoint-number
Delete range Deletes the specified breakpoint, the Observer point.
Disable Breakpoint-number-or-range
Enable Breakpoint-number-or-range does not delete breakpoints, just set it to invalid, or valid.
Example:
Show Breakpoint: Info break
Invalid setting: Disable 2-9
The Enable once Breakpoint-number setting specifies that the breakpoint is valid and is not valid when the breakpoint is reached.
The Enable Del Breakpoint-number setting specifies that the breakpoint is valid and deletes it when the breakpoint is reached.
Finish continues execution to the end of the function.
Line execution

Step
S
Step Number-of-steps-to-perform into the next line of code execution, will enter the function inside.
Next
N
Next number executes the next line of code. But it doesn't go inside the function.
Until
Until Line-number continues to run until the specified line number, or function, address, etc. is reached.
Return
Return expression pops up the selected stack frame. If the parameter is specified later, the value of the expression is returned.
Stepi
Si
Nexti
NI executes the next assembly/CPU instruction.
Info signals
Info handle
Handle Signal-name option performs the following actions when a signal is received: nostop (do not stop program), Stop (Stop program execution), print (display signal), noprint (not shown), pass/ Noignore (allows the program to process the signal), Nopass/ignore (does not allow the program to accept the signal)
Where displays the current line number and the function in which it is located.
Program Stack

BackTrace
Bt
BT Inner-function-nesting-depth
Bt-outer-function-nesting-depth shows the current stack's trace, the current function.
BackTrace full prints the values of all local variables.
Frame number
F number selects the specified stack frame.
Up number
Down number moves the specified stack frame up or down.
Info Frame addr Describes the selected stack frame.
Info args
Info All-reg
Info locals
Info catch shows the parameters of the selected stack frame, local variables, exception handling functions. All-reg also lists floating-point registers.
Source Code

List
L
List Line-number
List function
List-
List start#,end#
List filename:function lists the corresponding source code.
Set Listsize count
Show Listsize sets the number of lines when the list command prints the source code.
Directory Directory-name
Dir directory-name
Show directories adds the specified directory before the source code path.
directory when no parameters are followed, clear the source code directory.
Examine Variables

Print variable
P variable
P file::variable
P ' file ':: variable prints the value of the specified variable.
P *[email protected] prints the first length entry in the Arrary-var.
p/x var prints the integer variable var in hexadecimal.
p/d var prints the variable var as a signed integer.
p/u var prints the variable var as an unsigned integer.
p/o var prints the variable var as an octal number.
p/t var
x/b Address
x/b &variable Prints the value of the var variable in integer binary form.
P/C variable when characters are printed.
p/f variable prints variable var in floating-point format.
P/A variable prints the address in hexadecimal form.
X/W Address
X/4B &variable Prints the specified address in a four-byte set of ways.
Call expression is similar to print, but does not print void.
Disassem addr to disassemble the instructions in the specified address.
Controlling GDB

Set gdb-option value sets the options for GDB.
Set Print array on
Set Print array off
The show print array is printed in a readable form. The default is off.
Set Print array-indexes on
Set Print array-indexes off
Show print array-indexes Prints the subscript of an array element. The default is off.
Set Print pretty on
Set print pretty off
Show print pretty format prints the output of the C struct body.
Set Print Union on
Set Print union off
Show print Union prints the union in C. The default is on.
Set Print Demangle on
Set Print Demangle off
Show print Demangle controls the printing of names in C + +. The default is on.
Working Files

Info files
Info share lists the current files, shared libraries.
File file treats file as a debugged program. If no parameters are specified, discard.
The core file treats files as core. If no parameter is specified, it is discarded.
exec file treats file as the execution program. If no parameter is specified, it is discarded.
The symbol file reads the symbols table from file. If no parameter is specified, it is discarded.
Load file dynamically links into the file and reads its symbol table.
Path directory joins directory directories in the path to search for executable and symbol files.
Start and Stop

Run
R
Run command-line-arguments
Run < infile > outfile executes the program from the beginning and redirects are allowed.
Continue
C resumes execution until the next breakpoint or observer point.
Continue number continues execution, but the current breakpoint number is ignored. Useful when breakpoints are in a loop.
Kill stop program execution.
Quit
Q Exit the GDB debugger.


GDB Operation Tips

You need to add the "-g" option to GCC when compiling the executable so that it will add additional debugging information to the generated executable file.
Do not use the compiler's optimization options, such as "-O", "-o2". Because the compiler will change the program flow for optimization, which is not conducive to debugging.
Executing shell commands in GDB can be used: Shell command
The GDB command can be complete by using the TAB key. Press the TAB key two times to see all possible matches.
GDB command abbreviation: For example, the BRE in the info bre is equivalent to breakpoints.
GDB's operation in Emacs:
Emacs Button Action
M-x gdb Switch to GDB mode.
C-h m shows the GDB mode introduction information.
M-s is equivalent to the step command in GDB.
M-n is equivalent to the next command in GDB.
The m-i is equivalent to the Stepi command in GDB.
C-c C-f is equivalent to the finish command in GDB.
The m-c is equivalent to the Continue command in GDB.
The m-u is equivalent to the UP command in GDB.
M-d is equivalent to the down command in GDB.


x/20x &login_request

Program Debug Command GDB

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.