-
Notifications
You must be signed in to change notification settings - Fork 4
/
debug_instance_breakpoint.go
246 lines (188 loc) · 5.46 KB
/
debug_instance_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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// SPDX-FileCopyrightText: 2023 Steffen Vogel <post@steffenvogel.de>
// SPDX-License-Identifier: Apache-2.0
// Limit debugger support to platforms which are supported by Delve
// See: https://github.com/go-delve/delve/blob/master/pkg/proc/native/support_sentinel_linux.go
//go:build linux && (amd64 || arm64 || 386)
package gont
import (
"fmt"
"path/filepath"
"sync/atomic"
"time"
"github.com/go-delve/delve/pkg/proc"
"github.com/go-delve/delve/service/api"
"github.com/stv0g/gont/v2/pkg/trace"
"go.uber.org/zap"
)
//nolint:gochecknoglobals
var watchpointLoadConfig = proc.LoadConfig{
MaxStringLen: 512,
MaxArrayValues: 512,
MaxStructFields: 32,
FollowPointers: true,
MaxVariableRecurse: 3,
}
type traceEventPoint interface {
traceEvent(*api.Thread, *debuggerInstance) trace.Event
}
type breakpointInstance struct {
*api.Breakpoint
// The tracepoint configuration used for creating the breakpoint
tracepoint *Tracepoint
// The watchpoint which has been created by this tracepoint
watchpoint *watchpointInstance
message *debugMessage
}
type watchpointInstance struct {
*api.Breakpoint
breakpoint *breakpointInstance
hitCount atomic.Uint64
}
func (bpi *breakpointInstance) prepare(i int) error {
var err error
tp := bpi.tracepoint
if tp.Name == "" {
tp.Name = fmt.Sprintf("%s (%d)", tp.Location, i)
}
if tp.Message != "" {
bpi.message, err = parseDebugMessage(tp.Message)
if err != nil {
return fmt.Errorf("failed to parse message: %w", err)
}
tp.Variables = append(tp.Variables, bpi.message.args...)
}
if tp.File != "" && !filepath.IsAbs(tp.File) {
if tp.File, err = filepath.Abs(tp.File); err != nil {
return err
}
}
return nil
}
func (bpi *breakpointInstance) createWatchpoint(d *debuggerInstance, thr *api.Thread) error {
var wpi *watchpointInstance
var err error
if wpi = bpi.watchpoint; wpi == nil {
wpi = &watchpointInstance{
breakpoint: bpi,
}
bpi.watchpoint = wpi
}
if wpi.Breakpoint, err = d.CreateWatchpoint(thr.GoroutineID, 0, 0, bpi.tracepoint.WatchExpr, bpi.tracepoint.WatchType); err != nil {
return err
}
wp := wpi.Breakpoint
wp.UserData = wpi
if err := d.AmendBreakpoint(wp); err != nil {
return fmt.Errorf("failed to amend watchpoint: %w", err)
}
d.logger.Debug("Created new watchpoint", breakpointFields(wp)...)
return nil
}
func (bpi *breakpointInstance) traceEvent(t *api.Thread, d *debuggerInstance) trace.Event {
var msg string
if bpi.message == nil {
msg = fmt.Sprintf("Hit breakpoint %d: %s", bpi.ID, bpi.Name)
} else {
msg = bpi.message.evaluate(t.BreakpointInfo.Variables)
}
e := trace.Event{
Timestamp: time.Now(),
Type: "breakpoint",
Message: msg,
Line: t.Line,
File: t.File,
Function: t.Function.Name_,
}
e.Breakpoint, e.Data = breakpointData(t.Breakpoint, t.BreakpointInfo)
return e
}
func (wpi *watchpointInstance) traceEvent(t *api.Thread, d *debuggerInstance) trace.Event {
e := trace.Event{
Timestamp: time.Now(),
Type: "watchpoint",
File: t.File,
Line: t.Line,
Function: t.Function.Name_,
Data: wpi.breakpoint,
}
e.Breakpoint, e.Data = breakpointData(t.Breakpoint, t.BreakpointInfo)
e.Breakpoint.Variables = wpi.evaluate(t, d)
e.Breakpoint.TotalHitCount = wpi.hitCount.Add(1)
if msg := wpi.breakpoint.message; msg != nil {
e.Message = msg.evaluate(e.Breakpoint.Variables)
} else {
e.Message = fmt.Sprintf("Hit watchpoint %d: %s", wpi.ID, wpi.Name)
}
return e
}
func (wpi *watchpointInstance) evaluate(t *api.Thread, d *debuggerInstance) []api.Variable {
tp := wpi.breakpoint
vars := []api.Variable{}
for _, v := range tp.Variables {
ev, err := d.EvalVariableInScope(t.GoroutineID, 0, 0, v, watchpointLoadConfig)
if err != nil {
continue
}
vars = append(vars, *api.ConvertVar(ev))
}
return vars
}
func (wpi *watchpointInstance) wentOutOfScope(d *debuggerInstance) error {
d.logger.Debug("Watch went out of scope", zap.Any("wp", wpi))
bp := wpi.breakpoint.Breakpoint
bp.Disabled = false
if err := d.AmendBreakpoint(bp); err != nil {
return fmt.Errorf("failed to reenable watchpoint: %w", err)
}
return nil
}
func breakpointFields(bp *api.Breakpoint) []zap.Field {
fields := []zap.Field{}
if bp.Name != "" {
fields = append(fields, zap.String("name", bp.Name))
}
if bp.FunctionName != "" {
fields = append(fields, zap.String("function", bp.FunctionName))
}
if bp.File != "" {
fields = append(fields, zap.String("file", bp.File))
}
if bp.Line != 0 {
fields = append(fields, zap.Int("line", bp.Line))
}
if bp.ID >= 0 {
fields = append(fields, zap.Int("id", bp.ID))
}
if bp.Addr != 0 {
fields = append(fields, zap.Uint64("addr", bp.Addr))
}
if len(bp.Addrs) > 0 {
fields = append(fields, zap.Uint64s("addrs", bp.Addrs))
}
if bp.Cond != "" {
fields = append(fields, zap.String("cond", bp.Cond))
}
if bp.WatchExpr != "" {
fields = append(fields, zap.String("watch_expr", bp.WatchExpr))
}
return fields
}
func breakpointData(bp *api.Breakpoint, bpInfo *api.BreakpointInfo) (*trace.Breakpoint, any) {
b := &trace.Breakpoint{
ID: bp.ID,
Name: bp.Name,
TotalHitCount: bp.TotalHitCount,
HitCount: bp.HitCount,
Variables: bpInfo.Variables,
Arguments: bpInfo.Arguments,
Locals: bpInfo.Locals,
Stacktrace: bpInfo.Stacktrace,
}
switch i := bp.UserData.(type) {
case *breakpointInstance:
return b, i.tracepoint.UserData
case *watchpointInstance:
return b, i.breakpoint.tracepoint.UserData
}
return b, nil
}