2005-01-06 13:58:04 +00:00
|
|
|
/* $Id$
|
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"
|
|
|
|
|
2004-12-12 23:03:56 +00:00
|
|
|
LPTOP_LEVEL_EXCEPTION_FILTER GlobalTopLevelExceptionFilter = UnhandledExceptionFilter;
|
1998-12-04 18:28:13 +00:00
|
|
|
|
2005-01-19 20:16:26 +00:00
|
|
|
UINT
|
2006-10-26 04:55:34 +00:00
|
|
|
WINAPI
|
2005-01-19 20:16:26 +00:00
|
|
|
GetErrorMode(VOID)
|
|
|
|
{
|
2006-10-26 04:55:34 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
UINT ErrMode;
|
|
|
|
|
|
|
|
/* Query the current setting */
|
|
|
|
Status = NtQueryInformationProcess(NtCurrentProcess(),
|
|
|
|
ProcessDefaultHardErrorMode,
|
|
|
|
(PVOID)&ErrMode,
|
|
|
|
sizeof(ErrMode),
|
|
|
|
NULL);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* Fail if we couldn't query */
|
|
|
|
SetLastErrorByStatus(Status);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2006-10-26 04:55:34 +00:00
|
|
|
/* Check if NOT failing critical errors was requested */
|
|
|
|
if (ErrMode & SEM_FAILCRITICALERRORS)
|
|
|
|
{
|
|
|
|
/* Mask it out, since the native API works differently */
|
|
|
|
ErrMode &= ~SEM_FAILCRITICALERRORS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* OR it if the caller didn't, due to different native semantics */
|
|
|
|
ErrMode |= SEM_FAILCRITICALERRORS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the mode */
|
|
|
|
return ErrMode;
|
2005-01-19 20:16:26 +00:00
|
|
|
}
|
2003-07-10 18:50:51 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2005-05-09 01:46:57 +00:00
|
|
|
UINT
|
2006-10-26 04:55:34 +00:00
|
|
|
WINAPI
|
|
|
|
SetErrorMode(IN UINT uMode)
|
1998-12-04 18:28:13 +00:00
|
|
|
{
|
2006-10-26 04:55:34 +00:00
|
|
|
UINT PrevErrMode, NewMode;
|
|
|
|
NTSTATUS Status;
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2006-10-26 04:55:34 +00:00
|
|
|
/* Get the previous mode */
|
|
|
|
PrevErrMode = GetErrorMode();
|
|
|
|
NewMode = uMode;
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2006-10-26 04:55:34 +00:00
|
|
|
/* Check if failing critical errors was requested */
|
|
|
|
if (NewMode & SEM_FAILCRITICALERRORS)
|
|
|
|
{
|
|
|
|
/* Mask it out, since the native API works differently */
|
|
|
|
NewMode &= ~SEM_FAILCRITICALERRORS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* OR it if the caller didn't, due to different native semantics */
|
|
|
|
NewMode |= SEM_FAILCRITICALERRORS;
|
|
|
|
}
|
2005-01-19 20:16:26 +00:00
|
|
|
|
2006-10-26 04:55:34 +00:00
|
|
|
/* Set the new mode */
|
|
|
|
Status = NtSetInformationProcess(NtCurrentProcess(),
|
|
|
|
ProcessDefaultHardErrorMode,
|
|
|
|
(PVOID)&NewMode,
|
|
|
|
sizeof(NewMode));
|
|
|
|
if(!NT_SUCCESS(Status)) SetLastErrorByStatus(Status);
|
1998-12-04 18:28:13 +00:00
|
|
|
|
2006-10-26 04:55:34 +00:00
|
|
|
/* Return the previous mode */
|
|
|
|
return PrevErrMode;
|
|
|
|
}
|
2003-07-10 18:50:51 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
1998-12-04 18:28:13 +00:00
|
|
|
LPTOP_LEVEL_EXCEPTION_FILTER
|
2006-10-26 04:55:34 +00:00
|
|
|
WINAPI
|
1998-12-04 18:28:13 +00:00
|
|
|
SetUnhandledExceptionFilter(
|
2006-10-26 04:55:34 +00:00
|
|
|
IN LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter)
|
1998-12-04 18:28:13 +00:00
|
|
|
{
|
2004-12-12 22:39:45 +00:00
|
|
|
return InterlockedExchangePointer(&GlobalTopLevelExceptionFilter,
|
|
|
|
lpTopLevelExceptionFilter);
|
1998-12-04 18:28:13 +00:00
|
|
|
}
|
|
|
|
|
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*
|
2005-05-09 01:46:57 +00:00
|
|
|
_module_name_from_addr(const void* addr, void **module_start_addr,
|
2005-03-07 16:37:49 +00:00
|
|
|
char* psz, size_t nChars)
|
2004-07-07 16:32:02 +00:00
|
|
|
{
|
|
|
|
MEMORY_BASIC_INFORMATION mbi;
|
|
|
|
if (VirtualQuery(addr, &mbi, sizeof(mbi)) != sizeof(mbi) ||
|
|
|
|
!GetModuleFileNameA((HMODULE)mbi.AllocationBase, psz, nChars))
|
|
|
|
{
|
|
|
|
psz[0] = '\0';
|
2005-03-07 16:37:49 +00:00
|
|
|
*module_start_addr = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*module_start_addr = (void *)mbi.AllocationBase;
|
2004-07-07 16:32:02 +00:00
|
|
|
}
|
|
|
|
return psz;
|
|
|
|
}
|
|
|
|
|
2007-09-02 18:05:16 +00:00
|
|
|
#ifdef _M_IX86
|
2004-09-26 16:54:53 +00:00
|
|
|
static VOID
|
|
|
|
_dump_context(PCONTEXT pc)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Print out the CPU registers
|
|
|
|
*/
|
|
|
|
DbgPrint("CS:EIP %x:%x\n", pc->SegCs&0xffff, pc->Eip );
|
|
|
|
DbgPrint("DS %x ES %x FS %x GS %x\n", pc->SegDs&0xffff, pc->SegEs&0xffff,
|
|
|
|
pc->SegFs&0xffff, pc->SegGs&0xfff);
|
|
|
|
DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", pc->Eax, pc->Ebx, pc->Ecx);
|
|
|
|
DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x ESP: %.8x\n", pc->Edx,
|
|
|
|
pc->Ebp, pc->Esi, pc->Esp);
|
|
|
|
DbgPrint("EDI: %.8x EFLAGS: %.8x\n", pc->Edi, pc->EFlags);
|
|
|
|
}
|
2007-09-02 18:05:16 +00:00
|
|
|
#else
|
|
|
|
#warning Unknown architecture
|
|
|
|
static VOID
|
|
|
|
_dump_context(PCONTEXT pc)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
2004-07-07 16:32:02 +00:00
|
|
|
|
2006-05-01 21:33:26 +00:00
|
|
|
static LONG
|
|
|
|
BasepCheckForReadOnlyResource(IN PVOID Ptr)
|
|
|
|
{
|
|
|
|
PVOID Data;
|
|
|
|
ULONG Size, OldProtect;
|
|
|
|
MEMORY_BASIC_INFORMATION mbi;
|
|
|
|
NTSTATUS Status;
|
|
|
|
LONG Ret = EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
|
|
|
|
/* Check if it was an attempt to write to a read-only image section! */
|
|
|
|
Status = NtQueryVirtualMemory(NtCurrentProcess(),
|
|
|
|
Ptr,
|
|
|
|
MemoryBasicInformation,
|
|
|
|
&mbi,
|
|
|
|
sizeof(mbi),
|
|
|
|
NULL);
|
|
|
|
if (NT_SUCCESS(Status) &&
|
|
|
|
mbi.Protect == PAGE_READONLY && mbi.Type == MEM_IMAGE)
|
|
|
|
{
|
|
|
|
/* Attempt to treat it as a resource section. We need to
|
|
|
|
use SEH here because we don't know if it's actually a
|
|
|
|
resource mapping */
|
|
|
|
|
|
|
|
_SEH_TRY
|
|
|
|
{
|
|
|
|
Data = RtlImageDirectoryEntryToData(mbi.AllocationBase,
|
|
|
|
TRUE,
|
|
|
|
IMAGE_DIRECTORY_ENTRY_RESOURCE,
|
|
|
|
&Size);
|
|
|
|
|
|
|
|
if (Data != NULL &&
|
|
|
|
(ULONG_PTR)Ptr >= (ULONG_PTR)Data &&
|
|
|
|
(ULONG_PTR)Ptr < (ULONG_PTR)Data + Size)
|
|
|
|
{
|
|
|
|
/* The user tried to write into the resources. Make the page
|
|
|
|
writable... */
|
|
|
|
Size = 1;
|
|
|
|
Status = NtProtectVirtualMemory(NtCurrentProcess(),
|
|
|
|
&Ptr,
|
|
|
|
&Size,
|
|
|
|
PAGE_READWRITE,
|
|
|
|
&OldProtect);
|
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
Ret = EXCEPTION_CONTINUE_EXECUTION;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_SEH_HANDLE
|
|
|
|
{
|
|
|
|
}
|
|
|
|
_SEH_END;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2006-05-01 21:33:26 +00:00
|
|
|
LONG RetValue;
|
2004-07-07 16:32:02 +00:00
|
|
|
HANDLE DebugPort = NULL;
|
|
|
|
NTSTATUS ErrCode;
|
|
|
|
|
|
|
|
if (ExceptionInfo->ExceptionRecord->ExceptionCode == STATUS_ACCESS_VIOLATION &&
|
|
|
|
ExceptionInfo->ExceptionRecord->ExceptionInformation[0])
|
|
|
|
{
|
2006-05-01 21:33:26 +00:00
|
|
|
/* Change the protection on some write attempts, some InstallShield setups
|
|
|
|
have this bug */
|
|
|
|
RetValue = BasepCheckForReadOnlyResource(
|
|
|
|
(PVOID)ExceptionInfo->ExceptionRecord->ExceptionInformation[1]);
|
2004-07-07 16:32:02 +00:00
|
|
|
if (RetValue == EXCEPTION_CONTINUE_EXECUTION)
|
|
|
|
return EXCEPTION_CONTINUE_EXECUTION;
|
|
|
|
}
|
|
|
|
|
2004-12-13 13:32:24 +00:00
|
|
|
/* 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)
|
2004-07-07 16:32:02 +00:00
|
|
|
{
|
2004-12-13 13:32:24 +00:00
|
|
|
/* Pass the exception to debugger. */
|
|
|
|
DPRINT("Passing exception to debugger\n");
|
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
2004-07-07 16:32:02 +00:00
|
|
|
}
|
|
|
|
|
2005-01-19 20:16:26 +00:00
|
|
|
if ((GetErrorMode() & SEM_NOGPFAULTERRORBOX) == 0)
|
2004-07-07 16:32:02 +00:00
|
|
|
{
|
|
|
|
#ifdef _X86_
|
|
|
|
PULONG Frame;
|
2007-09-02 18:05:16 +00:00
|
|
|
#endif
|
2005-03-07 16:37:49 +00:00
|
|
|
PVOID StartAddr;
|
2004-07-07 16:32:02 +00:00
|
|
|
CHAR szMod[128] = "";
|
|
|
|
|
|
|
|
/* Print a stack trace. */
|
2005-03-07 16:37:49 +00:00
|
|
|
DbgPrint("Unhandled exception\n");
|
2005-08-30 16:28:36 +00:00
|
|
|
DbgPrint("ExceptionCode: %8x\n", ExceptionInfo->ExceptionRecord->ExceptionCode);
|
|
|
|
if (ExceptionInfo->ExceptionRecord->ExceptionCode == STATUS_ACCESS_VIOLATION &&
|
|
|
|
ExceptionInfo->ExceptionRecord->NumberParameters == 2)
|
|
|
|
{
|
|
|
|
DbgPrint("Faulting Address: %8x\n", ExceptionInfo->ExceptionRecord->ExceptionInformation[1]);
|
|
|
|
}
|
|
|
|
DbgPrint("Address: %8x %s\n",
|
2004-07-07 16:32:02 +00:00
|
|
|
ExceptionInfo->ExceptionRecord->ExceptionAddress,
|
2005-03-07 16:37:49 +00:00
|
|
|
_module_name_from_addr(ExceptionInfo->ExceptionRecord->ExceptionAddress, &StartAddr, szMod, sizeof(szMod)));
|
2004-09-26 16:54:53 +00:00
|
|
|
_dump_context ( ExceptionInfo->ContextRecord );
|
2004-07-07 16:32:02 +00:00
|
|
|
#ifdef _X86_
|
2005-03-07 16:37:49 +00:00
|
|
|
DbgPrint("Frames:\n");
|
2005-05-05 15:38:32 +00:00
|
|
|
_SEH_TRY
|
2004-07-07 16:32:02 +00:00
|
|
|
{
|
2005-05-05 15:38:32 +00:00
|
|
|
Frame = (PULONG)ExceptionInfo->ContextRecord->Ebp;
|
|
|
|
while (Frame[1] != 0 && Frame[1] != 0xdeadbeef)
|
|
|
|
{
|
|
|
|
if (IsBadReadPtr((PVOID)Frame[1], 4)) {
|
|
|
|
DbgPrint(" %8x%9s %s\n", Frame[1], "<invalid address>"," ");
|
|
|
|
} else {
|
|
|
|
_module_name_from_addr((const void*)Frame[1], &StartAddr,
|
|
|
|
szMod, sizeof(szMod));
|
|
|
|
DbgPrint(" %8x+%-8x %s\n",
|
|
|
|
(PVOID)StartAddr,
|
|
|
|
(ULONG_PTR)Frame[1] - (ULONG_PTR)StartAddr, szMod);
|
|
|
|
}
|
|
|
|
if (IsBadReadPtr((PVOID)Frame[0], sizeof(*Frame) * 2)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Frame = (PULONG)Frame[0];
|
2004-08-22 18:49:11 +00:00
|
|
|
}
|
2004-07-07 16:32:02 +00:00
|
|
|
}
|
2005-05-05 15:38:32 +00:00
|
|
|
_SEH_HANDLE
|
|
|
|
{
|
|
|
|
DbgPrint("<error dumping stack trace: 0x%x>\n", _SEH_GetExceptionCode());
|
|
|
|
}
|
|
|
|
_SEH_END;
|
2004-07-07 16:32:02 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-05-09 01:46:57 +00:00
|
|
|
* Returning EXCEPTION_EXECUTE_HANDLER means that the code in
|
2004-07-07 16:32:02 +00:00
|
|
|
* the __except block will be executed. Normally this will end up in a
|
|
|
|
* Terminate process.
|
|
|
|
*/
|
|
|
|
|
2005-05-09 01:46:57 +00:00
|
|
|
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
|
2006-10-26 04:55:34 +00:00
|
|
|
WINAPI
|
|
|
|
RaiseException(IN DWORD dwExceptionCode,
|
|
|
|
IN DWORD dwExceptionFlags,
|
|
|
|
IN DWORD nNumberOfArguments,
|
|
|
|
IN CONST ULONG_PTR *lpArguments OPTIONAL)
|
2001-03-31 01:17:30 +00:00
|
|
|
{
|
2006-10-26 04:55:34 +00:00
|
|
|
EXCEPTION_RECORD ExceptionRecord;
|
|
|
|
|
|
|
|
/* Setup the exception record */
|
|
|
|
ExceptionRecord.ExceptionCode = dwExceptionCode;
|
|
|
|
ExceptionRecord.ExceptionRecord = NULL;
|
|
|
|
ExceptionRecord.ExceptionAddress = (PVOID)RaiseException;
|
|
|
|
ExceptionRecord.ExceptionFlags = dwExceptionFlags & EXCEPTION_NONCONTINUABLE;
|
|
|
|
|
|
|
|
/* Check if we have arguments */
|
|
|
|
if (!lpArguments)
|
|
|
|
{
|
|
|
|
/* We don't */
|
|
|
|
ExceptionRecord.NumberParameters = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We do, normalize the count */
|
|
|
|
if (nNumberOfArguments > EXCEPTION_MAXIMUM_PARAMETERS)
|
|
|
|
{
|
|
|
|
nNumberOfArguments = EXCEPTION_MAXIMUM_PARAMETERS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the count of parameters */
|
|
|
|
ExceptionRecord.NumberParameters = nNumberOfArguments;
|
|
|
|
|
|
|
|
/* Loop each parameter */
|
|
|
|
for (nNumberOfArguments = 0;
|
|
|
|
(nNumberOfArguments < ExceptionRecord.NumberParameters);
|
|
|
|
nNumberOfArguments ++)
|
|
|
|
{
|
|
|
|
/* Copy the exception information */
|
|
|
|
ExceptionRecord.ExceptionInformation[nNumberOfArguments] =
|
|
|
|
*lpArguments++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Raise the exception */
|
|
|
|
RtlRaiseException(&ExceptionRecord);
|
2001-03-31 01:17:30 +00:00
|
|
|
}
|
|
|
|
|
2000-06-03 14:47:33 +00:00
|
|
|
/* EOF */
|