@ Clear out the GDB debugger in the fighter

Source: Internet
Author: User
Tags stack trace terminates gdb debugger

GDB commands a lot, this article will not be all introduced, will only introduce some of the most commonly used. Before the introduction, let's introduce a very useful feature in GDB: the completion function. It's just like the commands in the shell under Linux. When you enter the first few characters of a command, and then enter the TAB key, the shell will fill this command if the first few characters of the other command are the same. If the first few characters of the other command are the same, you hear a warning and then enter the TAB key, and the shell lists all the previous characters with the same command. The completion function in GDB not only can be filled with gdb command, but also can be filled with parameters.

This article introduces common commands and then combines a specific example to demonstrate how to actually use them. All of the following commands are entered under the shell except the first one to start the GDB command, and the rest are commands within GDB. Most commands within GDB can enter only the first few characters, as long as they do not conflict with other instructions. If quit can be shortened to Q, because the command starts with Q only quit. List can be abbreviated as L, and so on.

1. Start GDB

You can enter GDB to start the GDB program. GDB program has many parameters, there is no need to detail, but one of the most commonly used is to introduce: if you have compiled a program, we assume that the file name is Hello, you want to debug it with GDB, you can enter gdb hello to start gdb and load your program. If you just started GDB, you have to load your program in GDB after booting.

2. Load program = = = File

Inside GDB, loading the program is simple, using the file command. such as file hello. Of course, the path name of the program should be correct.

Exit GDB = = quit

In GDB's command mode, enter quit, and you can exit gdb. You can also enter ' c-d ' to exit gdb.

3. Run the program = = = Run

Once you have loaded the program you are about to debug in gdb, you can do so with the Run command. If your program requires parameters, you can then enter parameters after the Run command, just as you do a command that requires parameters under the shell.

4. Viewing program information = = = Info

The info command is used to view the information of the program, and when you use Help info to view the aid, the info command takes up two screens, with a lot of parameters, but most of them are not used. The most I use the info command is to use it to view breakpoint information.

4. 1 Viewing breakpoint information

Info BR

BR is the abbreviation for breakpoint break, remember GDB's completion function bar. With this command, you can get the details of all the breakpoints you have set. This includes the breakpoint number, type, state, memory address, location of the breakpoint in the source program, and so on.

4. 2 View Current source program

Info source

4. 3 Viewing stack information

Info stack

With this instruction you can see clearly the call hierarchy relationship of the program.

4. 4 Viewing the current parameters

Info args

4.5 Passing parameters
Run [arglist] or set args [arglist]

5. List source program = = = List

5. 1 Listing a function

List FUNCTION

6. Assigning values to variables = = = Set

6.1 Set Variable variable = value

7. Make the interrupt invalid or valid = = = Disable/enable

7.1 Disable breakpoint number can invalidate a breakpoint such as Diable main to invalidate the break main command

7.2 Enable breakpoint number to restore a breakpoint to a valid

8. Delete Breakpoint ===clean/delete

Disable just makes a breakpoint temporarily invalid, the breakpoint still exists in the program. If you want to completely delete a breakpoint, you can use the clear
or delete command.

8.1 Clear: Removes all breakpoints in the program.

8.2 Clear Line number: Deletes the breakpoint for this line.

8.3 Clear function Name: Deletes the breakpoint for the function.

8.4 Delete Breakpoint Number: Deletes the breakpoint with the specified number. If you want to delete more than one breakpoint at a time, each breakpoint number is separated by a space

=========================================================================
When a program goes wrong and generates a core
A quick way to locate error functions
GDB Program name core file name (typically core or core.xxxx)

Keys used by the debugger
R run. The program has not been used before running
C Cuntinue continues to run. Continue running in the back of the operation
Q exit
Kill terminates the Debug program
H Help
<tab> Command Completion function

Step Follow-in function
Next does not follow into the function
b Breakpoint Set breakpoints.
Usage:
The function is interrupted by the name of the letter B
b file Name: line number interrupts the specified line in this file. If it is the current file, then the file name and: number can be
Omitted
See current broken points using info break. Disable breakpoint Disable breakpoint number. Delete the delete breakpoint number.

