stack.go 7.4 KB
Newer Older
D
Derek Parker 已提交
1
package proc
2

3
import (
4
	"errors"
5
	"fmt"
D
Derek Parker 已提交
6

D
Derek Parker 已提交
7
	"github.com/derekparker/delve/pkg/dwarf/frame"
8
)
9

A
Alessandro Arzilli 已提交
10 11 12 13 14
// This code is partly adaped from runtime.gentraceback in
// $GOROOT/src/runtime/traceback.go

const runtimeStackBarrier = "runtime.stackBarrier"

D
Derek Parker 已提交
15
// NoReturnAddr is returned when return address
N
Nan Xiao 已提交
16
// could not be found during stack trace.
17 18 19 20 21 22 23 24
type NoReturnAddr struct {
	fn string
}

func (nra NoReturnAddr) Error() string {
	return fmt.Sprintf("could not find return address for %s", nra.fn)
}

D
Derek Parker 已提交
25
// Stackframe represents a frame in a system stack.
26
type Stackframe struct {
27
	// Address the function above this one on the call stack will return to.
28 29 30
	Current Location
	// Address of the call instruction for the function above on the call stack.
	Call Location
31
	// Start address of the stack frame.
32
	CFA int64
33
	// Description of the stack frame.
34
	FDE *frame.FrameDescriptionEntry
35
	// Return address for this stack frame (as read from the stack frame itself).
36
	Ret uint64
A
Alessandro Arzilli 已提交
37 38
	// Address to the memory location containing the return address
	addrret uint64
39 40
}

D
Derek Parker 已提交
41
// Scope returns a new EvalScope using this frame.
42 43
func (frame *Stackframe) Scope(thread *Thread) *EvalScope {
	return &EvalScope{Thread: thread, PC: frame.Current.PC, CFA: frame.CFA}
44 45
}

D
Derek Parker 已提交
46 47 48 49
// ReturnAddress returns the return address of the function
// this thread is executing.
func (t *Thread) ReturnAddress() (uint64, error) {
	locations, err := t.Stacktrace(2)
50 51 52
	if err != nil {
		return 0, err
	}
53
	if len(locations) < 2 {
54
		return 0, NoReturnAddr{locations[0].Current.Fn.BaseName()}
55
	}
56
	return locations[1].Current.PC, nil
A
aarzilli 已提交
57 58
}

A
Alessandro Arzilli 已提交
59
func (t *Thread) stackIterator(stkbar []savedLR, stkbarPos int) (*stackIterator, error) {
A
aarzilli 已提交
60
	regs, err := t.Registers(false)
A
aarzilli 已提交
61 62 63
	if err != nil {
		return nil, err
	}
64
	return newStackIterator(t.dbp, regs.PC(), regs.SP(), regs.BP(), stkbar, stkbarPos), nil
A
aarzilli 已提交
65 66
}

D
Derek Parker 已提交
67
// Stacktrace returns the stack trace for thread.
D
Derek Parker 已提交
68
// Note the locations in the array are return addresses not call addresses.
D
Derek Parker 已提交
69
func (t *Thread) Stacktrace(depth int) ([]Stackframe, error) {
A
Alessandro Arzilli 已提交
70
	it, err := t.stackIterator(nil, -1)
71
	if err != nil {
D
Derek Parker 已提交
72
		return nil, err
73
	}
A
aarzilli 已提交
74
	return it.stacktrace(depth)
A
aarzilli 已提交
75 76
}

A
aarzilli 已提交
77
func (g *G) stackIterator() (*stackIterator, error) {
A
Alessandro Arzilli 已提交
78 79 80 81
	stkbar, err := g.stkbar()
	if err != nil {
		return nil, err
	}
A
aarzilli 已提交
82
	if g.thread != nil {
A
Alessandro Arzilli 已提交
83
		return g.thread.stackIterator(stkbar, g.stkbarPos)
A
aarzilli 已提交
84
	}
85
	return newStackIterator(g.dbp, g.PC, g.SP, 0, stkbar, g.stkbarPos), nil
A
aarzilli 已提交
86 87 88 89 90 91 92 93
}

// 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
A
aarzilli 已提交
94
	}
A
aarzilli 已提交
95
	return it.stacktrace(depth)
A
aarzilli 已提交
96 97
}

D
Derek Parker 已提交
98 99
// GoroutineLocation returns the location of the given
// goroutine.
D
Derek Parker 已提交
100
func (dbp *Process) GoroutineLocation(g *G) *Location {
A
aarzilli 已提交
101 102
	f, l, fn := dbp.PCToLine(g.PC)
	return &Location{PC: g.PC, File: f, Line: l, Fn: fn}
103 104
}

D
Derek Parker 已提交
105
// NullAddrError is an error for a null address.
106 107 108 109 110 111
type NullAddrError struct{}

func (n NullAddrError) Error() string {
	return "NULL address"
}

A
aarzilli 已提交
112
// stackIterator holds information
D
Derek Parker 已提交
113 114
// required to iterate and walk the program
// stack.
A
aarzilli 已提交
115
type stackIterator struct {
116 117 118 119 120 121
	pc, sp, bp uint64
	top        bool
	atend      bool
	frame      Stackframe
	dbp        *Process
	err        error
122

A
Alessandro Arzilli 已提交
123 124 125 126 127 128 129 130 131
	stackBarrierPC uint64
	stkbar         []savedLR
}

