How to achieve an exception handling 3

zhaozj2021-02-16  53

C and abnormalities

Looking back, we are in the first quarter, which is used to register an exception callback function of the operating system. When an exception occurs, the function will be called.

VC extends the abnormal callback function, and adds two new parameters:

Struct Exception_registration * prev; DWord Handler; INT; DWORD EBP;

In VC , in addition to some exceptions, local variables of the Exception_Registration structure in each function (Author Note: Compiler may not generate any exception related code at all in a function, if there is no try or all in the function The local objects are not available to the description function (the translation: When an exception is generated, the local variables between the stack to Catch to the abnormality are released, which is an important responsibility for exception handling. If these local variables You don't need special release, such as int variable or simple structural variable, as long as you finish your stack pointer, it is not necessary, so the compiler identifies such a situation, making a certain optimization ))). The EBP in the above structure and the EBP pointer in the previous stack frame are overlapping (the translation: Please see how the C compiler implements exception handling 2), the function creates this structure on its stack at the beginning, This structure is registered to the operating system (the translation: "The location of the FS: [0] is changed to the pointer of this structure); recovering the recoveriver in the system's EXCEPTION_REGISTRATION structure (translation: 就 是:: [0] PREV in the structure, I will discuss the importance of the ID domain in the Exception_Registration structure in the next section.

When VC recompores a function, it generates two sets of data for functions:

a) abnormal callback function

b) A data structure that contains important information, such as each Catch block, their address, careful types of exceptions, etc., I will talk more in the next section.

Figure 4 shows the case where the program is running when considering an abnormality process. Widget's abnormal callback is in the head of the abnormal chain (the head of the abnormal chain is FS: [0] points to the content), the content of FS: [0] is the pointer to the Exception_Registration structure defined in the Widget header. When an exception is generated, the exception handles the address of the Widget's function information structure (that is, Exception_Registration pointer) to __cxframehandler function, __ cxxframehandler function Check this data structure, check if there is a Catch block in the function to the current exception, if not Found, the function returns ExceptionContinuesearch to the operating system, the operating system gets the next node in the exception handling chain, then calls the exception handler of this node (this node should be defined in the caller of this function)

This action continues until the abnormality process finds a Catch block that is interested in this exception. After finding the matching catch block, the program no longer returns to the operating system. However, before it calls the Catch block (in the function information structure with pointers, see Figure 4), the exception handle must perform stack release: Clear the stack frames of all functions before this function. Clearing the action of the stack frame is a bit complicated, and the exception handle must find a function that has not ended in an exception production, finds all local variables for each function, call the destructor of each variable. I will discuss this in more detail in the later chapters.

The abnormality processing is such a task that clears the function frame where the exception processing is cleared when an exception process. This operation is from FS: [0], that is, the exception handling chain begins, and then notification of a node along the chain one node It will be recycled, received the node of the notification, call the frame The sector of all local objects then returns, this process continues until the throwing exception is captured. Because the catch block is also part of a function, it also uses the function frame of the function to save the data. Therefore, when an exception handle is entered, the frame of the function is activated (the translation note is to change the EBP to that function, and the ESP is not moving, simply, when executed in the catch function block EBP is the frame top pointing to the function, and the ESP may be at a very far stack top). At the same time, each Catch block that can capture an abnormality (that is, the exception type and the unusual type of the Catch block is consistent) actually has only one parameter, which is the copy of this exception object or a reference copy of the abnormal object. Catch block knows how to copy this exception object from the function information structure, the compiler has produced enough information (translation: is based on the incoming ID, to determine how to copy exceptions).

When the abnormal object is copied, the function frame is activated, the abnormality process starts calling the Catch block, and the return of the CATCH block tells the abnormality processing after try-catch (the translation: Of course, you can then throw in the Catch block). Note that at this moment, even if the stack recovery already exists, the function frames have been cleaned, but these addresses are still in the stack (the translation: That is, when entering the CATCH block function, it is still the stack of the program, but multi-pressure A lot of things in, and some things before the top of the stack have been cleaned, but they still occupy space on the stack, this time is a good time to check the wrong source, this is also the purpose of my research for exceptional processing details) This is because the exception handling is still the same as other functions. It also uses the program stack to store its local variable, and the Cantch block stores the last function of the last function of the local variable during exception generation, the upper side of the stack frame (address more) Low), when the CATCH block returns, you need to delete the copy of this exception object (the translation note: "If the abnormal object is in the stack, you will automatically delete, if it is piled, you need to use Delete or Free to be clearly deleted). At the end of the CATCH block, the exception handle all the cleaned stack frames including their own frames, in fact, to point the ESP to the end of the current function frame (the translation: in front of the Catch block, EBP is Catch The EBP of the function is like 0x12ff58, and the ESP is in a far place such as 0x12f9bc. In fact, most things between the ESP to EBP have been deleted, and now the ESP is changed back, returns to the function of the function, the function is running. For example, 0x12ff80, 0x12ff58-0x12ff80 is actually a range of stack frames with the function of the CATCH block), complete this, and jump to the next statement of the TRY-CATCH block to continue execution. But how does the Catch block know where the end of the function stack frame? This is no way to know, this is why the compiler is to save an ESP reason why the compiler is to save an ESP. Referring to Figure 4, the stack frame pointer EBP minus 16 is the location where the ESP is located.

Catch blocks may generate a new exception or reap out the current exception. An exception has to monitor this situation and then take appropriate operation. If the abnormality process throws a new exception, the previous exception is deleted. If the Catch block decides to throw the captured exception again, the previous exception is passed behind.

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

New Post(0)