Stop and look around the program

Source: Internet
Author: User

Go from http://blog.csdn.net/todd911/article/details/280913891. Breakpoint List

Each breakpoint that is created, including power outages, monitoring points, and capture points, is identified as a unique integer identifier starting at 1. This identifier is used to perform the various

Operation. The debugger also contains a way to list all breakpoints and their properties.

Debug the following code: (Code 1)

[CPP]View Plaincopy
  1. #include <stdio.h>
  2. void display (int i)
  3. {
  4. i = i + 1;
  5. printf ("i =%d\n", i);
  6. }
  7. int main (void)
  8. {
  9. int i = 1;
  10. Display (i);
  11. return 0;
  12. }

Set breakpoints--show list of breakpoints--delete breakpoints--Displays a list of deleted breakpoints:

(GDB) Break main
Breakpoint 1 at 0x80483d4:file A.C, line 11.
(GDB) Break display
Breakpoint 2 at 0x80483aa:file A.C, line 5.
(GDB) Info breakpoints
Num Type Disp Enb Address What
1 breakpoint Keep Y 0x080483d4 in main at a.c:11
2 Breakpoint Keep y 0x080483aa in display at A.c:5
(GDB) Delete 1
(GDB) Info breakpoints
Num Type Disp Enb Address What
2 Breakpoint Keep y 0x080483aa in display at A.c:5
(GDB)

Info Breakpoints Command shorthand for I B

2.Set BreakpointsThere are many ways to specify breakpoints in GdB, and here are some common methods. A persistent breakpoint uses the Break command (abbreviated as B) to debug with code 1: Set a breakpoint at the entrance to the function: (GDB) breaks main
Breakpoint 1 at 0x80483d4:file A.C, line 11. Set a breakpoint on a line in the current active source code file: (GDB) Break 11
Breakpoint 3 at 0x80483d4:file A.C, line 11. Set breakpoints at a line of a file: (GDB) Break a.c:11
Note:breakpoint 3 also set at PC 0x80483d4.
Breakpoint 4 at 0x80483d4:file A.C, line 11. To set a breakpoint at the entrance of a function at a file: (GDB) Break A.c:main
Breakpoint 6 at 0x80483d4:file A.C, line 11. When a breakpoint is set, the validity of the breakpoint continues until it is deleted, when the GDB is disabled or exited, and the breakpoint is automatically deleted when the temporary breakpoint is first arrived. Temporary breakpoints are set using the Tbreak command (abbreviated TB). It is the same as the use of break. 2. Persistence of breakpoints if we do not exit gdb when we modify and recompile the code, the next time that GDB executes the Run command, GDB senses that the code has been modified and reloads the new version: The following uses Code 1 debugging: The A window executes as follows: (GDB) b main
Breakpoint 1 at 0x80483d4:file A.C, line 11. (GDB)

