blob: 2261942ab49e8892b43c9b4b03556bedfaed3c84 [file] [log] [blame]
Russ Coxfa2af442014-09-02 15:12:53 -04001// Copyright 2009 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package runtime
6
Michael Matloob432cb662015-11-11 12:39:30 -05007import (
Ian Lance Taylorea306ae2015-12-11 17:16:48 -08008 "runtime/internal/atomic"
Michael Matloob432cb662015-11-11 12:39:30 -05009 "runtime/internal/sys"
10 "unsafe"
11)
Russ Coxfa2af442014-09-02 15:12:53 -040012
13// The code in this file implements stack trace walking for all architectures.
14// The most important fact about a given architecture is whether it uses a link register.
15// On systems with link registers, the prologue for a non-leaf function stores the
16// incoming value of LR at the bottom of the newly allocated stack frame.
17// On systems without link registers, the architecture pushes a return PC during
18// the call instruction, so the return PC ends up above the stack frame.
19// In this file, the return PC is always called LR, no matter how it was found.
20//
21// To date, the opposite of a link register architecture is an x86 architecture.
22// This code may need to change if some other kind of non-link-register
23// architecture comes along.
24//
25// The other important fact is the size of a pointer: on 32-bit systems the LR
26// takes up only 4 bytes on the stack, while on 64-bit systems it takes up 8 bytes.
27// Typically this is ptrSize.
28//
29// As an exception, amd64p32 has ptrSize == 4 but the CALL instruction still
30// stores an 8-byte return PC onto the stack. To accommodate this, we use regSize
31// as the size of the architecture-pushed return PC.
32//
Michael Hudson-Doylea4855812015-10-08 21:52:03 +130033// usesLR is defined below in terms of minFrameSize, which is defined in
34// arch_$GOARCH.go. ptrSize and regSize are defined in stubs.go.
Russ Coxfa2af442014-09-02 15:12:53 -040035
Michael Matloob432cb662015-11-11 12:39:30 -050036const usesLR = sys.MinFrameSize > 0
Russ Coxfa2af442014-09-02 15:12:53 -040037
Russ Cox97f83862014-09-03 13:02:48 -040038var (
Keith Randall70b2da92014-09-29 21:21:36 -070039 // initialized in tracebackinit
Russ Cox656be312014-11-12 14:54:31 -050040 goexitPC uintptr
41 jmpdeferPC uintptr
42 mcallPC uintptr
43 morestackPC uintptr
44 mstartPC uintptr
Russ Cox656be312014-11-12 14:54:31 -050045 rt0_goPC uintptr
Austin Clements5c2be422018-01-30 16:01:33 -050046 asmcgocallPC uintptr
Russ Cox656be312014-11-12 14:54:31 -050047 sigpanicPC uintptr
Dmitry Vyukov59495e82015-02-07 15:31:18 +030048 runfinqPC uintptr
Dmitry Vyukov59495e82015-02-07 15:31:18 +030049 bgsweepPC uintptr
50 forcegchelperPC uintptr
51 timerprocPC uintptr
Austin Clements8d03acc2015-03-23 21:07:33 -040052 gcBgMarkWorkerPC uintptr
Russ Cox656be312014-11-12 14:54:31 -050053 systemstack_switchPC uintptr
Daniel Morsingdb6f88a2015-04-30 15:32:54 +010054 systemstackPC uintptr
Austin Clementse2bb03f2015-08-26 12:16:51 -040055 cgocallback_gofuncPC uintptr
David Lazar7bf0adc2017-03-07 21:14:12 -050056 skipPC uintptr
Daniel Morsingdb6f88a2015-04-30 15:32:54 +010057
58 gogoPC uintptr
Russ Cox97f83862014-09-03 13:02:48 -040059
60 externalthreadhandlerp uintptr // initialized elsewhere
61)
Russ Coxfa2af442014-09-02 15:12:53 -040062
Keith Randall70b2da92014-09-29 21:21:36 -070063func tracebackinit() {
64 // Go variable initialization happens late during runtime startup.
65 // Instead of initializing the variables above in the declarations,
66 // schedinit calls this function so that the variables are
67 // initialized and available earlier in the startup sequence.
Keith Randall70b2da92014-09-29 21:21:36 -070068 goexitPC = funcPC(goexit)
69 jmpdeferPC = funcPC(jmpdefer)
70 mcallPC = funcPC(mcall)
71 morestackPC = funcPC(morestack)
72 mstartPC = funcPC(mstart)
Keith Randall70b2da92014-09-29 21:21:36 -070073 rt0_goPC = funcPC(rt0_go)
Austin Clements5c2be422018-01-30 16:01:33 -050074 asmcgocallPC = funcPC(asmcgocall)
Keith Randall70b2da92014-09-29 21:21:36 -070075 sigpanicPC = funcPC(sigpanic)
Dmitry Vyukov59495e82015-02-07 15:31:18 +030076 runfinqPC = funcPC(runfinq)
Dmitry Vyukov59495e82015-02-07 15:31:18 +030077 bgsweepPC = funcPC(bgsweep)
78 forcegchelperPC = funcPC(forcegchelper)
79 timerprocPC = funcPC(timerproc)
Austin Clements8d03acc2015-03-23 21:07:33 -040080 gcBgMarkWorkerPC = funcPC(gcBgMarkWorker)
Russ Cox656be312014-11-12 14:54:31 -050081 systemstack_switchPC = funcPC(systemstack_switch)
Daniel Morsingdb6f88a2015-04-30 15:32:54 +010082 systemstackPC = funcPC(systemstack)
Austin Clementse2bb03f2015-08-26 12:16:51 -040083 cgocallback_gofuncPC = funcPC(cgocallback_gofunc)
David Lazar7bf0adc2017-03-07 21:14:12 -050084 skipPC = funcPC(skipPleaseUseCallersFrames)
Daniel Morsingdb6f88a2015-04-30 15:32:54 +010085
86 // used by sigprof handler
87 gogoPC = funcPC(gogo)
Keith Randall70b2da92014-09-29 21:21:36 -070088}
89
Russ Coxf95beae2014-09-16 10:36:38 -040090// Traceback over the deferred function calls.
91// Report them like calls that have been invoked but not started executing yet.
92func tracebackdefers(gp *g, callback func(*stkframe, unsafe.Pointer) bool, v unsafe.Pointer) {
93 var frame stkframe
94 for d := gp._defer; d != nil; d = d.link {
95 fn := d.fn
96 if fn == nil {
97 // Defer of nil function. Args don't matter.
98 frame.pc = 0
Austin Clements0efc8b22017-02-20 22:37:07 -050099 frame.fn = funcInfo{}
Russ Coxf95beae2014-09-16 10:36:38 -0400100 frame.argp = 0
101 frame.arglen = 0
102 frame.argmap = nil
103 } else {
Matthew Dempskya03bdc32016-02-29 15:01:00 -0800104 frame.pc = fn.fn
Russ Coxf95beae2014-09-16 10:36:38 -0400105 f := findfunc(frame.pc)
Austin Clements0efc8b22017-02-20 22:37:07 -0500106 if !f.valid() {
Russ Coxf95beae2014-09-16 10:36:38 -0400107 print("runtime: unknown pc in defer ", hex(frame.pc), "\n")
Keith Randallb2a950b2014-12-27 20:58:00 -0800108 throw("unknown pc")
Russ Coxf95beae2014-09-16 10:36:38 -0400109 }
110 frame.fn = f
111 frame.argp = uintptr(deferArgs(d))
Austin Clementsd5bd7972016-10-16 18:23:39 -0400112 frame.arglen, frame.argmap = getArgInfo(&frame, f, true, fn)
Russ Coxf95beae2014-09-16 10:36:38 -0400113 }
114 frame.continpc = frame.pc
115 if !callback((*stkframe)(noescape(unsafe.Pointer(&frame))), v) {
116 return
117 }
118 }
119}
Russ Coxfa2af442014-09-02 15:12:53 -0400120
David Lazaree972162017-03-06 14:48:36 -0500121const sizeofSkipFunction = 256
122
123// This function is defined in asm.s to be sizeofSkipFunction bytes long.
124func skipPleaseUseCallersFrames()
125
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +0000126// Generic traceback. Handles runtime stack prints (pcbuf == nil),
Russ Coxfa2af442014-09-02 15:12:53 -0400127// the runtime.Callers function (pcbuf != nil), as well as the garbage
128// collector (callback != nil). A little clunky to merge these, but avoids
129// duplicating the code and all its subtlety.
David Lazaree972162017-03-06 14:48:36 -0500130//
131// The skip argument is only valid with pcbuf != nil and counts the number
132// of logical frames to skip rather than physical frames (with inlining, a
133// PC in pcbuf can represent multiple calls). If a PC is partially skipped
134// and max > 1, pcbuf[1] will be runtime.skipPleaseUseCallersFrames+N where
135// N indicates the number of logical frames to skip in pcbuf[0].
Matthew Dempsky3c8a89d2015-02-25 14:41:21 +0900136func gentraceback(pc0, sp0, lr0 uintptr, gp *g, skip int, pcbuf *uintptr, max int, callback func(*stkframe, unsafe.Pointer) bool, v unsafe.Pointer, flags uint) int {
David Lazaree972162017-03-06 14:48:36 -0500137 if skip > 0 && callback != nil {
138 throw("gentraceback callback cannot be used with non-zero skip")
139 }
Keith Randall70b2da92014-09-29 21:21:36 -0700140 if goexitPC == 0 {
Keith Randallb2a950b2014-12-27 20:58:00 -0800141 throw("gentraceback before goexitPC initialization")
Keith Randall70b2da92014-09-29 21:21:36 -0700142 }
Russ Coxfa2af442014-09-02 15:12:53 -0400143 g := getg()
Russ Cox39bcbb32014-11-05 23:01:48 -0500144 if g == gp && g == g.m.curg {
145 // The starting sp has been passed in as a uintptr, and the caller may
146 // have other uintptr-typed stack references as well.
147 // If during one of the calls that got us here or during one of the
148 // callbacks below the stack must be grown, all these uintptr references
149 // to the stack will not be updated, and gentraceback will continue
150 // to inspect the old stack memory, which may no longer be valid.
151 // Even if all the variables were updated correctly, it is not clear that
152 // we want to expose a traceback that begins on one stack and ends
153 // on another stack. That could confuse callers quite a bit.
154 // Instead, we require that gentraceback and any other function that
155 // accepts an sp for the current goroutine (typically obtained by
156 // calling getcallersp) must not run on that goroutine's stack but
157 // instead on the g0 stack.
Keith Randallb2a950b2014-12-27 20:58:00 -0800158 throw("gentraceback cannot trace user goroutine on its own stack")
Russ Cox39bcbb32014-11-05 23:01:48 -0500159 }
Russ Coxbf1de1b2015-10-30 11:03:02 -0400160 level, _, _ := gotraceback()
Austin Clementsfaa7a7e2015-05-20 16:30:49 -0400161
Russ Coxfa2af442014-09-02 15:12:53 -0400162 if pc0 == ^uintptr(0) && sp0 == ^uintptr(0) { // Signal to fetch saved values from gp.
Russ Cox15b76ad2014-09-09 13:39:57 -0400163 if gp.syscallsp != 0 {
Russ Coxfa2af442014-09-02 15:12:53 -0400164 pc0 = gp.syscallpc
165 sp0 = gp.syscallsp
166 if usesLR {
167 lr0 = 0
168 }
169 } else {
170 pc0 = gp.sched.pc
171 sp0 = gp.sched.sp
172 if usesLR {
173 lr0 = gp.sched.lr
174 }
175 }
176 }
177
178 nprint := 0
179 var frame stkframe
180 frame.pc = pc0
181 frame.sp = sp0
182 if usesLR {
183 frame.lr = lr0
184 }
185 waspanic := false
Ian Lance Taylor5f9a8702016-04-27 14:18:29 -0700186 cgoCtxt := gp.cgoCtxt
Russ Coxfa2af442014-09-02 15:12:53 -0400187 printing := pcbuf == nil && callback == nil
Russ Coxfa2af442014-09-02 15:12:53 -0400188 _defer := gp._defer
Austin Clements032678e2017-11-09 17:55:45 -0500189 elideWrapper := false
Russ Coxfa2af442014-09-02 15:12:53 -0400190
Matthew Dempskya03bdc32016-02-29 15:01:00 -0800191 for _defer != nil && _defer.sp == _NoArgs {
Russ Coxfa2af442014-09-02 15:12:53 -0400192 _defer = _defer.link
193 }
Russ Coxfa2af442014-09-02 15:12:53 -0400194
195 // If the PC is zero, it's likely a nil function call.
196 // Start in the caller's frame.
197 if frame.pc == 0 {
198 if usesLR {
199 frame.pc = *(*uintptr)(unsafe.Pointer(frame.sp))
200 frame.lr = 0
201 } else {
Michael Matloob432cb662015-11-11 12:39:30 -0500202 frame.pc = uintptr(*(*sys.Uintreg)(unsafe.Pointer(frame.sp)))
203 frame.sp += sys.RegSize
Russ Coxfa2af442014-09-02 15:12:53 -0400204 }
205 }
206
207 f := findfunc(frame.pc)
Austin Clements0efc8b22017-02-20 22:37:07 -0500208 if !f.valid() {
Austin Clementsdbd8f3d2018-01-22 14:53:36 -0500209 if callback != nil || printing {
Russ Coxfa2af442014-09-02 15:12:53 -0400210 print("runtime: unknown pc ", hex(frame.pc), "\n")
Austin Clementsdbd8f3d2018-01-22 14:53:36 -0500211 tracebackHexdump(gp.stack, &frame, 0)
212 }
213 if callback != nil {
Keith Randallb2a950b2014-12-27 20:58:00 -0800214 throw("unknown pc")
Russ Coxfa2af442014-09-02 15:12:53 -0400215 }
216 return 0
217 }
218 frame.fn = f
219
Austin Clementsbeedb1e2015-08-12 23:43:43 -0400220 var cache pcvalueCache
221
Russ Coxfa2af442014-09-02 15:12:53 -0400222 n := 0
Russ Coxfa2af442014-09-02 15:12:53 -0400223 for n < max {
224 // Typically:
225 // pc is the PC of the running function.
226 // sp is the stack pointer at that program counter.
227 // fp is the frame pointer (caller's stack pointer) at that program counter, or nil if unknown.
228 // stk is the stack containing sp.
229 // The caller's program counter is lr, unless lr is zero, in which case it is *(uintptr*)sp.
Russ Coxfa2af442014-09-02 15:12:53 -0400230 f = frame.fn
Russ Cox88d3db02016-01-06 12:45:23 -0500231 if f.pcsp == 0 {
232 // No frame information, must be external function, like race support.
233 // See golang.org/issue/13568.
234 break
235 }
Russ Coxfa2af442014-09-02 15:12:53 -0400236
Russ Coxfa2af442014-09-02 15:12:53 -0400237 // Found an actual function.
238 // Derive frame pointer and link register.
239 if frame.fp == 0 {
Daniel Morsingdb6f88a2015-04-30 15:32:54 +0100240 // We want to jump over the systemstack switch. If we're running on the
241 // g0, this systemstack is at the top of the stack.
242 // if we're not on g0 or there's a no curg, then this is a regular call.
243 sp := frame.sp
244 if flags&_TraceJumpStack != 0 && f.entry == systemstackPC && gp == g.m.g0 && gp.m.curg != nil {
245 sp = gp.m.curg.sched.sp
Austin Clementsa640d952016-05-20 14:57:55 -0400246 frame.sp = sp
Ian Lance Taylor5f9a8702016-04-27 14:18:29 -0700247 cgoCtxt = gp.m.curg.cgoCtxt
Daniel Morsingdb6f88a2015-04-30 15:32:54 +0100248 }
Austin Clementsbeedb1e2015-08-12 23:43:43 -0400249 frame.fp = sp + uintptr(funcspdelta(f, frame.pc, &cache))
Russ Coxfa2af442014-09-02 15:12:53 -0400250 if !usesLR {
251 // On x86, call instruction pushes return PC before entering new function.
Michael Matloob432cb662015-11-11 12:39:30 -0500252 frame.fp += sys.RegSize
Russ Coxfa2af442014-09-02 15:12:53 -0400253 }
254 }
Austin Clements0efc8b22017-02-20 22:37:07 -0500255 var flr funcInfo
Austin Clements5c2be422018-01-30 16:01:33 -0500256 if topofstack(f, gp.m != nil && gp == gp.m.g0) {
Russ Coxfa2af442014-09-02 15:12:53 -0400257 frame.lr = 0
Austin Clements0efc8b22017-02-20 22:37:07 -0500258 flr = funcInfo{}
Russ Coxfa2af442014-09-02 15:12:53 -0400259 } else if usesLR && f.entry == jmpdeferPC {
260 // jmpdefer modifies SP/LR/PC non-atomically.
261 // If a profiling interrupt arrives during jmpdefer,
262 // the stack unwind may see a mismatched register set
263 // and get confused. Stop if we see PC within jmpdefer
264 // to avoid that confusion.
265 // See golang.org/issue/8153.
266 if callback != nil {
Keith Randallb2a950b2014-12-27 20:58:00 -0800267 throw("traceback_arm: found jmpdefer when tracing with callback")
Russ Coxfa2af442014-09-02 15:12:53 -0400268 }
269 frame.lr = 0
270 } else {
Austin Clementsfaa7a7e2015-05-20 16:30:49 -0400271 var lrPtr uintptr
Russ Coxfa2af442014-09-02 15:12:53 -0400272 if usesLR {
273 if n == 0 && frame.sp < frame.fp || frame.lr == 0 {
Austin Clementsfaa7a7e2015-05-20 16:30:49 -0400274 lrPtr = frame.sp
275 frame.lr = *(*uintptr)(unsafe.Pointer(lrPtr))
Russ Coxfa2af442014-09-02 15:12:53 -0400276 }
277 } else {
278 if frame.lr == 0 {
Michael Matloob432cb662015-11-11 12:39:30 -0500279 lrPtr = frame.fp - sys.RegSize
280 frame.lr = uintptr(*(*sys.Uintreg)(unsafe.Pointer(lrPtr)))
Russ Coxfa2af442014-09-02 15:12:53 -0400281 }
282 }
283 flr = findfunc(frame.lr)
Austin Clements0efc8b22017-02-20 22:37:07 -0500284 if !flr.valid() {
Russ Coxfa2af442014-09-02 15:12:53 -0400285 // This happens if you get a profiling interrupt at just the wrong time.
286 // In that context it is okay to stop early.
287 // But if callback is set, we're doing a garbage collection and must
288 // get everything, so crash loudly.
Austin Clementsebe38b82018-01-30 16:03:51 -0500289 doPrint := printing
Austin Clementsddb503b2018-01-31 12:05:24 -0500290 if doPrint && gp.m.incgo && f.entry == sigpanicPC {
Austin Clementsebe38b82018-01-30 16:03:51 -0500291 // We can inject sigpanic
292 // calls directly into C code,
293 // in which case we'll see a C
294 // return PC. Don't complain.
295 doPrint = false
296 }
297 if callback != nil || doPrint {
Keith Randall0bb8fc62014-12-28 23:16:32 -0800298 print("runtime: unexpected return pc for ", funcname(f), " called from ", hex(frame.lr), "\n")
Austin Clementsdbd8f3d2018-01-22 14:53:36 -0500299 tracebackHexdump(gp.stack, &frame, lrPtr)
300 }
301 if callback != nil {
Keith Randallb2a950b2014-12-27 20:58:00 -0800302 throw("unknown caller pc")
Russ Coxfa2af442014-09-02 15:12:53 -0400303 }
304 }
305 }
306
307 frame.varp = frame.fp
308 if !usesLR {
309 // On x86, call instruction pushes return PC before entering new function.
Michael Matloob432cb662015-11-11 12:39:30 -0500310 frame.varp -= sys.RegSize
Russ Coxfa2af442014-09-02 15:12:53 -0400311 }
312
Austin Clements3c0fee12015-01-14 11:09:50 -0500313 // If framepointer_enabled and there's a frame, then
314 // there's a saved bp here.
Austin Clementsfc5baec2015-02-03 09:18:15 -0500315 if framepointer_enabled && GOARCH == "amd64" && frame.varp > frame.sp {
Michael Matloob432cb662015-11-11 12:39:30 -0500316 frame.varp -= sys.RegSize
Austin Clements3c0fee12015-01-14 11:09:50 -0500317 }
318
Russ Coxfa2af442014-09-02 15:12:53 -0400319 // Derive size of arguments.
320 // Most functions have a fixed-size argument block,
321 // so we can use metadata about the function f.
322 // Not all, though: there are some variadic functions
323 // in package runtime and reflect, and for those we use call-specific
324 // metadata recorded by f's caller.
325 if callback != nil || printing {
Michael Matloob432cb662015-11-11 12:39:30 -0500326 frame.argp = frame.fp + sys.MinFrameSize
Austin Clementsd5bd7972016-10-16 18:23:39 -0400327 frame.arglen, frame.argmap = getArgInfo(&frame, f, callback != nil, nil)
Russ Coxfa2af442014-09-02 15:12:53 -0400328 }
329
Russ Coxfa2af442014-09-02 15:12:53 -0400330 // Determine frame's 'continuation PC', where it can continue.
331 // Normally this is the return address on the stack, but if sigpanic
332 // is immediately below this function on the stack, then the frame
333 // stopped executing due to a trap, and frame.pc is probably not
334 // a safe point for looking up liveness information. In this panicking case,
335 // the function either doesn't return at all (if it has no defers or if the
336 // defers do not recover) or it returns from one of the calls to
337 // deferproc a second time (if the corresponding deferred func recovers).
338 // It suffices to assume that the most recent deferproc is the one that
339 // returns; everything live at earlier deferprocs is still live at that one.
340 frame.continpc = frame.pc
341 if waspanic {
Keith Randall53c52262014-12-08 14:18:58 -0800342 if _defer != nil && _defer.sp == frame.sp {
Russ Coxfa2af442014-09-02 15:12:53 -0400343 frame.continpc = _defer.pc
344 } else {
345 frame.continpc = 0
346 }
347 }
348
Russ Coxf95beae2014-09-16 10:36:38 -0400349 // Unwind our local defer stack past this frame.
Keith Randall53c52262014-12-08 14:18:58 -0800350 for _defer != nil && (_defer.sp == frame.sp || _defer.sp == _NoArgs) {
Russ Coxfa2af442014-09-02 15:12:53 -0400351 _defer = _defer.link
352 }
353
Russ Coxfa2af442014-09-02 15:12:53 -0400354 if callback != nil {
355 if !callback((*stkframe)(noescape(unsafe.Pointer(&frame))), v) {
356 return n
357 }
358 }
David Lazaree972162017-03-06 14:48:36 -0500359
360 if pcbuf != nil {
361 if skip == 0 {
362 (*[1 << 20]uintptr)(unsafe.Pointer(pcbuf))[n] = frame.pc
363 } else {
364 // backup to CALL instruction to read inlining info (same logic as below)
365 tracepc := frame.pc
366 if (n > 0 || flags&_TraceTrap == 0) && frame.pc > f.entry && !waspanic {
367 tracepc--
368 }
369 inldata := funcdata(f, _FUNCDATA_InlTree)
370
371 // no inlining info, skip the physical frame
372 if inldata == nil {
373 skip--
374 goto skipped
375 }
376
377 ix := pcdatavalue(f, _PCDATA_InlTreeIndex, tracepc, &cache)
378 inltree := (*[1 << 20]inlinedCall)(inldata)
379 // skip the logical (inlined) frames
380 logicalSkipped := 0
381 for ix >= 0 && skip > 0 {
382 skip--
383 logicalSkipped++
384 ix = inltree[ix].parent
385 }
386
387 // skip the physical frame if there's more to skip
388 if skip > 0 {
389 skip--
390 goto skipped
391 }
392
393 // now we have a partially skipped frame
394 (*[1 << 20]uintptr)(unsafe.Pointer(pcbuf))[n] = frame.pc
395
396 // if there's room, pcbuf[1] is a skip PC that encodes the number of skipped frames in pcbuf[0]
397 if n+1 < max {
398 n++
399 skipPC := funcPC(skipPleaseUseCallersFrames) + uintptr(logicalSkipped)
400 (*[1 << 20]uintptr)(unsafe.Pointer(pcbuf))[n] = skipPC
401 }
402 }
403 }
404
Russ Coxfa2af442014-09-02 15:12:53 -0400405 if printing {
Austin Clements032678e2017-11-09 17:55:45 -0500406 // assume skip=0 for printing.
407 //
408 // Never elide wrappers if we haven't printed
409 // any frames. And don't elide wrappers that
410 // called panic rather than the wrapped
411 // function. Otherwise, leave them out.
412 name := funcname(f)
413 nextElideWrapper := elideWrapperCalling(name)
414 if (flags&_TraceRuntimeFrames) != 0 || showframe(f, gp, nprint == 0, elideWrapper && nprint != 0) {
Russ Coxfa2af442014-09-02 15:12:53 -0400415 // Print during crash.
416 // main(0x1, 0x2, 0x3)
417 // /home/rsc/go/src/runtime/x.go:23 +0xf
418 //
419 tracepc := frame.pc // back up to CALL instruction for funcline.
Russ Coxa22c11b2014-10-29 15:14:24 -0400420 if (n > 0 || flags&_TraceTrap == 0) && frame.pc > f.entry && !waspanic {
Russ Coxfa2af442014-09-02 15:12:53 -0400421 tracepc--
422 }
David Lazar781fd392017-02-17 16:08:36 -0500423 file, line := funcline(f, tracepc)
424 inldata := funcdata(f, _FUNCDATA_InlTree)
425 if inldata != nil {
426 inltree := (*[1 << 20]inlinedCall)(inldata)
427 ix := pcdatavalue(f, _PCDATA_InlTreeIndex, tracepc, nil)
428 for ix != -1 {
429 name := funcnameFromNameoff(f, inltree[ix].func_)
430 print(name, "(...)\n")
431 print("\t", file, ":", line, "\n")
432
433 file = funcfile(f, inltree[ix].file)
434 line = inltree[ix].line
435 ix = inltree[ix].parent
436 }
437 }
Austin Clements0c02bc02016-02-12 10:33:51 -0500438 if name == "runtime.gopanic" {
439 name = "panic"
440 }
441 print(name, "(")
Russ Coxfa2af442014-09-02 15:12:53 -0400442 argp := (*[100]uintptr)(unsafe.Pointer(frame.argp))
Michael Matloob432cb662015-11-11 12:39:30 -0500443 for i := uintptr(0); i < frame.arglen/sys.PtrSize; i++ {
Russ Coxfa2af442014-09-02 15:12:53 -0400444 if i >= 10 {
445 print(", ...")
446 break
447 }
448 if i != 0 {
449 print(", ")
450 }
451 print(hex(argp[i]))
452 }
453 print(")\n")
Russ Coxfa2af442014-09-02 15:12:53 -0400454 print("\t", file, ":", line)
455 if frame.pc > f.entry {
456 print(" +", hex(frame.pc-f.entry))
457 }
Russ Coxbf1de1b2015-10-30 11:03:02 -0400458 if g.m.throwing > 0 && gp == g.m.curg || level >= 2 {
Austin Clements27f88732017-06-09 11:58:53 -0400459 print(" fp=", hex(frame.fp), " sp=", hex(frame.sp), " pc=", hex(frame.pc))
Russ Coxfa2af442014-09-02 15:12:53 -0400460 }
461 print("\n")
462 nprint++
463 }
Austin Clements032678e2017-11-09 17:55:45 -0500464 elideWrapper = nextElideWrapper
Russ Coxfa2af442014-09-02 15:12:53 -0400465 }
466 n++
467
468 skipped:
Ian Lance Taylor5f9a8702016-04-27 14:18:29 -0700469 if f.entry == cgocallback_gofuncPC && len(cgoCtxt) > 0 {
470 ctxt := cgoCtxt[len(cgoCtxt)-1]
471 cgoCtxt = cgoCtxt[:len(cgoCtxt)-1]
472
473 // skip only applies to Go frames.
474 // callback != nil only used when we only care
475 // about Go frames.
476 if skip == 0 && callback == nil {
477 n = tracebackCgoContext(pcbuf, printing, ctxt, n, max)
478 }
479 }
480
Russ Cox97f83862014-09-03 13:02:48 -0400481 waspanic = f.entry == sigpanicPC
Russ Coxfa2af442014-09-02 15:12:53 -0400482
483 // Do not unwind past the bottom of the stack.
Austin Clements0efc8b22017-02-20 22:37:07 -0500484 if !flr.valid() {
Russ Coxfa2af442014-09-02 15:12:53 -0400485 break
486 }
487
488 // Unwind to next frame.
489 frame.fn = flr
490 frame.pc = frame.lr
491 frame.lr = 0
492 frame.sp = frame.fp
493 frame.fp = 0
Russ Coxf0d44db2014-09-12 07:29:19 -0400494 frame.argmap = nil
Russ Coxfa2af442014-09-02 15:12:53 -0400495
496 // On link register architectures, sighandler saves the LR on stack
497 // before faking a call to sigpanic.
498 if usesLR && waspanic {
499 x := *(*uintptr)(unsafe.Pointer(frame.sp))
Michael Matloob432cb662015-11-11 12:39:30 -0500500 frame.sp += sys.MinFrameSize
Aram Hăvărneanu846ee042015-03-08 14:20:20 +0100501 if GOARCH == "arm64" {
502 // arm64 needs 16-byte aligned SP, always
Michael Matloob432cb662015-11-11 12:39:30 -0500503 frame.sp += sys.PtrSize
Aram Hăvărneanu846ee042015-03-08 14:20:20 +0100504 }
Russ Coxfa2af442014-09-02 15:12:53 -0400505 f = findfunc(frame.pc)
506 frame.fn = f
Austin Clements0efc8b22017-02-20 22:37:07 -0500507 if !f.valid() {
Russ Coxfa2af442014-09-02 15:12:53 -0400508 frame.pc = x
Austin Clementsbeedb1e2015-08-12 23:43:43 -0400509 } else if funcspdelta(f, frame.pc, &cache) == 0 {
Russ Coxfa2af442014-09-02 15:12:53 -0400510 frame.lr = x
511 }
512 }
513 }
514
Matthew Dempsky3c8a89d2015-02-25 14:41:21 +0900515 if printing {
Russ Coxfa2af442014-09-02 15:12:53 -0400516 n = nprint
517 }
518
519 // If callback != nil, we're being called to gather stack information during
520 // garbage collection or stack growth. In that context, require that we used
521 // up the entire defer stack. If not, then there is a bug somewhere and the
522 // garbage collection or stack growth may not have seen the correct picture
523 // of the stack. Crash now instead of silently executing the garbage collection
524 // or stack copy incorrectly and setting up for a mysterious crash later.
525 //
526 // Note that panic != nil is okay here: there can be leftover panics,
527 // because the defers on the panic stack do not nest in frame order as
528 // they do on the defer stack. If you have:
529 //
530 // frame 1 defers d1
531 // frame 2 defers d2
532 // frame 3 defers d3
533 // frame 4 panics
534 // frame 4's panic starts running defers
535 // frame 5, running d3, defers d4
536 // frame 5 panics
537 // frame 5's panic starts running defers
538 // frame 6, running d4, garbage collects
539 // frame 6, running d2, garbage collects
540 //
541 // During the execution of d4, the panic stack is d4 -> d3, which
542 // is nested properly, and we'll treat frame 3 as resumable, because we
543 // can find d3. (And in fact frame 3 is resumable. If d4 recovers
544 // and frame 5 continues running, d3, d3 can recover and we'll
545 // resume execution in (returning from) frame 3.)
546 //
547 // During the execution of d2, however, the panic stack is d2 -> d3,
548 // which is inverted. The scan will match d2 to frame 2 but having
549 // d2 on the stack until then means it will not match d3 to frame 3.
550 // This is okay: if we're running d2, then all the defers after d2 have
551 // completed and their corresponding frames are dead. Not finding d3
552 // for frame 3 means we'll set frame 3's continpc == 0, which is correct
553 // (frame 3 is dead). At the end of the walk the panic stack can thus
554 // contain defers (d3 in this case) for dead frames. The inversion here
555 // always indicates a dead frame, and the effect of the inversion on the
556 // scan is to hide those dead frames, so the scan is still okay:
557 // what's left on the panic stack are exactly (and only) the dead frames.
558 //
559 // We require callback != nil here because only when callback != nil
560 // do we know that gentraceback is being called in a "must be correct"
561 // context as opposed to a "best effort" context. The tracebacks with
562 // callbacks only happen when everything is stopped nicely.
563 // At other times, such as when gathering a stack for a profiling signal
564 // or when printing a traceback during a crash, everything may not be
565 // stopped nicely, and the stack walk may not be able to complete.
566 // It's okay in those situations not to use up the entire defer stack:
567 // incomplete information then is still better than nothing.
568 if callback != nil && n < max && _defer != nil {
569 if _defer != nil {
Keith Randall53c52262014-12-08 14:18:58 -0800570 print("runtime: g", gp.goid, ": leftover defer sp=", hex(_defer.sp), " pc=", hex(_defer.pc), "\n")
Russ Coxfa2af442014-09-02 15:12:53 -0400571 }
Russ Coxfa2af442014-09-02 15:12:53 -0400572 for _defer = gp._defer; _defer != nil; _defer = _defer.link {
Keith Randall53c52262014-12-08 14:18:58 -0800573 print("\tdefer ", _defer, " sp=", hex(_defer.sp), " pc=", hex(_defer.pc), "\n")
Russ Coxfa2af442014-09-02 15:12:53 -0400574 }
Keith Randallb2a950b2014-12-27 20:58:00 -0800575 throw("traceback has leftover defers")
Russ Coxfa2af442014-09-02 15:12:53 -0400576 }
577
Russ Cox9c04d002015-08-26 11:39:10 -0400578 if callback != nil && n < max && frame.sp != gp.stktopsp {
579 print("runtime: g", gp.goid, ": frame.sp=", hex(frame.sp), " top=", hex(gp.stktopsp), "\n")
580 print("\tstack=[", hex(gp.stack.lo), "-", hex(gp.stack.hi), "] n=", n, " max=", max, "\n")
581 throw("traceback did not unwind completely")
582 }
583
Russ Coxfa2af442014-09-02 15:12:53 -0400584 return n
585}
586
Austin Clementsddd558e2016-12-16 11:57:25 -0500587// reflectMethodValue is a partial duplicate of reflect.makeFuncImpl
588// and reflect.methodValue.
Austin Clementsd5bd7972016-10-16 18:23:39 -0400589type reflectMethodValue struct {
590 fn uintptr
591 stack *bitvector // args bitmap
592}
593
594// getArgInfo returns the argument frame information for a call to f
595// with call frame frame.
596//
597// This is used for both actual calls with active stack frames and for
598// deferred calls that are not yet executing. If this is an actual
599// call, ctxt must be nil (getArgInfo will retrieve what it needs from
600// the active stack frame). If this is a deferred call, ctxt must be
601// the function object that was deferred.
Austin Clements0efc8b22017-02-20 22:37:07 -0500602func getArgInfo(frame *stkframe, f funcInfo, needArgMap bool, ctxt *funcval) (arglen uintptr, argmap *bitvector) {
Austin Clementsb43b3752015-11-17 17:14:32 -0500603 arglen = uintptr(f.args)
Russ Coxf95beae2014-09-16 10:36:38 -0400604 if needArgMap && f.args == _ArgsSizeUnknown {
605 // Extract argument bitmaps for reflect stubs from the calls they made to reflect.
Keith Randall0bb8fc62014-12-28 23:16:32 -0800606 switch funcname(f) {
Russ Coxf95beae2014-09-16 10:36:38 -0400607 case "reflect.makeFuncStub", "reflect.methodValueCall":
Austin Clementsd5bd7972016-10-16 18:23:39 -0400608 // These take a *reflect.methodValue as their
609 // context register.
610 var mv *reflectMethodValue
611 if ctxt != nil {
612 // This is not an actual call, but a
613 // deferred call. The function value
614 // is itself the *reflect.methodValue.
615 mv = (*reflectMethodValue)(unsafe.Pointer(ctxt))
616 } else {
617 // This is a real call that took the
618 // *reflect.methodValue as its context
619 // register and immediately saved it
620 // to 0(SP). Get the methodValue from
621 // 0(SP).
622 arg0 := frame.sp + sys.MinFrameSize
623 mv = *(**reflectMethodValue)(unsafe.Pointer(arg0))
624 }
625 if mv.fn != f.entry {
Keith Randall0bb8fc62014-12-28 23:16:32 -0800626 print("runtime: confused by ", funcname(f), "\n")
Keith Randallb2a950b2014-12-27 20:58:00 -0800627 throw("reflect mismatch")
Russ Coxf95beae2014-09-16 10:36:38 -0400628 }
Austin Clementsd5bd7972016-10-16 18:23:39 -0400629 bv := mv.stack
Austin Clementsb43b3752015-11-17 17:14:32 -0500630 arglen = uintptr(bv.n * sys.PtrSize)
631 argmap = bv
Russ Coxf95beae2014-09-16 10:36:38 -0400632 }
633 }
Austin Clementsb43b3752015-11-17 17:14:32 -0500634 return
Russ Coxf95beae2014-09-16 10:36:38 -0400635}
636
Ian Lance Taylor5f9a8702016-04-27 14:18:29 -0700637// tracebackCgoContext handles tracing back a cgo context value, from
638// the context argument to setCgoTraceback, for the gentraceback
639// function. It returns the new value of n.
640func tracebackCgoContext(pcbuf *uintptr, printing bool, ctxt uintptr, n, max int) int {
641 var cgoPCs [32]uintptr
642 cgoContextPCs(ctxt, cgoPCs[:])
643 var arg cgoSymbolizerArg
644 anySymbolized := false
645 for _, pc := range cgoPCs {
646 if pc == 0 || n >= max {
647 break
648 }
649 if pcbuf != nil {
650 (*[1 << 20]uintptr)(unsafe.Pointer(pcbuf))[n] = pc
651 }
652 if printing {
653 if cgoSymbolizer == nil {
654 print("non-Go function at pc=", hex(pc), "\n")
655 } else {
656 c := printOneCgoTraceback(pc, max-n, &arg)
657 n += c - 1 // +1 a few lines down
658 anySymbolized = true
659 }
660 }
661 n++
662 }
663 if anySymbolized {
664 arg.pc = 0
665 callCgoSymbolizer(&arg)
666 }
667 return n
668}
669
Russ Coxfa2af442014-09-02 15:12:53 -0400670func printcreatedby(gp *g) {
671 // Show what created goroutine, except main goroutine (goid 1).
672 pc := gp.gopc
673 f := findfunc(pc)
Austin Clements032678e2017-11-09 17:55:45 -0500674 if f.valid() && showframe(f, gp, false, false) && gp.goid != 1 {
Keith Randall0bb8fc62014-12-28 23:16:32 -0800675 print("created by ", funcname(f), "\n")
Russ Coxfa2af442014-09-02 15:12:53 -0400676 tracepc := pc // back up to CALL instruction for funcline.
677 if pc > f.entry {
Michael Matloob432cb662015-11-11 12:39:30 -0500678 tracepc -= sys.PCQuantum
Russ Coxfa2af442014-09-02 15:12:53 -0400679 }
Russ Cox656be312014-11-12 14:54:31 -0500680 file, line := funcline(f, tracepc)
Russ Coxfa2af442014-09-02 15:12:53 -0400681 print("\t", file, ":", line)
682 if pc > f.entry {
683 print(" +", hex(pc-f.entry))
684 }
685 print("\n")
686 }
687}
688
Matthew Dempsky3c8a89d2015-02-25 14:41:21 +0900689func traceback(pc, sp, lr uintptr, gp *g) {
Russ Coxa22c11b2014-10-29 15:14:24 -0400690 traceback1(pc, sp, lr, gp, 0)
691}
692
693// tracebacktrap is like traceback but expects that the PC and SP were obtained
694// from a trap, not from gp->sched or gp->syscallpc/gp->syscallsp or getcallerpc/getcallersp.
695// Because they are from a trap instead of from a saved pair,
696// the initial PC must not be rewound to the previous instruction.
697// (All the saved pairs record a PC that is a return address, so we
698// rewind it into the CALL instruction.)
Matthew Dempsky3c8a89d2015-02-25 14:41:21 +0900699func tracebacktrap(pc, sp, lr uintptr, gp *g) {
Russ Coxa22c11b2014-10-29 15:14:24 -0400700 traceback1(pc, sp, lr, gp, _TraceTrap)
701}
702
Matthew Dempsky3c8a89d2015-02-25 14:41:21 +0900703func traceback1(pc, sp, lr uintptr, gp *g, flags uint) {
Ian Lance Taylorea306ae2015-12-11 17:16:48 -0800704 // If the goroutine is in cgo, and we have a cgo traceback, print that.
705 if iscgo && gp.m != nil && gp.m.ncgo > 0 && gp.syscallsp != 0 && gp.m.cgoCallers != nil && gp.m.cgoCallers[0] != 0 {
706 // Lock cgoCallers so that a signal handler won't
707 // change it, copy the array, reset it, unlock it.
708 // We are locked to the thread and are not running
709 // concurrently with a signal handler.
710 // We just have to stop a signal handler from interrupting
711 // in the middle of our copy.
712 atomic.Store(&gp.m.cgoCallersUse, 1)
713 cgoCallers := *gp.m.cgoCallers
714 gp.m.cgoCallers[0] = 0
715 atomic.Store(&gp.m.cgoCallersUse, 0)
716
717 printCgoTraceback(&cgoCallers)
718 }
719
Russ Coxfa2af442014-09-02 15:12:53 -0400720 var n int
721 if readgstatus(gp)&^_Gscan == _Gsyscall {
Russ Coxa22c11b2014-10-29 15:14:24 -0400722 // Override registers if blocked in system call.
Russ Coxfa2af442014-09-02 15:12:53 -0400723 pc = gp.syscallpc
724 sp = gp.syscallsp
Russ Coxa22c11b2014-10-29 15:14:24 -0400725 flags &^= _TraceTrap
Russ Coxfa2af442014-09-02 15:12:53 -0400726 }
727 // Print traceback. By default, omits runtime frames.
728 // If that means we print nothing at all, repeat forcing all frames printed.
Russ Coxa22c11b2014-10-29 15:14:24 -0400729 n = gentraceback(pc, sp, lr, gp, 0, nil, _TracebackMaxFrames, nil, nil, flags)
730 if n == 0 && (flags&_TraceRuntimeFrames) == 0 {
731 n = gentraceback(pc, sp, lr, gp, 0, nil, _TracebackMaxFrames, nil, nil, flags|_TraceRuntimeFrames)
Russ Coxfa2af442014-09-02 15:12:53 -0400732 }
733 if n == _TracebackMaxFrames {
734 print("...additional frames elided...\n")
735 }
736 printcreatedby(gp)
737}
738
Matthew Dempsky3c8a89d2015-02-25 14:41:21 +0900739func callers(skip int, pcbuf []uintptr) int {
Russ Coxfa2af442014-09-02 15:12:53 -0400740 sp := getcallersp(unsafe.Pointer(&skip))
Austin Clements229aaac2017-09-22 15:16:26 -0400741 pc := getcallerpc()
Austin Clements719efc72015-05-20 11:50:48 -0400742 gp := getg()
Russ Cox39bcbb32014-11-05 23:01:48 -0500743 var n int
Russ Cox656be312014-11-12 14:54:31 -0500744 systemstack(func() {
Austin Clements719efc72015-05-20 11:50:48 -0400745 n = gentraceback(pc, sp, 0, gp, skip, &pcbuf[0], len(pcbuf), nil, nil, 0)
Russ Cox39bcbb32014-11-05 23:01:48 -0500746 })
747 return n
Russ Coxfa2af442014-09-02 15:12:53 -0400748}
749
Matthew Dempsky3c8a89d2015-02-25 14:41:21 +0900750func gcallers(gp *g, skip int, pcbuf []uintptr) int {
751 return gentraceback(^uintptr(0), ^uintptr(0), 0, gp, skip, &pcbuf[0], len(pcbuf), nil, nil, 0)
Russ Coxfa2af442014-09-02 15:12:53 -0400752}
Russ Cox7ba41e92014-09-03 11:11:16 -0400753
Austin Clements032678e2017-11-09 17:55:45 -0500754func showframe(f funcInfo, gp *g, firstFrame, elideWrapper bool) bool {
Russ Cox97f83862014-09-03 13:02:48 -0400755 g := getg()
Russ Cox181e26b2015-04-17 00:21:30 -0400756 if g.m.throwing > 0 && gp != nil && (gp == g.m.curg || gp == g.m.caughtsig.ptr()) {
Russ Cox97f83862014-09-03 13:02:48 -0400757 return true
758 }
Russ Coxbf1de1b2015-10-30 11:03:02 -0400759 level, _, _ := gotraceback()
Austin Clementse9720952017-06-12 11:12:12 -0400760 if level > 1 {
761 // Show all frames.
762 return true
763 }
764
Austin Clements032678e2017-11-09 17:55:45 -0500765 if !f.valid() {
766 return false
767 }
768
769 if elideWrapper {
770 file, _ := funcline(f, f.entry)
771 if file == "<autogenerated>" {
772 return false
773 }
774 }
775
Keith Randall0bb8fc62014-12-28 23:16:32 -0800776 name := funcname(f)
Russ Cox97f83862014-09-03 13:02:48 -0400777
Russ Coxf9feaff2016-11-12 23:01:37 -0500778 // Special case: always show runtime.gopanic frame
779 // in the middle of a stack trace, so that we can
780 // see the boundary between ordinary code and
781 // panic-induced deferred code.
Russ Cox97f83862014-09-03 13:02:48 -0400782 // See golang.org/issue/5832.
Russ Coxf9feaff2016-11-12 23:01:37 -0500783 if name == "runtime.gopanic" && !firstFrame {
Russ Cox97f83862014-09-03 13:02:48 -0400784 return true
785 }
786
Austin Clements032678e2017-11-09 17:55:45 -0500787 return contains(name, ".") && (!hasprefix(name, "runtime.") || isExportedRuntime(name))
Russ Cox54245cb2014-09-18 20:35:36 -0400788}
789
790// isExportedRuntime reports whether name is an exported runtime function.
791// It is only for runtime functions, so ASCII A-Z is fine.
792func isExportedRuntime(name string) bool {
793 const n = len("runtime.")
794 return len(name) > n && name[:n] == "runtime." && 'A' <= name[n] && name[n] <= 'Z'
Russ Cox97f83862014-09-03 13:02:48 -0400795}
796
Austin Clements032678e2017-11-09 17:55:45 -0500797// elideWrapperCalling returns whether a wrapper function that called
798// function "name" should be elided from stack traces.
799func elideWrapperCalling(name string) bool {
800 // If the wrapper called a panic function instead of the
801 // wrapped function, we want to include it in stacks.
802 return !(name == "runtime.gopanic" || name == "runtime.sigpanic" || name == "runtime.panicwrap")
803}
804
Russ Cox7ba41e92014-09-03 11:11:16 -0400805var gStatusStrings = [...]string{
806 _Gidle: "idle",
807 _Grunnable: "runnable",
808 _Grunning: "running",
809 _Gsyscall: "syscall",
810 _Gwaiting: "waiting",
811 _Gdead: "dead",
Russ Cox7ba41e92014-09-03 11:11:16 -0400812 _Gcopystack: "copystack",
813}
814
Russ Cox7ba41e92014-09-03 11:11:16 -0400815func goroutineheader(gp *g) {
816 gpstatus := readgstatus(gp)
817
Austin Clementsc7c7c702015-12-21 11:26:33 -0800818 isScan := gpstatus&_Gscan != 0
819 gpstatus &^= _Gscan // drop the scan bit
820
Russ Cox7ba41e92014-09-03 11:11:16 -0400821 // Basic string status
822 var status string
823 if 0 <= gpstatus && gpstatus < uint32(len(gStatusStrings)) {
824 status = gStatusStrings[gpstatus]
Russ Cox7ba41e92014-09-03 11:11:16 -0400825 } else {
826 status = "???"
827 }
828
829 // Override.
Austin Clementsc7c7c702015-12-21 11:26:33 -0800830 if gpstatus == _Gwaiting && gp.waitreason != "" {
Russ Cox7ba41e92014-09-03 11:11:16 -0400831 status = gp.waitreason
832 }
833
834 // approx time the G is blocked, in minutes
835 var waitfor int64
Russ Cox7ba41e92014-09-03 11:11:16 -0400836 if (gpstatus == _Gwaiting || gpstatus == _Gsyscall) && gp.waitsince != 0 {
837 waitfor = (nanotime() - gp.waitsince) / 60e9
838 }
839 print("goroutine ", gp.goid, " [", status)
Austin Clementsc7c7c702015-12-21 11:26:33 -0800840 if isScan {
841 print(" (scan)")
842 }
Russ Cox7ba41e92014-09-03 11:11:16 -0400843 if waitfor >= 1 {
844 print(", ", waitfor, " minutes")
845 }
Ian Lance Taylor165c15a2017-09-13 10:14:02 -0700846 if gp.lockedm != 0 {
Russ Cox7ba41e92014-09-03 11:11:16 -0400847 print(", locked to thread")
848 }
849 print("]:\n")
850}
851
852func tracebackothers(me *g) {
Russ Coxbf1de1b2015-10-30 11:03:02 -0400853 level, _, _ := gotraceback()
Russ Cox7ba41e92014-09-03 11:11:16 -0400854
855 // Show the current goroutine first, if we haven't already.
856 g := getg()
857 gp := g.m.curg
858 if gp != nil && gp != me {
859 print("\n")
860 goroutineheader(gp)
861 traceback(^uintptr(0), ^uintptr(0), 0, gp)
862 }
863
864 lock(&allglock)
865 for _, gp := range allgs {
Dmitry Vyukov59495e82015-02-07 15:31:18 +0300866 if gp == me || gp == g.m.curg || readgstatus(gp) == _Gdead || isSystemGoroutine(gp) && level < 2 {
Russ Cox7ba41e92014-09-03 11:11:16 -0400867 continue
868 }
869 print("\n")
870 goroutineheader(gp)
Keith Randall4b78c952015-04-28 14:53:19 -0700871 // Note: gp.m == g.m occurs when tracebackothers is
872 // called from a signal handler initiated during a
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +0000873 // systemstack call. The original G is still in the
Keith Randall4b78c952015-04-28 14:53:19 -0700874 // running state, and we want to print its stack.
875 if gp.m != g.m && readgstatus(gp)&^_Gscan == _Grunning {
Russ Cox7ba41e92014-09-03 11:11:16 -0400876 print("\tgoroutine running on other thread; stack unavailable\n")
877 printcreatedby(gp)
878 } else {
879 traceback(^uintptr(0), ^uintptr(0), 0, gp)
880 }
881 }
882 unlock(&allglock)
883}
884
Austin Clementsdbd8f3d2018-01-22 14:53:36 -0500885// tracebackHexdump hexdumps part of stk around frame.sp and frame.fp
886// for debugging purposes. If the address bad is included in the
887// hexdumped range, it will mark it as well.
888func tracebackHexdump(stk stack, frame *stkframe, bad uintptr) {
889 const expand = 32 * sys.PtrSize
890 const maxExpand = 256 * sys.PtrSize
891 // Start around frame.sp.
892 lo, hi := frame.sp, frame.sp
893 // Expand to include frame.fp.
894 if frame.fp != 0 && frame.fp < lo {
895 lo = frame.fp
896 }
897 if frame.fp != 0 && frame.fp > hi {
898 hi = frame.fp
899 }
900 // Expand a bit more.
901 lo, hi = lo-expand, hi+expand
902 // But don't go too far from frame.sp.
903 if lo < frame.sp-maxExpand {
904 lo = frame.sp - maxExpand
905 }
906 if hi > frame.sp+maxExpand {
907 hi = frame.sp + maxExpand
908 }
909 // And don't go outside the stack bounds.
910 if lo < stk.lo {
911 lo = stk.lo
912 }
913 if hi > stk.hi {
914 hi = stk.hi
915 }
916
917 // Print the hex dump.
918 print("stack: frame={sp:", hex(frame.sp), ", fp:", hex(frame.fp), "} stack=[", hex(stk.lo), ",", hex(stk.hi), ")\n")
919 hexdumpWords(lo, hi, func(p uintptr) byte {
920 switch p {
921 case frame.fp:
922 return '>'
923 case frame.sp:
924 return '<'
925 case bad:
926 return '!'
927 }
928 return 0
929 })
930}
931
Russ Cox7ba41e92014-09-03 11:11:16 -0400932// Does f mark the top of a goroutine stack?
Austin Clements5c2be422018-01-30 16:01:33 -0500933func topofstack(f funcInfo, g0 bool) bool {
Russ Cox7ba41e92014-09-03 11:11:16 -0400934 pc := f.entry
935 return pc == goexitPC ||
936 pc == mstartPC ||
937 pc == mcallPC ||
Russ Cox7ba41e92014-09-03 11:11:16 -0400938 pc == morestackPC ||
Russ Cox7ba41e92014-09-03 11:11:16 -0400939 pc == rt0_goPC ||
Austin Clements5c2be422018-01-30 16:01:33 -0500940 externalthreadhandlerp != 0 && pc == externalthreadhandlerp ||
941 // asmcgocall is TOS on the system stack because it
942 // switches to the system stack, but in this case we
943 // can come back to the regular stack and still want
944 // to be able to unwind through the call that appeared
945 // on the regular stack.
946 (g0 && pc == asmcgocallPC)
Russ Cox7ba41e92014-09-03 11:11:16 -0400947}
Dmitry Vyukov59495e82015-02-07 15:31:18 +0300948
Josh Bleecher Snyder2adc4e82015-02-17 15:44:42 -0800949// isSystemGoroutine reports whether the goroutine g must be omitted in
Dmitry Vyukov59495e82015-02-07 15:31:18 +0300950// stack dumps and deadlock detector.
951func isSystemGoroutine(gp *g) bool {
952 pc := gp.startpc
953 return pc == runfinqPC && !fingRunning ||
Dmitry Vyukov59495e82015-02-07 15:31:18 +0300954 pc == bgsweepPC ||
955 pc == forcegchelperPC ||
Austin Clements8d03acc2015-03-23 21:07:33 -0400956 pc == timerprocPC ||
957 pc == gcBgMarkWorkerPC
Dmitry Vyukov59495e82015-02-07 15:31:18 +0300958}
Ian Lance Taylorea306ae2015-12-11 17:16:48 -0800959
960// SetCgoTraceback records three C functions to use to gather
961// traceback information from C code and to convert that traceback
962// information into symbolic information. These are used when printing
963// stack traces for a program that uses cgo.
964//
965// The traceback and context functions may be called from a signal
966// handler, and must therefore use only async-signal safe functions.
967// The symbolizer function may be called while the program is
968// crashing, and so must be cautious about using memory. None of the
969// functions may call back into Go.
970//
971// The context function will be called with a single argument, a
972// pointer to a struct:
973//
Brad Fitzpatrick06d639e2016-04-27 16:56:13 -0500974// struct {
975// Context uintptr
976// }
Ian Lance Taylorea306ae2015-12-11 17:16:48 -0800977//
978// In C syntax, this struct will be
979//
Brad Fitzpatrick06d639e2016-04-27 16:56:13 -0500980// struct {
981// uintptr_t Context;
982// };
Ian Lance Taylorea306ae2015-12-11 17:16:48 -0800983//
984// If the Context field is 0, the context function is being called to
Ian Lance Taylor5f9a8702016-04-27 14:18:29 -0700985// record the current traceback context. It should record in the
986// Context field whatever information is needed about the current
987// point of execution to later produce a stack trace, probably the
988// stack pointer and PC. In this case the context function will be
989// called from C code.
Ian Lance Taylorea306ae2015-12-11 17:16:48 -0800990//
991// If the Context field is not 0, then it is a value returned by a
992// previous call to the context function. This case is called when the
993// context is no longer needed; that is, when the Go code is returning
Dmitry Vyukovba221722016-06-02 07:43:21 +0200994// to its C code caller. This permits the context function to release
995// any associated resources.
Ian Lance Taylorea306ae2015-12-11 17:16:48 -0800996//
997// While it would be correct for the context function to record a
998// complete a stack trace whenever it is called, and simply copy that
999// out in the traceback function, in a typical program the context
1000// function will be called many times without ever recording a
1001// traceback for that context. Recording a complete stack trace in a
1002// call to the context function is likely to be inefficient.
1003//
1004// The traceback function will be called with a single argument, a
1005// pointer to a struct:
1006//
Brad Fitzpatrick06d639e2016-04-27 16:56:13 -05001007// struct {
Ian Lance Taylor3d037cf2016-05-27 10:05:52 -07001008// Context uintptr
1009// SigContext uintptr
1010// Buf *uintptr
1011// Max uintptr
Brad Fitzpatrick06d639e2016-04-27 16:56:13 -05001012// }
Ian Lance Taylorea306ae2015-12-11 17:16:48 -08001013//
1014// In C syntax, this struct will be
1015//
Brad Fitzpatrick06d639e2016-04-27 16:56:13 -05001016// struct {
1017// uintptr_t Context;
Ian Lance Taylor3d037cf2016-05-27 10:05:52 -07001018// uintptr_t SigContext;
Brad Fitzpatrick06d639e2016-04-27 16:56:13 -05001019// uintptr_t* Buf;
1020// uintptr_t Max;
1021// };
Ian Lance Taylorea306ae2015-12-11 17:16:48 -08001022//
1023// The Context field will be zero to gather a traceback from the
1024// current program execution point. In this case, the traceback
1025// function will be called from C code.
1026//
1027// Otherwise Context will be a value previously returned by a call to
1028// the context function. The traceback function should gather a stack
1029// trace from that saved point in the program execution. The traceback
1030// function may be called from an execution thread other than the one
1031// that recorded the context, but only when the context is known to be
1032// valid and unchanging. The traceback function may also be called
1033// deeper in the call stack on the same thread that recorded the
1034// context. The traceback function may be called multiple times with
1035// the same Context value; it will usually be appropriate to cache the
1036// result, if possible, the first time this is called for a specific
1037// context value.
1038//
Ian Lance Taylor3d037cf2016-05-27 10:05:52 -07001039// If the traceback function is called from a signal handler on a Unix
1040// system, SigContext will be the signal context argument passed to
1041// the signal handler (a C ucontext_t* cast to uintptr_t). This may be
1042// used to start tracing at the point where the signal occurred. If
1043// the traceback function is not called from a signal handler,
1044// SigContext will be zero.
1045//
Ian Lance Taylorea306ae2015-12-11 17:16:48 -08001046// Buf is where the traceback information should be stored. It should
1047// be PC values, such that Buf[0] is the PC of the caller, Buf[1] is
1048// the PC of that function's caller, and so on. Max is the maximum
1049// number of entries to store. The function should store a zero to
1050// indicate the top of the stack, or that the caller is on a different
1051// stack, presumably a Go stack.
1052//
1053// Unlike runtime.Callers, the PC values returned should, when passed
1054// to the symbolizer function, return the file/line of the call
1055// instruction. No additional subtraction is required or appropriate.
1056//
1057// The symbolizer function will be called with a single argument, a
1058// pointer to a struct:
1059//
Brad Fitzpatrick06d639e2016-04-27 16:56:13 -05001060// struct {
1061// PC uintptr // program counter to fetch information for
1062// File *byte // file name (NUL terminated)
1063// Lineno uintptr // line number
1064// Func *byte // function name (NUL terminated)
1065// Entry uintptr // function entry point
1066// More uintptr // set non-zero if more info for this PC
1067// Data uintptr // unused by runtime, available for function
1068// }
Ian Lance Taylorea306ae2015-12-11 17:16:48 -08001069//
1070// In C syntax, this struct will be
1071//
Brad Fitzpatrick06d639e2016-04-27 16:56:13 -05001072// struct {
1073// uintptr_t PC;
1074// char* File;
1075// uintptr_t Lineno;
1076// char* Func;
1077// uintptr_t Entry;
1078// uintptr_t More;
1079// uintptr_t Data;
1080// };
Ian Lance Taylorea306ae2015-12-11 17:16:48 -08001081//
1082// The PC field will be a value returned by a call to the traceback
1083// function.
1084//
1085// The first time the function is called for a particular traceback,
1086// all the fields except PC will be 0. The function should fill in the
1087// other fields if possible, setting them to 0/nil if the information
1088// is not available. The Data field may be used to store any useful
1089// information across calls. The More field should be set to non-zero
1090// if there is more information for this PC, zero otherwise. If More
1091// is set non-zero, the function will be called again with the same
1092// PC, and may return different information (this is intended for use
1093// with inlined functions). If More is zero, the function will be
1094// called with the next PC value in the traceback. When the traceback
1095// is complete, the function will be called once more with PC set to
1096// zero; this may be used to free any information. Each call will
1097// leave the fields of the struct set to the same values they had upon
1098// return, except for the PC field when the More field is zero. The
1099// function must not keep a copy of the struct pointer between calls.
1100//
1101// When calling SetCgoTraceback, the version argument is the version
1102// number of the structs that the functions expect to receive.
1103// Currently this must be zero.
1104//
1105// The symbolizer function may be nil, in which case the results of
1106// the traceback function will be displayed as numbers. If the
1107// traceback function is nil, the symbolizer function will never be
1108// called. The context function may be nil, in which case the
1109// traceback function will only be called with the context field set
1110// to zero. If the context function is nil, then calls from Go to C
1111// to Go will not show a traceback for the C portion of the call stack.
Ian Lance Taylor03abde42016-06-02 12:01:03 -07001112//
1113// SetCgoTraceback should be called only once, ideally from an init function.
Ian Lance Taylorea306ae2015-12-11 17:16:48 -08001114func SetCgoTraceback(version int, traceback, context, symbolizer unsafe.Pointer) {
1115 if version != 0 {
1116 panic("unsupported version")
1117 }
Ian Lance Taylor5f9a8702016-04-27 14:18:29 -07001118
Ian Lance Taylor03abde42016-06-02 12:01:03 -07001119 if cgoTraceback != nil && cgoTraceback != traceback ||
1120 cgoContext != nil && cgoContext != context ||
1121 cgoSymbolizer != nil && cgoSymbolizer != symbolizer {
1122 panic("call SetCgoTraceback only once")
1123 }
1124
Ian Lance Taylorea306ae2015-12-11 17:16:48 -08001125 cgoTraceback = traceback
Ian Lance Taylor03abde42016-06-02 12:01:03 -07001126 cgoContext = context
Ian Lance Taylorea306ae2015-12-11 17:16:48 -08001127 cgoSymbolizer = symbolizer
Ian Lance Taylor5f9a8702016-04-27 14:18:29 -07001128
1129 // The context function is called when a C function calls a Go
1130 // function. As such it is only called by C code in runtime/cgo.
1131 if _cgo_set_context_function != nil {
1132 cgocall(_cgo_set_context_function, context)
1133 }
Ian Lance Taylorea306ae2015-12-11 17:16:48 -08001134}
1135
1136var cgoTraceback unsafe.Pointer
Ian Lance Taylor03abde42016-06-02 12:01:03 -07001137var cgoContext unsafe.Pointer
Ian Lance Taylorea306ae2015-12-11 17:16:48 -08001138var cgoSymbolizer unsafe.Pointer
1139
1140// cgoTracebackArg is the type passed to cgoTraceback.
1141type cgoTracebackArg struct {
Ian Lance Taylor3d037cf2016-05-27 10:05:52 -07001142 context uintptr
1143 sigContext uintptr
1144 buf *uintptr
1145 max uintptr
Ian Lance Taylorea306ae2015-12-11 17:16:48 -08001146}
1147
Ian Lance Taylor5f9a8702016-04-27 14:18:29 -07001148// cgoContextArg is the type passed to the context function.
Ian Lance Taylorea306ae2015-12-11 17:16:48 -08001149type cgoContextArg struct {
1150 context uintptr
1151}
1152
1153// cgoSymbolizerArg is the type passed to cgoSymbolizer.
1154type cgoSymbolizerArg struct {
1155 pc uintptr
1156 file *byte
1157 lineno uintptr
1158 funcName *byte
1159 entry uintptr
1160 more uintptr
1161 data uintptr
1162}
1163
1164// cgoTraceback prints a traceback of callers.
1165func printCgoTraceback(callers *cgoCallers) {
1166 if cgoSymbolizer == nil {
1167 for _, c := range callers {
1168 if c == 0 {
1169 break
1170 }
1171 print("non-Go function at pc=", hex(c), "\n")
1172 }
1173 return
1174 }
1175
Ian Lance Taylorea306ae2015-12-11 17:16:48 -08001176 var arg cgoSymbolizerArg
1177 for _, c := range callers {
1178 if c == 0 {
1179 break
1180 }
Ian Lance Taylor5f9a8702016-04-27 14:18:29 -07001181 printOneCgoTraceback(c, 0x7fffffff, &arg)
Ian Lance Taylorea306ae2015-12-11 17:16:48 -08001182 }
1183 arg.pc = 0
Ian Lance Taylor5f9a8702016-04-27 14:18:29 -07001184 callCgoSymbolizer(&arg)
1185}
1186
1187// printOneCgoTraceback prints the traceback of a single cgo caller.
1188// This can print more than one line because of inlining.
1189// Returns the number of frames printed.
1190func printOneCgoTraceback(pc uintptr, max int, arg *cgoSymbolizerArg) int {
1191 c := 0
1192 arg.pc = pc
1193 for {
1194 if c > max {
1195 break
1196 }
1197 callCgoSymbolizer(arg)
1198 if arg.funcName != nil {
1199 // Note that we don't print any argument
1200 // information here, not even parentheses.
1201 // The symbolizer must add that if appropriate.
1202 println(gostringnocopy(arg.funcName))
1203 } else {
1204 println("non-Go function")
1205 }
1206 print("\t")
1207 if arg.file != nil {
1208 print(gostringnocopy(arg.file), ":", arg.lineno, " ")
1209 }
Ian Lance Taylorc08436d2016-05-18 15:20:56 -07001210 print("pc=", hex(pc), "\n")
Ian Lance Taylor5f9a8702016-04-27 14:18:29 -07001211 c++
1212 if arg.more == 0 {
1213 break
1214 }
1215 }
1216 return c
1217}
1218
1219// callCgoSymbolizer calls the cgoSymbolizer function.
1220func callCgoSymbolizer(arg *cgoSymbolizerArg) {
1221 call := cgocall
1222 if panicking > 0 || getg().m.curg != getg() {
1223 // We do not want to call into the scheduler when panicking
1224 // or when on the system stack.
1225 call = asmcgocall
1226 }
Ian Lance Taylord00890b2016-07-10 18:44:09 -07001227 if msanenabled {
1228 msanwrite(unsafe.Pointer(arg), unsafe.Sizeof(cgoSymbolizerArg{}))
1229 }
Ian Lance Taylor5f9a8702016-04-27 14:18:29 -07001230 call(cgoSymbolizer, noescape(unsafe.Pointer(arg)))
1231}
1232
1233// cgoContextPCs gets the PC values from a cgo traceback.
1234func cgoContextPCs(ctxt uintptr, buf []uintptr) {
1235 if cgoTraceback == nil {
1236 return
1237 }
1238 call := cgocall
1239 if panicking > 0 || getg().m.curg != getg() {
1240 // We do not want to call into the scheduler when panicking
1241 // or when on the system stack.
1242 call = asmcgocall
1243 }
1244 arg := cgoTracebackArg{
1245 context: ctxt,
1246 buf: (*uintptr)(noescape(unsafe.Pointer(&buf[0]))),
1247 max: uintptr(len(buf)),
1248 }
Ian Lance Taylord00890b2016-07-10 18:44:09 -07001249 if msanenabled {
1250 msanwrite(unsafe.Pointer(&arg), unsafe.Sizeof(arg))
1251 }
Ian Lance Taylor5f9a8702016-04-27 14:18:29 -07001252 call(cgoTraceback, noescape(unsafe.Pointer(&arg)))
Ian Lance Taylorea306ae2015-12-11 17:16:48 -08001253}