MSVC debugging skills: pseudoregisters
- Translation from CodeProject.com
Let us start from why this article is. One day, a student let me help him solve a debug problem. For this, I looked at him to enter the code, when I saw the following code:
INT test = getLastError ();
He does this to know the error code that the previous function failed. Every time he needs to know the error code, you add the above code line. I suggested that he deletes all the code servings to debug "Watch" window to add "@err". He doesn't know what this is. Others don't know what is this technology. So I wrote this article to those who have never heard of "Pseudoregisters" technology.
What is "pseudoregister" technology?
"Pseudoregister" is not a real hardware register. Use the PSeudoreGister to view and use the specific values in the debugger (error code, thread information block, ...).
Let's take a look at @err. Start a debugging application. Place the breakpoint in your code to make the debugger interrupt. Open the Watch window, add @err to the window. You can see the value of zero in the value column. Now you can debug Your code and view the value. @Err always displays the getLastError () value of the current thread. When an error occurs, the value changes.
To test, write down the code that causes the error:
FILE * fp = fopen ("c: //a_file_that_does_not_exist.txt", "r");
When walking into the above-described code line, the @err value is modified to be 2. Use Tools-> Error Lookup can view the corresponding error code to explain as ("The System Cannot File Specified"). If you add ", HR", error message It will be displayed, do not have to "Error Lookup" tools (translator: general "Err, HR" is also the same)
Conditional expression
PseudOREGISTERS can also be used in the conditional expression, add the following code line to FOPEN:
IF (fp)
{
Fclose (fp);
}
Add breakpoints in "if (fp). Use Edit-> Breakpoints (ALT-F9) to add the condition" @ Err == 2 ". After startup debugging, enter the breakpoint when the FOPEN () file does not exist. If the file There is no trigger for breakpoints, even when other errors are triggered (for example, 4: Unable to open the specified file).
@Tib pseudoregister
@Err is not a unique debug register. Another important register is @tib. This is the current thread information and is very useful for multi-threaded debugging. If you add breakpoints in the multi-threaded call, the debugger is in no matter what thread When calling, it always triggers. Even if you step out of the code, you will enter another thread call (breakpoint). To solve this problem, you need to do the following error. If you want to trigger the breakpoint of the specified thread, add @TIB registers You will see register values such as "0x7ffa6000" or "2147115008". Editing breakpoint conditions, setting condition @ TIB == 0x7ffa6000. In this way, debugger is only triggered at the specified thread. Additional threads that call the same function will not trigger.
However, this is not applicable for Win98. For Windows 98, you need to use Intel CPU register @ fs == value
Complete register list:
PseudOREGISTER
Description
@Err
The last error value; and the getlasterror () API function is consistent
@Tib
Current thread information; if the debugger cannot process "fs: 0" format is necessary
@Clk
Registers that are not included in the document; just apply at Watch Window
@EAX, @ebx, @ecx, @EDX, @esi, @EDI, @eip, @ESP, @EBP, @eflintel CPU register
@CS, @ds, @es, @ss, @fs, @GS
Intel CPU segment register
@ ST0, @ ST1, @ ST2, @ ST3, @ ST4, @ ST5, @ ST6, @ ST7
Intel CPU floating point register