Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 1 | // 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 | |
| 5 | package runtime |
| 6 | |
Michael Matloob | 432cb66 | 2015-11-11 12:39:30 -0500 | [diff] [blame] | 7 | import ( |
Ian Lance Taylor | ea306ae | 2015-12-11 17:16:48 -0800 | [diff] [blame] | 8 | "runtime/internal/atomic" |
Michael Matloob | 432cb66 | 2015-11-11 12:39:30 -0500 | [diff] [blame] | 9 | "runtime/internal/sys" |
| 10 | "unsafe" |
| 11 | ) |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 12 | |
| 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-Doyle | a485581 | 2015-10-08 21:52:03 +1300 | [diff] [blame] | 33 | // 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 Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 35 | |
Michael Matloob | 432cb66 | 2015-11-11 12:39:30 -0500 | [diff] [blame] | 36 | const usesLR = sys.MinFrameSize > 0 |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 37 | |
Russ Cox | 97f8386 | 2014-09-03 13:02:48 -0400 | [diff] [blame] | 38 | var ( |
Keith Randall | 70b2da9 | 2014-09-29 21:21:36 -0700 | [diff] [blame] | 39 | // initialized in tracebackinit |
Russ Cox | 656be31 | 2014-11-12 14:54:31 -0500 | [diff] [blame] | 40 | goexitPC uintptr |
| 41 | jmpdeferPC uintptr |
| 42 | mcallPC uintptr |
| 43 | morestackPC uintptr |
| 44 | mstartPC uintptr |
Russ Cox | 656be31 | 2014-11-12 14:54:31 -0500 | [diff] [blame] | 45 | rt0_goPC uintptr |
Austin Clements | 5c2be42 | 2018-01-30 16:01:33 -0500 | [diff] [blame] | 46 | asmcgocallPC uintptr |
Russ Cox | 656be31 | 2014-11-12 14:54:31 -0500 | [diff] [blame] | 47 | sigpanicPC uintptr |
Dmitry Vyukov | 59495e8 | 2015-02-07 15:31:18 +0300 | [diff] [blame] | 48 | runfinqPC uintptr |
Dmitry Vyukov | 59495e8 | 2015-02-07 15:31:18 +0300 | [diff] [blame] | 49 | bgsweepPC uintptr |
| 50 | forcegchelperPC uintptr |
| 51 | timerprocPC uintptr |
Austin Clements | 8d03acc | 2015-03-23 21:07:33 -0400 | [diff] [blame] | 52 | gcBgMarkWorkerPC uintptr |
Russ Cox | 656be31 | 2014-11-12 14:54:31 -0500 | [diff] [blame] | 53 | systemstack_switchPC uintptr |
Daniel Morsing | db6f88a | 2015-04-30 15:32:54 +0100 | [diff] [blame] | 54 | systemstackPC uintptr |
Austin Clements | e2bb03f | 2015-08-26 12:16:51 -0400 | [diff] [blame] | 55 | cgocallback_gofuncPC uintptr |
David Lazar | 7bf0adc | 2017-03-07 21:14:12 -0500 | [diff] [blame] | 56 | skipPC uintptr |
Daniel Morsing | db6f88a | 2015-04-30 15:32:54 +0100 | [diff] [blame] | 57 | |
| 58 | gogoPC uintptr |
Russ Cox | 97f8386 | 2014-09-03 13:02:48 -0400 | [diff] [blame] | 59 | |
| 60 | externalthreadhandlerp uintptr // initialized elsewhere |
| 61 | ) |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 62 | |
Keith Randall | 70b2da9 | 2014-09-29 21:21:36 -0700 | [diff] [blame] | 63 | func 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 Randall | 70b2da9 | 2014-09-29 21:21:36 -0700 | [diff] [blame] | 68 | goexitPC = funcPC(goexit) |
| 69 | jmpdeferPC = funcPC(jmpdefer) |
| 70 | mcallPC = funcPC(mcall) |
| 71 | morestackPC = funcPC(morestack) |
| 72 | mstartPC = funcPC(mstart) |
Keith Randall | 70b2da9 | 2014-09-29 21:21:36 -0700 | [diff] [blame] | 73 | rt0_goPC = funcPC(rt0_go) |
Austin Clements | 5c2be42 | 2018-01-30 16:01:33 -0500 | [diff] [blame] | 74 | asmcgocallPC = funcPC(asmcgocall) |
Keith Randall | 70b2da9 | 2014-09-29 21:21:36 -0700 | [diff] [blame] | 75 | sigpanicPC = funcPC(sigpanic) |
Dmitry Vyukov | 59495e8 | 2015-02-07 15:31:18 +0300 | [diff] [blame] | 76 | runfinqPC = funcPC(runfinq) |
Dmitry Vyukov | 59495e8 | 2015-02-07 15:31:18 +0300 | [diff] [blame] | 77 | bgsweepPC = funcPC(bgsweep) |
| 78 | forcegchelperPC = funcPC(forcegchelper) |
| 79 | timerprocPC = funcPC(timerproc) |
Austin Clements | 8d03acc | 2015-03-23 21:07:33 -0400 | [diff] [blame] | 80 | gcBgMarkWorkerPC = funcPC(gcBgMarkWorker) |
Russ Cox | 656be31 | 2014-11-12 14:54:31 -0500 | [diff] [blame] | 81 | systemstack_switchPC = funcPC(systemstack_switch) |
Daniel Morsing | db6f88a | 2015-04-30 15:32:54 +0100 | [diff] [blame] | 82 | systemstackPC = funcPC(systemstack) |
Austin Clements | e2bb03f | 2015-08-26 12:16:51 -0400 | [diff] [blame] | 83 | cgocallback_gofuncPC = funcPC(cgocallback_gofunc) |
David Lazar | 7bf0adc | 2017-03-07 21:14:12 -0500 | [diff] [blame] | 84 | skipPC = funcPC(skipPleaseUseCallersFrames) |
Daniel Morsing | db6f88a | 2015-04-30 15:32:54 +0100 | [diff] [blame] | 85 | |
| 86 | // used by sigprof handler |
| 87 | gogoPC = funcPC(gogo) |
Keith Randall | 70b2da9 | 2014-09-29 21:21:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Russ Cox | f95beae | 2014-09-16 10:36:38 -0400 | [diff] [blame] | 90 | // Traceback over the deferred function calls. |
| 91 | // Report them like calls that have been invoked but not started executing yet. |
| 92 | func 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 Clements | 0efc8b2 | 2017-02-20 22:37:07 -0500 | [diff] [blame] | 99 | frame.fn = funcInfo{} |
Russ Cox | f95beae | 2014-09-16 10:36:38 -0400 | [diff] [blame] | 100 | frame.argp = 0 |
| 101 | frame.arglen = 0 |
| 102 | frame.argmap = nil |
| 103 | } else { |
Matthew Dempsky | a03bdc3 | 2016-02-29 15:01:00 -0800 | [diff] [blame] | 104 | frame.pc = fn.fn |
Russ Cox | f95beae | 2014-09-16 10:36:38 -0400 | [diff] [blame] | 105 | f := findfunc(frame.pc) |
Austin Clements | 0efc8b2 | 2017-02-20 22:37:07 -0500 | [diff] [blame] | 106 | if !f.valid() { |
Russ Cox | f95beae | 2014-09-16 10:36:38 -0400 | [diff] [blame] | 107 | print("runtime: unknown pc in defer ", hex(frame.pc), "\n") |
Keith Randall | b2a950b | 2014-12-27 20:58:00 -0800 | [diff] [blame] | 108 | throw("unknown pc") |
Russ Cox | f95beae | 2014-09-16 10:36:38 -0400 | [diff] [blame] | 109 | } |
| 110 | frame.fn = f |
| 111 | frame.argp = uintptr(deferArgs(d)) |
Austin Clements | d5bd797 | 2016-10-16 18:23:39 -0400 | [diff] [blame] | 112 | frame.arglen, frame.argmap = getArgInfo(&frame, f, true, fn) |
Russ Cox | f95beae | 2014-09-16 10:36:38 -0400 | [diff] [blame] | 113 | } |
| 114 | frame.continpc = frame.pc |
| 115 | if !callback((*stkframe)(noescape(unsafe.Pointer(&frame))), v) { |
| 116 | return |
| 117 | } |
| 118 | } |
| 119 | } |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 120 | |
David Lazar | ee97216 | 2017-03-06 14:48:36 -0500 | [diff] [blame] | 121 | const sizeofSkipFunction = 256 |
| 122 | |
| 123 | // This function is defined in asm.s to be sizeofSkipFunction bytes long. |
| 124 | func skipPleaseUseCallersFrames() |
| 125 | |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 126 | // Generic traceback. Handles runtime stack prints (pcbuf == nil), |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 127 | // 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 Lazar | ee97216 | 2017-03-06 14:48:36 -0500 | [diff] [blame] | 130 | // |
| 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 Dempsky | 3c8a89d | 2015-02-25 14:41:21 +0900 | [diff] [blame] | 136 | func 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 Lazar | ee97216 | 2017-03-06 14:48:36 -0500 | [diff] [blame] | 137 | if skip > 0 && callback != nil { |
| 138 | throw("gentraceback callback cannot be used with non-zero skip") |
| 139 | } |
Keith Randall | 70b2da9 | 2014-09-29 21:21:36 -0700 | [diff] [blame] | 140 | if goexitPC == 0 { |
Keith Randall | b2a950b | 2014-12-27 20:58:00 -0800 | [diff] [blame] | 141 | throw("gentraceback before goexitPC initialization") |
Keith Randall | 70b2da9 | 2014-09-29 21:21:36 -0700 | [diff] [blame] | 142 | } |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 143 | g := getg() |
Russ Cox | 39bcbb3 | 2014-11-05 23:01:48 -0500 | [diff] [blame] | 144 | 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 Randall | b2a950b | 2014-12-27 20:58:00 -0800 | [diff] [blame] | 158 | throw("gentraceback cannot trace user goroutine on its own stack") |
Russ Cox | 39bcbb3 | 2014-11-05 23:01:48 -0500 | [diff] [blame] | 159 | } |
Russ Cox | bf1de1b | 2015-10-30 11:03:02 -0400 | [diff] [blame] | 160 | level, _, _ := gotraceback() |
Austin Clements | faa7a7e | 2015-05-20 16:30:49 -0400 | [diff] [blame] | 161 | |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 162 | if pc0 == ^uintptr(0) && sp0 == ^uintptr(0) { // Signal to fetch saved values from gp. |
Russ Cox | 15b76ad | 2014-09-09 13:39:57 -0400 | [diff] [blame] | 163 | if gp.syscallsp != 0 { |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 164 | 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 Taylor | 5f9a870 | 2016-04-27 14:18:29 -0700 | [diff] [blame] | 186 | cgoCtxt := gp.cgoCtxt |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 187 | printing := pcbuf == nil && callback == nil |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 188 | _defer := gp._defer |
Austin Clements | 032678e | 2017-11-09 17:55:45 -0500 | [diff] [blame] | 189 | elideWrapper := false |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 190 | |
Matthew Dempsky | a03bdc3 | 2016-02-29 15:01:00 -0800 | [diff] [blame] | 191 | for _defer != nil && _defer.sp == _NoArgs { |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 192 | _defer = _defer.link |
| 193 | } |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 194 | |
| 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 Matloob | 432cb66 | 2015-11-11 12:39:30 -0500 | [diff] [blame] | 202 | frame.pc = uintptr(*(*sys.Uintreg)(unsafe.Pointer(frame.sp))) |
| 203 | frame.sp += sys.RegSize |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| 207 | f := findfunc(frame.pc) |
Austin Clements | 0efc8b2 | 2017-02-20 22:37:07 -0500 | [diff] [blame] | 208 | if !f.valid() { |
Austin Clements | dbd8f3d | 2018-01-22 14:53:36 -0500 | [diff] [blame] | 209 | if callback != nil || printing { |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 210 | print("runtime: unknown pc ", hex(frame.pc), "\n") |
Austin Clements | dbd8f3d | 2018-01-22 14:53:36 -0500 | [diff] [blame] | 211 | tracebackHexdump(gp.stack, &frame, 0) |
| 212 | } |
| 213 | if callback != nil { |
Keith Randall | b2a950b | 2014-12-27 20:58:00 -0800 | [diff] [blame] | 214 | throw("unknown pc") |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 215 | } |
| 216 | return 0 |
| 217 | } |
| 218 | frame.fn = f |
| 219 | |
Austin Clements | beedb1e | 2015-08-12 23:43:43 -0400 | [diff] [blame] | 220 | var cache pcvalueCache |
| 221 | |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 222 | n := 0 |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 223 | 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 Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 230 | f = frame.fn |
Russ Cox | 88d3db0 | 2016-01-06 12:45:23 -0500 | [diff] [blame] | 231 | 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 Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 236 | |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 237 | // Found an actual function. |
| 238 | // Derive frame pointer and link register. |
| 239 | if frame.fp == 0 { |
Daniel Morsing | db6f88a | 2015-04-30 15:32:54 +0100 | [diff] [blame] | 240 | // 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 Clements | a640d95 | 2016-05-20 14:57:55 -0400 | [diff] [blame] | 246 | frame.sp = sp |
Ian Lance Taylor | 5f9a870 | 2016-04-27 14:18:29 -0700 | [diff] [blame] | 247 | cgoCtxt = gp.m.curg.cgoCtxt |
Daniel Morsing | db6f88a | 2015-04-30 15:32:54 +0100 | [diff] [blame] | 248 | } |
Austin Clements | beedb1e | 2015-08-12 23:43:43 -0400 | [diff] [blame] | 249 | frame.fp = sp + uintptr(funcspdelta(f, frame.pc, &cache)) |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 250 | if !usesLR { |
| 251 | // On x86, call instruction pushes return PC before entering new function. |
Michael Matloob | 432cb66 | 2015-11-11 12:39:30 -0500 | [diff] [blame] | 252 | frame.fp += sys.RegSize |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 253 | } |
| 254 | } |
Austin Clements | 0efc8b2 | 2017-02-20 22:37:07 -0500 | [diff] [blame] | 255 | var flr funcInfo |
Austin Clements | 5c2be42 | 2018-01-30 16:01:33 -0500 | [diff] [blame] | 256 | if topofstack(f, gp.m != nil && gp == gp.m.g0) { |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 257 | frame.lr = 0 |
Austin Clements | 0efc8b2 | 2017-02-20 22:37:07 -0500 | [diff] [blame] | 258 | flr = funcInfo{} |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 259 | } 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 Randall | b2a950b | 2014-12-27 20:58:00 -0800 | [diff] [blame] | 267 | throw("traceback_arm: found jmpdefer when tracing with callback") |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 268 | } |
| 269 | frame.lr = 0 |
| 270 | } else { |
Austin Clements | faa7a7e | 2015-05-20 16:30:49 -0400 | [diff] [blame] | 271 | var lrPtr uintptr |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 272 | if usesLR { |
| 273 | if n == 0 && frame.sp < frame.fp || frame.lr == 0 { |
Austin Clements | faa7a7e | 2015-05-20 16:30:49 -0400 | [diff] [blame] | 274 | lrPtr = frame.sp |
| 275 | frame.lr = *(*uintptr)(unsafe.Pointer(lrPtr)) |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 276 | } |
| 277 | } else { |
| 278 | if frame.lr == 0 { |
Michael Matloob | 432cb66 | 2015-11-11 12:39:30 -0500 | [diff] [blame] | 279 | lrPtr = frame.fp - sys.RegSize |
| 280 | frame.lr = uintptr(*(*sys.Uintreg)(unsafe.Pointer(lrPtr))) |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | flr = findfunc(frame.lr) |
Austin Clements | 0efc8b2 | 2017-02-20 22:37:07 -0500 | [diff] [blame] | 284 | if !flr.valid() { |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 285 | // 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 Clements | ebe38b8 | 2018-01-30 16:03:51 -0500 | [diff] [blame] | 289 | doPrint := printing |
Austin Clements | ddb503b | 2018-01-31 12:05:24 -0500 | [diff] [blame] | 290 | if doPrint && gp.m.incgo && f.entry == sigpanicPC { |
Austin Clements | ebe38b8 | 2018-01-30 16:03:51 -0500 | [diff] [blame] | 291 | // 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 Randall | 0bb8fc6 | 2014-12-28 23:16:32 -0800 | [diff] [blame] | 298 | print("runtime: unexpected return pc for ", funcname(f), " called from ", hex(frame.lr), "\n") |
Austin Clements | dbd8f3d | 2018-01-22 14:53:36 -0500 | [diff] [blame] | 299 | tracebackHexdump(gp.stack, &frame, lrPtr) |
| 300 | } |
| 301 | if callback != nil { |
Keith Randall | b2a950b | 2014-12-27 20:58:00 -0800 | [diff] [blame] | 302 | throw("unknown caller pc") |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | frame.varp = frame.fp |
| 308 | if !usesLR { |
| 309 | // On x86, call instruction pushes return PC before entering new function. |
Michael Matloob | 432cb66 | 2015-11-11 12:39:30 -0500 | [diff] [blame] | 310 | frame.varp -= sys.RegSize |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 311 | } |
| 312 | |
Austin Clements | 3c0fee1 | 2015-01-14 11:09:50 -0500 | [diff] [blame] | 313 | // If framepointer_enabled and there's a frame, then |
| 314 | // there's a saved bp here. |
Austin Clements | fc5baec | 2015-02-03 09:18:15 -0500 | [diff] [blame] | 315 | if framepointer_enabled && GOARCH == "amd64" && frame.varp > frame.sp { |
Michael Matloob | 432cb66 | 2015-11-11 12:39:30 -0500 | [diff] [blame] | 316 | frame.varp -= sys.RegSize |
Austin Clements | 3c0fee1 | 2015-01-14 11:09:50 -0500 | [diff] [blame] | 317 | } |
| 318 | |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 319 | // 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 Matloob | 432cb66 | 2015-11-11 12:39:30 -0500 | [diff] [blame] | 326 | frame.argp = frame.fp + sys.MinFrameSize |
Austin Clements | d5bd797 | 2016-10-16 18:23:39 -0400 | [diff] [blame] | 327 | frame.arglen, frame.argmap = getArgInfo(&frame, f, callback != nil, nil) |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 328 | } |
| 329 | |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 330 | // 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 Randall | 53c5226 | 2014-12-08 14:18:58 -0800 | [diff] [blame] | 342 | if _defer != nil && _defer.sp == frame.sp { |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 343 | frame.continpc = _defer.pc |
| 344 | } else { |
| 345 | frame.continpc = 0 |
| 346 | } |
| 347 | } |
| 348 | |
Russ Cox | f95beae | 2014-09-16 10:36:38 -0400 | [diff] [blame] | 349 | // Unwind our local defer stack past this frame. |
Keith Randall | 53c5226 | 2014-12-08 14:18:58 -0800 | [diff] [blame] | 350 | for _defer != nil && (_defer.sp == frame.sp || _defer.sp == _NoArgs) { |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 351 | _defer = _defer.link |
| 352 | } |
| 353 | |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 354 | if callback != nil { |
| 355 | if !callback((*stkframe)(noescape(unsafe.Pointer(&frame))), v) { |
| 356 | return n |
| 357 | } |
| 358 | } |
David Lazar | ee97216 | 2017-03-06 14:48:36 -0500 | [diff] [blame] | 359 | |
| 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 Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 405 | if printing { |
Austin Clements | 032678e | 2017-11-09 17:55:45 -0500 | [diff] [blame] | 406 | // 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 Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 415 | // 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 Cox | a22c11b | 2014-10-29 15:14:24 -0400 | [diff] [blame] | 420 | if (n > 0 || flags&_TraceTrap == 0) && frame.pc > f.entry && !waspanic { |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 421 | tracepc-- |
| 422 | } |
David Lazar | 781fd39 | 2017-02-17 16:08:36 -0500 | [diff] [blame] | 423 | 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 Clements | 0c02bc0 | 2016-02-12 10:33:51 -0500 | [diff] [blame] | 438 | if name == "runtime.gopanic" { |
| 439 | name = "panic" |
| 440 | } |
| 441 | print(name, "(") |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 442 | argp := (*[100]uintptr)(unsafe.Pointer(frame.argp)) |
Michael Matloob | 432cb66 | 2015-11-11 12:39:30 -0500 | [diff] [blame] | 443 | for i := uintptr(0); i < frame.arglen/sys.PtrSize; i++ { |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 444 | 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 Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 454 | print("\t", file, ":", line) |
| 455 | if frame.pc > f.entry { |
| 456 | print(" +", hex(frame.pc-f.entry)) |
| 457 | } |
Russ Cox | bf1de1b | 2015-10-30 11:03:02 -0400 | [diff] [blame] | 458 | if g.m.throwing > 0 && gp == g.m.curg || level >= 2 { |
Austin Clements | 27f8873 | 2017-06-09 11:58:53 -0400 | [diff] [blame] | 459 | print(" fp=", hex(frame.fp), " sp=", hex(frame.sp), " pc=", hex(frame.pc)) |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 460 | } |
| 461 | print("\n") |
| 462 | nprint++ |
| 463 | } |
Austin Clements | 032678e | 2017-11-09 17:55:45 -0500 | [diff] [blame] | 464 | elideWrapper = nextElideWrapper |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 465 | } |
| 466 | n++ |
| 467 | |
| 468 | skipped: |
Ian Lance Taylor | 5f9a870 | 2016-04-27 14:18:29 -0700 | [diff] [blame] | 469 | 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 Cox | 97f8386 | 2014-09-03 13:02:48 -0400 | [diff] [blame] | 481 | waspanic = f.entry == sigpanicPC |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 482 | |
| 483 | // Do not unwind past the bottom of the stack. |
Austin Clements | 0efc8b2 | 2017-02-20 22:37:07 -0500 | [diff] [blame] | 484 | if !flr.valid() { |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 485 | 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 Cox | f0d44db | 2014-09-12 07:29:19 -0400 | [diff] [blame] | 494 | frame.argmap = nil |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 495 | |
| 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 Matloob | 432cb66 | 2015-11-11 12:39:30 -0500 | [diff] [blame] | 500 | frame.sp += sys.MinFrameSize |
Aram Hăvărneanu | 846ee04 | 2015-03-08 14:20:20 +0100 | [diff] [blame] | 501 | if GOARCH == "arm64" { |
| 502 | // arm64 needs 16-byte aligned SP, always |
Michael Matloob | 432cb66 | 2015-11-11 12:39:30 -0500 | [diff] [blame] | 503 | frame.sp += sys.PtrSize |
Aram Hăvărneanu | 846ee04 | 2015-03-08 14:20:20 +0100 | [diff] [blame] | 504 | } |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 505 | f = findfunc(frame.pc) |
| 506 | frame.fn = f |
Austin Clements | 0efc8b2 | 2017-02-20 22:37:07 -0500 | [diff] [blame] | 507 | if !f.valid() { |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 508 | frame.pc = x |
Austin Clements | beedb1e | 2015-08-12 23:43:43 -0400 | [diff] [blame] | 509 | } else if funcspdelta(f, frame.pc, &cache) == 0 { |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 510 | frame.lr = x |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
Matthew Dempsky | 3c8a89d | 2015-02-25 14:41:21 +0900 | [diff] [blame] | 515 | if printing { |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 516 | 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 Randall | 53c5226 | 2014-12-08 14:18:58 -0800 | [diff] [blame] | 570 | print("runtime: g", gp.goid, ": leftover defer sp=", hex(_defer.sp), " pc=", hex(_defer.pc), "\n") |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 571 | } |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 572 | for _defer = gp._defer; _defer != nil; _defer = _defer.link { |
Keith Randall | 53c5226 | 2014-12-08 14:18:58 -0800 | [diff] [blame] | 573 | print("\tdefer ", _defer, " sp=", hex(_defer.sp), " pc=", hex(_defer.pc), "\n") |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 574 | } |
Keith Randall | b2a950b | 2014-12-27 20:58:00 -0800 | [diff] [blame] | 575 | throw("traceback has leftover defers") |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 576 | } |
| 577 | |
Russ Cox | 9c04d00 | 2015-08-26 11:39:10 -0400 | [diff] [blame] | 578 | 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 Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 584 | return n |
| 585 | } |
| 586 | |
Austin Clements | ddd558e | 2016-12-16 11:57:25 -0500 | [diff] [blame] | 587 | // reflectMethodValue is a partial duplicate of reflect.makeFuncImpl |
| 588 | // and reflect.methodValue. |
Austin Clements | d5bd797 | 2016-10-16 18:23:39 -0400 | [diff] [blame] | 589 | type 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 Clements | 0efc8b2 | 2017-02-20 22:37:07 -0500 | [diff] [blame] | 602 | func getArgInfo(frame *stkframe, f funcInfo, needArgMap bool, ctxt *funcval) (arglen uintptr, argmap *bitvector) { |
Austin Clements | b43b375 | 2015-11-17 17:14:32 -0500 | [diff] [blame] | 603 | arglen = uintptr(f.args) |
Russ Cox | f95beae | 2014-09-16 10:36:38 -0400 | [diff] [blame] | 604 | if needArgMap && f.args == _ArgsSizeUnknown { |
| 605 | // Extract argument bitmaps for reflect stubs from the calls they made to reflect. |
Keith Randall | 0bb8fc6 | 2014-12-28 23:16:32 -0800 | [diff] [blame] | 606 | switch funcname(f) { |
Russ Cox | f95beae | 2014-09-16 10:36:38 -0400 | [diff] [blame] | 607 | case "reflect.makeFuncStub", "reflect.methodValueCall": |
Austin Clements | d5bd797 | 2016-10-16 18:23:39 -0400 | [diff] [blame] | 608 | // 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 Randall | 0bb8fc6 | 2014-12-28 23:16:32 -0800 | [diff] [blame] | 626 | print("runtime: confused by ", funcname(f), "\n") |
Keith Randall | b2a950b | 2014-12-27 20:58:00 -0800 | [diff] [blame] | 627 | throw("reflect mismatch") |
Russ Cox | f95beae | 2014-09-16 10:36:38 -0400 | [diff] [blame] | 628 | } |
Austin Clements | d5bd797 | 2016-10-16 18:23:39 -0400 | [diff] [blame] | 629 | bv := mv.stack |
Austin Clements | b43b375 | 2015-11-17 17:14:32 -0500 | [diff] [blame] | 630 | arglen = uintptr(bv.n * sys.PtrSize) |
| 631 | argmap = bv |
Russ Cox | f95beae | 2014-09-16 10:36:38 -0400 | [diff] [blame] | 632 | } |
| 633 | } |
Austin Clements | b43b375 | 2015-11-17 17:14:32 -0500 | [diff] [blame] | 634 | return |
Russ Cox | f95beae | 2014-09-16 10:36:38 -0400 | [diff] [blame] | 635 | } |
| 636 | |
Ian Lance Taylor | 5f9a870 | 2016-04-27 14:18:29 -0700 | [diff] [blame] | 637 | // 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. |
| 640 | func 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 Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 670 | func printcreatedby(gp *g) { |
| 671 | // Show what created goroutine, except main goroutine (goid 1). |
| 672 | pc := gp.gopc |
| 673 | f := findfunc(pc) |
Austin Clements | 032678e | 2017-11-09 17:55:45 -0500 | [diff] [blame] | 674 | if f.valid() && showframe(f, gp, false, false) && gp.goid != 1 { |
Keith Randall | 0bb8fc6 | 2014-12-28 23:16:32 -0800 | [diff] [blame] | 675 | print("created by ", funcname(f), "\n") |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 676 | tracepc := pc // back up to CALL instruction for funcline. |
| 677 | if pc > f.entry { |
Michael Matloob | 432cb66 | 2015-11-11 12:39:30 -0500 | [diff] [blame] | 678 | tracepc -= sys.PCQuantum |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 679 | } |
Russ Cox | 656be31 | 2014-11-12 14:54:31 -0500 | [diff] [blame] | 680 | file, line := funcline(f, tracepc) |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 681 | print("\t", file, ":", line) |
| 682 | if pc > f.entry { |
| 683 | print(" +", hex(pc-f.entry)) |
| 684 | } |
| 685 | print("\n") |
| 686 | } |
| 687 | } |
| 688 | |
Matthew Dempsky | 3c8a89d | 2015-02-25 14:41:21 +0900 | [diff] [blame] | 689 | func traceback(pc, sp, lr uintptr, gp *g) { |
Russ Cox | a22c11b | 2014-10-29 15:14:24 -0400 | [diff] [blame] | 690 | 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 Dempsky | 3c8a89d | 2015-02-25 14:41:21 +0900 | [diff] [blame] | 699 | func tracebacktrap(pc, sp, lr uintptr, gp *g) { |
Russ Cox | a22c11b | 2014-10-29 15:14:24 -0400 | [diff] [blame] | 700 | traceback1(pc, sp, lr, gp, _TraceTrap) |
| 701 | } |
| 702 | |
Matthew Dempsky | 3c8a89d | 2015-02-25 14:41:21 +0900 | [diff] [blame] | 703 | func traceback1(pc, sp, lr uintptr, gp *g, flags uint) { |
Ian Lance Taylor | ea306ae | 2015-12-11 17:16:48 -0800 | [diff] [blame] | 704 | // 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 Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 720 | var n int |
| 721 | if readgstatus(gp)&^_Gscan == _Gsyscall { |
Russ Cox | a22c11b | 2014-10-29 15:14:24 -0400 | [diff] [blame] | 722 | // Override registers if blocked in system call. |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 723 | pc = gp.syscallpc |
| 724 | sp = gp.syscallsp |
Russ Cox | a22c11b | 2014-10-29 15:14:24 -0400 | [diff] [blame] | 725 | flags &^= _TraceTrap |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 726 | } |
| 727 | // Print traceback. By default, omits runtime frames. |
| 728 | // If that means we print nothing at all, repeat forcing all frames printed. |
Russ Cox | a22c11b | 2014-10-29 15:14:24 -0400 | [diff] [blame] | 729 | 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 Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 732 | } |
| 733 | if n == _TracebackMaxFrames { |
| 734 | print("...additional frames elided...\n") |
| 735 | } |
| 736 | printcreatedby(gp) |
| 737 | } |
| 738 | |
Matthew Dempsky | 3c8a89d | 2015-02-25 14:41:21 +0900 | [diff] [blame] | 739 | func callers(skip int, pcbuf []uintptr) int { |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 740 | sp := getcallersp(unsafe.Pointer(&skip)) |
Austin Clements | 229aaac | 2017-09-22 15:16:26 -0400 | [diff] [blame] | 741 | pc := getcallerpc() |
Austin Clements | 719efc7 | 2015-05-20 11:50:48 -0400 | [diff] [blame] | 742 | gp := getg() |
Russ Cox | 39bcbb3 | 2014-11-05 23:01:48 -0500 | [diff] [blame] | 743 | var n int |
Russ Cox | 656be31 | 2014-11-12 14:54:31 -0500 | [diff] [blame] | 744 | systemstack(func() { |
Austin Clements | 719efc7 | 2015-05-20 11:50:48 -0400 | [diff] [blame] | 745 | n = gentraceback(pc, sp, 0, gp, skip, &pcbuf[0], len(pcbuf), nil, nil, 0) |
Russ Cox | 39bcbb3 | 2014-11-05 23:01:48 -0500 | [diff] [blame] | 746 | }) |
| 747 | return n |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 748 | } |
| 749 | |
Matthew Dempsky | 3c8a89d | 2015-02-25 14:41:21 +0900 | [diff] [blame] | 750 | func 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 Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 752 | } |
Russ Cox | 7ba41e9 | 2014-09-03 11:11:16 -0400 | [diff] [blame] | 753 | |
Austin Clements | 032678e | 2017-11-09 17:55:45 -0500 | [diff] [blame] | 754 | func showframe(f funcInfo, gp *g, firstFrame, elideWrapper bool) bool { |
Russ Cox | 97f8386 | 2014-09-03 13:02:48 -0400 | [diff] [blame] | 755 | g := getg() |
Russ Cox | 181e26b | 2015-04-17 00:21:30 -0400 | [diff] [blame] | 756 | if g.m.throwing > 0 && gp != nil && (gp == g.m.curg || gp == g.m.caughtsig.ptr()) { |
Russ Cox | 97f8386 | 2014-09-03 13:02:48 -0400 | [diff] [blame] | 757 | return true |
| 758 | } |
Russ Cox | bf1de1b | 2015-10-30 11:03:02 -0400 | [diff] [blame] | 759 | level, _, _ := gotraceback() |
Austin Clements | e972095 | 2017-06-12 11:12:12 -0400 | [diff] [blame] | 760 | if level > 1 { |
| 761 | // Show all frames. |
| 762 | return true |
| 763 | } |
| 764 | |
Austin Clements | 032678e | 2017-11-09 17:55:45 -0500 | [diff] [blame] | 765 | 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 Randall | 0bb8fc6 | 2014-12-28 23:16:32 -0800 | [diff] [blame] | 776 | name := funcname(f) |
Russ Cox | 97f8386 | 2014-09-03 13:02:48 -0400 | [diff] [blame] | 777 | |
Russ Cox | f9feaff | 2016-11-12 23:01:37 -0500 | [diff] [blame] | 778 | // 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 Cox | 97f8386 | 2014-09-03 13:02:48 -0400 | [diff] [blame] | 782 | // See golang.org/issue/5832. |
Russ Cox | f9feaff | 2016-11-12 23:01:37 -0500 | [diff] [blame] | 783 | if name == "runtime.gopanic" && !firstFrame { |
Russ Cox | 97f8386 | 2014-09-03 13:02:48 -0400 | [diff] [blame] | 784 | return true |
| 785 | } |
| 786 | |
Austin Clements | 032678e | 2017-11-09 17:55:45 -0500 | [diff] [blame] | 787 | return contains(name, ".") && (!hasprefix(name, "runtime.") || isExportedRuntime(name)) |
Russ Cox | 54245cb | 2014-09-18 20:35:36 -0400 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | // isExportedRuntime reports whether name is an exported runtime function. |
| 791 | // It is only for runtime functions, so ASCII A-Z is fine. |
| 792 | func isExportedRuntime(name string) bool { |
| 793 | const n = len("runtime.") |
| 794 | return len(name) > n && name[:n] == "runtime." && 'A' <= name[n] && name[n] <= 'Z' |
Russ Cox | 97f8386 | 2014-09-03 13:02:48 -0400 | [diff] [blame] | 795 | } |
| 796 | |
Austin Clements | 032678e | 2017-11-09 17:55:45 -0500 | [diff] [blame] | 797 | // elideWrapperCalling returns whether a wrapper function that called |
| 798 | // function "name" should be elided from stack traces. |
| 799 | func 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 Cox | 7ba41e9 | 2014-09-03 11:11:16 -0400 | [diff] [blame] | 805 | var gStatusStrings = [...]string{ |
| 806 | _Gidle: "idle", |
| 807 | _Grunnable: "runnable", |
| 808 | _Grunning: "running", |
| 809 | _Gsyscall: "syscall", |
| 810 | _Gwaiting: "waiting", |
| 811 | _Gdead: "dead", |
Russ Cox | 7ba41e9 | 2014-09-03 11:11:16 -0400 | [diff] [blame] | 812 | _Gcopystack: "copystack", |
| 813 | } |
| 814 | |
Russ Cox | 7ba41e9 | 2014-09-03 11:11:16 -0400 | [diff] [blame] | 815 | func goroutineheader(gp *g) { |
| 816 | gpstatus := readgstatus(gp) |
| 817 | |
Austin Clements | c7c7c70 | 2015-12-21 11:26:33 -0800 | [diff] [blame] | 818 | isScan := gpstatus&_Gscan != 0 |
| 819 | gpstatus &^= _Gscan // drop the scan bit |
| 820 | |
Russ Cox | 7ba41e9 | 2014-09-03 11:11:16 -0400 | [diff] [blame] | 821 | // Basic string status |
| 822 | var status string |
| 823 | if 0 <= gpstatus && gpstatus < uint32(len(gStatusStrings)) { |
| 824 | status = gStatusStrings[gpstatus] |
Russ Cox | 7ba41e9 | 2014-09-03 11:11:16 -0400 | [diff] [blame] | 825 | } else { |
| 826 | status = "???" |
| 827 | } |
| 828 | |
| 829 | // Override. |
Austin Clements | c7c7c70 | 2015-12-21 11:26:33 -0800 | [diff] [blame] | 830 | if gpstatus == _Gwaiting && gp.waitreason != "" { |
Russ Cox | 7ba41e9 | 2014-09-03 11:11:16 -0400 | [diff] [blame] | 831 | status = gp.waitreason |
| 832 | } |
| 833 | |
| 834 | // approx time the G is blocked, in minutes |
| 835 | var waitfor int64 |
Russ Cox | 7ba41e9 | 2014-09-03 11:11:16 -0400 | [diff] [blame] | 836 | if (gpstatus == _Gwaiting || gpstatus == _Gsyscall) && gp.waitsince != 0 { |
| 837 | waitfor = (nanotime() - gp.waitsince) / 60e9 |
| 838 | } |
| 839 | print("goroutine ", gp.goid, " [", status) |
Austin Clements | c7c7c70 | 2015-12-21 11:26:33 -0800 | [diff] [blame] | 840 | if isScan { |
| 841 | print(" (scan)") |
| 842 | } |
Russ Cox | 7ba41e9 | 2014-09-03 11:11:16 -0400 | [diff] [blame] | 843 | if waitfor >= 1 { |
| 844 | print(", ", waitfor, " minutes") |
| 845 | } |
Ian Lance Taylor | 165c15a | 2017-09-13 10:14:02 -0700 | [diff] [blame] | 846 | if gp.lockedm != 0 { |
Russ Cox | 7ba41e9 | 2014-09-03 11:11:16 -0400 | [diff] [blame] | 847 | print(", locked to thread") |
| 848 | } |
| 849 | print("]:\n") |
| 850 | } |
| 851 | |
| 852 | func tracebackothers(me *g) { |
Russ Cox | bf1de1b | 2015-10-30 11:03:02 -0400 | [diff] [blame] | 853 | level, _, _ := gotraceback() |
Russ Cox | 7ba41e9 | 2014-09-03 11:11:16 -0400 | [diff] [blame] | 854 | |
| 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 Vyukov | 59495e8 | 2015-02-07 15:31:18 +0300 | [diff] [blame] | 866 | if gp == me || gp == g.m.curg || readgstatus(gp) == _Gdead || isSystemGoroutine(gp) && level < 2 { |
Russ Cox | 7ba41e9 | 2014-09-03 11:11:16 -0400 | [diff] [blame] | 867 | continue |
| 868 | } |
| 869 | print("\n") |
| 870 | goroutineheader(gp) |
Keith Randall | 4b78c95 | 2015-04-28 14:53:19 -0700 | [diff] [blame] | 871 | // Note: gp.m == g.m occurs when tracebackothers is |
| 872 | // called from a signal handler initiated during a |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 873 | // systemstack call. The original G is still in the |
Keith Randall | 4b78c95 | 2015-04-28 14:53:19 -0700 | [diff] [blame] | 874 | // running state, and we want to print its stack. |
| 875 | if gp.m != g.m && readgstatus(gp)&^_Gscan == _Grunning { |
Russ Cox | 7ba41e9 | 2014-09-03 11:11:16 -0400 | [diff] [blame] | 876 | 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 Clements | dbd8f3d | 2018-01-22 14:53:36 -0500 | [diff] [blame] | 885 | // 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. |
| 888 | func 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 Cox | 7ba41e9 | 2014-09-03 11:11:16 -0400 | [diff] [blame] | 932 | // Does f mark the top of a goroutine stack? |
Austin Clements | 5c2be42 | 2018-01-30 16:01:33 -0500 | [diff] [blame] | 933 | func topofstack(f funcInfo, g0 bool) bool { |
Russ Cox | 7ba41e9 | 2014-09-03 11:11:16 -0400 | [diff] [blame] | 934 | pc := f.entry |
| 935 | return pc == goexitPC || |
| 936 | pc == mstartPC || |
| 937 | pc == mcallPC || |
Russ Cox | 7ba41e9 | 2014-09-03 11:11:16 -0400 | [diff] [blame] | 938 | pc == morestackPC || |
Russ Cox | 7ba41e9 | 2014-09-03 11:11:16 -0400 | [diff] [blame] | 939 | pc == rt0_goPC || |
Austin Clements | 5c2be42 | 2018-01-30 16:01:33 -0500 | [diff] [blame] | 940 | 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 Cox | 7ba41e9 | 2014-09-03 11:11:16 -0400 | [diff] [blame] | 947 | } |
Dmitry Vyukov | 59495e8 | 2015-02-07 15:31:18 +0300 | [diff] [blame] | 948 | |
Josh Bleecher Snyder | 2adc4e8 | 2015-02-17 15:44:42 -0800 | [diff] [blame] | 949 | // isSystemGoroutine reports whether the goroutine g must be omitted in |
Dmitry Vyukov | 59495e8 | 2015-02-07 15:31:18 +0300 | [diff] [blame] | 950 | // stack dumps and deadlock detector. |
| 951 | func isSystemGoroutine(gp *g) bool { |
| 952 | pc := gp.startpc |
| 953 | return pc == runfinqPC && !fingRunning || |
Dmitry Vyukov | 59495e8 | 2015-02-07 15:31:18 +0300 | [diff] [blame] | 954 | pc == bgsweepPC || |
| 955 | pc == forcegchelperPC || |
Austin Clements | 8d03acc | 2015-03-23 21:07:33 -0400 | [diff] [blame] | 956 | pc == timerprocPC || |
| 957 | pc == gcBgMarkWorkerPC |
Dmitry Vyukov | 59495e8 | 2015-02-07 15:31:18 +0300 | [diff] [blame] | 958 | } |
Ian Lance Taylor | ea306ae | 2015-12-11 17:16:48 -0800 | [diff] [blame] | 959 | |
| 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 Fitzpatrick | 06d639e | 2016-04-27 16:56:13 -0500 | [diff] [blame] | 974 | // struct { |
| 975 | // Context uintptr |
| 976 | // } |
Ian Lance Taylor | ea306ae | 2015-12-11 17:16:48 -0800 | [diff] [blame] | 977 | // |
| 978 | // In C syntax, this struct will be |
| 979 | // |
Brad Fitzpatrick | 06d639e | 2016-04-27 16:56:13 -0500 | [diff] [blame] | 980 | // struct { |
| 981 | // uintptr_t Context; |
| 982 | // }; |
Ian Lance Taylor | ea306ae | 2015-12-11 17:16:48 -0800 | [diff] [blame] | 983 | // |
| 984 | // If the Context field is 0, the context function is being called to |
Ian Lance Taylor | 5f9a870 | 2016-04-27 14:18:29 -0700 | [diff] [blame] | 985 | // 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 Taylor | ea306ae | 2015-12-11 17:16:48 -0800 | [diff] [blame] | 990 | // |
| 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 Vyukov | ba22172 | 2016-06-02 07:43:21 +0200 | [diff] [blame] | 994 | // to its C code caller. This permits the context function to release |
| 995 | // any associated resources. |
Ian Lance Taylor | ea306ae | 2015-12-11 17:16:48 -0800 | [diff] [blame] | 996 | // |
| 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 Fitzpatrick | 06d639e | 2016-04-27 16:56:13 -0500 | [diff] [blame] | 1007 | // struct { |
Ian Lance Taylor | 3d037cf | 2016-05-27 10:05:52 -0700 | [diff] [blame] | 1008 | // Context uintptr |
| 1009 | // SigContext uintptr |
| 1010 | // Buf *uintptr |
| 1011 | // Max uintptr |
Brad Fitzpatrick | 06d639e | 2016-04-27 16:56:13 -0500 | [diff] [blame] | 1012 | // } |
Ian Lance Taylor | ea306ae | 2015-12-11 17:16:48 -0800 | [diff] [blame] | 1013 | // |
| 1014 | // In C syntax, this struct will be |
| 1015 | // |
Brad Fitzpatrick | 06d639e | 2016-04-27 16:56:13 -0500 | [diff] [blame] | 1016 | // struct { |
| 1017 | // uintptr_t Context; |
Ian Lance Taylor | 3d037cf | 2016-05-27 10:05:52 -0700 | [diff] [blame] | 1018 | // uintptr_t SigContext; |
Brad Fitzpatrick | 06d639e | 2016-04-27 16:56:13 -0500 | [diff] [blame] | 1019 | // uintptr_t* Buf; |
| 1020 | // uintptr_t Max; |
| 1021 | // }; |
Ian Lance Taylor | ea306ae | 2015-12-11 17:16:48 -0800 | [diff] [blame] | 1022 | // |
| 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 Taylor | 3d037cf | 2016-05-27 10:05:52 -0700 | [diff] [blame] | 1039 | // 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 Taylor | ea306ae | 2015-12-11 17:16:48 -0800 | [diff] [blame] | 1046 | // 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 Fitzpatrick | 06d639e | 2016-04-27 16:56:13 -0500 | [diff] [blame] | 1060 | // 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 Taylor | ea306ae | 2015-12-11 17:16:48 -0800 | [diff] [blame] | 1069 | // |
| 1070 | // In C syntax, this struct will be |
| 1071 | // |
Brad Fitzpatrick | 06d639e | 2016-04-27 16:56:13 -0500 | [diff] [blame] | 1072 | // 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 Taylor | ea306ae | 2015-12-11 17:16:48 -0800 | [diff] [blame] | 1081 | // |
| 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 Taylor | 03abde4 | 2016-06-02 12:01:03 -0700 | [diff] [blame] | 1112 | // |
| 1113 | // SetCgoTraceback should be called only once, ideally from an init function. |
Ian Lance Taylor | ea306ae | 2015-12-11 17:16:48 -0800 | [diff] [blame] | 1114 | func SetCgoTraceback(version int, traceback, context, symbolizer unsafe.Pointer) { |
| 1115 | if version != 0 { |
| 1116 | panic("unsupported version") |
| 1117 | } |
Ian Lance Taylor | 5f9a870 | 2016-04-27 14:18:29 -0700 | [diff] [blame] | 1118 | |
Ian Lance Taylor | 03abde4 | 2016-06-02 12:01:03 -0700 | [diff] [blame] | 1119 | 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 Taylor | ea306ae | 2015-12-11 17:16:48 -0800 | [diff] [blame] | 1125 | cgoTraceback = traceback |
Ian Lance Taylor | 03abde4 | 2016-06-02 12:01:03 -0700 | [diff] [blame] | 1126 | cgoContext = context |
Ian Lance Taylor | ea306ae | 2015-12-11 17:16:48 -0800 | [diff] [blame] | 1127 | cgoSymbolizer = symbolizer |
Ian Lance Taylor | 5f9a870 | 2016-04-27 14:18:29 -0700 | [diff] [blame] | 1128 | |
| 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 Taylor | ea306ae | 2015-12-11 17:16:48 -0800 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | var cgoTraceback unsafe.Pointer |
Ian Lance Taylor | 03abde4 | 2016-06-02 12:01:03 -0700 | [diff] [blame] | 1137 | var cgoContext unsafe.Pointer |
Ian Lance Taylor | ea306ae | 2015-12-11 17:16:48 -0800 | [diff] [blame] | 1138 | var cgoSymbolizer unsafe.Pointer |
| 1139 | |
| 1140 | // cgoTracebackArg is the type passed to cgoTraceback. |
| 1141 | type cgoTracebackArg struct { |
Ian Lance Taylor | 3d037cf | 2016-05-27 10:05:52 -0700 | [diff] [blame] | 1142 | context uintptr |
| 1143 | sigContext uintptr |
| 1144 | buf *uintptr |
| 1145 | max uintptr |
Ian Lance Taylor | ea306ae | 2015-12-11 17:16:48 -0800 | [diff] [blame] | 1146 | } |
| 1147 | |
Ian Lance Taylor | 5f9a870 | 2016-04-27 14:18:29 -0700 | [diff] [blame] | 1148 | // cgoContextArg is the type passed to the context function. |
Ian Lance Taylor | ea306ae | 2015-12-11 17:16:48 -0800 | [diff] [blame] | 1149 | type cgoContextArg struct { |
| 1150 | context uintptr |
| 1151 | } |
| 1152 | |
| 1153 | // cgoSymbolizerArg is the type passed to cgoSymbolizer. |
| 1154 | type 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. |
| 1165 | func 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 Taylor | ea306ae | 2015-12-11 17:16:48 -0800 | [diff] [blame] | 1176 | var arg cgoSymbolizerArg |
| 1177 | for _, c := range callers { |
| 1178 | if c == 0 { |
| 1179 | break |
| 1180 | } |
Ian Lance Taylor | 5f9a870 | 2016-04-27 14:18:29 -0700 | [diff] [blame] | 1181 | printOneCgoTraceback(c, 0x7fffffff, &arg) |
Ian Lance Taylor | ea306ae | 2015-12-11 17:16:48 -0800 | [diff] [blame] | 1182 | } |
| 1183 | arg.pc = 0 |
Ian Lance Taylor | 5f9a870 | 2016-04-27 14:18:29 -0700 | [diff] [blame] | 1184 | 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. |
| 1190 | func 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 Taylor | c08436d | 2016-05-18 15:20:56 -0700 | [diff] [blame] | 1210 | print("pc=", hex(pc), "\n") |
Ian Lance Taylor | 5f9a870 | 2016-04-27 14:18:29 -0700 | [diff] [blame] | 1211 | c++ |
| 1212 | if arg.more == 0 { |
| 1213 | break |
| 1214 | } |
| 1215 | } |
| 1216 | return c |
| 1217 | } |
| 1218 | |
| 1219 | // callCgoSymbolizer calls the cgoSymbolizer function. |
| 1220 | func 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 Taylor | d00890b | 2016-07-10 18:44:09 -0700 | [diff] [blame] | 1227 | if msanenabled { |
| 1228 | msanwrite(unsafe.Pointer(arg), unsafe.Sizeof(cgoSymbolizerArg{})) |
| 1229 | } |
Ian Lance Taylor | 5f9a870 | 2016-04-27 14:18:29 -0700 | [diff] [blame] | 1230 | call(cgoSymbolizer, noescape(unsafe.Pointer(arg))) |
| 1231 | } |
| 1232 | |
| 1233 | // cgoContextPCs gets the PC values from a cgo traceback. |
| 1234 | func 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 Taylor | d00890b | 2016-07-10 18:44:09 -0700 | [diff] [blame] | 1249 | if msanenabled { |
| 1250 | msanwrite(unsafe.Pointer(&arg), unsafe.Sizeof(arg)) |
| 1251 | } |
Ian Lance Taylor | 5f9a870 | 2016-04-27 14:18:29 -0700 | [diff] [blame] | 1252 | call(cgoTraceback, noescape(unsafe.Pointer(&arg))) |
Ian Lance Taylor | ea306ae | 2015-12-11 17:16:48 -0800 | [diff] [blame] | 1253 | } |