comctl32/tooltips: Reset window subclass data when removing tools.
[wine.git] / dlls / ntdll / exception.c
blobdaca45aa836d8eb86b66ef1da86f5963f0c7e30e
1 /*
2 * NT exception handling routines
4 * Copyright 1999 Turchanov Sergey
5 * Copyright 1999 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <errno.h>
27 #include <signal.h>
28 #include <stdarg.h>
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "windef.h"
33 #include "winternl.h"
34 #include "wine/exception.h"
35 #include "wine/server.h"
36 #include "wine/list.h"
37 #include "wine/debug.h"
38 #include "excpt.h"
39 #include "ntdll_misc.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(seh);
43 typedef struct
45 struct list entry;
46 PVECTORED_EXCEPTION_HANDLER func;
47 ULONG count;
48 } VECTORED_HANDLER;
50 static struct list vectored_exception_handlers = LIST_INIT(vectored_exception_handlers);
51 static struct list vectored_continue_handlers = LIST_INIT(vectored_continue_handlers);
53 static RTL_CRITICAL_SECTION vectored_handlers_section;
54 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
56 0, 0, &vectored_handlers_section,
57 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
58 0, 0, { (DWORD_PTR)(__FILE__ ": vectored_handlers_section") }
60 static RTL_CRITICAL_SECTION vectored_handlers_section = { &critsect_debug, -1, 0, 0, 0, 0 };
63 static VECTORED_HANDLER *add_vectored_handler( struct list *handler_list, ULONG first,
64 PVECTORED_EXCEPTION_HANDLER func )
66 VECTORED_HANDLER *handler = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*handler) );
67 if (handler)
69 handler->func = RtlEncodePointer( func );
70 handler->count = 1;
71 RtlEnterCriticalSection( &vectored_handlers_section );
72 if (first) list_add_head( handler_list, &handler->entry );
73 else list_add_tail( handler_list, &handler->entry );
74 RtlLeaveCriticalSection( &vectored_handlers_section );
76 return handler;
80 static ULONG remove_vectored_handler( struct list *handler_list, VECTORED_HANDLER *handler )
82 struct list *ptr;
83 ULONG ret = FALSE;
85 RtlEnterCriticalSection( &vectored_handlers_section );
86 LIST_FOR_EACH( ptr, handler_list )
88 VECTORED_HANDLER *curr_handler = LIST_ENTRY( ptr, VECTORED_HANDLER, entry );
89 if (curr_handler == handler)
91 if (!--curr_handler->count) list_remove( ptr );
92 else handler = NULL; /* don't free it yet */
93 ret = TRUE;
94 break;
97 RtlLeaveCriticalSection( &vectored_handlers_section );
98 if (ret) RtlFreeHeap( GetProcessHeap(), 0, handler );
99 return ret;
103 /**********************************************************************
104 * wait_suspend
106 * Wait until the thread is no longer suspended.
108 void wait_suspend( CONTEXT *context )
110 LARGE_INTEGER timeout;
111 int saved_errno = errno;
112 context_t server_context;
114 context_to_server( &server_context, context );
116 /* store the context we got at suspend time */
117 SERVER_START_REQ( set_suspend_context )
119 wine_server_add_data( req, &server_context, sizeof(server_context) );
120 wine_server_call( req );
122 SERVER_END_REQ;
124 /* wait with 0 timeout, will only return once the thread is no longer suspended */
125 timeout.QuadPart = 0;
126 server_select( NULL, 0, SELECT_INTERRUPTIBLE, &timeout );
128 /* retrieve the new context */
129 SERVER_START_REQ( get_suspend_context )
131 wine_server_set_reply( req, &server_context, sizeof(server_context) );
132 wine_server_call( req );
134 SERVER_END_REQ;
136 context_from_server( context, &server_context );
137 errno = saved_errno;
141 /**********************************************************************
142 * send_debug_event
144 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
146 NTSTATUS send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
148 NTSTATUS ret;
149 DWORD i;
150 obj_handle_t handle = 0;
151 client_ptr_t params[EXCEPTION_MAXIMUM_PARAMETERS];
152 context_t server_context;
153 select_op_t select_op;
155 if (!NtCurrentTeb()->Peb->BeingDebugged) return 0; /* no debugger present */
157 for (i = 0; i < min( rec->NumberParameters, EXCEPTION_MAXIMUM_PARAMETERS ); i++)
158 params[i] = rec->ExceptionInformation[i];
160 context_to_server( &server_context, context );
162 SERVER_START_REQ( queue_exception_event )
164 req->first = first_chance;
165 req->code = rec->ExceptionCode;
166 req->flags = rec->ExceptionFlags;
167 req->record = wine_server_client_ptr( rec->ExceptionRecord );
168 req->address = wine_server_client_ptr( rec->ExceptionAddress );
169 req->len = i * sizeof(params[0]);
170 wine_server_add_data( req, params, req->len );
171 wine_server_add_data( req, &server_context, sizeof(server_context) );
172 if (!wine_server_call( req )) handle = reply->handle;
174 SERVER_END_REQ;
175 if (!handle) return 0;
177 select_op.wait.op = SELECT_WAIT;
178 select_op.wait.handles[0] = handle;
179 server_select( &select_op, offsetof( select_op_t, wait.handles[1] ), SELECT_INTERRUPTIBLE, NULL );
181 SERVER_START_REQ( get_exception_status )
183 req->handle = handle;
184 wine_server_set_reply( req, &server_context, sizeof(server_context) );
185 ret = wine_server_call( req );
187 SERVER_END_REQ;
188 if (ret >= 0) context_from_server( context, &server_context );
189 return ret;
193 /**********************************************************************
194 * call_vectored_handlers
196 * Call the vectored handlers chain.
198 LONG call_vectored_handlers( EXCEPTION_RECORD *rec, CONTEXT *context )
200 struct list *ptr;
201 LONG ret = EXCEPTION_CONTINUE_SEARCH;
202 EXCEPTION_POINTERS except_ptrs;
203 PVECTORED_EXCEPTION_HANDLER func;
204 VECTORED_HANDLER *handler, *to_free = NULL;
206 except_ptrs.ExceptionRecord = rec;
207 except_ptrs.ContextRecord = context;
209 RtlEnterCriticalSection( &vectored_handlers_section );
210 ptr = list_head( &vectored_exception_handlers );
211 while (ptr)
213 handler = LIST_ENTRY( ptr, VECTORED_HANDLER, entry );
214 handler->count++;
215 func = RtlDecodePointer( handler->func );
216 RtlLeaveCriticalSection( &vectored_handlers_section );
217 RtlFreeHeap( GetProcessHeap(), 0, to_free );
218 to_free = NULL;
220 TRACE( "calling handler at %p code=%x flags=%x\n",
221 func, rec->ExceptionCode, rec->ExceptionFlags );
222 ret = func( &except_ptrs );
223 TRACE( "handler at %p returned %x\n", func, ret );
225 RtlEnterCriticalSection( &vectored_handlers_section );
226 ptr = list_next( &vectored_exception_handlers, ptr );
227 if (!--handler->count) /* removed during execution */
229 list_remove( &handler->entry );
230 to_free = handler;
232 if (ret == EXCEPTION_CONTINUE_EXECUTION) break;
234 RtlLeaveCriticalSection( &vectored_handlers_section );
235 RtlFreeHeap( GetProcessHeap(), 0, to_free );
236 return ret;
240 /*******************************************************************
241 * raise_status
243 * Implementation of RtlRaiseStatus with a specific exception record.
245 void raise_status( NTSTATUS status, EXCEPTION_RECORD *rec )
247 EXCEPTION_RECORD ExceptionRec;
249 ExceptionRec.ExceptionCode = status;
250 ExceptionRec.ExceptionFlags = EH_NONCONTINUABLE;
251 ExceptionRec.ExceptionRecord = rec;
252 ExceptionRec.NumberParameters = 0;
253 for (;;) RtlRaiseException( &ExceptionRec ); /* never returns */
257 /***********************************************************************
258 * RtlRaiseStatus (NTDLL.@)
260 * Raise an exception with ExceptionCode = status
262 void WINAPI RtlRaiseStatus( NTSTATUS status )
264 raise_status( status, NULL );
268 /*******************************************************************
269 * RtlAddVectoredContinueHandler (NTDLL.@)
271 PVOID WINAPI RtlAddVectoredContinueHandler( ULONG first, PVECTORED_EXCEPTION_HANDLER func )
273 return add_vectored_handler( &vectored_continue_handlers, first, func );
277 /*******************************************************************
278 * RtlRemoveVectoredContinueHandler (NTDLL.@)
280 ULONG WINAPI RtlRemoveVectoredContinueHandler( PVOID handler )
282 return remove_vectored_handler( &vectored_continue_handlers, handler );
286 /*******************************************************************
287 * RtlAddVectoredExceptionHandler (NTDLL.@)
289 PVOID WINAPI DECLSPEC_HOTPATCH RtlAddVectoredExceptionHandler( ULONG first, PVECTORED_EXCEPTION_HANDLER func )
291 return add_vectored_handler( &vectored_exception_handlers, first, func );
295 /*******************************************************************
296 * RtlRemoveVectoredExceptionHandler (NTDLL.@)
298 ULONG WINAPI RtlRemoveVectoredExceptionHandler( PVOID handler )
300 return remove_vectored_handler( &vectored_exception_handlers, handler );
304 /*************************************************************
305 * __wine_spec_unimplemented_stub
307 * ntdll-specific implementation to avoid depending on kernel functions.
308 * Can be removed once ntdll.spec no longer contains stubs.
310 void __wine_spec_unimplemented_stub( const char *module, const char *function )
312 EXCEPTION_RECORD record;
314 record.ExceptionCode = EXCEPTION_WINE_STUB;
315 record.ExceptionFlags = EH_NONCONTINUABLE;
316 record.ExceptionRecord = NULL;
317 record.ExceptionAddress = __wine_spec_unimplemented_stub;
318 record.NumberParameters = 2;
319 record.ExceptionInformation[0] = (ULONG_PTR)module;
320 record.ExceptionInformation[1] = (ULONG_PTR)function;
321 for (;;) RtlRaiseException( &record );