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
32 #include "wine/exception.h"
37 #include "stackframe.h"
39 #include "debugtools.h"
41 DEFAULT_DEBUG_CHANNEL(seh
);
44 /*******************************************************************
45 * RaiseException (KERNEL32.418)
47 void WINAPI
RaiseException( DWORD code
, DWORD flags
, DWORD nbargs
, const LPDWORD args
)
49 EXCEPTION_RECORD record
;
51 /* Compose an exception record */
53 record
.ExceptionCode
= code
;
54 record
.ExceptionFlags
= flags
& EH_NONCONTINUABLE
;
55 record
.ExceptionRecord
= NULL
;
56 record
.ExceptionAddress
= RaiseException
;
59 if (nbargs
> EXCEPTION_MAXIMUM_PARAMETERS
) nbargs
= EXCEPTION_MAXIMUM_PARAMETERS
;
60 record
.NumberParameters
= nbargs
;
61 memcpy( record
.ExceptionInformation
, args
, nbargs
* sizeof(*args
) );
63 else record
.NumberParameters
= 0;
65 RtlRaiseException( &record
);
69 /*******************************************************************
70 * UnhandledExceptionFilter (KERNEL32.537)
72 DWORD WINAPI
UnhandledExceptionFilter(PEXCEPTION_POINTERS epointers
)
74 struct exception_event_request
*req
= get_req_buffer();
76 PDB
*pdb
= PROCESS_Current();
78 /* send a last chance event to the debugger */
79 req
->record
= *epointers
->ExceptionRecord
;
81 req
->context
= *epointers
->ContextRecord
;
82 if (!server_call_noerr( REQ_EXCEPTION_EVENT
)) *epointers
->ContextRecord
= req
->context
;
83 if (req
->status
== DBG_CONTINUE
) return EXCEPTION_CONTINUE_EXECUTION
;
87 DWORD ret
= pdb
->top_filter( epointers
);
88 if (ret
!= EXCEPTION_CONTINUE_SEARCH
) return ret
;
91 /* FIXME: Should check the current error mode */
93 sprintf( message
, "Unhandled exception 0x%08lx at address 0x%08lx.",
94 epointers
->ExceptionRecord
->ExceptionCode
,
95 (DWORD
)epointers
->ExceptionRecord
->ExceptionAddress
);
96 Callout
.MessageBoxA( 0, message
, "Error", MB_OK
| MB_ICONHAND
);
97 return EXCEPTION_EXECUTE_HANDLER
;
101 /*************************************************************
102 * SetUnhandledExceptionFilter (KERNEL32.516)
104 LPTOP_LEVEL_EXCEPTION_FILTER WINAPI
SetUnhandledExceptionFilter(
105 LPTOP_LEVEL_EXCEPTION_FILTER filter
)
107 PDB
*pdb
= PROCESS_Current();
108 LPTOP_LEVEL_EXCEPTION_FILTER old
= pdb
->top_filter
;
109 pdb
->top_filter
= filter
;
114 /**************************************************************************
115 * FatalAppExit16 (KERNEL.137)
117 void WINAPI
FatalAppExit16( UINT16 action
, LPCSTR str
)
120 FatalAppExitA( action
, str
);
124 /**************************************************************************
125 * FatalAppExitA (KERNEL32.108)
127 void WINAPI
FatalAppExitA( UINT action
, LPCSTR str
)
130 Callout
.MessageBoxA( 0, str
, NULL
, MB_SYSTEMMODAL
| MB_OK
);
135 /**************************************************************************
136 * FatalAppExitW (KERNEL32.109)
138 void WINAPI
FatalAppExitW( UINT action
, LPCWSTR str
)
141 Callout
.MessageBoxW( 0, str
, NULL
, MB_SYSTEMMODAL
| MB_OK
);
146 /*************************************************************
147 * WINE_exception_handler
149 * Exception handler for exception blocks declared in Wine code.
151 DWORD
WINE_exception_handler( EXCEPTION_RECORD
*record
, EXCEPTION_FRAME
*frame
,
152 CONTEXT
*context
, LPVOID pdispatcher
)
154 __WINE_FRAME
*wine_frame
= (__WINE_FRAME
*)frame
;
156 if (record
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
| EH_NESTED_CALL
))
157 return ExceptionContinueSearch
;
158 if (wine_frame
->u
.filter
)
160 EXCEPTION_POINTERS ptrs
;
161 ptrs
.ExceptionRecord
= record
;
162 ptrs
.ContextRecord
= context
;
163 switch(wine_frame
->u
.filter( &ptrs
))
165 case EXCEPTION_CONTINUE_SEARCH
:
166 return ExceptionContinueSearch
;
167 case EXCEPTION_CONTINUE_EXECUTION
:
168 return ExceptionContinueExecution
;
169 case EXCEPTION_EXECUTE_HANDLER
:
172 MESSAGE( "Invalid return value from exception filter\n" );
176 /* hack to make GetExceptionCode() work in handler */
177 wine_frame
->ExceptionCode
= record
->ExceptionCode
;
178 wine_frame
->ExceptionRecord
= wine_frame
;
180 RtlUnwind( frame
, 0, record
, 0 );
181 EXC_pop_frame( frame
);
182 longjmp( wine_frame
->jmp
, 1 );
186 /*************************************************************
187 * WINE_finally_handler
189 * Exception handler for try/finally blocks declared in Wine code.
191 DWORD
WINE_finally_handler( EXCEPTION_RECORD
*record
, EXCEPTION_FRAME
*frame
,
192 CONTEXT
*context
, LPVOID pdispatcher
)
194 __WINE_FRAME
*wine_frame
= (__WINE_FRAME
*)frame
;
196 if (!(record
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
)))
197 return ExceptionContinueSearch
;
198 wine_frame
->u
.finally_func( FALSE
);
199 return ExceptionContinueSearch
;