Yet another attempt at fixing CW_USEDEFAULT handling.
[wine.git] / win32 / except.c
blob550a6ea5e2de29cf17ec24c7123b59cc737deafe
1 /*
2 * Win32 exception functions
4 * Copyright (c) 1996 Onno Hovers, (onno@stack.urc.tue.nl)
5 * Copyright (c) 1999 Alexandre Julliard
7 * Notes:
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
12 * reasons for this:
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
21 * documented!
25 #include <assert.h>
26 #include <stdio.h>
27 #include "windef.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "winerror.h"
31 #include "ntddk.h"
32 #include "wine/exception.h"
33 #include "ldt.h"
34 #include "callback.h"
35 #include "process.h"
36 #include "thread.h"
37 #include "stackframe.h"
38 #include "server.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;
57 if (nbargs && args)
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();
75 PDB* pdb = PROCESS_Current();
76 char format[256];
77 char buffer[256];
78 HKEY hDbgConf;
79 DWORD bAuto = FALSE;
80 DWORD ret = EXCEPTION_EXECUTE_HANDLER;
82 /* send a last chance event to the debugger */
83 req->record = *epointers->ExceptionRecord;
84 req->first = 0;
85 req->context = *epointers->ContextRecord;
86 if (!server_call_noerr( REQ_EXCEPTION_EVENT )) *epointers->ContextRecord = req->context;
87 if (req->status == DBG_CONTINUE) return EXCEPTION_CONTINUE_EXECUTION;
89 if (pdb->top_filter)
91 DWORD ret = pdb->top_filter( epointers );
92 if (ret != EXCEPTION_CONTINUE_SEARCH) return ret;
95 /* FIXME: Should check the current error mode */
97 if (!RegOpenKeyA(HKEY_LOCAL_MACHINE,
98 "Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug",
99 &hDbgConf)) {
100 DWORD type;
101 DWORD count;
103 count = sizeof(format);
104 if (RegQueryValueExA(hDbgConf, "Debugger", 0, &type, format, &count))
105 format[0] = 0;
107 count = sizeof(bAuto);
108 if (RegQueryValueExA(hDbgConf, "Auto", 0, &type, (char*)&bAuto, &count))
109 bAuto = FALSE;
111 RegCloseKey(hDbgConf);
112 } else {
113 /* format[0] = 0; */
114 strcpy(format, "debugger/winedbg %ld %ld");
117 if (!bAuto && Callout.MessageBoxA) {
118 sprintf( buffer, "Unhandled exception 0x%08lx at address 0x%08lx.\n"
119 "Do you wish to debug it ?",
120 epointers->ExceptionRecord->ExceptionCode,
121 (DWORD)epointers->ExceptionRecord->ExceptionAddress );
122 if (Callout.MessageBoxA( 0, buffer, "Error", MB_YESNO | MB_ICONHAND ) == IDNO) {
123 TRACE("Killing process\n");
124 return EXCEPTION_EXECUTE_HANDLER;
128 if (format[0]) {
129 HANDLE hEvent;
130 PROCESS_INFORMATION info;
131 STARTUPINFOA startup;
133 TRACE("Starting debugger (fmt=%s)\n", format);
134 hEvent = ConvertToGlobalHandle(CreateEventA(NULL, FALSE, FALSE, NULL));
135 sprintf(buffer, format, GetCurrentProcessId(), hEvent);
136 memset(&startup, 0, sizeof(startup));
137 startup.cb = sizeof(startup);
138 startup.dwFlags = STARTF_USESHOWWINDOW;
139 startup.wShowWindow = SW_SHOWNORMAL;
140 if (CreateProcessA(NULL, buffer, NULL, NULL,
141 TRUE, 0, NULL, NULL, &startup, &info)) {
142 WaitForSingleObject(hEvent, INFINITE);
143 ret = EXCEPTION_CONTINUE_SEARCH;
144 } else {
145 ERR("Couldn't start debugger (%s)\n", buffer);
147 CloseHandle(hEvent);
148 } else {
149 ERR("No standard debugger defined in the registry => no debugging session\n");
152 return ret;
156 /***********************************************************************
157 * SetUnhandledExceptionFilter (KERNEL32.516)
159 LPTOP_LEVEL_EXCEPTION_FILTER WINAPI SetUnhandledExceptionFilter(
160 LPTOP_LEVEL_EXCEPTION_FILTER filter )
162 PDB *pdb = PROCESS_Current();
163 LPTOP_LEVEL_EXCEPTION_FILTER old = pdb->top_filter;
164 pdb->top_filter = filter;
165 return old;
169 /**************************************************************************
170 * FatalAppExit16 (KERNEL.137)
172 void WINAPI FatalAppExit16( UINT16 action, LPCSTR str )
174 WARN("AppExit\n");
175 FatalAppExitA( action, str );
179 /**************************************************************************
180 * FatalAppExitA (KERNEL32.108)
182 void WINAPI FatalAppExitA( UINT action, LPCSTR str )
184 WARN("AppExit\n");
185 Callout.MessageBoxA( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
186 ExitProcess(0);
190 /**************************************************************************
191 * FatalAppExitW (KERNEL32.109)
193 void WINAPI FatalAppExitW( UINT action, LPCWSTR str )
195 WARN("AppExit\n");
196 Callout.MessageBoxW( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
197 ExitProcess(0);
201 /*************************************************************
202 * WINE_exception_handler
204 * Exception handler for exception blocks declared in Wine code.
206 DWORD WINE_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
207 CONTEXT *context, LPVOID pdispatcher )
209 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
211 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
212 return ExceptionContinueSearch;
213 if (wine_frame->u.filter)
215 EXCEPTION_POINTERS ptrs;
216 ptrs.ExceptionRecord = record;
217 ptrs.ContextRecord = context;
218 switch(wine_frame->u.filter( &ptrs ))
220 case EXCEPTION_CONTINUE_SEARCH:
221 return ExceptionContinueSearch;
222 case EXCEPTION_CONTINUE_EXECUTION:
223 return ExceptionContinueExecution;
224 case EXCEPTION_EXECUTE_HANDLER:
225 break;
226 default:
227 MESSAGE( "Invalid return value from exception filter\n" );
228 assert( FALSE );
231 /* hack to make GetExceptionCode() work in handler */
232 wine_frame->ExceptionCode = record->ExceptionCode;
233 wine_frame->ExceptionRecord = wine_frame;
235 RtlUnwind( frame, 0, record, 0 );
236 EXC_pop_frame( frame );
237 longjmp( wine_frame->jmp, 1 );
241 /*************************************************************
242 * WINE_finally_handler
244 * Exception handler for try/finally blocks declared in Wine code.
246 DWORD WINE_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
247 CONTEXT *context, LPVOID pdispatcher )
249 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
251 if (!(record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
252 return ExceptionContinueSearch;
253 wine_frame->u.finally_func( FALSE );
254 return ExceptionContinueSearch;