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 != nil && 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 five pieces so it doesn't need an allocation for string 741 // concatenation. 742 func funcNamePiecesForPrint(name string) (string, 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 756 interior := name[i+1 : j] // '[' interior ']' 757 // This is an early-out to skip the more-detailed parsing that 758 // follows -- if there's no '[' in the interior, that implies 759 // (assuming balanced brackets) no ']' in the interior, and thus 760 // this will be the answer. If brackets are not balanced 761 // (malformed input, which was already a risk), this will 762 // eat/hide the unbalanced "]". 763 if bytealg.IndexByteString(interior, '[') < 0 { 764 return name[:i], "[...]", name[j+1:], "", "" 765 } 766 // Generic method of generic type. 767 // know interior contains at least "...[..." 768 // expect interior contains "...]___[...". 769 // don't know whether "..." contains balanced brackets or not. 770 // or the compiler might have a bug in its naming-things department. 771 // hope to return name[:i], "[...]", ___, "[...]", name[j+1:] 772 depth := 1 // beginning after first "[", looking for balancing "]" 773 rbr, lbr := -1, -1 774 for k, c := range interior { 775 if c == '[' { 776 depth++ 777 if depth != 1 { 778 continue 779 } 780 // rbr != -1 because rbr is only assigned if depth == 0 781 lbr = k 782 break // success, depth == 1, rbr >= 0, lbr > rbr 783 } 784 if c == ']' { 785 depth-- 786 if depth < 0 { 787 break // malformed "...]...]" 788 } 789 if depth != 0 { 790 continue 791 } 792 // cannot execute this twice; depth == 0 -> { ']' -> malformed, '[' -> success } 793 rbr = k 794 } 795 } 796 if depth == 1 { 797 if rbr >= 0 && lbr > rbr { 798 return name[:i], "[...]", interior[rbr+1 : lbr], "[...]", name[j+1:] 799 } 800 if rbr == -1 && lbr == -1 { 801 // the bracket seen in the interior must have been balanced in a "[]" pattern, not "][" 802 // return the single-brackets (not a generic method of a generic type) result 803 return name[:i], "[...]", name[j+1:], "", "" 804 } 805 } 806 807 // malformed, return the whole name 808 return name, "", "", "", "" 809 810 } 811 812 // funcNameForPrint returns the function name for printing to the user. 813 func funcNameForPrint(name string) string { 814 a, b, c, d, e := funcNamePiecesForPrint(name) 815 return a + b + c + d + e 816 } 817 818 // printFuncName prints a function name. name is the function name in 819 // the binary's func data table. 820 func printFuncName(name string) { 821 if name == "runtime.gopanic" { 822 print("panic") 823 return 824 } 825 a, b, c, d, e := funcNamePiecesForPrint(name) 826 print(a, b, c, d, e) 827 } 828 829 func printcreatedby(gp *g) { 830 // Show what created goroutine, except main goroutine (goid 1). 831 pc := gp.gopc 832 f := findfunc(pc) 833 if f.valid() && showframe(f.srcFunc(), gp, false, abi.FuncIDNormal) && gp.goid != 1 { 834 printcreatedby1(f, pc, gp.parentGoid) 835 } 836 } 837 838 func printcreatedby1(f funcInfo, pc uintptr, goid uint64) { 839 print("created by ") 840 printFuncName(funcname(f)) 841 if goid != 0 { 842 print(" in goroutine ", goid) 843 } 844 print("\n") 845 tracepc := pc // back up to CALL instruction for funcline. 846 if pc > f.entry() { 847 tracepc -= sys.PCQuantum 848 } 849 file, line := funcline(f, tracepc) 850 print("\t", file, ":", line) 851 if pc > f.entry() { 852 print(" +", hex(pc-f.entry())) 853 } 854 print("\n") 855 } 856 857 func traceback(pc, sp, lr uintptr, gp *g) { 858 traceback1(pc, sp, lr, gp, 0) 859 } 860 861 // tracebacktrap is like traceback but expects that the PC and SP were obtained 862 // from a trap, not from gp->sched or gp->syscallpc/gp->syscallsp or GetCallerPC/GetCallerSP. 863 // Because they are from a trap instead of from a saved pair, 864 // the initial PC must not be rewound to the previous instruction. 865 // (All the saved pairs record a PC that is a return address, so we 866 // rewind it into the CALL instruction.) 867 // If gp.m.libcall{g,pc,sp} information is available, it uses that information in preference to 868 // the pc/sp/lr passed in. 869 func tracebacktrap(pc, sp, lr uintptr, gp *g) { 870 if gp.m.libcallsp != 0 { 871 // We're in C code somewhere, traceback from the saved position. 872 traceback1(gp.m.libcallpc, gp.m.libcallsp, 0, gp.m.libcallg.ptr(), 0) 873 return 874 } 875 traceback1(pc, sp, lr, gp, unwindTrap) 876 } 877 878 func traceback1(pc, sp, lr uintptr, gp *g, flags unwindFlags) { 879 // If the goroutine is in cgo, and we have a cgo traceback, print that. 880 if iscgo && gp.m != nil && gp.m.ncgo > 0 && gp.syscallsp != 0 && gp.m.cgoCallers != nil && gp.m.cgoCallers[0] != 0 { 881 // Lock cgoCallers so that a signal handler won't 882 // change it, copy the array, reset it, unlock it. 883 // We are locked to the thread and are not running 884 // concurrently with a signal handler. 885 // We just have to stop a signal handler from interrupting 886 // in the middle of our copy. 887 gp.m.cgoCallersUse.Store(1) 888 cgoCallers := *gp.m.cgoCallers 889 gp.m.cgoCallers[0] = 0 890 gp.m.cgoCallersUse.Store(0) 891 892 printCgoTraceback(&cgoCallers) 893 } 894 895 if readgstatus(gp)&^_Gscan == _Gsyscall { 896 // Override registers if blocked in system call. 897 pc = gp.syscallpc 898 sp = gp.syscallsp 899 flags &^= unwindTrap 900 } 901 if gp.m != nil && gp.m.vdsoSP != 0 { 902 // Override registers if running in VDSO. This comes after the 903 // _Gsyscall check to cover VDSO calls after entersyscall. 904 pc = gp.m.vdsoPC 905 sp = gp.m.vdsoSP 906 flags &^= unwindTrap 907 } 908 909 // Print traceback. 910 // 911 // We print the first tracebackInnerFrames frames, and the last 912 // tracebackOuterFrames frames. There are many possible approaches to this. 913 // There are various complications to this: 914 // 915 // - We'd prefer to walk the stack once because in really bad situations 916 // traceback may crash (and we want as much output as possible) or the stack 917 // may be changing. 918 // 919 // - Each physical frame can represent several logical frames, so we might 920 // have to pause in the middle of a physical frame and pick up in the middle 921 // of a physical frame. 922 // 923 // - The cgo symbolizer can expand a cgo PC to more than one logical frame, 924 // and involves juggling state on the C side that we don't manage. Since its 925 // expansion state is managed on the C side, we can't capture the expansion 926 // state part way through, and because the output strings are managed on the 927 // C side, we can't capture the output. Thus, our only choice is to replay a 928 // whole expansion, potentially discarding some of it. 929 // 930 // Rejected approaches: 931 // 932 // - Do two passes where the first pass just counts and the second pass does 933 // all the printing. This is undesirable if the stack is corrupted or changing 934 // because we won't see a partial stack if we panic. 935 // 936 // - Keep a ring buffer of the last N logical frames and use this to print 937 // the bottom frames once we reach the end of the stack. This works, but 938 // requires keeping a surprising amount of state on the stack, and we have 939 // to run the cgo symbolizer twice—once to count frames, and a second to 940 // print them—since we can't retain the strings it returns. 941 // 942 // Instead, we print the outer frames, and if we reach that limit, we clone 943 // the unwinder, count the remaining frames, and then skip forward and 944 // finish printing from the clone. This makes two passes over the outer part 945 // of the stack, but the single pass over the inner part ensures that's 946 // printed immediately and not revisited. It keeps minimal state on the 947 // stack. And through a combination of skip counts and limits, we can do all 948 // of the steps we need with a single traceback printer implementation. 949 // 950 // We could be more lax about exactly how many frames we print, for example 951 // always stopping and resuming on physical frame boundaries, or at least 952 // cgo expansion boundaries. It's not clear that's much simpler. 953 flags |= unwindPrintErrors 954 var u unwinder 955 tracebackWithRuntime := func(showRuntime bool) int { 956 const maxInt int = 0x7fffffff 957 u.initAt(pc, sp, lr, gp, flags) 958 n, lastN := traceback2(&u, showRuntime, 0, tracebackInnerFrames) 959 if n < tracebackInnerFrames { 960 // We printed the whole stack. 961 return n 962 } 963 // Clone the unwinder and figure out how many frames are left. This 964 // count will include any logical frames already printed for u's current 965 // physical frame. 966 u2 := u 967 remaining, _ := traceback2(&u, showRuntime, maxInt, 0) 968 elide := remaining - lastN - tracebackOuterFrames 969 if elide > 0 { 970 print("...", elide, " frames elided...\n") 971 traceback2(&u2, showRuntime, lastN+elide, tracebackOuterFrames) 972 } else if elide <= 0 { 973 // There are tracebackOuterFrames or fewer frames left to print. 974 // Just print the rest of the stack. 975 traceback2(&u2, showRuntime, lastN, tracebackOuterFrames) 976 } 977 return n 978 } 979 // By default, omits runtime frames. If that means we print nothing at all, 980 // repeat forcing all frames printed. 981 if tracebackWithRuntime(false) == 0 { 982 tracebackWithRuntime(true) 983 } 984 printcreatedby(gp) 985 986 if gp.ancestors == nil { 987 return 988 } 989 for _, ancestor := range *gp.ancestors { 990 printAncestorTraceback(ancestor) 991 } 992 } 993 994 // traceback2 prints a stack trace starting at u. It skips the first "skip" 995 // logical frames, after which it prints at most "max" logical frames. It 996 // returns n, which is the number of logical frames skipped and printed, and 997 // lastN, which is the number of logical frames skipped or printed just in the 998 // physical frame that u references. 999 func traceback2(u *unwinder, showRuntime bool, skip, max int) (n, lastN int) { 1000 // commitFrame commits to a logical frame and returns whether this frame 1001 // should be printed and whether iteration should stop. 1002 commitFrame := func() (pr, stop bool) { 1003 if skip == 0 && max == 0 { 1004 // Stop 1005 return false, true 1006 } 1007 n++ 1008 lastN++ 1009 if skip > 0 { 1010 // Skip 1011 skip-- 1012 return false, false 1013 } 1014 // Print 1015 max-- 1016 return true, false 1017 } 1018 1019 gp := u.g.ptr() 1020 level, _, _ := gotraceback() 1021 var cgoBuf [32]uintptr 1022 for ; u.valid(); u.next() { 1023 lastN = 0 1024 f := u.frame.fn 1025 for iu, uf := newInlineUnwinder(f, u.symPC()); uf.valid(); uf = iu.next(uf) { 1026 sf := iu.srcFunc(uf) 1027 callee := u.calleeFuncID 1028 u.calleeFuncID = sf.funcID 1029 if !(showRuntime || showframe(sf, gp, n == 0, callee)) { 1030 continue 1031 } 1032 1033 if pr, stop := commitFrame(); stop { 1034 return 1035 } else if !pr { 1036 continue 1037 } 1038 1039 name := sf.name() 1040 file, line := iu.fileLine(uf) 1041 // Print during crash. 1042 // main(0x1, 0x2, 0x3) 1043 // /home/rsc/go/src/runtime/x.go:23 +0xf 1044 // 1045 printFuncName(name) 1046 print("(") 1047 if iu.isInlined(uf) { 1048 print("...") 1049 } else { 1050 argp := unsafe.Pointer(u.frame.argp) 1051 printArgs(f, argp, u.symPC()) 1052 } 1053 print(")\n") 1054 print("\t", file, ":", line) 1055 if !iu.isInlined(uf) { 1056 if u.frame.pc > f.entry() { 1057 print(" +", hex(u.frame.pc-f.entry())) 1058 } 1059 if gp.m != nil && gp.m.throwing >= throwTypeRuntime && gp == gp.m.curg || level >= 2 { 1060 print(" fp=", hex(u.frame.fp), " sp=", hex(u.frame.sp), " pc=", hex(u.frame.pc)) 1061 } 1062 } 1063 print("\n") 1064 } 1065 1066 // Print cgo frames. 1067 if cgoN := u.cgoCallers(cgoBuf[:]); cgoN > 0 { 1068 var arg cgoSymbolizerArg 1069 anySymbolized := false 1070 stop := false 1071 for _, pc := range cgoBuf[:cgoN] { 1072 if !cgoSymbolizerAvailable() { 1073 if pr, stop := commitFrame(); stop { 1074 break 1075 } else if pr { 1076 print("non-Go function at pc=", hex(pc), "\n") 1077 } 1078 } else { 1079 stop = printOneCgoTraceback(pc, commitFrame, &arg) 1080 anySymbolized = true 1081 if stop { 1082 break 1083 } 1084 } 1085 } 1086 if anySymbolized { 1087 // Free symbolization state. 1088 arg.pc = 0 1089 callCgoSymbolizer(&arg) 1090 } 1091 if stop { 1092 return 1093 } 1094 } 1095 } 1096 return n, 0 1097 } 1098 1099 // printAncestorTraceback prints the traceback of the given ancestor. 1100 // TODO: Unify this with gentraceback and CallersFrames. 1101 func printAncestorTraceback(ancestor ancestorInfo) { 1102 print("[originating from goroutine ", ancestor.goid, "]:\n") 1103 for fidx, pc := range ancestor.pcs { 1104 f := findfunc(pc) // f previously validated 1105 if showfuncinfo(f.srcFunc(), fidx == 0, abi.FuncIDNormal) { 1106 printAncestorTracebackFuncInfo(f, pc) 1107 } 1108 } 1109 if len(ancestor.pcs) == tracebackInnerFrames { 1110 print("...additional frames elided...\n") 1111 } 1112 // Show what created goroutine, except main goroutine (goid 1). 1113 f := findfunc(ancestor.gopc) 1114 if f.valid() && showfuncinfo(f.srcFunc(), false, abi.FuncIDNormal) && ancestor.goid != 1 { 1115 // In ancestor mode, we'll already print the goroutine ancestor. 1116 // Pass 0 for the goid parameter so we don't print it again. 1117 printcreatedby1(f, ancestor.gopc, 0) 1118 } 1119 } 1120 1121 // printAncestorTracebackFuncInfo prints the given function info at a given pc 1122 // within an ancestor traceback. The precision of this info is reduced 1123 // due to only have access to the pcs at the time of the caller 1124 // goroutine being created. 1125 func printAncestorTracebackFuncInfo(f funcInfo, pc uintptr) { 1126 u, uf := newInlineUnwinder(f, pc) 1127 file, line := u.fileLine(uf) 1128 printFuncName(u.srcFunc(uf).name()) 1129 print("(...)\n") 1130 print("\t", file, ":", line) 1131 if pc > f.entry() { 1132 print(" +", hex(pc-f.entry())) 1133 } 1134 print("\n") 1135 } 1136 1137 // callers should be an internal detail, 1138 // (and is almost identical to Callers), 1139 // but widely used packages access it using linkname. 1140 // Notable members of the hall of shame include: 1141 // - github.com/phuslu/log 1142 // 1143 // Do not remove or change the type signature. 1144 // See go.dev/issue/67401. 1145 // 1146 //go:linkname callers 1147 func callers(skip int, pcbuf []uintptr) int { 1148 sp := sys.GetCallerSP() 1149 pc := sys.GetCallerPC() 1150 gp := getg() 1151 var n int 1152 systemstack(func() { 1153 var u unwinder 1154 u.initAt(pc, sp, 0, gp, unwindSilentErrors) 1155 n = tracebackPCs(&u, skip, pcbuf) 1156 }) 1157 return n 1158 } 1159 1160 func gcallers(gp *g, skip int, pcbuf []uintptr) int { 1161 var u unwinder 1162 u.init(gp, unwindSilentErrors) 1163 return tracebackPCs(&u, skip, pcbuf) 1164 } 1165 1166 // showframe reports whether the frame with the given characteristics should 1167 // be printed during a traceback. 1168 func showframe(sf srcFunc, gp *g, firstFrame bool, calleeID abi.FuncID) bool { 1169 mp := getg().m 1170 if mp.throwing >= throwTypeRuntime && gp != nil && (gp == mp.curg || gp == mp.caughtsig.ptr()) { 1171 return true 1172 } 1173 return showfuncinfo(sf, firstFrame, calleeID) 1174 } 1175 1176 // showfuncinfo reports whether a function with the given characteristics should 1177 // be printed during a traceback. 1178 func showfuncinfo(sf srcFunc, firstFrame bool, calleeID abi.FuncID) bool { 1179 level, _, _ := gotraceback() 1180 if level > 1 { 1181 // Show all frames. 1182 return true 1183 } 1184 1185 if sf.funcID == abi.FuncIDWrapper && elideWrapperCalling(calleeID) { 1186 return false 1187 } 1188 1189 // Always show runtime.runFinalizers and runtime.runCleanups as 1190 // context that this goroutine is running finalizers or cleanups, 1191 // otherwise there is no obvious indicator. 1192 // 1193 // TODO(prattmic): A more general approach would be to always show the 1194 // outermost frame (besides runtime.goexit), even if it is a runtime. 1195 // Hiding the outermost frame allows the apparent outermost frame to 1196 // change across different traces, which seems impossible. 1197 // 1198 // Unfortunately, implementing this requires looking ahead at the next 1199 // frame, which goes against traceback's incremental approach (see big 1200 // comment in traceback1). 1201 if sf.funcID == abi.FuncID_runFinalizers || sf.funcID == abi.FuncID_runCleanups { 1202 return true 1203 } 1204 1205 name := sf.name() 1206 1207 // Special case: always show runtime.gopanic frame 1208 // in the middle of a stack trace, so that we can 1209 // see the boundary between ordinary code and 1210 // panic-induced deferred code. 1211 // See golang.org/issue/5832. 1212 if name == "runtime.gopanic" && !firstFrame { 1213 return true 1214 } 1215 1216 return bytealg.IndexByteString(name, '.') >= 0 && (!stringslite.HasPrefix(name, "runtime.") || isExportedRuntime(name)) 1217 } 1218 1219 // isExportedRuntime reports whether name is an exported runtime function. 1220 // It is only for runtime functions, so ASCII A-Z is fine. 1221 func isExportedRuntime(name string) bool { 1222 // Check and remove package qualifier. 1223 name, found := stringslite.CutPrefix(name, "runtime.") 1224 if !found { 1225 return false 1226 } 1227 rcvr := "" 1228 1229 // Extract receiver type, if any. 1230 // For example, runtime.(*Func).Entry 1231 i := len(name) - 1 1232 for i >= 0 && name[i] != '.' { 1233 i-- 1234 } 1235 if i >= 0 { 1236 rcvr = name[:i] 1237 name = name[i+1:] 1238 // Remove parentheses and star for pointer receivers. 1239 if len(rcvr) >= 3 && rcvr[0] == '(' && rcvr[1] == '*' && rcvr[len(rcvr)-1] == ')' { 1240 rcvr = rcvr[2 : len(rcvr)-1] 1241 } 1242 } 1243 1244 // Exported functions and exported methods on exported types. 1245 return len(name) > 0 && 'A' <= name[0] && name[0] <= 'Z' && (len(rcvr) == 0 || 'A' <= rcvr[0] && rcvr[0] <= 'Z') 1246 } 1247 1248 // elideWrapperCalling reports whether a wrapper function that called 1249 // function id should be elided from stack traces. 1250 func elideWrapperCalling(id abi.FuncID) bool { 1251 // If the wrapper called a panic function instead of the 1252 // wrapped function, we want to include it in stacks. 1253 return !(id == abi.FuncID_gopanic || id == abi.FuncID_sigpanic || id == abi.FuncID_panicwrap) 1254 } 1255 1256 var gStatusStrings = [...]string{ 1257 _Gidle: "idle", 1258 _Grunnable: "runnable", 1259 _Grunning: "running", 1260 _Gsyscall: "syscall", 1261 _Gwaiting: "waiting", 1262 _Gdead: "dead", 1263 _Gcopystack: "copystack", 1264 _Gleaked: "leaked", 1265 _Gpreempted: "preempted", 1266 _Gdeadextra: "waiting for cgo callback", 1267 } 1268 1269 func goroutineheader(gp *g) { 1270 level, _, _ := gotraceback() 1271 1272 gpstatus := readgstatus(gp) 1273 1274 isScan := gpstatus&_Gscan != 0 1275 gpstatus &^= _Gscan // drop the scan bit 1276 1277 // Basic string status 1278 var status string 1279 if 0 <= gpstatus && gpstatus < uint32(len(gStatusStrings)) { 1280 status = gStatusStrings[gpstatus] 1281 } else { 1282 status = "???" 1283 } 1284 1285 // Override. 1286 if (gpstatus == _Gwaiting || gpstatus == _Gleaked) && gp.waitreason != waitReasonZero { 1287 status = gp.waitreason.String() 1288 } 1289 1290 // approx time the G is blocked, in minutes 1291 var waitfor int64 1292 if (gpstatus == _Gwaiting || gpstatus == _Gsyscall) && gp.waitsince != 0 { 1293 waitfor = (nanotime() - gp.waitsince) / 60e9 1294 } 1295 print("goroutine ", gp.goid) 1296 if gp.m != nil && gp.m.throwing >= throwTypeRuntime && gp == gp.m.curg || level >= 2 { 1297 print(" gp=", gp) 1298 if gp.m != nil { 1299 print(" m=", gp.m.id, " mp=", gp.m) 1300 } else { 1301 print(" m=nil") 1302 } 1303 } 1304 print(" [", status) 1305 if gpstatus == _Gleaked { 1306 print(" (leaked)") 1307 } 1308 if isScan { 1309 print(" (scan)") 1310 } 1311 if bubble := gp.bubble; bubble != nil && 1312 gpstatus == _Gwaiting && 1313 gp.waitreason.isIdleInSynctest() && 1314 !stringslite.HasSuffix(status, "(durable)") { 1315 // If this isn't a status where the name includes a (durable) 1316 // suffix to distinguish it from the non-durable form, add it here. 1317 print(" (durable)") 1318 } 1319 if waitfor >= 1 { 1320 print(", ", waitfor, " minutes") 1321 } 1322 if gp.lockedm != 0 { 1323 print(", locked to thread") 1324 } 1325 if bubble := gp.bubble; bubble != nil { 1326 print(", synctest bubble ", bubble.id) 1327 } 1328 print("]") 1329 if gp.labels != nil && debug.tracebacklabels.Load() == 1 { 1330 labels := (*label.Set)(gp.labels).List 1331 if len(labels) > 0 { 1332 print(" {") 1333 for i, kv := range labels { 1334 // Try to be nice and only quote the keys/values if one of them has characters that need quoting or escaping. 1335 printq := func(s string) { 1336 if tracebackStringNeedsQuoting(s) { 1337 print(quoted(s)) 1338 } else { 1339 print(s) 1340 } 1341 } 1342 printq(kv.Key) 1343 print(": ") 1344 printq(kv.Value) 1345 if i < len(labels)-1 { 1346 print(", ") 1347 } 1348 } 1349 print("}") 1350 } 1351 } 1352 print(":\n") 1353 } 1354 1355 func tracebackStringNeedsQuoting(s string) bool { 1356 for _, r := range s { 1357 if !('a' <= r && r <= 'z' || 1358 'A' <= r && r <= 'Z' || 1359 '0' <= r && r <= '9' || 1360 r == '.' || r == '/' || r == '_') { 1361 return true 1362 } 1363 } 1364 return false 1365 } 1366 1367 func tracebackothers(me *g) { 1368 tracebacksomeothers(me, func(*g) bool { return true }) 1369 } 1370 1371 func tracebacksomeothers(me *g, showf func(*g) bool) { 1372 level, _, _ := gotraceback() 1373 1374 // Show the current goroutine first, if we haven't already. 1375 curgp := getg().m.curg 1376 if curgp != nil && curgp != me { 1377 print("\n") 1378 goroutineheader(curgp) 1379 traceback(^uintptr(0), ^uintptr(0), 0, curgp) 1380 } 1381 1382 // We can't call locking forEachG here because this may be during fatal 1383 // throw/panic, where locking could be out-of-order or a direct 1384 // deadlock. 1385 // 1386 // Instead, use forEachGRace, which requires no locking. We don't lock 1387 // against concurrent creation of new Gs, but even with allglock we may 1388 // miss Gs created after this loop. 1389 forEachGRace(func(gp *g) { 1390 if gp == me || gp == curgp { 1391 return 1392 } 1393 if status := readgstatus(gp); status == _Gdead || status == _Gdeadextra { 1394 return 1395 } 1396 if !showf(gp) { 1397 return 1398 } 1399 if isSystemGoroutine(gp, false) && level < 2 { 1400 return 1401 } 1402 print("\n") 1403 goroutineheader(gp) 1404 // Note: gp.m == getg().m occurs when tracebackothers is called 1405 // from a signal handler initiated during a systemstack call. 1406 // The original G is still in the running state, and we want to 1407 // print its stack. 1408 // 1409 // There's a small window of time in exitsyscall where a goroutine could be 1410 // in _Grunning as it's exiting a syscall. This could be the case even if the 1411 // world is stopped or frozen. 1412 // 1413 // This is OK because the goroutine will not exit the syscall while the world 1414 // is stopped or frozen. This is also why it's safe to check syscallsp here, 1415 // and safe to take the goroutine's stack trace. The syscall path mutates 1416 // syscallsp only just before exiting the syscall. 1417 if gp.m != getg().m && readgstatus(gp)&^_Gscan == _Grunning && gp.syscallsp == 0 { 1418 print("\tgoroutine running on other thread; stack unavailable\n") 1419 printcreatedby(gp) 1420 } else { 1421 traceback(^uintptr(0), ^uintptr(0), 0, gp) 1422 } 1423 }) 1424 } 1425 1426 // tracebackHexdump hexdumps part of stk around frame.sp and frame.fp 1427 // for debugging purposes. If the address bad is included in the 1428 // hexdumped range, it will mark it as well. 1429 func tracebackHexdump(stk stack, frame *stkframe, bad uintptr) { 1430 const expand = 32 * goarch.PtrSize 1431 const maxExpand = 256 * goarch.PtrSize 1432 // Start around frame.sp. 1433 lo, hi := frame.sp, frame.sp 1434 // Expand to include frame.fp. 1435 if frame.fp != 0 && frame.fp < lo { 1436 lo = frame.fp 1437 } 1438 if frame.fp != 0 && frame.fp > hi { 1439 hi = frame.fp 1440 } 1441 // Expand a bit more. 1442 lo, hi = lo-expand, hi+expand 1443 // But don't go too far from frame.sp. 1444 if lo < frame.sp-maxExpand { 1445 lo = frame.sp - maxExpand 1446 } 1447 if hi > frame.sp+maxExpand { 1448 hi = frame.sp + maxExpand 1449 } 1450 // And don't go outside the stack bounds. 1451 if lo < stk.lo { 1452 lo = stk.lo 1453 } 1454 if hi > stk.hi { 1455 hi = stk.hi 1456 } 1457 1458 // Print the hex dump. 1459 print("stack: frame={sp:", hex(frame.sp), ", fp:", hex(frame.fp), "} stack=[", hex(stk.lo), ",", hex(stk.hi), ")\n") 1460 hexdumpWords(lo, hi-lo, func(p uintptr, m hexdumpMarker) { 1461 if p == frame.fp { 1462 m.start() 1463 println("FP") 1464 } 1465 if p == frame.sp { 1466 m.start() 1467 println("SP") 1468 } 1469 if p == bad { 1470 m.start() 1471 println("bad") 1472 } 1473 }) 1474 } 1475 1476 // isSystemGoroutine reports whether the goroutine g must be omitted 1477 // in stack dumps and deadlock detector. This is any goroutine that 1478 // starts at a runtime.* entry point, except for runtime.main, 1479 // runtime.handleAsyncEvent (wasm only) and sometimes 1480 // runtime.runFinalizers/runtime.runCleanups. 1481 // 1482 // If fixed is true, any goroutine that can vary between user and 1483 // system (that is, the finalizer goroutine) is considered a user 1484 // goroutine. 1485 func isSystemGoroutine(gp *g, fixed bool) bool { 1486 // Keep this in sync with internal/trace.IsSystemGoroutine. 1487 f := findfunc(gp.startpc) 1488 if !f.valid() { 1489 return false 1490 } 1491 if f.funcID == abi.FuncID_runtime_main || f.funcID == abi.FuncID_corostart || f.funcID == abi.FuncID_handleAsyncEvent { 1492 return false 1493 } 1494 if f.funcID == abi.FuncID_runFinalizers { 1495 // We include the finalizer goroutine if it's calling 1496 // back into user code. 1497 if fixed { 1498 // This goroutine can vary. In fixed mode, 1499 // always consider it a user goroutine. 1500 return false 1501 } 1502 return fingStatus.Load()&fingRunningFinalizer == 0 1503 } 1504 if f.funcID == abi.FuncID_runCleanups { 1505 // We include the cleanup goroutines if they're calling 1506 // back into user code. 1507 if fixed { 1508 // This goroutine can vary. In fixed mode, 1509 // always consider it a user goroutine. 1510 return false 1511 } 1512 return !gp.runningCleanups.Load() 1513 } 1514 return stringslite.HasPrefix(funcname(f), "runtime.") 1515 } 1516 1517 // SetCgoTraceback records three C functions to use to gather 1518 // traceback information from C code and to convert that traceback 1519 // information into symbolic information. These are used when printing 1520 // stack traces for a program that uses cgo. 1521 // 1522 // The traceback and context functions may be called from a signal 1523 // handler, and must therefore use only async-signal safe functions. 1524 // The symbolizer function may be called while the program is 1525 // crashing, and so must be cautious about using memory. None of the 1526 // functions may call back into Go. 1527 // 1528 // The context function will be called with a single argument, a 1529 // pointer to a struct: 1530 // 1531 // struct { 1532 // Context uintptr 1533 // } 1534 // 1535 // In C syntax, this struct will be 1536 // 1537 // struct { 1538 // uintptr_t Context; 1539 // }; 1540 // 1541 // If the Context field is 0, the context function is being called to 1542 // record the current traceback context. It should record in the 1543 // Context field whatever information is needed about the current 1544 // point of execution to later produce a stack trace, probably the 1545 // stack pointer and PC. In this case the context function will be 1546 // called from C code. 1547 // 1548 // If the Context field is not 0, then it is a value returned by a 1549 // previous call to the context function. This case is called when the 1550 // context is no longer needed; that is, when the Go code is returning 1551 // to its C code caller. This permits the context function to release 1552 // any associated resources. 1553 // 1554 // While it would be correct for the context function to record a 1555 // complete a stack trace whenever it is called, and simply copy that 1556 // out in the traceback function, in a typical program the context 1557 // function will be called many times without ever recording a 1558 // traceback for that context. Recording a complete stack trace in a 1559 // call to the context function is likely to be inefficient. 1560 // 1561 // The traceback function will be called with a single argument, a 1562 // pointer to a struct: 1563 // 1564 // struct { 1565 // Context uintptr 1566 // SigContext uintptr 1567 // Buf *uintptr 1568 // Max uintptr 1569 // } 1570 // 1571 // In C syntax, this struct will be 1572 // 1573 // struct { 1574 // uintptr_t Context; 1575 // uintptr_t SigContext; 1576 // uintptr_t* Buf; 1577 // uintptr_t Max; 1578 // }; 1579 // 1580 // The Context field will be zero to gather a traceback from the 1581 // current program execution point. In this case, the traceback 1582 // function will be called from C code. 1583 // 1584 // Otherwise Context will be a value previously returned by a call to 1585 // the context function. The traceback function should gather a stack 1586 // trace from that saved point in the program execution. The traceback 1587 // function may be called from an execution thread other than the one 1588 // that recorded the context, but only when the context is known to be 1589 // valid and unchanging. The traceback function may also be called 1590 // deeper in the call stack on the same thread that recorded the 1591 // context. The traceback function may be called multiple times with 1592 // the same Context value; it will usually be appropriate to cache the 1593 // result, if possible, the first time this is called for a specific 1594 // context value. 1595 // 1596 // If the traceback function is called from a signal handler on a Unix 1597 // system, SigContext will be the signal context argument passed to 1598 // the signal handler (a C ucontext_t* cast to uintptr_t). This may be 1599 // used to start tracing at the point where the signal occurred. If 1600 // the traceback function is not called from a signal handler, 1601 // SigContext will be zero. 1602 // 1603 // Buf is where the traceback information should be stored. It should 1604 // be PC values, such that Buf[0] is the PC of the caller, Buf[1] is 1605 // the PC of that function's caller, and so on. Max is the maximum 1606 // number of entries to store. The function should store a zero to 1607 // indicate the top of the stack, or that the caller is on a different 1608 // stack, presumably a Go stack. 1609 // 1610 // Unlike runtime.Callers, the PC values returned should, when passed 1611 // to the symbolizer function, return the file/line of the call 1612 // instruction. No additional subtraction is required or appropriate. 1613 // 1614 // On all platforms, the traceback function is invoked when a call from 1615 // Go to C to Go requests a stack trace. On linux/amd64, linux/ppc64le, 1616 // linux/arm64, and freebsd/amd64, the traceback function is also invoked 1617 // when a signal is received by a thread that is executing a cgo call. 1618 // The traceback function should not make assumptions about when it is 1619 // called, as future versions of Go may make additional calls. 1620 // 1621 // The symbolizer function will be called with a single argument, a 1622 // pointer to a struct: 1623 // 1624 // struct { 1625 // PC uintptr // program counter to fetch information for 1626 // File *byte // file name (NUL terminated) 1627 // Lineno uintptr // line number 1628 // Func *byte // function name (NUL terminated) 1629 // Entry uintptr // function entry point 1630 // More uintptr // set non-zero if more info for this PC 1631 // Data uintptr // unused by runtime, available for function 1632 // } 1633 // 1634 // In C syntax, this struct will be 1635 // 1636 // struct { 1637 // uintptr_t PC; 1638 // char* File; 1639 // uintptr_t Lineno; 1640 // char* Func; 1641 // uintptr_t Entry; 1642 // uintptr_t More; 1643 // uintptr_t Data; 1644 // }; 1645 // 1646 // The PC field will be a value returned by a call to the traceback 1647 // function. 1648 // 1649 // The first time the function is called for a particular traceback, 1650 // all the fields except PC will be 0. The function should fill in the 1651 // other fields if possible, setting them to 0/nil if the information 1652 // is not available. The Data field may be used to store any useful 1653 // information across calls. The More field should be set to non-zero 1654 // if there is more information for this PC, zero otherwise. If More 1655 // is set non-zero, the function will be called again with the same 1656 // PC, and may return different information (this is intended for use 1657 // with inlined functions). If More is zero, the function will be 1658 // called with the next PC value in the traceback. When the traceback 1659 // is complete, the function will be called once more with PC set to 1660 // zero; this may be used to free any information. Each call will 1661 // leave the fields of the struct set to the same values they had upon 1662 // return, except for the PC field when the More field is zero. The 1663 // function must not keep a copy of the struct pointer between calls. 1664 // 1665 // When calling SetCgoTraceback, the version argument is the version 1666 // number of the structs that the functions expect to receive. 1667 // Currently this must be zero. 1668 // 1669 // The symbolizer function may be nil, in which case the results of 1670 // the traceback function will be displayed as numbers. If the 1671 // traceback function is nil, the symbolizer function will never be 1672 // called. The context function may be nil, in which case the 1673 // traceback function will only be called with the context field set 1674 // to zero. If the context function is nil, then calls from Go to C 1675 // to Go will not show a traceback for the C portion of the call stack. 1676 // 1677 // SetCgoTraceback should be called only once, ideally from an init function. 1678 func SetCgoTraceback(version int, traceback, context, symbolizer unsafe.Pointer) { 1679 if version != 0 { 1680 panic("unsupported version") 1681 } 1682 1683 if cgoTraceback != nil && cgoTraceback != traceback || 1684 cgoContext != nil && cgoContext != context || 1685 cgoSymbolizer != nil && cgoSymbolizer != symbolizer { 1686 panic("call SetCgoTraceback only once") 1687 } 1688 1689 cgoTraceback = traceback 1690 cgoContext = context 1691 cgoSymbolizer = symbolizer 1692 1693 if _cgo_set_traceback_functions != nil { 1694 type cgoSetTracebackFunctionsArg struct { 1695 traceback unsafe.Pointer 1696 context unsafe.Pointer 1697 symbolizer unsafe.Pointer 1698 } 1699 arg := cgoSetTracebackFunctionsArg{ 1700 traceback: traceback, 1701 context: context, 1702 symbolizer: symbolizer, 1703 } 1704 cgocall(_cgo_set_traceback_functions, noescape(unsafe.Pointer(&arg))) 1705 } 1706 } 1707 1708 var cgoTraceback unsafe.Pointer 1709 var cgoContext unsafe.Pointer 1710 var cgoSymbolizer unsafe.Pointer 1711 1712 func cgoTracebackAvailable() bool { 1713 // - The traceback function must be registered via SetCgoTraceback. 1714 // - This must be a cgo binary (providing _cgo_call_traceback_function). 1715 return cgoTraceback != nil && _cgo_call_traceback_function != nil 1716 } 1717 1718 func cgoSymbolizerAvailable() bool { 1719 // - The symbolizer function must be registered via SetCgoTraceback. 1720 // - This must be a cgo binary (providing _cgo_call_symbolizer_function). 1721 return cgoSymbolizer != nil && _cgo_call_symbolizer_function != nil 1722 } 1723 1724 // cgoTracebackArg is the type passed to cgoTraceback. 1725 type cgoTracebackArg struct { 1726 context uintptr 1727 sigContext uintptr 1728 buf *uintptr 1729 max uintptr 1730 } 1731 1732 // cgoContextArg is the type passed to the context function. 1733 type cgoContextArg struct { 1734 context uintptr 1735 } 1736 1737 // cgoSymbolizerArg is the type passed to cgoSymbolizer. 1738 type cgoSymbolizerArg struct { 1739 pc uintptr 1740 file *byte 1741 lineno uintptr 1742 funcName *byte 1743 entry uintptr 1744 more uintptr 1745 data uintptr 1746 } 1747 1748 // printCgoTraceback prints a traceback of callers. 1749 func printCgoTraceback(callers *cgoCallers) { 1750 if !cgoSymbolizerAvailable() { 1751 for _, c := range callers { 1752 if c == 0 { 1753 break 1754 } 1755 print("non-Go function at pc=", hex(c), "\n") 1756 } 1757 return 1758 } 1759 1760 commitFrame := func() (pr, stop bool) { return true, false } 1761 var arg cgoSymbolizerArg 1762 for _, c := range callers { 1763 if c == 0 { 1764 break 1765 } 1766 printOneCgoTraceback(c, commitFrame, &arg) 1767 } 1768 arg.pc = 0 1769 callCgoSymbolizer(&arg) 1770 } 1771 1772 // printOneCgoTraceback prints the traceback of a single cgo caller. 1773 // This can print more than one line because of inlining. 1774 // It returns the "stop" result of commitFrame. 1775 // 1776 // Preconditions: cgoSymbolizerAvailable returns true. 1777 func printOneCgoTraceback(pc uintptr, commitFrame func() (pr, stop bool), arg *cgoSymbolizerArg) bool { 1778 arg.pc = pc 1779 for { 1780 if pr, stop := commitFrame(); stop { 1781 return true 1782 } else if !pr { 1783 continue 1784 } 1785 1786 callCgoSymbolizer(arg) 1787 if arg.funcName != nil { 1788 // Note that we don't print any argument 1789 // information here, not even parentheses. 1790 // The symbolizer must add that if appropriate. 1791 println(gostringnocopy(arg.funcName)) 1792 } else { 1793 println("non-Go function") 1794 } 1795 print("\t") 1796 if arg.file != nil { 1797 print(gostringnocopy(arg.file), ":", arg.lineno, " ") 1798 } 1799 print("pc=", hex(pc), "\n") 1800 if arg.more == 0 { 1801 return false 1802 } 1803 } 1804 } 1805 1806 // callCgoSymbolizer calls the cgoSymbolizer function. 1807 // 1808 // Preconditions: cgoSymbolizerAvailable returns true. 1809 func callCgoSymbolizer(arg *cgoSymbolizerArg) { 1810 call := cgocall 1811 if panicking.Load() > 0 || getg().m.curg != getg() { 1812 // We do not want to call into the scheduler when panicking 1813 // or when on the system stack. 1814 call = asmcgocall 1815 } 1816 if msanenabled { 1817 msanwrite(unsafe.Pointer(arg), unsafe.Sizeof(cgoSymbolizerArg{})) 1818 } 1819 if asanenabled { 1820 asanwrite(unsafe.Pointer(arg), unsafe.Sizeof(cgoSymbolizerArg{})) 1821 } 1822 call(_cgo_call_symbolizer_function, noescape(unsafe.Pointer(arg))) 1823 } 1824 1825 // cgoContextPCs gets the PC values from a cgo traceback. 1826 // 1827 // Preconditions: cgoTracebackAvailable returns true. 1828 func cgoContextPCs(ctxt uintptr, buf []uintptr) { 1829 call := cgocall 1830 if panicking.Load() > 0 || getg().m.curg != getg() { 1831 // We do not want to call into the scheduler when panicking 1832 // or when on the system stack. 1833 call = asmcgocall 1834 } 1835 arg := cgoTracebackArg{ 1836 context: ctxt, 1837 buf: (*uintptr)(noescape(unsafe.Pointer(&buf[0]))), 1838 max: uintptr(len(buf)), 1839 } 1840 if msanenabled { 1841 msanwrite(unsafe.Pointer(&arg), unsafe.Sizeof(arg)) 1842 } 1843 if asanenabled { 1844 asanwrite(unsafe.Pointer(&arg), unsafe.Sizeof(arg)) 1845 } 1846 call(_cgo_call_traceback_function, noescape(unsafe.Pointer(&arg))) 1847 } 1848