GDB Common Command usage instructions (i)

Source: Internet
Author: User

This article by the domineering Pineapple Original, reprint please indicate source: http://www.cnblogs.com/xsln/p/gdb_instructions1.html

All articles about GDB index please click here

GDB (GNU Debugger) is a debugging tool under Unix and Unix-like systems. The functionality is extremely powerful and covers almost all the features you need.
GDB is the main help you complete the following four aspects of the function:
1. Start your program, you can follow your custom requirements to run the program at will.
2. Allow the program being debugged to stop at the breakpoint you have specified for the reset.
3. When the program is stopped, you can check what happens in your program at this time, as well as the memory status.
4. Dynamically change the execution environment of your program.

GDB uses the general purpose: Help command is very powerful! Multi-use help! Help always has the information you need. If you don't know how to use Help, enter it in GDB: Help all

First, GDB uses preconditions: Compile-time Add debug information.

gcc/g++ is in the compile-time join-G, other languages please Baidu itself. It is worth noting that the-G score is 4 levels:

    1. -g0 equals no-G. That does not contain any information
    2. -G1 only contains the smallest information, generally only you do not need to debug, only need to backtrace information, and really care about the size of the program, or have other confidential/special needs to use-G1.
    3. –G2 is the default level for GDB and contains the vast majority of the information you need.
    4. –G3 contains some additional information, such as containing macro definition information. When you need to debug a macro definition, use-G3

Second, the most common use of GDB:

1. Debug the program. There are several ways to run your program under GDB:

1) gdb ${Your program} into GDB, enter run (abbreviated R) ${arg1} ${arg2} ... ${argn}

2) gdb--args ${Your program} ${ARG1} ${arg2} ... ${argn} After entering GDB, run running.

3) After GDB enters gdb, enter file ${your program}. Then use set args ${arg1} ${arg2} ... ${argn} set your program parameters, then run.

2. Debug a Running Program:

GDB ${Your Program} ${program PID}

3. Check Core:

GDB ${Your program} ${core file}

Third, GDB common commands:

1. BackTrace: Displays stack information. Abbreviated to BT.

2. Frame x switches to frame x. where x is displayed in the BT command, starting with 0. 0 represents the top of the stack. Abbreviated to F.

3. Up/down x Move the X-frame toward the top/bottom of the stack. When x is not entered, the default is 1.

4. Print x prints x information, x can be a variable, or it can be an object or an array. Abbreviated to P.

5. Print */&x the contents/address of the X.

6. Call function. Note This command requires a program that is running.

7. Set Substitute-path From_path To_path, replace the source file path. When the compiler and the machine code path to run the program are not the same, you need to use this directive to replace the code path, otherwise you can not see the source in GdB.

8. Break X.cpp:n sets a breakpoint on the nth line of x.cpp, and GDB gives the breakpoint number M. The command can be abbreviated to B. The break command is explained in more detail later.

9. Command m sets what to look for when the program executes to the breakpoint m, for example:

Command n

>printf "x is%d\n", X

>c

>end

If there is no parameter n behind the command, then the commands are assigned to the last breakpoint, which actually means that break and command are used together, which is very handy in scripting. The GDB script will explain in more detail later

X/nfu ${addr} Prints the contents of the addr. Addr can be any valid address expression, such as 0x562fb3d, a currently valid pointer variable p, or an address &var of a currently valid variable var. NFU is the format, n represents the length of the view, F represents the format (for example, 16 binary or 10 binary), and U represents units (such as single-byte B, double-word h, four-word w, etc.). Give me a chestnut:

(GDB) X/3xw 0x562fb3d//This directive means: Display address 0x562fb3d in 16 binary format 3 units, each unit four bytes of content. You will get the following values:

0x562fb3d:0x00282ff4 0X080484E0 0x00000000

Continue continue to run the program. After entering debug mode, if you have obtained the information you need or need to continue to run the program to use. can be abbreviated to C

Until execution to the current loop is complete. can be shortened to u

Step, stepping, stepping into the current function. Can be shortened to S

Next stepping through the current function. can be abbreviated to n

Finish execution to the current function return

Set var x=10 changes the value of the current variable x. You can also use this: set {int}0x83040 = 10 to cast the value of the memory address 0x83040 to int and assign a value of 10

. info locals local variables to print the current stack frame

Jump causes the currently executing program to go to a line, or jump to an address. Because only the program jumps and does not change the stack value, jumping out of the function to another place can result in a return error. In addition, people familiar with the Assembly know that when the program runs, a register is used to hold the memory address where the current code resides. So, the jump command changes the value in this register. You can then use set $PC to change the address of the jump execution. Example: Set $pc = 0x485

A. Return: Forces the function to return. You can specify a return value

Iv. Program interrupt mechanism: monitoring points (Watchpoint), Breakpoints (breakpoint), and snap points (Catchpoint):

