- Separate application calls to ShowOwnedPopups from Wine calls (in
[wine/wine64.git] / win32 / except.c
blob22cf918a9aa7391833dc778d70510dcb2bed85a9
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 <stdio.h>
26 #include "windef.h"
27 #include "winerror.h"
28 #include "ntddk.h"
29 #include "wine/exception.h"
30 #include "ldt.h"
31 #include "callback.h"
32 #include "process.h"
33 #include "thread.h"
34 #include "stackframe.h"
35 #include "server.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;
54 if (nbargs && args)
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 struct exception_event_request *req = get_req_buffer();
72 PDB* pdb = PROCESS_Current();
73 char format[256];
74 char buffer[256];
75 HKEY hDbgConf;
76 DWORD bAuto = FALSE;
77 DWORD ret = EXCEPTION_EXECUTE_HANDLER;
79 /* send a last chance event to the debugger */
80 req->record = *epointers->ExceptionRecord;
81 req->first = 0;
82 req->context = *epointers->ContextRecord;
83 if (!server_call_noerr( REQ_EXCEPTION_EVENT )) *epointers->ContextRecord = req->context;
84 switch (req->status)
86 case DBG_CONTINUE:
87 return EXCEPTION_CONTINUE_EXECUTION;
88 case DBG_EXCEPTION_NOT_HANDLED:
89 TerminateProcess( GetCurrentProcess(), epointers->ExceptionRecord->ExceptionCode );
90 break; /* not reached */
91 case 0: /* no debugger is present */
92 break;
93 default:
94 FIXME("Unsupported yet debug continue value %d (please report)\n", req->status);
97 if (pdb->top_filter)
99 DWORD ret = pdb->top_filter( epointers );
100 if (ret != EXCEPTION_CONTINUE_SEARCH) return ret;
103 /* FIXME: Should check the current error mode */
105 if (!RegOpenKeyA(HKEY_LOCAL_MACHINE,
106 "Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug",
107 &hDbgConf)) {
108 DWORD type;
109 DWORD count;
111 count = sizeof(format);
112 if (RegQueryValueExA(hDbgConf, "Debugger", 0, &type, format, &count))
113 format[0] = 0;
115 count = sizeof(bAuto);
116 if (RegQueryValueExA(hDbgConf, "Auto", 0, &type, (char*)&bAuto, &count))
117 bAuto = FALSE;
119 RegCloseKey(hDbgConf);
120 } else {
121 /* format[0] = 0; */
122 strcpy(format, "debugger/winedbg %ld %ld");
125 if (!bAuto && Callout.MessageBoxA) {
126 sprintf( buffer, "Unhandled exception 0x%08lx at address 0x%08lx.\n"
127 "Do you wish to debug it ?",
128 epointers->ExceptionRecord->ExceptionCode,
129 (DWORD)epointers->ExceptionRecord->ExceptionAddress );
130 if (Callout.MessageBoxA( 0, buffer, "Error", MB_YESNO | MB_ICONHAND ) == IDNO) {
131 TRACE("Killing process\n");
132 return EXCEPTION_EXECUTE_HANDLER;
136 if (format[0]) {
137 HANDLE hEvent;
138 PROCESS_INFORMATION info;
139 STARTUPINFOA startup;
141 TRACE("Starting debugger (fmt=%s)\n", format);
142 hEvent = ConvertToGlobalHandle(CreateEventA(NULL, FALSE, FALSE, NULL));
143 sprintf(buffer, format, GetCurrentProcessId(), hEvent);
144 memset(&startup, 0, sizeof(startup));
145 startup.cb = sizeof(startup);
146 startup.dwFlags = STARTF_USESHOWWINDOW;
147 startup.wShowWindow = SW_SHOWNORMAL;
148 if (CreateProcessA(NULL, buffer, NULL, NULL,
149 TRUE, 0, NULL, NULL, &startup, &info)) {
150 WaitForSingleObject(hEvent, INFINITE);
151 ret = EXCEPTION_CONTINUE_SEARCH;
152 } else {
153 ERR("Couldn't start debugger (%s) (%ld)\n", buffer, GetLastError());
155 CloseHandle(hEvent);
156 } else {
157 ERR("No standard debugger defined in the registry => no debugging session\n");
160 return ret;
164 /***********************************************************************
165 * SetUnhandledExceptionFilter (KERNEL32.516)
167 LPTOP_LEVEL_EXCEPTION_FILTER WINAPI SetUnhandledExceptionFilter(
168 LPTOP_LEVEL_EXCEPTION_FILTER filter )
170 PDB *pdb = PROCESS_Current();
171 LPTOP_LEVEL_EXCEPTION_FILTER old = pdb->top_filter;
172 pdb->top_filter = filter;
173 return old;
177 /**************************************************************************
178 * FatalAppExit16 (KERNEL.137)
180 void WINAPI FatalAppExit16( UINT16 action, LPCSTR str )
182 WARN("AppExit\n");
183 FatalAppExitA( action, str );
187 /**************************************************************************
188 * FatalAppExitA (KERNEL32.108)
190 void WINAPI FatalAppExitA( UINT action, LPCSTR str )
192 WARN("AppExit\n");
193 Callout.MessageBoxA( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
194 ExitProcess(0);
198 /**************************************************************************
199 * FatalAppExitW (KERNEL32.109)
201 void WINAPI FatalAppExitW( UINT action, LPCWSTR str )
203 WARN("AppExit\n");
204 Callout.MessageBoxW( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
205 ExitProcess(0);