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"
34 #include "stackframe.h"
36 #include "debugtools.h"
38 DEFAULT_DEBUG_CHANNEL(seh
);
41 /*******************************************************************
42 * RaiseException (KERNEL32.418)
44 void WINAPI
RaiseException( DWORD code
, DWORD flags
, DWORD nbargs
, const LPDWORD args
)
46 EXCEPTION_RECORD record
;
48 /* Compose an exception record */
50 record
.ExceptionCode
= code
;
51 record
.ExceptionFlags
= flags
& EH_NONCONTINUABLE
;
52 record
.ExceptionRecord
= NULL
;
53 record
.ExceptionAddress
= RaiseException
;
56 if (nbargs
> EXCEPTION_MAXIMUM_PARAMETERS
) nbargs
= EXCEPTION_MAXIMUM_PARAMETERS
;
57 record
.NumberParameters
= nbargs
;
58 memcpy( record
.ExceptionInformation
, args
, nbargs
* sizeof(*args
) );
60 else record
.NumberParameters
= 0;
62 RtlRaiseException( &record
);
66 /*******************************************************************
67 * UnhandledExceptionFilter (KERNEL32.537)
69 DWORD WINAPI
UnhandledExceptionFilter(PEXCEPTION_POINTERS epointers
)
71 PDB
* pdb
= PROCESS_Current();
76 DWORD ret
= EXCEPTION_EXECUTE_HANDLER
;
79 /* send a last chance event to the debugger */
82 struct exception_event_request
*req
= server_alloc_req( sizeof(*req
),
83 sizeof(EXCEPTION_RECORD
)+sizeof(CONTEXT
) );
84 CONTEXT
*context_ptr
= server_data_ptr(req
);
85 EXCEPTION_RECORD
*rec_ptr
= (EXCEPTION_RECORD
*)(context_ptr
+ 1);
87 *rec_ptr
= *epointers
->ExceptionRecord
;
88 *context_ptr
= *epointers
->ContextRecord
;
89 if (!server_call_noerr( REQ_EXCEPTION_EVENT
)) *epointers
->ContextRecord
= *context_ptr
;
97 return EXCEPTION_CONTINUE_EXECUTION
;
98 case DBG_EXCEPTION_NOT_HANDLED
:
99 TerminateProcess( GetCurrentProcess(), epointers
->ExceptionRecord
->ExceptionCode
);
100 break; /* not reached */
101 case 0: /* no debugger is present */
104 FIXME("Unsupported yet debug continue value %d (please report)\n", status
);
109 DWORD ret
= pdb
->top_filter( epointers
);
110 if (ret
!= EXCEPTION_CONTINUE_SEARCH
) return ret
;
113 /* FIXME: Should check the current error mode */
115 if (!RegOpenKeyA(HKEY_LOCAL_MACHINE
,
116 "Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug",
121 count
= sizeof(format
);
122 if (RegQueryValueExA(hDbgConf
, "Debugger", 0, &type
, format
, &count
))
125 count
= sizeof(bAuto
);
126 if (RegQueryValueExA(hDbgConf
, "Auto", 0, &type
, (char*)&bAuto
, &count
))
129 RegCloseKey(hDbgConf
);
132 strcpy(format
, "debugger/winedbg %ld %ld");
135 if (!bAuto
&& Callout
.MessageBoxA
) {
136 sprintf( buffer
, "Unhandled exception 0x%08lx at address 0x%08lx.\n"
137 "Do you wish to debug it ?",
138 epointers
->ExceptionRecord
->ExceptionCode
,
139 (DWORD
)epointers
->ExceptionRecord
->ExceptionAddress
);
140 if (Callout
.MessageBoxA( 0, buffer
, "Error", MB_YESNO
| MB_ICONHAND
) == IDNO
) {
141 TRACE("Killing process\n");
142 return EXCEPTION_EXECUTE_HANDLER
;
148 PROCESS_INFORMATION info
;
149 STARTUPINFOA startup
;
150 OBJECT_ATTRIBUTES attr
;
152 attr
.Length
= sizeof(attr
);
153 attr
.RootDirectory
= 0;
154 attr
.Attributes
= OBJ_INHERIT
;
155 attr
.ObjectName
= NULL
;
156 attr
.SecurityDescriptor
= NULL
;
157 attr
.SecurityQualityOfService
= NULL
;
159 TRACE("Starting debugger (fmt=%s)\n", format
);
160 NtCreateEvent( &hEvent
, EVENT_ALL_ACCESS
, &attr
, FALSE
, FALSE
);
161 sprintf(buffer
, format
, GetCurrentProcessId(), hEvent
);
162 memset(&startup
, 0, sizeof(startup
));
163 startup
.cb
= sizeof(startup
);
164 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
165 startup
.wShowWindow
= SW_SHOWNORMAL
;
166 if (CreateProcessA(NULL
, buffer
, NULL
, NULL
,
167 TRUE
, 0, NULL
, NULL
, &startup
, &info
)) {
168 WaitForSingleObject(hEvent
, INFINITE
);
169 ret
= EXCEPTION_CONTINUE_SEARCH
;
171 ERR("Couldn't start debugger (%s) (%ld)\n", buffer
, GetLastError());
175 ERR("No standard debugger defined in the registry => no debugging session\n");
182 /***********************************************************************
183 * SetUnhandledExceptionFilter (KERNEL32.516)
185 LPTOP_LEVEL_EXCEPTION_FILTER WINAPI
SetUnhandledExceptionFilter(
186 LPTOP_LEVEL_EXCEPTION_FILTER filter
)
188 PDB
*pdb
= PROCESS_Current();
189 LPTOP_LEVEL_EXCEPTION_FILTER old
= pdb
->top_filter
;
190 pdb
->top_filter
= filter
;
195 /**************************************************************************
196 * FatalAppExit16 (KERNEL.137)
198 void WINAPI
FatalAppExit16( UINT16 action
, LPCSTR str
)
201 FatalAppExitA( action
, str
);
205 /**************************************************************************
206 * FatalAppExitA (KERNEL32.108)
208 void WINAPI
FatalAppExitA( UINT action
, LPCSTR str
)
211 Callout
.MessageBoxA( 0, str
, NULL
, MB_SYSTEMMODAL
| MB_OK
);
216 /**************************************************************************
217 * FatalAppExitW (KERNEL32.109)
219 void WINAPI
FatalAppExitW( UINT action
, LPCWSTR str
)
222 Callout
.MessageBoxW( 0, str
, NULL
, MB_SYSTEMMODAL
| MB_OK
);