Added DebugBreak.
[wine.git] / win32 / except.c
blobdad51bb9ac540da227ee46d2dbfca3f651342b5f
1 /*
2 * Win32 exception functions
4 * Copyright (c) 1996 Onno Hovers, (onno@stack.urc.tue.nl)
5 * Copyright (c) 1999 Alexandre Julliard
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).
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!
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"
36 DEFAULT_DEBUG_CHANNEL(seh)
39 /*******************************************************************
40 * RaiseException (KERNEL32.418)
42 void WINAPI RaiseException( DWORD code, DWORD flags, DWORD nbargs, const LPDWORD args )
44 EXCEPTION_RECORD record;
46 /* Compose an exception record */
48 record.ExceptionCode = code;
49 record.ExceptionFlags = flags & EH_NONCONTINUABLE;
50 record.ExceptionRecord = NULL;
51 record.ExceptionAddress = RaiseException;
52 if (nbargs && args)
54 if (nbargs > EXCEPTION_MAXIMUM_PARAMETERS) nbargs = EXCEPTION_MAXIMUM_PARAMETERS;
55 record.NumberParameters = nbargs;
56 memcpy( record.ExceptionInformation, args, nbargs * sizeof(*args) );
58 else record.NumberParameters = 0;
60 RtlRaiseException( &record );
64 /*******************************************************************
65 * UnhandledExceptionFilter (KERNEL32.537)
67 DWORD WINAPI UnhandledExceptionFilter(PEXCEPTION_POINTERS epointers)
69 char message[80];
70 PDB *pdb = PROCESS_Current();
72 if (pdb->flags & PDB32_DEBUGGED)
74 if (DEBUG_SendExceptionEvent( epointers->ExceptionRecord, FALSE ) == DBG_CONTINUE)
75 return EXCEPTION_CONTINUE_EXECUTION;
77 else if (pdb->top_filter)
79 DWORD ret = pdb->top_filter( epointers );
80 if (ret != EXCEPTION_CONTINUE_SEARCH) return ret;
83 /* FIXME: Should check the current error mode */
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;
93 /*************************************************************
94 * SetUnhandledExceptionFilter (KERNEL32.516)
96 LPTOP_LEVEL_EXCEPTION_FILTER WINAPI SetUnhandledExceptionFilter(
97 LPTOP_LEVEL_EXCEPTION_FILTER filter )
99 PDB *pdb = PROCESS_Current();
100 LPTOP_LEVEL_EXCEPTION_FILTER old = pdb->top_filter;
101 pdb->top_filter = filter;
102 return old;
106 /*************************************************************
107 * WINE_exception_handler
109 * Exception handler for exception blocks declared in Wine code.
111 DWORD WINAPI WINE_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
112 CONTEXT *context, LPVOID pdispatcher )
114 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
116 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
117 return ExceptionContinueSearch;
118 if (wine_frame->u.e.filter)
120 EXCEPTION_POINTERS ptrs;
121 __WINE_DUMMY_FRAME dummy;
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 ))
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 );
139 RtlUnwind( frame, 0, record, 0 );
140 longjmp( wine_frame->u.e.jmp, 1 );
144 /*************************************************************
145 * WINE_finally_handler
147 * Exception handler for try/finally blocks declared in Wine code.
149 DWORD WINAPI WINE_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
150 CONTEXT *context, LPVOID pdispatcher )
152 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
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;