This article by the domineering Pineapple Original, reprint please indicate source: http://www.cnblogs.com/xsln/p/5150725.html
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:
- -g0 equals no-G. That does not contain any information
- -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.
- –G2 is the default level for GDB and contains the vast majority of the information you need.
- –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:
- BackTrace: Displays stack information. Abbreviated to BT.
- Frame x switches to X-frames. where x is displayed in the BT command, starting with 0. 0 represents the top of the stack. Abbreviated to F.
- Up/down x go up/down a few frames. When x is not entered, the default is up/down 1 frames.
- Print x Prints x information, x can be a variable, or it can be an object or an array.
- Print */&x the contents/address of the X.
- Call function. Note This command requires a program that is running.
- Set Substitute-path From_path To_path, replacing 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.
- 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.
- 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
- 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 One-step debugging, stepping into the current function. Can be shortened to S
- Next step through the current function. can be abbreviated to n
- Finish executes 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
- Return: Forces the function to return. You can specify a return value
GDB Common Command usage instructions (i)