Using the GDB Debug program

Source: Internet
Author: User

GDB (GNU Debuger) is a powerful debugger in Linux. GDB allows you to view the internal structure of the program, print variable values, set breakpoints, single-step debugging source code, run-time modification of variable values, path tracking, thread switching, display assembly code and so on.

Compile

To debug with GDB, you need to include the-G option when compiling the program. When this option is set, GCC will add the debug information "wedge" to the program, and GDB can use these wedges to interact with the program.

    Gcc-g Myfile.c-o Myfile_debug
Debugging
  1. Loading program
    You can use the GDB program method, or start with the file program after GDB starts.
  2. Set input parameters and display
    the Set args command sets the parameters sent to the program, and the show args command displays its default parameters.
  3. Print Code Content
    list Line1,line2 Prints the code, and does not enter parameters to print from the current line. Printing data with Print
  4. Breakpoint
    Set Breakpoint: 1. Break line number; 2. break function name; 3. Break line number or function if condition: This is a conditional breakpoint setting command, and if the condition is true, the program stops when it reaches the specified line or function. such as B if i==2
    Show Breakpoint Information: info break
    Deletes the specified breakpoint: The Delete breakpoint breakpoint number. If no number argument is taken, all breakpoints are deleted
    Forbidden Breakpoint: Disable Breakpoint breakpoint number
    Allow breakpoints: Enable breakpoint breakpoint number
    Clear breakpoint: Clear source code line number. Disposable Clear Direct
  5. Run the program
    run the prepared program using the Run command, followed by the parameters passed to the program. If you use the Run command without parameters, GDB uses the parameters of the previous Run command again.
  6. Detecting variable types
    Print the type of an array or variable: Whatis variable name
    View a detailed definition of the structure with the PTYPE variable name
  7. Single-Step debugging
    a single-step trial of next or Step commands will enter the inside of the function body. If you have entered inside the function and want to exit the function to return to the calling function, you can use the command finish.
  8. Setting the detection point
    the command display can show the value of a variable, and the value of the set variable is displayed (scoped) at the end or when a breakpoint is encountered.
  9. Call path (stack information)
    The BackTrace command can print the function's call path, providing forward tracking functionality. Abbreviation BT
  10. Multithreading thread
    info thread lists the thread number in the current process and enters the thread that needs to be debugged with the thread ID
  11. Compilation disassemble
    disassemble function name, printing the assembly code of the specified function

Common commands

Using the GDB Debug program

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.