Home | History | Annotate | Download | only in runtime
      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 //go:nosplit
     31 //go:nowritebarrierrec
     32 func (c *sigctxt) sigpc() uintptr { return uintptr(c.eip()) }
     33 
     34 func (c *sigctxt) sigsp() uintptr { return uintptr(c.esp()) }
     35 func (c *sigctxt) siglr() uintptr { return 0 }
     36 func (c *sigctxt) fault() uintptr { return uintptr(c.sigaddr()) }
     37 
     38 // preparePanic sets up the stack to look like a call to sigpanic.
     39 func (c *sigctxt) preparePanic(sig uint32, gp *g) {
     40 	if GOOS == "darwin" {
     41 		// Work around Leopard bug that doesn't set FPE_INTDIV.
     42 		// Look at instruction to see if it is a divide.
     43 		// Not necessary in Snow Leopard (si_code will be != 0).
     44 		if sig == _SIGFPE && gp.sigcode0 == 0 {
     45 			pc := (*[4]byte)(unsafe.Pointer(gp.sigpc))
     46 			i := 0
     47 			if pc[i] == 0x66 { // 16-bit instruction prefix
     48 				i++
     49 			}
     50 			if pc[i] == 0xF6 || pc[i] == 0xF7 {
     51 				gp.sigcode0 = _FPE_INTDIV
     52 			}
     53 		}
     54 	}
     55 
     56 	pc := uintptr(c.eip())
     57 	sp := uintptr(c.esp())
     58 
     59 	// If we don't recognize the PC as code
     60 	// but we do recognize the top pointer on the stack as code,
     61 	// then assume this was a call to non-code and treat like
     62 	// pc == 0, to make unwinding show the context.
     63 	if pc != 0 && !findfunc(pc).valid() && findfunc(*(*uintptr)(unsafe.Pointer(sp))).valid() {
     64 		pc = 0
     65 	}
     66 
     67 	// Only push runtime.sigpanic if pc != 0.
     68 	// If pc == 0, probably panicked because of a
     69 	// call to a nil func. Not pushing that onto sp will
     70 	// make the trace look like a call to runtime.sigpanic instead.
     71 	// (Otherwise the trace will end at runtime.sigpanic and we
     72 	// won't get to see who faulted.)
     73 	if pc != 0 {
     74 		if sys.RegSize > sys.PtrSize {
     75 			sp -= sys.PtrSize
     76 			*(*uintptr)(unsafe.Pointer(sp)) = 0
     77 		}
     78 		sp -= sys.PtrSize
     79 		*(*uintptr)(unsafe.Pointer(sp)) = pc
     80 		c.set_esp(uint32(sp))
     81 	}
     82 	c.set_eip(uint32(funcPC(sigpanic)))
     83 }
     84