2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Runtime Library
4 * PURPOSE: User-Mode Exception Support
5 * FILE: lib/rtl/exception.c
6 * PROGRAMERS: Alex Ionescu (alex@relsoft.net)
7 * David Welch <welch@cwcom.net>
8 * Skywing <skywing@valhallalegends.com>
9 * KJK::Hyperion <noog@libero.it>
12 /* INCLUDES *****************************************************************/
19 /* GLOBALS *****************************************************************/
21 PRTLP_UNHANDLED_EXCEPTION_FILTER RtlpUnhandledExceptionFilter
;
23 /* FUNCTIONS ***************************************************************/
25 #if !defined(_M_IX86) && !defined(_M_AMD64)
32 RtlRaiseException(IN PEXCEPTION_RECORD ExceptionRecord
)
37 /* Capture the context */
38 RtlCaptureContext(&Context
);
40 /* Save the exception address */
41 ExceptionRecord
->ExceptionAddress
= _ReturnAddress();
43 /* Write the context flag */
44 Context
.ContextFlags
= CONTEXT_FULL
;
46 /* Check if user mode debugger is active */
47 if (RtlpCheckForActiveDebugger())
49 /* Raise an exception immediately */
50 Status
= ZwRaiseException(ExceptionRecord
, &Context
, TRUE
);
54 /* Dispatch the exception and check if we should continue */
55 if (!RtlDispatchException(ExceptionRecord
, &Context
))
57 /* Raise the exception */
58 Status
= ZwRaiseException(ExceptionRecord
, &Context
, FALSE
);
62 /* Continue, go back to previous context */
63 Status
= ZwContinue(&Context
, FALSE
);
67 /* If we returned, raise a status */
68 RtlRaiseStatus(Status
);
77 #pragma warning(disable:4717) // RtlRaiseStatus is recursive by design
85 RtlRaiseStatus(IN NTSTATUS Status
)
87 EXCEPTION_RECORD ExceptionRecord
;
90 /* Capture the context */
91 RtlCaptureContext(&Context
);
93 /* Create an exception record */
94 ExceptionRecord
.ExceptionAddress
= _ReturnAddress();
95 ExceptionRecord
.ExceptionCode
= Status
;
96 ExceptionRecord
.ExceptionRecord
= NULL
;
97 ExceptionRecord
.NumberParameters
= 0;
98 ExceptionRecord
.ExceptionFlags
= EXCEPTION_NONCONTINUABLE
;
100 /* Write the context flag */
101 Context
.ContextFlags
= CONTEXT_FULL
;
103 /* Check if user mode debugger is active */
104 if (RtlpCheckForActiveDebugger())
106 /* Raise an exception immediately */
107 ZwRaiseException(&ExceptionRecord
, &Context
, TRUE
);
111 /* Dispatch the exception */
112 RtlDispatchException(&ExceptionRecord
, &Context
);
114 /* Raise exception if we got here */
115 Status
= ZwRaiseException(&ExceptionRecord
, &Context
, FALSE
);
118 /* If we returned, raise a status */
119 RtlRaiseStatus(Status
);
133 RtlCaptureStackBackTrace(IN ULONG FramesToSkip
,
134 IN ULONG FramesToCapture
,
135 OUT PVOID
*BackTrace
,
136 OUT PULONG BackTraceHash OPTIONAL
)
138 PVOID Frames
[2 * 64];
142 /* Skip a frame for the caller */
145 /* Don't go past the limit */
146 if ((FramesToCapture
+ FramesToSkip
) >= 128) return 0;
148 /* Do the back trace */
149 FrameCount
= RtlWalkFrameChain(Frames
, FramesToCapture
+ FramesToSkip
, 0);
151 /* Make sure we're not skipping all of them */
152 if (FrameCount
<= FramesToSkip
) return 0;
154 /* Loop all the frames */
155 for (i
= 0; i
< FramesToCapture
; i
++)
157 /* Don't go past the limit */
158 if ((FramesToSkip
+ i
) >= FrameCount
) break;
160 /* Save this entry and hash it */
161 BackTrace
[i
] = Frames
[FramesToSkip
+ i
];
162 Hash
+= PtrToUlong(BackTrace
[i
]);
166 if (BackTraceHash
) *BackTraceHash
= Hash
;
168 /* Clear the other entries and return count */
169 RtlFillMemoryUlong(Frames
, 128, 0);
174 * Private helper function to lookup the module name from a given address.
175 * The address can point to anywhere within the module.
178 _module_name_from_addr(const void* addr
, void **module_start_addr
,
179 char* psz
, size_t nChars
)
182 MEMORY_BASIC_INFORMATION mbi
;
183 if (VirtualQuery(addr
, &mbi
, sizeof(mbi
)) != sizeof(mbi
) ||
184 !GetModuleFileNameA((HMODULE
) mbi
.AllocationBase
, psz
, nChars
))
187 *module_start_addr
= 0;
191 *module_start_addr
= (void *) mbi
.AllocationBase
;
196 *module_start_addr
= 0;
203 _dump_context(PCONTEXT pc
)
207 * Print out the CPU registers
209 DbgPrint("CS:EIP %x:%x\n", pc
->SegCs
& 0xffff, pc
->Eip
);
210 DbgPrint("DS %x ES %x FS %x GS %x\n", pc
->SegDs
& 0xffff, pc
->SegEs
& 0xffff,
211 pc
->SegFs
& 0xffff, pc
->SegGs
& 0xfff);
212 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", pc
->Eax
, pc
->Ebx
, pc
->Ecx
);
213 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x ESP: %.8x\n", pc
->Edx
,
214 pc
->Ebp
, pc
->Esi
, pc
->Esp
);
215 DbgPrint("EDI: %.8x EFLAGS: %.8x\n", pc
->Edi
, pc
->EFlags
);
216 #elif defined(_M_AMD64)
217 DbgPrint("CS:RIP %x:%I64x\n", pc
->SegCs
& 0xffff, pc
->Rip
);
218 DbgPrint("DS %x ES %x FS %x GS %x\n", pc
->SegDs
& 0xffff, pc
->SegEs
& 0xffff,
219 pc
->SegFs
& 0xffff, pc
->SegGs
& 0xfff);
220 DbgPrint("RAX: %I64x RBX: %I64x RCX: %I64x RDI: %I64x\n", pc
->Rax
, pc
->Rbx
, pc
->Rcx
, pc
->Rdi
);
221 DbgPrint("RDX: %I64x RBP: %I64x RSI: %I64x RSP: %I64x\n", pc
->Rdx
, pc
->Rbp
, pc
->Rsi
, pc
->Rsp
);
222 DbgPrint("R8: %I64x R9: %I64x R10: %I64x R11: %I64x\n", pc
->R8
, pc
->R9
, pc
->R10
, pc
->R11
);
223 DbgPrint("R12: %I64x R13: %I64x R14: %I64x R15: %I64x\n", pc
->R12
, pc
->R13
, pc
->R14
, pc
->R15
);
224 DbgPrint("EFLAGS: %.8x\n", pc
->EFlags
);
225 #elif defined(_M_ARM)
226 DbgPrint("Pc: %lx Lr: %lx Sp: %lx Cpsr: %lx\n", pc
->Pc
, pc
->Lr
, pc
->Sp
, pc
->Cpsr
);
227 DbgPrint("R0: %lx R1: %lx R2: %lx R3: %lx\n", pc
->R0
, pc
->R1
, pc
->R2
, pc
->R3
);
228 DbgPrint("R4: %lx R5: %lx R6: %lx R7: %lx\n", pc
->R4
, pc
->R5
, pc
->R6
, pc
->R7
);
229 DbgPrint("R8: %lx R9: %lx R10: %lx R11: %lx\n", pc
->R8
, pc
->R9
, pc
->R10
, pc
->R11
);
230 DbgPrint("R12: %lx \n", pc
->R12
);
232 #pragma message ("Unknown architecture")
237 PrintStackTrace(struct _EXCEPTION_POINTERS
*ExceptionInfo
)
240 CHAR szMod
[128] = "";
241 PEXCEPTION_RECORD ExceptionRecord
= ExceptionInfo
->ExceptionRecord
;
242 PCONTEXT ContextRecord
= ExceptionInfo
->ContextRecord
;
244 /* Print a stack trace. */
245 DbgPrint("Unhandled exception\n");
246 DbgPrint("ExceptionCode: %8x\n", ExceptionRecord
->ExceptionCode
);
248 if ((NTSTATUS
) ExceptionRecord
->ExceptionCode
== STATUS_ACCESS_VIOLATION
&&
249 ExceptionRecord
->NumberParameters
== 2)
251 DbgPrint("Faulting Address: %8x\n", ExceptionRecord
->ExceptionInformation
[1]);
254 _dump_context(ContextRecord
);
255 _module_name_from_addr(ExceptionRecord
->ExceptionAddress
, &StartAddr
, szMod
, sizeof(szMod
));
256 DbgPrint("Address:\n %8x+%-8x %s\n",
258 (ULONG_PTR
) ExceptionRecord
->ExceptionAddress
- (ULONG_PTR
) StartAddr
,
261 DbgPrint("Frames:\n");
266 PULONG Frame
= (PULONG
) ContextRecord
->Ebp
;
268 for (i
= 0; Frame
[1] != 0 && Frame
[1] != 0xdeadbeef && i
< 128; i
++)
270 //if (IsBadReadPtr((PVOID) Frame[1], 4))
273 DbgPrint(" %8x%9s %s\n", Frame
[1], "<invalid address>", " ");
277 _module_name_from_addr((const void*) Frame
[1], &StartAddr
,
278 szMod
, sizeof(szMod
));
279 DbgPrint(" %8x+%-8x %s\n",
281 (ULONG_PTR
) Frame
[1] - (ULONG_PTR
) StartAddr
,
285 if (Frame
[0] == 0) break;
286 //if (IsBadReadPtr((PVOID) Frame[0], sizeof(*Frame) * 2))
289 Frame
= (PULONG
) Frame
[0];
292 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
294 DbgPrint("<error dumping stack trace: 0x%x>\n", _SEH2_GetExceptionCode());
306 RtlUnhandledExceptionFilter(IN
struct _EXCEPTION_POINTERS
* ExceptionInfo
)
308 /* This is used by the security cookie checks, and also called externally */
310 PrintStackTrace(ExceptionInfo
);
311 return ERROR_CALL_NOT_IMPLEMENTED
;
319 RtlSetUnhandledExceptionFilter(IN PRTLP_UNHANDLED_EXCEPTION_FILTER TopLevelExceptionFilter
)
321 /* Set the filter which is used by the CriticalSection package */
322 RtlpUnhandledExceptionFilter
= RtlEncodePointer(TopLevelExceptionFilter
);