2014-11-19 18:24:03 -07:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package runtime
|
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2015-04-12 20:02:44 -06:00
|
|
|
func disableWER() {
|
|
|
|
// do not display Windows Error Reporting dialogue
|
|
|
|
const (
|
|
|
|
SEM_FAILCRITICALERRORS = 0x0001
|
|
|
|
SEM_NOGPFAULTERRORBOX = 0x0002
|
|
|
|
SEM_NOALIGNMENTFAULTEXCEPT = 0x0004
|
|
|
|
SEM_NOOPENFILEERRORBOX = 0x8000
|
|
|
|
)
|
|
|
|
errormode := uint32(stdcall1(_SetErrorMode, SEM_NOGPFAULTERRORBOX))
|
|
|
|
stdcall1(_SetErrorMode, uintptr(errormode)|SEM_FAILCRITICALERRORS|SEM_NOGPFAULTERRORBOX|SEM_NOOPENFILEERRORBOX)
|
|
|
|
}
|
|
|
|
|
|
|
|
// in sys_windows_386.s and sys_windows_amd64.s
|
|
|
|
func exceptiontramp()
|
|
|
|
func firstcontinuetramp()
|
|
|
|
func lastcontinuetramp()
|
|
|
|
|
|
|
|
func initExceptionHandler() {
|
|
|
|
stdcall2(_AddVectoredExceptionHandler, 1, funcPC(exceptiontramp))
|
2015-07-05 18:34:01 -06:00
|
|
|
if _AddVectoredContinueHandler == nil || unsafe.Sizeof(&_AddVectoredContinueHandler) == 4 {
|
2015-04-12 20:02:44 -06:00
|
|
|
// use SetUnhandledExceptionFilter for windows-386 or
|
2015-07-05 18:34:01 -06:00
|
|
|
// if VectoredContinueHandler is unavailable.
|
2015-04-12 20:02:44 -06:00
|
|
|
// note: SetUnhandledExceptionFilter handler won't be called, if debugging.
|
|
|
|
stdcall1(_SetUnhandledExceptionFilter, funcPC(lastcontinuetramp))
|
|
|
|
} else {
|
|
|
|
stdcall2(_AddVectoredContinueHandler, 1, funcPC(firstcontinuetramp))
|
|
|
|
stdcall2(_AddVectoredContinueHandler, 0, funcPC(lastcontinuetramp))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-19 18:24:03 -07:00
|
|
|
func isgoexception(info *exceptionrecord, r *context) bool {
|
|
|
|
// Only handle exception if executing instructions in Go binary
|
|
|
|
// (not Windows library code).
|
2015-03-29 15:59:00 -06:00
|
|
|
// TODO(mwhudson): needs to loop to support shared libs
|
2015-04-06 18:55:02 -06:00
|
|
|
if r.ip() < firstmoduledata.text || firstmoduledata.etext < r.ip() {
|
2014-11-19 18:24:03 -07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-04-12 20:02:44 -06:00
|
|
|
// Go will only handle some exceptions.
|
|
|
|
switch info.exceptioncode {
|
|
|
|
default:
|
2014-11-19 18:24:03 -07:00
|
|
|
return false
|
2015-04-12 20:02:44 -06:00
|
|
|
case _EXCEPTION_ACCESS_VIOLATION:
|
|
|
|
case _EXCEPTION_INT_DIVIDE_BY_ZERO:
|
|
|
|
case _EXCEPTION_INT_OVERFLOW:
|
|
|
|
case _EXCEPTION_FLT_DENORMAL_OPERAND:
|
|
|
|
case _EXCEPTION_FLT_DIVIDE_BY_ZERO:
|
|
|
|
case _EXCEPTION_FLT_INEXACT_RESULT:
|
|
|
|
case _EXCEPTION_FLT_OVERFLOW:
|
|
|
|
case _EXCEPTION_FLT_UNDERFLOW:
|
|
|
|
case _EXCEPTION_BREAKPOINT:
|
2014-11-19 18:24:03 -07:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called by sigtramp from Windows VEH handler.
|
|
|
|
// Return value signals whether the exception has been handled (EXCEPTION_CONTINUE_EXECUTION)
|
|
|
|
// or should be made available to other handlers in the chain (EXCEPTION_CONTINUE_SEARCH).
|
|
|
|
func exceptionhandler(info *exceptionrecord, r *context, gp *g) int32 {
|
|
|
|
if !isgoexception(info, r) {
|
|
|
|
return _EXCEPTION_CONTINUE_SEARCH
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make it look like a call to the signal func.
|
|
|
|
// Have to pass arguments out of band since
|
|
|
|
// augmenting the stack frame would break
|
|
|
|
// the unwinding code.
|
|
|
|
gp.sig = info.exceptioncode
|
|
|
|
gp.sigcode0 = uintptr(info.exceptioninformation[0])
|
|
|
|
gp.sigcode1 = uintptr(info.exceptioninformation[1])
|
2015-02-11 18:14:02 -07:00
|
|
|
gp.sigpc = r.ip()
|
2014-11-19 18:24:03 -07:00
|
|
|
|
2015-02-11 18:14:02 -07:00
|
|
|
// Only push runtime·sigpanic if r.ip() != 0.
|
|
|
|
// If r.ip() == 0, probably panicked because of a
|
2016-03-01 16:21:55 -07:00
|
|
|
// call to a nil func. Not pushing that onto sp will
|
2014-11-19 18:24:03 -07:00
|
|
|
// make the trace look like a call to runtime·sigpanic instead.
|
|
|
|
// (Otherwise the trace will end at runtime·sigpanic and we
|
|
|
|
// won't get to see who faulted.)
|
2015-02-11 18:14:02 -07:00
|
|
|
if r.ip() != 0 {
|
|
|
|
sp := unsafe.Pointer(r.sp())
|
2016-02-29 16:01:00 -07:00
|
|
|
sp = add(sp, ^(unsafe.Sizeof(uintptr(0)) - 1)) // sp--
|
2015-02-11 18:14:02 -07:00
|
|
|
*((*uintptr)(sp)) = r.ip()
|
|
|
|
r.setsp(uintptr(sp))
|
2014-11-19 18:24:03 -07:00
|
|
|
}
|
2015-02-11 18:14:02 -07:00
|
|
|
r.setip(funcPC(sigpanic))
|
2014-11-19 18:24:03 -07:00
|
|
|
return _EXCEPTION_CONTINUE_EXECUTION
|
|
|
|
}
|
|
|
|
|
|
|
|
// It seems Windows searches ContinueHandler's list even
|
|
|
|
// if ExceptionHandler returns EXCEPTION_CONTINUE_EXECUTION.
|
|
|
|
// firstcontinuehandler will stop that search,
|
|
|
|
// if exceptionhandler did the same earlier.
|
|
|
|
func firstcontinuehandler(info *exceptionrecord, r *context, gp *g) int32 {
|
|
|
|
if !isgoexception(info, r) {
|
|
|
|
return _EXCEPTION_CONTINUE_SEARCH
|
|
|
|
}
|
|
|
|
return _EXCEPTION_CONTINUE_EXECUTION
|
|
|
|
}
|
|
|
|
|
2014-12-30 19:46:58 -07:00
|
|
|
var testingWER bool
|
|
|
|
|
2014-11-19 18:24:03 -07:00
|
|
|
// lastcontinuehandler is reached, because runtime cannot handle
|
|
|
|
// current exception. lastcontinuehandler will print crash info and exit.
|
2015-04-08 23:13:48 -06:00
|
|
|
func lastcontinuehandler(info *exceptionrecord, r *context, gp *g) int32 {
|
2014-12-30 19:46:58 -07:00
|
|
|
if testingWER {
|
|
|
|
return _EXCEPTION_CONTINUE_SEARCH
|
|
|
|
}
|
|
|
|
|
2014-11-19 18:24:03 -07:00
|
|
|
_g_ := getg()
|
|
|
|
|
|
|
|
if panicking != 0 { // traceback already printed
|
|
|
|
exit(2)
|
|
|
|
}
|
|
|
|
panicking = 1
|
|
|
|
|
2015-02-11 18:14:02 -07:00
|
|
|
print("Exception ", hex(info.exceptioncode), " ", hex(info.exceptioninformation[0]), " ", hex(info.exceptioninformation[1]), " ", hex(r.ip()), "\n")
|
2014-11-19 18:24:03 -07:00
|
|
|
|
2015-02-11 18:14:02 -07:00
|
|
|
print("PC=", hex(r.ip()), "\n")
|
2014-11-19 18:24:03 -07:00
|
|
|
if _g_.m.lockedg != nil && _g_.m.ncgo > 0 && gp == _g_.m.g0 {
|
2015-04-20 18:56:45 -06:00
|
|
|
if iscgo {
|
|
|
|
print("signal arrived during external code execution\n")
|
|
|
|
}
|
2014-11-19 18:24:03 -07:00
|
|
|
gp = _g_.m.lockedg
|
|
|
|
}
|
|
|
|
print("\n")
|
|
|
|
|
2015-10-30 09:03:02 -06:00
|
|
|
level, _, docrash := gotraceback()
|
|
|
|
if level > 0 {
|
2015-02-11 18:14:02 -07:00
|
|
|
tracebacktrap(r.ip(), r.sp(), 0, gp)
|
2014-11-19 18:24:03 -07:00
|
|
|
tracebackothers(gp)
|
|
|
|
dumpregs(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
if docrash {
|
|
|
|
crash()
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(2)
|
|
|
|
return 0 // not reached
|
|
|
|
}
|
|
|
|
|
2015-04-12 20:02:44 -06:00
|
|
|
func sigpanic() {
|
|
|
|
g := getg()
|
|
|
|
if !canpanic(g) {
|
|
|
|
throw("unexpected signal during runtime execution")
|
|
|
|
}
|
|
|
|
|
2016-02-29 16:01:00 -07:00
|
|
|
switch g.sig {
|
2015-04-12 20:02:44 -06:00
|
|
|
case _EXCEPTION_ACCESS_VIOLATION:
|
|
|
|
if g.sigcode1 < 0x1000 || g.paniconfault {
|
|
|
|
panicmem()
|
|
|
|
}
|
|
|
|
print("unexpected fault address ", hex(g.sigcode1), "\n")
|
|
|
|
throw("fault")
|
|
|
|
case _EXCEPTION_INT_DIVIDE_BY_ZERO:
|
|
|
|
panicdivide()
|
|
|
|
case _EXCEPTION_INT_OVERFLOW:
|
|
|
|
panicoverflow()
|
|
|
|
case _EXCEPTION_FLT_DENORMAL_OPERAND,
|
|
|
|
_EXCEPTION_FLT_DIVIDE_BY_ZERO,
|
|
|
|
_EXCEPTION_FLT_INEXACT_RESULT,
|
|
|
|
_EXCEPTION_FLT_OVERFLOW,
|
|
|
|
_EXCEPTION_FLT_UNDERFLOW:
|
|
|
|
panicfloat()
|
|
|
|
}
|
|
|
|
throw("fault")
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
badsignalmsg [100]byte
|
|
|
|
badsignallen int32
|
|
|
|
)
|
|
|
|
|
|
|
|
func setBadSignalMsg() {
|
|
|
|
const msg = "runtime: signal received on thread not created by Go.\n"
|
|
|
|
for i, c := range msg {
|
|
|
|
badsignalmsg[i] = byte(c)
|
|
|
|
badsignallen++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Following are not implemented.
|
|
|
|
|
2015-12-26 10:51:59 -07:00
|
|
|
func initsig(preinit bool) {
|
2015-04-12 20:02:44 -06:00
|
|
|
}
|
|
|
|
|
2014-11-19 18:24:03 -07:00
|
|
|
func sigenable(sig uint32) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func sigdisable(sig uint32) {
|
|
|
|
}
|
2015-01-29 20:37:41 -07:00
|
|
|
|
|
|
|
func sigignore(sig uint32) {
|
|
|
|
}
|
2015-04-12 20:02:44 -06:00
|
|
|
|
2015-07-21 23:34:48 -06:00
|
|
|
func badsignal2()
|
|
|
|
|
runtime: minor simplifications to signal code
Change setsig, setsigstack, getsig, raise, raiseproc to take uint32 for
signal number parameter, as that is the type mostly used for signal
numbers. Same for dieFromSignal, sigInstallGoHandler, raisebadsignal.
Remove setsig restart parameter, as it is always either true or
irrelevant.
Don't check the handler in setsigstack, as the only caller does that
anyhow.
Don't bother to convert the handler from sigtramp to sighandler in
getsig, as it will never be called when the handler is sigtramp or
sighandler.
Don't check the return value from rt_sigaction in the GNU/Linux version
of setsigstack; no other setsigstack checks it, and it never fails.
Change-Id: I6bbd677e048a77eddf974dd3d017bc3c560fbd48
Reviewed-on: https://go-review.googlesource.com/29953
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-09-27 23:24:51 -06:00
|
|
|
func raisebadsignal(sig uint32) {
|
2015-07-21 23:34:48 -06:00
|
|
|
badsignal2()
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:42:13 -06:00
|
|
|
func signame(sig uint32) string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-04-12 20:02:44 -06:00
|
|
|
func crash() {
|
|
|
|
// TODO: This routine should do whatever is needed
|
|
|
|
// to make the Windows program abort/crash as it
|
|
|
|
// would if Go was not intercepting signals.
|
|
|
|
// On Unix the routine would remove the custom signal
|
|
|
|
// handler and then raise a signal (like SIGABRT).
|
|
|
|
// Something like that should happen here.
|
|
|
|
// It's okay to leave this empty for now: if crash returns
|
|
|
|
// the ordinary exit-after-panic happens.
|
|
|
|
}
|