Added DebugBreak.
[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)
73     {
74         if (DEBUG_SendExceptionEvent( epointers->ExceptionRecord, FALSE ) == DBG_CONTINUE)
75             return EXCEPTION_CONTINUE_EXECUTION;
76     }
77     else if (pdb->top_filter)
78     {
79         DWORD ret = pdb->top_filter( epointers );
80         if (ret != EXCEPTION_CONTINUE_SEARCH) return ret;
81     }
82
83     /* FIXME: Should check the current error mode */
84
85     sprintf( message, "Unhandled exception 0x%08lx at address 0x%08lx.",
86              epointers->ExceptionRecord->ExceptionCode,
87              (DWORD)epointers->ExceptionRecord->ExceptionAddress );
88     MessageBoxA( 0, message, "Error", MB_OK | MB_ICONHAND );
89     return EXCEPTION_EXECUTE_HANDLER;
90 }
91
92
93 /*************************************************************
94  *            SetUnhandledExceptionFilter   (KERNEL32.516)
95  */
96 LPTOP_LEVEL_EXCEPTION_FILTER WINAPI SetUnhandledExceptionFilter(
97                                           LPTOP_LEVEL_EXCEPTION_FILTER filter )
98 {
99     PDB *pdb = PROCESS_Current();
100     LPTOP_LEVEL_EXCEPTION_FILTER old = pdb->top_filter;
101     pdb->top_filter = filter;
102     return old;
103 }
104
105
106 /*************************************************************
107  *            WINE_exception_handler
108  *
109  * Exception handler for exception blocks declared in Wine code.
110  */
111 DWORD WINAPI WINE_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
112                                      CONTEXT *context, LPVOID pdispatcher )
113 {
114     __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
115
116     if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
117         return ExceptionContinueSearch;
118     if (wine_frame->u.e.filter)
119     {
120         EXCEPTION_POINTERS ptrs;
121         __WINE_DUMMY_FRAME dummy;
122
123         ptrs.ExceptionRecord = record;
124         ptrs.ContextRecord = context;
125         dummy.u.e.code = record->ExceptionCode;
126         switch(wine_frame->u.e.filter( &ptrs, dummy, wine_frame->u.e.param ))
127         {
128         case EXCEPTION_CONTINUE_SEARCH:
129             return ExceptionContinueSearch;
130         case EXCEPTION_CONTINUE_EXECUTION:
131             return ExceptionContinueExecution;
132         case EXCEPTION_EXECUTE_HANDLER:
133             break;
134         default:
135             MESSAGE( "Invalid return value from exception filter\n" );
136             assert( FALSE );
137         }
138     }
139     RtlUnwind( frame, 0, record, 0 );
140     longjmp( wine_frame->u.e.jmp, 1 );
141 }
142
143
144 /*************************************************************
145  *            WINE_finally_handler
146  *
147  * Exception handler for try/finally blocks declared in Wine code.
148  */
149 DWORD WINAPI WINE_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
150                                    CONTEXT *context, LPVOID pdispatcher )
151 {
152     __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
153
154     if (!(record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
155         return ExceptionContinueSearch;
156     wine_frame->u.f.finally_func( FALSE, wine_frame->u.f.param );
157     return ExceptionContinueSearch;
158 }