Bugfix: Exception handlers are *not* WINAPI.
[wine] / win32 / except.c
1 /*
2  * Win32 exception functions
3  *
4  * Copyright (c) 1996 Onno Hovers, (onno@stack.urc.tue.nl)
5  * Copyright (c) 1999 Alexandre Julliard
6  *
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).  
17  *
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!
22  *
23  */
24
25 #include <assert.h>
26 #include "winuser.h"
27 #include "winerror.h"
28 #include "ntddk.h"
29 #include "wine/exception.h"
30 #include "ldt.h"
31 #include "process.h"
32 #include "thread.h"
33 #include "stackframe.h"
34 #include "debugtools.h"
35
36 DEFAULT_DEBUG_CHANNEL(seh)
37
38
39 /*******************************************************************
40  *         RaiseException  (KERNEL32.418)
41  */
42 void WINAPI RaiseException( DWORD code, DWORD flags, DWORD nbargs, const LPDWORD args )
43 {
44     EXCEPTION_RECORD record;
45
46     /* Compose an exception record */ 
47     
48     record.ExceptionCode    = code;
49     record.ExceptionFlags   = flags & EH_NONCONTINUABLE;
50     record.ExceptionRecord  = NULL;
51     record.ExceptionAddress = RaiseException;
52     if (nbargs && args)
53     {
54         if (nbargs > EXCEPTION_MAXIMUM_PARAMETERS) nbargs = EXCEPTION_MAXIMUM_PARAMETERS;
55         record.NumberParameters = nbargs;
56         memcpy( record.ExceptionInformation, args, nbargs * sizeof(*args) );
57     }
58     else record.NumberParameters = 0;
59
60     RtlRaiseException( &record );
61 }
62
63
64 /*******************************************************************
65  *         UnhandledExceptionFilter   (KERNEL32.537)
66  */
67 DWORD WINAPI UnhandledExceptionFilter(PEXCEPTION_POINTERS epointers)
68 {
69     char message[80];
70     PDB *pdb = PROCESS_Current();
71
72     if (pdb->flags & PDB32_DEBUGGED) return EXCEPTION_CONTINUE_SEARCH;
73
74     if (pdb->top_filter)
75     {
76         DWORD ret = pdb->top_filter( epointers );
77         if (ret != EXCEPTION_CONTINUE_SEARCH) return ret;
78     }
79
80     /* FIXME: does not belong here */
81     if (((*EXC_GetDebugEventHook())( epointers->ExceptionRecord,
82                                      epointers->ContextRecord, FALSE )) == DBG_CONTINUE)
83         return EXCEPTION_CONTINUE_EXECUTION;
84
85     /* FIXME: Should check the current error mode */
86
87     sprintf( message, "Unhandled exception 0x%08lx at address 0x%08lx.",
88              epointers->ExceptionRecord->ExceptionCode,
89              (DWORD)epointers->ExceptionRecord->ExceptionAddress );
90     MessageBoxA( 0, message, "Error", MB_OK | MB_ICONHAND );
91     return EXCEPTION_EXECUTE_HANDLER;
92 }
93
94
95 /*************************************************************
96  *            SetUnhandledExceptionFilter   (KERNEL32.516)
97  */
98 LPTOP_LEVEL_EXCEPTION_FILTER WINAPI SetUnhandledExceptionFilter(
99                                           LPTOP_LEVEL_EXCEPTION_FILTER filter )
100 {
101     PDB *pdb = PROCESS_Current();
102     LPTOP_LEVEL_EXCEPTION_FILTER old = pdb->top_filter;
103     pdb->top_filter = filter;
104     return old;
105 }
106
107
108 /*************************************************************
109  *            WINE_exception_handler
110  *
111  * Exception handler for exception blocks declared in Wine code.
112  */
113 DWORD WINE_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
114                               CONTEXT *context, LPVOID pdispatcher )
115 {
116     __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
117
118     if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
119         return ExceptionContinueSearch;
120     if (wine_frame->u.filter)
121     {
122         EXCEPTION_POINTERS ptrs;
123         ptrs.ExceptionRecord = record;
124         ptrs.ContextRecord = context;
125         switch(wine_frame->u.filter( &ptrs ))
126         {
127         case EXCEPTION_CONTINUE_SEARCH:
128             return ExceptionContinueSearch;
129         case EXCEPTION_CONTINUE_EXECUTION:
130             return ExceptionContinueExecution;
131         case EXCEPTION_EXECUTE_HANDLER:
132             break;
133         default:
134             MESSAGE( "Invalid return value from exception filter\n" );
135             assert( FALSE );
136         }
137     }
138     /* hack to make GetExceptionCode() work in handler */
139     wine_frame->ExceptionCode   = record->ExceptionCode;
140     wine_frame->ExceptionRecord = wine_frame;
141
142     RtlUnwind( frame, 0, record, 0 );
143     EXC_pop_frame( frame );
144     longjmp( wine_frame->jmp, 1 );
145 }
146
147
148 /*************************************************************
149  *            WINE_finally_handler
150  *
151  * Exception handler for try/finally blocks declared in Wine code.
152  */
153 DWORD WINE_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
154                             CONTEXT *context, LPVOID pdispatcher )
155 {
156     __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
157
158     if (!(record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
159         return ExceptionContinueSearch;
160     wine_frame->u.finally_func( FALSE );
161     return ExceptionContinueSearch;
162 }