1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
// Copyright 2014 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.
package server
import (
"fmt"
"os"
"runtime"
"syscall"
"time"
)
// ptraceRun runs all the closures from fc on a dedicated OS thread. Errors
// are returned on ec. Both channels must be unbuffered, to ensure that the
// resultant error is sent back to the same goroutine that sent the closure.
func ptraceRun(fc chan func() error, ec chan error) {
if cap(fc) != 0 || cap(ec) != 0 {
panic("ptraceRun was given buffered channels")
}
runtime.LockOSThread()
for f := range fc {
ec <- f()
}
}
func (s *Server) startProcess(name string, argv []string, attr *os.ProcAttr) (proc *os.Process, err error) {
s.fc <- func() error {
var err1 error
proc, err1 = os.StartProcess(name, argv, attr)
return err1
}
err = <-s.ec
return
}
func (s *Server) ptraceCont(pid int, signal int) (err error) {
s.fc <- func() error {
return syscall.PtraceCont(pid, signal)
}
return <-s.ec
}
func (s *Server) ptraceGetRegs(pid int, regsout *syscall.PtraceRegs) (err error) {
s.fc <- func() error {
return syscall.PtraceGetRegs(pid, regsout)
}
return <-s.ec
}
func (s *Server) ptracePeek(pid int, addr uintptr, out []byte) (err error) {
s.fc <- func() error {
n, err := syscall.PtracePeekText(pid, addr, out)
if err != nil {
return err
}
if n != len(out) {
return fmt.Errorf("ptracePeek: peeked %d bytes, want %d", n, len(out))
}
return nil
}
return <-s.ec
}
func (s *Server) ptracePoke(pid int, addr uintptr, data []byte) (err error) {
s.fc <- func() error {
n, err := syscall.PtracePokeText(pid, addr, data)
if err != nil {
return err
}
if n != len(data) {
return fmt.Errorf("ptracePoke: poked %d bytes, want %d", n, len(data))
}
return nil
}
return <-s.ec
}
func (s *Server) ptraceSetOptions(pid int, options int) (err error) {
s.fc <- func() error {
return syscall.PtraceSetOptions(pid, options)
}
return <-s.ec
}
func (s *Server) ptraceSetRegs(pid int, regs *syscall.PtraceRegs) (err error) {
s.fc <- func() error {
return syscall.PtraceSetRegs(pid, regs)
}
return <-s.ec
}
func (s *Server) ptraceSingleStep(pid int) (err error) {
s.fc <- func() error {
return syscall.PtraceSingleStep(pid)
}
return <-s.ec
}
type breakpointsChangedError struct {
call call
}
func (*breakpointsChangedError) Error() string {
return "breakpoints changed"
}
func (s *Server) wait(pid int, allowBreakpointsChange bool) (wpid int, status syscall.WaitStatus, err error) {
// We poll syscall.Wait4 with WNOHANG, sleeping in between, as a poor man's
// waitpid-with-timeout. This allows adding and removing breakpoints
// concurrently with waiting to hit an existing breakpoint.
f := func() error {
var err1 error
wpid, err1 = syscall.Wait4(pid, &status, syscall.WALL|syscall.WNOHANG, nil)
return err1
}
const (
minSleep = 1 * time.Microsecond
maxSleep = 100 * time.Millisecond
)
for sleep := minSleep; ; {
s.fc <- f
err = <-s.ec
// wpid == 0 means that wait found nothing (and returned due to WNOHANG).
if wpid != 0 {
return
}
if allowBreakpointsChange {
select {
case c := <-s.breakpointc:
return 0, 0, &breakpointsChangedError{c}
default:
}
}
time.Sleep(sleep)
if sleep < maxSleep {
sleep *= 10
}
}
}
|