Use of the GDB debugger

Source: Internet
Author: User
Tags gdb debugger

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

    1. (gdb) List//Show 10-line program at a time
    2. (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
    3. (GDB) List <line1,line2>//display of contents between parameters
(3) Run the program: Run &LT;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

    1. Set args argument1 argument2

(4) Setting breakpoints

Click ( here) to collapse or open

  1. 1. Set breakpoints
  2. (GDB) Break <line-number> //a line setting
  3. (GDB) break <function-name> //Set breakpoints before a function
  4. 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.
  5. (GDB) Break <line-number> if <conditionalexpression > //expressions conform to C syntax
  6. (GDB) Break if i==3
  7. Note: A variable in a conditional expression must be within the scope of a variable in the row where the breakpoint is set.
  8. 3. If the breakpoint is already set, you can use the condition command to add a condition to the specified breakpoint
  9. (GDB) Condition break-number if <expression>
  10. 4. You can set breakpoints in the specified source file
  11. (GDB) Break <filename: Line-number>
  12. (GDB) Break <filename: function-name>
  13. such as: (GDB) Break main. C: 10
  14. (GDB) Break main. C: Main
  15. 5. Delete Breakpoints
  16. (GDB) Info break//Print all breakpoint information
  17. (GDB) Delete breakpoint <point-number>
  18. 6. Disable or Enable breakpoints
  19. (GDB) Break <line-number>
  20. 7. Clear Breakpoints
  21. (GDB) Clear
  22. (gdb) Clear <number>
  23. 8. Observer point (data Breakpoint)
  24. (GDB) Watch <condition>
  25. The difference between breakpoints and observation points: 1. All operation with breakpoint is suitable for watchpoint
  26. 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. 1. Data observation Commands
  2. (gdb) Print <variable-name>
  3. 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
  4. (gdb) Print <file:: Variable>
  5. (gdb) Print <function:: Variable>
  6. Such as:
  7. (gdb) Print ' main.c '::A
  8. 2. Calls to functions in the program
  9. Specifying the value of a parameter when calling a function
  10. (gdb) Print func(arg1, arg2,.. . )
  11. 3. View the value of an expression
  12. (gdb) Print <expression>
  13. 4. View the value of an array
  14. (gdb) Print <array-name>
  15. Use of the @ operator in GDB:
  16. (gdb) Print *[email protected]
  17. The array is the starting address for a contiguous memory space, and Len is the length of the memory that needs to be viewed.
  18. Array can be an array, or it can be a dynamically allocated heap interval.
  19. If it is a static array, direct :
  20. (gdb) Print array name
  21. You can show all the data.
  22. 5. View Memory
  23. The examine command allows the user to view the values in the specified memory
  24. (gdb) Examine /nfu <address>
  25. Parameters:
  26. N: A positive integer representing the memory that displays several units
  27. F: Display format, binary (U hex) or string (s) or instruction (i)
  28. u: unit, b= 1 bytes , h= 2 bytes, w= 4 bytes, g= 8 bytes.
  29. 6. Viewing registers
  30. (GDB) Info Registers
  31. (GDB) Info all-registers
  32. (GDB) Info registers-name
  33. 7. Automatic display of variables
  34. Using the display command, you can automatically display variable values when a program steps in or encounters a breakpoint.
  35. (GDB) Display/fmt expr
  36. Parameters: FMT, can be hex (x), 16 no character (U)
  37. decimal (d) Eight (o) binary (t) character formatting (c) floating-point format (f)
  38. Info display can view the information displayed automatically for display settings.
  39. 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".

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.