GDB usage Summary

Source: Internet
Author: User

Address: http://hi.baidu.com/%CF%E6%CB% AE %C0%CB%D7%D3/blog/item/ce5d77171b24a8024b90a72f.html
GDB usage Summary

1. GDB exe
Make the EXEProgramRun in the debug environment
2. Break functiona
Set the endpoint in the functiona Function
3. Run
Execute the program from the main entry to the breakpoint functiona
4. n
Next, one-step execution, equivalent to the debugging command step over in VC
5. s
Step into: Go to the sub-function to check the sub-function execution.

6. BT
Backtrace to view the stack

7. P Variant
Print the variable variant Value
8. L
LIST command to view the context of the current line. 10 lines are displayed by default.

9. P variant = correct value
If you find that the value of variant is incorrect at this time, we can set a correct value for variant (correct value)
Then, run the following command:

10. c
Continue execution. The execution can be continued based on the changed value. Equivalent to F5 in VC

11. Quit or Ctrl + c
Exit GDB

In the command line of GDB, you can use file exeprogram to load the file to debug.
GDB-silent indicates that the gdb copyright information is not displayed or GDB-Q (quiet)
GDB-H displays GDB help

12 about help
GDB> help

Apropos ARGs // find all gdb commands and the expressions containing ARGs in its documents
Complete I // list all gdb commands starting with I
The HELP command for a command, such as help info
Display info usage. You can view ARGs, breakpoints, and stack in info ......
The show command only displays information about GDB, such as show version.

13 break
Break function
set the endpoint of a function
Break linenum
set a breakpoint at a specific line
Break + offset
-offset
break * address sets a breakpoint on an address
14 watch
watch expr
view an expression
rwatch expr
View an expression, when reading this expression, set the breakpoint

15 view Source Code
List linenum source code before and after linenum display
List + list the code lines behind the current row
list-list the code lines before the current row
list function
set listsize count
set the number of lines to display the Code
show listsize
display the number of lines to print the Code
list first, last
display source code lines from first to last

16 edit source code
Edit edit current row
Edit num
Edit function: edit a file containing the Function Definition
Edit filename: Function
Set Editor
Editor =/usr/bin/VI
Export Editor
GDB ....
Reprinted:
Use of GDB

When a program error occurs and a core is generated
How to quickly locate error functions
GDB program name Core File Name (usually core, or core. xxxx)

Key used by the debugging program
R Run
C cuntinue continues to run. Continue running after interruption
Q exit
Kill the program to terminate debugging
H help
<Tab>; command completion Function

Step and Input Functions
Next does not follow the Input Function
B breakpoint.
Usage:
Function B is interrupted.
B. File Name: the row number indicates that the row is interrupted in this file. If it is the current file, the file name and the number can be
Omitted
Use info break to check the current number of broken points. Disable resumable checkpoint disable. Delete the delete checkpoint.

L list the code lines. 10 rows in one column. The connection using list will be displayed in a scrolling manner. You can also follow the list
Keep up with file name: row number
Watch observe the value of a variable. The value of this variable is displayed during each interruption.
P Print prints the value of a variable. Unlike watch, print is only displayed once.
Here, by the way, we will talk about how to change a value. When you run the p command, for example, if you use p B,
At this time, you will see the value of B, that is, the above $1 = 15.
You can also use P to change a value. For example, run the following command p B = 100,
At this time, you will find that the value of B is 100: $1 = 100.

Online transcription
Basic usage
Preface
Errors in program code can be divided into several classes. Apart from the most common syntax errors, the Compilation Program will tell you where the errors are. Most errors can be classified as execution errors. The GDB function is to find errors during execution. If there is no debugging program, we can only add the command of output variable value to the program to understand the program execution status. With the gdb debugging program, we can set to stop the program execution anywhere, then we can view variable values and change variables at will, and execute the program line by line.
The process of executing a debugging program is usually as follows:

1. Enter the debugging program and specify the executable file.
2. Specify the directory where the program code is located.
3. Set the breakpoint and run the program.
4. after the program is interrupted, you can (1) view the program execution status; view the variable value or change the variable value (2) Gradually execute the program, or execute the program at full speed until the next breakpoint or until the program ends.
5. Leave the debugging program.
The following items are described as follows:
1. Enter GDB and specify the executable file
2. Specify the directory where the program code is located and view the program code
3. Set and clear breakpoints
4. Full speed and step-by-step execution
5. view and modify the value
6. View program execution status
7. Read Core File Information
Enter GDB and the specified executable file:
1. Go to GDB and read the executable file (named 'program '), and prepare for debugging.
GDB Program
Specify the directory where the program code is located and view the program code
1. Add the directory dir to the directory list for receiving and searching program code (If your program code and executable file are in the same directory, you do not need to specify the directory where the program code is located .) :
(GDB) Directory dir

2. view the program code in the following format:
(GDB) List =>; displays the code of each row before and after the currently executed program code; or displays the code of the program after the last list
(GDB) list function =>; displays the code of the five lines before and after the start of the program.
(GDB) List-=>; the first 10 lines of the program code are displayed last time.
Breakpoint setting and clearing
1. Set the breakpoint (the command is break, which can be abbreviated as (B). The format is as follows:
(GDB) Break filename. C: 30 =>; stop running at row 30th of filename. C.
(GDB) Break function =>; interrupts program execution when the function is enabled.
(GDB) Break filename. C: function =>; set the breakpoint at the function in the filename. c file of the program code.
(GDB) Break =>; set the breakpoint for the next command to be executed.
(GDB) Break... If cond =>; it is interrupted only when cond is established. Cond must be written in C syntax.

2. display the information of each breakpoint.
(GDB) info break

3. Clear the breakpoint (command is clear) in the same format as break. For example:
(GDB) Clear filename. C: 30

4. Clear the breakpoint. Num is the breakpoint number displayed in info break.
(GDB) delete num
Full speed and step-by-step program execution
1. Execute the program at full speed starting with the program until the breakpoint or program execution is completed.
(GDB) Run

2. After the program is interrupted, the program is executed at full speed until the next breakpoint or the program ends (the continue command can be abbreviated as C ).
(GDB) continue

3. execute a program. If a function is called, the code containing the function program is considered as a program (the next command can be abbreviated as N ).
(GDB) Next

4. execute a program. If you call a function, the function is executed row by row (the step command can be abbreviated as S ).
(GDB) Step

5. execute a program line. If the program is in the last line of the For/while/do loop, the program is executed until the end of the loop and stops (the until command can be abbreviated as U ).
(GDB)

6. Execute the current program until you return to the previous program.
(GDB) Finish
View and modify values
1. Print Description: displays the execution result of this description (the print command can be abbreviated as P ). For example
(GDB) print a =>; display the content of the variable.
(GDB) print sizeof (A) =>; display the length of variable.

2. Display Description: displays the description value at each breakpoint or when each step is executed. For example
(GDB) display

3. Change the value:
(GDB) print (A = 10) =>; set the value of variable A to 10.

View program execution status
1. Check which function call programs (the backtrace command can be abbreviated as BT) are executed by the program, that is, view the function call stack.
(GDB) backtrace
Read Core File Information
1. Read the program and program. core files to view the program variable value and program process status during core dump.
GDB program Core
Note: The 'core' file is the name of the core worker generated when core dump is encountered after being executed by the program file. If you still need this core file, we recommend that you change the file name to program. Core. After entering the preceding command, you can use the view variable value provided by GDB and view the program execution status to read the state of the program core dump.

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.