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