github.com/4ad/go@v0.0.0-20161219182952-69a12818b605/src/runtime/signal_386.go (about)

     1  // Copyright 2013 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build darwin dragonfly freebsd linux nacl netbsd openbsd
     6  
     7  package runtime
     8  
     9  import (
    10  	"runtime/internal/sys"
    11  	"unsafe"
    12  )
    13  
    14  func dumpregs(c *sigctxt) {
    15  	print("eax    ", hex(c.eax()), "\n")
    16  	print("ebx    ", hex(c.ebx()), "\n")
    17  	print("ecx    ", hex(c.ecx()), "\n")
    18  	print("edx    ", hex(c.edx()), "\n")
    19  	print("edi    ", hex(c.edi()), "\n")
    20  	print("esi    ", hex(c.esi()), "\n")
    21  	print("ebp    ", hex(c.ebp()), "\n")
    22  	print("esp    ", hex(c.esp()), "\n")
    23  	print("eip    ", hex(c.eip()), "\n")
    24  	print("eflags ", hex(c.eflags()), "\n")
    25  	print("cs     ", hex(c.cs()), "\n")
    26  	print("fs     ", hex(c.fs()), "\n")
    27  	print("gs     ", hex(c.gs()), "\n")
    28  }
    29  
    30  var crashing int32
    31  
    32  // May run during STW, so write barriers are not allowed.
    33  //
    34  //go:nowritebarrierrec
    35  func sighandler(sig uint32, info *siginfo, ctxt unsafe.Pointer, gp *g) {
    36  	_g_ := getg()
    37  	c := &sigctxt{info, ctxt}
    38  
    39  	if sig == _SIGPROF {
    40  		sigprof(uintptr(c.eip()), uintptr(c.esp()), 0, gp, _g_.m)
    41  		return
    42  	}
    43  
    44  	flags := int32(_SigThrow)
    45  	if sig < uint32(len(sigtable)) {
    46  		flags = sigtable[sig].flags
    47  	}
    48  	if c.sigcode() != _SI_USER && flags&_SigPanic != 0 {
    49  		// Make it look like a call to the signal func.
    50  		// Have to pass arguments out of band since
    51  		// augmenting the stack frame would break
    52  		// the unwinding code.
    53  		gp.sig = sig
    54  		gp.sigcode0 = uintptr(c.sigcode())
    55  		gp.sigcode1 = uintptr(c.sigaddr())
    56  		gp.sigpc = uintptr(c.eip())
    57  
    58  		if GOOS == "darwin" {
    59  			// Work around Leopard bug that doesn't set FPE_INTDIV.
    60  			// Look at instruction to see if it is a divide.
    61  			// Not necessary in Snow Leopard (si_code will be != 0).
    62  			if sig == _SIGFPE && gp.sigcode0 == 0 {
    63  				pc := (*[4]byte)(unsafe.Pointer(gp.sigpc))
    64  				i := 0
    65  				if pc[i] == 0x66 { // 16-bit instruction prefix
    66  					i++
    67  				}
    68  				if pc[i] == 0xF6 || pc[i] == 0xF7 {
    69  					gp.sigcode0 = _FPE_INTDIV
    70  				}
    71  			}
    72  		}
    73  
    74  		pc := uintptr(c.eip())
    75  		sp := uintptr(c.esp())
    76  
    77  		// If we don't recognize the PC as code
    78  		// but we do recognize the top pointer on the stack as code,
    79  		// then assume this was a call to non-code and treat like
    80  		// pc == 0, to make unwinding show the context.
    81  		if pc != 0 && findfunc(pc) == nil && findfunc(*(*uintptr)(unsafe.Pointer(sp))) != nil {
    82  			pc = 0
    83  		}
    84  
    85  		// Only push runtime.sigpanic if pc != 0.
    86  		// If pc == 0, probably panicked because of a
    87  		// call to a nil func. Not pushing that onto sp will
    88  		// make the trace look like a call to runtime.sigpanic instead.
    89  		// (Otherwise the trace will end at runtime.sigpanic and we
    90  		// won't get to see who faulted.)
    91  		if pc != 0 {
    92  			if sys.RegSize > sys.PtrSize {
    93  				sp -= sys.PtrSize
    94  				*(*uintptr)(unsafe.Pointer(sp)) = 0
    95  			}
    96  			sp -= sys.PtrSize
    97  			*(*uintptr)(unsafe.Pointer(sp)) = pc
    98  			c.set_esp(uint32(sp))
    99  		}
   100  		c.set_eip(uint32(funcPC(sigpanic)))
   101  		return
   102  	}
   103  
   104  	if c.sigcode() == _SI_USER || flags&_SigNotify != 0 {
   105  		if sigsend(sig) {
   106  			return
   107  		}
   108  	}
   109  
   110  	if c.sigcode() == _SI_USER && signal_ignored(sig) {
   111  		return
   112  	}
   113  
   114  	if flags&_SigKill != 0 {
   115  		dieFromSignal(int32(sig))
   116  	}
   117  
   118  	if flags&_SigThrow == 0 {
   119  		return
   120  	}
   121  
   122  	_g_.m.throwing = 1
   123  	_g_.m.caughtsig.set(gp)
   124  
   125  	if crashing == 0 {
   126  		startpanic()
   127  	}
   128  
   129  	if sig < uint32(len(sigtable)) {
   130  		print(sigtable[sig].name, "\n")
   131  	} else {
   132  		print("Signal ", sig, "\n")
   133  	}
   134  
   135  	print("PC=", hex(c.eip()), " m=", _g_.m.id, "\n")
   136  	if _g_.m.lockedg != nil && _g_.m.ncgo > 0 && gp == _g_.m.g0 {
   137  		print("signal arrived during cgo execution\n")
   138  		gp = _g_.m.lockedg
   139  	}
   140  	print("\n")
   141  
   142  	level, _, docrash := gotraceback()
   143  	if level > 0 {
   144  		goroutineheader(gp)
   145  		tracebacktrap(uintptr(c.eip()), uintptr(c.esp()), 0, gp)
   146  		if crashing > 0 && gp != _g_.m.curg && _g_.m.curg != nil && readgstatus(_g_.m.curg)&^_Gscan == _Grunning {
   147  			// tracebackothers on original m skipped this one; trace it now.
   148  			goroutineheader(_g_.m.curg)
   149  			traceback(^uintptr(0), ^uintptr(0), 0, gp)
   150  		} else if crashing == 0 {
   151  			tracebackothers(gp)
   152  			print("\n")
   153  		}
   154  		dumpregs(c)
   155  	}
   156  
   157  	if docrash {
   158  		crashing++
   159  		if crashing < sched.mcount {
   160  			// There are other m's that need to dump their stacks.
   161  			// Relay SIGQUIT to the next m by sending it to the current process.
   162  			// All m's that have already received SIGQUIT have signal masks blocking
   163  			// receipt of any signals, so the SIGQUIT will go to an m that hasn't seen it yet.
   164  			// When the last m receives the SIGQUIT, it will fall through to the call to
   165  			// crash below. Just in case the relaying gets botched, each m involved in
   166  			// the relay sleeps for 5 seconds and then does the crash/exit itself.
   167  			// In expected operation, the last m has received the SIGQUIT and run
   168  			// crash/exit and the process is gone, all long before any of the
   169  			// 5-second sleeps have finished.
   170  			print("\n-----\n\n")
   171  			raiseproc(_SIGQUIT)
   172  			usleep(5 * 1000 * 1000)
   173  		}
   174  		crash()
   175  	}
   176  
   177  	exit(2)
   178  }