Basic usage of GDB

Source: Internet
Author: User
Tags regex expression
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 specifies the line interruption in this file. If it is the current file, the file name and: Can
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. GDB
The 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 GDB
In addition to the program, we can set to stop the program execution anywhere, and 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. Enter GDB and read the executable file (File Name: 'program ') 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 => display the program code of each row before and after the currently executed program code; or display the program code after the last list
(GDB) list function => display the program code of the first five lines at the beginning 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 the execution at line 30th of filename. C.
(GDB) Break function => interrupt program execution when the function is entered.
(GDB) Break filename. C: function => set the breakpoint at the function in the filename. c file of the program code.
(GDB) Break => set a 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 a core dump occurs when the program file is executed. 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 variable value and execution status provided by GDB to read the program.
Core Dump status.

Add a bit
(I was just using GDB recently. I feel pretty good, but sometimes it is better to add printf to my eyes.
Make)
I use GDB to directly debug the program, instead of using core.

GDB Executable File Name (entering the gdb environment)

Set ags -? (Set parameters to pass parameters to the program you want to debug)

In addition, you can use break functionname and break linenum to set breakpoints.
You can use info break to view the information of the current breakpoint.
When you use clear to clear a breakpoint, it must correspond to break.
If you set a breakpoint with functionname, you can only clear functionname.
Only clear linenum can be used for breakpoints set with linenum.

BTW: You can download GDB at www.sunfreeware.com.

The above are some of my experiences with GDB over the past few days, all of which are the places I have made mistakes.
I hope it will be helpful to me and other similar GDB cainiao.

For example, you can use DDD at the beginning.
Graphical

You can also use
Delete breakpoint number
Delete
You can use info break to obtain the breakpoint number.

I also talked about my experiences:
R (program running parameters) can be used to set runtime parameters.
Shell is used to run shell environment things, such as shell make. After it is finished, directly re-start the program by R, you can run the program with the parameters that have been added last time without hitting the parameters.
B file name: the row number is useful sometimes.
Info B view breakpoint Information
Disable breakpoint number pause a breakpoint
Enable breakpoint number to start a breakpoint
Delete (breakpoint number) deletes a breakpoint. All Breakpoints are deleted without the breakpoint number.
The X/32uxb pointer displays 32 bytes of data pointed to by the pointer in hexadecimal notation.

In Linux C Programming, we often use GDB to debug programs. The following are some common commands:
1. Break Function
Set a breakpoint on a function. When a function is overloaded, breakpoints may be set on several overloaded functions at the same time.

Break + offset
Break-offset
Set the breakpoint in the first or last lines of the current program running.

Break linenum
Set a breakpoint on the line with linenum

Break filename: linenum
Set a breakpoint in line linenum of the original file named filename

Break filename: Function
Set a breakpoint on the function named filename
When multiple files contain the same function name, the file name must be provided.

Break * address
Set a breakpoint on the address. This command allows you to set a breakpoint in a program without debugging information.

Break
When the break command does not contain any parameters
Set a breakpoint on the next command. In addition to the stack bottom, this command causes the program
Stops when the function returns. The similar command is finish, but finish does not set the breakpoint. In this case
Loop statements are useful. GDB executes at least one command when resuming execution.

Break... If cond
This command sets a condition breakpoint, which is specified by cond. Each time GDB executes this breakpoint
Cond is calculated. When the cond value is non-zero, the program stops at the breakpoint.

Ignore bnum count'
Set the number of times the bnum breakpoint is ignored to 'Count', that is, the breakpoint bnum is then executed to the Count + 1
Program stopped

Tbreak ARGs or TB
Set the breakpoint to only valid once. The use of argS is the same as that of parameters in break.

Hbreak ARGs
Set a hardware-supported breakpoint. The main purpose of this command is to debug the EPROM/ROM program.
This command can be used to set breakpoints without changing the code. This can be done with sparclite DSU
. When the program accesses certain variables and code, DSU sets a "trap ". Note: You can only use
A breakpoint. When a new breakpoint is set, the original breakpoint is deleted first.

Thbreak ARGs'
Set a breakpoint for only one active hardware

Rbreak RegEx
Set breakpoints on all functions that meet the RegEx expression. This command sets none on all matching functions
Conditional breakpoint. When this command is complete, all the breakpoint information that has been set is displayed. Breakpoint and
The break command is set differently. When debugging a c ++ program, this command sets a breakpoint on the overloaded function.
Very useful.

Info breakpoints [N]
Info break [N]
Info watchpoints [N]
Display the settings table for all breakpoints and observation points, with the following columns
* Breakpoint numbers * ---- breakpoint number
* Type * ---- breakpoint type (breakpoint or observation point)
* Disposition * --- display the breakpoint status
* Enabled or disabled * --- enable or not enable. 'Y' indicates enable, and 'n' indicates not enable.
* Address * ---- address of the breakpoint in your program (memory address)
* What * --- address, the line number of the breakpoint in your program.
If the breakpoint is a conditional breakpoint, this command also displays the conditions required by the breakpoint.
The 'Info break' command with the parameter n only displays the information of the breakpoint specified by N.
This command also displays the running information of the breakpoint (executed several times). This function uses 'ignore'
Command is useful. You can 'ignore' a breakpoint multiple times. Use this command to view breakpoints
Number of executions. This allows you to quickly locate errors.

