github.com/lovishpuri/go-40569/src@v0.0.0-20230519171745-f8623e7c56cf/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  #ifdef _AMD64_
    15          c->Rip = *(DWORD64 *)c->Rsp;
    16          c->Rsp += 8;
    17  #elif defined(_X86_)
    18          c->Eip = *(DWORD *)c->Esp;
    19          c->Esp += 4;
    20  #else
    21          c->Pc = c->Lr;
    22  #endif
    23          return EXCEPTION_CONTINUE_EXECUTION;
    24      }
    25      return EXCEPTION_CONTINUE_SEARCH;
    26  }
    27  LONG WINAPI customContinueHandlder(struct _EXCEPTION_POINTERS *ExceptionInfo)
    28  {
    29      if (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_BREAKPOINT)
    30      {
    31          continueCount++;
    32          return EXCEPTION_CONTINUE_EXECUTION;
    33      }
    34      return EXCEPTION_CONTINUE_SEARCH;
    35  }
    36  
    37  void throwFromC()
    38  {
    39      DebugBreak();
    40  }
    41  int main()
    42  {
    43      // simulate a "lazily" attached debugger, by calling some go code before attaching the exception/continue handler
    44      Dummy();
    45      exceptionCount = 0;
    46      continueCount = 0;
    47      void *exceptionHandlerHandle = AddVectoredExceptionHandler(0, customExceptionHandlder);
    48      if (NULL == exceptionHandlerHandle)
    49      {
    50          printf("cannot add vectored exception handler\n");
    51          fflush(stdout);
    52          return 2;
    53      }
    54      void *continueHandlerHandle = AddVectoredContinueHandler(0, customContinueHandlder);
    55      if (NULL == continueHandlerHandle)
    56      {
    57          printf("cannot add vectored continue handler\n");
    58          fflush(stdout);
    59          return 2;
    60      }
    61      CallMeBack(throwFromC);
    62      RemoveVectoredContinueHandler(continueHandlerHandle);
    63      RemoveVectoredExceptionHandler(exceptionHandlerHandle);
    64      printf("exceptionCount: %d\ncontinueCount: %d\n", exceptionCount, continueCount);
    65      fflush(stdout);
    66      return 0;
    67  }