In the previous articleArticle Visual StudioBreakpoint basics for debugging What is a breakpoint, Int Yes Intel Series CPU To allowProgramAn interruption or exception occurs. If the program is interrupted or an exception occurs, CPU Will interrupt the execution of the program, go to IDT Find the routine for processing the interrupt (or exception ( Handler). IDT It is initialized when the operating system is started. IDT For example, what is IDT , How to compile IDT Routine, how Initialization IDT You can search for some materials online.
In short, we only need to know,CPUWhen executing program commandsINT 3Interrupt program execution,CPUThen goIDTFind the routine entry for processing breakpoints in the table. The task of this routine is:
1.First, check whether a debugger is installed on the machine.-Remember, this step is very important and will be introduced in later articles.
2.If the debugger is not installed on the machine, the operating system terminates the execution of the program.
3.Otherwise, the operating system starts the debugger and attaches it to the process.
4.In this way, we can check the values of internal variables in the program in the debugger.
In the previous articleINT 3(OrDebugbreak (), OrDebugger. Break ()) The command is on our ownCodeIt is hard-coded, so weVisual StudioIn the corresponding code line, there is a little red ball, that isVisual StudioAdd a dynamicINT 3Command. Now the problem is coming,Visual StudioHow to find the correct insert in the programINT 3Where is the instruction?
Or more specifically, weSource codeIn a text file,Visual StudioThe code line needs to be translated into the position in the program instruction set.Visual StudioThe reason for translation is that a lineC ++OrC #The code will correspond to several lines of Assembly commands.
Therefore,Visual StudioAn additional file is required to execute the translation process. This additional file is called the debugging symbol file (SymbolsIs generated by the compiler.Visual StudioSeries compilers, whether it isC #,VB. NETOrC ++The compiler generates this debugging symbol file,. PDBFile. So if you take a lookDebugFolder, you will find this file.
So let's take a lookVisual StudioSupports various breakpoints and explains the implementation methods of various breakpoints.
Conditional breakpoint
First, let's take a look at how to set conditional breakpoints. There are two types of conditional breakpoints: one is set based on the number of triggers, and the other is set based on a preset condition.
Set the number of triggers
For example, you have a loop1000Times, you know there isBugAlways in500This occurs only after the first time, so you certainly want to set a breakpoint in the loop, but the previous500This breakpoint will not be triggered once, otherwise500TimesF5It is indeed not a simple task.
Set according to preset Conditions
If you already know some conditionsBug, Then it is best to set according to the conditions. As shown in:
In (Breakpoint Condition) Dialog box, you only need to enter a normalC #,C ++OrVB. NET(Of course, The syntax is consistent with the source code syntax in your project), the requirement of this statement is that you must returnBoolValue-Otherwise, it is not a condition.
Third, there is a breakpoint filter. When you are on the breakpoint, right-click the pop-up menu, there will be a "filter (Filter. I will not elaborate here. If you are interested, you can write a multi-threaded or multi-process program and try this function.
After knowing the principle of the breakpoint, understanding the breakpoint should not be a problem.
Monitoring breakpoint ( Watching point )
Sometimes, you may need to check the values of some variables in the program, but you do not want to interrupt the program execution. For example, if you are debugging a network protocol stack, a program may be receiving data packets and you want to see the data packet format. However, if the program is interrupted, subsequent data packets may be lost.
Therefore, we generally add some log record code in the source code to record the values of some variables for subsequent analysis. If logs are required after the product is released, it is a good idea to add these log codes to the source code. But what if you just want to temporarily check the values of some variables?
At this time, it is very useful to monitor breakpoints,Visual StudioYou can print some variable values in the debugger window without modifying the program source code.
Demonstrate how to monitor breakpoints:
Set the monitoring breakpoint step, or note:
1.Set a normal breakpoint
2.Right-click the breakpoint you just set and select"When hit..."
3.Check The first "print a message (Print a message) "Check box, enter a string of text. By default, the text you enter will be printed directly to the debugging output window. Except:
A.To$Several keywords starting with a symbol. For example$ FunctionIt is replaced with the name of the function where the breakpoint is located. Other keywords includeWhen breakpoints is hit.
B.Use Braces{}The name of the included variable. Such a string is replaced with the value of the variable.
This is the effect of the breakpoint monitoring. Note that you can onlyVisual StudioOutput (Output.
The advantage of monitoring breakpoints over logging is that you do not need to modify the source code and recompile the code. ActuallyVisual StudioThe principle of implementing breakpoint monitoring is also very simple, that is, inserting a normal breakpoint, processing after the breakpoint is triggered and printing in"When breakpoints is hit"Window output expression, and finally the execution of the program is automatically restored.