Use GDB debugging procedures (3)

zhaozj2021-02-08  286

Fourth, maintain the stop point

The above mentioned how to set the stop point of the program, the stop point in the GDB is the three classes described above. In GDB, if you feel that the defined stop point is not used, you can use the delete, clear, disable, and enable, these commands to maintain.

Clear clears all defined stoppoints.

Clear Clear Clear all stop points on the function.

Clear Clear Clear all the stop points set on the specified line.

Delete [BreakPoints] [Range ...] Deletes the specified breakpoint, BreakPoints is a breakpoint. If the break point is not specified, it means deleting all breakpoints. Range represents the range of breakpoints (such as: 3-7). Its shorthand command is D.

A better way to delete is the disable stop point. The stop point of Disable, the GDB will not be deleted. When you need it, Enable is as follows.

Disable [BreakPoints] [Range ...] Disable, the stop point specified by Disable, BreakPoints is the stop point number. If nothing is specified, it means all stop points for Disable. The shorthand command is DIS.

Enable [Breakpoints] [Range ...] Enable The stoppoint specified, Breakpoints is the stop point number.

Enable [BreakPoints] Once Range ... Enable The stop point specified once, when the program is stopped, the stop point is automatically disabled by GDB.

Enable [BreakPoints] delete Range ... Enable The stop point specified once, when the program is stopped, the stop point is automatically deleted by GDB.

5. Stop condition maintenance

As mentioned earlier, when we mentioned the breakpoint, we mentioned that you can set a condition. When the conditions are set up, the program is automatically stopped. This is a very powerful function. Here, I want to tell the relevant maintenance commands for this condition. In general, set a condition for breakpoints, we use the IF keyword, followed by its breakpoint conditions. Moreover, after the condition is set, we can use the construct command to modify the conditions of the breakpoint. (Only Break and Watch Commands Support IF, Catch does not currently support IF)

Condition Modify the stop condition for BNUM for Expression.

Condition Clears the stop condition of BNUM.

There is also a particularly special maintenance command ignore, you can specify the program run, ignore the stop condition a few times.

Ignore means that the stop condition COUNT that ignores the breakpoint of BNUM.

6. Set the run command for the stop point

We can use the Command command provided by GDB to set the run command of the stop point. That is, when the running program is stopped, we can make it automatically run for some other commands, which is advantageous to automate debugging. GDB-based automation debugging is a powerful support.

Commands [BNUM] ... command-list ... End

Write a list of commands for the breakpoint BNUM. When the program is stopped by the breakpoint, the GDB will run the command in the command list.

E.g:

Break foo if x> 0 Commands Printf "x IS% D / N", X Continue End breakpoint is set in the function foo, the breakpoint condition is x> 0, if the program is broken, that is, once X In the FOO function is greater than 0, GDB automatically prints the value of X and continues to run the program.

If you want to clear the command sequence on the breakpoint, just make a commands command, and you will do it directly.

Seven, breakpoint menu

In C , you may repeat the function of the same name (function overload), in which case BREAK cannot tell GDB to stop in which function is to stop. Of course, you can use Break that is, tell GDB's parameter type of the function to specify a function. Otherwise, GDB will list a break menu for you to choose the breakpoint you need. You just need to enter the number in your menu list. Such as:

(GDB) B String :: after [0] Cancel [1] all [2] file: string.cc; line number: 867 [3] file: string.cc; line number: string.cc; line number: string.cc Line Number: 875 [5] file: string.cc; line number: 853 [6] file: string.cc; line number: 846 [7] file: string.cc; line number: 735> 2 4 6 Breakpoint 1 at 0xb26c: file String.cc, line 867. Breakpoint 2 at 0xb344: file String.cc, line 875. Breakpoint 3 at 0xafcc:. file String.cc, line 846. Multiple breakpoints were set Use the "delete" command to delete UNWANTED BREAKPOINTS. (GDB)

It can be seen that GDB lists all After's overloaded functions, you can choose the list number. 0 indicates that the setting breakpoint is given, and 1 means that all functions set breakpoints.

Eight, recovery procedures running and single-step debugging

When the program is stopped, you can use the Continue command to recover the run until the end of the program, or the next breakpoint. You can also use the STEP or NEXT command single-step tracker.

