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 amd64 amd64p32
      6 // +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
      7 
      8 package runtime
      9 
     10 import (
     11 	"runtime/internal/sys"
     12 	"unsafe"
     13 )
     14 
     15 func dumpregs(c *sigctxt) {
     16 	print("rax    ", hex(c.rax()), "\n")
     17 	print("rbx    ", hex(c.rbx()), "\n")
     18 	print("rcx    ", hex(c.rcx()), "\n")
     19 	print("rdx    ", hex(c.rdx()), "\n")
     20 	print("rdi    ", hex(c.rdi()), "\n")
     21 	print("rsi    ", hex(c.rsi()), "\n")
     22 	print("rbp    ", hex(c.rbp()), "\n")
     23 	print("rsp    ", hex(c.rsp()), "\n")
     24 	print("r8     ", hex(c.r8()), "\n")
     25 	print("r9     ", hex(c.r9()), "\n")
     26 	print("r10    ", hex(c.r10()), "\n")
     27 	print("r11    ", hex(c.r11()), "\n")
     28 	print("r12    ", hex(c.r12()), "\n")
     29 	print("r13    ", hex(c.r13()), "\n")
     30 	print("r14    ", hex(c.r14()), "\n")
     31 	print("r15    ", hex(c.r15()), "\n")
     32 	print("rip    ", hex(c.rip()), "\n")
     33 	print("rflags ", hex(c.rflags()), "\n")
     34 	print("cs     ", hex(c.cs()), "\n")
     35 	print("fs     ", hex(c.fs()), "\n")
     36 	print("gs     ", hex(c.gs()), "\n")
     37 }
     38 
     39 //go:nosplit
     40 //go:nowritebarrierrec
     41 func (c *sigctxt) sigpc() uintptr { return uintptr(c.rip()) }
     42 
     43 func (c *sigctxt) sigsp() uintptr { return uintptr(c.rsp()) }
     44 func (c *sigctxt) siglr() uintptr { return 0 }
     45 func (c *sigctxt) fault() uintptr { return uintptr(c.sigaddr()) }
     46 
     47 // preparePanic sets up the stack to look like a call to sigpanic.
     48 func (c *sigctxt) preparePanic(sig uint32, gp *g) {
     49 	if GOOS == "darwin" {
     50 		// Work around Leopard bug that doesn't set FPE_INTDIV.
     51 		// Look at instruction to see if it is a divide.
     52 		// Not necessary in Snow Leopard (si_code will be != 0).
     53 		if sig == _SIGFPE && gp.sigcode0 == 0 {
     54 			pc := (*[4]byte)(unsafe.Pointer(gp.sigpc))
     55 			i := 0
     56 			if pc[i]&0xF0 == 0x40 { // 64-bit REX prefix
     57 				i++
     58 			} else if pc[i] == 0x66 { // 16-bit instruction prefix
     59 				i++
     60 			}
     61 			if pc[i] == 0xF6 || pc[i] == 0xF7 {
     62 				gp.sigcode0 = _FPE_INTDIV
     63 			}
     64 		}
     65 	}
     66 
     67 	pc := uintptr(c.rip())
     68 	sp := uintptr(c.rsp())
     69 
     70 	// If we don't recognize the PC as code
     71 	// but we do recognize the top pointer on the stack as code,
     72 	// then assume this was a call to non-code and treat like
     73 	// pc == 0, to make unwinding show the context.
     74 	if pc != 0 && !findfunc(pc).valid() && findfunc(*(*uintptr)(unsafe.Pointer(sp))).valid() {
     75 		pc = 0
     76 	}
     77 
     78 	// Only push runtime.sigpanic if pc != 0.
     79 	// If pc == 0, probably panicked because of a
     80 	// call to a nil func. Not pushing that onto sp will
     81 	// make the trace look like a call to runtime.sigpanic instead.
     82 	// (Otherwise the trace will end at runtime.sigpanic and we
     83 	// won't get to see who faulted.)
     84 	if pc != 0 {
     85 		if sys.RegSize > sys.PtrSize {
     86 			sp -= sys.PtrSize
     87 			*(*uintptr)(unsafe.Pointer(sp)) = 0
     88 		}
     89 		sp -= sys.PtrSize
     90 		*(*uintptr)(unsafe.Pointer(sp)) = pc
     91 		c.set_rsp(uint64(sp))
     92 	}
     93 	c.set_rip(uint64(funcPC(sigpanic)))
     94 }
     95