2 * Win32 exception functions
4 * Copyright (c) 1996 Onno Hovers, (onno@stack.urc.tue.nl)
5 * Copyright (c) 1999 Alexandre Julliard
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
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
32 #include "wine/exception.h"
36 #include "stackframe.h"
38 #include "debugtools.h"
40 DEFAULT_DEBUG_CHANNEL(seh)
43 /*******************************************************************
44 * RaiseException (KERNEL32.418)
46 void WINAPI RaiseException( DWORD code, DWORD flags, DWORD nbargs, const LPDWORD args )
48 EXCEPTION_RECORD record;
50 /* Compose an exception record */
52 record.ExceptionCode = code;
53 record.ExceptionFlags = flags & EH_NONCONTINUABLE;
54 record.ExceptionRecord = NULL;
55 record.ExceptionAddress = RaiseException;
58 if (nbargs > EXCEPTION_MAXIMUM_PARAMETERS) nbargs = EXCEPTION_MAXIMUM_PARAMETERS;
59 record.NumberParameters = nbargs;
60 memcpy( record.ExceptionInformation, args, nbargs * sizeof(*args) );
62 else record.NumberParameters = 0;
64 RtlRaiseException( &record );
68 /*******************************************************************
69 * UnhandledExceptionFilter (KERNEL32.537)
71 DWORD WINAPI UnhandledExceptionFilter(PEXCEPTION_POINTERS epointers)
74 PDB *pdb = PROCESS_Current();
76 if (pdb->flags & PDB32_DEBUGGED) return EXCEPTION_CONTINUE_SEARCH;
80 DWORD ret = pdb->top_filter( epointers );
81 if (ret != EXCEPTION_CONTINUE_SEARCH) return ret;
84 /* FIXME: does not belong here */
85 if (wine_debugger( epointers->ExceptionRecord,
86 epointers->ContextRecord, FALSE ) == DBG_CONTINUE)
87 return EXCEPTION_CONTINUE_EXECUTION;
89 /* FIXME: Should check the current error mode */
91 sprintf( message, "Unhandled exception 0x%08lx at address 0x%08lx.",
92 epointers->ExceptionRecord->ExceptionCode,
93 (DWORD)epointers->ExceptionRecord->ExceptionAddress );
94 MessageBoxA( 0, message, "Error", MB_OK | MB_ICONHAND );
95 return EXCEPTION_EXECUTE_HANDLER;
99 /*************************************************************
100 * SetUnhandledExceptionFilter (KERNEL32.516)
102 LPTOP_LEVEL_EXCEPTION_FILTER WINAPI SetUnhandledExceptionFilter(
103 LPTOP_LEVEL_EXCEPTION_FILTER filter )
105 PDB *pdb = PROCESS_Current();
106 LPTOP_LEVEL_EXCEPTION_FILTER old = pdb->top_filter;
107 pdb->top_filter = filter;
112 /*************************************************************
113 * WINE_exception_handler
115 * Exception handler for exception blocks declared in Wine code.
117 DWORD WINE_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
118 CONTEXT *context, LPVOID pdispatcher )
120 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
122 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
123 return ExceptionContinueSearch;
124 if (wine_frame->u.filter)
126 EXCEPTION_POINTERS ptrs;
127 ptrs.ExceptionRecord = record;
128 ptrs.ContextRecord = context;
129 switch(wine_frame->u.filter( &ptrs ))
131 case EXCEPTION_CONTINUE_SEARCH:
132 return ExceptionContinueSearch;
133 case EXCEPTION_CONTINUE_EXECUTION:
134 return ExceptionContinueExecution;
135 case EXCEPTION_EXECUTE_HANDLER:
138 MESSAGE( "Invalid return value from exception filter\n" );
142 /* hack to make GetExceptionCode() work in handler */
143 wine_frame->ExceptionCode = record->ExceptionCode;
144 wine_frame->ExceptionRecord = wine_frame;
146 RtlUnwind( frame, 0, record, 0 );
147 EXC_pop_frame( frame );
148 longjmp( wine_frame->jmp, 1 );
152 /*************************************************************
153 * WINE_finally_handler
155 * Exception handler for try/finally blocks declared in Wine code.
157 DWORD WINE_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
158 CONTEXT *context, LPVOID pdispatcher )
160 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
162 if (!(record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
163 return ExceptionContinueSearch;
164 wine_frame->u.finally_func( FALSE );
165 return ExceptionContinueSearch;