Gdb
The GCC compilation option plus –g adds debug information to the generated target code.
Gcc–g–o Hello hello.c
Redhat 6.3 Default is not to install GDB, if installed from the system CD (iOS image file), unzip it in the Packages folder to find gdb-7.2-56.el6.i686.rpm, and then execute the following command:
RPM-IVH gdb-7.2-56.el6.i686.rpm (because different numbers of versions may differ)
1. Start GDB
directive: GDB program name [Corefile]
Corefile is optional, but it enhances GDB's ability to debug
Linux does not generate the Corefile file by default, so you need to add the same code in the. bashrc file Ulimit–c Unlimited the. bashrc file is in the host directory (after modifying the play. bashrc file, remember to execute: BASHRC let the changes take effect)
Corefile file saves the program's memory information when the program is running in error (GDB debugging is run-time error , code error is the compiler)
Write a wrong procedure first:
To compile it
compiler does not have an error
And look at the time it runs.
The program makes an error by manipulating a null pointer, and produces a corefile file in the current directory
Now combine corefile with GDB for debugging
Plus corefile can know the wrong information, and where the code where the error occurred, without adding corefile, such as
There is no error message, the program requires you to step-by-step debugging and then find the error.
2. GDB Common Command Run
Let the program run directly, knowing that a breakpoint or error has been encountered
GDB can see the memory information of the program error, 0X000000004004D4 represents the memory address.
where
What's wrong with the test () function? Where command, displays the execution function tree that caused the segment error
This is the order of the function calls, and number No. 0 represents the current function. Number 1th means that the main function calls the test function, so test is number 0th, and main is the order of the call function is saved by the stack.
List
List[m,n], m/n is the first M-line and the last N-row to display where the error code appears. Show nearby 10 rows without parameters
Whatis
What type of query variable is a rule with print
Print
one of the most useful features of GDB is that it can display the values of any expression, variable in the program being debugged
Print variable \ Expression
print ' file ':: variable \ expression, ' is required to let gdb know that it refers to a file name
Print FuncName:: variable, expression
PType
PType tells you the structure of the definition, note is the structure of the body
Break
The break command sets a breakpoint:
Breaklinenum
Breakfuncname
Breakfilename::linenum
Breakfilename:funcname
Well, this can be translated and understood.
Infobreak Viewing breakpoint information
Continue run to the next breakpoint
Delete Deletes all breakpoints, delete num Deletes the breakpoint of the specified label
Set Variable
setvariable Changing the value of a variable
SetVariable varname = value
Single Step Commissioning Step Next
Step only runs the line code compared to continue step, and step enters the function when it encounters the function.
Next is equal to step but does not enter the function, but runs to the next line
Return stops executing the current function, saying that value is returned to the caller, and step jumps out with return when running inside the function
Linux GDB Learning