GDB debug Manual [Go]

Source: Internet
Author: User
Tags exception handling memory usage gdb debugger

Linux contains a GNU debug program called GDB. GDB is a powerful debugger for debugging C and C + + programs. It allows you to observe the program's internal structure and memory usage while the program is running. Here are some of the features that GDB provides: It enables you to monitor the value of variables in your program, which allows you to set breakpoints so that the program stops executing on the specified line of code, and it enables you to execute your code in line. Type gdb on the command line and press ENTER to run GDB. (Windows needs to install MinGW or Cygwin and need to configure environment variables to use)

GDB command-line arguments

Start GDB:

L GDB Executable

L GDB-E executable-c Core-file

L GDB executable-pid Process-id

(Use command ' PS-AUXW ' to view PID of the process)

Options

Meaning

Help
-H

Lists command-line arguments.

–exec=File
-E file

Specifies the executable file.

–core=Core-file
-C Core-file

Specifies the core file.

–command=File
-X file

Reads the GDB command from the specified file.

–directory=Directory
-D Directory

Adds 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 the commands in the. gdbinit file. By default, the commands in this file are executed after all command-line arguments have been processed.

–batch

Run 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 file.

-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 programs.

–pid=Process-id
-P Process-id

Specifies the process ID to be attached.

GDB command

The commands used in GDB:

Command

Describe

Help

Lists GDB help information.

Help Topic

Lists the GDB commands in the related topic.

Help command

Lists command description information.

Apropos Search-word

Search for 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.

Info set

Lists the options you can set.

Break and Watch

Break funtion
Break Line-number

Sets a breakpoint at the specified function, or at the line number.

Break +Offset
Break-Offset

Sets a breakpoint 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 in the nth line 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 ifcondition

If the condition is met, set the breakpoint at the specified location.

Break Line threadthread-number

Interrupts in the specified thread. Use info threads to display the thread number.

Tbreak

Sets a temporary breakpoint. Breakpoints are deleted after a break.

Watch Condition

Sets 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

Do not delete the breakpoint, just set it to invalid, or valid.
Example:
Show Breakpoint: Info break
Invalid setting: Disable 2-9

Enable onceBreakpoint-number

Sets the specified breakpoint to be valid, which is not valid when the breakpoint is reached.

Enable del Breakpoint-number

Sets the specified breakpoint to be valid and removes it when the breakpoint is reached.

Finish

Continue execution to the end of the function.

Line execution

Step
S
Step number-of-steps-to-perform

The execution of the next line of code goes inside the function.

Next
N
Next number

Executes the next line of code. But it doesn't go inside the function.

Until
Until line-number

Until line-number ifcondition

Continue running until you reach the specified line number, or function, address, and so on.

Return
return expression

POPs 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 directive.

Info signals
Info handle
Handle signal-nameoption

When the signal is received, the following actions are performed: 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 (do 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

Displays 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 number of stack frames up or down.

Info Frame Addr

Describes the selected stack frame.

Info args
Info All-reg
Info locals
Info catch

Displays 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

List 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 *Array-var@length

Prints the first length entry in the Arrary-var .

p/x var

The integer variable varis printed in hexadecimal.

P/d var

The variable var is printed as a signed integer.

P/u var

The variable var is printed as an unsigned integer.

p/o var

The variable var is printed 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

Print variable varin 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

Similar to print, but void is not printed.

Disassem Addr

The instructions in the specified address are disassembled.

Controlling GDB

Set gdb-option value

Sets the options for GDB.

Set Print array on
Set Print array off
Show Print Array

Prints an array in 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

Formats the output of the C-struct body.

Set Print Union on
Set Print union off
Show Print Union

Print a 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

Take file as a debugging program. If no parameters are specified, discard.

Core file

Take file as a core file. If no parameter is specified, it is discarded.

exec file

Consider file as the executing procedure. If no parameter is specified, it is discarded.

Symbol file

Reads the symbol table from file . If no parameter is specified, it is discarded.

Load file

Dynamically chain into the file and read its symbol table.

Path directory

Add directory directories to the path of the search executable and symbol files.

Start and Stop

Run
R
Run command-line-arguments
Run < infile > outfile

Execute the program from the beginning, and also allow redirection.

Continue
C

Resumes execution until the next breakpoint or observer point.

Continue Number

Continues execution, but ignores the current breakpoint number times. Useful when breakpoints are in a loop.

Kill

Stop program execution.

Quit
Q

Exit the GDB debugger.

GDB Operation Tips

L need to add "-g" to GCC when compiling the executable so that it will add additional debugging information to the generated executable file.

L 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.

L Execute shell commands in GDB: Shell command

The GDB command can be used to complete the TAB key. Press the TAB key two times to see all possible matches.

L GDB command abbreviation: For example, the BRE in the info bre is equivalent to breakpoints.

GDB's operation in Emacs:

Emacs Key

Action

M-x GDB

Switch to GDB mode.

C-h m

Displays the GDB pattern introduction information.

M-s

is equivalent to the step command in GDB.

M-n

is equivalent to the next command in GDB.

M-i

Equivalent to the Stepi command in GDB.

C-c c-f

Equivalent to the finish command in GDB.

M-c

Equivalent to the Continue command in GDB.

M-u

is equivalent to the UP command in GDB.

M-d

is equivalent to the down command in GDB.

This address: Program Life >> gdb Debug Manual

Source: http://www.programlife.net/gdb-manual.html

GDB debug Manual [Go]

Related Article

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.