type savedLR struct {
	ptr uint64
	val uint64
}

132
func newStackIterator(dbp *Process, pc, sp, bp uint64, stkbar []savedLR, stkbarPos int) *stackIterator {
133 134 135 136
	stackBarrierFunc := dbp.goSymTable.LookupFunc(runtimeStackBarrier) // stack barriers were removed in Go 1.9
	var stackBarrierPC uint64
	if stackBarrierFunc != nil && stkbar != nil {
		stackBarrierPC = stackBarrierFunc.Entry
A
Alessandro Arzilli 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
		fn := dbp.goSymTable.PCToFunc(pc)
		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:]
	}
152
	return &stackIterator{pc: pc, sp: sp, bp: bp, top: true, dbp: dbp, err: nil, atend: false, stackBarrierPC: stackBarrierPC, stkbar: stkbar}
153 154
}

D
Derek Parker 已提交
155
// Next points the iterator to the next stack frame.
A
aarzilli 已提交
156
func (it *stackIterator) Next() bool {
157 158 159
	if it.err != nil || it.atend {
		return false
	}
160
	it.frame, it.err = it.dbp.frameInfo(it.pc, it.sp, it.bp, it.top)
161
	if it.err != nil {
162
		if _, nofde := it.err.(*frame.NoFDEForPCError); nofde && !it.top {
A
Alessandro Arzilli 已提交
163
			it.frame = Stackframe{Current: Location{PC: it.pc, File: "?", Line: -1}, Call: Location{PC: it.pc, File: "?", Line: -1}, CFA: 0, Ret: 0}
164 165 166 167
			it.atend = true
			it.err = nil
			return true
		}
168 169 170 171 172 173 174
		return false
	}

	if it.frame.Ret <= 0 {
		it.atend = true
		return true
	}
A
Alessandro Arzilli 已提交
175 176 177 178 179 180 181

	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:]
	}

182
	// Look for "top of stack" functions.
183
	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") {
184 185 186 187 188 189 190
		it.atend = true
		return true
	}

	it.top = false
	it.pc = it.frame.Ret
	it.sp = uint64(it.frame.CFA)
191
	it.bp, _ = readUintRaw(it.dbp.currentThread, uintptr(it.bp), int64(it.dbp.arch.PtrSize()))
192 193 194
	return true
}

D
Derek Parker 已提交
195
// Frame returns the frame the iterator is pointing at.
A
aarzilli 已提交
196
func (it *stackIterator) Frame() Stackframe {
197 198 199 200 201 202
	if it.err != nil {
		panic(it.err)
	}
	return it.frame
}

D
Derek Parker 已提交
203
// Err returns the error encountered during stack iteration.
A
aarzilli 已提交
204
func (it *stackIterator) Err() error {
205 206 207
	return it.err
}

208
func (dbp *Process) frameInfo(pc, sp, bp uint64, top bool) (Stackframe, error) {
209
	fde, err := dbp.frameEntries.FDEForPC(pc)
210 211 212 213 214 215 216 217
	if _, nofde := err.(*frame.NoFDEForPCError); nofde {
		if bp == 0 {
			return Stackframe{}, err
		}
		// When no FDE is available attempt to use BP instead
		retaddr := uintptr(int(bp) + dbp.arch.PtrSize())
		cfa := int64(retaddr) + int64(dbp.arch.PtrSize())
		return dbp.newStackframe(pc, cfa, retaddr, nil, top)
218
	}
219

220 221 222 223
	spoffset, retoffset := fde.ReturnAddressOffset(pc)
	cfa := int64(sp) + spoffset

	retaddr := uintptr(cfa + retoffset)
224 225 226 227
	return dbp.newStackframe(pc, cfa, retaddr, fde, top)
}

func (dbp *Process) newStackframe(pc uint64, cfa int64, retaddr uintptr, fde *frame.FrameDescriptionEntry, top bool) (Stackframe, error) {
228 229 230
	if retaddr == 0 {
		return Stackframe{}, NullAddrError{}
	}
231 232
	f, l, fn := dbp.PCToLine(pc)
	ret, err := readUintRaw(dbp.currentThread, retaddr, int64(dbp.arch.PtrSize()))
233 234 235
	if err != nil {
		return Stackframe{}, err
	}
236
	r := Stackframe{Current: Location{PC: pc, File: f, Line: l, Fn: fn}, CFA: cfa, FDE: fde, Ret: ret, addrret: uint64(retaddr)}
237 238
	if !top {
		r.Call.File, r.Call.Line, r.Call.Fn = dbp.PCToLine(pc - 1)
239
		r.Call.PC = r.Current.PC
240 241 242 243
	} else {
		r.Call = r.Current
	}
	return r, nil
244 245
}

A
aarzilli 已提交
246
func (it *stackIterator) stacktrace(depth int) ([]Stackframe, error) {
247 248 249
	if depth < 0 {
		return nil, errors.New("negative maximum stack depth")
	}
250
	frames := make([]Stackframe, 0, depth+1)
251 252 253
	for it.Next() {
		frames = append(frames, it.Frame())
		if len(frames) >= depth+1 {
A
aarzilli 已提交
254 255
			break
		}
256 257 258
	}
	if err := it.Err(); err != nil {
		return nil, err
259
	}
260
	return frames, nil
261
}