-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
stepinto.go
371 lines (314 loc) · 7.85 KB
/
stepinto.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package main
import (
"bytes"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"sort"
"strings"
"github.com/aarzilli/gdlv/internal/dlvclient/service/api"
)
type stmtsInLocVisitor struct {
r []*ast.CallExpr
pkg string
fset *token.FileSet
loc api.Location
isType map[string]bool
}
type stmtsInLocVisitorSkip struct {
w *stmtsInLocVisitor
}
type callKind uint8
const (
typeCall callKind = iota
builtinCall
funCall
)
func kindOfCall(pkg string, fun ast.Node, isType map[string]bool) callKind {
switch fun1 := fun.(type) {
case (*ast.ParenExpr):
return kindOfCall(pkg, fun1.X, isType)
case (*ast.StarExpr):
return kindOfCall(pkg, fun1.X, isType)
case (*ast.ArrayType):
return typeCall
case (*ast.FuncType):
return typeCall
case (*ast.InterfaceType):
return typeCall
case (*ast.StructType):
return typeCall
case (*ast.MapType):
return typeCall
case (*ast.SelectorExpr):
lident, ok := fun1.X.(*ast.Ident)
if !ok {
return funCall
}
if isType[fmt.Sprintf("%s.%s", lident.Name, fun1.Sel.Name)] {
return typeCall
}
return funCall
case (*ast.Ident):
switch fun1.Name {
case "append", "cap", "close", "complex", "copy", "delete", "imag", "len", "make", "new", "panic", "print", "println", "real", "recover":
return builtinCall
case "byte", "rune":
return typeCall
}
if isType[pkg+"."+fun1.Name] || isType[fun1.Name] {
return typeCall
}
return funCall
default:
return funCall
}
}
func (w *stmtsInLocVisitor) Visit(n ast.Node) (wret ast.Visitor) {
if n == nil {
return w
}
switch sn := n.(type) {
case (*ast.DeferStmt):
return &stmtsInLocVisitorSkip{w}
case (*ast.GoStmt):
return &stmtsInLocVisitorSkip{w}
case (*ast.File):
w.pkg = sn.Name.Name
case (*ast.CallExpr):
start := w.fset.Position(n.End())
ok := start.Filename == w.loc.File
ok = ok && start.Line == w.loc.Line
ok = ok && kindOfCall(w.pkg, sn.Fun, w.isType) == funCall
if ok {
w.r = append(w.r, sn)
}
default:
//TODO: calculate the pos of the first character in w.loc.File:w.loc.Line and check that is between n.Pos() and n.End()
}
return w
}
func (w *stmtsInLocVisitorSkip) Visit(n ast.Node) (wret ast.Visitor) {
return w.w
}
// Reorder a list of calls so that a function call appears after all its arguments are evaluated
func reorderCalls(calls []*ast.CallExpr) {
for i := 0; i < len(calls); {
end := calls[i].End()
argstart := i + 1
argend := argstart
for ; argend < len(calls); argend++ {
if calls[argend].Pos() >= end {
break
}
}
if argend > argstart {
reorderCalls(calls[argstart:argend])
curcall := calls[i]
copy(calls[i:argend-1], calls[argstart:argend])
calls[argend-1] = curcall
}
i = argend
}
}
func stmtsInLoc(loc api.Location, n ast.Node, fset *token.FileSet, isType map[string]bool) []*ast.CallExpr {
visitor := stmtsInLocVisitor{fset: fset, loc: loc, isType: isType}
ast.Walk(&visitor, n)
reorderCalls(visitor.r)
return visitor.r
}
type stepIntoCall struct {
Name string
Inst api.AsmInstruction
X *ast.CallExpr
fset *token.FileSet
}
func (sic *stepIntoCall) ColInterval() (int, int) {
return sic.fset.Position(sic.X.Fun.Pos()).Column, sic.fset.Position(sic.X.Fun.End()).Column
}
func (sic *stepIntoCall) Filename() string {
return sic.fset.Position(sic.X.Fun.Pos()).Filename
}
func (sic *stepIntoCall) Line() int {
return sic.fset.Position(sic.X.Fun.Pos()).Line
}
func (sic *stepIntoCall) ExprString() string {
var buf bytes.Buffer
format.Node(&buf, sic.fset, sic.X)
return buf.String()
}
func isExportedRuntime(name string) bool {
const n = len("runtime.")
return len(name) > n && name[:n] == "runtime." && 'A' <= name[n] && name[n] <= 'Z'
}
func isVisibleCall(inst api.AsmInstruction) bool {
if !strings.HasPrefix(inst.Text, "call ") {
return false
}
if inst.DestLoc != nil && inst.DestLoc.Function != nil {
fn := inst.DestLoc.Function
if strings.HasPrefix(fn.Name(), "runtime.") && !isExportedRuntime(fn.Name()) {
return false
}
}
return true
}
func isClosure(name string) bool {
const strFunc = "func"
name = removePath(name)
v := strings.Split(name, ".")
if len(v) == 0 {
return false
}
s := v[len(v)-1]
if !strings.HasPrefix(s, strFunc) {
return false
}
for i := len(strFunc); i < len(s); i++ {
ch := s[i]
if ch < '0' || ch > '9' {
return false
}
}
return true
}
func removePath(name string) string {
if d := strings.LastIndex(name, "/"); d >= 0 {
name = name[d+1:]
}
return name
}
func selectorSequence(x ast.Expr) bool {
for {
switch x1 := x.(type) {
case (*ast.SelectorExpr):
x = x1.X
case (*ast.Ident):
return true
default:
return false
}
}
}
func stepIntoList(loc api.Location) []stepIntoCall {
var fset token.FileSet
n, err := parser.ParseFile(&fset, loc.File, nil, 0)
if err != nil {
return nil
}
text, err := client.DisassemblePC(currentEvalScope(), loc.PC, api.IntelFlavour)
if err != nil {
return nil
}
isType := map[string]bool{}
for _, n := range typesPanel.slice {
isType[n] = true
}
isFunc := map[string]bool{}
for _, n := range funcsPanel.slice {
isFunc[n] = true
}
callInstrs := []api.AsmInstruction{}
for _, inst := range text {
if inst.Loc.File != loc.File || inst.Loc.Line != loc.Line {
continue
}
if isVisibleCall(inst) {
callInstrs = append(callInstrs, inst)
}
}
if len(callInstrs) <= 1 {
return nil
}
callExprs := stmtsInLoc(loc, n, &fset, isType)
if len(callInstrs) != len(callExprs) {
return nil
}
var v []stepIntoCall
for i := range callInstrs {
inst := callInstrs[i]
x := callExprs[i]
var fun string
switch x1 := x.Fun.(type) {
case (*ast.SelectorExpr):
if id, ok := x1.X.(*ast.Ident); ok {
fun = fmt.Sprintf("%s.%s", id.Name, x1.Sel.Name)
}
case (*ast.Ident):
fun = x1.Name
}
if fun != "" && isFunc[fun] && (inst.DestLoc == nil || inst.DestLoc.Function == nil || (!isClosure(inst.DestLoc.Function.Name()) && removePath(inst.DestLoc.Function.Name()) != fun)) {
return nil
}
sic := stepIntoCall{Inst: inst, X: x, fset: &fset}
switch {
case fun != "":
sic.Name = fun
case selectorSequence(x.Fun):
var buf bytes.Buffer
format.Node(&buf, &fset, x.Fun)
sic.Name = buf.String()
case inst.DestLoc != nil && inst.DestLoc.Function != nil:
sic.Name = removePath(inst.DestLoc.Function.Name())
default:
sic.Name = fmt.Sprintf("call%d", i)
}
// check that name is unique
for j := range v {
if v[j].Name == sic.Name {
sic.Name += fmt.Sprintf("#%d", i)
break
}
}
v = append(v, sic)
}
return v
}
type stepIntoInfo struct {
Lineno, Colno int
Filename string
Valid bool
Calls []stepIntoCall
Call stepIntoCall
Msg string
}
type sortStepIntoCallsByColWidth []stepIntoCall
func (v sortStepIntoCallsByColWidth) Len() int { return len(v) }
func (v sortStepIntoCallsByColWidth) Swap(i int, j int) { v[i], v[j] = v[j], v[i] }
func (v sortStepIntoCallsByColWidth) Less(i int, j int) bool {
ia, ib := v[i].ColInterval()
ja, jb := v[j].ColInterval()
return (ib - ia) < (jb - ja)
}
func (sii *stepIntoInfo) Config(filename string, lineno, colno int) bool {
if sii.Colno == colno && sii.Lineno == lineno && sii.Filename == filename {
return sii.Valid
}
sii.Colno = colno
sii.Lineno = lineno
sii.Filename = filename
sii.Valid = false
state, err := client.GetState()
if err != nil || state.CurrentThread == nil {
return false
}
sics := stepIntoList(api.Location{File: filename, Line: lineno, PC: state.CurrentThread.PC})
sort.Sort(sortStepIntoCallsByColWidth(sics))
listingPanel.stepIntoInfo.Calls = sics
for _, sic := range sii.Calls {
a, b := sic.ColInterval()
if colno >= a && colno < b && sic.Inst.Loc.PC >= state.CurrentThread.PC {
sii.Valid = true
sii.Call = sic
expr := sic.ExprString()
if len(expr) > 20 {
expr = expr[:20] + "..."
}
sii.Msg = fmt.Sprintf("Step into %s", expr)
return true
}
}
return false
}