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 // same as the size bump used in scanframeworker. 505 frame.sp += alignUp(sys.MinFrameSize, sys.StackAlign) 506 f = findfunc(frame.pc) 507 frame.fn = f 508 if !f.valid() { 509 frame.pc = x 510 } else if funcspdelta(f, frame.pc) == 0 { 511 frame.lr = x 512 } 513 } 514 515 u.resolveInternal(false, false) 516 } 517 518 // finishInternal is an unwinder-internal helper called after the stack has been 519 // exhausted. It sets the unwinder to an invalid state and checks that it 520 // successfully unwound the entire stack. 521 func (u *unwinder) finishInternal() { 522 u.frame.pc = 0 523 524 // Note that panic != nil is okay here: there can be leftover panics, 525 // because the defers on the panic stack do not nest in frame order as 526 // they do on the defer stack. If you have: 527 // 528 // frame 1 defers d1 529 // frame 2 defers d2 530 // frame 3 defers d3 531 // frame 4 panics 532 // frame 4's panic starts running defers 533 // frame 5, running d3, defers d4 534 // frame 5 panics 535 // frame 5's panic starts running defers 536 // frame 6, running d4, garbage collects 537 // frame 6, running d2, garbage collects 538 // 539 // During the execution of d4, the panic stack is d4 -> d3, which 540 // is nested properly, and we'll treat frame 3 as resumable, because we 541 // can find d3. (And in fact frame 3 is resumable. If d4 recovers 542 // and frame 5 continues running, d3, d3 can recover and we'll 543 // resume execution in (returning from) frame 3.) 544 // 545 // During the execution of d2, however, the panic stack is d2 -> d3, 546 // which is inverted. The scan will match d2 to frame 2 but having 547 // d2 on the stack until then means it will not match d3 to frame 3. 548 // This is okay: if we're running d2, then all the defers after d2 have 549 // completed and their corresponding frames are dead. Not finding d3 550 // for frame 3 means we'll set frame 3's continpc == 0, which is correct 551 // (frame 3 is dead). At the end of the walk the panic stack can thus 552 // contain defers (d3 in this case) for dead frames. The inversion here 553 // always indicates a dead frame, and the effect of the inversion on the 554 // scan is to hide those dead frames, so the scan is still okay: 555 // what's left on the panic stack are exactly (and only) the dead frames. 556 // 557 // We require callback != nil here because only when callback != nil 558 // do we know that gentraceback is being called in a "must be correct" 559 // context as opposed to a "best effort" context. The tracebacks with 560 // callbacks only happen when everything is stopped nicely. 561 // At other times, such as when gathering a stack for a profiling signal 562 // or when printing a traceback during a crash, everything may not be 563 // stopped nicely, and the stack walk may not be able to complete. 564 gp := u.g.ptr() 565 if u.flags&(unwindPrintErrors|unwindSilentErrors) == 0 && u.frame.sp != gp.stktopsp { 566 print("runtime: g", gp.goid, ": frame.sp=", hex(u.frame.sp), " top=", hex(gp.stktopsp), "\n") 567 print("\tstack=[", hex(gp.stack.lo), "-", hex(gp.stack.hi), "\n") 568 throw("traceback did not unwind completely") 569 } 570 } 571 572 // symPC returns the PC that should be used for symbolizing the current frame. 573 // Specifically, this is the PC of the last instruction executed in this frame. 574 // 575 // If this frame did a normal call, then frame.pc is a return PC, so this will 576 // return frame.pc-1, which points into the CALL instruction. If the frame was 577 // interrupted by a signal (e.g., profiler, segv, etc) then frame.pc is for the 578 // trapped instruction, so this returns frame.pc. See issue #34123. Finally, 579 // frame.pc can be at function entry when the frame is initialized without 580 // actually running code, like in runtime.mstart, in which case this returns 581 // frame.pc because that's the best we can do. 582 func (u *unwinder) symPC() uintptr { 583 if u.flags&unwindTrap == 0 && u.frame.pc > u.frame.fn.entry() { 584 // Regular call. 585 return u.frame.pc - 1 586 } 587 // Trapping instruction or we're at the function entry point. 588 return u.frame.pc 589 } 590 591 // cgoCallers populates pcBuf with the cgo callers of the current frame using 592 // the registered cgo unwinder. It returns the number of PCs written to pcBuf. 593 // If the current frame is not a cgo frame or if there's no registered cgo 594 // unwinder, it returns 0. 595 func (u *unwinder) cgoCallers(pcBuf []uintptr) int { 596 if !cgoTracebackAvailable() || u.frame.fn.funcID != abi.FuncID_cgocallback || u.cgoCtxt < 0 { 597 // We don't have a cgo unwinder (typical case), or we do but we're not 598 // in a cgo frame or we're out of cgo context. 599 return 0 600 } 601 602 ctxt := u.g.ptr().cgoCtxt[u.cgoCtxt] 603 u.cgoCtxt-- 604 cgoContextPCs(ctxt, pcBuf) 605 for i, pc := range pcBuf { 606 if pc == 0 { 607 return i 608 } 609 } 610 return len(pcBuf) 611 } 612 613 // tracebackPCs populates pcBuf with the return addresses for each frame from u 614 // and returns the number of PCs written to pcBuf. The returned PCs correspond 615 // to "logical frames" rather than "physical frames"; that is if A is inlined 616 // into B, this will still return a PCs for both A and B. This also includes PCs 617 // generated by the cgo unwinder, if one is registered. 618 // 619 // If skip != 0, this skips this many logical frames. 620 // 621 // Callers should set the unwindSilentErrors flag on u. 622 func tracebackPCs(u *unwinder, skip int, pcBuf []uintptr) int { 623 var cgoBuf [32]uintptr 624 n := 0 625 for ; n < len(pcBuf) && u.valid(); u.next() { 626 f := u.frame.fn 627 cgoN := u.cgoCallers(cgoBuf[:]) 628 629 // TODO: Why does &u.cache cause u to escape? (Same in traceback2) 630 for iu, uf := newInlineUnwinder(f, u.symPC()); n < len(pcBuf) && uf.valid(); uf = iu.next(uf) { 631 sf := iu.srcFunc(uf) 632 if sf.funcID == abi.FuncIDWrapper && elideWrapperCalling(u.calleeFuncID) { 633 // ignore wrappers 634 } else if skip > 0 { 635 skip-- 636 } else { 637 // Callers expect the pc buffer to contain return addresses 638 // and do the -1 themselves, so we add 1 to the call pc to 639 // create a "return pc". Since there is no actual call, here 640 // "return pc" just means a pc you subtract 1 from to get 641 // the pc of the "call". The actual no-op we insert may or 642 // may not be 1 byte. 643 pcBuf[n] = uf.pc + 1 644 n++ 645 } 646 u.calleeFuncID = sf.funcID 647 } 648 // Add cgo frames (if we're done skipping over the requested number of 649 // Go frames). 650 if skip == 0 { 651 n += copy(pcBuf[n:], cgoBuf[:cgoN]) 652 } 653 } 654 return n 655 } 656 657 // printArgs prints function arguments in traceback. 658 func printArgs(f funcInfo, argp unsafe.Pointer, pc uintptr) { 659 p := (*[abi.TraceArgsMaxLen]uint8)(funcdata(f, abi.FUNCDATA_ArgInfo)) 660 if p == nil { 661 return 662 } 663 664 liveInfo := funcdata(f, abi.FUNCDATA_ArgLiveInfo) 665 liveIdx := pcdatavalue(f, abi.PCDATA_ArgLiveIndex, pc) 666 startOffset := uint8(0xff) // smallest offset that needs liveness info (slots with a lower offset is always live) 667 if liveInfo != nil { 668 startOffset = *(*uint8)(liveInfo) 669 } 670 671 isLive := func(off, slotIdx uint8) bool { 672 if liveInfo == nil || liveIdx <= 0 { 673 return true // no liveness info, always live 674 } 675 if off < startOffset { 676 return true 677 } 678 bits := *(*uint8)(add(liveInfo, uintptr(liveIdx)+uintptr(slotIdx/8))) 679 return bits&(1<<(slotIdx%8)) != 0 680 } 681 682 print1 := func(off, sz, slotIdx uint8) { 683 x := readUnaligned64(add(argp, uintptr(off))) 684 // mask out irrelevant bits 685 if sz < 8 { 686 shift := 64 - sz*8 687 if goarch.BigEndian { 688 x = x >> shift 689 } else { 690 x = x << shift >> shift 691 } 692 } 693 print(hex(x)) 694 if !isLive(off, slotIdx) { 695 print("?") 696 } 697 } 698 699 start := true 700 printcomma := func() { 701 if !start { 702 print(", ") 703 } 704 } 705 pi := 0 706 slotIdx := uint8(0) // register arg spill slot index 707 printloop: 708 for { 709 o := p[pi] 710 pi++ 711 switch o { 712 case abi.TraceArgsEndSeq: 713 break printloop 714 case abi.TraceArgsStartAgg: 715 printcomma() 716 print("{") 717 start = true 718 continue 719 case abi.TraceArgsEndAgg: 720 print("}") 721 case abi.TraceArgsDotdotdot: 722 printcomma() 723 print("...") 724 case abi.TraceArgsOffsetTooLarge: 725 printcomma() 726 print("_") 727 default: 728 printcomma() 729 sz := p[pi] 730 pi++ 731 print1(o, sz, slotIdx) 732 if o >= startOffset { 733 slotIdx++ 734 } 735 } 736 start = false 737 } 738 } 739 740 // funcNamePiecesForPrint returns the function name for printing to the user. 741 // It returns three pieces so it doesn't need an allocation for string 742 // concatenation. 743 func funcNamePiecesForPrint(name string) (string, string, string) { 744 // Replace the shape name in generic function with "...". 745 i := bytealg.IndexByteString(name, '[') 746 if i < 0 { 747 return name, "", "" 748 } 749 j := len(name) - 1 750 for name[j] != ']' { 751 j-- 752 } 753 if j <= i { 754 return name, "", "" 755 } 756 return name[:i], "[...]", name[j+1:] 757 } 758 759 // funcNameForPrint returns the function name for printing to the user. 760 func funcNameForPrint(name string) string { 761 a, b, c := funcNamePiecesForPrint(name) 762 return a + b + c 763 } 764 765 // printFuncName prints a function name. name is the function name in 766 // the binary's func data table. 767 func printFuncName(name string) { 768 if name == "runtime.gopanic" { 769 print("panic") 770 return 771 } 772 a, b, c := funcNamePiecesForPrint(name) 773 print(a, b, c) 774 } 775 776 func printcreatedby(gp *g) { 777 // Show what created goroutine, except main goroutine (goid 1). 778 pc := gp.gopc 779 f := findfunc(pc) 780 if f.valid() && showframe(f.srcFunc(), gp, false, abi.FuncIDNormal) && gp.goid != 1 { 781 printcreatedby1(f, pc, gp.parentGoid) 782 } 783 } 784 785 func printcreatedby1(f funcInfo, pc uintptr, goid uint64) { 786 print("created by ") 787 printFuncName(funcname(f)) 788 if goid != 0 { 789 print(" in goroutine ", goid) 790 } 791 print("\n") 792 tracepc := pc // back up to CALL instruction for funcline. 793 if pc > f.entry() { 794 tracepc -= sys.PCQuantum 795 } 796 file, line := funcline(f, tracepc) 797 print("\t", file, ":", line) 798 if pc > f.entry() { 799 print(" +", hex(pc-f.entry())) 800 } 801 print("\n") 802 } 803 804 func traceback(pc, sp, lr uintptr, gp *g) { 805 traceback1(pc, sp, lr, gp, 0) 806 } 807 808 // tracebacktrap is like traceback but expects that the PC and SP were obtained 809 // from a trap, not from gp->sched or gp->syscallpc/gp->syscallsp or GetCallerPC/GetCallerSP. 810 // Because they are from a trap instead of from a saved pair, 811 // the initial PC must not be rewound to the previous instruction. 812 // (All the saved pairs record a PC that is a return address, so we 813 // rewind it into the CALL instruction.) 814 // If gp.m.libcall{g,pc,sp} information is available, it uses that information in preference to 815 // the pc/sp/lr passed in. 816 func tracebacktrap(pc, sp, lr uintptr, gp *g) { 817 if gp.m.libcallsp != 0 { 818 // We're in C code somewhere, traceback from the saved position. 819 traceback1(gp.m.libcallpc, gp.m.libcallsp, 0, gp.m.libcallg.ptr(), 0) 820 return 821 } 822 traceback1(pc, sp, lr, gp, unwindTrap) 823 } 824 825 func traceback1(pc, sp, lr uintptr, gp *g, flags unwindFlags) { 826 // If the goroutine is in cgo, and we have a cgo traceback, print that. 827 if iscgo && gp.m != nil && gp.m.ncgo > 0 && gp.syscallsp != 0 && gp.m.cgoCallers != nil && gp.m.cgoCallers[0] != 0 { 828 // Lock cgoCallers so that a signal handler won't 829 // change it, copy the array, reset it, unlock it. 830 // We are locked to the thread and are not running 831 // concurrently with a signal handler. 832 // We just have to stop a signal handler from interrupting 833 // in the middle of our copy. 834 gp.m.cgoCallersUse.Store(1) 835 cgoCallers := *gp.m.cgoCallers 836 gp.m.cgoCallers[0] = 0 837 gp.m.cgoCallersUse.Store(0) 838 839 printCgoTraceback(&cgoCallers) 840 } 841 842 if readgstatus(gp)&^_Gscan == _Gsyscall { 843 // Override registers if blocked in system call. 844 pc = gp.syscallpc 845 sp = gp.syscallsp 846 flags &^= unwindTrap 847 } 848 if gp.m != nil && gp.m.vdsoSP != 0 { 849 // Override registers if running in VDSO. This comes after the 850 // _Gsyscall check to cover VDSO calls after entersyscall. 851 pc = gp.m.vdsoPC 852 sp = gp.m.vdsoSP 853 flags &^= unwindTrap 854 } 855 856 // Print traceback. 857 // 858 // We print the first tracebackInnerFrames frames, and the last 859 // tracebackOuterFrames frames. There are many possible approaches to this. 860 // There are various complications to this: 861 // 862 // - We'd prefer to walk the stack once because in really bad situations 863 // traceback may crash (and we want as much output as possible) or the stack 864 // may be changing. 865 // 866 // - Each physical frame can represent several logical frames, so we might 867 // have to pause in the middle of a physical frame and pick up in the middle 868 // of a physical frame. 869 // 870 // - The cgo symbolizer can expand a cgo PC to more than one logical frame, 871 // and involves juggling state on the C side that we don't manage. Since its 872 // expansion state is managed on the C side, we can't capture the expansion 873 // state part way through, and because the output strings are managed on the 874 // C side, we can't capture the output. Thus, our only choice is to replay a 875 // whole expansion, potentially discarding some of it. 876 // 877 // Rejected approaches: 878 // 879 // - Do two passes where the first pass just counts and the second pass does 880 // all the printing. This is undesirable if the stack is corrupted or changing 881 // because we won't see a partial stack if we panic. 882 // 883 // - Keep a ring buffer of the last N logical frames and use this to print 884 // the bottom frames once we reach the end of the stack. This works, but 885 // requires keeping a surprising amount of state on the stack, and we have 886 // to run the cgo symbolizer twice—once to count frames, and a second to 887 // print them—since we can't retain the strings it returns. 888 // 889 // Instead, we print the outer frames, and if we reach that limit, we clone 890 // the unwinder, count the remaining frames, and then skip forward and 891 // finish printing from the clone. This makes two passes over the outer part 892 // of the stack, but the single pass over the inner part ensures that's 893 // printed immediately and not revisited. It keeps minimal state on the 894 // stack. And through a combination of skip counts and limits, we can do all 895 // of the steps we need with a single traceback printer implementation. 896 // 897 // We could be more lax about exactly how many frames we print, for example 898 // always stopping and resuming on physical frame boundaries, or at least 899 // cgo expansion boundaries. It's not clear that's much simpler. 900 flags |= unwindPrintErrors 901 var u unwinder 902 tracebackWithRuntime := func(showRuntime bool) int { 903 const maxInt int = 0x7fffffff 904 u.initAt(pc, sp, lr, gp, flags) 905 n, lastN := traceback2(&u, showRuntime, 0, tracebackInnerFrames) 906 if n < tracebackInnerFrames { 907 // We printed the whole stack. 908 return n 909 } 910 // Clone the unwinder and figure out how many frames are left. This 911 // count will include any logical frames already printed for u's current 912 // physical frame. 913 u2 := u 914 remaining, _ := traceback2(&u, showRuntime, maxInt, 0) 915 elide := remaining - lastN - tracebackOuterFrames 916 if elide > 0 { 917 print("...", elide, " frames elided...\n") 918 traceback2(&u2, showRuntime, lastN+elide, tracebackOuterFrames) 919 } else if elide <= 0 { 920 // There are tracebackOuterFrames or fewer frames left to print. 921 // Just print the rest of the stack. 922 traceback2(&u2, showRuntime, lastN, tracebackOuterFrames) 923 } 924 return n 925 } 926 // By default, omits runtime frames. If that means we print nothing at all, 927 // repeat forcing all frames printed. 928 if tracebackWithRuntime(false) == 0 { 929 tracebackWithRuntime(true) 930 } 931 printcreatedby(gp) 932 933 if gp.ancestors == nil { 934 return 935 } 936 for _, ancestor := range *gp.ancestors { 937 printAncestorTraceback(ancestor) 938 } 939 } 940 941 // traceback2 prints a stack trace starting at u. It skips the first "skip" 942 // logical frames, after which it prints at most "max" logical frames. It 943 // returns n, which is the number of logical frames skipped and printed, and 944 // lastN, which is the number of logical frames skipped or printed just in the 945 // physical frame that u references. 946 func traceback2(u *unwinder, showRuntime bool, skip, max int) (n, lastN int) { 947 // commitFrame commits to a logical frame and returns whether this frame 948 // should be printed and whether iteration should stop. 949 commitFrame := func() (pr, stop bool) { 950 if skip == 0 && max == 0 { 951 // Stop 952 return false, true 953 } 954 n++ 955 lastN++ 956 if skip > 0 { 957 // Skip 958 skip-- 959 return false, false 960 } 961 // Print 962 max-- 963 return true, false 964 } 965 966 gp := u.g.ptr() 967 level, _, _ := gotraceback() 968 var cgoBuf [32]uintptr 969 for ; u.valid(); u.next() { 970 lastN = 0 971 f := u.frame.fn 972 for iu, uf := newInlineUnwinder(f, u.symPC()); uf.valid(); uf = iu.next(uf) { 973 sf := iu.srcFunc(uf) 974 callee := u.calleeFuncID 975 u.calleeFuncID = sf.funcID 976 if !(showRuntime || showframe(sf, gp, n == 0, callee)) { 977 continue 978 } 979 980 if pr, stop := commitFrame(); stop { 981 return 982 } else if !pr { 983 continue 984 } 985 986 name := sf.name() 987 file, line := iu.fileLine(uf) 988 // Print during crash. 989 // main(0x1, 0x2, 0x3) 990 // /home/rsc/go/src/runtime/x.go:23 +0xf 991 // 992 printFuncName(name) 993 print("(") 994 if iu.isInlined(uf) { 995 print("...") 996 } else { 997 argp := unsafe.Pointer(u.frame.argp) 998 printArgs(f, argp, u.symPC()) 999 } 1000 print(")\n") 1001 print("\t", file, ":", line) 1002 if !iu.isInlined(uf) { 1003 if u.frame.pc > f.entry() { 1004 print(" +", hex(u.frame.pc-f.entry())) 1005 } 1006 if gp.m != nil && gp.m.throwing >= throwTypeRuntime && gp == gp.m.curg || level >= 2 { 1007 print(" fp=", hex(u.frame.fp), " sp=", hex(u.frame.sp), " pc=", hex(u.frame.pc)) 1008 } 1009 } 1010 print("\n") 1011 } 1012 1013 // Print cgo frames. 1014 if cgoN := u.cgoCallers(cgoBuf[:]); cgoN > 0 { 1015 var arg cgoSymbolizerArg 1016 anySymbolized := false 1017 stop := false 1018 for _, pc := range cgoBuf[:cgoN] { 1019 if !cgoSymbolizerAvailable() { 1020 if pr, stop := commitFrame(); stop { 1021 break 1022 } else if pr { 1023 print("non-Go function at pc=", hex(pc), "\n") 1024 } 1025 } else { 1026 stop = printOneCgoTraceback(pc, commitFrame, &arg) 1027 anySymbolized = true 1028 if stop { 1029 break 1030 } 1031 } 1032 } 1033 if anySymbolized { 1034 // Free symbolization state. 1035 arg.pc = 0 1036 callCgoSymbolizer(&arg) 1037 } 1038 if stop { 1039 return 1040 } 1041 } 1042 } 1043 return n, 0 1044 } 1045 1046 // printAncestorTraceback prints the traceback of the given ancestor. 1047 // TODO: Unify this with gentraceback and CallersFrames. 1048 func printAncestorTraceback(ancestor ancestorInfo) { 1049 print("[originating from goroutine ", ancestor.goid, "]:\n") 1050 for fidx, pc := range ancestor.pcs { 1051 f := findfunc(pc) // f previously validated 1052 if showfuncinfo(f.srcFunc(), fidx == 0, abi.FuncIDNormal) { 1053 printAncestorTracebackFuncInfo(f, pc) 1054 } 1055 } 1056 if len(ancestor.pcs) == tracebackInnerFrames { 1057 print("...additional frames elided...\n") 1058 } 1059 // Show what created goroutine, except main goroutine (goid 1). 1060 f := findfunc(ancestor.gopc) 1061 if f.valid() && showfuncinfo(f.srcFunc(), false, abi.FuncIDNormal) && ancestor.goid != 1 { 1062 // In ancestor mode, we'll already print the goroutine ancestor. 1063 // Pass 0 for the goid parameter so we don't print it again. 1064 printcreatedby1(f, ancestor.gopc, 0) 1065 } 1066 } 1067 1068 // printAncestorTracebackFuncInfo prints the given function info at a given pc 1069 // within an ancestor traceback. The precision of this info is reduced 1070 // due to only have access to the pcs at the time of the caller 1071 // goroutine being created. 1072 func printAncestorTracebackFuncInfo(f funcInfo, pc uintptr) { 1073 u, uf := newInlineUnwinder(f, pc) 1074 file, line := u.fileLine(uf) 1075 printFuncName(u.srcFunc(uf).name()) 1076 print("(...)\n") 1077 print("\t", file, ":", line) 1078 if pc > f.entry() { 1079 print(" +", hex(pc-f.entry())) 1080 } 1081 print("\n") 1082 } 1083 1084 // callers should be an internal detail, 1085 // (and is almost identical to Callers), 1086 // but widely used packages access it using linkname. 1087 // Notable members of the hall of shame include: 1088 // - github.com/phuslu/log 1089 // 1090 // Do not remove or change the type signature. 1091 // See go.dev/issue/67401. 1092 // 1093 //go:linkname callers 1094 func callers(skip int, pcbuf []uintptr) int { 1095 sp := sys.GetCallerSP() 1096 pc := sys.GetCallerPC() 1097 gp := getg() 1098 var n int 1099 systemstack(func() { 1100 var u unwinder 1101 u.initAt(pc, sp, 0, gp, unwindSilentErrors) 1102 n = tracebackPCs(&u, skip, pcbuf) 1103 }) 1104 return n 1105 } 1106 1107 func gcallers(gp *g, skip int, pcbuf []uintptr) int { 1108 var u unwinder 1109 u.init(gp, unwindSilentErrors) 1110 return tracebackPCs(&u, skip, pcbuf) 1111 } 1112 1113 // showframe reports whether the frame with the given characteristics should 1114 // be printed during a traceback. 1115 func showframe(sf srcFunc, gp *g, firstFrame bool, calleeID abi.FuncID) bool { 1116 mp := getg().m 1117 if mp.throwing >= throwTypeRuntime && gp != nil && (gp == mp.curg || gp == mp.caughtsig.ptr()) { 1118 return true 1119 } 1120 return showfuncinfo(sf, firstFrame, calleeID) 1121 } 1122 1123 // showfuncinfo reports whether a function with the given characteristics should 1124 // be printed during a traceback. 1125 func showfuncinfo(sf srcFunc, firstFrame bool, calleeID abi.FuncID) bool { 1126 level, _, _ := gotraceback() 1127 if level > 1 { 1128 // Show all frames. 1129 return true 1130 } 1131 1132 if sf.funcID == abi.FuncIDWrapper && elideWrapperCalling(calleeID) { 1133 return false 1134 } 1135 1136 // Always show runtime.runFinalizers and runtime.runCleanups as 1137 // context that this goroutine is running finalizers or cleanups, 1138 // otherwise there is no obvious indicator. 1139 // 1140 // TODO(prattmic): A more general approach would be to always show the 1141 // outermost frame (besides runtime.goexit), even if it is a runtime. 1142 // Hiding the outermost frame allows the apparent outermost frame to 1143 // change across different traces, which seems impossible. 1144 // 1145 // Unfortunately, implementing this requires looking ahead at the next 1146 // frame, which goes against traceback's incremental approach (see big 1147 // comment in traceback1). 1148 if sf.funcID == abi.FuncID_runFinalizers || sf.funcID == abi.FuncID_runCleanups { 1149 return true 1150 } 1151 1152 name := sf.name() 1153 1154 // Special case: always show runtime.gopanic frame 1155 // in the middle of a stack trace, so that we can 1156 // see the boundary between ordinary code and 1157 // panic-induced deferred code. 1158 // See golang.org/issue/5832. 1159 if name == "runtime.gopanic" && !firstFrame { 1160 return true 1161 } 1162 1163 return bytealg.IndexByteString(name, '.') >= 0 && (!stringslite.HasPrefix(name, "runtime.") || isExportedRuntime(name)) 1164 } 1165 1166 // isExportedRuntime reports whether name is an exported runtime function. 1167 // It is only for runtime functions, so ASCII A-Z is fine. 1168 func isExportedRuntime(name string) bool { 1169 // Check and remove package qualifier. 1170 name, found := stringslite.CutPrefix(name, "runtime.") 1171 if !found { 1172 return false 1173 } 1174 rcvr := "" 1175 1176 // Extract receiver type, if any. 1177 // For example, runtime.(*Func).Entry 1178 i := len(name) - 1 1179 for i >= 0 && name[i] != '.' { 1180 i-- 1181 } 1182 if i >= 0 { 1183 rcvr = name[:i] 1184 name = name[i+1:] 1185 // Remove parentheses and star for pointer receivers. 1186 if len(rcvr) >= 3 && rcvr[0] == '(' && rcvr[1] == '*' && rcvr[len(rcvr)-1] == ')' { 1187 rcvr = rcvr[2 : len(rcvr)-1] 1188 } 1189 } 1190 1191 // Exported functions and exported methods on exported types. 1192 return len(name) > 0 && 'A' <= name[0] && name[0] <= 'Z' && (len(rcvr) == 0 || 'A' <= rcvr[0] && rcvr[0] <= 'Z') 1193 } 1194 1195 // elideWrapperCalling reports whether a wrapper function that called 1196 // function id should be elided from stack traces. 1197 func elideWrapperCalling(id abi.FuncID) bool { 1198 // If the wrapper called a panic function instead of the 1199 // wrapped function, we want to include it in stacks. 1200 return !(id == abi.FuncID_gopanic || id == abi.FuncID_sigpanic || id == abi.FuncID_panicwrap) 1201 } 1202 1203 var gStatusStrings = [...]string{ 1204 _Gidle: "idle", 1205 _Grunnable: "runnable", 1206 _Grunning: "running", 1207 _Gsyscall: "syscall", 1208 _Gwaiting: "waiting", 1209 _Gdead: "dead", 1210 _Gcopystack: "copystack", 1211 _Gleaked: "leaked", 1212 _Gpreempted: "preempted", 1213 _Gdeadextra: "waiting for cgo callback", 1214 } 1215 1216 func goroutineheader(gp *g) { 1217 level, _, _ := gotraceback() 1218 1219 gpstatus := readgstatus(gp) 1220 1221 isScan := gpstatus&_Gscan != 0 1222 gpstatus &^= _Gscan // drop the scan bit 1223 1224 // Basic string status 1225 var status string 1226 if 0 <= gpstatus && gpstatus < uint32(len(gStatusStrings)) { 1227 status = gStatusStrings[gpstatus] 1228 } else { 1229 status = "???" 1230 } 1231 1232 // Override. 1233 if (gpstatus == _Gwaiting || gpstatus == _Gleaked) && gp.waitreason != waitReasonZero { 1234 status = gp.waitreason.String() 1235 } 1236 1237 // approx time the G is blocked, in minutes 1238 var waitfor int64 1239 if (gpstatus == _Gwaiting || gpstatus == _Gsyscall) && gp.waitsince != 0 { 1240 waitfor = (nanotime() - gp.waitsince) / 60e9 1241 } 1242 print("goroutine ", gp.goid) 1243 if gp.m != nil && gp.m.throwing >= throwTypeRuntime && gp == gp.m.curg || level >= 2 { 1244 print(" gp=", gp) 1245 if gp.m != nil { 1246 print(" m=", gp.m.id, " mp=", gp.m) 1247 } else { 1248 print(" m=nil") 1249 } 1250 } 1251 print(" [", status) 1252 if gpstatus == _Gleaked { 1253 print(" (leaked)") 1254 } 1255 if isScan { 1256 print(" (scan)") 1257 } 1258 if bubble := gp.bubble; bubble != nil && 1259 gpstatus == _Gwaiting && 1260 gp.waitreason.isIdleInSynctest() && 1261 !stringslite.HasSuffix(status, "(durable)") { 1262 // If this isn't a status where the name includes a (durable) 1263 // suffix to distinguish it from the non-durable form, add it here. 1264 print(" (durable)") 1265 } 1266 if waitfor >= 1 { 1267 print(", ", waitfor, " minutes") 1268 } 1269 if gp.lockedm != 0 { 1270 print(", locked to thread") 1271 } 1272 if bubble := gp.bubble; bubble != nil { 1273 print(", synctest bubble ", bubble.id) 1274 } 1275 if gp.labels != nil && debug.tracebacklabels.Load() == 1 { 1276 labels := (*label.Set)(gp.labels).List 1277 if len(labels) > 0 { 1278 print(" labels:{") 1279 for i, kv := range labels { 1280 print(quoted(kv.Key), ": ", quoted(kv.Value)) 1281 if i < len(labels)-1 { 1282 print(", ") 1283 } 1284 } 1285 print("}") 1286 } 1287 } 1288 print("]:\n") 1289 } 1290 1291 func tracebackothers(me *g) { 1292 tracebacksomeothers(me, func(*g) bool { return true }) 1293 } 1294 1295 func tracebacksomeothers(me *g, showf func(*g) bool) { 1296 level, _, _ := gotraceback() 1297 1298 // Show the current goroutine first, if we haven't already. 1299 curgp := getg().m.curg 1300 if curgp != nil && curgp != me { 1301 print("\n") 1302 goroutineheader(curgp) 1303 traceback(^uintptr(0), ^uintptr(0), 0, curgp) 1304 } 1305 1306 // We can't call locking forEachG here because this may be during fatal 1307 // throw/panic, where locking could be out-of-order or a direct 1308 // deadlock. 1309 // 1310 // Instead, use forEachGRace, which requires no locking. We don't lock 1311 // against concurrent creation of new Gs, but even with allglock we may 1312 // miss Gs created after this loop. 1313 forEachGRace(func(gp *g) { 1314 if gp == me || gp == curgp { 1315 return 1316 } 1317 if status := readgstatus(gp); status == _Gdead || status == _Gdeadextra { 1318 return 1319 } 1320 if !showf(gp) { 1321 return 1322 } 1323 if isSystemGoroutine(gp, false) && level < 2 { 1324 return 1325 } 1326 print("\n") 1327 goroutineheader(gp) 1328 // Note: gp.m == getg().m occurs when tracebackothers is called 1329 // from a signal handler initiated during a systemstack call. 1330 // The original G is still in the running state, and we want to 1331 // print its stack. 1332 // 1333 // There's a small window of time in exitsyscall where a goroutine could be 1334 // in _Grunning as it's exiting a syscall. This could be the case even if the 1335 // world is stopped or frozen. 1336 // 1337 // This is OK because the goroutine will not exit the syscall while the world 1338 // is stopped or frozen. This is also why it's safe to check syscallsp here, 1339 // and safe to take the goroutine's stack trace. The syscall path mutates 1340 // syscallsp only just before exiting the syscall. 1341 if gp.m != getg().m && readgstatus(gp)&^_Gscan == _Grunning && gp.syscallsp == 0 { 1342 print("\tgoroutine running on other thread; stack unavailable\n") 1343 printcreatedby(gp) 1344 } else { 1345 traceback(^uintptr(0), ^uintptr(0), 0, gp) 1346 } 1347 }) 1348 } 1349 1350 // tracebackHexdump hexdumps part of stk around frame.sp and frame.fp 1351 // for debugging purposes. If the address bad is included in the 1352 // hexdumped range, it will mark it as well. 1353 func tracebackHexdump(stk stack, frame *stkframe, bad uintptr) { 1354 const expand = 32 * goarch.PtrSize 1355 const maxExpand = 256 * goarch.PtrSize 1356 // Start around frame.sp. 1357 lo, hi := frame.sp, frame.sp 1358 // Expand to include frame.fp. 1359 if frame.fp != 0 && frame.fp < lo { 1360 lo = frame.fp 1361 } 1362 if frame.fp != 0 && frame.fp > hi { 1363 hi = frame.fp 1364 } 1365 // Expand a bit more. 1366 lo, hi = lo-expand, hi+expand 1367 // But don't go too far from frame.sp. 1368 if lo < frame.sp-maxExpand { 1369 lo = frame.sp - maxExpand 1370 } 1371 if hi > frame.sp+maxExpand { 1372 hi = frame.sp + maxExpand 1373 } 1374 // And don't go outside the stack bounds. 1375 if lo < stk.lo { 1376 lo = stk.lo 1377 } 1378 if hi > stk.hi { 1379 hi = stk.hi 1380 } 1381 1382 // Print the hex dump. 1383 print("stack: frame={sp:", hex(frame.sp), ", fp:", hex(frame.fp), "} stack=[", hex(stk.lo), ",", hex(stk.hi), ")\n") 1384 hexdumpWords(lo, hi-lo, func(p uintptr, m hexdumpMarker) { 1385 if p == frame.fp { 1386 m.start() 1387 println("FP") 1388 } 1389 if p == frame.sp { 1390 m.start() 1391 println("SP") 1392 } 1393 if p == bad { 1394 m.start() 1395 println("bad") 1396 } 1397 }) 1398 } 1399 1400 // isSystemGoroutine reports whether the goroutine g must be omitted 1401 // in stack dumps and deadlock detector. This is any goroutine that 1402 // starts at a runtime.* entry point, except for runtime.main, 1403 // runtime.handleAsyncEvent (wasm only) and sometimes 1404 // runtime.runFinalizers/runtime.runCleanups. 1405 // 1406 // If fixed is true, any goroutine that can vary between user and 1407 // system (that is, the finalizer goroutine) is considered a user 1408 // goroutine. 1409 func isSystemGoroutine(gp *g, fixed bool) bool { 1410 // Keep this in sync with internal/trace.IsSystemGoroutine. 1411 f := findfunc(gp.startpc) 1412 if !f.valid() { 1413 return false 1414 } 1415 if f.funcID == abi.FuncID_runtime_main || f.funcID == abi.FuncID_corostart || f.funcID == abi.FuncID_handleAsyncEvent { 1416 return false 1417 } 1418 if f.funcID == abi.FuncID_runFinalizers { 1419 // We include the finalizer goroutine if it's calling 1420 // back into user code. 1421 if fixed { 1422 // This goroutine can vary. In fixed mode, 1423 // always consider it a user goroutine. 1424 return false 1425 } 1426 return fingStatus.Load()&fingRunningFinalizer == 0 1427 } 1428 if f.funcID == abi.FuncID_runCleanups { 1429 // We include the cleanup goroutines if they're calling 1430 // back into user code. 1431 if fixed { 1432 // This goroutine can vary. In fixed mode, 1433 // always consider it a user goroutine. 1434 return false 1435 } 1436 return !gp.runningCleanups.Load() 1437 } 1438 return stringslite.HasPrefix(funcname(f), "runtime.") 1439 } 1440 1441 // SetCgoTraceback records three C functions to use to gather 1442 // traceback information from C code and to convert that traceback 1443 // information into symbolic information. These are used when printing 1444 // stack traces for a program that uses cgo. 1445 // 1446 // The traceback and context functions may be called from a signal 1447 // handler, and must therefore use only async-signal safe functions. 1448 // The symbolizer function may be called while the program is 1449 // crashing, and so must be cautious about using memory. None of the 1450 // functions may call back into Go. 1451 // 1452 // The context function will be called with a single argument, a 1453 // pointer to a struct: 1454 // 1455 // struct { 1456 // Context uintptr 1457 // } 1458 // 1459 // In C syntax, this struct will be 1460 // 1461 // struct { 1462 // uintptr_t Context; 1463 // }; 1464 // 1465 // If the Context field is 0, the context function is being called to 1466 // record the current traceback context. It should record in the 1467 // Context field whatever information is needed about the current 1468 // point of execution to later produce a stack trace, probably the 1469 // stack pointer and PC. In this case the context function will be 1470 // called from C code. 1471 // 1472 // If the Context field is not 0, then it is a value returned by a 1473 // previous call to the context function. This case is called when the 1474 // context is no longer needed; that is, when the Go code is returning 1475 // to its C code caller. This permits the context function to release 1476 // any associated resources. 1477 // 1478 // While it would be correct for the context function to record a 1479 // complete a stack trace whenever it is called, and simply copy that 1480 // out in the traceback function, in a typical program the context 1481 // function will be called many times without ever recording a 1482 // traceback for that context. Recording a complete stack trace in a 1483 // call to the context function is likely to be inefficient. 1484 // 1485 // The traceback function will be called with a single argument, a 1486 // pointer to a struct: 1487 // 1488 // struct { 1489 // Context uintptr 1490 // SigContext uintptr 1491 // Buf *uintptr 1492 // Max uintptr 1493 // } 1494 // 1495 // In C syntax, this struct will be 1496 // 1497 // struct { 1498 // uintptr_t Context; 1499 // uintptr_t SigContext; 1500 // uintptr_t* Buf; 1501 // uintptr_t Max; 1502 // }; 1503 // 1504 // The Context field will be zero to gather a traceback from the 1505 // current program execution point. In this case, the traceback 1506 // function will be called from C code. 1507 // 1508 // Otherwise Context will be a value previously returned by a call to 1509 // the context function. The traceback function should gather a stack 1510 // trace from that saved point in the program execution. The traceback 1511 // function may be called from an execution thread other than the one 1512 // that recorded the context, but only when the context is known to be 1513 // valid and unchanging. The traceback function may also be called 1514 // deeper in the call stack on the same thread that recorded the 1515 // context. The traceback function may be called multiple times with 1516 // the same Context value; it will usually be appropriate to cache the 1517 // result, if possible, the first time this is called for a specific 1518 // context value. 1519 // 1520 // If the traceback function is called from a signal handler on a Unix 1521 // system, SigContext will be the signal context argument passed to 1522 // the signal handler (a C ucontext_t* cast to uintptr_t). This may be 1523 // used to start tracing at the point where the signal occurred. If 1524 // the traceback function is not called from a signal handler, 1525 // SigContext will be zero. 1526 // 1527 // Buf is where the traceback information should be stored. It should 1528 // be PC values, such that Buf[0] is the PC of the caller, Buf[1] is 1529 // the PC of that function's caller, and so on. Max is the maximum 1530 // number of entries to store. The function should store a zero to 1531 // indicate the top of the stack, or that the caller is on a different 1532 // stack, presumably a Go stack. 1533 // 1534 // Unlike runtime.Callers, the PC values returned should, when passed 1535 // to the symbolizer function, return the file/line of the call 1536 // instruction. No additional subtraction is required or appropriate. 1537 // 1538 // On all platforms, the traceback function is invoked when a call from 1539 // Go to C to Go requests a stack trace. On linux/amd64, linux/ppc64le, 1540 // linux/arm64, and freebsd/amd64, the traceback function is also invoked 1541 // when a signal is received by a thread that is executing a cgo call. 1542 // The traceback function should not make assumptions about when it is 1543 // called, as future versions of Go may make additional calls. 1544 // 1545 // The symbolizer function will be called with a single argument, a 1546 // pointer to a struct: 1547 // 1548 // struct { 1549 // PC uintptr // program counter to fetch information for 1550 // File *byte // file name (NUL terminated) 1551 // Lineno uintptr // line number 1552 // Func *byte // function name (NUL terminated) 1553 // Entry uintptr // function entry point 1554 // More uintptr // set non-zero if more info for this PC 1555 // Data uintptr // unused by runtime, available for function 1556 // } 1557 // 1558 // In C syntax, this struct will be 1559 // 1560 // struct { 1561 // uintptr_t PC; 1562 // char* File; 1563 // uintptr_t Lineno; 1564 // char* Func; 1565 // uintptr_t Entry; 1566 // uintptr_t More; 1567 // uintptr_t Data; 1568 // }; 1569 // 1570 // The PC field will be a value returned by a call to the traceback 1571 // function. 1572 // 1573 // The first time the function is called for a particular traceback, 1574 // all the fields except PC will be 0. The function should fill in the 1575 // other fields if possible, setting them to 0/nil if the information 1576 // is not available. The Data field may be used to store any useful 1577 // information across calls. The More field should be set to non-zero 1578 // if there is more information for this PC, zero otherwise. If More 1579 // is set non-zero, the function will be called again with the same 1580 // PC, and may return different information (this is intended for use 1581 // with inlined functions). If More is zero, the function will be 1582 // called with the next PC value in the traceback. When the traceback 1583 // is complete, the function will be called once more with PC set to 1584 // zero; this may be used to free any information. Each call will 1585 // leave the fields of the struct set to the same values they had upon 1586 // return, except for the PC field when the More field is zero. The 1587 // function must not keep a copy of the struct pointer between calls. 1588 // 1589 // When calling SetCgoTraceback, the version argument is the version 1590 // number of the structs that the functions expect to receive. 1591 // Currently this must be zero. 1592 // 1593 // The symbolizer function may be nil, in which case the results of 1594 // the traceback function will be displayed as numbers. If the 1595 // traceback function is nil, the symbolizer function will never be 1596 // called. The context function may be nil, in which case the 1597 // traceback function will only be called with the context field set 1598 // to zero. If the context function is nil, then calls from Go to C 1599 // to Go will not show a traceback for the C portion of the call stack. 1600 // 1601 // SetCgoTraceback should be called only once, ideally from an init function. 1602 func SetCgoTraceback(version int, traceback, context, symbolizer unsafe.Pointer) { 1603 if version != 0 { 1604 panic("unsupported version") 1605 } 1606 1607 if cgoTraceback != nil && cgoTraceback != traceback || 1608 cgoContext != nil && cgoContext != context || 1609 cgoSymbolizer != nil && cgoSymbolizer != symbolizer { 1610 panic("call SetCgoTraceback only once") 1611 } 1612 1613 cgoTraceback = traceback 1614 cgoContext = context 1615 cgoSymbolizer = symbolizer 1616 1617 if _cgo_set_traceback_functions != nil { 1618 type cgoSetTracebackFunctionsArg struct { 1619 traceback unsafe.Pointer 1620 context unsafe.Pointer 1621 symbolizer unsafe.Pointer 1622 } 1623 arg := cgoSetTracebackFunctionsArg{ 1624 traceback: traceback, 1625 context: context, 1626 symbolizer: symbolizer, 1627 } 1628 cgocall(_cgo_set_traceback_functions, noescape(unsafe.Pointer(&arg))) 1629 } 1630 } 1631 1632 var cgoTraceback unsafe.Pointer 1633 var cgoContext unsafe.Pointer 1634 var cgoSymbolizer unsafe.Pointer 1635 1636 func cgoTracebackAvailable() bool { 1637 // - The traceback function must be registered via SetCgoTraceback. 1638 // - This must be a cgo binary (providing _cgo_call_traceback_function). 1639 return cgoTraceback != nil && _cgo_call_traceback_function != nil 1640 } 1641 1642 func cgoSymbolizerAvailable() bool { 1643 // - The symbolizer function must be registered via SetCgoTraceback. 1644 // - This must be a cgo binary (providing _cgo_call_symbolizer_function). 1645 return cgoSymbolizer != nil && _cgo_call_symbolizer_function != nil 1646 } 1647 1648 // cgoTracebackArg is the type passed to cgoTraceback. 1649 type cgoTracebackArg struct { 1650 context uintptr 1651 sigContext uintptr 1652 buf *uintptr 1653 max uintptr 1654 } 1655 1656 // cgoContextArg is the type passed to the context function. 1657 type cgoContextArg struct { 1658 context uintptr 1659 } 1660 1661 // cgoSymbolizerArg is the type passed to cgoSymbolizer. 1662 type cgoSymbolizerArg struct { 1663 pc uintptr 1664 file *byte 1665 lineno uintptr 1666 funcName *byte 1667 entry uintptr 1668 more uintptr 1669 data uintptr 1670 } 1671 1672 // printCgoTraceback prints a traceback of callers. 1673 func printCgoTraceback(callers *cgoCallers) { 1674 if !cgoSymbolizerAvailable() { 1675 for _, c := range callers { 1676 if c == 0 { 1677 break 1678 } 1679 print("non-Go function at pc=", hex(c), "\n") 1680 } 1681 return 1682 } 1683 1684 commitFrame := func() (pr, stop bool) { return true, false } 1685 var arg cgoSymbolizerArg 1686 for _, c := range callers { 1687 if c == 0 { 1688 break 1689 } 1690 printOneCgoTraceback(c, commitFrame, &arg) 1691 } 1692 arg.pc = 0 1693 callCgoSymbolizer(&arg) 1694 } 1695 1696 // printOneCgoTraceback prints the traceback of a single cgo caller. 1697 // This can print more than one line because of inlining. 1698 // It returns the "stop" result of commitFrame. 1699 // 1700 // Preconditions: cgoSymbolizerAvailable returns true. 1701 func printOneCgoTraceback(pc uintptr, commitFrame func() (pr, stop bool), arg *cgoSymbolizerArg) bool { 1702 arg.pc = pc 1703 for { 1704 if pr, stop := commitFrame(); stop { 1705 return true 1706 } else if !pr { 1707 continue 1708 } 1709 1710 callCgoSymbolizer(arg) 1711 if arg.funcName != nil { 1712 // Note that we don't print any argument 1713 // information here, not even parentheses. 1714 // The symbolizer must add that if appropriate. 1715 println(gostringnocopy(arg.funcName)) 1716 } else { 1717 println("non-Go function") 1718 } 1719 print("\t") 1720 if arg.file != nil { 1721 print(gostringnocopy(arg.file), ":", arg.lineno, " ") 1722 } 1723 print("pc=", hex(pc), "\n") 1724 if arg.more == 0 { 1725 return false 1726 } 1727 } 1728 } 1729 1730 // callCgoSymbolizer calls the cgoSymbolizer function. 1731 // 1732 // Preconditions: cgoSymbolizerAvailable returns true. 1733 func callCgoSymbolizer(arg *cgoSymbolizerArg) { 1734 call := cgocall 1735 if panicking.Load() > 0 || getg().m.curg != getg() { 1736 // We do not want to call into the scheduler when panicking 1737 // or when on the system stack. 1738 call = asmcgocall 1739 } 1740 if msanenabled { 1741 msanwrite(unsafe.Pointer(arg), unsafe.Sizeof(cgoSymbolizerArg{})) 1742 } 1743 if asanenabled { 1744 asanwrite(unsafe.Pointer(arg), unsafe.Sizeof(cgoSymbolizerArg{})) 1745 } 1746 call(_cgo_call_symbolizer_function, noescape(unsafe.Pointer(arg))) 1747 } 1748 1749 // cgoContextPCs gets the PC values from a cgo traceback. 1750 // 1751 // Preconditions: cgoTracebackAvailable returns true. 1752 func cgoContextPCs(ctxt uintptr, buf []uintptr) { 1753 call := cgocall 1754 if panicking.Load() > 0 || getg().m.curg != getg() { 1755 // We do not want to call into the scheduler when panicking 1756 // or when on the system stack. 1757 call = asmcgocall 1758 } 1759 arg := cgoTracebackArg{ 1760 context: ctxt, 1761 buf: (*uintptr)(noescape(unsafe.Pointer(&buf[0]))), 1762 max: uintptr(len(buf)), 1763 } 1764 if msanenabled { 1765 msanwrite(unsafe.Pointer(&arg), unsafe.Sizeof(arg)) 1766 } 1767 if asanenabled { 1768 asanwrite(unsafe.Pointer(&arg), unsafe.Sizeof(arg)) 1769 } 1770 call(_cgo_call_traceback_function, noescape(unsafe.Pointer(&arg))) 1771 } 1772