delve/pkg/proc/native/threads_linux.go

149 lines
3.6 KiB
Go
Raw Normal View History

package native
2015-01-14 02:37:10 +00:00
import (
"fmt"
"syscall"
"unsafe"
2015-01-14 02:37:10 +00:00
sys "golang.org/x/sys/unix"
"github.com/go-delve/delve/pkg/proc"
"github.com/go-delve/delve/pkg/proc/linutil"
2015-01-14 02:37:10 +00:00
)
2016-01-15 05:26:54 +00:00
type WaitStatus sys.WaitStatus
2016-01-10 08:57:52 +00:00
// OSSpecificDetails hold Linux specific
// process details.
type OSSpecificDetails struct {
delayedSignal int
registers sys.PtraceRegs
running bool
}
2015-01-14 02:37:10 +00:00
func (t *Thread) stop() (err error) {
2017-02-08 00:23:47 +00:00
err = sys.Tgkill(t.dbp.pid, t.ID, sys.SIGSTOP)
2015-01-14 02:37:10 +00:00
if err != nil {
err = fmt.Errorf("stop err %s on thread %d", err, t.ID)
return
2015-01-14 02:37:10 +00:00
}
return
}
// Stopped returns whether the thread is stopped at
// the operating system level.
func (t *Thread) Stopped() bool {
2016-01-10 08:57:52 +00:00
state := status(t.ID, t.dbp.os.comm)
return state == StatusTraceStop || state == StatusTraceStopT
2015-01-14 02:37:10 +00:00
}
func (t *Thread) resume() error {
sig := t.os.delayedSignal
t.os.delayedSignal = 0
return t.resumeWithSig(sig)
}
func (t *Thread) resumeWithSig(sig int) (err error) {
t.os.running = true
t.dbp.execPtraceFunc(func() { err = PtraceCont(t.ID, sig) })
return
2015-01-14 02:37:10 +00:00
}
func (t *Thread) singleStep() (err error) {
for {
2016-01-10 08:57:52 +00:00
t.dbp.execPtraceFunc(func() { err = sys.PtraceSingleStep(t.ID) })
if err != nil {
return err
}
wpid, status, err := t.dbp.waitFast(t.ID)
if err != nil {
return err
}
2017-02-08 00:23:47 +00:00
if (status == nil || status.Exited()) && wpid == t.dbp.pid {
t.dbp.postExit()
rs := 0
if status != nil {
rs = status.ExitStatus()
}
return proc.ErrProcessExited{Pid: t.dbp.pid, Status: rs}
}
2016-01-10 08:57:52 +00:00
if wpid == t.ID && status.StopSignal() == sys.SIGTRAP {
return nil
}
2015-01-14 02:37:10 +00:00
}
}
func (t *Thread) Blocked() bool {
regs, err := t.Registers(false)
if err != nil {
return false
}
pc := regs.PC()
fn := t.BinInfo().PCToFunc(pc)
2015-02-28 14:05:37 +00:00
if fn != nil && ((fn.Name == "runtime.futex") || (fn.Name == "runtime.usleep") || (fn.Name == "runtime.clone")) {
2015-02-27 23:11:13 +00:00
return true
}
return false
}
func (t *Thread) restoreRegisters(savedRegs proc.Registers) error {
sr := savedRegs.(*linutil.AMD64Registers)
var restoreRegistersErr error
t.dbp.execPtraceFunc(func() {
oldRegs := (*sys.PtraceRegs)(sr.Regs)
var currentRegs sys.PtraceRegs
restoreRegistersErr = sys.PtraceGetRegs(t.ID, &currentRegs)
if restoreRegistersErr != nil {
return
}
// restoreRegisters is only supposed to restore CPU registers, not FS_BASE and GS_BASE
oldRegs.Fs_base = currentRegs.Fs_base
oldRegs.Gs_base = currentRegs.Gs_base
restoreRegistersErr = sys.PtraceSetRegs(t.ID, oldRegs)
if restoreRegistersErr != nil {
return
}
if sr.Fpregset.Xsave != nil {
iov := sys.Iovec{Base: &sr.Fpregset.Xsave[0], Len: uint64(len(sr.Fpregset.Xsave))}
_, _, restoreRegistersErr = syscall.Syscall6(syscall.SYS_PTRACE, sys.PTRACE_SETREGSET, uintptr(t.ID), _NT_X86_XSTATE, uintptr(unsafe.Pointer(&iov)), 0, 0)
return
}
2015-01-14 02:37:10 +00:00
_, _, restoreRegistersErr = syscall.Syscall6(syscall.SYS_PTRACE, sys.PTRACE_SETFPREGS, uintptr(t.ID), uintptr(0), uintptr(unsafe.Pointer(&sr.Fpregset.AMD64PtraceFpRegs)), 0, 0)
return
})
if restoreRegistersErr == syscall.Errno(0) {
restoreRegistersErr = nil
}
return restoreRegistersErr
2015-01-14 02:37:10 +00:00
}
func (t *Thread) WriteMemory(addr uintptr, data []byte) (written int, err error) {
if t.dbp.exited {
return 0, proc.ErrProcessExited{Pid: t.dbp.pid}
}
if len(data) == 0 {
return
}
2016-01-10 08:57:52 +00:00
t.dbp.execPtraceFunc(func() { written, err = sys.PtracePokeData(t.ID, addr, data) })
return
}
func (t *Thread) ReadMemory(data []byte, addr uintptr) (n int, err error) {
if t.dbp.exited {
return 0, proc.ErrProcessExited{Pid: t.dbp.pid}
}
if len(data) == 0 {
return
}
2016-01-10 08:57:52 +00:00
t.dbp.execPtraceFunc(func() { _, err = sys.PtracePeekData(t.ID, addr, data) })
if err == nil {
n = len(data)
}
return
}