Continue [ignore-count] c [ignore-count] FG [ignore-count] recovery program runs until the program ends, or the next breakpoint arrival. Ignore-count indicates that the number of breakpoints is ignored. The three commands of Continue, C, and FG are the same meaning.

Step Single step tracking, if there is a function call, he will enter the function. The premise of entering the function is that this function is compiled with Debug information. Very like Step in in Tools such as VC. Behind COUNT can also not be added, do not add a strip execution, plus the COUNT section instruction to execute, and then stop.

Next The same single step tracking, if there is a function call, he will not enter the function. Very STEP over, such as the VC and other tools. Behind COUNT can also not be added, do not add a strip execution, plus the COUNT section instruction to execute, and then stop. SET STEP-MODE SET STEP-MODE ON Open STEP-MODE mode, so that the program does not stop because there is no debug information when performing single-step tracking. This parameter is favorable to view the machine code.

Set Step-Mod Off Turns the Step-Mode mode.

Finish running procedure until the current function completes the return. And print functions returned to the stack address and return value and parameter value.

Until or u When you are tired of walking in a cyclic body, this command can run the program until the cyclic body is exited.

Stepi or Si nexti or Ni single step tracking one machine directive! A program code may be completed by several machine instructions, STEPI and NEXTI can perform machine instructions in a single step. As with the same function as the same function is "Display / I $ PC", after running this command, single-step tracking will play machine instructions while playing the program code (that is, assembly code)

Nine, signals (SIGNALS)

The signal is a soft interrupt, a method of processing an asynchronous event. In general, the operating system supports many signals. Especially UNIX, compare important applications generally process signals. Unix defines a number of signals, such as Sigint represents the interrupt character signal, that is, the signal of Ctrl C, Sigbus represents the signal of the hardware fault; SIGCHLD represents the signal of the child process; Sigkill indicates the signal of the termination program, and the like. Semicidal programming is a very important technology under UNIX.

GDB has the ability to deal with any signal when you debug the program, you can tell GDB which signal needs to process. You can ask GDB When you receive the signal you specified, you will stop running the program that is running for you to debug. You can use the GDB's handle command to complete this feature.

Handle defines a signal processing in GDB. Signal can be started with SIG or not starting with SIG, can be used to define a range to process signals (such as: sigio-sigkill, indicating processing from SIGIO signals to sigkill, including SIGIO, SIGIOT, SIGKILL three signals ), You can also use the keyword all to indicate all signals to process. Once the debugged program receives the signal, the running program will be immediately stopped by GDB for debug. Its can be one or more of the following keywords.

Nostop When the debugging program receives a signal, GDB does not stop the running of the program, but the message tells you that this signal is received. When the STOP receives the signal when the debugged program, GDB will stop your program. Print When the debugging program receives the signal, GDB will display a message. NOPRINT When the debugging program receives the signal, GDB will not tell you information about the signal. PASS NOIGNORE When the debugging program receives the signal, the GDB does not process the signal. This means that GDB will hand over this signal to the debugged program. When NOPASS IGNORE receives a signal when the debugged program receives the signal, the GDB will not let the debug program process this signal. Info Signals Info Handle View which signals are in GDB detection.

Ten, thread stops

If your program is multi-thread, you can define if your breakpoint is on all threads or in a particular thread. GDB is easy to help you have completed this.

BREAK Thread BREAK Thread if ... Linespec Specifies the line number of the source program that is set in the breakpoint. Threadno Specifies the id's ID, note that this ID is GDB assignment, you can view thread information in the program through the "Info Threads" command. If you don't specify Thread , you indicate that your breakpoint is located on all threads. You can also specify a breakpoint condition for a thread. Such as: (GDB) Break Frik.c: 13 Thread 28 IF Bartab> Lim

When your program is parked by GDB, all running threads will be stopped. This makes it easy you to view the overall situation of the running program. And all threads are still running when you are running in your recovery program. It is afraid that the main process is in a single step of debugging.

<- Previous Next -> (All rights reserved, please indicate the author and source)

转载请注明原文地址:https://www.9cbs.com/read-1119.html

New Post(0)