delve/proc/threads_linux.go
Derek Parker e4fc5e32c2 Refactor: Use thread-locked goroutine for ptrace ops
Previously either the terminal client or the debugger service would
either lock main goroutine to a thread or provide a locked goroutine to
run _all_ DebuggedProcess functions in. This is unnecessary because only
ptrace functions need to be run from the same thread that originated the
PT_ATTACH request.

Here we use a specific thread-locked goroutine to service any ptrace
request. That goroutine is also responsible for the initial spawning /
attaching of the process, since it must be responsible for the PT_ATTACH
request.
2015-06-13 12:57:42 -05:00

83 lines
1.9 KiB
Go

package proc
import (
"fmt"
sys "golang.org/x/sys/unix"
)
// Not actually used, but necessary
// to be defined.
type OSSpecificDetails struct {
registers sys.PtraceRegs
}
func (t *Thread) Halt() error {
if stopped(t.Id) {
return nil
}
err := sys.Tgkill(t.dbp.Pid, t.Id, sys.SIGSTOP)
if err != nil {
return fmt.Errorf("Halt err %s %d", err, t.Id)
}
_, _, err = wait(t.Id, 0)
if err != nil {
return fmt.Errorf("wait err %s %d", err, t.Id)
}
return nil
}
func (t *Thread) resume() (err error) {
t.dbp.execPtraceFunc(func() { err = PtraceCont(t.Id, 0) })
return
}
func (t *Thread) singleStep() (err error) {
t.dbp.execPtraceFunc(func() { err = sys.PtraceSingleStep(t.Id) })
if err != nil {
return err
}
_, _, err = wait(t.Id, 0)
return err
}
func (t *Thread) blocked() bool {
// TODO(dp) cache the func pc to remove this lookup
pc, _ := t.PC()
fn := t.dbp.goSymTable.PCToFunc(pc)
if fn != nil && ((fn.Name == "runtime.futex") || (fn.Name == "runtime.usleep") || (fn.Name == "runtime.clone")) {
return true
}
return false
}
func (thread *Thread) saveRegisters() (Registers, error) {
var err error
thread.dbp.execPtraceFunc(func() { err = sys.PtraceGetRegs(thread.Id, &thread.os.registers) })
if err != nil {
return nil, fmt.Errorf("could not save register contents")
}
return &Regs{&thread.os.registers}, nil
}
func (thread *Thread) restoreRegisters() (err error) {
thread.dbp.execPtraceFunc(func() { err = sys.PtraceSetRegs(thread.Id, &thread.os.registers) })
return
}
func writeMemory(thread *Thread, addr uintptr, data []byte) (written int, err error) {
if len(data) == 0 {
return
}
thread.dbp.execPtraceFunc(func() { written, err = sys.PtracePokeData(thread.Id, addr, data) })
return
}
func readMemory(thread *Thread, addr uintptr, data []byte) (read int, err error) {
if len(data) == 0 {
return
}
thread.dbp.execPtraceFunc(func() { read, err = sys.PtracePeekData(thread.Id, addr, data) })
return
}