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