72a9967afd7bed7ed8077e8f0ad6f989495b17e7
[ghc.git] / rts / win32 / veh_excn.h
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team 1998-2000
4 *
5 * Header for windows Error Handling implementations
6 *
7 * ---------------------------------------------------------------------------*/
8
9 #pragma once
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <Rts.h>
14
15 // Stuff needed to install and use VEH exception handlers
16 #include <excpt.h>
17 #include <windows.h>
18
19 // Exception handling.
20 //
21 // On Windows, the default action for things like division by zero and
22 // segfaults is to pop up a Dr. Watson error reporting dialog if the exception
23 // is unhandled by the user code.
24 //
25 // This is a pain when we are SSHed into a Windows machine, or when we
26 // want to debug a problem with gdb (gdb will get a first and second chance to
27 // handle the exception, but if it doesn't the pop-up will show).
28 //
29 //
30 // Previously this code was handled using SEH (Structured Exception Handlers)
31 // however each compiler and platform have different ways of dealing with SEH.
32 //
33 // MSVC compilers have the keywords __try, __catch and __except to have the
34 // compiler generate the appropriate SEH handler code for you.
35 //
36 // MinGW compilers have no such keywords and require you to manually set the
37 // SEH Handlers, however because SEH is implemented differently in x86 and x64
38 // the methods to use them in GCC differs.
39 //
40 // x86: SEH is based on the stack, the SEH handlers are available at FS[0].
41 // On startup one would only need to add a new handler there. This has
42 // a number of issues such as hard to share handlers and it can be exploited.
43 //
44 // x64: In order to fix the issues with the way SEH worked in x86, on x64 SEH handlers
45 // are statically compiled and added to the .pdata section by the compiler.
46 // Instead of being thread global they can now be Image global since you have to
47 // specify the RVA of the region of code that the handlers govern.
48 //
49 // You can on x64 Dynamically allocate SEH handlers, but it seems that (based on
50 // experimentation and it's very under-documented) that the dynamic calls cannot override
51 // static SEH handlers in the .pdata section.
52 //
53 // Because of this and because GHC no longer needs to support < windows XP, the better
54 // alternative for handling errors would be using the in XP introduced VEH.
55 //
56 // The bonus is because VEH (Vectored Exception Handler) are a runtime construct the API
57 // is the same for both x86 and x64 (note that the Context object does contain CPU specific
58 // structures) and the calls are the same cross compilers. Which means this file can be
59 // simplified quite a bit.
60 // Using VEH also means we don't have to worry about the dynamic code generated by GHCi.
61
62 // Prototype of the VEH callback function.
63 // See https://msdn.microsoft.com/en-us/library/windows/desktop/ms681419(v=vs.85).aspx
64 //
65 long WINAPI __hs_exception_handler( struct _EXCEPTION_POINTERS *exception_data );
66 long WINAPI __hs_exception_filter(struct _EXCEPTION_POINTERS *exception_data);
67
68 // prototypes to the functions doing the registration and unregistration of the VEH handlers
69 void __register_hs_exception_handler( void );
70 void __unregister_hs_exception_handler( void );