Source file src/runtime/traceback.go
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 7 import ( 8 "internal/abi" 9 "internal/bytealg" 10 "internal/goarch" 11 "internal/runtime/pprof/label" 12 "internal/runtime/sys" 13 "internal/stringslite" 14 "unsafe" 15 ) 16 17 // The code in this file implements stack trace walking for all architectures. 18 // The most important fact about a given architecture is whether it uses a link register. 19 // On systems with link registers, the prologue for a non-leaf function stores the 20 // incoming value of LR at the bottom of the newly allocated stack frame. 21 // On systems without link registers (x86), the architecture pushes a return PC during 22 // the call instruction, so the return PC ends up above the stack frame. 23 // In this file, the return PC is always called LR, no matter how it was found. 24 25 const usesLR = sys.MinFrameSize > 0 26 27 // retPCSize is the size of the return-PC slot between stack frames on 28 // architectures without a link register. 29 // The linker describes the same slot using the target's register size. 30 const retPCSize = goarch.PtrSize 31 32 const ( 33 // tracebackInnerFrames is the number of innermost frames to print in a 34 // stack trace. The total maximum frames is tracebackInnerFrames + 35 // tracebackOuterFrames. 36 tracebackInnerFrames = 50 37 38 // tracebackOuterFrames is the number of outermost frames to print in a 39 // stack trace. 40 tracebackOuterFrames = 50 41 ) 42 43 // unwindFlags control the behavior of various unwinders. 44 type unwindFlags uint8 45 46 const ( 47 // unwindPrintErrors indicates that if unwinding encounters an error, it 48 // should print a message and stop without throwing. This is used for things 49 // like stack printing, where it's better to get incomplete information than 50 // to crash. This is also used in situations where everything may not be 51 // stopped nicely and the stack walk may not be able to complete, such as 52 // during profiling signals or during a crash. 53 // 54 // If neither unwindPrintErrors or unwindSilentErrors are set, unwinding 55 // performs extra consistency checks and throws on any error. 56 // 57 // Note that there are a small number of fatal situations that will throw 58 // regardless of unwindPrintErrors or unwindSilentErrors. 59 unwindPrintErrors unwindFlags = 1 << iota 60 61 // unwindSilentErrors silently ignores errors during unwinding. 62 unwindSilentErrors 63 64 // unwindTrap indicates that the initial PC and SP are from a trap, not a 65 // return PC from a call. 66 // 67 // The unwindTrap flag is updated during unwinding. If set, frame.pc is the 68 // address of a faulting instruction instead of the return address of a 69 // call. It also means the liveness at pc may not be known. 70 // 71 // TODO: Distinguish frame.continpc, which is really the stack map PC, from 72 // the actual continuation PC, which is computed differently depending on 73 // this flag and a few other things. 74 unwindTrap 75 76 // unwindJumpStack indicates that, if the traceback is on a system stack, it 77 // should resume tracing at the user stack when the system stack is 78 // exhausted. 79 unwindJumpStack 80 ) 81 82 // errFatal reports whether an unwinding error should throw rather than be 83 // tolerated: always with neither unwindPrintErrors nor unwindSilentErrors 84 // set (e.g. GC unwinds), or under GODEBUG=tracebackcrash=1. 85 func (u *unwinder) errFatal() bool { 86 return u.flags&(unwindPrintErrors|unwindSilentErrors) == 0 || debug.tracebackcrash != 0 87 } 88 89 // An unwinder iterates the physical stack frames of a Go sack. 90 // 91 // Typical use of an unwinder looks like: 92 // 93 // var u unwinder 94 // for u.init(gp, 0); u.valid(); u.next() { 95 // // ... use frame info in u ... 96 // } 97 // 98 // Implementation note: This is carefully structured to be pointer-free because 99 // tracebacks happen in places that disallow write barriers (e.g., signals). 100 // Even if this is stack-allocated, its pointer-receiver methods don't know that 101 // their receiver is on the stack, so they still emit write barriers. Here we 102 // address that by carefully avoiding any pointers in this type. Another 103 // approach would be to split this into a mutable part that's passed by pointer 104 // but contains no pointers itself and an immutable part that's passed and 105 // returned by value and can contain pointers. We could potentially hide that 106 // we're doing that in trivial methods that are inlined into the caller that has 107 // the stack allocation, but that's fragile. 108 type unwinder struct { 109 // frame is the current physical stack frame, or all 0s if 110 // there is no frame. 111 frame stkframe 112 113 // g is the G who's stack is being unwound. If the 114 // unwindJumpStack flag is set and the unwinder jumps stacks, 115 // this will be different from the initial G. 116 g guintptr 117 118 // cgoCtxt is the index into g.cgoCtxt of the next frame on the cgo stack. 119 // The cgo stack is unwound in tandem with the Go stack as we find marker frames. 120 cgoCtxt int 121 122 // calleeFuncID is the function ID of the caller of the current 123 // frame. 124 calleeFuncID abi.FuncID 125 126 // flags are the flags to this unwind. Some of these are updated as we 127 // unwind (see the flags documentation). 128 flags unwindFlags 129 } 130 131 // init initializes u to start unwinding gp's stack and positions the 132 // iterator on gp's innermost frame. gp must not be the current G. 133 // 134 // A single unwinder can be reused for multiple unwinds. 135 func (u *unwinder) init(gp *g, flags unwindFlags) { 136 // Implementation note: This starts the iterator on the first frame and we 137 // provide a "valid" method. Alternatively, this could start in a "before 138 // the first frame" state and "next" could return whether it was able to 139 // move to the next frame, but that's both more awkward to use in a "for" 140 // loop and is harder to implement because we have to do things differently 141 // for the first frame. 142 u.initAt(^uintptr(0), ^uintptr(0), ^uintptr(0), gp, flags) 143 } 144 145 func (u *unwinder) initAt(pc0, sp0, lr0 uintptr, gp *g, flags unwindFlags) { 146 // Don't call this "g"; it's too easy get "g" and "gp" confused. 147 if ourg := getg(); ourg == gp && ourg == ourg.m.curg { 148 // The starting sp has been passed in as a uintptr, and the caller may 149 // have other uintptr-typed stack references as well. 150 // If during one of the calls that got us here or during one of the 151 // callbacks below the stack must be grown, all these uintptr references 152 // to the stack will not be updated, and traceback will continue 153 // to inspect the old stack memory, which may no longer be valid. 154 // Even if all the variables were updated correctly, it is not clear that 155 // we want to expose a traceback that begins on one stack and ends 156 // on another stack. That could confuse callers quite a bit. 157 // Instead, we require that initAt and any other function that 158 // accepts an sp for the current goroutine (typically obtained by 159 // calling GetCallerSP) must not run on that goroutine's stack but 160 // instead on the g0 stack. 161 throw("cannot trace user goroutine on its own stack") 162 } 163 164 if pc0 == ^uintptr(0) && sp0 == ^uintptr(0) { // Signal to fetch saved values from gp. 165 if gp.syscallsp != 0 { 166 pc0 = gp.syscallpc 167 sp0 = gp.syscallsp 168 if usesLR { 169 lr0 = 0 170 } 171 } else { 172 pc0 = gp.sched.pc 173 sp0 = gp.sched.sp 174 if usesLR { 175 lr0 = gp.sched.lr 176 } 177 } 178 } 179 180 var frame stkframe 181 frame.pc = pc0 182 frame.sp = sp0 183 if usesLR { 184 frame.lr = lr0 185 } 186 187 // If the PC is zero, it's likely a nil function call. 188 // Start in the caller's frame. 189 if frame.pc == 0 { 190 if usesLR { 191 frame.pc = *(*uintptr)(unsafe.Pointer(frame.sp)) 192 frame.lr = 0 193 } else { 194 frame.pc = *(*uintptr)(unsafe.Pointer(frame.sp)) 195 frame.sp += retPCSize 196 } 197 } 198 199 // internal/runtime/atomic functions call into kernel helpers on 200 // arm < 7. See internal/runtime/atomic/sys_linux_arm.s. 201 // 202 // Start in the caller's frame. 203 if GOARCH == "arm" && goarm < 7 && GOOS == "linux" && frame.pc&0xffff0000 == 0xffff0000 { 204 // Note that the calls are simple BL without pushing the return 205 // address, so we use LR directly. 206 // 207 // The kernel helpers are frameless leaf functions, so SP and 208 // LR are not touched. 209 frame.pc = frame.lr 210 frame.lr = 0 211 } 212 213 f := findfunc(frame.pc) 214 if !f.valid() { 215 if flags&unwindSilentErrors == 0 { 216 print("runtime: g ", gp.goid, " gp=", gp, ": unknown pc ", hex(frame.pc), "\n") 217 tracebackHexdump(gp.stack, &frame, 0) 218 } 219 if flags&(unwindPrintErrors|unwindSilentErrors) == 0 { 220 throw("unknown pc") 221 } 222 *u = unwinder{} 223 return 224 } 225 frame.fn = f 226 227 // Populate the unwinder. 228 *u = unwinder{ 229 frame: frame, 230 g: gp.guintptr(), 231 cgoCtxt: len(gp.cgoCtxt) - 1, 232 calleeFuncID: abi.FuncIDNormal, 233 flags: flags, 234 } 235 236 isSyscall := frame.pc == pc0 && frame.sp == sp0 && pc0 == gp.syscallpc && sp0 == gp.syscallsp 237 u.resolveInternal(true, isSyscall) 238 } 239 240 func (u *unwinder) valid() bool { 241 return u.frame.pc != 0 242 } 243 244 // resolveInternal fills in u.frame based on u.frame.fn, pc, and sp. 245 // 246 // innermost indicates that this is the first resolve on this stack. If 247 // innermost is set, isSyscall indicates that the PC/SP was retrieved from 248 // gp.syscall*; this is otherwise ignored. 249 // 250 // On entry, u.frame contains: 251 // - fn is the running function. 252 // - pc is the PC in the running function. 253 // - sp is the stack pointer at that program counter. 254 // - For the innermost frame on LR machines, lr is the program counter that called fn. 255 // 256 // On return, u.frame contains: 257 // - fp is the stack pointer of the caller. 258 // - lr is the program counter that called fn. 259 // - varp, argp, and continpc are populated for the current frame. 260 // 261 // If fn is a stack-jumping function, resolveInternal can change the entire 262 // frame state to follow that stack jump. 263 // 264 // This is internal to unwinder. 265 func (u *unwinder) resolveInternal(innermost, isSyscall bool) { 266 frame := &u.frame 267 gp := u.g.ptr() 268 269 f := frame.fn 270 if f.pcsp == 0 { 271 // No frame information, must be external function, like race support. 272 // See golang.org/issue/13568. 273 u.finishInternal() 274 return 275 } 276 277 // Compute function info flags. 278 flag := f.flag 279 if f.funcID == abi.FuncID_cgocallback { 280 // cgocallback does write SP to switch from the g0 to the curg stack, 281 // but it carefully arranges that during the transition BOTH stacks 282 // have cgocallback frame valid for unwinding through. 283 // So we don't need to exclude it with the other SP-writing functions. 284 flag &^= abi.FuncFlagSPWrite 285 } 286 if isSyscall { 287 // Some Syscall functions write to SP, but they do so only after 288 // saving the entry PC/SP using entersyscall. 289 // Since we are using the entry PC/SP, the later SP write doesn't matter. 290 flag &^= abi.FuncFlagSPWrite 291 } 292 293 // Found an actual function. 294 // Derive frame pointer. 295 if frame.fp == 0 { 296 // Jump over system stack transitions. If we're on g0 and there's a user 297 // goroutine, try to jump. Otherwise this is a regular call. 298 // We also defensively check that this won't switch M's on us, 299 // which could happen at critical points in the scheduler. 300 // This ensures gp.m doesn't change from a stack jump. 301 if u.flags&unwindJumpStack != 0 && gp == gp.m.g0 && gp.m.curg != nil && gp.m.curg.m == gp.m { 302 switch f.funcID { 303 case abi.FuncID_morestack: 304 // morestack does not return normally -- newstack() 305 // gogo's to curg.sched. Match that. 306 // This keeps morestack() from showing up in the backtrace, 307 // but that makes some sense since it'll never be returned 308 // to. 309 gp = gp.m.curg 310 u.g.set(gp) 311 frame.pc = gp.sched.pc 312 frame.fn = findfunc(frame.pc) 313 f = frame.fn 314 flag = f.flag 315 frame.lr = gp.sched.lr 316 frame.sp = gp.sched.sp 317 u.cgoCtxt = len(gp.cgoCtxt) - 1 318 case abi.FuncID_systemstack: 319 // systemstack returns normally, so just follow the 320 // stack transition. 321 if usesLR && funcspdelta(f, frame.pc) == 0 { 322 // We're at the function prologue and the stack 323 // switch hasn't happened, or epilogue where we're 324 // about to return. Just unwind normally. 325 // Do this only on LR machines because on x86 326 // systemstack doesn't have an SP delta (the CALL 327 // instruction opens the frame), therefore no way 328 // to check. 329 flag &^= abi.FuncFlagSPWrite 330 break 331 } 332 gp = gp.m.curg 333 u.g.set(gp) 334 frame.sp = gp.sched.sp 335 u.cgoCtxt = len(gp.cgoCtxt) - 1 336 flag &^= abi.FuncFlagSPWrite 337 } 338 } 339 frame.fp = frame.sp + uintptr(funcspdelta(f, frame.pc)) 340 if !usesLR { 341 // On x86, call instruction pushes return PC before entering new function. 342 frame.fp += retPCSize 343 } 344 } 345 346 // Derive link register. 347 if flag&abi.FuncFlagTopFrame != 0 { 348 // This function marks the top of the stack. Stop the traceback. 349 frame.lr = 0 350 } else if flag&abi.FuncFlagSPWrite != 0 && (!innermost || u.flags&(unwindPrintErrors|unwindSilentErrors) != 0) { 351 // The function we are in does a write to SP that we don't know 352 // how to encode in the spdelta table. Examples include context 353 // switch routines like runtime.gogo but also any code that switches 354 // to the g0 stack to run host C code. 355 // We can't reliably unwind the SP (we might not even be on 356 // the stack we think we are), so stop the traceback here. 357 // 358 // The one exception (encoded in the complex condition above) is that 359 // we assume if we're doing a precise traceback, and this is the 360 // innermost frame, that the SPWRITE function voluntarily preempted itself on entry 361 // during the stack growth check. In that case, the function has 362 // not yet had a chance to do any writes to SP and is safe to unwind. 363 // isAsyncSafePoint does not allow assembly functions to be async preempted, 364 // and preemptPark double-checks that SPWRITE functions are not async preempted. 365 // So for GC stack traversal, we can safely ignore SPWRITE for the innermost frame, 366 // but farther up the stack we'd better not find any. 367 // This is somewhat imprecise because we're just guessing that we're in the stack 368 // growth check. It would be better if SPWRITE were encoded in the spdelta 369 // table so we would know for sure that we were still in safe code. 370 // 371 // uSE uPE inn | action 372 // T _ _ | frame.lr = 0 373 // F T _ | frame.lr = 0 374 // F F F | print; panic 375 // F F T | ignore SPWrite 376 if u.flags&(unwindPrintErrors|unwindSilentErrors) == 0 && !innermost { 377 println("traceback: unexpected SPWRITE function", funcname(f)) 378 throw("traceback") 379 } 380 frame.lr = 0 381 } else { 382 var lrPtr uintptr 383 if usesLR { 384 if innermost && frame.sp < frame.fp || frame.lr == 0 { 385 lrPtr = frame.sp 386 frame.lr = *(*uintptr)(unsafe.Pointer(lrPtr)) 387 } 388 } else { 389 if frame.lr == 0 { 390 lrPtr = frame.fp - retPCSize 391 frame.lr = *(*uintptr)(unsafe.Pointer(lrPtr)) 392 } 393 } 394 } 395 396 frame.varp = frame.fp 397 if !usesLR { 398 // On x86, call instruction pushes return PC before entering new function. 399 frame.varp -= retPCSize 400 } 401 402 // For architectures with frame pointers, if there's 403 // a frame, then there's a saved frame pointer here. 404 // 405 // NOTE: This code is not as general as it looks. 406 // On x86, the ABI is to save the frame pointer word at the 407 // top of the stack frame, so we have to back down over it. 408 // On arm64, the frame pointer should be at the bottom of 409 // the stack (with R29 (aka FP) = RSP), in which case we would 410 // not want to do the subtraction here. But we started out without 411 // any frame pointer, and when we wanted to add it, we didn't 412 // want to break all the assembly doing direct writes to 8(RSP) 413 // to set the first parameter to a called function. 414 // So we decided to write the FP link *below* the stack pointer 415 // (with R29 = RSP - 8 in Go functions). 416 // This is technically ABI-compatible but not standard. 417 // And it happens to end up mimicking the x86 layout. 418 // Other architectures may make different decisions. 419 if frame.varp > frame.sp && framepointer_enabled { 420 frame.varp -= goarch.PtrSize 421 } 422 423 frame.argp = frame.fp + sys.MinFrameSize 424 425 // Determine frame's 'continuation PC', where it can continue. 426 // Normally this is the return address on the stack, but if sigpanic 427 // is immediately below this function on the stack, then the frame 428 // stopped executing due to a trap, and frame.pc is probably not 429 // a safe point for looking up liveness information. In this panicking case, 430 // the function either doesn't return at all (if it has no defers or if the 431 // defers do not recover) or it returns from one of the calls to 432 // deferproc a second time (if the corresponding deferred func recovers). 433 // In the latter case, use a deferreturn call site as the continuation pc. 434 frame.continpc = frame.pc 435 if u.calleeFuncID == abi.FuncID_sigpanic { 436 if frame.fn.deferreturn != 0 { 437 frame.continpc = frame.fn.entry() + uintptr(frame.fn.deferreturn) + 1 438 // Note: this may perhaps keep return variables alive longer than 439 // strictly necessary, as we are using "function has a defer statement" 440 // as a proxy for "function actually deferred something". It seems 441 // to be a minor drawback. (We used to actually look through the 442 // gp._defer for a defer corresponding to this function, but that 443 // is hard to do with defer records on the stack during a stack copy.) 444 // Note: the +1 is to offset the -1 that 445 // (*stkframe).getStackMap does to back up a return 446 // address make sure the pc is in the CALL instruction. 447 } else { 448 frame.continpc = 0 449 } 450 } 451 } 452 453 func isInjectedCall(id abi.FuncID) bool { 454 return id == abi.FuncID_sigpanic || id == abi.FuncID_asyncPreempt || id == abi.FuncID_debugCallV2 455 } 456 457 func (u *unwinder) next() { 458 frame := &u.frame 459 f := frame.fn 460 gp := u.g.ptr() 461 462 // Do not unwind past the bottom of the stack. 463 if frame.lr == 0 { 464 u.finishInternal() 465 return 466 } 467 flr := findfunc(frame.lr) 468 if !flr.valid() { 469 // This happens if you get a profiling interrupt at just the wrong time. 470 fail := u.errFatal() 471 doPrint := u.flags&unwindSilentErrors == 0 472 if doPrint && gp.m != nil && gp.m.incgo && f.funcID == abi.FuncID_sigpanic { 473 // We can inject sigpanic 474 // calls directly into C code, 475 // in which case we'll see a C 476 // return PC. Don't complain. 477 doPrint = false 478 } 479 if fail || doPrint { 480 print("runtime: g ", gp.goid, ": unexpected return pc for ", funcname(f), " called from ", hex(frame.lr), "\n") 481 tracebackHexdump(gp.stack, frame, 0) 482 } 483 if fail { 484 throw("unknown caller pc") 485 } 486 frame.lr = 0 487 u.finishInternal() 488 return 489 } 490 491 if frame.pc == frame.lr && frame.sp == frame.fp { 492 // If the next frame is identical to the current frame, we cannot make 493 // progress, like the invalid-caller-PC case above. A stuck frame does not 494 // always mean the stack is corrupt: a signal can land in machine code the 495 // runtime has no unwind information for, such as a JIT or an assembly blob 496 // entered by a jump from a frameless Go symbol, whose prologue leaves 497 // pc == lr and sp == fp. Such generated machine code is an ABI violation, 498 // but does not imply the stack is corrupt. Do not unwind, because no 499 // amount of unwinding can recover that failure class. 500 fail := u.errFatal() 501 if fail || u.flags&unwindSilentErrors == 0 { 502 print("runtime: traceback stuck. pc=", hex(frame.pc), " sp=", hex(frame.sp), "\n") 503 tracebackHexdump(gp.stack, frame, frame.sp) 504 } 505 if fail { 506 throw("traceback stuck") 507 } 508 frame.lr = 0 509 u.finishInternal() 510 return 511 } 512 513 injectedCall := isInjectedCall(f.funcID) 514 if injectedCall { 515 u.flags |= unwindTrap 516 } else { 517 u.flags &^= unwindTrap 518 } 519 520 // Unwind to next frame. 521 u.calleeFuncID = f.funcID 522 frame.fn = flr 523 frame.pc = frame.lr 524 frame.lr = 0 525 frame.sp = frame.fp 526 frame.fp = 0 527 528 // On link register architectures, sighandler saves the LR on stack 529 // before faking a call. 530 if usesLR && injectedCall { 531 x := *(*uintptr)(unsafe.Pointer(frame.sp)) 532 // same as the size bump used in scanframeworker. 533 frame.sp += alignUp(sys.MinFrameSize, sys.StackAlign) 534 f = findfunc(frame.pc) 535 frame.fn = f 536 if !f.valid() { 537 frame.pc = x 538 } else if funcspdelta(f, frame.pc) == 0 { 539 frame.lr = x 540 } 541 } 542 543 u.resolveInternal(false, false) 544 } 545 546 // finishInternal is an unwinder-internal helper called after the stack has been 547 // exhausted. It sets the unwinder to an invalid state and checks that it 548 // successfully unwound the entire stack. 549 func (u *unwinder) finishInternal() { 550 u.frame.pc = 0 551 552 // Note that panic != nil is okay here: there can be leftover panics, 553 // because the defers on the panic stack do not nest in frame order as 554 // they do on the defer stack. If you have: 555 // 556 // frame 1 defers d1 557 // frame 2 defers d2 558 // frame 3 defers d3 559 // frame 4 panics 560 // frame 4's panic starts running defers 561 // frame 5, running d3, defers d4 562 // frame 5 panics 563 // frame 5's panic starts running defers 564 // frame 6, running d4, garbage collects 565 // frame 6, running d2, garbage collects 566 // 567 // During the execution of d4, the panic stack is d4 -> d3, which 568 // is nested properly, and we'll treat frame 3 as resumable, because we 569 // can find d3. (And in fact frame 3 is resumable. If d4 recovers 570 // and frame 5 continues running, d3, d3 can recover and we'll 571 // resume execution in (returning from) frame 3.) 572 // 573 // During the execution of d2, however, the panic stack is d2 -> d3, 574 // which is inverted. The scan will match d2 to frame 2 but having 575 // d2 on the stack until then means it will not match d3 to frame 3. 576 // This is okay: if we're running d2, then all the defers after d2 have 577 // completed and their corresponding frames are dead. Not finding d3 578 // for frame 3 means we'll set frame 3's continpc == 0, which is correct 579 // (frame 3 is dead). At the end of the walk the panic stack can thus 580 // contain defers (d3 in this case) for dead frames. The inversion here 581 // always indicates a dead frame, and the effect of the inversion on the 582 // scan is to hide those dead frames, so the scan is still okay: 583 // what's left on the panic stack are exactly (and only) the dead frames. 584 // 585 // We require callback != nil here because only when callback != nil 586 // do we know that gentraceback is being called in a "must be correct" 587 // context as opposed to a "best effort" context. The tracebacks with 588 // callbacks only happen when everything is stopped nicely. 589 // At other times, such as when gathering a stack for a profiling signal 590 // or when printing a traceback during a crash, everything may not be 591 // stopped nicely, and the stack walk may not be able to complete. 592 gp := u.g.ptr() 593 if u.flags&(unwindPrintErrors|unwindSilentErrors) == 0 && u.frame.sp != gp.stktopsp { 594 print("runtime: g", gp.goid, ": frame.sp=", hex(u.frame.sp), " top=", hex(gp.stktopsp), "\n") 595 print("\tstack=[", hex(gp.stack.lo), "-", hex(gp.stack.hi), "\n") 596 throw("traceback did not unwind completely") 597 } 598 } 599 600 // symPC returns the PC that should be used for symbolizing the current frame. 601 // Specifically, this is the PC of the last instruction executed in this frame. 602 // 603 // If this frame did a normal call, then frame.pc is a return PC, so this will 604 // return frame.pc-1, which points into the CALL instruction. If the frame was 605 // interrupted by a signal (e.g., profiler, segv, etc) then frame.pc is for the 606 // trapped instruction, so this returns frame.pc. See issue #34123. Finally, 607 // frame.pc can be at function entry when the frame is initialized without 608 // actually running code, like in runtime.mstart, in which case this returns 609 // frame.pc because that's the best we can do. 610 func (u *unwinder) symPC() uintptr { 611 if u.flags&unwindTrap == 0 && u.frame.pc > u.frame.fn.entry() { 612 // Regular call. 613 return u.frame.pc - 1 614 } 615 // Trapping instruction or we're at the function entry point. 616 return u.frame.pc 617 } 618 619 // cgoCallers populates pcBuf with the cgo callers of the current frame using 620 // the registered cgo unwinder. It returns the number of PCs written to pcBuf. 621 // If the current frame is not a cgo frame or if there's no registered cgo 622 // unwinder, it returns 0. 623 func (u *unwinder) cgoCallers(pcBuf []uintptr) int { 624 if !cgoTracebackAvailable() || u.frame.fn.funcID != abi.FuncID_cgocallback || u.cgoCtxt < 0 { 625 // We don't have a cgo unwinder (typical case), or we do but we're not 626 // in a cgo frame or we're out of cgo context. 627 return 0 628 } 629 630 ctxt := u.g.ptr().cgoCtxt[u.cgoCtxt] 631 u.cgoCtxt-- 632 cgoContextPCs(ctxt, pcBuf) 633 for i, pc := range pcBuf { 634 if pc == 0 { 635 return i 636 } 637 } 638 return len(pcBuf) 639 } 640 641 // tracebackPCs populates pcBuf with the return addresses for each frame from u 642 // and returns the number of PCs written to pcBuf. The returned PCs correspond 643 // to "logical frames" rather than "physical frames"; that is if A is inlined 644 // into B, this will still return a PCs for both A and B. This also includes PCs 645 // generated by the cgo unwinder, if one is registered. 646 // 647 // If skip != 0, this skips this many logical frames. 648 // 649 // Callers should set the unwindSilentErrors flag on u. 650 func tracebackPCs(u *unwinder, skip int, pcBuf []uintptr) int { 651 var cgoBuf [32]uintptr 652 n := 0 653 for ; n < len(pcBuf) && u.valid(); u.next() { 654 f := u.frame.fn 655 cgoN := u.cgoCallers(cgoBuf[:]) 656 657 // TODO: Why does &u.cache cause u to escape? (Same in traceback2) 658 for iu, uf := newInlineUnwinder(f, u.symPC()); n < len(pcBuf) && uf.valid(); uf = iu.next(uf) { 659 sf := iu.srcFunc(uf) 660 if sf.funcID == abi.FuncIDWrapper && elideWrapperCalling(u.calleeFuncID) { 661 // ignore wrappers 662 } else if skip > 0 { 663 skip-- 664 } else { 665 // Callers expect the pc buffer to contain return addresses 666 // and do the -1 themselves, so we add 1 to the call pc to 667 // create a "return pc". Since there is no actual call, here 668 // "return pc" just means a pc you subtract 1 from to get 669 // the pc of the "call". The actual no-op we insert may or 670 // may not be 1 byte. 671 pcBuf[n] = uf.pc + 1 672 n++ 673 } 674 u.calleeFuncID = sf.funcID 675 } 676 // Add cgo frames (if we're done skipping over the requested number of 677 // Go frames). 678 if skip == 0 { 679 n += copy(pcBuf[n:], cgoBuf[:cgoN]) 680 } 681 } 682 return n 683 } 684 685 // printArgs prints function arguments in traceback. 686 func printArgs(f funcInfo, argp unsafe.Pointer, pc uintptr) { 687 p := (*[abi.TraceArgsMaxLen]uint8)(funcdata(f, abi.FUNCDATA_ArgInfo)) 688 if p == nil { 689 return 690 } 691 692 liveInfo := funcdata(f, abi.FUNCDATA_ArgLiveInfo) 693 liveIdx := pcdatavalue(f, abi.PCDATA_ArgLiveIndex, pc) 694 startOffset := uint8(0xff) // smallest offset that needs liveness info (slots with a lower offset is always live) 695 if liveInfo != nil { 696 startOffset = *(*uint8)(liveInfo) 697 } 698 699 isLive := func(off, slotIdx uint8) bool { 700 if liveInfo == nil || liveIdx <= 0 { 701 return true // no liveness info, always live 702 } 703 if off < startOffset { 704 return true 705 } 706 bits := *(*uint8)(add(liveInfo, uintptr(liveIdx)+uintptr(slotIdx/8))) 707 return bits&(1<<(slotIdx%8)) != 0 708 } 709 710 print1 := func(off, sz, slotIdx uint8) { 711 x := readUnaligned64(add(argp, uintptr(off))) 712 // mask out irrelevant bits 713 if sz < 8 { 714 shift := 64 - sz*8 715 if goarch.BigEndian { 716 x = x >> shift 717 } else { 718 x = x << shift >> shift 719 } 720 } 721 print(hex(x)) 722 if !isLive(off, slotIdx) { 723 print("?") 724 } 725 } 726 727 start := true 728 printcomma := func() { 729 if !start { 730 print(", ") 731 } 732 } 733 pi := 0 734 slotIdx := uint8(0) // register arg spill slot index 735 printloop: 736 for { 737 o := p[pi] 738 pi++ 739 switch o { 740 case abi.TraceArgsEndSeq: 741 break printloop 742 case abi.TraceArgsStartAgg: 743 printcomma() 744 print("{") 745 start = true 746 continue 747 case abi.TraceArgsEndAgg: 748 print("}") 749 case abi.TraceArgsDotdotdot: 750 printcomma() 751 print("...") 752 case abi.TraceArgsOffsetTooLarge: 753 printcomma() 754 print("_") 755 default: 756 printcomma() 757 sz := p[pi] 758 pi++ 759 print1(o, sz, slotIdx) 760 if o >= startOffset { 761 slotIdx++ 762 } 763 } 764 start = false 765 } 766 } 767 768 // funcNamePiecesForPrint returns the function name for printing to the user. 769 // It returns five pieces so it doesn't need an allocation for string 770 // concatenation. 771 func funcNamePiecesForPrint(name string) (string, string, string, string, string) { 772 // Replace the shape name in generic function with "...". 773 i := bytealg.IndexByteString(name, '[') 774 if i < 0 { 775 return name, "", "", "", "" 776 } 777 j := len(name) - 1 778 for name[j] != ']' { 779 j-- 780 } 781 if j <= i { 782 return name, "", "", "", "" 783 } 784 785 interior := name[i+1 : j] // '[' interior ']' 786 // This is an early-out to skip the more-detailed parsing that 787 // follows -- if there's no '[' in the interior, that implies 788 // (assuming balanced brackets) no ']' in the interior, and thus 789 // this will be the answer. If brackets are not balanced 790 // (malformed input, which was already a risk), this will 791 // eat/hide the unbalanced "]". 792 if bytealg.IndexByteString(interior, '[') < 0 { 793 return name[:i], "[...]", name[j+1:], "", "" 794 } 795 // Generic method of generic type. 796 // know interior contains at least "...[..." 797 // expect interior contains "...]___[...". 798 // don't know whether "..." contains balanced brackets or not. 799 // or the compiler might have a bug in its naming-things department. 800 // hope to return name[:i], "[...]", ___, "[...]", name[j+1:] 801 depth := 1 // beginning after first "[", looking for balancing "]" 802 rbr, lbr := -1, -1 803 for k, c := range interior { 804 if c == '[' { 805 depth++ 806 if depth != 1 { 807 continue 808 } 809 // rbr != -1 because rbr is only assigned if depth == 0 810 lbr = k 811 break // success, depth == 1, rbr >= 0, lbr > rbr 812 } 813 if c == ']' { 814 depth-- 815 if depth < 0 { 816 break // malformed "...]...]" 817 } 818 if depth != 0 { 819 continue 820 } 821 // cannot execute this twice; depth == 0 -> { ']' -> malformed, '[' -> success } 822 rbr = k 823 } 824 } 825 if depth == 1 { 826 if rbr >= 0 && lbr > rbr { 827 return name[:i], "[...]", interior[rbr+1 : lbr], "[...]", name[j+1:] 828 } 829 if rbr == -1 && lbr == -1 { 830 // the bracket seen in the interior must have been balanced in a "[]" pattern, not "][" 831 // return the single-brackets (not a generic method of a generic type) result 832 return name[:i], "[...]", name[j+1:], "", "" 833 } 834 } 835 836 // malformed, return the whole name 837 return name, "", "", "", "" 838 839 } 840 841 // funcNameForPrint returns the function name for printing to the user. 842 func funcNameForPrint(name string) string { 843 a, b, c, d, e := funcNamePiecesForPrint(name) 844 return a + b + c + d + e 845 } 846 847 // printFuncName prints a function name. name is the function name in 848 // the binary's func data table. 849 func printFuncName(name string) { 850 if name == "runtime.gopanic" { 851 print("panic") 852 return 853 } 854 a, b, c, d, e := funcNamePiecesForPrint(name) 855 print(a, b, c, d, e) 856 } 857 858 func printcreatedby(gp *g) { 859 // Show what created goroutine, except main goroutine (goid 1). 860 pc := gp.gopc 861 f := findfunc(pc) 862 if f.valid() && showframe(f.srcFunc(), gp, false, abi.FuncIDNormal) && gp.goid != 1 { 863 printcreatedby1(f, pc, gp.parentGoid) 864 } 865 } 866 867 func printcreatedby1(f funcInfo, pc uintptr, goid uint64) { 868 print("created by ") 869 printFuncName(funcname(f)) 870 if goid != 0 { 871 print(" in goroutine ", goid) 872 } 873 print("\n") 874 tracepc := pc // back up to CALL instruction for funcline. 875 if pc > f.entry() { 876 tracepc -= sys.PCQuantum 877 } 878 file, line := funcline(f, tracepc) 879 print("\t", file, ":", line) 880 if pc > f.entry() { 881 print(" +", hex(pc-f.entry())) 882 } 883 print("\n") 884 } 885 886 func traceback(pc, sp, lr uintptr, gp *g) { 887 traceback1(pc, sp, lr, gp, 0) 888 } 889 890 // tracebacktrap is like traceback but expects that the PC and SP were obtained 891 // from a trap, not from gp->sched or gp->syscallpc/gp->syscallsp or GetCallerPC/GetCallerSP. 892 // Because they are from a trap instead of from a saved pair, 893 // the initial PC must not be rewound to the previous instruction. 894 // (All the saved pairs record a PC that is a return address, so we 895 // rewind it into the CALL instruction.) 896 // If gp.m.libcall{g,pc,sp} information is available, it uses that information in preference to 897 // the pc/sp/lr passed in. 898 func tracebacktrap(pc, sp, lr uintptr, gp *g) { 899 if gp.m.libcallsp != 0 { 900 // We're in C code somewhere, traceback from the saved position. 901 traceback1(gp.m.libcallpc, gp.m.libcallsp, 0, gp.m.libcallg.ptr(), 0) 902 return 903 } 904 traceback1(pc, sp, lr, gp, unwindTrap) 905 } 906 907 func traceback1(pc, sp, lr uintptr, gp *g, flags unwindFlags) { 908 // If the goroutine is in cgo, and we have a cgo traceback, print that. 909 if iscgo && gp.m != nil && gp.m.ncgo > 0 && gp.syscallsp != 0 && gp.m.cgoCallers != nil && gp.m.cgoCallers[0] != 0 { 910 // Lock cgoCallers so that a signal handler won't 911 // change it, copy the array, reset it, unlock it. 912 // We are locked to the thread and are not running 913 // concurrently with a signal handler. 914 // We just have to stop a signal handler from interrupting 915 // in the middle of our copy. 916 gp.m.cgoCallersUse.Store(1) 917 cgoCallers := *gp.m.cgoCallers 918 gp.m.cgoCallers[0] = 0 919 gp.m.cgoCallersUse.Store(0) 920 921 printCgoTraceback(&cgoCallers) 922 } 923 924 if readgstatus(gp)&^_Gscan == _Gsyscall { 925 // Override registers if blocked in system call. 926 pc = gp.syscallpc 927 sp = gp.syscallsp 928 flags &^= unwindTrap 929 } 930 if gp.m != nil && gp.m.vdsoSP != 0 { 931 // Override registers if running in VDSO. This comes after the 932 // _Gsyscall check to cover VDSO calls after entersyscall. 933 pc = gp.m.vdsoPC 934 sp = gp.m.vdsoSP 935 flags &^= unwindTrap 936 } 937 938 // Print traceback. 939 // 940 // We print the first tracebackInnerFrames frames, and the last 941 // tracebackOuterFrames frames. There are many possible approaches to this. 942 // There are various complications to this: 943 // 944 // - We'd prefer to walk the stack once because in really bad situations 945 // traceback may crash (and we want as much output as possible) or the stack 946 // may be changing. 947 // 948 // - Each physical frame can represent several logical frames, so we might 949 // have to pause in the middle of a physical frame and pick up in the middle 950 // of a physical frame. 951 // 952 // - The cgo symbolizer can expand a cgo PC to more than one logical frame, 953 // and involves juggling state on the C side that we don't manage. Since its 954 // expansion state is managed on the C side, we can't capture the expansion 955 // state part way through, and because the output strings are managed on the 956 // C side, we can't capture the output. Thus, our only choice is to replay a 957 // whole expansion, potentially discarding some of it. 958 // 959 // Rejected approaches: 960 // 961 // - Do two passes where the first pass just counts and the second pass does 962 // all the printing. This is undesirable if the stack is corrupted or changing 963 // because we won't see a partial stack if we panic. 964 // 965 // - Keep a ring buffer of the last N logical frames and use this to print 966 // the bottom frames once we reach the end of the stack. This works, but 967 // requires keeping a surprising amount of state on the stack, and we have 968 // to run the cgo symbolizer twice—once to count frames, and a second to 969 // print them—since we can't retain the strings it returns. 970 // 971 // Instead, we print the outer frames, and if we reach that limit, we clone 972 // the unwinder, count the remaining frames, and then skip forward and 973 // finish printing from the clone. This makes two passes over the outer part 974 // of the stack, but the single pass over the inner part ensures that's 975 // printed immediately and not revisited. It keeps minimal state on the 976 // stack. And through a combination of skip counts and limits, we can do all 977 // of the steps we need with a single traceback printer implementation. 978 // 979 // We could be more lax about exactly how many frames we print, for example 980 // always stopping and resuming on physical frame boundaries, or at least 981 // cgo expansion boundaries. It's not clear that's much simpler. 982 flags |= unwindPrintErrors 983 var u unwinder 984 tracebackWithRuntime := func(showRuntime bool) int { 985 const maxInt int = 0x7fffffff 986 u.initAt(pc, sp, lr, gp, flags) 987 n, lastN := traceback2(&u, showRuntime, 0, tracebackInnerFrames) 988 if n < tracebackInnerFrames { 989 // We printed the whole stack. 990 return n 991 } 992 // Clone the unwinder and figure out how many frames are left. This 993 // count will include any logical frames already printed for u's current 994 // physical frame. 995 u2 := u 996 remaining, _ := traceback2(&u, showRuntime, maxInt, 0) 997 elide := remaining - lastN - tracebackOuterFrames 998 if elide > 0 { 999 print("...", elide, " frames elided...\n") 1000 traceback2(&u2, showRuntime, lastN+elide, tracebackOuterFrames) 1001 } else if elide <= 0 { 1002 // There are tracebackOuterFrames or fewer frames left to print. 1003 // Just print the rest of the stack. 1004 traceback2(&u2, showRuntime, lastN, tracebackOuterFrames) 1005 } 1006 return n 1007 } 1008 // By default, omits runtime frames. If that means we print nothing at all, 1009 // repeat forcing all frames printed. 1010 if tracebackWithRuntime(false) == 0 { 1011 tracebackWithRuntime(true) 1012 } 1013 printcreatedby(gp) 1014 1015 if gp.ancestors == nil { 1016 return 1017 } 1018 for _, ancestor := range *gp.ancestors { 1019 printAncestorTraceback(ancestor) 1020 } 1021 } 1022 1023 // traceback2 prints a stack trace starting at u. It skips the first "skip" 1024 // logical frames, after which it prints at most "max" logical frames. It 1025 // returns n, which is the number of logical frames skipped and printed, and 1026 // lastN, which is the number of logical frames skipped or printed just in the 1027 // physical frame that u references. 1028 func traceback2(u *unwinder, showRuntime bool, skip, max int) (n, lastN int) { 1029 // commitFrame commits to a logical frame and returns whether this frame 1030 // should be printed and whether iteration should stop. 1031 commitFrame := func() (pr, stop bool) { 1032 if skip == 0 && max == 0 { 1033 // Stop 1034 return false, true 1035 } 1036 n++ 1037 lastN++ 1038 if skip > 0 { 1039 // Skip 1040 skip-- 1041 return false, false 1042 } 1043 // Print 1044 max-- 1045 return true, false 1046 } 1047 1048 gp := u.g.ptr() 1049 level, _, _ := gotraceback() 1050 var cgoBuf [32]uintptr 1051 for ; u.valid(); u.next() { 1052 lastN = 0 1053 f := u.frame.fn 1054 for iu, uf := newInlineUnwinder(f, u.symPC()); uf.valid(); uf = iu.next(uf) { 1055 sf := iu.srcFunc(uf) 1056 callee := u.calleeFuncID 1057 u.calleeFuncID = sf.funcID 1058 if !(showRuntime || showframe(sf, gp, n == 0, callee)) { 1059 continue 1060 } 1061 1062 if pr, stop := commitFrame(); stop { 1063 return 1064 } else if !pr { 1065 continue 1066 } 1067 1068 name := sf.name() 1069 file, line := iu.fileLine(uf) 1070 // Print during crash. 1071 // main(0x1, 0x2, 0x3) 1072 // /home/rsc/go/src/runtime/x.go:23 +0xf 1073 // 1074 printFuncName(name) 1075 print("(") 1076 if iu.isInlined(uf) { 1077 print("...") 1078 } else { 1079 argp := unsafe.Pointer(u.frame.argp) 1080 printArgs(f, argp, u.symPC()) 1081 } 1082 print(")\n") 1083 print("\t", file, ":", line) 1084 if !iu.isInlined(uf) { 1085 if u.frame.pc > f.entry() { 1086 print(" +", hex(u.frame.pc-f.entry())) 1087 } 1088 if gp.m != nil && gp.m.throwing >= throwTypeRuntime && gp == gp.m.curg || level >= 2 { 1089 print(" fp=", hex(u.frame.fp), " sp=", hex(u.frame.sp), " pc=", hex(u.frame.pc)) 1090 } 1091 } 1092 print("\n") 1093 } 1094 1095 // Print cgo frames. 1096 if cgoN := u.cgoCallers(cgoBuf[:]); cgoN > 0 { 1097 var arg cgoSymbolizerArg 1098 anySymbolized := false 1099 stop := false 1100 for _, pc := range cgoBuf[:cgoN] { 1101 if !cgoSymbolizerAvailable() { 1102 if pr, stop := commitFrame(); stop { 1103 break 1104 } else if pr { 1105 print("non-Go function at pc=", hex(pc), "\n") 1106 } 1107 } else { 1108 stop = printOneCgoTraceback(pc, commitFrame, &arg) 1109 anySymbolized = true 1110 if stop { 1111 break 1112 } 1113 } 1114 } 1115 if anySymbolized { 1116 // Free symbolization state. 1117 arg.pc = 0 1118 callCgoSymbolizer(&arg) 1119 } 1120 if stop { 1121 return 1122 } 1123 } 1124 } 1125 return n, 0 1126 } 1127 1128 // printAncestorTraceback prints the traceback of the given ancestor. 1129 // TODO: Unify this with gentraceback and CallersFrames. 1130 func printAncestorTraceback(ancestor ancestorInfo) { 1131 print("[originating from goroutine ", ancestor.goid, "]:\n") 1132 for fidx, pc := range ancestor.pcs { 1133 f := findfunc(pc) // f previously validated 1134 if showfuncinfo(f.srcFunc(), fidx == 0, abi.FuncIDNormal) { 1135 printAncestorTracebackFuncInfo(f, pc) 1136 } 1137 } 1138 if len(ancestor.pcs) == tracebackInnerFrames { 1139 print("...additional frames elided...\n") 1140 } 1141 // Show what created goroutine, except main goroutine (goid 1). 1142 f := findfunc(ancestor.gopc) 1143 if f.valid() && showfuncinfo(f.srcFunc(), false, abi.FuncIDNormal) && ancestor.goid != 1 { 1144 // In ancestor mode, we'll already print the goroutine ancestor. 1145 // Pass 0 for the goid parameter so we don't print it again. 1146 printcreatedby1(f, ancestor.gopc, 0) 1147 } 1148 } 1149 1150 // printAncestorTracebackFuncInfo prints the given function info at a given pc 1151 // within an ancestor traceback. The precision of this info is reduced 1152 // due to only have access to the pcs at the time of the caller 1153 // goroutine being created. 1154 func printAncestorTracebackFuncInfo(f funcInfo, pc uintptr) { 1155 u, uf := newInlineUnwinder(f, pc) 1156 file, line := u.fileLine(uf) 1157 printFuncName(u.srcFunc(uf).name()) 1158 print("(...)\n") 1159 print("\t", file, ":", line) 1160 if pc > f.entry() { 1161 print(" +", hex(pc-f.entry())) 1162 } 1163 print("\n") 1164 } 1165 1166 // callers should be an internal detail, 1167 // (and is almost identical to Callers), 1168 // but widely used packages access it using linkname. 1169 // Notable members of the hall of shame include: 1170 // - github.com/phuslu/log 1171 // 1172 // Do not remove or change the type signature. 1173 // See go.dev/issue/67401. 1174 // 1175 //go:linkname callers 1176 func callers(skip int, pcbuf []uintptr) int { 1177 sp := sys.GetCallerSP() 1178 pc := sys.GetCallerPC() 1179 gp := getg() 1180 var n int 1181 systemstack(func() { 1182 var u unwinder 1183 u.initAt(pc, sp, 0, gp, unwindSilentErrors) 1184 n = tracebackPCs(&u, skip, pcbuf) 1185 }) 1186 return n 1187 } 1188 1189 func gcallers(gp *g, skip int, pcbuf []uintptr) int { 1190 var u unwinder 1191 u.init(gp, unwindSilentErrors) 1192 return tracebackPCs(&u, skip, pcbuf) 1193 } 1194 1195 // showframe reports whether the frame with the given characteristics should 1196 // be printed during a traceback. 1197 func showframe(sf srcFunc, gp *g, firstFrame bool, calleeID abi.FuncID) bool { 1198 mp := getg().m 1199 if mp.throwing >= throwTypeRuntime && gp != nil && (gp == mp.curg || gp == mp.caughtsig.ptr()) { 1200 return true 1201 } 1202 return showfuncinfo(sf, firstFrame, calleeID) 1203 } 1204 1205 // showfuncinfo reports whether a function with the given characteristics should 1206 // be printed during a traceback. 1207 func showfuncinfo(sf srcFunc, firstFrame bool, calleeID abi.FuncID) bool { 1208 level, _, _ := gotraceback() 1209 if level > 1 { 1210 // Show all frames. 1211 return true 1212 } 1213 1214 if sf.funcID == abi.FuncIDWrapper && elideWrapperCalling(calleeID) { 1215 return false 1216 } 1217 1218 // Always show runtime.runFinalizers and runtime.runCleanups as 1219 // context that this goroutine is running finalizers or cleanups, 1220 // otherwise there is no obvious indicator. 1221 // 1222 // TODO(prattmic): A more general approach would be to always show the 1223 // outermost frame (besides runtime.goexit), even if it is a runtime. 1224 // Hiding the outermost frame allows the apparent outermost frame to 1225 // change across different traces, which seems impossible. 1226 // 1227 // Unfortunately, implementing this requires looking ahead at the next 1228 // frame, which goes against traceback's incremental approach (see big 1229 // comment in traceback1). 1230 if sf.funcID == abi.FuncID_runFinalizers || sf.funcID == abi.FuncID_runCleanups { 1231 return true 1232 } 1233 1234 name := sf.name() 1235 1236 // Special case: always show runtime.gopanic frame 1237 // in the middle of a stack trace, so that we can 1238 // see the boundary between ordinary code and 1239 // panic-induced deferred code. 1240 // See golang.org/issue/5832. 1241 if name == "runtime.gopanic" && !firstFrame { 1242 return true 1243 } 1244 1245 return bytealg.IndexByteString(name, '.') >= 0 && (!stringslite.HasPrefix(name, "runtime.") || isExportedRuntime(name)) 1246 } 1247 1248 // isExportedRuntime reports whether name is an exported runtime function. 1249 // It is only for runtime functions, so ASCII A-Z is fine. 1250 func isExportedRuntime(name string) bool { 1251 // Check and remove package qualifier. 1252 name, found := stringslite.CutPrefix(name, "runtime.") 1253 if !found { 1254 return false 1255 } 1256 rcvr := "" 1257 1258 // Extract receiver type, if any. 1259 // For example, runtime.(*Func).Entry 1260 i := len(name) - 1 1261 for i >= 0 && name[i] != '.' { 1262 i-- 1263 } 1264 if i >= 0 { 1265 rcvr = name[:i] 1266 name = name[i+1:] 1267 // Remove parentheses and star for pointer receivers. 1268 if len(rcvr) >= 3 && rcvr[0] == '(' && rcvr[1] == '*' && rcvr[len(rcvr)-1] == ')' { 1269 rcvr = rcvr[2 : len(rcvr)-1] 1270 } 1271 } 1272 1273 // Exported functions and exported methods on exported types. 1274 return len(name) > 0 && 'A' <= name[0] && name[0] <= 'Z' && (len(rcvr) == 0 || 'A' <= rcvr[0] && rcvr[0] <= 'Z') 1275 } 1276 1277 // elideWrapperCalling reports whether a wrapper function that called 1278 // function id should be elided from stack traces. 1279 func elideWrapperCalling(id abi.FuncID) bool { 1280 // If the wrapper called a panic function instead of the 1281 // wrapped function, we want to include it in stacks. 1282 return !(id == abi.FuncID_gopanic || id == abi.FuncID_sigpanic || id == abi.FuncID_panicwrap) 1283 } 1284 1285 var gStatusStrings = [...]string{ 1286 _Gidle: "idle", 1287 _Grunnable: "runnable", 1288 _Grunning: "running", 1289 _Gsyscall: "syscall", 1290 _Gwaiting: "waiting", 1291 _Gdead: "dead", 1292 _Gcopystack: "copystack", 1293 _Gleaked: "leaked", 1294 _Gpreempted: "preempted", 1295 _Gdeadextra: "waiting for cgo callback", 1296 } 1297 1298 func goroutineheader(gp *g) { 1299 level, _, _ := gotraceback() 1300 1301 gpstatus := readgstatus(gp) 1302 1303 isScan := gpstatus&_Gscan != 0 1304 gpstatus &^= _Gscan // drop the scan bit 1305 1306 // Basic string status 1307 var status string 1308 if 0 <= gpstatus && gpstatus < uint32(len(gStatusStrings)) { 1309 status = gStatusStrings[gpstatus] 1310 } else { 1311 status = "???" 1312 } 1313 1314 // Override. 1315 if (gpstatus == _Gwaiting || gpstatus == _Gleaked) && gp.waitreason != waitReasonZero { 1316 status = gp.waitreason.String() 1317 } 1318 1319 // approx time the G is blocked, in minutes 1320 var waitfor int64 1321 if (gpstatus == _Gwaiting || gpstatus == _Gsyscall) && gp.waitsince != 0 { 1322 waitfor = (nanotime() - gp.waitsince) / 60e9 1323 } 1324 print("goroutine ", gp.goid) 1325 if gp.m != nil && gp.m.throwing >= throwTypeRuntime && gp == gp.m.curg || level >= 2 { 1326 print(" gp=", gp) 1327 if gp.m != nil { 1328 print(" m=", gp.m.id, " mp=", gp.m) 1329 } else { 1330 print(" m=nil") 1331 } 1332 } 1333 print(" [", status) 1334 if gpstatus == _Gleaked { 1335 print(" (leaked)") 1336 } 1337 if isScan { 1338 print(" (scan)") 1339 } 1340 if bubble := gp.bubble; bubble != nil && 1341 gpstatus == _Gwaiting && 1342 gp.waitreason.isIdleInSynctest() && 1343 !stringslite.HasSuffix(status, "(durable)") { 1344 // If this isn't a status where the name includes a (durable) 1345 // suffix to distinguish it from the non-durable form, add it here. 1346 print(" (durable)") 1347 } 1348 if waitfor >= 1 { 1349 print(", ", waitfor, " minutes") 1350 } 1351 if gp.lockedm != 0 { 1352 print(", locked to thread") 1353 } 1354 if bubble := gp.bubble; bubble != nil { 1355 print(", synctest bubble ", bubble.id) 1356 } 1357 print("]") 1358 if gp.labels != nil && debug.tracebacklabels.Load() == 1 { 1359 labels := (*label.Set)(gp.labels).List 1360 if len(labels) > 0 { 1361 print(" {") 1362 for i, kv := range labels { 1363 // Try to be nice and only quote the keys/values if one of them has characters that need quoting or escaping. 1364 printq := func(s string) { 1365 if tracebackStringNeedsQuoting(s) { 1366 print(quoted(s)) 1367 } else { 1368 print(s) 1369 } 1370 } 1371 printq(kv.Key) 1372 print(": ") 1373 printq(kv.Value) 1374 if i < len(labels)-1 { 1375 print(", ") 1376 } 1377 } 1378 print("}") 1379 } 1380 } 1381 print(":\n") 1382 } 1383 1384 func tracebackStringNeedsQuoting(s string) bool { 1385 for _, r := range s { 1386 if !('a' <= r && r <= 'z' || 1387 'A' <= r && r <= 'Z' || 1388 '0' <= r && r <= '9' || 1389 r == '.' || r == '/' || r == '_') { 1390 return true 1391 } 1392 } 1393 return false 1394 } 1395 1396 func tracebackothers(me *g) { 1397 tracebacksomeothers(me, func(*g) bool { return true }) 1398 } 1399 1400 func tracebacksomeothers(me *g, showf func(*g) bool) { 1401 level, _, _ := gotraceback() 1402 1403 // Show the current goroutine first, if we haven't already. 1404 curgp := getg().m.curg 1405 if curgp != nil && curgp != me { 1406 print("\n") 1407 goroutineheader(curgp) 1408 traceback(^uintptr(0), ^uintptr(0), 0, curgp) 1409 } 1410 1411 // We can't call locking forEachG here because this may be during fatal 1412 // throw/panic, where locking could be out-of-order or a direct 1413 // deadlock. 1414 // 1415 // Instead, use forEachGRace, which requires no locking. We don't lock 1416 // against concurrent creation of new Gs, but even with allglock we may 1417 // miss Gs created after this loop. 1418 forEachGRace(func(gp *g) { 1419 if gp == me || gp == curgp { 1420 return 1421 } 1422 if status := readgstatus(gp); status == _Gdead || status == _Gdeadextra { 1423 return 1424 } 1425 if !showf(gp) { 1426 return 1427 } 1428 if isSystemGoroutine(gp, false) && level < 2 { 1429 return 1430 } 1431 print("\n") 1432 goroutineheader(gp) 1433 // Note: gp.m == getg().m occurs when tracebackothers is called 1434 // from a signal handler initiated during a systemstack call. 1435 // The original G is still in the running state, and we want to 1436 // print its stack. 1437 // 1438 // There's a small window of time in exitsyscall where a goroutine could be 1439 // in _Grunning as it's exiting a syscall. This could be the case even if the 1440 // world is stopped or frozen. 1441 // 1442 // This is OK because the goroutine will not exit the syscall while the world 1443 // is stopped or frozen. This is also why it's safe to check syscallsp here, 1444 // and safe to take the goroutine's stack trace. The syscall path mutates 1445 // syscallsp only just before exiting the syscall. 1446 if gp.m != getg().m && readgstatus(gp)&^_Gscan == _Grunning && gp.syscallsp == 0 { 1447 print("\tgoroutine running on other thread; stack unavailable\n") 1448 printcreatedby(gp) 1449 } else { 1450 traceback(^uintptr(0), ^uintptr(0), 0, gp) 1451 } 1452 }) 1453 } 1454 1455 // tracebackHexdump hexdumps part of stk around frame.sp and frame.fp 1456 // for debugging purposes. If the address bad is included in the 1457 // hexdumped range, it will mark it as well. 1458 func tracebackHexdump(stk stack, frame *stkframe, bad uintptr) { 1459 const expand = 32 * goarch.PtrSize 1460 const maxExpand = 256 * goarch.PtrSize 1461 // Start around frame.sp. 1462 lo, hi := frame.sp, frame.sp 1463 // Expand to include frame.fp. 1464 if frame.fp != 0 && frame.fp < lo { 1465 lo = frame.fp 1466 } 1467 if frame.fp != 0 && frame.fp > hi { 1468 hi = frame.fp 1469 } 1470 // Expand a bit more. 1471 lo, hi = lo-expand, hi+expand 1472 // But don't go too far from frame.sp. 1473 if lo < frame.sp-maxExpand { 1474 lo = frame.sp - maxExpand 1475 } 1476 if hi > frame.sp+maxExpand { 1477 hi = frame.sp + maxExpand 1478 } 1479 // And don't go outside the stack bounds. 1480 if lo < stk.lo { 1481 lo = stk.lo 1482 } 1483 if hi > stk.hi { 1484 hi = stk.hi 1485 } 1486 1487 // Print the hex dump. 1488 print("stack: frame={sp:", hex(frame.sp), ", fp:", hex(frame.fp), "} stack=[", hex(stk.lo), ",", hex(stk.hi), ")\n") 1489 hexdumpWords(lo, hi-lo, func(p uintptr, m hexdumpMarker) { 1490 if p == frame.fp { 1491 m.start() 1492 println("FP") 1493 } 1494 if p == frame.sp { 1495 m.start() 1496 println("SP") 1497 } 1498 if p == bad { 1499 m.start() 1500 println("bad") 1501 } 1502 }) 1503 } 1504 1505 // isSystemGoroutine reports whether the goroutine g must be omitted 1506 // in stack dumps and deadlock detector. This is any goroutine that 1507 // starts at a runtime.* entry point, except for runtime.main, 1508 // runtime.handleAsyncEvent (wasm only) and sometimes 1509 // runtime.runFinalizers/runtime.runCleanups. 1510 // 1511 // If fixed is true, any goroutine that can vary between user and 1512 // system (that is, the finalizer goroutine) is considered a user 1513 // goroutine. 1514 func isSystemGoroutine(gp *g, fixed bool) bool { 1515 // Keep this in sync with internal/trace.IsSystemGoroutine. 1516 f := findfunc(gp.startpc) 1517 if !f.valid() { 1518 return false 1519 } 1520 if f.funcID == abi.FuncID_runtime_main || f.funcID == abi.FuncID_corostart || f.funcID == abi.FuncID_handleAsyncEvent { 1521 return false 1522 } 1523 if f.funcID == abi.FuncID_runFinalizers { 1524 // We include the finalizer goroutine if it's calling 1525 // back into user code. 1526 if fixed { 1527 // This goroutine can vary. In fixed mode, 1528 // always consider it a user goroutine. 1529 return false 1530 } 1531 return fingStatus.Load()&fingRunningFinalizer == 0 1532 } 1533 if f.funcID == abi.FuncID_runCleanups { 1534 // We include the cleanup goroutines if they're calling 1535 // back into user code. 1536 if fixed { 1537 // This goroutine can vary. In fixed mode, 1538 // always consider it a user goroutine. 1539 return false 1540 } 1541 return !gp.runningCleanups.Load() 1542 } 1543 return stringslite.HasPrefix(funcname(f), "runtime.") 1544 } 1545 1546 // SetCgoTraceback records three C functions to use to gather 1547 // traceback information from C code and to convert that traceback 1548 // information into symbolic information. These are used when printing 1549 // stack traces for a program that uses cgo. 1550 // 1551 // The traceback and context functions may be called from a signal 1552 // handler, and must therefore use only async-signal safe functions. 1553 // The symbolizer function may be called while the program is 1554 // crashing, and so must be cautious about using memory. None of the 1555 // functions may call back into Go. 1556 // 1557 // The context function will be called with a single argument, a 1558 // pointer to a struct: 1559 // 1560 // struct { 1561 // Context uintptr 1562 // } 1563 // 1564 // In C syntax, this struct will be 1565 // 1566 // struct { 1567 // uintptr_t Context; 1568 // }; 1569 // 1570 // If the Context field is 0, the context function is being called to 1571 // record the current traceback context. It should record in the 1572 // Context field whatever information is needed about the current 1573 // point of execution to later produce a stack trace, probably the 1574 // stack pointer and PC. In this case the context function will be 1575 // called from C code. 1576 // 1577 // If the Context field is not 0, then it is a value returned by a 1578 // previous call to the context function. This case is called when the 1579 // context is no longer needed; that is, when the Go code is returning 1580 // to its C code caller. This permits the context function to release 1581 // any associated resources. 1582 // 1583 // While it would be correct for the context function to record a 1584 // complete a stack trace whenever it is called, and simply copy that 1585 // out in the traceback function, in a typical program the context 1586 // function will be called many times without ever recording a 1587 // traceback for that context. Recording a complete stack trace in a 1588 // call to the context function is likely to be inefficient. 1589 // 1590 // The traceback function will be called with a single argument, a 1591 // pointer to a struct: 1592 // 1593 // struct { 1594 // Context uintptr 1595 // SigContext uintptr 1596 // Buf *uintptr 1597 // Max uintptr 1598 // } 1599 // 1600 // In C syntax, this struct will be 1601 // 1602 // struct { 1603 // uintptr_t Context; 1604 // uintptr_t SigContext; 1605 // uintptr_t* Buf; 1606 // uintptr_t Max; 1607 // }; 1608 // 1609 // The Context field will be zero to gather a traceback from the 1610 // current program execution point. In this case, the traceback 1611 // function will be called from C code. 1612 // 1613 // Otherwise Context will be a value previously returned by a call to 1614 // the context function. The traceback function should gather a stack 1615 // trace from that saved point in the program execution. The traceback 1616 // function may be called from an execution thread other than the one 1617 // that recorded the context, but only when the context is known to be 1618 // valid and unchanging. The traceback function may also be called 1619 // deeper in the call stack on the same thread that recorded the 1620 // context. The traceback function may be called multiple times with 1621 // the same Context value; it will usually be appropriate to cache the 1622 // result, if possible, the first time this is called for a specific 1623 // context value. 1624 // 1625 // If the traceback function is called from a signal handler on a Unix 1626 // system, SigContext will be the signal context argument passed to 1627 // the signal handler (a C ucontext_t* cast to uintptr_t). This may be 1628 // used to start tracing at the point where the signal occurred. If 1629 // the traceback function is not called from a signal handler, 1630 // SigContext will be zero. 1631 // 1632 // Buf is where the traceback information should be stored. It should 1633 // be PC values, such that Buf[0] is the PC of the caller, Buf[1] is 1634 // the PC of that function's caller, and so on. Max is the maximum 1635 // number of entries to store. The function should store a zero to 1636 // indicate the top of the stack, or that the caller is on a different 1637 // stack, presumably a Go stack. 1638 // 1639 // Unlike runtime.Callers, the PC values returned should, when passed 1640 // to the symbolizer function, return the file/line of the call 1641 // instruction. No additional subtraction is required or appropriate. 1642 // 1643 // On all platforms, the traceback function is invoked when a call from 1644 // Go to C to Go requests a stack trace. On linux/amd64, linux/ppc64le, 1645 // linux/arm64, and freebsd/amd64, the traceback function is also invoked 1646 // when a signal is received by a thread that is executing a cgo call. 1647 // The traceback function should not make assumptions about when it is 1648 // called, as future versions of Go may make additional calls. 1649 // 1650 // The symbolizer function will be called with a single argument, a 1651 // pointer to a struct: 1652 // 1653 // struct { 1654 // PC uintptr // program counter to fetch information for 1655 // File *byte // file name (NUL terminated) 1656 // Lineno uintptr // line number 1657 // Func *byte // function name (NUL terminated) 1658 // Entry uintptr // function entry point 1659 // More uintptr // set non-zero if more info for this PC 1660 // Data uintptr // unused by runtime, available for function 1661 // } 1662 // 1663 // In C syntax, this struct will be 1664 // 1665 // struct { 1666 // uintptr_t PC; 1667 // char* File; 1668 // uintptr_t Lineno; 1669 // char* Func; 1670 // uintptr_t Entry; 1671 // uintptr_t More; 1672 // uintptr_t Data; 1673 // }; 1674 // 1675 // The PC field will be a value returned by a call to the traceback 1676 // function. 1677 // 1678 // The first time the function is called for a particular traceback, 1679 // all the fields except PC will be 0. The function should fill in the 1680 // other fields if possible, setting them to 0/nil if the information 1681 // is not available. The Data field may be used to store any useful 1682 // information across calls. The More field should be set to non-zero 1683 // if there is more information for this PC, zero otherwise. If More 1684 // is set non-zero, the function will be called again with the same 1685 // PC, and may return different information (this is intended for use 1686 // with inlined functions). If More is zero, the function will be 1687 // called with the next PC value in the traceback. When the traceback 1688 // is complete, the function will be called once more with PC set to 1689 // zero; this may be used to free any information. Each call will 1690 // leave the fields of the struct set to the same values they had upon 1691 // return, except for the PC field when the More field is zero. The 1692 // function must not keep a copy of the struct pointer between calls. 1693 // 1694 // When calling SetCgoTraceback, the version argument is the version 1695 // number of the structs that the functions expect to receive. 1696 // Currently this must be zero. 1697 // 1698 // The symbolizer function may be nil, in which case the results of 1699 // the traceback function will be displayed as numbers. If the 1700 // traceback function is nil, the symbolizer function will never be 1701 // called. The context function may be nil, in which case the 1702 // traceback function will only be called with the context field set 1703 // to zero. If the context function is nil, then calls from Go to C 1704 // to Go will not show a traceback for the C portion of the call stack. 1705 // 1706 // SetCgoTraceback should be called only once, ideally from an init function. 1707 func SetCgoTraceback(version int, traceback, context, symbolizer unsafe.Pointer) { 1708 if version != 0 { 1709 panic("unsupported version") 1710 } 1711 1712 if cgoTraceback != nil && cgoTraceback != traceback || 1713 cgoContext != nil && cgoContext != context || 1714 cgoSymbolizer != nil && cgoSymbolizer != symbolizer { 1715 panic("call SetCgoTraceback only once") 1716 } 1717 1718 cgoTraceback = traceback 1719 cgoContext = context 1720 cgoSymbolizer = symbolizer 1721 1722 if _cgo_set_traceback_functions != nil { 1723 type cgoSetTracebackFunctionsArg struct { 1724 traceback unsafe.Pointer 1725 context unsafe.Pointer 1726 symbolizer unsafe.Pointer 1727 } 1728 arg := cgoSetTracebackFunctionsArg{ 1729 traceback: traceback, 1730 context: context, 1731 symbolizer: symbolizer, 1732 } 1733 cgocall(_cgo_set_traceback_functions, noescape(unsafe.Pointer(&arg))) 1734 } 1735 } 1736 1737 var cgoTraceback unsafe.Pointer 1738 var cgoContext unsafe.Pointer 1739 var cgoSymbolizer unsafe.Pointer 1740 1741 func cgoTracebackAvailable() bool { 1742 // - The traceback function must be registered via SetCgoTraceback. 1743 // - This must be a cgo binary (providing _cgo_call_traceback_function). 1744 return cgoTraceback != nil && _cgo_call_traceback_function != nil 1745 } 1746 1747 func cgoSymbolizerAvailable() bool { 1748 // - The symbolizer function must be registered via SetCgoTraceback. 1749 // - This must be a cgo binary (providing _cgo_call_symbolizer_function). 1750 return cgoSymbolizer != nil && _cgo_call_symbolizer_function != nil 1751 } 1752 1753 // cgoTracebackArg is the type passed to cgoTraceback. 1754 type cgoTracebackArg struct { 1755 context uintptr 1756 sigContext uintptr 1757 buf *uintptr 1758 max uintptr 1759 } 1760 1761 // cgoContextArg is the type passed to the context function. 1762 type cgoContextArg struct { 1763 context uintptr 1764 } 1765 1766 // cgoSymbolizerArg is the type passed to cgoSymbolizer. 1767 type cgoSymbolizerArg struct { 1768 pc uintptr 1769 file *byte 1770 lineno uintptr 1771 funcName *byte 1772 entry uintptr 1773 more uintptr 1774 data uintptr 1775 } 1776 1777 // printCgoTraceback prints a traceback of callers. 1778 func printCgoTraceback(callers *cgoCallers) { 1779 if !cgoSymbolizerAvailable() { 1780 for _, c := range callers { 1781 if c == 0 { 1782 break 1783 } 1784 print("non-Go function at pc=", hex(c), "\n") 1785 } 1786 return 1787 } 1788 1789 commitFrame := func() (pr, stop bool) { return true, false } 1790 var arg cgoSymbolizerArg 1791 for _, c := range callers { 1792 if c == 0 { 1793 break 1794 } 1795 printOneCgoTraceback(c, commitFrame, &arg) 1796 } 1797 arg.pc = 0 1798 callCgoSymbolizer(&arg) 1799 } 1800 1801 // printOneCgoTraceback prints the traceback of a single cgo caller. 1802 // This can print more than one line because of inlining. 1803 // It returns the "stop" result of commitFrame. 1804 // 1805 // Preconditions: cgoSymbolizerAvailable returns true. 1806 func printOneCgoTraceback(pc uintptr, commitFrame func() (pr, stop bool), arg *cgoSymbolizerArg) bool { 1807 arg.pc = pc 1808 for { 1809 if pr, stop := commitFrame(); stop { 1810 return true 1811 } else if !pr { 1812 continue 1813 } 1814 1815 callCgoSymbolizer(arg) 1816 if arg.funcName != nil { 1817 // Note that we don't print any argument 1818 // information here, not even parentheses. 1819 // The symbolizer must add that if appropriate. 1820 println(gostringnocopy(arg.funcName)) 1821 } else { 1822 println("non-Go function") 1823 } 1824 print("\t") 1825 if arg.file != nil { 1826 print(gostringnocopy(arg.file), ":", arg.lineno, " ") 1827 } 1828 print("pc=", hex(pc), "\n") 1829 if arg.more == 0 { 1830 return false 1831 } 1832 } 1833 } 1834 1835 // callCgoSymbolizer calls the cgoSymbolizer function. 1836 // 1837 // Preconditions: cgoSymbolizerAvailable returns true. 1838 func callCgoSymbolizer(arg *cgoSymbolizerArg) { 1839 call := cgocall 1840 if panicking.Load() > 0 || getg().m.curg != getg() { 1841 // We do not want to call into the scheduler when panicking 1842 // or when on the system stack. 1843 call = asmcgocall 1844 } 1845 if msanenabled { 1846 msanwrite(unsafe.Pointer(arg), unsafe.Sizeof(cgoSymbolizerArg{})) 1847 } 1848 if asanenabled { 1849 asanwrite(unsafe.Pointer(arg), unsafe.Sizeof(cgoSymbolizerArg{})) 1850 } 1851 call(_cgo_call_symbolizer_function, noescape(unsafe.Pointer(arg))) 1852 } 1853 1854 // cgoContextPCs gets the PC values from a cgo traceback. 1855 // 1856 // Preconditions: cgoTracebackAvailable returns true. 1857 func cgoContextPCs(ctxt uintptr, buf []uintptr) { 1858 call := cgocall 1859 if panicking.Load() > 0 || getg().m.curg != getg() { 1860 // We do not want to call into the scheduler when panicking 1861 // or when on the system stack. 1862 call = asmcgocall 1863 } 1864 arg := cgoTracebackArg{ 1865 context: ctxt, 1866 buf: (*uintptr)(noescape(unsafe.Pointer(&buf[0]))), 1867 max: uintptr(len(buf)), 1868 } 1869 if msanenabled { 1870 msanwrite(unsafe.Pointer(&arg), unsafe.Sizeof(arg)) 1871 } 1872 if asanenabled { 1873 asanwrite(unsafe.Pointer(&arg), unsafe.Sizeof(arg)) 1874 } 1875 call(_cgo_call_traceback_function, noescape(unsafe.Pointer(&arg))) 1876 } 1877