-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
ptrace.go
157 lines (138 loc) · 3.74 KB
/
ptrace.go
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
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build linux
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
}
}
}