1
0
mirror of https://github.com/golang/go synced 2024-11-19 15:34:47 -07:00
go/src/runtime/signal_amd64x.go
Austin Clements 0efc8b2188 runtime: avoid repeated findmoduledatap calls
Currently almost every function that deals with a *_func has to first
look up the *moduledata for the module containing the function's entry
point. This means we almost always do at least two identical module
lookups whenever we deal with a *_func (one to get the *_func and
another to get something from its module data) and sometimes several
more.

Fix this by making findfunc return a new funcInfo type that embeds
*_func, but also includes the *moduledata, and making all of the
functions that currently take a *_func instead take a funcInfo and use
the already-found *moduledata.

This transformation is trivial for the most part, since the *_func
type is usually inferred. The annoying part is that we can no longer
use nil to indicate failure, so this introduces a funcInfo.valid()
method and replaces nil checks with calls to valid.

Change-Id: I9b8075ef1c31185c1943596d96dec45c7ab5100f
Reviewed-on: https://go-review.googlesource.com/37331
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Hudson-Doyle <michael.hudson@canonical.com>
2017-03-06 19:17:24 +00:00

95 lines
2.9 KiB
Go

// Copyright 2013 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.
// +build amd64 amd64p32
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
package runtime
import (
"runtime/internal/sys"
"unsafe"
)
func dumpregs(c *sigctxt) {
print("rax ", hex(c.rax()), "\n")
print("rbx ", hex(c.rbx()), "\n")
print("rcx ", hex(c.rcx()), "\n")
print("rdx ", hex(c.rdx()), "\n")
print("rdi ", hex(c.rdi()), "\n")
print("rsi ", hex(c.rsi()), "\n")
print("rbp ", hex(c.rbp()), "\n")
print("rsp ", hex(c.rsp()), "\n")
print("r8 ", hex(c.r8()), "\n")
print("r9 ", hex(c.r9()), "\n")
print("r10 ", hex(c.r10()), "\n")
print("r11 ", hex(c.r11()), "\n")
print("r12 ", hex(c.r12()), "\n")
print("r13 ", hex(c.r13()), "\n")
print("r14 ", hex(c.r14()), "\n")
print("r15 ", hex(c.r15()), "\n")
print("rip ", hex(c.rip()), "\n")
print("rflags ", hex(c.rflags()), "\n")
print("cs ", hex(c.cs()), "\n")
print("fs ", hex(c.fs()), "\n")
print("gs ", hex(c.gs()), "\n")
}
//go:nosplit
//go:nowritebarrierrec
func (c *sigctxt) sigpc() uintptr { return uintptr(c.rip()) }
func (c *sigctxt) sigsp() uintptr { return uintptr(c.rsp()) }
func (c *sigctxt) siglr() uintptr { return 0 }
func (c *sigctxt) fault() uintptr { return uintptr(c.sigaddr()) }
// preparePanic sets up the stack to look like a call to sigpanic.
func (c *sigctxt) preparePanic(sig uint32, gp *g) {
if GOOS == "darwin" {
// Work around Leopard bug that doesn't set FPE_INTDIV.
// Look at instruction to see if it is a divide.
// Not necessary in Snow Leopard (si_code will be != 0).
if sig == _SIGFPE && gp.sigcode0 == 0 {
pc := (*[4]byte)(unsafe.Pointer(gp.sigpc))
i := 0
if pc[i]&0xF0 == 0x40 { // 64-bit REX prefix
i++
} else if pc[i] == 0x66 { // 16-bit instruction prefix
i++
}
if pc[i] == 0xF6 || pc[i] == 0xF7 {
gp.sigcode0 = _FPE_INTDIV
}
}
}
pc := uintptr(c.rip())
sp := uintptr(c.rsp())
// If we don't recognize the PC as code
// but we do recognize the top pointer on the stack as code,
// then assume this was a call to non-code and treat like
// pc == 0, to make unwinding show the context.
if pc != 0 && !findfunc(pc).valid() && findfunc(*(*uintptr)(unsafe.Pointer(sp))).valid() {
pc = 0
}
// Only push runtime.sigpanic if pc != 0.
// If pc == 0, probably panicked because of a
// call to a nil func. Not pushing that onto sp will
// 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.)
if pc != 0 {
if sys.RegSize > sys.PtrSize {
sp -= sys.PtrSize
*(*uintptr)(unsafe.Pointer(sp)) = 0
}
sp -= sys.PtrSize
*(*uintptr)(unsafe.Pointer(sp)) = pc
c.set_rsp(uint64(sp))
}
c.set_rip(uint64(funcPC(sigpanic)))
}