2015-06-12 19:49:23 +00:00
|
|
|
package proc
|
2015-04-19 22:11:33 +00:00
|
|
|
|
2015-08-10 13:55:57 +00:00
|
|
|
import (
|
2016-02-02 11:26:29 +00:00
|
|
|
"errors"
|
2015-08-10 13:55:57 +00:00
|
|
|
"fmt"
|
2017-02-08 00:23:47 +00:00
|
|
|
|
2017-02-08 16:00:44 +00:00
|
|
|
"github.com/derekparker/delve/pkg/dwarf/frame"
|
2015-08-10 13:55:57 +00:00
|
|
|
)
|
2015-04-19 22:11:33 +00:00
|
|
|
|
2017-02-08 13:14:57 +00:00
|
|
|
// This code is partly adaped from runtime.gentraceback in
|
|
|
|
// $GOROOT/src/runtime/traceback.go
|
|
|
|
|
|
|
|
const runtimeStackBarrier = "runtime.stackBarrier"
|
|
|
|
|
2016-01-10 08:57:52 +00:00
|
|
|
// NoReturnAddr is returned when return address
|
2016-05-13 18:28:56 +00:00
|
|
|
// could not be found during stack trace.
|
2015-08-20 14:28:11 +00:00
|
|
|
type NoReturnAddr struct {
|
2017-04-21 06:55:53 +00:00
|
|
|
Fn string
|
2015-08-20 14:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (nra NoReturnAddr) Error() string {
|
2017-04-21 06:55:53 +00:00
|
|
|
return fmt.Sprintf("could not find return address for %s", nra.Fn)
|
2015-08-20 14:28:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-10 08:57:52 +00:00
|
|
|
// Stackframe represents a frame in a system stack.
|
2015-08-28 20:06:29 +00:00
|
|
|
type Stackframe struct {
|
2015-09-20 04:19:03 +00:00
|
|
|
// Address the function above this one on the call stack will return to.
|
2015-09-17 08:42:34 +00:00
|
|
|
Current Location
|
|
|
|
// Address of the call instruction for the function above on the call stack.
|
|
|
|
Call Location
|
2016-06-21 20:05:28 +00:00
|
|
|
// Start address of the stack frame.
|
2016-07-03 07:02:21 +00:00
|
|
|
CFA int64
|
2017-05-16 18:23:33 +00:00
|
|
|
// High address of the stack.
|
|
|
|
StackHi uint64
|
2016-06-21 20:05:28 +00:00
|
|
|
// Return address for this stack frame (as read from the stack frame itself).
|
2016-07-03 07:02:21 +00:00
|
|
|
Ret uint64
|
2017-02-08 13:14:57 +00:00
|
|
|
// Address to the memory location containing the return address
|
|
|
|
addrret uint64
|
2017-06-23 11:31:05 +00:00
|
|
|
// Err is set if an error occoured during stacktrace
|
|
|
|
Err error
|
2015-09-17 08:42:34 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 22:57:47 +00:00
|
|
|
// ThreadStacktrace returns the stack trace for thread.
|
2015-07-01 03:16:52 +00:00
|
|
|
// Note the locations in the array are return addresses not call addresses.
|
2017-04-21 07:50:38 +00:00
|
|
|
func ThreadStacktrace(thread Thread, depth int) ([]Stackframe, error) {
|
2017-02-15 13:41:03 +00:00
|
|
|
regs, err := thread.Registers(false)
|
2015-04-19 22:11:33 +00:00
|
|
|
if err != nil {
|
2015-07-01 03:16:52 +00:00
|
|
|
return nil, err
|
2015-04-19 22:11:33 +00:00
|
|
|
}
|
2017-05-16 18:23:33 +00:00
|
|
|
it := newStackIterator(thread.BinInfo(), thread, regs.PC(), regs.SP(), regs.BP(), 0, nil, -1)
|
2016-03-18 08:51:48 +00:00
|
|
|
return it.stacktrace(depth)
|
2015-06-17 17:11:57 +00:00
|
|
|
}
|
|
|
|
|
2016-03-18 08:51:48 +00:00
|
|
|
func (g *G) stackIterator() (*stackIterator, error) {
|
2017-02-08 13:14:57 +00:00
|
|
|
stkbar, err := g.stkbar()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-21 06:55:53 +00:00
|
|
|
if g.Thread != nil {
|
|
|
|
regs, err := g.Thread.Registers(false)
|
2017-02-15 13:41:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-16 18:23:33 +00:00
|
|
|
return newStackIterator(g.variable.bi, g.Thread, regs.PC(), regs.SP(), regs.BP(), g.stackhi, stkbar, g.stkbarPos), nil
|
2016-03-18 08:51:48 +00:00
|
|
|
}
|
2017-05-16 18:23:33 +00:00
|
|
|
return newStackIterator(g.variable.bi, g.variable.mem, g.PC, g.SP, 0, g.stackhi, stkbar, g.stkbarPos), nil
|
2016-03-18 08:51:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stacktrace returns the stack trace for a goroutine.
|
|
|
|
// Note the locations in the array are return addresses not call addresses.
|
|
|
|
func (g *G) Stacktrace(depth int) ([]Stackframe, error) {
|
|
|
|
it, err := g.stackIterator()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-06-17 17:11:57 +00:00
|
|
|
}
|
2016-03-18 08:51:48 +00:00
|
|
|
return it.stacktrace(depth)
|
2015-06-17 17:11:57 +00:00
|
|
|
}
|
|
|
|
|
2016-01-10 08:57:52 +00:00
|
|
|
// NullAddrError is an error for a null address.
|
2015-05-07 21:55:06 +00:00
|
|
|
type NullAddrError struct{}
|
|
|
|
|
|
|
|
func (n NullAddrError) Error() string {
|
|
|
|
return "NULL address"
|
|
|
|
}
|
|
|
|
|
2016-03-18 08:51:48 +00:00
|
|
|
// stackIterator holds information
|
2016-01-10 08:57:52 +00:00
|
|
|
// required to iterate and walk the program
|
|
|
|
// stack.
|
2016-03-18 08:51:48 +00:00
|
|
|
type stackIterator struct {
|
2017-02-16 13:16:00 +00:00
|
|
|
pc, sp, bp uint64
|
|
|
|
top bool
|
|
|
|
atend bool
|
|
|
|
frame Stackframe
|
2017-04-13 23:19:57 +00:00
|
|
|
bi *BinaryInfo
|
2017-04-21 06:55:53 +00:00
|
|
|
mem MemoryReadWriter
|
2017-02-16 13:16:00 +00:00
|
|
|
err error
|
2015-10-16 06:42:02 +00:00
|
|
|
|
2017-05-16 18:23:33 +00:00
|
|
|
stackhi uint64
|
2017-02-08 13:14:57 +00:00
|
|
|
stackBarrierPC uint64
|
|
|
|
stkbar []savedLR
|
|
|
|
}
|
|
|
|
|
|
|
|
type savedLR struct {
|
|
|
|
ptr uint64
|
|
|
|
val uint64
|
|
|
|
}
|
|
|
|
|
2017-05-16 18:23:33 +00:00
|
|
|
func newStackIterator(bi *BinaryInfo, mem MemoryReadWriter, pc, sp, bp, stackhi uint64, stkbar []savedLR, stkbarPos int) *stackIterator {
|
2017-09-01 13:30:45 +00:00
|
|
|
stackBarrierFunc := bi.LookupFunc[runtimeStackBarrier] // stack barriers were removed in Go 1.9
|
2017-03-13 17:53:16 +00:00
|
|
|
var stackBarrierPC uint64
|
|
|
|
if stackBarrierFunc != nil && stkbar != nil {
|
|
|
|
stackBarrierPC = stackBarrierFunc.Entry
|
2017-09-01 13:30:45 +00:00
|
|
|
fn := bi.PCToFunc(pc)
|
2017-02-08 13:14:57 +00:00
|
|
|
if fn != nil && fn.Name == runtimeStackBarrier {
|
|
|
|
// We caught the goroutine as it's executing the stack barrier, we must
|
|
|
|
// determine whether or not g.stackPos has already been incremented or not.
|
|
|
|
if len(stkbar) > 0 && stkbar[stkbarPos].ptr < sp {
|
|
|
|
// runtime.stackBarrier has not incremented stkbarPos.
|
|
|
|
} else if stkbarPos > 0 && stkbar[stkbarPos-1].ptr < sp {
|
|
|
|
// runtime.stackBarrier has incremented stkbarPos.
|
|
|
|
stkbarPos--
|
|
|
|
} else {
|
|
|
|
return &stackIterator{err: fmt.Errorf("failed to unwind through stackBarrier at SP %x", sp)}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stkbar = stkbar[stkbarPos:]
|
|
|
|
}
|
2017-05-16 18:23:33 +00:00
|
|
|
return &stackIterator{pc: pc, sp: sp, bp: bp, top: true, bi: bi, mem: mem, err: nil, atend: false, stackhi: stackhi, stackBarrierPC: stackBarrierPC, stkbar: stkbar}
|
2015-10-16 06:42:02 +00:00
|
|
|
}
|
|
|
|
|
2016-01-10 08:57:52 +00:00
|
|
|
// Next points the iterator to the next stack frame.
|
2016-03-18 08:51:48 +00:00
|
|
|
func (it *stackIterator) Next() bool {
|
2015-10-16 06:42:02 +00:00
|
|
|
if it.err != nil || it.atend {
|
|
|
|
return false
|
|
|
|
}
|
2017-04-13 23:19:57 +00:00
|
|
|
it.frame, it.err = it.frameInfo(it.pc, it.sp, it.bp, it.top)
|
2015-10-16 06:42:02 +00:00
|
|
|
if it.err != nil {
|
2016-03-18 08:32:17 +00:00
|
|
|
if _, nofde := it.err.(*frame.NoFDEForPCError); nofde && !it.top {
|
2016-05-29 19:20:09 +00:00
|
|
|
it.frame = Stackframe{Current: Location{PC: it.pc, File: "?", Line: -1}, Call: Location{PC: it.pc, File: "?", Line: -1}, CFA: 0, Ret: 0}
|
2016-03-18 08:32:17 +00:00
|
|
|
it.atend = true
|
|
|
|
it.err = nil
|
|
|
|
return true
|
|
|
|
}
|
2015-10-16 06:42:02 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if it.frame.Ret <= 0 {
|
|
|
|
it.atend = true
|
|
|
|
return true
|
|
|
|
}
|
2017-02-08 13:14:57 +00:00
|
|
|
|
|
|
|
if it.stkbar != nil && it.frame.Ret == it.stackBarrierPC && it.frame.addrret == it.stkbar[0].ptr {
|
|
|
|
// Skip stack barrier frames
|
|
|
|
it.frame.Ret = it.stkbar[0].val
|
|
|
|
it.stkbar = it.stkbar[1:]
|
|
|
|
}
|
|
|
|
|
2015-10-16 06:42:02 +00:00
|
|
|
// Look for "top of stack" functions.
|
2017-02-16 13:16:00 +00:00
|
|
|
if it.frame.Current.Fn != nil && (it.frame.Current.Fn.Name == "runtime.goexit" || it.frame.Current.Fn.Name == "runtime.rt0_go" || it.frame.Current.Fn.Name == "runtime.mcall") {
|
2015-10-16 06:42:02 +00:00
|
|
|
it.atend = true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
it.top = false
|
|
|
|
it.pc = it.frame.Ret
|
|
|
|
it.sp = uint64(it.frame.CFA)
|
2017-04-21 06:55:53 +00:00
|
|
|
it.bp, _ = readUintRaw(it.mem, uintptr(it.bp), int64(it.bi.Arch.PtrSize()))
|
2015-10-16 06:42:02 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-01-10 08:57:52 +00:00
|
|
|
// Frame returns the frame the iterator is pointing at.
|
2016-03-18 08:51:48 +00:00
|
|
|
func (it *stackIterator) Frame() Stackframe {
|
2015-10-16 06:42:02 +00:00
|
|
|
if it.err != nil {
|
|
|
|
panic(it.err)
|
|
|
|
}
|
|
|
|
return it.frame
|
|
|
|
}
|
|
|
|
|
2016-01-10 08:57:52 +00:00
|
|
|
// Err returns the error encountered during stack iteration.
|
2016-03-18 08:51:48 +00:00
|
|
|
func (it *stackIterator) Err() error {
|
2015-10-16 06:42:02 +00:00
|
|
|
return it.err
|
|
|
|
}
|
|
|
|
|
2017-04-13 23:19:57 +00:00
|
|
|
func (it *stackIterator) frameInfo(pc, sp, bp uint64, top bool) (Stackframe, error) {
|
|
|
|
fde, err := it.bi.frameEntries.FDEForPC(pc)
|
2017-02-16 13:16:00 +00:00
|
|
|
if _, nofde := err.(*frame.NoFDEForPCError); nofde {
|
|
|
|
if bp == 0 {
|
|
|
|
return Stackframe{}, err
|
|
|
|
}
|
|
|
|
// When no FDE is available attempt to use BP instead
|
2017-04-21 06:55:53 +00:00
|
|
|
retaddr := uintptr(int(bp) + it.bi.Arch.PtrSize())
|
|
|
|
cfa := int64(retaddr) + int64(it.bi.Arch.PtrSize())
|
2017-04-13 23:19:57 +00:00
|
|
|
return it.newStackframe(pc, cfa, retaddr, nil, top)
|
2015-08-28 20:06:29 +00:00
|
|
|
}
|
2017-02-16 13:16:00 +00:00
|
|
|
|
2015-08-28 20:06:29 +00:00
|
|
|
spoffset, retoffset := fde.ReturnAddressOffset(pc)
|
|
|
|
cfa := int64(sp) + spoffset
|
|
|
|
|
|
|
|
retaddr := uintptr(cfa + retoffset)
|
2017-04-13 23:19:57 +00:00
|
|
|
return it.newStackframe(pc, cfa, retaddr, fde, top)
|
2017-02-16 13:16:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-13 23:19:57 +00:00
|
|
|
func (it *stackIterator) newStackframe(pc uint64, cfa int64, retaddr uintptr, fde *frame.FrameDescriptionEntry, top bool) (Stackframe, error) {
|
2015-08-28 20:06:29 +00:00
|
|
|
if retaddr == 0 {
|
|
|
|
return Stackframe{}, NullAddrError{}
|
|
|
|
}
|
2017-04-13 23:19:57 +00:00
|
|
|
f, l, fn := it.bi.PCToLine(pc)
|
2017-04-21 06:55:53 +00:00
|
|
|
ret, err := readUintRaw(it.mem, retaddr, int64(it.bi.Arch.PtrSize()))
|
2015-08-28 20:06:29 +00:00
|
|
|
if err != nil {
|
2017-06-23 11:31:05 +00:00
|
|
|
it.err = err
|
2015-08-28 20:06:29 +00:00
|
|
|
}
|
2017-09-01 13:30:45 +00:00
|
|
|
r := Stackframe{Current: Location{PC: pc, File: f, Line: l, Fn: fn}, CFA: cfa, Ret: ret, addrret: uint64(retaddr), StackHi: it.stackhi}
|
2015-09-17 08:42:34 +00:00
|
|
|
if !top {
|
2017-04-13 23:19:57 +00:00
|
|
|
r.Call.File, r.Call.Line, r.Call.Fn = it.bi.PCToLine(pc - 1)
|
2017-02-16 19:20:12 +00:00
|
|
|
r.Call.PC = r.Current.PC
|
2015-09-17 08:42:34 +00:00
|
|
|
} else {
|
|
|
|
r.Call = r.Current
|
|
|
|
}
|
|
|
|
return r, nil
|
2015-08-28 20:06:29 +00:00
|
|
|
}
|
|
|
|
|
2016-03-18 08:51:48 +00:00
|
|
|
func (it *stackIterator) stacktrace(depth int) ([]Stackframe, error) {
|
2016-02-02 11:26:29 +00:00
|
|
|
if depth < 0 {
|
|
|
|
return nil, errors.New("negative maximum stack depth")
|
|
|
|
}
|
2015-08-28 20:06:29 +00:00
|
|
|
frames := make([]Stackframe, 0, depth+1)
|
2015-10-16 06:42:02 +00:00
|
|
|
for it.Next() {
|
|
|
|
frames = append(frames, it.Frame())
|
|
|
|
if len(frames) >= depth+1 {
|
2015-06-17 17:11:57 +00:00
|
|
|
break
|
|
|
|
}
|
2015-10-16 06:42:02 +00:00
|
|
|
}
|
|
|
|
if err := it.Err(); err != nil {
|
2017-06-23 11:31:05 +00:00
|
|
|
if len(frames) == 0 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
frames = append(frames, Stackframe{Err: err})
|
2015-04-19 22:11:33 +00:00
|
|
|
}
|
2015-08-28 20:06:29 +00:00
|
|
|
return frames, nil
|
2015-04-19 22:11:33 +00:00
|
|
|
}
|