1999-05-09 18:12:19 +02:00
|
|
|
/*
|
|
|
|
* NT exception handling routines
|
|
|
|
*
|
|
|
|
* Copyright 1999 Turchanov Sergey
|
|
|
|
* Copyright 1999 Alexandre Julliard
|
|
|
|
*/
|
|
|
|
|
1999-06-18 20:21:24 +02:00
|
|
|
#include <signal.h>
|
1999-05-09 18:12:19 +02:00
|
|
|
#include "winnt.h"
|
|
|
|
#include "ntddk.h"
|
1999-05-23 22:02:04 +02:00
|
|
|
#include "process.h"
|
1999-06-18 20:21:24 +02:00
|
|
|
#include "global.h"
|
1999-05-12 15:10:39 +02:00
|
|
|
#include "wine/exception.h"
|
1999-05-09 18:12:19 +02:00
|
|
|
#include "stackframe.h"
|
1999-06-18 20:21:24 +02:00
|
|
|
#include "miscemu.h"
|
|
|
|
#include "debugtools.h"
|
1999-05-09 18:12:19 +02:00
|
|
|
|
|
|
|
DEFAULT_DEBUG_CHANNEL(seh)
|
|
|
|
|
|
|
|
/* Exception record for handling exceptions happening inside exception handlers */
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
EXCEPTION_FRAME frame;
|
|
|
|
EXCEPTION_FRAME *prevFrame;
|
|
|
|
} EXC_NESTED_FRAME;
|
|
|
|
|
1999-09-27 12:58:45 +02:00
|
|
|
|
1999-06-18 20:21:24 +02:00
|
|
|
/* Default hook for built-in debugger */
|
|
|
|
static DWORD default_hook( EXCEPTION_RECORD *rec, CONTEXT *ctx, BOOL first )
|
|
|
|
{
|
|
|
|
if (!first)
|
|
|
|
{
|
|
|
|
DPRINTF( "stopping process due to unhandled exception %08lx.\n",
|
|
|
|
rec->ExceptionCode );
|
|
|
|
raise( SIGSTOP );
|
|
|
|
}
|
|
|
|
return 0; /* not handled */
|
|
|
|
}
|
|
|
|
static DEBUGHOOK debug_hook = default_hook;
|
|
|
|
|
|
|
|
/*******************************************************************
|
|
|
|
* EXC_SetDebugEventHook
|
|
|
|
* EXC_GetDebugEventHook
|
|
|
|
*
|
|
|
|
* Set/Get the hook for the built-in debugger.
|
|
|
|
*
|
|
|
|
* FIXME: the built-in debugger should use the normal debug events.
|
|
|
|
*/
|
|
|
|
void EXC_SetDebugEventHook( DEBUGHOOK hook )
|
|
|
|
{
|
|
|
|
debug_hook = hook;
|
|
|
|
}
|
|
|
|
DEBUGHOOK EXC_GetDebugEventHook(void)
|
|
|
|
{
|
|
|
|
return debug_hook;
|
|
|
|
}
|
1999-05-09 18:12:19 +02:00
|
|
|
|
|
|
|
/*******************************************************************
|
|
|
|
* EXC_RaiseHandler
|
|
|
|
*
|
|
|
|
* Handler for exceptions happening inside a handler.
|
|
|
|
*/
|
1999-11-10 20:42:13 +01:00
|
|
|
static DWORD EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
|
|
|
|
CONTEXT *context, EXCEPTION_FRAME **dispatcher )
|
1999-05-09 18:12:19 +02:00
|
|
|
{
|
|
|
|
if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
|
|
|
|
return ExceptionContinueSearch;
|
|
|
|
/* We shouldn't get here so we store faulty frame in dispatcher */
|
|
|
|
*dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
|
|
|
|
return ExceptionNestedException;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*******************************************************************
|
|
|
|
* EXC_UnwindHandler
|
|
|
|
*
|
|
|
|
* Handler for exceptions happening inside an unwind handler.
|
|
|
|
*/
|
1999-11-10 20:42:13 +01:00
|
|
|
static DWORD EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
|
|
|
|
CONTEXT *context, EXCEPTION_FRAME **dispatcher )
|
1999-05-09 18:12:19 +02:00
|
|
|
{
|
|
|
|
if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
|
|
|
|
return ExceptionContinueSearch;
|
|
|
|
/* We shouldn't get here so we store faulty frame in dispatcher */
|
|
|
|
*dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
|
|
|
|
return ExceptionCollidedUnwind;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*******************************************************************
|
|
|
|
* EXC_CallHandler
|
|
|
|
*
|
|
|
|
* Call an exception handler, setting up an exception frame to catch exceptions
|
|
|
|
* happening during the handler execution.
|
1999-10-24 01:55:50 +02:00
|
|
|
* Please do not change the first 4 parameters order in any way - some exceptions handlers
|
|
|
|
* rely on Base Pointer (EBP) to have a fixed position related to the exception frame
|
1999-05-09 18:12:19 +02:00
|
|
|
*/
|
1999-10-24 01:55:50 +02:00
|
|
|
static DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
|
|
|
|
CONTEXT *context, EXCEPTION_FRAME **dispatcher,
|
|
|
|
PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler)
|
1999-05-09 18:12:19 +02:00
|
|
|
{
|
|
|
|
EXC_NESTED_FRAME newframe;
|
|
|
|
DWORD ret;
|
|
|
|
|
|
|
|
newframe.frame.Handler = nested_handler;
|
|
|
|
newframe.prevFrame = frame;
|
|
|
|
EXC_push_frame( &newframe.frame );
|
1999-05-23 22:02:04 +02:00
|
|
|
TRACE( "calling handler at %p code=%lx flags=%lx\n",
|
|
|
|
handler, record->ExceptionCode, record->ExceptionFlags );
|
1999-05-09 18:12:19 +02:00
|
|
|
ret = handler( record, frame, context, dispatcher );
|
|
|
|
TRACE( "handler returned %lx\n", ret );
|
|
|
|
EXC_pop_frame( &newframe.frame );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*******************************************************************
|
|
|
|
* EXC_DefaultHandling
|
|
|
|
*
|
|
|
|
* Default handling for exceptions. Called when we didn't find a suitable handler.
|
|
|
|
*/
|
|
|
|
static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
|
|
|
|
{
|
1999-05-23 22:02:04 +02:00
|
|
|
if ((PROCESS_Current()->flags & PDB32_DEBUGGED) &&
|
1999-11-24 02:24:50 +01:00
|
|
|
(DEBUG_SendExceptionEvent( rec, FALSE, context ) == DBG_CONTINUE))
|
1999-05-23 22:02:04 +02:00
|
|
|
return; /* continue execution */
|
|
|
|
|
1999-06-18 20:21:24 +02:00
|
|
|
if (debug_hook( rec, context, FALSE ) == DBG_CONTINUE)
|
|
|
|
return; /* continue execution */
|
|
|
|
|
1999-05-09 18:12:19 +02:00
|
|
|
if (rec->ExceptionFlags & EH_STACK_INVALID)
|
|
|
|
ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
|
|
|
|
else if (rec->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION)
|
|
|
|
ERR("Process attempted to continue execution after noncontinuable exception.\n");
|
|
|
|
else
|
|
|
|
ERR("Unhandled exception code %lx flags %lx addr %p\n",
|
|
|
|
rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
|
|
|
|
TerminateProcess( GetCurrentProcess(), 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-05-13 18:21:37 +02:00
|
|
|
/***********************************************************************
|
1999-11-14 00:54:04 +01:00
|
|
|
* EXC_RtlRaiseException / RtlRaiseException (NTDLL.464)
|
1999-05-09 18:12:19 +02:00
|
|
|
*/
|
1999-11-14 00:54:04 +01:00
|
|
|
DEFINE_REGS_ENTRYPOINT_1( RtlRaiseException, EXC_RtlRaiseException, EXCEPTION_RECORD * )
|
|
|
|
void WINAPI EXC_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
|
1999-05-09 18:12:19 +02:00
|
|
|
{
|
|
|
|
PEXCEPTION_FRAME frame, dispatch, nested_frame;
|
|
|
|
EXCEPTION_RECORD newrec;
|
|
|
|
DWORD res;
|
|
|
|
|
1999-05-23 22:02:04 +02:00
|
|
|
TRACE( "code=%lx flags=%lx\n", rec->ExceptionCode, rec->ExceptionFlags );
|
|
|
|
|
|
|
|
if ((PROCESS_Current()->flags & PDB32_DEBUGGED) &&
|
1999-11-24 02:24:50 +01:00
|
|
|
(DEBUG_SendExceptionEvent( rec, TRUE, context ) == DBG_CONTINUE))
|
1999-05-23 22:02:04 +02:00
|
|
|
return; /* continue execution */
|
|
|
|
|
1999-06-18 20:21:24 +02:00
|
|
|
if (debug_hook( rec, context, TRUE ) == DBG_CONTINUE)
|
|
|
|
return; /* continue execution */
|
|
|
|
|
1999-06-22 13:43:42 +02:00
|
|
|
frame = NtCurrentTeb()->except;
|
1999-05-09 18:12:19 +02:00
|
|
|
nested_frame = NULL;
|
|
|
|
while (frame != (PEXCEPTION_FRAME)0xFFFFFFFF)
|
|
|
|
{
|
|
|
|
/* Check frame address */
|
1999-06-22 13:43:42 +02:00
|
|
|
if (((void*)frame < NtCurrentTeb()->stack_low) ||
|
|
|
|
((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
|
1999-05-09 18:12:19 +02:00
|
|
|
(int)frame & 3)
|
|
|
|
{
|
|
|
|
rec->ExceptionFlags |= EH_STACK_INVALID;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call handler */
|
1999-10-24 01:55:50 +02:00
|
|
|
res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
|
1999-05-09 18:12:19 +02:00
|
|
|
if (frame == nested_frame)
|
|
|
|
{
|
|
|
|
/* no longer nested */
|
|
|
|
nested_frame = NULL;
|
|
|
|
rec->ExceptionFlags &= ~EH_NESTED_CALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(res)
|
|
|
|
{
|
|
|
|
case ExceptionContinueExecution:
|
|
|
|
if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
|
|
|
|
newrec.ExceptionCode = STATUS_NONCONTINUABLE_EXCEPTION;
|
|
|
|
newrec.ExceptionFlags = EH_NONCONTINUABLE;
|
|
|
|
newrec.ExceptionRecord = rec;
|
|
|
|
newrec.NumberParameters = 0;
|
|
|
|
RtlRaiseException( &newrec ); /* never returns */
|
|
|
|
break;
|
|
|
|
case ExceptionContinueSearch:
|
|
|
|
break;
|
|
|
|
case ExceptionNestedException:
|
|
|
|
if (nested_frame < dispatch) nested_frame = dispatch;
|
|
|
|
rec->ExceptionFlags |= EH_NESTED_CALL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
|
|
|
|
newrec.ExceptionFlags = EH_NONCONTINUABLE;
|
|
|
|
newrec.ExceptionRecord = rec;
|
|
|
|
newrec.NumberParameters = 0;
|
|
|
|
RtlRaiseException( &newrec ); /* never returns */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
frame = frame->Prev;
|
|
|
|
}
|
|
|
|
EXC_DefaultHandling( rec, context );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*******************************************************************
|
1999-11-14 00:54:04 +01:00
|
|
|
* EXC_RtlUnwind / RtlUnwind (KERNEL32.590) (NTDLL.518)
|
1999-05-09 18:12:19 +02:00
|
|
|
*/
|
1999-11-14 00:54:04 +01:00
|
|
|
DEFINE_REGS_ENTRYPOINT_4( RtlUnwind, EXC_RtlUnwind,
|
|
|
|
PEXCEPTION_FRAME, LPVOID, PEXCEPTION_RECORD, DWORD )
|
|
|
|
void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME pEndFrame, LPVOID unusedEip,
|
|
|
|
PEXCEPTION_RECORD pRecord, DWORD returnEax,
|
|
|
|
CONTEXT *context )
|
1999-05-09 18:12:19 +02:00
|
|
|
{
|
|
|
|
EXCEPTION_RECORD record, newrec;
|
|
|
|
PEXCEPTION_FRAME frame, dispatch;
|
|
|
|
|
1999-06-26 20:40:24 +02:00
|
|
|
#ifdef __i386__
|
1999-09-27 12:58:45 +02:00
|
|
|
context->Eax = returnEax;
|
1999-06-26 20:40:24 +02:00
|
|
|
#endif
|
1999-05-13 18:21:37 +02:00
|
|
|
|
1999-05-09 18:12:19 +02:00
|
|
|
/* build an exception record, if we do not have one */
|
|
|
|
if (!pRecord)
|
|
|
|
{
|
|
|
|
record.ExceptionCode = STATUS_UNWIND;
|
|
|
|
record.ExceptionFlags = 0;
|
|
|
|
record.ExceptionRecord = NULL;
|
1999-06-18 20:21:24 +02:00
|
|
|
record.ExceptionAddress = GET_IP(context);
|
1999-05-09 18:12:19 +02:00
|
|
|
record.NumberParameters = 0;
|
|
|
|
pRecord = &record;
|
|
|
|
}
|
|
|
|
|
|
|
|
pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
|
1999-05-23 22:02:04 +02:00
|
|
|
|
|
|
|
TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
|
|
|
|
|
1999-05-09 18:12:19 +02:00
|
|
|
/* get chain of exception frames */
|
1999-06-22 13:43:42 +02:00
|
|
|
frame = NtCurrentTeb()->except;
|
1999-05-09 18:12:19 +02:00
|
|
|
while ((frame != (PEXCEPTION_FRAME)0xffffffff) && (frame != pEndFrame))
|
|
|
|
{
|
|
|
|
/* Check frame address */
|
|
|
|
if (pEndFrame && (frame > pEndFrame))
|
|
|
|
{
|
|
|
|
newrec.ExceptionCode = STATUS_INVALID_UNWIND_TARGET;
|
|
|
|
newrec.ExceptionFlags = EH_NONCONTINUABLE;
|
|
|
|
newrec.ExceptionRecord = pRecord;
|
|
|
|
newrec.NumberParameters = 0;
|
|
|
|
RtlRaiseException( &newrec ); /* never returns */
|
|
|
|
}
|
1999-06-22 13:43:42 +02:00
|
|
|
if (((void*)frame < NtCurrentTeb()->stack_low) ||
|
|
|
|
((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
|
1999-05-09 18:12:19 +02:00
|
|
|
(int)frame & 3)
|
|
|
|
{
|
|
|
|
newrec.ExceptionCode = STATUS_BAD_STACK;
|
|
|
|
newrec.ExceptionFlags = EH_NONCONTINUABLE;
|
|
|
|
newrec.ExceptionRecord = pRecord;
|
|
|
|
newrec.NumberParameters = 0;
|
|
|
|
RtlRaiseException( &newrec ); /* never returns */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call handler */
|
1999-10-24 01:55:50 +02:00
|
|
|
switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
|
|
|
|
frame->Handler, EXC_UnwindHandler ))
|
1999-05-09 18:12:19 +02:00
|
|
|
{
|
|
|
|
case ExceptionContinueSearch:
|
|
|
|
break;
|
|
|
|
case ExceptionCollidedUnwind:
|
|
|
|
frame = dispatch;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
|
|
|
|
newrec.ExceptionFlags = EH_NONCONTINUABLE;
|
|
|
|
newrec.ExceptionRecord = pRecord;
|
|
|
|
newrec.NumberParameters = 0;
|
|
|
|
RtlRaiseException( &newrec ); /* never returns */
|
|
|
|
break;
|
|
|
|
}
|
1999-06-18 20:21:24 +02:00
|
|
|
frame = EXC_pop_frame( frame );
|
1999-05-09 18:12:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*******************************************************************
|
1999-11-14 00:54:04 +01:00
|
|
|
* EXC_NtRaiseException / NtRaiseException (NTDLL.175)
|
1999-05-09 18:12:19 +02:00
|
|
|
*/
|
1999-11-14 00:54:04 +01:00
|
|
|
DEFINE_REGS_ENTRYPOINT_3( NtRaiseException, EXC_NtRaiseException,
|
|
|
|
EXCEPTION_RECORD *, CONTEXT *, BOOL )
|
|
|
|
void WINAPI EXC_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
|
|
|
|
BOOL first, CONTEXT *context )
|
1999-05-09 18:12:19 +02:00
|
|
|
{
|
1999-11-14 00:54:04 +01:00
|
|
|
EXC_RtlRaiseException( rec, ctx );
|
1999-05-09 18:12:19 +02:00
|
|
|
*context = *ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlRaiseStatus (NTDLL.465)
|
|
|
|
*
|
|
|
|
* Raise an exception with ExceptionCode = status
|
|
|
|
*/
|
|
|
|
void WINAPI RtlRaiseStatus( NTSTATUS status )
|
|
|
|
{
|
|
|
|
EXCEPTION_RECORD ExceptionRec;
|
|
|
|
|
|
|
|
ExceptionRec.ExceptionCode = status;
|
|
|
|
ExceptionRec.ExceptionFlags = EH_NONCONTINUABLE;
|
|
|
|
ExceptionRec.ExceptionRecord = NULL;
|
|
|
|
ExceptionRec.NumberParameters = 0;
|
|
|
|
RtlRaiseException( &ExceptionRec );
|
|
|
|
}
|
1999-06-18 20:21:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
1999-11-14 00:54:04 +01:00
|
|
|
* EXC_DebugBreak / DebugBreak (KERNEL32.181)
|
1999-06-18 20:21:24 +02:00
|
|
|
*/
|
1999-11-14 00:54:04 +01:00
|
|
|
DEFINE_REGS_ENTRYPOINT_0( DebugBreak, EXC_DebugBreak )
|
|
|
|
void WINAPI EXC_DebugBreak( CONTEXT *context )
|
1999-06-18 20:21:24 +02:00
|
|
|
{
|
|
|
|
EXCEPTION_RECORD rec;
|
|
|
|
|
|
|
|
rec.ExceptionCode = EXCEPTION_BREAKPOINT;
|
1999-09-22 17:25:45 +02:00
|
|
|
rec.ExceptionFlags = 0;
|
1999-06-18 20:21:24 +02:00
|
|
|
rec.ExceptionRecord = NULL;
|
|
|
|
rec.NumberParameters = 0;
|
1999-11-14 00:54:04 +01:00
|
|
|
EXC_RtlRaiseException( &rec, context );
|
1999-06-18 20:21:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* DebugBreak16 (KERNEL.203)
|
|
|
|
*/
|
1999-07-31 19:32:05 +02:00
|
|
|
void WINAPI DebugBreak16( CONTEXT86 *context )
|
1999-06-18 20:21:24 +02:00
|
|
|
{
|
1999-07-31 19:32:05 +02:00
|
|
|
#ifdef __i386__
|
1999-11-14 00:54:04 +01:00
|
|
|
EXC_DebugBreak( context );
|
1999-07-31 19:32:05 +02:00
|
|
|
#endif /* defined(__i386__) */
|
1999-06-18 20:21:24 +02:00
|
|
|
}
|