Maint info breakpoints
Display all breakpoints, whether set or automatically set by GDB.
Breakpoint meaning:
Breakpoint: breakpoint, normal breakpoint
Watchpoint: common observation point
Longjmp: Internal breakpoint used to process 'longjmp' calls
Longjmp resume: Internal breakpoint, Which is set on the target of the 'longjmp' call.
Until: Internal breakpoint used by the 'until 'command
Finish: Finish 'internal breakpoint used by the command

2. Watch expr
Set an observation point for the expression (variable) expr. When the expression value changes, stop the program immediately.
This command uses expr as the expression to set an observation point. GDB will add the expression to the program and monitor
Depending on the program running, GDB stops the program when the expression value is changed. This can also be used in sparclite.
The new self-trap tool provided by DSU. When the program accesses an address or a command (this address is being sent by debugging)
DSU will generate self-traps. DSU supports the 'Watch 'command for the data address, but the hardware breakpoint is sent.
The memory can only store two breakpoint addresses, and the type of the breakpoint must be the same. There are two 'rwatch' breakpoints.

Or two 'awatch 'breakpoints.

Rwatch expr'
Set an observation point. When expr is read by the program, the program is paused.

Awatch expr'
Set an observation point. When expr is read and written, the program is paused.

Info watchpoints
In a multi-threaded program, the function of observation points is very limited. GDB can only observe the value of expressions in one thread.
If you are sure that the expression is only accessed by the current thread, the use of observation points is valid. GDB does not pay attention
Non-current thread changes the expression value.

Rwatch <expr>
When the expression (variable) expr is read, stop the program.

Awatch <expr>
When the value of an expression (variable) is read or written, stop the program.

Info watchpoints
Lists all the observed points currently set.

3. catch exceptions
Use this command to set a breakpoint in an activated Exception Handling handle. Exceptions is the one you want to catch
. You can also use the 'info catch 'command to list active Exception Handling handles.

Exception Handling in GDB cannot be performed in the following cases:
* If you use an interactive function, when the function stops running, GDB will control
Back to you. If an exception occurs in the call, the function will continue to run until it encounters a breakpoint and a signal.
Or exit the operation.
* You cannot manually generate an exception (that is, the exception can only be generated by the program running)
* You cannot manually set an exception handling handle.
Sometimes the 'catch 'command is not necessarily the best way to handle debugging exceptions. If you need to know what the exception is
Set a breakpoint before the exception handling handle is called, so that you can check the stack content.
If you set a breakpoint on an exception handling handle, you do not know the location and cause of the exception easily.
To set a breakpoint only before the exception handling handle is awakened, you must understand the implementation details of some languages.

3. cont n
The program is stopped only when the breakpoint is passed for the nth time.

4. Enable breakpoint number
To restore a breakpoint with multiple numbers, separate them with spaces.

5. Disable breakpoint number
Make the breakpoint invalid, but the breakpoint is still

6. Delete breakpoint number or expression
Delete a breakpoint

7. Clear the line number of the breakpoint
Clear a breakpoint

8. view the breakpoint list
Info break

9. Watch counter> 15
Program terminated when counter> 15

10. When the program crashes, Linux will generate a core file, which can be used
Gdb a. out core
Where
View the cause of the crash

11. Continue
Resume the program running until the next breakpoint is reached

12. Run
The program starts running until a breakpoint is reached.

13. Step
Execute a line of code

14. Next
Unlike s, he does not track the code, and executes the code step by step.

15. Press enter to execute the previous command.

16. Print variable
Print the value of a Variable

17. DISPLAY variable
The value of a variable is displayed every time a breakpoint is run. It is used to observe the changes of a variable.

18. Set variable =
Resetting the value of a variable during Program Execution

19. printf "% 2.2 S/N", (char *) 0x120100fa0
Output the result: He

20. Set the column width of GDB. below is to set the screen to 70 columns.
Set width 70

21. info ARGs list the command line parameters accepted by your program
Info registers list register status
Info breakpoint lists breakpoints set in a program
To obtain specific information about info, use help info.

22. Set
This command is used to set a runtime environment (using an expression) for your program ).
Set prompt $ set the gdb prompt to $.
Set ARGs can Specify runtime parameters. (For example, set ARGs 10 20 30 40 50)
Run the show ARGs command to view the set running parameters.

23. Show
The show command is used to display the status of GDB itself.
The 'set' command can be used to change the vast majority of information displayed by 'show '.
Use the show Radix command to display the base number
Use the 'set' command without any variable to display the values of all variables that can be set.
Three Variables cannot be set using the 'set' command:
Show version: displays the version number of GDB.
Show copying: displays copyright information
Show warranty show guarantee information

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.