2004-07-07 16:32:02 +00:00
|
|
|
/* $Id: except.c,v 1.15 2004/07/07 16:32:01 navaraf Exp $
|
2000-06-03 14:47:33 +00:00
|
|
|
*
|
1998-12-04 18:28:13 +00:00
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS system libraries
|
|
|
|
* FILE: lib/kernel32/misc/except.c
|
|
|
|
* PURPOSE: Exception functions
|
|
|
|
* PROGRAMMER: Ariadne ( ariadne@xs4all.nl)
|
1999-10-07 23:46:27 +00:00
|
|
|
* modified from WINE [ Onno Hovers, (onno@stack.urc.tue.nl) ]
|
1998-12-04 18:28:13 +00:00
|
|
|
* UPDATE HISTORY:
|
|
|
|
* Created 01/11/98
|
|
|
|
*/
|
|
|
|
|
2003-01-15 21:24:36 +00:00
|
|
|
#include <k32.h>
|
2002-09-08 10:23:54 +00:00
|
|
|
|
2004-07-07 16:32:02 +00:00
|
|
|
#define NDEBUG
|
|
|
|
#include "../include/debug.h"
|
|
|
|
|
|
|
|
UINT GlobalErrMode = 0;
|
|
|
|
LPTOP_LEVEL_EXCEPTION_FILTER GlobalTopLevelExceptionFilter = NULL;
|
1998-12-04 18:28:13 +00:00
|
|
|
|
|
|
|
UINT GetErrorMode(void)
|
|
|
|
{
|
|
|
|
return GlobalErrMode;
|
|
|
|
}
|
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
1998-12-04 18:28:13 +00:00
|
|
|
UINT
|
|
|
|
STDCALL
|
|
|
|
SetErrorMode( UINT uMode )
|
|
|
|
{
|
|
|
|
UINT OldErrMode = GetErrorMode();
|
|
|
|
GlobalErrMode = uMode;
|
|
|
|
return OldErrMode;
|
|
|
|
}
|
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
1998-12-04 18:28:13 +00:00
|
|
|
LPTOP_LEVEL_EXCEPTION_FILTER
|
|
|
|
STDCALL
|
|
|
|
SetUnhandledExceptionFilter(
|
|
|
|
LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter
|
|
|
|
)
|
|
|
|
{
|
|
|
|
LPTOP_LEVEL_EXCEPTION_FILTER OldTopLevelExceptionFilter =
|
|
|
|
GlobalTopLevelExceptionFilter;
|
2003-08-24 14:49:55 +00:00
|
|
|
GlobalTopLevelExceptionFilter = lpTopLevelExceptionFilter;
|
1998-12-04 18:28:13 +00:00
|
|
|
return OldTopLevelExceptionFilter;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-07-07 16:32:02 +00:00
|
|
|
/*
|
|
|
|
* Private helper function to lookup the module name from a given address.
|
|
|
|
* The address can point to anywhere within the module.
|
|
|
|
*/
|
|
|
|
static const char*
|
|
|
|
_module_name_from_addr(const void* addr, char* psz, size_t nChars)
|
|
|
|
{
|
|
|
|
MEMORY_BASIC_INFORMATION mbi;
|
|
|
|
if (VirtualQuery(addr, &mbi, sizeof(mbi)) != sizeof(mbi) ||
|
|
|
|
!GetModuleFileNameA((HMODULE)mbi.AllocationBase, psz, nChars))
|
|
|
|
{
|
|
|
|
psz[0] = '\0';
|
|
|
|
}
|
|
|
|
return psz;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
/*
|
|
|
|
* @unimplemented
|
|
|
|
*/
|
2004-07-07 16:32:02 +00:00
|
|
|
LONG STDCALL
|
1998-12-04 18:28:13 +00:00
|
|
|
UnhandledExceptionFilter(struct _EXCEPTION_POINTERS *ExceptionInfo)
|
|
|
|
{
|
2004-07-07 16:32:02 +00:00
|
|
|
DWORD RetValue;
|
|
|
|
HANDLE DebugPort = NULL;
|
|
|
|
NTSTATUS ErrCode;
|
|
|
|
static int RescursionTrap = 3;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
if (ExceptionInfo->ExceptionRecord->ExceptionCode == STATUS_ACCESS_VIOLATION &&
|
|
|
|
ExceptionInfo->ExceptionRecord->ExceptionInformation[0])
|
|
|
|
{
|
|
|
|
RetValue = _BasepCheckForReadOnlyResource(
|
|
|
|
ExceptionInfo->ExceptionRecord->ExceptionInformation[1]);
|
|
|
|
if (RetValue == EXCEPTION_CONTINUE_EXECUTION)
|
|
|
|
return EXCEPTION_CONTINUE_EXECUTION;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (--RescursionTrap > 0)
|
|
|
|
{
|
|
|
|
/* Is there a debugger running ? */
|
|
|
|
ErrCode = NtQueryInformationProcess(NtCurrentProcess(), ProcessDebugPort,
|
|
|
|
&DebugPort, sizeof(HANDLE), NULL);
|
|
|
|
if (!NT_SUCCESS(ErrCode) && ErrCode != STATUS_NOT_IMPLEMENTED)
|
|
|
|
{
|
|
|
|
SetLastErrorByStatus(ErrCode);
|
|
|
|
return EXCEPTION_EXECUTE_HANDLER;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DebugPort)
|
|
|
|
{
|
|
|
|
/* Pass the exception to debugger. */
|
|
|
|
DPRINT("Passing exception to debugger\n");
|
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Run unhandled exception handler. */
|
|
|
|
if (GlobalTopLevelExceptionFilter != NULL)
|
|
|
|
{
|
|
|
|
RetValue = GlobalTopLevelExceptionFilter(ExceptionInfo);
|
|
|
|
if (RetValue == EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
return EXCEPTION_EXECUTE_HANDLER;
|
|
|
|
if (RetValue == EXCEPTION_CONTINUE_EXECUTION)
|
|
|
|
return EXCEPTION_CONTINUE_EXECUTION;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (RescursionTrap >= 0 && (GetErrorMode() & SEM_NOGPFAULTERRORBOX) == 0)
|
|
|
|
{
|
|
|
|
#ifdef _X86_
|
|
|
|
PULONG Frame;
|
|
|
|
CHAR szMod[128] = "";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Print a stack trace. */
|
|
|
|
DPRINT1("Unhandled exception\n");
|
|
|
|
DPRINT1("Address:\n");
|
|
|
|
DPRINT1(" %8x %s\n",
|
|
|
|
ExceptionInfo->ExceptionRecord->ExceptionAddress,
|
|
|
|
_module_name_from_addr(ExceptionInfo->ExceptionRecord->ExceptionAddress, szMod, sizeof(szMod)));
|
|
|
|
|
|
|
|
#ifdef _X86_
|
|
|
|
DPRINT1("Frames:\n");
|
|
|
|
Frame = (PULONG)ExceptionInfo->ContextRecord->Ebp;
|
|
|
|
while (Frame[1] != 0 && Frame[1] != 0xdeadbeef)
|
|
|
|
{
|
|
|
|
_module_name_from_addr((const void*)Frame[1], szMod, sizeof(szMod));
|
|
|
|
DPRINT1(" %8x %s\n", Frame[1], szMod);
|
|
|
|
Frame = (PULONG)Frame[0];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returning EXCEPTION_EXECUTE_HANDLER means that the code in
|
|
|
|
* the __except block will be executed. Normally this will end up in a
|
|
|
|
* Terminate process.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return EXCEPTION_EXECUTE_HANDLER;
|
1998-12-04 18:28:13 +00:00
|
|
|
}
|
2000-06-03 14:47:33 +00:00
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2001-03-31 01:17:30 +00:00
|
|
|
VOID
|
|
|
|
STDCALL
|
|
|
|
RaiseException (
|
|
|
|
DWORD dwExceptionCode,
|
|
|
|
DWORD dwExceptionFlags,
|
|
|
|
DWORD nNumberOfArguments,
|
2001-08-03 17:39:09 +00:00
|
|
|
CONST DWORD * lpArguments OPTIONAL
|
2001-03-31 01:17:30 +00:00
|
|
|
)
|
|
|
|
{
|
2001-08-03 17:39:09 +00:00
|
|
|
EXCEPTION_RECORD ExceptionRecord;
|
|
|
|
|
|
|
|
/* Do NOT normalize dwExceptionCode: it will be done in
|
|
|
|
* NTDLL.RtlRaiseException().
|
|
|
|
*/
|
|
|
|
ExceptionRecord.ExceptionCode = dwExceptionCode;
|
|
|
|
ExceptionRecord.ExceptionRecord = NULL;
|
|
|
|
ExceptionRecord.ExceptionAddress = (PVOID) RaiseException;
|
|
|
|
/*
|
|
|
|
* Normalize dwExceptionFlags.
|
|
|
|
*/
|
|
|
|
ExceptionRecord.ExceptionFlags = (dwExceptionFlags & EXCEPTION_NONCONTINUABLE);
|
|
|
|
/*
|
|
|
|
* Normalize nNumberOfArguments.
|
|
|
|
*/
|
|
|
|
if (EXCEPTION_MAXIMUM_PARAMETERS < nNumberOfArguments)
|
|
|
|
{
|
|
|
|
nNumberOfArguments = EXCEPTION_MAXIMUM_PARAMETERS;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If the exception has no argument,
|
|
|
|
* ignore nNumberOfArguments and lpArguments.
|
|
|
|
*/
|
2004-05-29 11:51:33 +00:00
|
|
|
if (NULL == lpArguments)
|
2001-08-03 17:39:09 +00:00
|
|
|
{
|
|
|
|
ExceptionRecord.NumberParameters = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ExceptionRecord.NumberParameters = nNumberOfArguments;
|
|
|
|
for ( nNumberOfArguments = 0;
|
|
|
|
(nNumberOfArguments < ExceptionRecord.NumberParameters);
|
|
|
|
nNumberOfArguments ++
|
|
|
|
)
|
|
|
|
{
|
|
|
|
ExceptionRecord.ExceptionInformation [nNumberOfArguments]
|
|
|
|
= *lpArguments ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RtlRaiseException (& ExceptionRecord);
|
2001-03-31 01:17:30 +00:00
|
|
|
}
|
|
|
|
|
2000-06-03 14:47:33 +00:00
|
|
|
/* EOF */
|