OSDN Git Service

Initial revision
[pf3gnuchains/pf3gnuchains3x.git] / winsup / mingw / samples / seh / sehfix.c
1 /*\r
2  * sehfix.c\r
3  *\r
4  * A test program involving an exception handler that fixes the exception\r
5  * causing condition.\r
6  *\r
7  * In this code we install an exception handler my_handler and then a piece\r
8  * of inline assembly attempts to write at the address marked in eax, after\r
9  * setting eax to 10. This should produce an exception. The handler then\r
10  * changes the eax register of the exception context to be the address of\r
11  * a static variable and restarts the code. This should allow everything\r
12  * to continue.\r
13  */\r
14 \r
15 #include <windows.h>\r
16 #include <excpt.h>\r
17 \r
18 #include "exutil.h"\r
19 \r
20 int     x;\r
21 \r
22 EXCEPTION_DISPOSITION\r
23 my_handler (\r
24         struct _EXCEPTION_RECORD* pExceptionRec,\r
25         void* pEstablisherFrame,\r
26         struct _CONTEXT* pContextRecord,\r
27         void* pDispatcherContext\r
28         )\r
29 {\r
30         printf ("In my exception handler!\n");\r
31         DumpExceptionRecord (pExceptionRec);\r
32         pContextRecord->Eax = (DWORD) &x;\r
33         return ExceptionContinueExecution;\r
34 }\r
35 \r
36 main ()\r
37 {\r
38         x = 2;\r
39 \r
40         printf ("x = %d\n", x);\r
41 \r
42         WalkExceptionHandlers();\r
43 \r
44         __try1(my_handler)\r
45 \r
46         WalkExceptionHandlers();\r
47 \r
48         /* This assembly code should produce an exception. */\r
49         __asm__("movl $10,%%eax;movl $1,(%%eax);" : : : "%eax");\r
50 \r
51         __except1\r
52 \r
53         WalkExceptionHandlers();\r
54 \r
55         printf ("x = %d\n", x);\r
56 \r
57         printf ("Finished!\n");\r
58 }\r
59 \r
60 \r