1. Monitoring points. The monitoring point is to monitor an address in memory, and when the data of that address is changed (or read), the program surrenders control to the debugger. Note the monitoring points are divided into software and hardware modes: The way GDB uses software monitoring points is to test the values of variables while stepping into your program, so the execution of the program slows down. At the same time, the software monitoring point is only valid on the current thread. Fortunately, the 32-bit Intel x86 processor provides 4 special debug registers for easy debugging, and GDB can use these registers to establish hardware monitoring points. GDB always takes precedence over hardware monitoring points, because it does not slow down the execution of the program. However, the number of available (enable) hardware monitoring points is limited. If you set too many hardware monitoring points, GDB may not be able to activate all of them when the program changes from the interrupted state to the state of execution (for example, Continue,until or finish). In addition, the number of active hardware monitoring points can only be known if you are trying to continue executing the program, that is, GDB will not warn you before you run the program, even if you set up too many hardware monitoring points.

There are 3 commands for setting up a watch, watch (write monitoring), Rwatch (read monitoring), and Awatch (reading and writing monitoring). They are used in the same way as the following:

1) (r/a) watch X. X is a variable name. When the value of x changes/is read, the program surrenders control to enter the debugger.

2) (r/a) watch 0xN. n is a valid address. When the content of the address changes/is read, the program surrenders control to enter the debugger.

3) (R/A) Watch * (int *) 0xN. n is a valid address. When the int pointer in the address changes/is read, the program surrenders control to enter the debugger.

4) (r/a) watch-l * (int *) 0xN. n is a valid address. When the int pointer in the address changes/is read, or the content of the address changes/is read, the program surrenders control to enter the debugger.

Note 3 and 4) differ in that when the-l option is added, both the expression itself and the content that the expression points to are monitored.

2. Breakpoints are when a step is executed to the program, the program hand over control to enter the debugger. It is worth noting that break will have some variants: Tbreak,hbreak,thbreak and Rbreak. The Tbreak is the same as the break function, except that the breakpoint you set is automatically deleted after the trigger. Hbreak is a hardware breakpoint. Thbreak is both a temporary hardware breakpoint. Note Hardware breakpoints require hardware support, and some hardware may not support this type of breakpoint. Rbreak is a little bit more special, it will add breakpoints in the full position of the matching regular expression, followed by a detailed explanation. To remove Rbreak, the other break families use the following methods:

1) (t/h) break x.cpp:y. Add a breakpoint in line y of code x.cpp. X.cpp if not specified, the currently executing file is used as the breakpoint file. If the program is not executing, use the source code file that contains the main function as the breakpoint file. If neither X.cpp nor y is specified, the current debugger point is used as the breakpoint.

2) (t/h) break 0xN. Add a breakpoint at address N. n must be a valid snippet (code segment) address.

3) (t/h) break X.cpp:func. Add a breakpoint at the entrance to the Func function of X.cpp. X.cpp can not provide direct use of the break func. Note that because of the presence of overloads (overload), GDB may ask which function you want to add a breakpoint to. You can also avoid the problem by specifying a parameter type, such as break func (int, char *)

4) (t/h) break +/-n. Adds a breakpoint after/before the nth row of the current run.

5) Rbreak REGEXP. A breakpoint is added to all function entries that conform to the regular expression RegExp. For example, Rbeak ex_* indicates that a breakpoint is added at the entrance of all functions that meet the beginning of ex_.

Note that there are 2 optional parameters, thread IDs, and conditions after break. The thread ID refers to the line program number in info threads, not the system-supplied TID. For example, break X.cpp:y 2 if (a==24), which indicates that a breakpoint is added to line y of the x.cpp of line Line 2, and only if the value of a is 24 o'clock, the program will surrender control to enter the debugger.

In addition, breakpoints can be saved with the Save command to make it easier for the user to reset the breakpoint the next time they enter the program debugging.

3. The capture point is when certain events occur, the program surrenders control to enter the debugger. For example catch a exception,assert,signal,fork even syscall. The Tcatch is just like the catch function, except that the set snap point is automatically deleted once it is triggered. Catchpoint will be introduced in detail later.

Five, Tracking Point (tracepoint):

A trace point differs from the above three breakpoints in that it simply tracks the information of the record without interrupting the program's operation. When your program is a realtime program, or interacts with other programs, you may want to use a tracking point to reach the monitoring program without disrupting the program's own behavior. As with breakpoints, the trace point saves some memory information at the track point for the user to consult, such as an array or an object.

In addition, tracepoints can be saved with the Save command to make it easier for users to reset these tracking points the next time they re-enter the program debugging.

Vi. Checkpoint (checkpoint):

GDB can save the state of a program at a point in time, or a program image, and can return to that state later. This is called checkpoint.

Each checkpoint is a copy of the process. So when a bug is hard to reproduce, and you worry about debugging and re-starting from scratch, you can make a checkpoint before estimating to reproduce the bug, so that even if the debug is overdone, you can start with this checkpoint. Instead of restarting the entire program and expecting it to reproduce the bug (it may take a long time!!) )。

But each checkpoint has a unique process ID, which is different from the PID of the original program, so it needs to be considered carefully if the program needs to use PID information.

GDB Common Command usage instructions (i)

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.