L list lists lines of code. Column 10 rows at a time. The connection using list will scroll to display. You can also follow the list
Keep up with the file name: line number
Watch observes the value of a variable. The value of this variable is displayed at each interrupt
P Print Prints the value of a variable. Unlike watch, print is displayed only once
Here, by the way, how to change a value. When you are under the command p, for example you use P B,
At this point you will see the value of B, which is the above = 15.
You can also use p to change a value, such as the next command p B = 100 try,
At this point you will find that the value of B becomes 100: $ = 100.



Online transcription
Introduction to basic usage methods
Objective
Error in program code can be divided into several categories, in addition to the most easily debug syntax errors, the compiler will tell you where the error, most of the errors can be categorized as an execution error. GDB's function is to look for execution-time errors. If there is no debugging procedure, we can only add the output variable value instruction in the program to understand the state of the program execution. With the GDB debugging program, we can set to stop the execution of the program anywhere, then you can view the variable value and change the variable, and execute the program line by row.
The process of executing a debugging procedure is usually this:

1. Go to the Debug program and specify the executable file.
2. Specify the directory where the program code resides.
3. Execute the program after setting the breakpoint.
4. After the breakpoint is interrupted, the program can (1) view the execution status of the program, examine the value of the variable or change the value of the variable (2) to step through the program, or at full speed to execute the program to the next breakpoint or until the end of the program.
5. Leave the error-removing procedure.
The following sections are divided into the following:
1. Enter GDB and specify the executable file
2. Specify the directory where the program code is located and the viewer code
3. Setting and clearing of breakpoints
4. Full speed and progressive execution of procedures
5. Viewing and changing variable values
6. View Program Execution Status
7. Read Core file information
Enter GDB and specify the executable file:
1. Enter GDB and read the executable file ("program") to prepare for debugging.
GDB Program
Specify the directory where the program code is located and the viewer code
1. Add directory dir to the directory of the Finder 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 resides.) ):
(GDB) Directory DIR

