forked from go-delve/delve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.go
62 lines (56 loc) · 1.39 KB
/
stack.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
package proctl
import (
"debug/gosym"
"encoding/binary"
)
type stackLocation struct {
addr uint64
file string
line int
fn *gosym.Func
}
// Takes an offset from RSP and returns the address of the
// instruction the currect function is going to return to.
func (thread *ThreadContext) ReturnAddress() (uint64, error) {
regs, err := thread.Registers()
if err != nil {
return 0, err
}
locations, err := thread.Process.stacktrace(regs.PC(), regs.SP(), 1)
if err != nil {
return 0, err
}
return locations[0].addr, nil
}
type NullAddrError struct{}
func (n NullAddrError) Error() string {
return "NULL address"
}
func (dbp *DebuggedProcess) stacktrace(pc, sp uint64, depth int) ([]stackLocation, error) {
var (
ret = pc
data = make([]byte, 8)
btoffset int64
locations []stackLocation
retaddr uintptr
)
for i := int64(0); i < int64(depth); i++ {
fde, err := dbp.frameEntries.FDEForPC(ret)
if err != nil {
return nil, err
}
btoffset += fde.ReturnAddressOffset(ret)
retaddr = uintptr(int64(sp) + btoffset + (i * 8))
if retaddr == 0 {
return nil, NullAddrError{}
}
_, err = readMemory(dbp.CurrentThread, retaddr, data)
if err != nil {
return nil, err
}
ret = binary.LittleEndian.Uint64(data)
f, l, fn := dbp.goSymTable.PCToLine(ret)
locations = append(locations, stackLocation{addr: ret, file: f, line: l, fn: fn})
}
return locations, nil
}