GDB Learning Notes

Source: Internet
Author: User
Tags thread stop

Set breakpoints

b Main; At the main () entrance

b 148; In line 148th

viewing breakpoints

Info b

Run the program

R

Single statement execution

N

Continue running the program

C

Print the value of the variable i

P I

Viewing the function stack

Bt

Exit function

Finish

The help command is just an example of the type of GDB command, and if you want to see the commands in the category, you can see all the commands that set a breakpoint by using the helps <class> command, such as breakpoints. You can also go directly to help <command> to see the commands.

tab complements the function name, command name

In gdb, we can pause in the following ways: Breakpoints (breakpoint), Observation points (watchpoint), Snap points (catchpoint), signal (signals), thread stop (threads Stops). If you want to restore the program to run, you can use the C or Continue command.


First, set breakpoints (breakpoint)

We use the break command to set breakpoints. There are several ways to set breakpoints on the front:

Break <function>
Stops when the specified function is entered. The function name can be specified in C + + using the Class::function or functions (type,type) format.

Break <linenum>
Stops at the specified line number.

Break +offset
Break-offset
Stops at the offset line before or after the current line number. Offiset is the natural number.

Break Filename:linenum
Stop at the LineNum line of the source file filename.

Break Filename:function
Stop at the entrance of the function function of the source file filename.

Break *address
Stop at the memory address where the program is running.

Break
The break command stops at the next instruction when there are no arguments.

Break ... if <condition>
... Can be the above parameters, condition indicates the condition, when the condition is established, stop. For example, in an environment-based body, you can set the break if i=100, which means that when I is 100, the program is stopped.

When viewing breakpoints, you can use the info command as follows: (Note: n indicates a breakpoint number)
info breakpoints [n]
Info break [n]

Second, set the observation point (watchpoint)

The observer generally observes whether the value of an expression (the variable is also an expression) has changed, and if so, stops the program immediately. We have the following methods to set the observation point:

Watch <expr>
Set an observer point for the expression (variable) expr. When the value of a quantity expression changes, stop the program immediately.

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.


Third, set the snap point (catchpoint)

    you can set a snap point to catch some events while the program is running. such as: Loading shared libraries (dynamic link libraries) or C + + exceptions. The format of the snap point is:
    
    catch <event>
         when the event occurs, stop the program. The event can be the following:
        1, throw an exception thrown by C + +. (throw keyword)
        2, Catch a C + + caught exception. (Catch is keyword)
        3, when Exec calls the system to call exec. (exec is a keyword, currently this feature is only useful on HP-UX)
        4, when fork calls the system call fork. (Fork is the keyword, currently this feature is only useful under HP-UX)
        5, Vfork calls the system when calling Vfork. (Vfork is a keyword that currently works only on HP-UX)
        6, load, or load <libname> When you load a shared library (dynamic link library). (load is a keyword that currently works only on HP-UX)
        7, unload, or unload <libname> When you uninstall a shared library (dynamic link library). (Unload is the keyword, currently this feature is only useful on HP-UX)

Tcatch <event>
Only one snap point is set once, and when the program stops, the point is automatically deleted.

Four, maintenance stop point

It says how to set the stop point of the program, and the stop point in GDB is the three categories mentioned above. In GDB, you can use the Delete, clear, disable, enable commands to maintain a defined stop point if you feel it is useless.

Clear
Clears all of the defined stop points.

Clear <function>
Clear <filename:function>
Clears all settings on the function's stop point.

Clear <linenum>
Clear <filename:linenum>
Clears all stops that are set on the specified line.

delete [breakpoints] [range ...]
Deletes the specified breakpoint, breakpoints the breakpoint number. If you do not specify a breakpoint number, all breakpoints are deleted. Range represents the range of breakpoint numbers (for example: 3-7). Its shorthand command is D.


A better way than delete is disable stop point, disable the stop point, GDB will not delete, when you need, enable, just like the Recycle Bin.

disable [breakpoints] [range ...]
Disable the stop point specified by the breakpoints as the stop point number. If nothing is specified, all disable stops are indicated. The shorthand command is dis.

Enable [breakpoints] [range ...]
The stop point specified by enable, breakpoints is the stop number.

Enable [breakpoints] once range ...
Enable the stop point specified once, when the program stops, the stop is automatically disable by GDB.

Enable [breakpoints] Delete range ...
Enable the stop point specified once, when the program stops, the stop is automatically deleted by GDB.

View stack Information
—————

When the program is stopped, the first thing you need to do is to see where the program is parked. When your program calls a function, the address of the function, the function arguments, the local variables within the function are pressed into the stack. You can use the GDB command to view the information in the current stack.

Here are some GDB commands to view the function call stack information:

BackTrace
Bt
Prints all the information for the current function call stack. Such as:

(GDB) bt
#0 func (n=250) at Tst.c:6
#1 0x08048524 in Main (Argc=1, argv=0xbffff674) at tst.c:30
#2 0x400409ed in __libc_start_main () from/lib/libc.so.6

The function's call stack information can be seen from the above: __libc_start_main to Main ()---func ()

First, display the source code

GDB can print out the source code of the program being debugged, of course, when the program compiles must add-g parameters, the source program information compiled into the execution file. Otherwise you will not see the source program. When the program stops, GDB reports that the program is parked on the first line of the file. You can use the List command to print the program's source code. Let's take a look at the GDB command that looks at the source code.

List <linenum>
Displays the source program around the LineNum line of the program.

List <function>
Displays the source program for a function named function.

List
Displays the source program after the current line.

List-
Displays the source program in front of the current line.

V. Force call function

Call <expr>
A function can be used in an expression to achieve the purpose of forcing the function to be called. and displays the return value of the function, if the function return value is void, then it is not displayed.

Another similar command can also be done with this function--print,print can be followed by the expression, so you can also use him to invoke the function, print and call is different, if the function returned Void,call is not displayed, print shows the function return value, And the value is stored in the historical data.

First, modify the value of the variable

Modifying the values of variables that are run by the debugger is easy to implement in GDB and is done using GDB's Print command. Such as:

(gdb) Print x=4

X=4 This expression is the syntax for C/s + +, meaning to change the value of variable x to 4, if your current debugging language is Pascal, then you can use Pascal's syntax: X:=4.

GdB Learning Notes

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.