summaryrefslogtreecommitdiff
path: root/Runtime/Utilities/ErrorExit.cpp
blob: 414aa06cc0025bb66a89f5a06ed71339f97d8f28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "UnityPrefix.h"
#include "ErrorExit.h"
#include "Runtime/Misc/ReproductionLog.h"
#if SUPPORT_ERROR_EXIT
#include "Runtime/Threads/ThreadSpecificValue.h"
#include "Stacktrace.h"

#if UNITY_WIN

#include "Configuration/UnityConfigureOther.h"
#include "Runtime/Mono/MonoIncludes.h"

#include "PlatformDependent/Win/WinUtils.h"

#if !UNITY_WIN_ENABLE_CRASHHANDLER
#error "UNITY_WIN_ENABLE_CRASHHANDLER must be defined"
#endif

#include "../../Tools/BugReporterWin/lib/CrashHandler.h"
extern CrashHandler *gUnityCrashHandler;

#endif

ExitErrorCode gExitErrorCode = kErrorNone;
bool gExitErrorDidCleanup = false;
#if !UNITY_WIN
static UNITY_TLS_VALUE(jmp_buf*) gJumpBuffer;
#endif

ExitErrorCode GetExitErrorCode()
{
	return gExitErrorCode;
}

#if UNITY_PEPPER
#define STACKTRACE() ""
#else
#define STACKTRACE() GetStacktrace().c_str()
#endif

void ExitWithErrorCode(ExitErrorCode error)
{
	printf_console("Exit with error code: %d\n", error);
	printf_console("\n========== OUTPUTING STACK TRACE ==================\n\n");
	printf_console("%s\n", STACKTRACE());
	printf_console("\n========== END OF STACKTRACE ===========\n\n");
	
	#if SUPPORT_REPRODUCE_LOG
	if (RunningReproduction())
	{
		ErrorString("ExitWithErrorCode invoked, exiting application because we are in reproduction mode.");
		ReproductionExitPlayer(1);
	}
	#endif

	#if !UNITY_RELEASE
	printf_console("\n*** DEBUGMODE -> ExitWithError code calling abort for debugging ease *** ");
	abort();
	#endif
	
	gExitErrorCode = error;
	
	#if !UNITY_WIN

	jmp_buf *buf = gJumpBuffer;	
	
	if (buf)
	{
		printf_console("Got error %d. Bailing out.\n", error);
		longjmp (*buf, 1);
	}
	else
	{
		printf_console("Got error %d. Cannot exit at this point.\n", error);
	}

	#endif
}

#if !UNITY_WIN

void InsertThreadExitPoint(jmp_buf *buf)
{
	gJumpBuffer = buf;
}

AutoRemoveEntryPoint::AutoRemoveEntryPoint (jmp_buf *buf)
{
	if (gJumpBuffer)
		m_ShouldRemove = false;
	else
	{
		m_ShouldRemove = true;
		gJumpBuffer = buf;
	}
}

AutoRemoveEntryPoint::~AutoRemoveEntryPoint ()
{
	if (m_ShouldRemove)
		gJumpBuffer = NULL;
}

#endif

const char* GetExitErrorString(ExitErrorCode err)
{
	switch(err)
	{
		case kErrorSecurityBreach:
			return "The content was stopped because it\nattempted an insecure operation.";
		case kErrorFatalException:
			return "The content was stopped because a fatal\ncontent error has been detected.";
		case kErrorNoSSE2Architecture:
			return "Unity Web Player requires an SSE2 capable CPU.";
		case kErrorIncompatibleRuntimeVersion:
			return "The Unity Web Player content which you are trying to load was built with an older version of the Unity Editor, and is incompatible with this runtime.";
		case kErrorUnsupportedGPU:
			return "Unity Web Player requires DX9 level graphics card.\nMake sure you have graphics card drivers installed.";
		default:
			return NULL;
	}
}

#if UNITY_WIN

extern LPTOP_LEVEL_EXCEPTION_FILTER exceptionFilter;
int HandleSignal( EXCEPTION_POINTERS* ep );

DWORD OnExcept(DWORD code, LPEXCEPTION_POINTERS exception)
{
	__try
	{
		DWORD result;
		if( NULL != exceptionFilter )
		{
			#if WEBPLUG
			winutils::ProcessInternalCrash(exception, false);
			#endif

			result = exceptionFilter(exception);
		}
		else
		{
			result = mono_unity_seh_handler(exception);
		}

		if (EXCEPTION_CONTINUE_EXECUTION != result)
		{
			gExitErrorCode = kErrorFatalException;
			result = EXCEPTION_EXECUTE_HANDLER;
		}

		return result;
	}
	__except (EXCEPTION_EXECUTE_HANDLER)
	{
		gExitErrorCode = kErrorFatalException;
		return EXCEPTION_EXECUTE_HANDLER;
	}
}

#endif
#endif