The b window executes as follows: Modify i = i + 1 in the display function, I = i + 2, and recompile back to the a window to continue execution: (GDB) Rbreakpoint 1, Main () at A.c:11
one int i = 1; (GDB) n
Display (i);
(GDB) s
Display (I=1) at A.c:5
5 i = i + 2; discovery is already a modified code. 3. Delete breakpoint The following use code 1 debug: Delete the specified breakpoint (GDB) Info breakpoints
Num Type Disp Enb Address What
4 Breakpoint Keep Y 0x080483d4 in main at a.c:11
5 Breakpoint Keep Y 0x080483aa in display at A.c:5
(GDB) Delete 4
(GDB) Info breakpoints
Num Type Disp Enb Address What
5 Breakpoint Keep Y 0x080483aa in display at A.c:5
(GDB) Delete all breakpoints (GDB) info breakpoints
Num Type Disp Enb Address What
5 Breakpoint Keep Y 0x080483aa in display at A.c:5
6 Breakpoint Keep Y 0x080483d4 in main at a.c:11
(GDB) Delete
Delete all breakpoints? (Y or N) y
(GDB) Info breakpoints
No breakpoints or watchpoints.
(GDB) where the shortcut to the Delete command is D. If clear does not take any parameters, the currently stopped breakpoint is deleted: (GDB) Info breakpoints
Num Type Disp Enb Address What
Breakpoint Dis y 0x080483d4 in main at a.c:11
Breakpoint Keep y 0x080483aa in display at A.c:5
(GDB) R
Starting program:/root/a.out
Breakpoint, Main () at A.c:11
one int i = 1;
(GDB) Clear
Deleted Breakpoint 17
(GDB) Info breakpoints
Num Type Disp Enb Address What
Breakpoint Keep y 0x080483aa in display at A.c:5 if clear with parameters, all breakpoints in the row are deleted. GDB allows you to set multiple breakpoints on the same line, and if you use Delete, you must delete one, and clear can be deleted together. You can use any of the following methods: Clear function, clear filename:function, clear line_number, and clear File:linenumber (GDB) Info breakpoints
Num Type Disp Enb Address What
Breakpoint Keep y 0x080483d4 in main at a.c:11
Breakpoint Keep y 0x080483d4 in main at a.c:11
Breakpoint Keep y 0x080483d4 in main at a.c:11
(GDB) Clear Main
Deleted Breakpoints 14 15 16
(GDB) Info breakpoints
No breakpoints or watchpoints.
(GDB) 4. Disabling breakpoints Each breakpoint can be disabled or enabled. The execution of the program is paused only when GDB encounters a breakpoint enabled, otherwise the disabled breakpoint is ignored. Use the Disable breakpoint-list command to disable breakpoints, and use the Enable Breakpoint-list command to enable breakpoints. The following code 1 debugs are used: (GDB) Info breakpoints
Num Type Disp Enb Address What
Breakpoint Keep y 0x080483d4 in main at a.c:11
Breakpoint Keep y 0x080483aa in display at A.c:5
(gdb) Disable 17
(GDB) Info breakpoints
Num Type Disp Enb Address What
Breakpoint Keep n 0x080483d4 in main at a.c:11
Breakpoint Keep y 0x080483aa in display at A.c:5
(GDB) Enable 17
(GDB) Info breakpoints
Num Type Disp Enb Address What
Breakpoint Keep y 0x080483d4 in main at a.c:11
Breakpoint Keep y 0x080483aa in display at A.c:5
(GDB) also has an enable once command, which is disabled the next time the breakpoint causes GdB to pause execution, with the following syntax: Enable once breakpoint-list debugging for the following code: (Code 2) [CPP]View Plaincopy
  1. #include <stdio.h>
  2. int main (void)
  3. {
  4. int m = 1;
  5. int i;
  6. For (i=0; i<10; i++) {
  7. printf ("%d", I);
  8. }
  9. return 0;
  10. }
(GDB) B 8
Breakpoint 2 at 0x80483c5:file B.C, line 8.
(GDB) Info breakpoints
Num Type Disp Enb Address What
2 Breakpoint Keep Y 0x080483c5 in main at B.c:8
(GDB) Disable 2
(GDB) Enable once 2
(GDB) Info breakpoints
Num Type Disp Enb Address What
2 Breakpoint Dis y 0x080483c5 in main at B.c:8
(GDB) R
Starting program:/root/a.out
Breakpoint 2, Main () at B.c:8
8 printf ("%d\n", I);
(GDB) C
Continuing.
0123456789
Program exited normally. Breakpoints are executed only once, and subsequent breakpoints are ignored. There is no disable once command.
5. Breakpoint properties You can use the Info breakpoints (abbreviated to I b) command to get a list of all breakpoints set and their properties. (GDB) I B
Num Type Disp Enb Address What
1 breakpoint Keep Y 0x080483d4 in main at a.c:11
2 Breakpoint Keep y 0x080483aa in display at A.c:5

The output of the next I B is described below. Num: identifier, unique identifier of the breakpoint. Type: The field where the breakpoint is a breakpoint, a watch point, or a capture point. Disp: Deployment, there is one deployment for each power outage, but what happens on that breakpoint the next time the breakpoint causes GDB to pause the execution of the program. There are 3 possible deployment types. Keep: Hold, the breakpoint is not changed the next time the breakpoint is reached. The default deployment after the new breakpoint is created. Del: Delete and delete the breakpoint the next time you reach the breakpoint. The deployment occurs using the Rbreak command. DIS: Disabled, the breakpoint is disabled the next time the breakpoint is reached. The deployment occurs with the Enable once command. ENB: Enabled state that indicates whether the breakpoint is currently enabled or disabled. Address: This is where the breakpoint is set in memory. What: location, showing the line number and file name of the breakpoint location. 6. There are 3 types of recovery execution methods, the first of which is to use step and next without debugging the program, only the next line of code is executed and then paused again. The second class consists of using continue, which allows GDB to unconditionally resume execution of the program until it encounters another power outage or the end of the program. The last type of method involves the condition: restore with the finish or until command. In this case, gdb resumes execution; The program continues to run until it encounters a predetermined condition, reaches another breakpoint, or the program finishes. Step (s), Next (n), and continue (abbreviated as C) are simple and not detailed. It is important to note that both next and step use an optional numeric parameter that represents the additional number of rows executed with next or step, such as Next 3, which is typed next three times in a row. 6.1.finishfinish (abbreviated as FIN) instructs GDB to resume execution until the current stack frame is finished, that is, if you are in a function other than main, the finish command causes GDB to resume execution until the function returns. The following code 1 debugs are used: (GDB) b display
Breakpoint 1 at 0x4004ff:file GDB.C, line 5.
(GDB) R
Starting program:/home/yanwenjie/ctest/a.out