2. View the program code, the format is:
(gdb) List = Displays the program code of the five elements before and after the execution of the program code, or shows the program code from the last list
(gdb) List function = Displays the program code of the five lines before and after the program starts.
(GDB) List-the previous 10 lines of the last display program code.
Setting and clearing of breakpoints
1. Set a breakpoint (the command is break, abbreviated to (b), the format is:
(gdb) Break filename.c:30 = Stop execution at line 30th of filename.c.
(GDB) Break function = breaks the execution of the program when it enters function.
(gdb) Break filename.c:function = Set breakpoints in the function functions in the program code file FILENAME.C.
(GDB) Break = sets a breakpoint on the next command to be executed.
(GDB) Break ... if cond = is interrupted only when cond is established. Cond must be written in C language.

2. Displays the information for 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. When the breakpoint is cleared, NUM is the breakpoint number displayed in the info break.
(GDB) Delete NUM
Full speed and progressive execution of the program
1. Execute the program at full speed from the beginning of the program until the breakpoint is reached or the program finishes executing.
(GDB) Run

2. After the program is interrupted, the program is executed at full speed to the next breakpoint or until the end of the program (continue instruction can be abbreviated as C).
(GDB) Continue

3. Execute a one-line procedure. If the function is called, the code that contains the function is treated as a line of programs (the next instruction can be abbreviated to n).

(GDB) Next

4. Execute a one-line procedure. If the call function is entered, the function is executed line by row (the step instruction can be abbreviated as s).
(GDB) Step

5. Execute a one-line program, if the program is the last line in the for/while/do Loop loop, it will be executed until the end of the loop after the first line of the program stop (until instruction can be abbreviated to u).
(GDB) until

6. Implement the current procedure until you return to the previous level.
(GDB) Finish
Viewing and changing variable values
1. The print narrative shows the results of the execution of the narrative (the print instruction can be abbreviated to P). Such as
(gdb) Print a = Displays the contents of a variable.
(gdb) print sizeof (a) = shows the length of the A variable.

2. Display narrative that displays the narrative value at each breakpoint or every step of the execution. Such as
(GDB) Display a

3. Change the value of the variable:
(gdb) print (a=10) = = Sets the value of variable A to 10.

View Program Execution Status
1. View the program execution to this point, is the program through which function calls (BackTrace instruction can be abbreviated to BT), that is, to view the function call stack.
(GDB) BackTrace
Read Core file information
1. Read into program and program. Core file to view program variable values and program flow status at Core Dump.
GDB Program Core
Description: The ' core ' file is the core profile generated by the program file when it encounters the core Dump. If you still need the Core file, we recommend that you change the file name to Program.core. After entering the above command, you can read the status of the program Core Dump with the view variable value provided by GDB and the viewer execution state.

Add a bit
(I've been using gdb recently, but sometimes it's better to have my eyes plus printf.)
Make
I'm more using GDB to debug the program directly, not the core

GDB executable file name (enter GDB environment)

Set AGS-? (Set parameters, that is, to the program you want to debug)

In addition, breakpoints can be set by breaking functionname and break linenum
Use info break to view information for the current breakpoint
When you clear a breakpoint with clear, you should correspond to break
If it is a breakpoint set with functionname can only clear functionname
Breakpoints set with LineNum can only be used with the clear linenum

BTW: You can go to www.sunfreeware.com to download gdb

These are some of the lessons I've learned about gdb in recent days, all the places I've made mistakes.
Hope to be with me and other similar GDB rookie of the Help.

If you can use DDD at the beginning
The graphical

In addition, for breakpoints you can use
Delete Breakpoint Number
Delete
Breakpoint numbers can be obtained using info break

I also talk about my experience:
r (parameter of program run) can set parameters at run time
Shell to run the shell environment things, such as Shell make, after the direct R Restart program, you can take the last already added parameters to run, no more knocking parameters
b file Name: line number sometimes very useful, hey
Info b See breakpoint information
Disable breakpoint number pauses a breakpoint
Enable breakpoint number to start a breakpoint
Delete (breakpoint number) Remove a breakpoint, without a breakpoint number, delete all
The X/32UXB pointer displays the 32-byte data that the pointer points to in 16 binary

In the C programming of Linux, we often use GDB to debug the program, the following are some common instructions
1.break FUNCTION
Set a breakpoint on a function. function overloading, it is possible to set breakpoints on several overloaded functions at the same time

Break +offset
Break-offset
Set breakpoints on the first or last lines of the current program to run

Break LineNum
Set breakpoints on lines with line number LineNum

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

Break Filename:function
Set breakpoints on function functions with file name filename
When you have multiple files that may contain the same function name, you must give the file name.

Break *address
Set breakpoints on address addresses, this command promises you to set breakpoints in programs without debugging information

Break
When the break command does not contain any arguments, the break command is in the current execution of the program running stack.
Set a breakpoint on the next instruction. In addition to the bottom of the stack, this command enables the program to
Stops when the function returns. A similar command is finish, but finish does not set a breakpoint. This point in
is useful in looping statements. GDB executes at least one instruction when resuming execution.

Break ... if COND
This command sets a conditional breakpoint, which is specified by cond, and each time GDB executes to this breakpoint
Cond are calculated when the value of cond is nonzero, the program stops at the breakpoint

Ignore Bnum COUNT '
Set the number of times the Bnum breakpoint is ignored as ' COUNT ', that is, the breakpoint Bnum is executed to section count+1
Program stops at times

Tbreak ARGS or abbreviated to TB
Set a breakpoint to be valid only once. Use of args in conjunction with the parameters in break

Hbreak ARGS
Sets a breakpoint that is supported by the hardware. The main purpose of this command is to debug the Eprom/rom program.
Because this command can set breakpoints without changing the code. This can be combined with the Sparclite DSU
Use. When the program accesses certain variables and code, the DSU sets the trap. Attention: You can only use it once
A breakpoint that deletes the original breakpoint when a new breakpoint is set

Thbreak ARGS '
Set hardware support breakpoints with only one effect

Rbreak REGEX
Set a breakpoint on all functions that satisfy the expression regex. This command sets the none on all matching functions.
Conditional breakpoint, which displays all the breakpoint information that is set when this command is completed. This command sets the breakpoint and
The break command is set to nothing different. When you debug a C + + program, this command sets a breakpoint on an overloaded function
Very useful.

info breakpoints [N]
Info break [N]
Info watchpoints [N]
Show all breakpoints and observer settings table with some of the following columns
*breakpoint numbers*----Breakpoint Number
*type*----Breakpoint Type (breakpoint or observation point)
*disposition*---Show the status of a breakpoint
*enabled or disabled*---to enable or not to enable. ' Y ' means enable, ' n ' means not to enable.
*address*----address, breakpoint in your program address (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 for the breakpoint.
The ' Info break ' command with parameter n displays information only for breakpoints specified by N.
This command also displays the operation information for the breakpoint (which was executed several times), which uses the ' Ignore '
command is useful. You can ' ignore ' a breakpoint many times. Use this command to view breakpoints
How many times have been executed. This will find the error faster.

Maint Info Breakpoints
Displays all breakpoints, whether you set it or GDB automatically set it.
Meaning of the breakpoint:
Breakpoint: Breakpoint, normal breakpoint
Watchpoint: General observation Point
LONGJMP: Internal breakpoint, for handling ' longjmp ' calls
LONGJMP Resume: Internal breakpoint, set on the target of ' longjmp ' call
Until: Internal breakpoint used by ' until ' command
Internal breakpoint used by the Finish:finish ' command

2.watch EXPR
Set an observer point for the expression (variable) expr. When the value of a quantity expression changes, stop the program immediately.
This command sets an observer point using expr as an expression. GDB will add an expression to the program and monitor the
Depending on the operation of the program, GDB causes the program to stop when the value of the expression is changed. This can also be used in Sparclite.
The DSU provides a new self-trapping tool. When a program accesses an address or an instruction (this address is sent in a debug
Memory, the DSU will generate self-trapping. For data address DSU supports ' watch ' command, however hardware breakpoints are sent
The register can store only two breakpoint addresses, and the breakpoint must be of the same type. is two ' rwatch ' breakpoints

Or a two ' awatch ' breakpoint.

Rwatch EXPR '
Sets an observer point that is paused when expr is read by the program.

Awatch EXPR '
Sets an observer point where the program is paused when expr is read out and then written.

Info watchpoints
In multi-threaded programs, the Observer's role is limited, and GDB can only observe the values of expressions in one thread
If you are sure that the expression is only accessed by the current thread, then the use of the observer point is valid. GDB cannot focus on a
Changes to the value of an expression by a non-current thread.

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

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

Info watchpoints
Lists all the observed points that are currently set.


3.catch EXCEPTIONS
Use this command to set breakpoints in an activated exception handling handle. Exceptions is the one you want to seize
The exception. You can use the ' info catch ' command to list the active exception handling handles.

GDB does not handle exception handling in the following cases:
* If you use an interactive function, when the function is finished, GDB will return the control as normal
Back to you. If an exception occurs in the call, the function will continue to run until a breakpoint is encountered, a signal
or quit running.
* You cannot manually create an exception (that is, exceptions 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 debug exception handling. If you need to know what's abnormal,
In the exact position, it is best to set a breakpoint before the exception handling handle is called, so that you can check the contents of the stack.
If you set a breakpoint on an exception handler, you don't easily know where and why the exception occurred.
To set breakpoints only before the exception handling handle is awakened, you must understand the implementation details of some languages.

3.cont N
The nth time the breakpoint is passed before the program is stopped running

4.enable Breakpoint Number
Restore a temporary deactivation breakpoint, to recover multiple numbered breakpoints, free space to separate the numbers

5.disable Breakpoint Number
Invalidates the breakpoint, but the breakpoint is still

6.delete Breakpoint number or expression
Delete a breakpoint

7.clear Breakpoint Line number
Clear a Breakpoint

8. View a list of breakpoints
Info break

9.watch counter>15
The program terminates when Counter>15.

10. When the program crashes, Linux generates a core file that can be used
GDB A.out Core
where
View the cause of the crash

11.continue
The recovery program runs until it touches the next breakpoint

12.run
The program starts running until it hits a breakpoint

13.step
Execute a line of code

14.next
Unlike S, he doesn't track inside the code, step-by-step execution of the code

15. Enter directly to execute the previous command

16.print variables
Print a value for a variable

17.display variables
The value of the variable is displayed each time it is run to a breakpoint to observe the change of the variable

18.set variable =
Re-setting the value of a variable in program execution

19.printf "%2.2s\n", (char*) 0x120100fa0
Results printed out: He

20. Set the GDB column width, set the screen to 70 columns
Set width 70

. info args lists the command line arguments that your program accepts
Info registers lists the status of registers
Info Breakpoint lists breakpoints that are set in the program
To get specific information about info, use Help info.

Set
This command is used to set up a running environment for your program (using an expression).
Set prompt $ to subscript character the GDB hint to $.
Set args can specify runtime parameters. (Example: Set args 10 20 30 40 50)
The show args command can view the set run parameters.

. Show
The show command is used to display the state of GDB itself.
Use ' Set ' command to change most of the information displayed by ' Show '
Use the show radix command to display cardinality
Use the ' Set ' command without any parameter variables to display the values of all variables that can be set
There are three variables that cannot be set with the ' Set ' command:
Show version shows GDB's build number
Show copying display copyright information
Show warranty Display warranty information
Share to  RecommendedLike to respond to recommendations like2010-03-13 22:36:15 Small salt (look at my ecstasy!) Hey, you want some salt? )

1.what is GDB?
A debugger that allows you to observe the value of a variable in a running program, and GCC, Emacs, makes up the 3 musketeers that build the entire GNU system.

2.How it work?
The first thing to install, then you can gdb application, into the GDB mode, execute (run) boot routine. (list-display source code)

3. One of the advantages
GDB's function calls are slow, meaning that they are only evaluated when the variable is really needed.
int c = Add (UP)
printf ("%d", c);

Gdb-->run-->print c-1222342. The value of C is not calculated until it is printed. (There may be a switch to control the slow calculation)

4. A precondition
When GCC is combined with-G or-ggdb, it is primarily generated for debugging purposes. (Emacs seems to turn this option off by default)

5. Graphical debugging interface exists

6. Enter Help to view assistance
Help, the category of commands is displayed, Help class name

7.how use?
Debug 3 Program yourself.

8. Common commands (from:http://my.donews.com/tangfl/2006/10/23/gdb-debug-example/)
Break NUM sets a breakpoint on the specified line.
BT displays all the call stack frames. This command can be used to display the order in which functions are called.
Clear removes breakpoints that are set on a specific source file, on a specific line. Its usage is clear filename:num
Continue continue to execute the program being debugged. This command is used when the program stops running because of processing a signal or a breakpoint.
Display EXPR Displays the value of an expression every time the program stops. An expression consists of a variable defined by a program.
The file file loads the specified executable file for debugging.
Help NAME displays assistance information for the specified command.
Info break Displays the list of current breakpoints, including the number of times the breakpoint was reached.
Info files Displays detailed information about the files being debugged.
Info func Displays all the function names.
Info Local Displays the local variable information in the function.
Info prog shows the execution state of the program being debugged.
Info var displays all the global and static variable names.
Kill terminates the program that is being debugged.
The list displays the source code snippet.
Make to run the Make tool without exiting GDB.
Next executes a line of source code forward without stepping into other functions.
Print expr Displays the value of the expression expr.
BackTrace displays the current position in the program and the stack trace that represents how to get to the current location (synonyms: where)
Breakpoint setting a breakpoint in the program
CD changes the current working directory
Clear delete the breakpoint that you just stopped at
When you commands a breakpoint, list the commands that will be executed
Continue continue execution from breakpoint
Delete Deletes a breakpoint or a monitoring point, or it can be used with other commands
Displays variables and expressions when the display program is stopped
Down moves the stack frame so that another function becomes the current function
Frame Select frames for next continue command
Info displays various information related to the program
Jump starts running another point in the source program
Kill abnormally terminates a program that runs under GDB control
List lists the contents of the original file corresponding to the program being executed
Next executes the next source line to execute a function in its entirety
Print displays the value of a variable or an expression
PWD Displays the current working directory
Pype Displays the contents of a data structure, such as a struct or C + + class
Quit Quit GDB
Reverse-search reverse lookup of regular expressions in source files
Run to execute the program
Search searches the source file for regular expressions
Set variable assigning a value to a variable
Signal sending a signal to a running process
Step executes the next source line and enters the next function if necessary
Undisplay Display Command Anti-command, do not show expression
Until end current loop
Up moves the stack frame so that another function becomes the current function
Watch set a monitoring point (i.e. data breakpoint) in the program
Whatis display variable or function type

@ Clear out the GDB debugger in the fighter

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.