Http://blog.chinaunix.net/uid-23193900-id-3184605.html
1. What is program debugging? The purpose of the program debugging is to find out the hidden faults in the program, correct those abnormal instructions, so that the program can work properly. 2. There are several different levels of debugging for a debug category program. The highest level of course is the programmer who visually observes and infers through their own eyes the faulty code and modifies it. The lowest level is theassembly code for debugging。 Because the assembly language code is complex, lengthy and not intuitive. Debugging a program at the assembly level is a laborious thing to do. The most used is probably the source program levelsymbol-level debugging, in this debug mode we are able to use high-level language statements as an executable minimum unit, using the symbols and line numbers of the code appearing in the source program in the debugger to interactively reference the execution of a variable or control statement. 3. Debugging content first of all, to understand what aspects of debugging a program, and then use the tools provided by the debugger for debugging related. For example, data testing, syntax errors, program logic errors. 4. Debugging process (1) Start debugger: Start the GDB debugger, GDB filename//filename is an executable (2) listing the source program: List command, displaying 10 rows at a time. Such as:
Click ( here) to collapse or open
- (gdb) List//Show 10-line program at a time
- (gdb) List <line-number>//Displays 10 lines of content before and after the parameter line, such as List 10, which prints the contents of 5-14
- (GDB) List <line1,line2>//display of contents between parameters
(3) Run the program: Run <ARG1 arg2...> arg1,2 is the parameter passed to the program. You can use the SET command to set parameters passed to the program, and then run directly to
Click ( here) to collapse or open
- Set args argument1 argument2
(4) Setting breakpoints
Click ( here) to collapse or open
- 1. Set breakpoints
- (GDB) Break <line-number> //a line setting
- (GDB) break <function-name> //Set breakpoints before a function
- 2. Sometimes it is necessary to see the state of the program when the variable equals a value in the run, such as the value of the related variable when the For loop executes to the number of times. You can use breakpoints at this point.
- (GDB) Break <line-number> if <conditionalexpression > //expressions conform to C syntax
- (GDB) Break if i==3
- Note: A variable in a conditional expression must be within the scope of a variable in the row where the breakpoint is set.
- 3. If the breakpoint is already set, you can use the condition command to add a condition to the specified breakpoint
- (GDB) Condition break-number if <expression>
- 4. You can set breakpoints in the specified source file
- (GDB) Break <filename: Line-number>
- (GDB) Break <filename: function-name>
- such as: (GDB) Break main. C: 10
- (GDB) Break main. C: Main
- 5. Delete Breakpoints
- (GDB) Info break//Print all breakpoint information
- (GDB) Delete breakpoint <point-number>
- 6. Disable or Enable breakpoints
- (GDB) Break <line-number>
- 7. Clear Breakpoints
- (GDB) Clear
- (gdb) Clear <number>
- 8. Observer point (data Breakpoint)
- (GDB) Watch <condition>
- The difference between breakpoints and observation points: 1. All operation with breakpoint is suitable for watchpoint
- 2. The breakpoint is interrupted when the CPU is taken to an address fetch instruction, and the Observer is interrupted when the CPU reads and writes data to an address.
(5) Viewing runtime data in debugging, you need to know the value of each variable, the running state of the program.
Click ( here) to collapse or open
- 1. Data observation Commands
- (gdb) Print <variable-name>
- In GdB, you can view global variables, static local variables, and local variables that are valid for the current scope. If a local variable conflicts with a global variable and you need to see the value of a global variable, you can use the "::" operator
- (gdb) Print <file:: Variable>
- (gdb) Print <function:: Variable>
- Such as:
- (gdb) Print ' main.c '::A
- 2. Calls to functions in the program
- Specifying the value of a parameter when calling a function
- (gdb) Print func(arg1, arg2,.. . )
- 3. View the value of an expression
- (gdb) Print <expression>
- 4. View the value of an array
- (gdb) Print <array-name>
- Use of the @ operator in GDB:
- (gdb) Print *[email protected]
- The array is the starting address for a contiguous memory space, and Len is the length of the memory that needs to be viewed.
- Array can be an array, or it can be a dynamically allocated heap interval.
- If it is a static array, direct :
- (gdb) Print array name
- You can show all the data.
- 5. View Memory
- The examine command allows the user to view the values in the specified memory
- (gdb) Examine /nfu <address>
- Parameters:
- N: A positive integer representing the memory that displays several units
- F: Display format, binary (U hex) or string (s) or instruction (i)
- u: unit, b= 1 bytes , h= 2 bytes, w= 4 bytes, g= 8 bytes.
- 6. Viewing registers
- (GDB) Info Registers
- (GDB) Info all-registers
- (GDB) Info registers-name
- 7. Automatic display of variables
- Using the display command, you can automatically display variable values when a program steps in or encounters a breakpoint.
- (GDB) Display/fmt expr
- Parameters: FMT, can be hex (x), 16 no character (U)
- decimal (d) Eight (o) binary (t) character formatting (c) floating-point format (f)
- Info display can view the information displayed automatically for display settings.
- Undisplay Delete Auto display. Disable and enable also apply to this command.
5. Use GDB to track data on the stack (data in the stack is a local variableEach function and its variables have a stack frame-frame,The current stack frame is always a frame 0。 All of these frames form a full run stack of programs. You can use the BackTrace command to print information about a stack frame. Command: (GDB) bt if the Func () function is called in the main function and the Func () function is executing, then the stack frame 0 is the func () function. The variable that is printed with the command info Locals is the current stack frame. If you want to see the local variables in the stack frame of the main () function, you can switch using the command "(GDB) frame 1".