C Compilation: Use GDB for debugging

Source: Internet
Author: User
Tags one more line

Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!

 

GDB isThe GNU Debugger. It is a UNIX platform debugger (debugger) that can be used for C, C ++, objective-C, Java, Fortran, etc.ProgramDebug.

In GDB, you can setBreakpoint (break point)To control the progress of the program running, and view the variables and function call status at the breakpoint to detect possible problems. In many ides, GDB has a graphical interface.

Here we mainly introduce the command line usage of GDB, and take the C program as an example. The computer used for testing is the Mac OS system.

 

Start GDB

There are two C files below. (No bugs. We use GDB to view the program running details)

One program isTest. cWith the main programMain ().Mean. cThe program definesMean ()Function in main.

Test. c

 # Define Arraysize 4 Float Mean ( Float , Float  );  Int Main (){  Int  I;  Float A = 4.5  ;  Float B = 5.5  ;  Float Rlt = 0.0  ;  Float Array_a [arraysize] = { 1.0 , 2.0 ,3.0 , 4.0  };  Float Array_ B [arraysize] = { 4.0 , 3.0 , 2.0 , 1.0  };  Float  Array_rlt [arraysize];  For (I = 0 ; I <arraysize- 1 ; I ++) {Array_rlt [I] = Mean (array_a [I], array_ B [I]);} rlt = Mean (A, B );  Return   0  ;} 

 

Mean. c

 
FloatMean (FloatA,FloatB ){Return(A + B )/2.0;}

 

Use GCC to compile the above two programs at the same time. To use GDB for debugging, you must use-GOption (debugging information is generated during compilation ):

$ Gcc-g-o Test test. C mean. c

Generate the main executable file.

(If necessary, use:

$ Chmod + X test

.)

 

Go to GDB and prepare the debugging program:

$ GDB Test

Enter the interactive command line of GDB.

 

Display program

We can directly display a program line, such as viewing the program line 9th:

(GDB) List 9

Programs with 10 rows in total are displayed as 9th behavior centers. We actually compiled two files. Without instructions, test. c is the main program file by default:

 
4 5 Int main () 6 {7 int I; 8 float a = 4.5; 9 float B = 5.5; 10 float rlt = 0.0; 11 12 float array_a [arraysize] = {1.0, 2.0, 3.0, 4.0}; 13 float array_ B [arraysize] = {4.0, 3.0, 2.0, 1.0 };

 

To view the content in mean. C, you need to describe the file name:

(GDB) List mean. C: 1

 

You can specify the range of program lines to be listed:

(GDB) List 5, 15

That is, programs with 5-15 rows displayed.

 

Display a function, such:

(GDB) List mean

 

Set breakpoints

We can run the program:

(GDB) Run

The program ends normally.

 

There is nothing interesting about running the program. The main function of GDB is to pause the program midway through.

Breakpoint (break point)Is a position in program execution. In GDB, when the program runs to this position, the program will be suspended. We can view the program status at this time, such as the value of the variable.

 

We can set a breakpoint in a line of the program, for example:

(GDB) break 16

The breakpoint will be set in row 16th of test. C.

 

You can view the breakpoint you set:

(GDB) info break

Each breakpoint has an identification number. You can delete a breakpoint according to the sequence number:

(GDB) delete 1

You can also delete all breakpoints:

(GDB) delete breakpoints

 

View breakpoints

Set the breakpoint and run the program. The program will be paused when it runs to 16 rows. GDB display:

 
Breakpoint 1, main () at test. C: 1616 for (I = 0; I<Arraysize-1; I ++ ){

 

View the row where the breakpoint is located:

(GDB) List

 

View the value of a variable at the breakpoint:

(GDB) print

(GDB) print array_a

 

View all local variables:

(GDB) info local

 

View the stack status at this time (reflecting the function call, see Linux from program to process ):

(GDB) info Stack

 

You can change the value of a variable.

(GDB) set var A = 0.0

(GDB) set var array_a = {0.0, 0.0, 1.0, 1.0}

When the program continues to run, the changed value is used.

 

If we set the breakpoint in:

(GDB) Break Mean. C: 2

At this time, there are two A in the stack, one belongs to main () and the other belongs to mean (). We can use function: variable to differentiate:

(GDB) print mean:

 

Operation Control

Let the program start from the breakpoint and run one more line:

(GDB) Step

You can also use the following command to resume running from the breakpoint until the next breakpoint:

(GDB) continue

UseRunStart running again.

 

Help

You can learn more with the help of GDB:

(GDB) Help

Or a more specific command:

(GDB) Help info

 

Exit

Run the following command to exit GDB:

(GDB) quit

 

Debug

Below is a C program with a bug, demo. c

# Include <stdio. h> Struct  Node {  Int  Element;}; typedef  Struct Node *Pnode;  Int  Main (){  Int  I; pnode NP = NULL;  For (I = 0 ; I < 10 ; I ++ ) {Printf (  "  Hello world!  "  );} Printf (  " % D \ n  " , NP-> Element );} 

The program can be compiled successfully, but the following code is returned:

Segmentation fault

You can use GDB to find the problem.

 

Updata:

The soil and water genie reminds you that the gdb command can only enter the abbreviation of the first letter. For example, c Indicates continue, B Indicates break, and Q indicates quit.

Related Article

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.