-
Notifications
You must be signed in to change notification settings - Fork 3
/
breakpoint.go
219 lines (192 loc) · 4.63 KB
/
breakpoint.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"errors"
"fmt"
"go.uber.org/zap"
"os"
"path"
"syscall"
)
type BInfo struct {
original []byte
filename string
lineno int
pc uint64
kind BPKIND
}
type BP struct {
infos []*BInfo
pid int
}
type BPKIND uint64
const (
USERBPTYPE BPKIND = 1
INTERNALBPTYPE BPKIND = 2
)
func (b *BPKIND) String() string {
if *b == USERBPTYPE {
return "USERBPTYPE"
}
if *b == INTERNALBPTYPE {
return "INTERNALBPTYPE"
}
return "unknown"
}
func (bp *BP) setPcBreakPoint(pid int, pc uint64) ([]byte, error) {
// no need to add RwLock
var err error
if bp.infos == nil {
bp.infos = make([]*BInfo, 0, 1)
}
for _, v := range bp.infos {
if v.pc == pc {
return nil, HasExistedBreakPointErr
}
}
original := make([]byte, 1)
_, err = syscall.PtracePeekData(pid, uintptr(pc), original)
if err != nil {
return nil, err
}
_, err = syscall.PtracePokeData(pid, uintptr(pc), []byte{0xCC})
if err != nil {
return nil, err
}
return original, nil
}
func (bp *BP) SetInternalBreakPoint(pid int, pc uint64) (*BInfo, error) {
var (
original []byte
err error
)
if original, err = bp.setPcBreakPoint(pid, pc); err != nil {
return nil, err
}
bInfo := &BInfo{original: original, filename: "", lineno: 0, pc: pc, kind: INTERNALBPTYPE}
bp.infos = append(bp.infos, bInfo)
return bInfo, nil
}
func (bp *BP) SetFileLineBreakPoint(bi *BI, pid int, filename string, lineno int) (*BInfo, error) {
logger.Debug("SetFileLineBreakPoint", zap.String("filename", filename), zap.Int("lineno", lineno))
curDir, err := os.Getwd()
if err != nil {
logger.Error("SetFileLineBreakPoint:GetWd", zap.Error(err), zap.Int(filename, lineno))
return nil, err
}
fullfilename := path.Join(curDir, filename)
pc, err := bi.fileLineToPcForBreakPoint(fullfilename, lineno)
if err != nil {
logger.Error("SetFileLineBreakPoint:fileLineToPc",
zap.Error(err),
zap.Int(fullfilename, lineno))
return nil, err
}
logger.Debug("SetFileLineBreakPoint:fileLineToPc",
zap.Uint64("pc", pc),
zap.String("fullfilename", fullfilename),
zap.Int("lineno", lineno))
var (
info *BInfo
original []byte
)
if original, err = bp.setPcBreakPoint(pid, pc); err != nil {
logger.Error("SetFileLineBreakPoint",
zap.Error(err),
zap.Int("Pid", pid),
zap.String("fullfilename", fullfilename),
zap.Int("lineno", lineno))
return nil, err
}
info = &BInfo{original: original, filename: filename, lineno: lineno, pc: pc, kind: USERBPTYPE}
bp.infos = append(bp.infos, info)
return info, err
}
func (bp *BP) Continue(pid int) error {
return syscall.PtraceCont(pid, 0)
}
func (bp *BP) findBreakPoint(pc uint64) (*BInfo, bool) {
for _, v := range bp.infos {
if v.pc == pc {
return v, true
}
}
return nil, false
}
func (bp *BP) enableBreakPoint(pid int, info *BInfo) error {
if info == nil {
return errors.New("enableBreakPoint breakpointinfo is null")
}
logger.Debug("enableBreakPoint", zap.Uint64("pc", info.pc))
if _, err := syscall.PtracePokeData(pid, uintptr(info.pc), []byte{0xCC}); err != nil {
return err
}
return nil
}
func (bp *BP) disableBreakPoint(pid int, info *BInfo) error {
if info == nil {
return errors.New("disableBreakPoint breakpointinfo is null")
}
logger.Debug("disableBreakPoint", zap.Uint64("pc", info.pc))
if _, err := syscall.PtracePokeData(pid, uintptr(info.pc), info.original); err != nil {
return err
}
return nil
}
func (bp *BP) singleStepInstructionWithBreakpointCheck(pid int) error {
var (
pc uint64
err error
info *BInfo
ok bool
)
if pc, err = getPtracePc(); err != nil {
return err
}
pc = pc - 1
if info, ok = bp.findBreakPoint(pc); !ok {
return nil
}
if err = bp.disableBreakPoint(pid, info); err != nil {
return err
}
defer bp.enableBreakPoint(pid, info)
if err = setPcRegister(target.cmd, pc); err != nil {
return err
}
if err = syscall.PtraceSingleStep(pid); err != nil {
return err
}
var s syscall.WaitStatus
if _, err = syscall.Wait4(pid, &s, syscall.WALL, nil); err != nil {
return err
}
if s.Exited() {
return nil
}
if s.StopSignal() == syscall.SIGTRAP {
return nil
}
return fmt.Errorf("unknown waitstatus %v, signal %d", s, s.Signal())
}
func (bp *BP) clearInternalBreakPoint(pc uint64) {
infos := make([]*BInfo, 0, len(bp.infos))
for _, v := range bp.infos {
if !(v.kind == INTERNALBPTYPE && v.pc == pc) {
infos = append(infos, v)
}
}
bp.infos = infos
}
func (bp *BP) SetBpWhenRestart(pid int) error {
for _, v := range bp.infos {
if v.kind == INTERNALBPTYPE {
bp.clearInternalBreakPoint(v.pc)
}
if v.kind == USERBPTYPE {
if err := bp.enableBreakPoint(pid, v); err != nil {
return err
}
}
}
return nil
}