Talk gdb together (third time: GDB breakpoint Debugging)

Source: Internet
Author: User
Tags gdb debugger


Hello, everyone, crossing, last time we were talking about GDB's single-step debugging and how to use GDB for one-step debugging.

In this one, we continue to introduce GDB's debugging capabilities: Breakpoint Debugging. Of course, we'll also show you how to use GDB to debug breakpoints.

Gossip Hugh, words return to the positive. Let's talk gdb together!


Crossing, let's first talk about what a breakpoint is. The so-called breakpoint, is to artificially let the program in a place to stop running, this place is

What we call a breakpoint. GDB provides breakpoint debugging functionality, breakpoints have position breakpoints and conditional breakpoint Two, below we describe how to use GDB

To debug the breakpoint.


positional breakpoints: Use the breakpoint Command break (abbreviated to B) to add a location breakpoint during the debugging process. Example: B n/fun. The n here means

Set a breakpoint on the nth line in the program. Fun is the function name that sets a breakpoint at the beginning of a function fun. When you finish setting breakpoints, the program runs to

Stop at the breakpoint and Use command continue (abbreviated as C) for single-step debugging. Note that the single-step debugging here means that the program will

Runs until a breakpoint is encountered and stops, unlike the single-step debug command mentioned earlier, next is based on the order of execution of the program

Walk step-by-step.


Conditional breakpoint: also uses breakpoint command B, but adds a condition to the command. Example: B n/fun if condition. Here the n represents the line number,

Fun means the function name, condition is the condition, it's written in the same way as in C. The command indicates that if the condition after the if is satisfied

Stop at the beginning of n rows or functions called fun. Note that the condition in the conditional breakpoint is not satisfied when the program does not stop, this and the bit

Breakpoints are not the same, the location breakpoint is unconditionally stopped as long as the program runs to the point where the breakpoint is located. Debugging that is used with conditional breakpoints

The command is run (abbreviated as R), which is a bit like the start command, which causes the program to run from the beginning until the condition in the conditional breakpoint is met.

The order stops at the conditional breakpoint. A comparison of the continue command in a location breakpoint also has this function, except that it does not allow the program to

The head begins to run.


You can use GDB for breakpoint management during the debugging process. GBD provides breakpoints management features: View breakpoints, close breakpoints, start breakpoints

and delete breakpoints. Let's say these features separately.


To View breakpoints: Use the info command to view all breakpoints in debugging, example: I breakpoints.

GDB numbers breakpoints and each breakpoint has an ordinal number.

Close breakpoint: use disable breakpoints N to disable a breakpoint that is numbered N.

Enable breakpoints: use enable N to start a breakpoint with number N.

Delete Breakpoint: Use the command delete breakpoints N to delete the breakpoint with number N.


Crossing, you can set one or more breakpoints in the program according to the needs of debugging, and the breakpoint type is not restricted. When we suspect that the program

Where there is an error, you can set a breakpoint in this place. When the program is running to a breakpoint, it can be stopped, which is more than using a single step

It is much more efficient to invoke the command to run step-by-step. For a debugging method of breakpoints, let's give an example to illustrate:


1 #include <stdio.h> 2    3  void Funa ()  4  {  5          printf ("Funa is running \ n");  6  }  7    8  int main ()   9  {ten          int i =0; one   for          (i=0; i<5; ++i)                  printf ("i=%d", i);          printf ("Break point Test Program"),          Funa ();          return 0;  


1. Open Vim, enter the above code, and save as M.C file.


2. Compile the file with gcc : gcc-g m.c-o m


3. Using the GDB debugger : gdb m


4. Set the location breakpoint : (GDB) b 13, which indicates that a location breakpoint is set on line 13th of the program above. After running the program output the following results:

Breakpoint 2 at 0x804847c:file M.C, line 13.

(GDB) b Funa indicates where a location breakpoint is set in the Funa function. After running the program output the following results:

Breakpoint 1 at 0x8048453:file M.C, line 5.


5. Set conditional breakpoint : (gdb) b if i=3 indicates that a conditional breakpoint is set on line 12th of the above program. The condition at which the breakpoint stops is

I=3. After running the program outputs the following results:

Breakpoint 3 at 0x8048472:file M.C, line 12.


6. View All Breakpoints : (GDB) I breakpoints after running the program output the following results

Num Type Disp Enb Address What

1 breakpoint Keep Y 0x0804847c in main at m.c:13

2 Breakpoint Keep Y 0x08048453 in Funa at M.c:5

3 Breakpoint Keep Y 0x08048472 in main at M.c:12

Stop only if i=3

The number in the first column of the result is the breakpoint number. ENB which column indicates that these breakpoints are in the start state.


7. Start Debugging : (GDB) Start //Start Debugging Command for start, after running the program output the following results:

Temporary breakpoint 4 at 0x804846a:file M.C, line 10.

Starting program:xxx/test/s


Temporary breakpoint 4, main () at M.c:10

Ten int i = 0;

(GDB) n //using single-step debugging, the post-run program outputs the following results:

for (i=0; i<5; ++i)

(GDB) //Press ENTER HERE to continue the single-step debugging, after running the program output the following results:


Breakpoint 1, Main () at M.c:13 //program stops at the breakpoint at line 13

printf ("i=%d", i);

(GDB) C //Here is the continuation of the single-step debugging, after the run program output the following results:

Continuing.


Breakpoint 1, Main () at M.c:13 //The program stops at the breakpoint at line 13 again

printf ("i=%d", i);

(gdb) Disable 1 //Closes the position breakpoint at line 13. The method of starting/deleting breakpoints is the same as closing breakpoints

(GDB) n //using single-Step debugging

for (i=0; i<5; ++i)

(GDB) //Press ENTER HERE to continue the single-step debugging, after running the program output the following results:

printf ("i=%d", i);

(GDB)

for (i=0; i<5; ++i) //single-step test, the program will not stop at 13 lines, but repeatedly execute 12, 13 lines of content.

(GDB)

printf ("i=%d", i);

(GDB)

for (i=0; i<5; ++i)

Use Stop to stop debugging and then experience the conditional breakpoint.

(GDB) b if i=3 //First design a conditional breakpoint in program 12 lines

Breakpoint 1 at 0x8048472:file M.C, line 12.

(GDB) I breakpoints

Num Type Disp Enb Address What

1 breakpoint Keep Y 0x08048472 in main at M.c:12

Stop only if i=3

(GDB) Run //Start debugging, if the condition in the conditional breakpoint is met, then the program stops at the breakpoint where

Starting program:xxx/test/s


Breakpoint 1, Main () at M.c:12 //As we wish, the program stops after the conditions are met.

for (i=0; i<5; ++i)

Crossing, through this comprehensive example, you have mastered how to use GDB to debug breakpoints. But there is one thing to note, here

Breakpoints are set to demonstrate the debugging capabilities of GDB, and in practice, you can set breakpoints based on errors in the program, completing

Actually debugging the program is to find out the error in the program, this is our main purpose.


Crossing, here's what we're going to say about GDB today. Want to know how to funeral, and listen to tell!



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Talk gdb together (third time: GDB breakpoint Debugging)

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.