blob: 501ecb0411cf0887065ae438f0e6c99fe7dc02ed [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
46 sigpanicPC uintptr
Dmitry Vyukov59495e82015-02-07 15:31:18 +030047 runfinqPC uintptr
Dmitry Vyukov59495e82015-02-07 15:31:18 +030048 bgsweepPC uintptr
49 forcegchelperPC uintptr
50 timerprocPC uintptr
Austin Clements8d03acc2015-03-23 21:07:33 -040051 gcBgMarkWorkerPC uintptr
Russ Cox656be312014-11-12 14:54:31 -050052 systemstack_switchPC uintptr
Daniel Morsingdb6f88a2015-04-30 15:32:54 +010053 systemstackPC uintptr
Austin Clementse2bb03f2015-08-26 12:16:51 -040054 cgocallback_gofuncPC uintptr
David Lazar7bf0adc2017-03-07 21:14:12 -050055 skipPC uintptr
Daniel Morsingdb6f88a2015-04-30 15:32:54 +010056
57 gogoPC uintptr
Russ Cox97f83862014-09-03 13:02:48 -040058
59 externalthreadhandlerp uintptr // initialized elsewhere
60)
Russ Coxfa2af442014-09-02 15:12:53 -040061
Keith Randall70b2da92014-09-29 21:21:36 -070062func tracebackinit() {
63 // Go variable initialization happens late during runtime startup.
64 // Instead of initializing the variables above in the declarations,
65 // schedinit calls this function so that the variables are
66 // initialized and available earlier in the startup sequence.
Keith Randall70b2da92014-09-29 21:21:36 -070067 goexitPC = funcPC(goexit)
68 jmpdeferPC = funcPC(jmpdefer)
69 mcallPC = funcPC(mcall)
70 morestackPC = funcPC(morestack)
71 mstartPC = funcPC(mstart)
Keith Randall70b2da92014-09-29 21:21:36 -070072 rt0_goPC = funcPC(rt0_go)
73 sigpanicPC = funcPC(sigpanic)
Dmitry Vyukov59495e82015-02-07 15:31:18 +030074 runfinqPC = funcPC(runfinq)
Dmitry Vyukov59495e82015-02-07 15:31:18 +030075 bgsweepPC = funcPC(bgsweep)
76 forcegchelperPC = funcPC(forcegchelper)
77 timerprocPC = funcPC(timerproc)
Austin Clements8d03acc2015-03-23 21:07:33 -040078 gcBgMarkWorkerPC = funcPC(gcBgMarkWorker)
Russ Cox656be312014-11-12 14:54:31 -050079 systemstack_switchPC = funcPC(systemstack_switch)
Daniel Morsingdb6f88a2015-04-30 15:32:54 +010080 systemstackPC = funcPC(systemstack)
Austin Clementse2bb03f2015-08-26 12:16:51 -040081 cgocallback_gofuncPC = funcPC(cgocallback_gofunc)
David Lazar7bf0adc2017-03-07 21:14:12 -050082 skipPC = funcPC(skipPleaseUseCallersFrames)
Daniel Morsingdb6f88a2015-04-30 15:32:54 +010083
84 // used by sigprof handler
85 gogoPC = funcPC(gogo)
Keith Randall70b2da92014-09-29 21:21:36 -070086}
87
Russ Coxf95beae2014-09-16 10:36:38 -040088// Traceback over the deferred function calls.
89// Report them like calls that have been invoked but not started executing yet.
90func tracebackdefers(gp *g, callback func(*stkframe, unsafe.Pointer) bool, v unsafe.Pointer) {
91 var frame stkframe
92 for d := gp._defer; d != nil; d = d.link {
93 fn := d.fn
94 if fn == nil {
95 // Defer of nil function. Args don't matter.
96 frame.pc = 0
Austin Clements0efc8b22017-02-20 22:37:07 -050097 frame.fn = funcInfo{}
Russ Coxf95beae2014-09-16 10:36:38 -040098 frame.argp = 0
99 frame.arglen = 0
100 frame.argmap = nil
101 } else {
Matthew Dempskya03bdc32016-02-29 15:01:00 -0800102 frame.pc = fn.fn
Russ Coxf95beae2014-09-16 10:36:38 -0400103 f := findfunc(frame.pc)
Austin Clements0efc8b22017-02-20 22:37:07 -0500104 if !f.valid() {
Russ Coxf95beae2014-09-16 10:36:38 -0400105 print("runtime: unknown pc in defer ", hex(frame.pc), "\n")
Keith Randallb2a950b2014-12-27 20:58:00 -0800106 throw("unknown pc")
Russ Coxf95beae2014-09-16 10:36:38 -0400107 }
108 frame.fn = f
109 frame.argp = uintptr(deferArgs(d))
Austin Clementsd5bd7972016-10-16 18:23:39 -0400110 frame.arglen, frame.argmap = getArgInfo(&frame, f, true, fn)
Russ Coxf95beae2014-09-16 10:36:38 -0400111 }
112 frame.continpc = frame.pc
113 if !callback((*stkframe)(noescape(unsafe.Pointer(&frame))), v) {
114 return
115 }
116 }
117}
Russ Coxfa2af442014-09-02 15:12:53 -0400118
David Lazaree972162017-03-06 14:48:36 -0500119const sizeofSkipFunction = 256
120
121// This function is defined in asm.s to be sizeofSkipFunction bytes long.
122func skipPleaseUseCallersFrames()
123
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +0000124// Generic traceback. Handles runtime stack prints (pcbuf == nil),
Russ Coxfa2af442014-09-02 15:12:53 -0400125// the runtime.Callers function (pcbuf != nil), as well as the garbage
126// collector (callback != nil). A little clunky to merge these, but avoids
127// duplicating the code and all its subtlety.
David Lazaree972162017-03-06 14:48:36 -0500128//
129// The skip argument is only valid with pcbuf != nil and counts the number
130// of logical frames to skip rather than physical frames (with inlining, a
131// PC in pcbuf can represent multiple calls). If a PC is partially skipped
132// and max > 1, pcbuf[1] will be runtime.skipPleaseUseCallersFrames+N where
133// N indicates the number of logical frames to skip in pcbuf[0].
Matthew Dempsky3c8a89d2015-02-25 14:41:21 +0900134func 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 -0500135 if skip > 0 && callback != nil {
136 throw("gentraceback callback cannot be used with non-zero skip")
137 }
Keith Randall70b2da92014-09-29 21:21:36 -0700138 if goexitPC == 0 {
Keith Randallb2a950b2014-12-27 20:58:00 -0800139 throw("gentraceback before goexitPC initialization")
Keith Randall70b2da92014-09-29 21:21:36 -0700140 }
Russ Coxfa2af442014-09-02 15:12:53 -0400141 g := getg()
Russ Cox39bcbb32014-11-05 23:01:48 -0500142 if g == gp && g == g.m.curg {
143 // The starting sp has been passed in as a uintptr, and the caller may
144 // have other uintptr-typed stack references as well.
145 // If during one of the calls that got us here or during one of the
146 // callbacks below the stack must be grown, all these uintptr references
147 // to the stack will not be updated, and gentraceback will continue
148 // to inspect the old stack memory, which may no longer be valid.
149 // Even if all the variables were updated correctly, it is not clear that
150 // we want to expose a traceback that begins on one stack and ends
151 // on another stack. That could confuse callers quite a bit.
152 // Instead, we require that gentraceback and any other function that
153 // accepts an sp for the current goroutine (typically obtained by
154 // calling getcallersp) must not run on that goroutine's stack but
155 // instead on the g0 stack.
Keith Randallb2a950b2014-12-27 20:58:00 -0800156 throw("gentraceback cannot trace user goroutine on its own stack")
Russ Cox39bcbb32014-11-05 23:01:48 -0500157 }
Russ Coxbf1de1b2015-10-30 11:03:02 -0400158 level, _, _ := gotraceback()
Austin Clementsfaa7a7e2015-05-20 16:30:49 -0400159
Russ Coxfa2af442014-09-02 15:12:53 -0400160 if pc0 == ^uintptr(0) && sp0 == ^uintptr(0) { // Signal to fetch saved values from gp.
Russ Cox15b76ad2014-09-09 13:39:57 -0400161 if gp.syscallsp != 0 {
Russ Coxfa2af442014-09-02 15:12:53 -0400162 pc0 = gp.syscallpc
163 sp0 = gp.syscallsp
164 if usesLR {
165 lr0 = 0
166 }
167 } else {
168 pc0 = gp.sched.pc
169 sp0 = gp.sched.sp
170 if usesLR {
171 lr0 = gp.sched.lr
172 }
173 }
174 }
175
176 nprint := 0
177 var frame stkframe
178 frame.pc = pc0
179 frame.sp = sp0
180 if usesLR {
181 frame.lr = lr0
182 }
183 waspanic := false
Ian Lance Taylor5f9a8702016-04-27 14:18:29 -0700184 cgoCtxt := gp.cgoCtxt
Russ Coxfa2af442014-09-02 15:12:53 -0400185 printing := pcbuf == nil && callback == nil
Russ Coxfa2af442014-09-02 15:12:53 -0400186 _defer := gp._defer
Austin Clements032678e2017-11-09 17:55:45 -0500187 elideWrapper := false
Russ Coxfa2af442014-09-02 15:12:53 -0400188
Matthew Dempskya03bdc32016-02-29 15:01:00 -0800189 for _defer != nil && _defer.sp == _NoArgs {
Russ Coxfa2af442014-09-02 15:12:53 -0400190 _defer = _defer.link
191 }
Russ Coxfa2af442014-09-02 15:12:53 -0400192
193 // If the PC is zero, it's likely a nil function call.
194 // Start in the caller's frame.
195 if frame.pc == 0 {
196 if usesLR {
197 frame.pc = *(*uintptr)(unsafe.Pointer(frame.sp))
198 frame.lr = 0
199 } else {
Michael Matloob432cb662015-11-11 12:39:30 -0500200 frame.pc = uintptr(*(*sys.Uintreg)(unsafe.Pointer(frame.sp)))
201 frame.sp += sys.RegSize
Russ Coxfa2af442014-09-02 15:12:53 -0400202 }
203 }
204
205 f := findfunc(frame.pc)
Austin Clements0efc8b22017-02-20 22:37:07 -0500206 if !f.valid() {
Russ Coxfa2af442014-09-02 15:12:53 -0400207 if callback != nil {
208 print("runtime: unknown pc ", hex(frame.pc), "\n")
Keith Randallb2a950b2014-12-27 20:58:00 -0800209 throw("unknown pc")
Russ Coxfa2af442014-09-02 15:12:53 -0400210 }
211 return 0
212 }
213 frame.fn = f
214
Austin Clementsbeedb1e2015-08-12 23:43:43 -0400215 var cache pcvalueCache
216
Russ Coxfa2af442014-09-02 15:12:53 -0400217 n := 0
Russ Coxfa2af442014-09-02 15:12:53 -0400218 for n < max {
219 // Typically:
220 // pc is the PC of the running function.
221 // sp is the stack pointer at that program counter.
222 // fp is the frame pointer (caller's stack pointer) at that program counter, or nil if unknown.
223 // stk is the stack containing sp.
224 // 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 -0400225 f = frame.fn
Russ Cox88d3db02016-01-06 12:45:23 -0500226 if f.pcsp == 0 {
227 // No frame information, must be external function, like race support.
228 // See golang.org/issue/13568.
229 break
230 }
Russ Coxfa2af442014-09-02 15:12:53 -0400231
Russ Coxfa2af442014-09-02 15:12:53 -0400232 // Found an actual function.
233 // Derive frame pointer and link register.
234 if frame.fp == 0 {
Daniel Morsingdb6f88a2015-04-30 15:32:54 +0100235 // We want to jump over the systemstack switch. If we're running on the
236 // g0, this systemstack is at the top of the stack.
237 // if we're not on g0 or there's a no curg, then this is a regular call.
238 sp := frame.sp
239 if flags&_TraceJumpStack != 0 && f.entry == systemstackPC && gp == g.m.g0 && gp.m.curg != nil {
240 sp = gp.m.curg.sched.sp
Austin Clementsa640d952016-05-20 14:57:55 -0400241 frame.sp = sp
Ian Lance Taylor5f9a8702016-04-27 14:18:29 -0700242 cgoCtxt = gp.m.curg.cgoCtxt
Daniel Morsingdb6f88a2015-04-30 15:32:54 +0100243 }
Austin Clementsbeedb1e2015-08-12 23:43:43 -0400244 frame.fp = sp + uintptr(funcspdelta(f, frame.pc, &cache))
Russ Coxfa2af442014-09-02 15:12:53 -0400245 if !usesLR {
246 // On x86, call instruction pushes return PC before entering new function.
Michael Matloob432cb662015-11-11 12:39:30 -0500247 frame.fp += sys.RegSize
Russ Coxfa2af442014-09-02 15:12:53 -0400248 }
249 }
Austin Clements0efc8b22017-02-20 22:37:07 -0500250 var flr funcInfo
Russ Coxfa2af442014-09-02 15:12:53 -0400251 if topofstack(f) {
252 frame.lr = 0
Austin Clements0efc8b22017-02-20 22:37:07 -0500253 flr = funcInfo{}
Russ Coxfa2af442014-09-02 15:12:53 -0400254 } else if usesLR && f.entry == jmpdeferPC {
255 // jmpdefer modifies SP/LR/PC non-atomically.
256 // If a profiling interrupt arrives during jmpdefer,
257 // the stack unwind may see a mismatched register set
258 // and get confused. Stop if we see PC within jmpdefer
259 // to avoid that confusion.
260 // See golang.org/issue/8153.
261 if callback != nil {
Keith Randallb2a950b2014-12-27 20:58:00 -0800262 throw("traceback_arm: found jmpdefer when tracing with callback")
Russ Coxfa2af442014-09-02 15:12:53 -0400263 }
264 frame.lr = 0
265 } else {
Austin Clementsfaa7a7e2015-05-20 16:30:49 -0400266 var lrPtr uintptr
Russ Coxfa2af442014-09-02 15:12:53 -0400267 if usesLR {
268 if n == 0 && frame.sp < frame.fp || frame.lr == 0 {
Austin Clementsfaa7a7e2015-05-20 16:30:49 -0400269 lrPtr = frame.sp
270 frame.lr = *(*uintptr)(unsafe.Pointer(lrPtr))
Russ Coxfa2af442014-09-02 15:12:53 -0400271 }
272 } else {
273 if frame.lr == 0 {
Michael Matloob432cb662015-11-11 12:39:30 -0500274 lrPtr = frame.fp - sys.RegSize
275 frame.lr = uintptr(*(*sys.Uintreg)(unsafe.Pointer(lrPtr)))
Russ Coxfa2af442014-09-02 15:12:53 -0400276 }
277 }
278 flr = findfunc(frame.lr)
Austin Clements0efc8b22017-02-20 22:37:07 -0500279 if !flr.valid() {
Russ Coxfa2af442014-09-02 15:12:53 -0400280 // This happens if you get a profiling interrupt at just the wrong time.
281 // In that context it is okay to stop early.
282 // But if callback is set, we're doing a garbage collection and must
283 // get everything, so crash loudly.
284 if callback != nil {
Keith Randall0bb8fc62014-12-28 23:16:32 -0800285 print("runtime: unexpected return pc for ", funcname(f), " called from ", hex(frame.lr), "\n")
Keith Randallb2a950b2014-12-27 20:58:00 -0800286 throw("unknown caller pc")
Russ Coxfa2af442014-09-02 15:12:53 -0400287 }
288 }
289 }
290
291 frame.varp = frame.fp
292 if !usesLR {
293 // On x86, call instruction pushes return PC before entering new function.
Michael Matloob432cb662015-11-11 12:39:30 -0500294 frame.varp -= sys.RegSize
Russ Coxfa2af442014-09-02 15:12:53 -0400295 }
296
Austin Clements3c0fee12015-01-14 11:09:50 -0500297 // If framepointer_enabled and there's a frame, then
298 // there's a saved bp here.
Austin Clementsfc5baec2015-02-03 09:18:15 -0500299 if framepointer_enabled && GOARCH == "amd64" && frame.varp > frame.sp {
Michael Matloob432cb662015-11-11 12:39:30 -0500300 frame.varp -= sys.RegSize
Austin Clements3c0fee12015-01-14 11:09:50 -0500301 }
302
Russ Coxfa2af442014-09-02 15:12:53 -0400303 // Derive size of arguments.
304 // Most functions have a fixed-size argument block,
305 // so we can use metadata about the function f.
306 // Not all, though: there are some variadic functions
307 // in package runtime and reflect, and for those we use call-specific
308 // metadata recorded by f's caller.
309 if callback != nil || printing {
Michael Matloob432cb662015-11-11 12:39:30 -0500310 frame.argp = frame.fp + sys.MinFrameSize
Austin Clementsd5bd7972016-10-16 18:23:39 -0400311 frame.arglen, frame.argmap = getArgInfo(&frame, f, callback != nil, nil)
Russ Coxfa2af442014-09-02 15:12:53 -0400312 }
313
Russ Coxfa2af442014-09-02 15:12:53 -0400314 // Determine frame's 'continuation PC', where it can continue.
315 // Normally this is the return address on the stack, but if sigpanic
316 // is immediately below this function on the stack, then the frame
317 // stopped executing due to a trap, and frame.pc is probably not
318 // a safe point for looking up liveness information. In this panicking case,
319 // the function either doesn't return at all (if it has no defers or if the
320 // defers do not recover) or it returns from one of the calls to
321 // deferproc a second time (if the corresponding deferred func recovers).
322 // It suffices to assume that the most recent deferproc is the one that
323 // returns; everything live at earlier deferprocs is still live at that one.
324 frame.continpc = frame.pc
325 if waspanic {
Keith Randall53c52262014-12-08 14:18:58 -0800326 if _defer != nil && _defer.sp == frame.sp {
Russ Coxfa2af442014-09-02 15:12:53 -0400327 frame.continpc = _defer.pc
328 } else {
329 frame.continpc = 0
330 }
331 }
332
Russ Coxf95beae2014-09-16 10:36:38 -0400333 // Unwind our local defer stack past this frame.
Keith Randall53c52262014-12-08 14:18:58 -0800334 for _defer != nil && (_defer.sp == frame.sp || _defer.sp == _NoArgs) {
Russ Coxfa2af442014-09-02 15:12:53 -0400335 _defer = _defer.link
336 }
337
Russ Coxfa2af442014-09-02 15:12:53 -0400338 if callback != nil {
339 if !callback((*stkframe)(noescape(unsafe.Pointer(&frame))), v) {
340 return n
341 }
342 }
David Lazaree972162017-03-06 14:48:36 -0500343
344 if pcbuf != nil {
345 if skip == 0 {
346 (*[1 << 20]uintptr)(unsafe.Pointer(pcbuf))[n] = frame.pc
347 } else {
348 // backup to CALL instruction to read inlining info (same logic as below)
349 tracepc := frame.pc
350 if (n > 0 || flags&_TraceTrap == 0) && frame.pc > f.entry && !waspanic {
351 tracepc--
352 }
353 inldata := funcdata(f, _FUNCDATA_InlTree)
354
355 // no inlining info, skip the physical frame
356 if inldata == nil {
357 skip--
358 goto skipped
359 }
360
361 ix := pcdatavalue(f, _PCDATA_InlTreeIndex, tracepc, &cache)
362 inltree := (*[1 << 20]inlinedCall)(inldata)
363 // skip the logical (inlined) frames
364 logicalSkipped := 0
365 for ix >= 0 && skip > 0 {
366 skip--
367 logicalSkipped++
368 ix = inltree[ix].parent
369 }
370
371 // skip the physical frame if there's more to skip
372 if skip > 0 {
373 skip--
374 goto skipped
375 }
376
377 // now we have a partially skipped frame
378 (*[1 << 20]uintptr)(unsafe.Pointer(pcbuf))[n] = frame.pc
379
380 // if there's room, pcbuf[1] is a skip PC that encodes the number of skipped frames in pcbuf[0]
381 if n+1 < max {
382 n++
383 skipPC := funcPC(skipPleaseUseCallersFrames) + uintptr(logicalSkipped)
384 (*[1 << 20]uintptr)(unsafe.Pointer(pcbuf))[n] = skipPC
385 }
386 }
387 }
388
Russ Coxfa2af442014-09-02 15:12:53 -0400389 if printing {
Austin Clements032678e2017-11-09 17:55:45 -0500390 // assume skip=0 for printing.
391 //
392 // Never elide wrappers if we haven't printed
393 // any frames. And don't elide wrappers that
394 // called panic rather than the wrapped
395 // function. Otherwise, leave them out.
396 name := funcname(f)
397 nextElideWrapper := elideWrapperCalling(name)
398 if (flags&_TraceRuntimeFrames) != 0 || showframe(f, gp, nprint == 0, elideWrapper && nprint != 0) {
Russ Coxfa2af442014-09-02 15:12:53 -0400399 // Print during crash.
400 // main(0x1, 0x2, 0x3)
401 // /home/rsc/go/src/runtime/x.go:23 +0xf
402 //
403 tracepc := frame.pc // back up to CALL instruction for funcline.
Russ Coxa22c11b2014-10-29 15:14:24 -0400404 if (n > 0 || flags&_TraceTrap == 0) && frame.pc > f.entry && !waspanic {
Russ Coxfa2af442014-09-02 15:12:53 -0400405 tracepc--
406 }
David Lazar781fd392017-02-17 16:08:36 -0500407 file, line := funcline(f, tracepc)
408 inldata := funcdata(f, _FUNCDATA_InlTree)
409 if inldata != nil {
410 inltree := (*[1 << 20]inlinedCall)(inldata)
411 ix := pcdatavalue(f, _PCDATA_InlTreeIndex, tracepc, nil)
412 for ix != -1 {
413 name := funcnameFromNameoff(f, inltree[ix].func_)
414 print(name, "(...)\n")
415 print("\t", file, ":", line, "\n")
416
417 file = funcfile(f, inltree[ix].file)
418 line = inltree[ix].line
419 ix = inltree[ix].parent
420 }
421 }
Austin Clements0c02bc02016-02-12 10:33:51 -0500422 if name == "runtime.gopanic" {
423 name = "panic"
424 }
425 print(name, "(")
Russ Coxfa2af442014-09-02 15:12:53 -0400426 argp := (*[100]uintptr)(unsafe.Pointer(frame.argp))
Michael Matloob432cb662015-11-11 12:39:30 -0500427 for i := uintptr(0); i < frame.arglen/sys.PtrSize; i++ {
Russ Coxfa2af442014-09-02 15:12:53 -0400428 if i >= 10 {
429 print(", ...")
430 break
431 }
432 if i != 0 {
433 print(", ")
434 }
435 print(hex(argp[i]))
436 }
437 print(")\n")
Russ Coxfa2af442014-09-02 15:12:53 -0400438 print("\t", file, ":", line)
439 if frame.pc > f.entry {
440 print(" +", hex(frame.pc-f.entry))
441 }
Russ Coxbf1de1b2015-10-30 11:03:02 -0400442 if g.m.throwing > 0 && gp == g.m.curg || level >= 2 {
Austin Clements27f88732017-06-09 11:58:53 -0400443 print(" fp=", hex(frame.fp), " sp=", hex(frame.sp), " pc=", hex(frame.pc))
Russ Coxfa2af442014-09-02 15:12:53 -0400444 }
445 print("\n")
446 nprint++
447 }
Austin Clements032678e2017-11-09 17:55:45 -0500448 elideWrapper = nextElideWrapper
Russ Coxfa2af442014-09-02 15:12:53 -0400449 }
450 n++
451
452 skipped:
Ian Lance Taylor5f9a8702016-04-27 14:18:29 -0700453 if f.entry == cgocallback_gofuncPC && len(cgoCtxt) > 0 {
454 ctxt := cgoCtxt[len(cgoCtxt)-1]
455 cgoCtxt = cgoCtxt[:len(cgoCtxt)-1]
456
457 // skip only applies to Go frames.
458 // callback != nil only used when we only care
459 // about Go frames.
460 if skip == 0 && callback == nil {
461 n = tracebackCgoContext(pcbuf, printing, ctxt, n, max)
462 }
463 }
464
Russ Cox97f83862014-09-03 13:02:48 -0400465 waspanic = f.entry == sigpanicPC
Russ Coxfa2af442014-09-02 15:12:53 -0400466
467 // Do not unwind past the bottom of the stack.
Austin Clements0efc8b22017-02-20 22:37:07 -0500468 if !flr.valid() {
Russ Coxfa2af442014-09-02 15:12:53 -0400469 break
470 }
471
472 // Unwind to next frame.
473 frame.fn = flr
474 frame.pc = frame.lr
475 frame.lr = 0
476 frame.sp = frame.fp
477 frame.fp = 0
Russ Coxf0d44db2014-09-12 07:29:19 -0400478 frame.argmap = nil
Russ Coxfa2af442014-09-02 15:12:53 -0400479
480 // On link register architectures, sighandler saves the LR on stack
481 // before faking a call to sigpanic.
482 if usesLR && waspanic {
483 x := *(*uintptr)(unsafe.Pointer(frame.sp))
Michael Matloob432cb662015-11-11 12:39:30 -0500484 frame.sp += sys.MinFrameSize
Aram Hăvărneanu846ee042015-03-08 14:20:20 +0100485 if GOARCH == "arm64" {
486 // arm64 needs 16-byte aligned SP, always
Michael Matloob432cb662015-11-11 12:39:30 -0500487 frame.sp += sys.PtrSize
Aram Hăvărneanu846ee042015-03-08 14:20:20 +0100488 }
Russ Coxfa2af442014-09-02 15:12:53 -0400489 f = findfunc(frame.pc)
490 frame.fn = f
Austin Clements0efc8b22017-02-20 22:37:07 -0500491 if !f.valid() {
Russ Coxfa2af442014-09-02 15:12:53 -0400492 frame.pc = x
Austin Clementsbeedb1e2015-08-12 23:43:43 -0400493 } else if funcspdelta(f, frame.pc, &cache) == 0 {
Russ Coxfa2af442014-09-02 15:12:53 -0400494 frame.lr = x
495 }
496 }
497 }
498
Matthew Dempsky3c8a89d2015-02-25 14:41:21 +0900499 if printing {
Russ Coxfa2af442014-09-02 15:12:53 -0400500 n = nprint
501 }
502
503 // If callback != nil, we're being called to gather stack information during
504 // garbage collection or stack growth. In that context, require that we used
505 // up the entire defer stack. If not, then there is a bug somewhere and the
506 // garbage collection or stack growth may not have seen the correct picture
507 // of the stack. Crash now instead of silently executing the garbage collection
508 // or stack copy incorrectly and setting up for a mysterious crash later.
509 //
510 // Note that panic != nil is okay here: there can be leftover panics,
511 // because the defers on the panic stack do not nest in frame order as
512 // they do on the defer stack. If you have:
513 //
514 // frame 1 defers d1
515 // frame 2 defers d2
516 // frame 3 defers d3
517 // frame 4 panics
518 // frame 4's panic starts running defers
519 // frame 5, running d3, defers d4
520 // frame 5 panics
521 // frame 5's panic starts running defers
522 // frame 6, running d4, garbage collects
523 // frame 6, running d2, garbage collects
524 //
525 // During the execution of d4, the panic stack is d4 -> d3, which
526 // is nested properly, and we'll treat frame 3 as resumable, because we
527 // can find d3. (And in fact frame 3 is resumable. If d4 recovers
528 // and frame 5 continues running, d3, d3 can recover and we'll
529 // resume execution in (returning from) frame 3.)
530 //
531 // During the execution of d2, however, the panic stack is d2 -> d3,
532 // which is inverted. The scan will match d2 to frame 2 but having
533 // d2 on the stack until then means it will not match d3 to frame 3.
534 // This is okay: if we're running d2, then all the defers after d2 have
535 // completed and their corresponding frames are dead. Not finding d3
536 // for frame 3 means we'll set frame 3's continpc == 0, which is correct
537 // (frame 3 is dead). At the end of the walk the panic stack can thus
538 // contain defers (d3 in this case) for dead frames. The inversion here
539 // always indicates a dead frame, and the effect of the inversion on the
540 // scan is to hide those dead frames, so the scan is still okay:
541 // what's left on the panic stack are exactly (and only) the dead frames.
542 //
543 // We require callback != nil here because only when callback != nil
544 // do we know that gentraceback is being called in a "must be correct"
545 // context as opposed to a "best effort" context. The tracebacks with
546 // callbacks only happen when everything is stopped nicely.
547 // At other times, such as when gathering a stack for a profiling signal
548 // or when printing a traceback during a crash, everything may not be
549 // stopped nicely, and the stack walk may not be able to complete.
550 // It's okay in those situations not to use up the entire defer stack:
551 // incomplete information then is still better than nothing.
552 if callback != nil && n < max && _defer != nil {
553 if _defer != nil {
Keith Randall53c52262014-12-08 14:18:58 -0800554 print("runtime: g", gp.goid, ": leftover defer sp=", hex(_defer.sp), " pc=", hex(_defer.pc), "\n")
Russ Coxfa2af442014-09-02 15:12:53 -0400555 }
Russ Coxfa2af442014-09-02 15:12:53 -0400556 for _defer = gp._defer; _defer != nil; _defer = _defer.link {
Keith Randall53c52262014-12-08 14:18:58 -0800557 print("\tdefer ", _defer, " sp=", hex(_defer.sp), " pc=", hex(_defer.pc), "\n")
Russ Coxfa2af442014-09-02 15:12:53 -0400558 }
Keith Randallb2a950b2014-12-27 20:58:00 -0800559 throw("traceback has leftover defers")
Russ Coxfa2af442014-09-02 15:12:53 -0400560 }
561
Russ Cox9c04d002015-08-26 11:39:10 -0400562 if callback != nil && n < max && frame.sp != gp.stktopsp {
563 print("runtime: g", gp.goid, ": frame.sp=", hex(frame.sp), " top=", hex(gp.stktopsp), "\n")
564 print("\tstack=[", hex(gp.stack.lo), "-", hex(gp.stack.hi), "] n=", n, " max=", max, "\n")
565 throw("traceback did not unwind completely")
566 }
567
Russ Coxfa2af442014-09-02 15:12:53 -0400568 return n
569}
570
Austin Clementsddd558e2016-12-16 11:57:25 -0500571// reflectMethodValue is a partial duplicate of reflect.makeFuncImpl
572// and reflect.methodValue.
Austin Clementsd5bd7972016-10-16 18:23:39 -0400573type reflectMethodValue struct {
574 fn uintptr
575 stack *bitvector // args bitmap
576}
577
578// getArgInfo returns the argument frame information for a call to f
579// with call frame frame.
580//
581// This is used for both actual calls with active stack frames and for
582// deferred calls that are not yet executing. If this is an actual
583// call, ctxt must be nil (getArgInfo will retrieve what it needs from
584// the active stack frame). If this is a deferred call, ctxt must be
585// the function object that was deferred.
Austin Clements0efc8b22017-02-20 22:37:07 -0500586func getArgInfo(frame *stkframe, f funcInfo, needArgMap bool, ctxt *funcval) (arglen uintptr, argmap *bitvector) {
Austin Clementsb43b3752015-11-17 17:14:32 -0500587 arglen = uintptr(f.args)
Russ Coxf95beae2014-09-16 10:36:38 -0400588 if needArgMap && f.args == _ArgsSizeUnknown {
589 // Extract argument bitmaps for reflect stubs from the calls they made to reflect.
Keith Randall0bb8fc62014-12-28 23:16:32 -0800590 switch funcname(f) {
Russ Coxf95beae2014-09-16 10:36:38 -0400591 case "reflect.makeFuncStub", "reflect.methodValueCall":
Austin Clementsd5bd7972016-10-16 18:23:39 -0400592 // These take a *reflect.methodValue as their
593 // context register.
594 var mv *reflectMethodValue
595 if ctxt != nil {
596 // This is not an actual call, but a
597 // deferred call. The function value
598 // is itself the *reflect.methodValue.
599 mv = (*reflectMethodValue)(unsafe.Pointer(ctxt))
600 } else {
601 // This is a real call that took the
602 // *reflect.methodValue as its context
603 // register and immediately saved it
604 // to 0(SP). Get the methodValue from
605 // 0(SP).
606 arg0 := frame.sp + sys.MinFrameSize
607 mv = *(**reflectMethodValue)(unsafe.Pointer(arg0))
608 }
609 if mv.fn != f.entry {
Keith Randall0bb8fc62014-12-28 23:16:32 -0800610 print("runtime: confused by ", funcname(f), "\n")
Keith Randallb2a950b2014-12-27 20:58:00 -0800611 throw("reflect mismatch")
Russ Coxf95beae2014-09-16 10:36:38 -0400612 }
Austin Clementsd5bd7972016-10-16 18:23:39 -0400613 bv := mv.stack
Austin Clementsb43b3752015-11-17 17:14:32 -0500614 arglen = uintptr(bv.n * sys.PtrSize)
615 argmap = bv
Russ Coxf95beae2014-09-16 10:36:38 -0400616 }
617 }
Austin Clementsb43b3752015-11-17 17:14:32 -0500618 return
Russ Coxf95beae2014-09-16 10:36:38 -0400619}
620
Ian Lance Taylor5f9a8702016-04-27 14:18:29 -0700621// tracebackCgoContext handles tracing back a cgo context value, from
622// the context argument to setCgoTraceback, for the gentraceback
623// function. It returns the new value of n.
624func tracebackCgoContext(pcbuf *uintptr, printing bool, ctxt uintptr, n, max int) int {
625 var cgoPCs [32]uintptr
626 cgoContextPCs(ctxt, cgoPCs[:])
627 var arg cgoSymbolizerArg
628 anySymbolized := false
629 for _, pc := range cgoPCs {
630 if pc == 0 || n >= max {
631 break
632 }
633 if pcbuf != nil {
634 (*[1 << 20]uintptr)(unsafe.Pointer(pcbuf))[n] = pc
635 }
636 if printing {
637 if cgoSymbolizer == nil {
638 print("non-Go function at pc=", hex(pc), "\n")
639 } else {
640 c := printOneCgoTraceback(pc, max-n, &arg)
641 n += c - 1 // +1 a few lines down
642 anySymbolized = true
643 }
644 }
645 n++
646 }
647 if anySymbolized {
648 arg.pc = 0
649 callCgoSymbolizer(&arg)
650 }
651 return n
652}
653
Russ Coxfa2af442014-09-02 15:12:53 -0400654func printcreatedby(gp *g) {
655 // Show what created goroutine, except main goroutine (goid 1).
656 pc := gp.gopc
657 f := findfunc(pc)
Austin Clements032678e2017-11-09 17:55:45 -0500658 if f.valid() && showframe(f, gp, false, false) && gp.goid != 1 {
Keith Randall0bb8fc62014-12-28 23:16:32 -0800659 print("created by ", funcname(f), "\n")
Russ Coxfa2af442014-09-02 15:12:53 -0400660 tracepc := pc // back up to CALL instruction for funcline.
661 if pc > f.entry {
Michael Matloob432cb662015-11-11 12:39:30 -0500662 tracepc -= sys.PCQuantum
Russ Coxfa2af442014-09-02 15:12:53 -0400663 }
Russ Cox656be312014-11-12 14:54:31 -0500664 file, line := funcline(f, tracepc)
Russ Coxfa2af442014-09-02 15:12:53 -0400665 print("\t", file, ":", line)
666 if pc > f.entry {
667 print(" +", hex(pc-f.entry))
668 }
669 print("\n")
670 }
671}
672
Matthew Dempsky3c8a89d2015-02-25 14:41:21 +0900673func traceback(pc, sp, lr uintptr, gp *g) {
Russ Coxa22c11b2014-10-29 15:14:24 -0400674 traceback1(pc, sp, lr, gp, 0)
675}
676
677// tracebacktrap is like traceback but expects that the PC and SP were obtained
678// from a trap, not from gp->sched or gp->syscallpc/gp->syscallsp or getcallerpc/getcallersp.
679// Because they are from a trap instead of from a saved pair,
680// the initial PC must not be rewound to the previous instruction.
681// (All the saved pairs record a PC that is a return address, so we
682// rewind it into the CALL instruction.)
Matthew Dempsky3c8a89d2015-02-25 14:41:21 +0900683func tracebacktrap(pc, sp, lr uintptr, gp *g) {
Russ Coxa22c11b2014-10-29 15:14:24 -0400684 traceback1(pc, sp, lr, gp, _TraceTrap)
685}
686
Matthew Dempsky3c8a89d2015-02-25 14:41:21 +0900687func traceback1(pc, sp, lr uintptr, gp *g, flags uint) {
Ian Lance Taylorea306ae2015-12-11 17:16:48 -0800688 // If the goroutine is in cgo, and we have a cgo traceback, print that.
689 if iscgo && gp.m != nil && gp.m.ncgo > 0 && gp.syscallsp != 0 && gp.m.cgoCallers != nil && gp.m.cgoCallers[0] != 0 {
690 // Lock cgoCallers so that a signal handler won't
691 // change it, copy the array, reset it, unlock it.
692 // We are locked to the thread and are not running
693 // concurrently with a signal handler.
694 // We just have to stop a signal handler from interrupting
695 // in the middle of our copy.
696 atomic.Store(&gp.m.cgoCallersUse, 1)
697 cgoCallers := *gp.m.cgoCallers
698 gp.m.cgoCallers[0] = 0
699 atomic.Store(&gp.m.cgoCallersUse, 0)
700
701 printCgoTraceback(&cgoCallers)
702 }
703
Russ Coxfa2af442014-09-02 15:12:53 -0400704 var n int
705 if readgstatus(gp)&^_Gscan == _Gsyscall {
Russ Coxa22c11b2014-10-29 15:14:24 -0400706 // Override registers if blocked in system call.
Russ Coxfa2af442014-09-02 15:12:53 -0400707 pc = gp.syscallpc
708 sp = gp.syscallsp
Russ Coxa22c11b2014-10-29 15:14:24 -0400709 flags &^= _TraceTrap
Russ Coxfa2af442014-09-02 15:12:53 -0400710 }
711 // Print traceback. By default, omits runtime frames.
712 // If that means we print nothing at all, repeat forcing all frames printed.
Russ Coxa22c11b2014-10-29 15:14:24 -0400713 n = gentraceback(pc, sp, lr, gp, 0, nil, _TracebackMaxFrames, nil, nil, flags)
714 if n == 0 && (flags&_TraceRuntimeFrames) == 0 {
715 n = gentraceback(pc, sp, lr, gp, 0, nil, _TracebackMaxFrames, nil, nil, flags|_TraceRuntimeFrames)
Russ Coxfa2af442014-09-02 15:12:53 -0400716 }
717 if n == _TracebackMaxFrames {
718 print("...additional frames elided...\n")
719 }
720 printcreatedby(gp)
721}
722
Matthew Dempsky3c8a89d2015-02-25 14:41:21 +0900723func callers(skip int, pcbuf []uintptr) int {
Russ Coxfa2af442014-09-02 15:12:53 -0400724 sp := getcallersp(unsafe.Pointer(&skip))
Austin Clements229aaac2017-09-22 15:16:26 -0400725 pc := getcallerpc()
Austin Clements719efc72015-05-20 11:50:48 -0400726 gp := getg()
Russ Cox39bcbb32014-11-05 23:01:48 -0500727 var n int
Russ Cox656be312014-11-12 14:54:31 -0500728 systemstack(func() {
Austin Clements719efc72015-05-20 11:50:48 -0400729 n = gentraceback(pc, sp, 0, gp, skip, &pcbuf[0], len(pcbuf), nil, nil, 0)
Russ Cox39bcbb32014-11-05 23:01:48 -0500730 })
731 return n
Russ Coxfa2af442014-09-02 15:12:53 -0400732}
733
Matthew Dempsky3c8a89d2015-02-25 14:41:21 +0900734func gcallers(gp *g, skip int, pcbuf []uintptr) int {
735 return gentraceback(^uintptr(0), ^uintptr(0), 0, gp, skip, &pcbuf[0], len(pcbuf), nil, nil, 0)
Russ Coxfa2af442014-09-02 15:12:53 -0400736}
Russ Cox7ba41e92014-09-03 11:11:16 -0400737
Austin Clements032678e2017-11-09 17:55:45 -0500738func showframe(f funcInfo, gp *g, firstFrame, elideWrapper bool) bool {
Russ Cox97f83862014-09-03 13:02:48 -0400739 g := getg()
Russ Cox181e26b2015-04-17 00:21:30 -0400740 if g.m.throwing > 0 && gp != nil && (gp == g.m.curg || gp == g.m.caughtsig.ptr()) {
Russ Cox97f83862014-09-03 13:02:48 -0400741 return true
742 }
Russ Coxbf1de1b2015-10-30 11:03:02 -0400743 level, _, _ := gotraceback()
Austin Clementse9720952017-06-12 11:12:12 -0400744 if level > 1 {
745 // Show all frames.
746 return true
747 }
748
Austin Clements032678e2017-11-09 17:55:45 -0500749 if !f.valid() {
750 return false
751 }
752
753 if elideWrapper {
754 file, _ := funcline(f, f.entry)
755 if file == "<autogenerated>" {
756 return false
757 }
758 }
759
Keith Randall0bb8fc62014-12-28 23:16:32 -0800760 name := funcname(f)
Russ Cox97f83862014-09-03 13:02:48 -0400761
Russ Coxf9feaff2016-11-12 23:01:37 -0500762 // Special case: always show runtime.gopanic frame
763 // in the middle of a stack trace, so that we can
764 // see the boundary between ordinary code and
765 // panic-induced deferred code.
Russ Cox97f83862014-09-03 13:02:48 -0400766 // See golang.org/issue/5832.
Russ Coxf9feaff2016-11-12 23:01:37 -0500767 if name == "runtime.gopanic" && !firstFrame {
Russ Cox97f83862014-09-03 13:02:48 -0400768 return true
769 }
770
Austin Clements032678e2017-11-09 17:55:45 -0500771 return contains(name, ".") && (!hasprefix(name, "runtime.") || isExportedRuntime(name))
Russ Cox54245cb2014-09-18 20:35:36 -0400772}
773
774// isExportedRuntime reports whether name is an exported runtime function.
775// It is only for runtime functions, so ASCII A-Z is fine.
776func isExportedRuntime(name string) bool {
777 const n = len("runtime.")
778 return len(name) > n && name[:n] == "runtime." && 'A' <= name[n] && name[n] <= 'Z'
Russ Cox97f83862014-09-03 13:02:48 -0400779}
780
Austin Clements032678e2017-11-09 17:55:45 -0500781// elideWrapperCalling returns whether a wrapper function that called
782// function "name" should be elided from stack traces.
783func elideWrapperCalling(name string) bool {
784 // If the wrapper called a panic function instead of the
785 // wrapped function, we want to include it in stacks.
786 return !(name == "runtime.gopanic" || name == "runtime.sigpanic" || name == "runtime.panicwrap")
787}
788
Russ Cox7ba41e92014-09-03 11:11:16 -0400789var gStatusStrings = [...]string{
790 _Gidle: "idle",
791 _Grunnable: "runnable",
792 _Grunning: "running",
793 _Gsyscall: "syscall",
794 _Gwaiting: "waiting",
795 _Gdead: "dead",
Russ Cox7ba41e92014-09-03 11:11:16 -0400796 _Gcopystack: "copystack",
797}
798
Russ Cox7ba41e92014-09-03 11:11:16 -0400799func goroutineheader(gp *g) {
800 gpstatus := readgstatus(gp)
801
Austin Clementsc7c7c702015-12-21 11:26:33 -0800802 isScan := gpstatus&_Gscan != 0
803 gpstatus &^= _Gscan // drop the scan bit
804
Russ Cox7ba41e92014-09-03 11:11:16 -0400805 // Basic string status
806 var status string
807 if 0 <= gpstatus && gpstatus < uint32(len(gStatusStrings)) {
808 status = gStatusStrings[gpstatus]
Russ Cox7ba41e92014-09-03 11:11:16 -0400809 } else {
810 status = "???"
811 }
812
813 // Override.
Austin Clementsc7c7c702015-12-21 11:26:33 -0800814 if gpstatus == _Gwaiting && gp.waitreason != "" {
Russ Cox7ba41e92014-09-03 11:11:16 -0400815 status = gp.waitreason
816 }
817
818 // approx time the G is blocked, in minutes
819 var waitfor int64
Russ Cox7ba41e92014-09-03 11:11:16 -0400820 if (gpstatus == _Gwaiting || gpstatus == _Gsyscall) && gp.waitsince != 0 {
821 waitfor = (nanotime() - gp.waitsince) / 60e9
822 }
823 print("goroutine ", gp.goid, " [", status)
Austin Clementsc7c7c702015-12-21 11:26:33 -0800824 if isScan {
825 print(" (scan)")
826 }
Russ Cox7ba41e92014-09-03 11:11:16 -0400827 if waitfor >= 1 {
828 print(", ", waitfor, " minutes")
829 }
Ian Lance Taylor165c15a2017-09-13 10:14:02 -0700830 if gp.lockedm != 0 {
Russ Cox7ba41e92014-09-03 11:11:16 -0400831 print(", locked to thread")
832 }
833 print("]:\n")
834}
835
836func tracebackothers(me *g) {
Russ Coxbf1de1b2015-10-30 11:03:02 -0400837 level, _, _ := gotraceback()
Russ Cox7ba41e92014-09-03 11:11:16 -0400838
839 // Show the current goroutine first, if we haven't already.
840 g := getg()
841 gp := g.m.curg
842 if gp != nil && gp != me {
843 print("\n")
844 goroutineheader(gp)
845 traceback(^uintptr(0), ^uintptr(0), 0, gp)
846 }
847
848 lock(&allglock)
849 for _, gp := range allgs {
Dmitry Vyukov59495e82015-02-07 15:31:18 +0300850 if gp == me || gp == g.m.curg || readgstatus(gp) == _Gdead || isSystemGoroutine(gp) && level < 2 {
Russ Cox7ba41e92014-09-03 11:11:16 -0400851 continue
852 }
853 print("\n")
854 goroutineheader(gp)
Keith Randall4b78c952015-04-28 14:53:19 -0700855 // Note: gp.m == g.m occurs when tracebackothers is
856 // called from a signal handler initiated during a
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +0000857 // systemstack call. The original G is still in the
Keith Randall4b78c952015-04-28 14:53:19 -0700858 // running state, and we want to print its stack.
859 if gp.m != g.m && readgstatus(gp)&^_Gscan == _Grunning {
Russ Cox7ba41e92014-09-03 11:11:16 -0400860 print("\tgoroutine running on other thread; stack unavailable\n")
861 printcreatedby(gp)
862 } else {
863 traceback(^uintptr(0), ^uintptr(0), 0, gp)
864 }
865 }
866 unlock(&allglock)
867}
868
Russ Cox7ba41e92014-09-03 11:11:16 -0400869// Does f mark the top of a goroutine stack?
Austin Clements0efc8b22017-02-20 22:37:07 -0500870func topofstack(f funcInfo) bool {
Russ Cox7ba41e92014-09-03 11:11:16 -0400871 pc := f.entry
872 return pc == goexitPC ||
873 pc == mstartPC ||
874 pc == mcallPC ||
Russ Cox7ba41e92014-09-03 11:11:16 -0400875 pc == morestackPC ||
Russ Cox7ba41e92014-09-03 11:11:16 -0400876 pc == rt0_goPC ||
877 externalthreadhandlerp != 0 && pc == externalthreadhandlerp
878}
Dmitry Vyukov59495e82015-02-07 15:31:18 +0300879
Josh Bleecher Snyder2adc4e82015-02-17 15:44:42 -0800880// isSystemGoroutine reports whether the goroutine g must be omitted in
Dmitry Vyukov59495e82015-02-07 15:31:18 +0300881// stack dumps and deadlock detector.
882func isSystemGoroutine(gp *g) bool {
883 pc := gp.startpc
884 return pc == runfinqPC && !fingRunning ||
Dmitry Vyukov59495e82015-02-07 15:31:18 +0300885 pc == bgsweepPC ||
886 pc == forcegchelperPC ||
Austin Clements8d03acc2015-03-23 21:07:33 -0400887 pc == timerprocPC ||
888 pc == gcBgMarkWorkerPC
Dmitry Vyukov59495e82015-02-07 15:31:18 +0300889}
Ian Lance Taylorea306ae2015-12-11 17:16:48 -0800890
891// SetCgoTraceback records three C functions to use to gather
892// traceback information from C code and to convert that traceback
893// information into symbolic information. These are used when printing
894// stack traces for a program that uses cgo.
895//
896// The traceback and context functions may be called from a signal
897// handler, and must therefore use only async-signal safe functions.
898// The symbolizer function may be called while the program is
899// crashing, and so must be cautious about using memory. None of the
900// functions may call back into Go.
901//
902// The context function will be called with a single argument, a
903// pointer to a struct:
904//
Brad Fitzpatrick06d639e2016-04-27 16:56:13 -0500905// struct {
906// Context uintptr
907// }
Ian Lance Taylorea306ae2015-12-11 17:16:48 -0800908//
909// In C syntax, this struct will be
910//
Brad Fitzpatrick06d639e2016-04-27 16:56:13 -0500911// struct {
912// uintptr_t Context;
913// };
Ian Lance Taylorea306ae2015-12-11 17:16:48 -0800914//
915// If the Context field is 0, the context function is being called to
Ian Lance Taylor5f9a8702016-04-27 14:18:29 -0700916// record the current traceback context. It should record in the
917// Context field whatever information is needed about the current
918// point of execution to later produce a stack trace, probably the
919// stack pointer and PC. In this case the context function will be
920// called from C code.
Ian Lance Taylorea306ae2015-12-11 17:16:48 -0800921//
922// If the Context field is not 0, then it is a value returned by a
923// previous call to the context function. This case is called when the
924// context is no longer needed; that is, when the Go code is returning
Dmitry Vyukovba221722016-06-02 07:43:21 +0200925// to its C code caller. This permits the context function to release
926// any associated resources.
Ian Lance Taylorea306ae2015-12-11 17:16:48 -0800927//
928// While it would be correct for the context function to record a
929// complete a stack trace whenever it is called, and simply copy that
930// out in the traceback function, in a typical program the context
931// function will be called many times without ever recording a
932// traceback for that context. Recording a complete stack trace in a
933// call to the context function is likely to be inefficient.
934//
935// The traceback function will be called with a single argument, a
936// pointer to a struct:
937//
Brad Fitzpatrick06d639e2016-04-27 16:56:13 -0500938// struct {
Ian Lance Taylor3d037cf2016-05-27 10:05:52 -0700939// Context uintptr
940// SigContext uintptr
941// Buf *uintptr
942// Max uintptr
Brad Fitzpatrick06d639e2016-04-27 16:56:13 -0500943// }
Ian Lance Taylorea306ae2015-12-11 17:16:48 -0800944//
945// In C syntax, this struct will be
946//
Brad Fitzpatrick06d639e2016-04-27 16:56:13 -0500947// struct {
948// uintptr_t Context;
Ian Lance Taylor3d037cf2016-05-27 10:05:52 -0700949// uintptr_t SigContext;
Brad Fitzpatrick06d639e2016-04-27 16:56:13 -0500950// uintptr_t* Buf;
951// uintptr_t Max;
952// };
Ian Lance Taylorea306ae2015-12-11 17:16:48 -0800953//
954// The Context field will be zero to gather a traceback from the
955// current program execution point. In this case, the traceback
956// function will be called from C code.
957//
958// Otherwise Context will be a value previously returned by a call to
959// the context function. The traceback function should gather a stack
960// trace from that saved point in the program execution. The traceback
961// function may be called from an execution thread other than the one
962// that recorded the context, but only when the context is known to be
963// valid and unchanging. The traceback function may also be called
964// deeper in the call stack on the same thread that recorded the
965// context. The traceback function may be called multiple times with
966// the same Context value; it will usually be appropriate to cache the
967// result, if possible, the first time this is called for a specific
968// context value.
969//
Ian Lance Taylor3d037cf2016-05-27 10:05:52 -0700970// If the traceback function is called from a signal handler on a Unix
971// system, SigContext will be the signal context argument passed to
972// the signal handler (a C ucontext_t* cast to uintptr_t). This may be
973// used to start tracing at the point where the signal occurred. If
974// the traceback function is not called from a signal handler,
975// SigContext will be zero.
976//
Ian Lance Taylorea306ae2015-12-11 17:16:48 -0800977// Buf is where the traceback information should be stored. It should
978// be PC values, such that Buf[0] is the PC of the caller, Buf[1] is
979// the PC of that function's caller, and so on. Max is the maximum
980// number of entries to store. The function should store a zero to
981// indicate the top of the stack, or that the caller is on a different
982// stack, presumably a Go stack.
983//
984// Unlike runtime.Callers, the PC values returned should, when passed
985// to the symbolizer function, return the file/line of the call
986// instruction. No additional subtraction is required or appropriate.
987//
988// The symbolizer function will be called with a single argument, a
989// pointer to a struct:
990//
Brad Fitzpatrick06d639e2016-04-27 16:56:13 -0500991// struct {
992// PC uintptr // program counter to fetch information for
993// File *byte // file name (NUL terminated)
994// Lineno uintptr // line number
995// Func *byte // function name (NUL terminated)
996// Entry uintptr // function entry point
997// More uintptr // set non-zero if more info for this PC
998// Data uintptr // unused by runtime, available for function
999// }
Ian Lance Taylorea306ae2015-12-11 17:16:48 -08001000//
1001// In C syntax, this struct will be
1002//
Brad Fitzpatrick06d639e2016-04-27 16:56:13 -05001003// struct {
1004// uintptr_t PC;
1005// char* File;
1006// uintptr_t Lineno;
1007// char* Func;
1008// uintptr_t Entry;
1009// uintptr_t More;
1010// uintptr_t Data;
1011// };
Ian Lance Taylorea306ae2015-12-11 17:16:48 -08001012//
1013// The PC field will be a value returned by a call to the traceback
1014// function.
1015//
1016// The first time the function is called for a particular traceback,
1017// all the fields except PC will be 0. The function should fill in the
1018// other fields if possible, setting them to 0/nil if the information
1019// is not available. The Data field may be used to store any useful
1020// information across calls. The More field should be set to non-zero
1021// if there is more information for this PC, zero otherwise. If More
1022// is set non-zero, the function will be called again with the same
1023// PC, and may return different information (this is intended for use
1024// with inlined functions). If More is zero, the function will be
1025// called with the next PC value in the traceback. When the traceback
1026// is complete, the function will be called once more with PC set to
1027// zero; this may be used to free any information. Each call will
1028// leave the fields of the struct set to the same values they had upon
1029// return, except for the PC field when the More field is zero. The
1030// function must not keep a copy of the struct pointer between calls.
1031//
1032// When calling SetCgoTraceback, the version argument is the version
1033// number of the structs that the functions expect to receive.
1034// Currently this must be zero.
1035//
1036// The symbolizer function may be nil, in which case the results of
1037// the traceback function will be displayed as numbers. If the
1038// traceback function is nil, the symbolizer function will never be
1039// called. The context function may be nil, in which case the
1040// traceback function will only be called with the context field set
1041// to zero. If the context function is nil, then calls from Go to C
1042// to Go will not show a traceback for the C portion of the call stack.
Ian Lance Taylor03abde42016-06-02 12:01:03 -07001043//
1044// SetCgoTraceback should be called only once, ideally from an init function.
Ian Lance Taylorea306ae2015-12-11 17:16:48 -08001045func SetCgoTraceback(version int, traceback, context, symbolizer unsafe.Pointer) {
1046 if version != 0 {
1047 panic("unsupported version")
1048 }
Ian Lance Taylor5f9a8702016-04-27 14:18:29 -07001049
Ian Lance Taylor03abde42016-06-02 12:01:03 -07001050 if cgoTraceback != nil && cgoTraceback != traceback ||
1051 cgoContext != nil && cgoContext != context ||
1052 cgoSymbolizer != nil && cgoSymbolizer != symbolizer {
1053 panic("call SetCgoTraceback only once")
1054 }
1055
Ian Lance Taylorea306ae2015-12-11 17:16:48 -08001056 cgoTraceback = traceback
Ian Lance Taylor03abde42016-06-02 12:01:03 -07001057 cgoContext = context
Ian Lance Taylorea306ae2015-12-11 17:16:48 -08001058 cgoSymbolizer = symbolizer
Ian Lance Taylor5f9a8702016-04-27 14:18:29 -07001059
1060 // The context function is called when a C function calls a Go
1061 // function. As such it is only called by C code in runtime/cgo.
1062 if _cgo_set_context_function != nil {
1063 cgocall(_cgo_set_context_function, context)
1064 }
Ian Lance Taylorea306ae2015-12-11 17:16:48 -08001065}
1066
1067var cgoTraceback unsafe.Pointer
Ian Lance Taylor03abde42016-06-02 12:01:03 -07001068var cgoContext unsafe.Pointer
Ian Lance Taylorea306ae2015-12-11 17:16:48 -08001069var cgoSymbolizer unsafe.Pointer
1070
1071// cgoTracebackArg is the type passed to cgoTraceback.
1072type cgoTracebackArg struct {
Ian Lance Taylor3d037cf2016-05-27 10:05:52 -07001073 context uintptr
1074 sigContext uintptr
1075 buf *uintptr
1076 max uintptr
Ian Lance Taylorea306ae2015-12-11 17:16:48 -08001077}
1078
Ian Lance Taylor5f9a8702016-04-27 14:18:29 -07001079// cgoContextArg is the type passed to the context function.
Ian Lance Taylorea306ae2015-12-11 17:16:48 -08001080type cgoContextArg struct {
1081 context uintptr
1082}
1083
1084// cgoSymbolizerArg is the type passed to cgoSymbolizer.
1085type cgoSymbolizerArg struct {
1086 pc uintptr
1087 file *byte
1088 lineno uintptr
1089 funcName *byte
1090 entry uintptr
1091 more uintptr
1092 data uintptr
1093}
1094
1095// cgoTraceback prints a traceback of callers.
1096func printCgoTraceback(callers *cgoCallers) {
1097 if cgoSymbolizer == nil {
1098 for _, c := range callers {
1099 if c == 0 {
1100 break
1101 }
1102 print("non-Go function at pc=", hex(c), "\n")
1103 }
1104 return
1105 }
1106
Ian Lance Taylorea306ae2015-12-11 17:16:48 -08001107 var arg cgoSymbolizerArg
1108 for _, c := range callers {
1109 if c == 0 {
1110 break
1111 }
Ian Lance Taylor5f9a8702016-04-27 14:18:29 -07001112 printOneCgoTraceback(c, 0x7fffffff, &arg)
Ian Lance Taylorea306ae2015-12-11 17:16:48 -08001113 }
1114 arg.pc = 0
Ian Lance Taylor5f9a8702016-04-27 14:18:29 -07001115 callCgoSymbolizer(&arg)
1116}
1117
1118// printOneCgoTraceback prints the traceback of a single cgo caller.
1119// This can print more than one line because of inlining.
1120// Returns the number of frames printed.
1121func printOneCgoTraceback(pc uintptr, max int, arg *cgoSymbolizerArg) int {
1122 c := 0
1123 arg.pc = pc
1124 for {
1125 if c > max {
1126 break
1127 }
1128 callCgoSymbolizer(arg)
1129 if arg.funcName != nil {
1130 // Note that we don't print any argument
1131 // information here, not even parentheses.
1132 // The symbolizer must add that if appropriate.
1133 println(gostringnocopy(arg.funcName))
1134 } else {
1135 println("non-Go function")
1136 }
1137 print("\t")
1138 if arg.file != nil {
1139 print(gostringnocopy(arg.file), ":", arg.lineno, " ")
1140 }
Ian Lance Taylorc08436d2016-05-18 15:20:56 -07001141 print("pc=", hex(pc), "\n")
Ian Lance Taylor5f9a8702016-04-27 14:18:29 -07001142 c++
1143 if arg.more == 0 {
1144 break
1145 }
1146 }
1147 return c
1148}
1149
1150// callCgoSymbolizer calls the cgoSymbolizer function.
1151func callCgoSymbolizer(arg *cgoSymbolizerArg) {
1152 call := cgocall
1153 if panicking > 0 || getg().m.curg != getg() {
1154 // We do not want to call into the scheduler when panicking
1155 // or when on the system stack.
1156 call = asmcgocall
1157 }
Ian Lance Taylord00890b2016-07-10 18:44:09 -07001158 if msanenabled {
1159 msanwrite(unsafe.Pointer(arg), unsafe.Sizeof(cgoSymbolizerArg{}))
1160 }
Ian Lance Taylor5f9a8702016-04-27 14:18:29 -07001161 call(cgoSymbolizer, noescape(unsafe.Pointer(arg)))
1162}
1163
1164// cgoContextPCs gets the PC values from a cgo traceback.
1165func cgoContextPCs(ctxt uintptr, buf []uintptr) {
1166 if cgoTraceback == nil {
1167 return
1168 }
1169 call := cgocall
1170 if panicking > 0 || getg().m.curg != getg() {
1171 // We do not want to call into the scheduler when panicking
1172 // or when on the system stack.
1173 call = asmcgocall
1174 }
1175 arg := cgoTracebackArg{
1176 context: ctxt,
1177 buf: (*uintptr)(noescape(unsafe.Pointer(&buf[0]))),
1178 max: uintptr(len(buf)),
1179 }
Ian Lance Taylord00890b2016-07-10 18:44:09 -07001180 if msanenabled {
1181 msanwrite(unsafe.Pointer(&arg), unsafe.Sizeof(arg))
1182 }
Ian Lance Taylor5f9a8702016-04-27 14:18:29 -07001183 call(cgoTraceback, noescape(unsafe.Pointer(&arg)))
Ian Lance Taylorea306ae2015-12-11 17:16:48 -08001184}