开发者

Imitation of hardware exceptions

Can anyone tell me a code for next function, which raise开发者_如何学编程s EXCEPTION_FLT_STACK_CHECK or EXCEPTION_BREAKPOINT, for I could catch them in main func:

int _tmain(int argc, _TCHAR* argv[])
{
    __try 
    { 
        FaultingStack(); // What I need to write in this function???
    } 
    __except(GetExceptionCode() == EXCEPTION_FLT_STACK_CHECK ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
    { 
        return FALSE;
    }
    return TRUE;

    return 0;
}

Do not suggest RaiseException func, I need an example of fault code, not software raised exception

UPD: I need one more code snippet for next exception EXCEPTION_INT_OVERFLOW


Breakpoint exception is raised easily. You can use one of the following (which is all the same):

DebugBreak(); // API function
__debugbreak(); // MSVC intrinsic
__asm int 3; // Actual instruction

Now, EXCEPTION_FLT_STACK_CHECK is related to the invalid floating-point register stack state.

First one should enable FP exceptions related to the FP stack:

#include <float.h>
_clearfp();
_controlfp(_controlfp(0, 0) & ~(EM_INVALID), MCW_EM);

Next, make FP stack overflow/underflow:

for (float f; ; )
    __asm fstp f;


Assuming MSVC since this is a Windows question. You can get a breakpoint exception by using the __debugbreak() intrinsic. Test without attaching a debugger. A floating point stack check fault requires unmasking the under/overflow exceptions in the FPU control word. And, say, popping the stack too often. I rolled them both in one sample program:

int _tmain(int argc, _TCHAR* argv[])
{
    // STATUS_BREAKPOINT
    __debugbreak();

    // STATUS_FLOAT_STACK_CHECK
    _control87(_EM_UNDERFLOW | _EM_OVERFLOW, _MCW_EM);
    double temp = 0;
    __asm {
        fstp [temp]
        fstp [temp]
    }
    return 0;
}

`

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