C Compilation: Debugging with GDB

Source: Internet
Author: User
Tags one more line

Vamei Source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!

GDB is the GNU Debugger abbreviation. It is a UNIX platform debugger (debugger), can be used for C, C + +, OBJECTIVE-C, Java, Fortran and other programs debug.

In GDB, you can detect possible problems by setting breakpoints (break point) to control the progress of the run, and by looking at the variables and function call conditions at the breakpoint. In many Ides, GDB has a graphical interface.

Here is the main introduction to GDB command line use, and the C program as an example. The computer used for the test is the Mac OS system.

start GdB

The following are two C files. (There is no bug.) We use GDB to see the details of the program running)

One of the programs is test.c, which has the main program main (). The mean () function is defined in the mean.c program and is called in Main ().

TEST.c

#define ARRAYSIZE 4float mean (floatfloat); int Main () {    int  i;     float a=4.5;     float b=5.5;     float rlt=0.0;     float array_a[arraysize]={1.02.03.04.0};     float array_b[arraysize]={4.03.02.01.0};     float array_rlt[arraysize];      for 0 1; i++) {        = mean (Array_a[i], array_b[i]);    }     = mean (a, b);     return 0 ;}

Mean.c

float mean (floatfloat  b) {   return (A + B)/2.0;}

Use GCC to compile the above two programs at the same time. In order to debug using GDB, you must use the- g option (Generate debugging information at compile time):

$GCC-G-O test test.c mean.c

Generates the main executable file.

(if necessary, use:

$chmod +x Test

To increase the user's execution privileges. )

Go to GDB and prepare the Debug program:

$GDB Test

Enter GDB's interactive command line.

Show Programs

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

(GDB) List 9

A program with a total of 10 lines will be shown in the 9th Act Center. We actually compiled two files, and in the case of no description, the default is the main program file test.c:

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};

If you want to see what's in mean.c, you need to explain the file name:

(GDB) List mean.c:1

You can specify the scope of the program lines that you want to list:

(GDB) List 5, 15

The 5-15-line program is displayed.

Displays a function, such as:

(GDB) List mean

Set Breakpoints

We can run the program:

(GDB) Run

The program ends normally.

There is no interesting place to run the program. GDB's main function is to allow the program to pause Midway.

A breakpoint is a place in the execution of a program. In GdB, when the program runs to that location, the program pauses, and we can see the status of the program at this point, such as the value of the variable.

We can set breakpoints on one line of the program, such as:

(GDB) Break 16

A breakpoint is set on line 16th of test.c.

You can view the breakpoints you've set:

(GDB) Info break

Each breakpoint has an identification number. We can delete a breakpoint based on its ordinal number:

(GDB) Delete 1

You can also delete all breakpoints:

(GDB) Delete breakpoints

Viewing Breakpoints

Set breakpoints and run the program with Run, and the program will be paused when running to 16 lines. GDB shows:

Breakpoint 1, Main () at test.c:1616        < ARRAYSIZE-1; i++) {

To view the line where the breakpoint is located:

(GDB) List

To view a variable value at a breakpoint:

(GDB) Print a

(gdb) Print array_a

To 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 at:

(GDB) Break Mean.c:2

There are two a in the stack, one belongs to main () and one belongs to mean (). We can differentiate in function::variable way:

(gdb) Print mean::a

Operation Control

Let the program start with a breakpoint and run one more line:

(GDB) Step

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

(GDB) Continue

Use run to start again.

Help

You can learn more through GDB's help:

(GDB) Help

or more specific commands:

(GDB) Help info

Exit

Use the following command to exit GDB:

(GDB) quit

Debug

Here is a bug in the C program, DEMO.C

#include <stdio.h>struct  node {    intstruct node *pnode; int Main () {    int  i;    Pnode NP=NULL;      for (i=0; i<; i++) {        printf ("Hello world! " );    }    printf ("%d \ n", np->Element);}

The program can compile successfully, but the run will return:

Segmentation fault

You can use GDB to find the problem.

Updata:

Water and Soil Wizard reminder: gdb command can only enter initials, such as C for Continue,b on behalf of BREAK,Q representative quit.

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.