A summary of Linux debugging C + + programs

Source: Internet
Author: User

One: Cause

(1) Perhaps we are very familiar with Windows VC6.0 and codeblocks Debugging Tools-Interface debugging, but have you ever thought about each of your keys or shortcuts behind the instructions is what, let us go into the Linux GCC dynamic debugging tool GDB

(2) program debugging is nothing more than: Debug (gcc-g-o target source), set breakpoints (b N), observe the face (info locals), observe a specific value (print expr); execute (start); Execute the next line (next); Enter the child function (step), execute to the end of the current function (finish), view source code (list), etc.

(3) GCC process: C program for the editing of Vim, the compilation and operation of the program to GCC, program debugging to GDB (kdbg is a visual debugging tool)

Second: Examples of interpretation

(1) The commissioning step is divided into two steps

1) Compile-time must add-g to generate debug information, so that debug information is included in the executable file exe--command line:

GCC-G-o Target source

2) load the executable exe into the DEBUG environment:

GDB Target

3) gdb to debug command prompt, you can type debug command

(2) Debugging common commands

1) Basic Debug commands (important)

List line number: Lists the source code from the first few lines of the product
List function Name: Lists the source code for a function
Start: Begin executing the program, stop waiting for the command in front of the first line of the main function
Next (N): Executes the next column of statements
Step (s): Executes the next line of statements and enters into the function if there is a function call
Breaktrace (or BT): View all levels of function calls and parameters
FRAME (f) Framing number: Select Stack frame
info (i) Locals: View the value of the current stack frame local variable
Finish: Perform to the current function to return and then get down and wait for the command

Print (p): Prints the value of an expression that modifies the value of a variable or invokes a function
Set VAR: Modifying the value of a variable
Quit: Exit GDB

2) Advanced Debug commands

Break (b) Line number: Set a breakpoint on a line

Break function Name: Set a breakpoint at the beginning of a function
Break...if ... : Set Conditional breakpoints
Continue (or C): Starting from the current position rather than stepping through the program

Span style= "COLOR: #ff0000" >delete breakpoints: Delete all breakpoints
delete breakpoints N: Delete breakpoint with ordinal n
disable Breakpoints: Disabling breakpoints
enable breakpoints: enabling Breakpoints
info (or i) Breakpoints: see which breakpoints are currently set
Span style= "White-space:pre" >
run (or R): From the beginning of a continuous rather than a step program
display variable name: trace to see a variable, each time it stops to display its value
Undisplay: Cancels the tracking of those variables that were previously set

  3) format input view variable print (p)

Output format:
In general, GDB outputs the value of a variable based on the type of the variable. But you can also customize the format of GDB's output.
For example, you want to output the hexadecimal of an integer, or binary to see the bit in the integer variable. To
To do so, you can use GDB's data display format:
x Displays the variable in hexadecimal format.
D Displays the variable in decimal format.
u displays unsigned integers in hexadecimal format.
o Displays the variable in octal format.
T displays the variable in binary format.
A displays the variable in hexadecimal format.
C Displays the variable in character format.
F Displays the variable in floating-point number format.

A summary of Linux debugging C + + programs

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.