Use GDB to debug the program

Source: Internet
Author: User

1. debug with GDBProgram

1.1 compileSource code

1.2 How to access and exit GDB

1.3 browsing SourceCode

1.3.1 LIST Command

1.3.2 how to find strings in GDB

1.4 break point breakpoint

1.4.1 set breakpoints

1.4.2 delete a breakpoint

1.4.3 view breakpoint Information

1.5 Watch point monitoring point

1.5.1 set monitoring points

1.5.2 view monitoring variable content

1.6 catchpoints

1.7 run the program

1.8 view Variables

2. clewn

2.1 installation

2.2 simple use

3. ddd

3.1 Installation

3.2 use

4. Download and reference documents

<1>. Use GDB to debug the program

1.1Compile source code

To use GDB to debug a program, you must use the-G option when compiling source code with GCC to include debugging information in the compiled binary file.

Xuqiang @ Ubuntu :~ /GDB $ GCC Debug. C-g-o debug

1.2How to start GDB program debugging and exit GDB Program

After the source file is compiled, if a binary file is generated (debug is used in the preceding example), Type GDB debug in the directory of the debug file to start GDB debugging:

Xuqiang @ Ubuntu :~ /GDB $ GDB debug

Gnu gdb 6.8-debiancopyright (c) 2008 Free Software Foundation, Inc. license gplv3 +: gnu gpl Version 3 or later

To exit GDB, enter the quit command:

(GDB) quit

1.3Browse source code

Now we are able to enter the gdb environment. If we want to set a breakpoint to start debugging, if we cannot know where to set the breakpoint, We need to "browse the source code ", locate the location where you want to set the breakpoint and set the breakpoint. You can use the gdb LIST command or find the line for breakpoint setting by searching strings.

1.3.1 LIST Command

After the LIST command, you can use linenum or function-name. You can also set the number of lines displayed by default for each LIST command.

(GDB) List # Display the listsize row again from the end of the last row

(GDB) List 10 # Display content near 10th rows

(GDB) List main # Display the rows near the main function

(GDB) show listsize# Display the number of lines displayed by the default list command

Number of source lines GDB will list by default is 10.

(GDB) show listsize# Set the number of lines displayed by the default list command

Number of source lines GDB will list by default is 5.

1.3.2How to find strings in GDB

In GDB, you can use the search command to search for strings and display all codes that meet the conditions. Here, you can use regular expressions.

(GDB) Search main

19 int main ()

1.4Breakpoint

1.4.1 set breakpoints

(GDB) Break main # Stop when the main function starts running

Breakpoint 1 at 0x80483d5: file Debug. C, line 22.

(GDB) Break 26 # Set a breakpoint in line 26

Breakpoint 2 at 0x80483dc: file Debug. C, line 26. (GDB) Break Debug. C: 24 # Set the breakpoint Note: breakpoint 2 also set at PC 0x80483dc. breakpoint 3 at 0x80483dc: file Debug. C, line 24 in the 24 lines of the Debug. c file.

1.4.2 delete a breakpoint 

(GDB) Clear main # Deleting breakpoints on the Main Function

Deleted breakpoint 1

(GDB) Clear 26 # Delete a breakpoint of 26 rows

Deleted breakpoint 2 (GDB) Clear Debug. C: Main # Delete the breakpoint on the main function in the debug. c file

 1.4.3 view breakpoint Information

(GDB) info breakpoints # View the breakpoint information num type disp ENB address what3 breakpoint keep y 0x080483dc in main at debug. C: 24

1.5Watch point monitoring point 

1.5.1 set monitoring points 

(GDB) Watch I # Monitor variable I. When the value of I changes, GDB stops running the program.

Hardware watchpoint 6: I 1.5.2 view monitoring variable content

When the monitored Variables change, GDB stops running the program as follows:

Breakpoint 5, main () at Debug. C: 3333 I = 10;

1.6Catchpoints 

Catchpoints is mainly used by applications. when an event occurs, GDB stops running the program.

(GDB) catch fork # If the program calls the fork function, GDB stops running catchpoint 8 (fork)

1.7Run the program 

If you enter the debugging environment of GDB according to the method in 1.2 and set a breakpoint by browsing the source code, how can you run the program?

1. Run the program to be debugged

(GDB) run2. if it stops at the breakpoint after running, use the continue command to continue the program execution (GDB) continue

3. Use step for single-step debugging

(GDB) Step

1.8View Variables 

The print command can be used in GDB to view the value of program variables.

(GDB) print P # P is the struct type

$2 = {x = 0, y = 0} (GDB) print arr # Arr is an int array $3 = {1, 2} (GDB) print # A is int type $4 = 10 (GDB) print * PTR @ 5 # Int * PTR = (int *) malloc (5 * sizeof (INT); $5 = {0, 0, 0, 0, 0} <2>. clewn

2.1 installation

Download clewn, decompress it, and enter the directory. Run the following command to complete the installation.

Xuqiang @ Ubuntu :~ /GDB/clewn-1.15 $./configure

Xuqiang @ Ubuntu :~ /GDB/clewn-1.15 $ makexuqiang @ Ubuntu :~ /GDB/clewn-1.15 $ make install

2.2 simple use

Start clewn and clewn will start gvim to display the source code:

Xuqiang @ Ubuntu :~ /GDB $ clewn-va Debug. c

 

Load a binary file in GDB:

(GDB) file debug

Reading symbols from/home/xuqiang/GDB/debug... done.

Now, you can use Ctrl + B in the gvim window to set the breakpoint:

 

Shift + R starts to run the program, and the program will stop at the first breakpoint. Common shortcut keys:

L = info locals

B = info breakpoint

R = run

C = continue

S = step

Note that the information will be displayed in the gdb window.

<3>. ddd

If the above clewn still feels a little complicated, you can use DDD to debug the program and install it through apt-Get install DDD. Very comfortable to use.

 

<4>. Download and reference documents

/Files/xuqiang/gdb-test-sample.rar

Http://www.delorie.com/gnu/docs/gdb/

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.