2015-06-12 19:49:23 +00:00
|
|
|
package proc
|
2015-04-19 22:11:33 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Takes an offset from RSP and returns the address of the
|
|
|
|
// instruction the currect function is going to return to.
|
2015-06-12 19:51:23 +00:00
|
|
|
func (thread *Thread) ReturnAddress() (uint64, error) {
|
2015-06-28 15:00:56 +00:00
|
|
|
_, locations, err := thread.Stacktrace(1)
|
2015-04-19 22:11:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2015-06-17 17:11:57 +00:00
|
|
|
return locations[0].PC, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the stack trace for thread
|
|
|
|
// Note that it doesn't include the current frame and the locations in the array are return addresses not call addresses
|
2015-06-28 15:00:56 +00:00
|
|
|
func (thread *Thread) Stacktrace(depth int) (*Location, []Location, error) {
|
|
|
|
loc, err := thread.Location()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2015-06-17 17:11:57 +00:00
|
|
|
regs, err := thread.Registers()
|
2015-04-19 22:11:33 +00:00
|
|
|
if err != nil {
|
2015-06-28 15:00:56 +00:00
|
|
|
return nil, nil, err
|
2015-06-17 17:11:57 +00:00
|
|
|
}
|
|
|
|
locations, err := thread.dbp.stacktrace(regs.PC(), regs.SP(), depth)
|
|
|
|
if err != nil {
|
2015-06-28 15:00:56 +00:00
|
|
|
return nil, nil, err
|
2015-04-19 22:11:33 +00:00
|
|
|
}
|
2015-06-28 15:00:56 +00:00
|
|
|
return loc, locations, nil
|
2015-06-17 17:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the stack trace for a goroutine
|
|
|
|
// Note that it doesn't include the current frame and the locations in the array are return addresses not call addresses
|
2015-06-28 15:00:56 +00:00
|
|
|
func (dbp *Process) GoroutineStacktrace(g *G, depth int) (*Location, []Location, error) {
|
2015-06-17 17:11:57 +00:00
|
|
|
if g.thread != nil {
|
|
|
|
return g.thread.Stacktrace(depth)
|
|
|
|
}
|
2015-06-28 15:00:56 +00:00
|
|
|
locs, err := dbp.stacktrace(g.PC, g.SP, depth)
|
|
|
|
return dbp.GoroutineLocation(g), locs, err
|
2015-06-17 17:11:57 +00:00
|
|
|
}
|
|
|
|
|
2015-06-20 22:54:52 +00:00
|
|
|
func (dbp *Process) GoroutineLocation(g *G) *Location {
|
2015-06-17 17:11:57 +00:00
|
|
|
f, l, fn := dbp.PCToLine(g.PC)
|
|
|
|
return &Location{PC: g.PC, File: f, Line: l, Fn: fn}
|
2015-04-19 22:11:33 +00:00
|
|
|
}
|
|
|
|
|
2015-05-07 21:55:06 +00:00
|
|
|
type NullAddrError struct{}
|
|
|
|
|
|
|
|
func (n NullAddrError) Error() string {
|
|
|
|
return "NULL address"
|
|
|
|
}
|
|
|
|
|
2015-06-20 22:54:52 +00:00
|
|
|
func (dbp *Process) stacktrace(pc, sp uint64, depth int) ([]Location, error) {
|
2015-04-19 22:11:33 +00:00
|
|
|
var (
|
|
|
|
ret = pc
|
2015-06-13 19:04:09 +00:00
|
|
|
data = make([]byte, dbp.arch.PtrSize())
|
2015-04-19 22:11:33 +00:00
|
|
|
btoffset int64
|
2015-06-17 17:11:57 +00:00
|
|
|
locations []Location
|
2015-04-19 22:11:33 +00:00
|
|
|
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)
|
2015-06-13 19:04:09 +00:00
|
|
|
retaddr = uintptr(int64(sp) + btoffset + (i * int64(dbp.arch.PtrSize())))
|
2015-05-07 21:55:06 +00:00
|
|
|
if retaddr == 0 {
|
|
|
|
return nil, NullAddrError{}
|
|
|
|
}
|
2015-04-19 22:11:33 +00:00
|
|
|
_, err = readMemory(dbp.CurrentThread, retaddr, data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret = binary.LittleEndian.Uint64(data)
|
2015-06-17 17:11:57 +00:00
|
|
|
if ret <= 0 {
|
|
|
|
break
|
|
|
|
}
|
2015-04-19 22:11:33 +00:00
|
|
|
f, l, fn := dbp.goSymTable.PCToLine(ret)
|
2015-06-17 17:11:57 +00:00
|
|
|
locations = append(locations, Location{PC: ret, File: f, Line: l, Fn: fn})
|
|
|
|
if fn != nil && fn.Name == "runtime.goexit" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2015-04-19 22:11:33 +00:00
|
|
|
}
|
|
|
|
return locations, nil
|
|
|
|
}
|