long WINAPI __hs_exception_handler()

in rts/win32/veh_excn.c [98:155]


long WINAPI __hs_exception_handler(struct _EXCEPTION_POINTERS *exception_data)
{
    if (!crash_dump && filter_called)
      return EXCEPTION_CONTINUE_EXECUTION;

    long action   = EXCEPTION_CONTINUE_SEARCH;
    int exit_code = EXIT_FAILURE;
    ULONG_PTR what;
    fprintf (stderr, "\n");

    // When the system unwinds the VEH stack after having handled an excn,
    // return immediately.
    if ((exception_data->ExceptionRecord->ExceptionFlags & EH_UNWINDING) == 0)
    {
        // Error handling cases covered by this implementation.
        switch (exception_data->ExceptionRecord->ExceptionCode) {
            case EXCEPTION_FLT_DIVIDE_BY_ZERO:
            case EXCEPTION_INT_DIVIDE_BY_ZERO:
                fprintf(stderr, "divide by zero\n");
                action = EXCEPTION_CONTINUE_EXECUTION;
                exit_code = SIGFPE;
                break;
            case EXCEPTION_STACK_OVERFLOW:
                fprintf(stderr, "C stack overflow in generated code\n");
                action = EXCEPTION_CONTINUE_EXECUTION;
                break;
            case EXCEPTION_ACCESS_VIOLATION:
                what = exception_data->ExceptionRecord->ExceptionInformation[0];
                fprintf(stderr, "Access violation in generated code"
                                " when %s 0x%" PRIxPTR "\n"
                                , what == 0 ? "reading"
                                : what == 1 ? "writing"
                                : what == 8 ? "executing data at"
                                :             "?"
                                , (uintptr_t) exception_data
                                                ->ExceptionRecord
                                                ->ExceptionInformation[1]
                                );
                action = EXCEPTION_CONTINUE_EXECUTION;
                exit_code = SIGSEGV;
                break;
            default:;
        }

        // If an error has occurred and we've decided to continue execution
        // then we've done so to prevent something else from handling the error.
        // But the correct action is still to exit as fast as possible.
        if (EXCEPTION_CONTINUE_EXECUTION == action)
        {
            fflush(stderr);
            generateStack (exception_data);
            generateDump (exception_data);
            stg_exit(exit_code);
        }
    }

    return action;
}