Join talkGDB (third: GDB breakpoint debugging)

Source: Internet
Author: User

Join talkGDB (third: GDB breakpoint debugging)

Hello, everyone. We talked about the single-step debugging function of GDB in the last time and how to use GDB for single-step debugging.

In this session, we will continue to introduce the debugging function of GDB: breakpoint debugging. Of course, we will also introduce how to use GDB for breakpoint debugging.

When you leave the rest of your time, your words will go right. Let's talk GDB together!

Let's talk about what a breakpoint is. The so-called breakpoint is to artificially stop the program running somewhere.

Breakpoint. GDB provides the breakpoint debugging function, where there are two types of breakpoint: Location breakpoint and conditional breakpoint. The following describes how to use GDB

For breakpoint debugging.

Location breakpoint:You can use the breakpoint command break (abbreviated as B) to add a location breakpoint during debugging. Example: B n/fun. N here

Set a breakpoint in line n of the program. Fun is the function name, indicating that a breakpoint is set at the beginning of function fun. After the breakpoint is set, the program runs

The breakpoint will stop, and then you can use the command continue (abbreviated as c) for single-step debugging. Note that the single-step debugging mentioned here refers to

Run until the breakpoint is reached. Unlike the single-step debugging command next mentioned above, next is based on the execution sequence of the program.

Step by step.

Conditional breakpoint:The breakpoint command B is also used, but the conditions are added to the command. Example: B n/fun if condition. n indicates the row number,

Fun indicates the function name, and condition is the condition. It is written in the same way as in C. This command indicates that if the conditions after if meet

Stop at the beginning of n rows or a function named fun. Note that the program will not stop when the conditions in the condition breakpoint are not met.

When a breakpoint is set, the program stops unconditionally as long as the program runs to the position where the breakpoint is located. Debugging used with conditional breakpoints

The command is run (r). It is a bit similar to the start command. It enables the program to run from the beginning until the conditions in the condition breakpoint are met.

The Order stops at the condition breakpoint. Compared with the continue command in the position breakpoint, this function is also available. The difference is that it does not allow the program

Header to start running.

You can use GDB to manage breakpoints during debugging. The breakpoint management functions provided by GBD include viewing breakpoints, closing breakpoints, and starting breakpoints.

And delete breakpoints. These functions are described as follows.

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

GDB numbers the breakpoint. Each breakpoint has a sequence number.

Disable breakpoint:You can use disable breakpoints n to disable breakpoints numbered n.

Enable breakpoint:Enable n can be used to start a breakpoint numbered n.

Delete breakpoint:Run the delete breakpoints n command to delete a breakpoint numbered n.

You can set one or more breakpoints in the program based on debugging needs, and the breakpoint type is not limited. When we suspect that the program

When there is a mistake in a place, you can set a breakpoint in this place. When the program runs at the breakpoint, it can be stopped.

It is more efficient to call commands step by step. The following is an example of how to debug a breakpoint:

 

 1 #include
 
   2    3  void funA()  4  {  5          printf("funA is running \n");  6  }  7    8  int main()   9  { 10          int i =0; 11   12          for(i=0; i<5; ++i) 13                  printf("i= %d",i); 14   15          printf("break point test program"); 16   17          funA(); 18          return 0; 19  }
 

1. Open vim, Enter the above Code and save it as the m. c file.

2. Use GCC to compile the file: Gcc-g m. c-o m

3. Use GDB to debug the program: Gdb m

4. Set the position breakpoint(Gdb) B 13 indicates that a position breakpoint is set in row 13th of the above program. After running, the program outputs the following results:

Breakpoint 2 at 0x804847c: file m. c, line 13.

(Gdb) B funA indicates where to set a position breakpoint in the funA function. After running, the program outputs the following results:

Breakpoint 1 at 0x8048453: file m. c, line 5.

5. Set the condition breakpoint(Gdb) B 12 if I = 3 indicates that a conditional breakpoint is set in row 12th of the above program. When the breakpoint is stopped

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

Breakpoint 3 at 0x8048472: file m. c, line 12.

6. View All Breakpoints(Gdb) after I breakpoints runs, the program outputs 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. Which column of Enb indicates that these breakpoints are in the startup status.

7. Start debugging(Gdb) start // the command to start debugging is start. After running, the program outputs 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

10 int I = 0;

(Gdb) n // use single-step debugging. After running, the program outputs the following results:

12 for (I = 0; I <5; ++ I)

(Gdb) // press Enter here to continue single-step debugging. After running, the program outputs the following results:


Breakpoint 1, main () at m. c: 13 // The program stops at the Breakpoint of line 13.

13 printf ("I = % d", I );

(Gdb) c // here, it indicates to continue single-step debugging. After running, the program outputs the following results:

Continuing.


Breakpoint 1, main () at m. c: 13 // The program stops at the Breakpoint of line 13 again.

13 printf ("I = % d", I );

(Gdb) disable 1 // close the breakpoint at the position of 13 rows. The method for starting or deleting a breakpoint is the same as that for disabling a breakpoint.

(Gdb) n // use single-step debugging

12 for (I = 0; I <5; ++ I)

(Gdb) // press Enter here to continue single-step debugging. After running, the program outputs the following results:

13 printf ("I = % d", I );

(Gdb)

12 for (I = 0; I <5; ++ I) // after one-step debugging, the program will not stop in 13 rows, but will execute 12 or 13 rows of content repeatedly.

(Gdb)

13 printf ("I = % d", I );

(Gdb)

12 for (I = 0; I <5; ++ I)

Use stop to stop debugging and try the conditional breakpoint.

(Gdb) B 12 if I = 3 // first design a conditional breakpoint in 12 rows of the program

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 conditions in the breakpoint are met, where is the program stopped?

Starting program: xxx/test/s


Breakpoint 1, main () at m. c: 12 // as we wish, after the program meets the conditions, it stops.

12 for (I = 0; I <5; ++ I)

Looking at this comprehensive example, we have learned how to use GDB for breakpoint debugging. However, it should be noted that

The breakpoint is set to demonstrate the debugging function of GDB. In actual use, you can set the breakpoint Based on errors in the program.

Debugging the program is to identify errors in the program. This is our main purpose.

Let's talk about GDB today. I want to know what to do later, and listen to the next decomposition!

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.