
On linux we can not read memory if the thread we use to do it is occupied doing certain system calls. The exact conditions when this happens have never been clear. This problem was worked around by using the Blocked method which recognized the most common circumstances where this would happen. However this is a hack: Blocked returning true doesn't mean that the problem will manifest and Blocked returning false doesn't necessarily mean the problem will not manifest. A side effect of this is issue #2151 where sometimes we can't read the memory of a thread and find its associated goroutine. This commit fixes this problem by always reading memory using a thread we know to be good for this, specifically the one returned by ContinueOnce. In particular the changes are as follows: 1. Remove (ProcessInternal).CurrentThread and (ProcessInternal).SetCurrentThread, the "current thread" becomes a field of Target, CurrentThread becomes a (*Target) method and (*Target).SwitchThread basically just sets a field Target. 2. The backends keep track of their own internal idea of what the current thread is, to use it to read memory, this is the thread they return from ContinueOnce as trapthread 3. The current thread in the backend and the current thread in Target only ever get synchronized in two places: when the backend creates a Target object the currentThread field of Target is initialized with the backend's current thread and when (*Target).Restart gets called (when a recording is rewound the currentThread used by Target might not exist anymore). 4. We remove the MemoryReadWriter interface embedded in Thread and instead add a Memory method to Process that returns a MemoryReadWriter. The backends will return something here that will read memory using the current thread saved by the backend. 5. The Thread.Blocked method is removed One possible problem with this change is processes that have threads with different memory maps. As far as I can determine this could happen on old versions of linux but this option was removed in linux 2.5. Fixes #2151
155 lines
3.6 KiB
Go
155 lines
3.6 KiB
Go
package native
|
|
|
|
import (
|
|
"errors"
|
|
"syscall"
|
|
|
|
sys "golang.org/x/sys/windows"
|
|
|
|
"github.com/go-delve/delve/pkg/proc"
|
|
"github.com/go-delve/delve/pkg/proc/winutil"
|
|
)
|
|
|
|
// waitStatus is a synonym for the platform-specific WaitStatus
|
|
type waitStatus sys.WaitStatus
|
|
|
|
// osSpecificDetails holds information specific to the Windows
|
|
// operating system / kernel.
|
|
type osSpecificDetails struct {
|
|
hThread syscall.Handle
|
|
}
|
|
|
|
func (t *nativeThread) singleStep() error {
|
|
context := winutil.NewCONTEXT()
|
|
context.ContextFlags = _CONTEXT_ALL
|
|
|
|
// Set the processor TRAP flag
|
|
err := _GetThreadContext(t.os.hThread, context)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
context.EFlags |= 0x100
|
|
|
|
err = _SetThreadContext(t.os.hThread, context)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
suspendcnt := 0
|
|
|
|
// If a thread simultaneously hits a breakpoint and is suspended by the Go
|
|
// runtime it will have a suspend count greater than 1 and to actually take
|
|
// a single step we have to resume it multiple times here.
|
|
// We keep a counter of how many times it was suspended so that after
|
|
// single-stepping we can re-suspend it the corrent number of times.
|
|
for {
|
|
n, err := _ResumeThread(t.os.hThread)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
suspendcnt++
|
|
if n == 1 {
|
|
break
|
|
}
|
|
}
|
|
|
|
for {
|
|
var tid, exitCode int
|
|
t.dbp.execPtraceFunc(func() {
|
|
tid, exitCode, err = t.dbp.waitForDebugEvent(waitBlocking | waitSuspendNewThreads)
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if tid == 0 {
|
|
t.dbp.postExit()
|
|
return proc.ErrProcessExited{Pid: t.dbp.pid, Status: exitCode}
|
|
}
|
|
|
|
if t.dbp.os.breakThread == t.ID {
|
|
break
|
|
}
|
|
|
|
t.dbp.execPtraceFunc(func() {
|
|
err = _ContinueDebugEvent(uint32(t.dbp.pid), uint32(t.dbp.os.breakThread), _DBG_CONTINUE)
|
|
})
|
|
}
|
|
|
|
for i := 0; i < suspendcnt; i++ {
|
|
_, err = _SuspendThread(t.os.hThread)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
t.dbp.execPtraceFunc(func() {
|
|
err = _ContinueDebugEvent(uint32(t.dbp.pid), uint32(t.ID), _DBG_CONTINUE)
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Unset the processor TRAP flag
|
|
err = _GetThreadContext(t.os.hThread, context)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
context.EFlags &= ^uint32(0x100)
|
|
|
|
return _SetThreadContext(t.os.hThread, context)
|
|
}
|
|
|
|
func (t *nativeThread) resume() error {
|
|
var err error
|
|
t.dbp.execPtraceFunc(func() {
|
|
//TODO: Note that we are ignoring the thread we were asked to continue and are continuing the
|
|
//thread that we last broke on.
|
|
err = _ContinueDebugEvent(uint32(t.dbp.pid), uint32(t.ID), _DBG_CONTINUE)
|
|
})
|
|
return err
|
|
}
|
|
|
|
// Stopped returns whether the thread is stopped at the operating system
|
|
// level. On windows this always returns true.
|
|
func (t *nativeThread) Stopped() bool {
|
|
return true
|
|
}
|
|
|
|
func (t *nativeThread) WriteMemory(addr uint64, data []byte) (int, error) {
|
|
if t.dbp.exited {
|
|
return 0, proc.ErrProcessExited{Pid: t.dbp.pid}
|
|
}
|
|
if len(data) == 0 {
|
|
return 0, nil
|
|
}
|
|
var count uintptr
|
|
err := _WriteProcessMemory(t.dbp.os.hProcess, uintptr(addr), &data[0], uintptr(len(data)), &count)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return int(count), nil
|
|
}
|
|
|
|
var ErrShortRead = errors.New("short read")
|
|
|
|
func (t *nativeThread) ReadMemory(buf []byte, addr uint64) (int, error) {
|
|
if t.dbp.exited {
|
|
return 0, proc.ErrProcessExited{Pid: t.dbp.pid}
|
|
}
|
|
if len(buf) == 0 {
|
|
return 0, nil
|
|
}
|
|
var count uintptr
|
|
err := _ReadProcessMemory(t.dbp.os.hProcess, uintptr(addr), &buf[0], uintptr(len(buf)), &count)
|
|
if err == nil && count != uintptr(len(buf)) {
|
|
err = ErrShortRead
|
|
}
|
|
return int(count), err
|
|
}
|
|
|
|
func (t *nativeThread) restoreRegisters(savedRegs proc.Registers) error {
|
|
return _SetThreadContext(t.os.hThread, savedRegs.(*winutil.AMD64Registers).Context)
|
|
}
|