2 * Win32 exception functions
4 * Copyright (c) 1996 Onno Hovers, (onno@stack.urc.tue.nl)
5 * Copyright (c) 1999 Alexandre Julliard
8 * What really happens behind the scenes of those new
9 * __try{...}__except(..){....} and
10 * __try{...}__finally{...}
11 * statements is simply not documented by Microsoft. There could be different
13 * One reason could be that they try to hide the fact that exception
14 * handling in Win32 looks almost the same as in OS/2 2.x.
15 * Another reason could be that Microsoft does not want others to write
16 * binary compatible implementations of the Win32 API (like us).
18 * Whatever the reason, THIS SUCKS!! Ensuring portabilty or future
19 * compatability may be valid reasons to keep some things undocumented.
20 * But exception handling is so basic to Win32 that it should be
29 #include "wine/exception.h"
33 #include "stackframe.h"
34 #include "debugtools.h"
36 DEFAULT_DEBUG_CHANNEL(seh)
39 /*******************************************************************
40 * RaiseException (KERNEL32.418)
42 void WINAPI RaiseException( DWORD code, DWORD flags, DWORD nbargs, const LPDWORD args )
44 EXCEPTION_RECORD record;
46 /* Compose an exception record */
48 record.ExceptionCode = code;
49 record.ExceptionFlags = flags & EH_NONCONTINUABLE;
50 record.ExceptionRecord = NULL;
51 record.ExceptionAddress = RaiseException;
54 if (nbargs > EXCEPTION_MAXIMUM_PARAMETERS) nbargs = EXCEPTION_MAXIMUM_PARAMETERS;
55 record.NumberParameters = nbargs;
56 memcpy( record.ExceptionInformation, args, nbargs * sizeof(*args) );
58 else record.NumberParameters = 0;
60 RtlRaiseException( &record );
64 /*******************************************************************
65 * UnhandledExceptionFilter (KERNEL32.537)
67 DWORD WINAPI UnhandledExceptionFilter(PEXCEPTION_POINTERS epointers)
70 PDB *pdb = PROCESS_Current();
72 if (pdb->flags & PDB32_DEBUGGED) return EXCEPTION_CONTINUE_SEARCH;
76 DWORD ret = pdb->top_filter( epointers );
77 if (ret != EXCEPTION_CONTINUE_SEARCH) return ret;
80 /* FIXME: does not belong here */
81 if (((*EXC_GetDebugEventHook())( epointers->ExceptionRecord,
82 epointers->ContextRecord, FALSE )) == DBG_CONTINUE)
83 return EXCEPTION_CONTINUE_EXECUTION;
85 /* FIXME: Should check the current error mode */
87 sprintf( message, "Unhandled exception 0x%08lx at address 0x%08lx.",
88 epointers->ExceptionRecord->ExceptionCode,
89 (DWORD)epointers->ExceptionRecord->ExceptionAddress );
90 MessageBoxA( 0, message, "Error", MB_OK | MB_ICONHAND );
91 return EXCEPTION_EXECUTE_HANDLER;
95 /*************************************************************
96 * SetUnhandledExceptionFilter (KERNEL32.516)
98 LPTOP_LEVEL_EXCEPTION_FILTER WINAPI SetUnhandledExceptionFilter(
99 LPTOP_LEVEL_EXCEPTION_FILTER filter )
101 PDB *pdb = PROCESS_Current();
102 LPTOP_LEVEL_EXCEPTION_FILTER old = pdb->top_filter;
103 pdb->top_filter = filter;
108 /*************************************************************
109 * WINE_exception_handler
111 * Exception handler for exception blocks declared in Wine code.
113 DWORD WINAPI WINE_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
114 CONTEXT *context, LPVOID pdispatcher )
116 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
118 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
119 return ExceptionContinueSearch;
120 if (wine_frame->u.filter)
122 EXCEPTION_POINTERS ptrs;
123 ptrs.ExceptionRecord = record;
124 ptrs.ContextRecord = context;
125 switch(wine_frame->u.filter( &ptrs ))
127 case EXCEPTION_CONTINUE_SEARCH:
128 return ExceptionContinueSearch;
129 case EXCEPTION_CONTINUE_EXECUTION:
130 return ExceptionContinueExecution;
131 case EXCEPTION_EXECUTE_HANDLER:
134 MESSAGE( "Invalid return value from exception filter\n" );
138 /* hack to make GetExceptionCode() work in handler */
139 wine_frame->ExceptionCode = record->ExceptionCode;
140 wine_frame->ExceptionRecord = wine_frame;
142 RtlUnwind( frame, 0, record, 0 );
143 EXC_pop_frame( frame );
144 longjmp( wine_frame->jmp, 1 );
148 /*************************************************************
149 * WINE_finally_handler
151 * Exception handler for try/finally blocks declared in Wine code.
153 DWORD WINAPI WINE_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
154 CONTEXT *context, LPVOID pdispatcher )
156 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
158 if (!(record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
159 return ExceptionContinueSearch;
160 wine_frame->u.finally_func( FALSE );
161 return ExceptionContinueSearch;