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
23 #include "wine/port.h"
31 #define WIN32_NO_STATUS
34 #include "wine/exception.h"
35 #include "wine/server.h"
36 #include "wine/list.h"
37 #include "wine/debug.h"
39 #include "ntdll_misc.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
46 PVECTORED_EXCEPTION_HANDLER func
;
50 static struct list vectored_handlers
= LIST_INIT(vectored_handlers
);
52 static RTL_CRITICAL_SECTION vectored_handlers_section
;
53 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
55 0, 0, &vectored_handlers_section
,
56 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
57 0, 0, { (DWORD_PTR
)(__FILE__
": vectored_handlers_section") }
59 static RTL_CRITICAL_SECTION vectored_handlers_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
61 /**********************************************************************
64 * Wait until the thread is no longer suspended.
66 void wait_suspend( CONTEXT
*context
)
68 LARGE_INTEGER timeout
;
69 int saved_errno
= errno
;
70 context_t server_context
;
72 context_to_server( &server_context
, context
);
74 /* store the context we got at suspend time */
75 SERVER_START_REQ( set_suspend_context
)
77 wine_server_add_data( req
, &server_context
, sizeof(server_context
) );
78 wine_server_call( req
);
82 /* wait with 0 timeout, will only return once the thread is no longer suspended */
84 server_select( NULL
, 0, SELECT_INTERRUPTIBLE
, &timeout
);
86 /* retrieve the new context */
87 SERVER_START_REQ( get_suspend_context
)
89 wine_server_set_reply( req
, &server_context
, sizeof(server_context
) );
90 wine_server_call( req
);
94 context_from_server( context
, &server_context
);
99 /**********************************************************************
102 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
104 NTSTATUS
send_debug_event( EXCEPTION_RECORD
*rec
, int first_chance
, CONTEXT
*context
)
108 obj_handle_t handle
= 0;
109 client_ptr_t params
[EXCEPTION_MAXIMUM_PARAMETERS
];
110 context_t server_context
;
111 select_op_t select_op
;
113 if (!NtCurrentTeb()->Peb
->BeingDebugged
) return 0; /* no debugger present */
115 for (i
= 0; i
< min( rec
->NumberParameters
, EXCEPTION_MAXIMUM_PARAMETERS
); i
++)
116 params
[i
] = rec
->ExceptionInformation
[i
];
118 context_to_server( &server_context
, context
);
120 SERVER_START_REQ( queue_exception_event
)
122 req
->first
= first_chance
;
123 req
->code
= rec
->ExceptionCode
;
124 req
->flags
= rec
->ExceptionFlags
;
125 req
->record
= wine_server_client_ptr( rec
->ExceptionRecord
);
126 req
->address
= wine_server_client_ptr( rec
->ExceptionAddress
);
127 req
->len
= i
* sizeof(params
[0]);
128 wine_server_add_data( req
, params
, req
->len
);
129 wine_server_add_data( req
, &server_context
, sizeof(server_context
) );
130 if (!wine_server_call( req
)) handle
= reply
->handle
;
133 if (!handle
) return 0;
135 select_op
.wait
.op
= SELECT_WAIT
;
136 select_op
.wait
.handles
[0] = handle
;
137 server_select( &select_op
, offsetof( select_op_t
, wait
.handles
[1] ), SELECT_INTERRUPTIBLE
, NULL
);
139 SERVER_START_REQ( get_exception_status
)
141 req
->handle
= handle
;
142 wine_server_set_reply( req
, &server_context
, sizeof(server_context
) );
143 ret
= wine_server_call( req
);
146 if (ret
>= 0) context_from_server( context
, &server_context
);
151 /**********************************************************************
152 * call_vectored_handlers
154 * Call the vectored handlers chain.
156 LONG
call_vectored_handlers( EXCEPTION_RECORD
*rec
, CONTEXT
*context
)
159 LONG ret
= EXCEPTION_CONTINUE_SEARCH
;
160 EXCEPTION_POINTERS except_ptrs
;
161 PVECTORED_EXCEPTION_HANDLER func
;
162 VECTORED_HANDLER
*handler
, *to_free
= NULL
;
164 except_ptrs
.ExceptionRecord
= rec
;
165 except_ptrs
.ContextRecord
= context
;
167 RtlEnterCriticalSection( &vectored_handlers_section
);
168 ptr
= list_head( &vectored_handlers
);
171 handler
= LIST_ENTRY( ptr
, VECTORED_HANDLER
, entry
);
173 func
= RtlDecodePointer( handler
->func
);
174 RtlLeaveCriticalSection( &vectored_handlers_section
);
175 RtlFreeHeap( GetProcessHeap(), 0, to_free
);
178 TRACE( "calling handler at %p code=%x flags=%x\n",
179 func
, rec
->ExceptionCode
, rec
->ExceptionFlags
);
180 ret
= func( &except_ptrs
);
181 TRACE( "handler at %p returned %x\n", func
, ret
);
183 RtlEnterCriticalSection( &vectored_handlers_section
);
184 ptr
= list_next( &vectored_handlers
, ptr
);
185 if (!--handler
->count
) /* removed during execution */
187 list_remove( &handler
->entry
);
190 if (ret
== EXCEPTION_CONTINUE_EXECUTION
) break;
192 RtlLeaveCriticalSection( &vectored_handlers_section
);
193 RtlFreeHeap( GetProcessHeap(), 0, to_free
);
198 /*******************************************************************
201 * Implementation of RtlRaiseStatus with a specific exception record.
203 void raise_status( NTSTATUS status
, EXCEPTION_RECORD
*rec
)
205 EXCEPTION_RECORD ExceptionRec
;
207 ExceptionRec
.ExceptionCode
= status
;
208 ExceptionRec
.ExceptionFlags
= EH_NONCONTINUABLE
;
209 ExceptionRec
.ExceptionRecord
= rec
;
210 ExceptionRec
.NumberParameters
= 0;
211 for (;;) RtlRaiseException( &ExceptionRec
); /* never returns */
215 /***********************************************************************
216 * RtlRaiseStatus (NTDLL.@)
218 * Raise an exception with ExceptionCode = status
220 void WINAPI
RtlRaiseStatus( NTSTATUS status
)
222 raise_status( status
, NULL
);
226 /*******************************************************************
227 * RtlAddVectoredExceptionHandler (NTDLL.@)
229 PVOID WINAPI
RtlAddVectoredExceptionHandler( ULONG first
, PVECTORED_EXCEPTION_HANDLER func
)
231 VECTORED_HANDLER
*handler
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*handler
) );
234 handler
->func
= RtlEncodePointer( func
);
236 RtlEnterCriticalSection( &vectored_handlers_section
);
237 if (first
) list_add_head( &vectored_handlers
, &handler
->entry
);
238 else list_add_tail( &vectored_handlers
, &handler
->entry
);
239 RtlLeaveCriticalSection( &vectored_handlers_section
);
245 /*******************************************************************
246 * RtlRemoveVectoredExceptionHandler (NTDLL.@)
248 ULONG WINAPI
RtlRemoveVectoredExceptionHandler( PVOID handler
)
253 RtlEnterCriticalSection( &vectored_handlers_section
);
254 LIST_FOR_EACH( ptr
, &vectored_handlers
)
256 VECTORED_HANDLER
*curr_handler
= LIST_ENTRY( ptr
, VECTORED_HANDLER
, entry
);
257 if (curr_handler
== handler
)
259 if (!--curr_handler
->count
) list_remove( ptr
);
260 else handler
= NULL
; /* don't free it yet */
265 RtlLeaveCriticalSection( &vectored_handlers_section
);
266 if (ret
) RtlFreeHeap( GetProcessHeap(), 0, handler
);
271 /*************************************************************
272 * __wine_spec_unimplemented_stub
274 * ntdll-specific implementation to avoid depending on kernel functions.
275 * Can be removed once ntdll.spec no longer contains stubs.
277 void __wine_spec_unimplemented_stub( const char *module
, const char *function
)
279 EXCEPTION_RECORD record
;
281 record
.ExceptionCode
= EXCEPTION_WINE_STUB
;
282 record
.ExceptionFlags
= EH_NONCONTINUABLE
;
283 record
.ExceptionRecord
= NULL
;
284 record
.ExceptionAddress
= __wine_spec_unimplemented_stub
;
285 record
.NumberParameters
= 2;
286 record
.ExceptionInformation
[0] = (ULONG_PTR
)module
;
287 record
.ExceptionInformation
[1] = (ULONG_PTR
)function
;
288 for (;;) RtlRaiseException( &record
);