github.com/mtsmfm/go/src@v0.0.0-20221020090648-44bdcb9f8fde/runtime/testdata/testwinlib/main.c (about) 1 #include <stdio.h> 2 #include <windows.h> 3 #include "testwinlib.h" 4 5 int exceptionCount; 6 int continueCount; 7 LONG WINAPI customExceptionHandlder(struct _EXCEPTION_POINTERS *ExceptionInfo) 8 { 9 if (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_BREAKPOINT) 10 { 11 exceptionCount++; 12 // prepare context to resume execution 13 CONTEXT *c = ExceptionInfo->ContextRecord; 14 c->Rip = *(ULONG_PTR *)c->Rsp; 15 c->Rsp += 8; 16 return EXCEPTION_CONTINUE_EXECUTION; 17 } 18 return EXCEPTION_CONTINUE_SEARCH; 19 } 20 LONG WINAPI customContinueHandlder(struct _EXCEPTION_POINTERS *ExceptionInfo) 21 { 22 if (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_BREAKPOINT) 23 { 24 continueCount++; 25 return EXCEPTION_CONTINUE_EXECUTION; 26 } 27 return EXCEPTION_CONTINUE_SEARCH; 28 } 29 30 void throwFromC() 31 { 32 DebugBreak(); 33 } 34 int main() 35 { 36 // simulate a "lazily" attached debugger, by calling some go code before attaching the exception/continue handler 37 Dummy(); 38 exceptionCount = 0; 39 continueCount = 0; 40 void *exceptionHandlerHandle = AddVectoredExceptionHandler(0, customExceptionHandlder); 41 if (NULL == exceptionHandlerHandle) 42 { 43 printf("cannot add vectored exception handler\n"); 44 fflush(stdout); 45 return 2; 46 } 47 void *continueHandlerHandle = AddVectoredContinueHandler(0, customContinueHandlder); 48 if (NULL == continueHandlerHandle) 49 { 50 printf("cannot add vectored continue handler\n"); 51 fflush(stdout); 52 return 2; 53 } 54 CallMeBack(throwFromC); 55 RemoveVectoredContinueHandler(continueHandlerHandle); 56 RemoveVectoredExceptionHandler(exceptionHandlerHandle); 57 printf("exceptionCount: %d\ncontinueCount: %d\n", exceptionCount, continueCount); 58 fflush(stdout); 59 return 0; 60 }