Breakpoint 1, display (I=1) at Gdb.c:5
5 i = i + 1;
(GDB) Finish
Run till exit from #0 display (I=1) at Gdb.c:5
i = 2
Main () at gdb.c:13
return 0;
(GDB) 6.2.untiluntil (abbreviated as U) executes the remainder of the loop, allowing GdB to pause at the first line of code following the loop, without a breakpoint in the loop. Debug using Code 2: (GDB) b Main (GDB) r
Breakpoint 1, Main () at Gdb.c:5
5 int m = 1;
(GDB) n
7 for (i=0; i<10; i++) {
(GDB)
8 printf ("%d", I);
(GDB)
7 for (i=0; i<10; i++) {
(GDB) until
return 0;
(GDB) n
11} 7. A conditional breakpoint conditional breakpoint tells the debugger to stop at a breakpoint only if a certain condition is met, such as when a variable has a particular value of interest. The syntax for setting a conditional breakpoint is: The break Break-args if (condition) conditional terminal is extremely flexible and consists mainly of the following operators: 1. Equal, logical, or unequal operators (<, <=, = =,!). =, >, >=, &&, | | such as: break if String = = NULL && i < 02. Bitwise AND SHIFT Operators (&, |, ^, >>, <<, etc.), for example: Break test.c:34 if (x &amp ; Y) = = 13. Arithmetic operator (+ 、-、 x,/,%), for example: Break MyFunc If I% (j+3)! = 04. Your own functions, as long as they are linked to the program, for example: Break Test.c:myfunc if! Check_variable (i) 5. Library functions, as long as the library is linked to code, for example: Break if Stelen (mystring) = = 0 8. Breakpoint Command ListWhen GdB encounters a breakpoint, it almost always looks at a variable, and if it encounters the same breakpoint over and over again, the same variable is viewed over and over again, allowing GDB to complete a process by automatically executing a set of commands each time it arrives at a breakpoint, which is what the breakpoint command list can accomplish. Use the commands command to set the command list. The commands command sets the list of commands. Commands Breakpoint-num.....commands....end where Breakpoint-number is the identifier of the breakpoint on which the command is to be added. Commands is a list of any valid GDB commands that are split with new rows. Enter the command, and then type end to indicate that the input command is complete. Since then, whenever gdb breaks at this breakpoint, it executes any commands you enter. To debug using code 2:

(GDB) I B
Num Type Disp Enb Address What
1 breakpoint Keep Y 0x000000000040050c in main at Gdb.c:8
(GDB) Commands 1
Type commands for breakpoint (s) 1, one per line.
End with a line saying just "End".
>printf "I is%d\n", I
>end
(GDB) R
Breakpoint 1, Main () at Gdb.c:8
8 printf ("%d", I);
I is 0
(GDB) C
Continuing.


Breakpoint 1, Main () at Gdb.c:8
8 printf ("%d", I);
I is 1

9. A monitoring point monitoring point is a special type of breakpoint that resembles a normal breakpoint and is an instruction that requires GDB to suspend execution of the program. The monitor point instructs gdb to pause execution whenever an expression changes its value. When the variable i exists in scope, use the following method to set the watch: Watch I it will make every time I change when gdb pauses. Debug with the following code: [CPP]View Plaincopy
  1. #include <stdio.h>
  2. int main (void)
  3. {
  4. int m = 1;
  5. int i;
  6. For (i=0; i<10; i++) {
  7. if (i%2 ==0) {
  8. m++;
  9. }
  10. }
  11. return 0;
  12. }
The debug results are as follows: (GDB) b main
Breakpoint 1 at 0x4004b8:file GDB.C, line 5.
(GDB) R
Breakpoint 1, Main () at Gdb.c:5
5 int m = 1;
(GDB) Watch M
Hardware Watchpoint 2:m
(GDB) P m
$ = 0
(GDB) C
Continuing.
Hardware Watchpoint 2:m


Old value = 0
New value = 2 (as if the assignment to M is not stopped as a monitoring point)
Main () at Gdb.c:7
7 for (i=0; i<10; i++) {
(GDB) P m
$ = 2

Stop and look around the program

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.