github.com/q45/go@v0.0.0-20151101211701-a4fb8c13db3f/src/runtime/traceback.go (about) 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 "unsafe" 8 9 // The code in this file implements stack trace walking for all architectures. 10 // The most important fact about a given architecture is whether it uses a link register. 11 // On systems with link registers, the prologue for a non-leaf function stores the 12 // incoming value of LR at the bottom of the newly allocated stack frame. 13 // On systems without link registers, the architecture pushes a return PC during 14 // the call instruction, so the return PC ends up above the stack frame. 15 // In this file, the return PC is always called LR, no matter how it was found. 16 // 17 // To date, the opposite of a link register architecture is an x86 architecture. 18 // This code may need to change if some other kind of non-link-register 19 // architecture comes along. 20 // 21 // The other important fact is the size of a pointer: on 32-bit systems the LR 22 // takes up only 4 bytes on the stack, while on 64-bit systems it takes up 8 bytes. 23 // Typically this is ptrSize. 24 // 25 // As an exception, amd64p32 has ptrSize == 4 but the CALL instruction still 26 // stores an 8-byte return PC onto the stack. To accommodate this, we use regSize 27 // as the size of the architecture-pushed return PC. 28 // 29 // usesLR is defined below in terms of minFrameSize, which is defined in 30 // arch_$GOARCH.go. ptrSize and regSize are defined in stubs.go. 31 32 const usesLR = minFrameSize > 0 33 34 var ( 35 // initialized in tracebackinit 36 goexitPC uintptr 37 jmpdeferPC uintptr 38 mcallPC uintptr 39 morestackPC uintptr 40 mstartPC uintptr 41 rt0_goPC uintptr 42 sigpanicPC uintptr 43 runfinqPC uintptr 44 backgroundgcPC uintptr 45 bgsweepPC uintptr 46 forcegchelperPC uintptr 47 timerprocPC uintptr 48 gcBgMarkWorkerPC uintptr 49 systemstack_switchPC uintptr 50 systemstackPC uintptr 51 stackBarrierPC uintptr 52 cgocallback_gofuncPC uintptr 53 54 gogoPC uintptr 55 56 externalthreadhandlerp uintptr // initialized elsewhere 57 ) 58 59 func tracebackinit() { 60 // Go variable initialization happens late during runtime startup. 61 // Instead of initializing the variables above in the declarations, 62 // schedinit calls this function so that the variables are 63 // initialized and available earlier in the startup sequence. 64 goexitPC = funcPC(goexit) 65 jmpdeferPC = funcPC(jmpdefer) 66 mcallPC = funcPC(mcall) 67 morestackPC = funcPC(morestack) 68 mstartPC = funcPC(mstart) 69 rt0_goPC = funcPC(rt0_go) 70 sigpanicPC = funcPC(sigpanic) 71 runfinqPC = funcPC(runfinq) 72 backgroundgcPC = funcPC(backgroundgc) 73 bgsweepPC = funcPC(bgsweep) 74 forcegchelperPC = funcPC(forcegchelper) 75 timerprocPC = funcPC(timerproc) 76 gcBgMarkWorkerPC = funcPC(gcBgMarkWorker) 77 systemstack_switchPC = funcPC(systemstack_switch) 78 systemstackPC = funcPC(systemstack) 79 stackBarrierPC = funcPC(stackBarrier) 80 cgocallback_gofuncPC = funcPC(cgocallback_gofunc) 81 82 // used by sigprof handler 83 gogoPC = funcPC(gogo) 84 } 85 86 // Traceback over the deferred function calls. 87 // Report them like calls that have been invoked but not started executing yet. 88 func tracebackdefers(gp *g, callback func(*stkframe, unsafe.Pointer) bool, v unsafe.Pointer) { 89 var frame stkframe 90 for d := gp._defer; d != nil; d = d.link { 91 fn := d.fn 92 if fn == nil { 93 // Defer of nil function. Args don't matter. 94 frame.pc = 0 95 frame.fn = nil 96 frame.argp = 0 97 frame.arglen = 0 98 frame.argmap = nil 99 } else { 100 frame.pc = uintptr(fn.fn) 101 f := findfunc(frame.pc) 102 if f == nil { 103 print("runtime: unknown pc in defer ", hex(frame.pc), "\n") 104 throw("unknown pc") 105 } 106 frame.fn = f 107 frame.argp = uintptr(deferArgs(d)) 108 setArgInfo(&frame, f, true) 109 } 110 frame.continpc = frame.pc 111 if !callback((*stkframe)(noescape(unsafe.Pointer(&frame))), v) { 112 return 113 } 114 } 115 } 116 117 // Generic traceback. Handles runtime stack prints (pcbuf == nil), 118 // the runtime.Callers function (pcbuf != nil), as well as the garbage 119 // collector (callback != nil). A little clunky to merge these, but avoids 120 // duplicating the code and all its subtlety. 121 func gentraceback(pc0, sp0, lr0 uintptr, gp *g, skip int, pcbuf *uintptr, max int, callback func(*stkframe, unsafe.Pointer) bool, v unsafe.Pointer, flags uint) int { 122 if goexitPC == 0 { 123 throw("gentraceback before goexitPC initialization") 124 } 125 g := getg() 126 if g == gp && g == g.m.curg { 127 // The starting sp has been passed in as a uintptr, and the caller may 128 // have other uintptr-typed stack references as well. 129 // If during one of the calls that got us here or during one of the 130 // callbacks below the stack must be grown, all these uintptr references 131 // to the stack will not be updated, and gentraceback will continue 132 // to inspect the old stack memory, which may no longer be valid. 133 // Even if all the variables were updated correctly, it is not clear that 134 // we want to expose a traceback that begins on one stack and ends 135 // on another stack. That could confuse callers quite a bit. 136 // Instead, we require that gentraceback and any other function that 137 // accepts an sp for the current goroutine (typically obtained by 138 // calling getcallersp) must not run on that goroutine's stack but 139 // instead on the g0 stack. 140 throw("gentraceback cannot trace user goroutine on its own stack") 141 } 142 level, _, _ := gotraceback() 143 144 // Fix up returns to the stack barrier by fetching the 145 // original return PC from gp.stkbar. 146 stkbar := gp.stkbar[gp.stkbarPos:] 147 148 if pc0 == ^uintptr(0) && sp0 == ^uintptr(0) { // Signal to fetch saved values from gp. 149 if gp.syscallsp != 0 { 150 pc0 = gp.syscallpc 151 sp0 = gp.syscallsp 152 if usesLR { 153 lr0 = 0 154 } 155 } else { 156 pc0 = gp.sched.pc 157 sp0 = gp.sched.sp 158 if usesLR { 159 lr0 = gp.sched.lr 160 } 161 } 162 } 163 164 nprint := 0 165 var frame stkframe 166 frame.pc = pc0 167 frame.sp = sp0 168 if usesLR { 169 frame.lr = lr0 170 } 171 waspanic := false 172 printing := pcbuf == nil && callback == nil 173 _defer := gp._defer 174 175 for _defer != nil && uintptr(_defer.sp) == _NoArgs { 176 _defer = _defer.link 177 } 178 179 // If the PC is zero, it's likely a nil function call. 180 // Start in the caller's frame. 181 if frame.pc == 0 { 182 if usesLR { 183 frame.pc = *(*uintptr)(unsafe.Pointer(frame.sp)) 184 frame.lr = 0 185 } else { 186 frame.pc = uintptr(*(*uintreg)(unsafe.Pointer(frame.sp))) 187 frame.sp += regSize 188 } 189 } 190 191 f := findfunc(frame.pc) 192 if f == nil { 193 if callback != nil { 194 print("runtime: unknown pc ", hex(frame.pc), "\n") 195 throw("unknown pc") 196 } 197 return 0 198 } 199 frame.fn = f 200 201 var cache pcvalueCache 202 203 n := 0 204 for n < max { 205 // Typically: 206 // pc is the PC of the running function. 207 // sp is the stack pointer at that program counter. 208 // fp is the frame pointer (caller's stack pointer) at that program counter, or nil if unknown. 209 // stk is the stack containing sp. 210 // The caller's program counter is lr, unless lr is zero, in which case it is *(uintptr*)sp. 211 f = frame.fn 212 213 // Found an actual function. 214 // Derive frame pointer and link register. 215 if frame.fp == 0 { 216 // We want to jump over the systemstack switch. If we're running on the 217 // g0, this systemstack is at the top of the stack. 218 // if we're not on g0 or there's a no curg, then this is a regular call. 219 sp := frame.sp 220 if flags&_TraceJumpStack != 0 && f.entry == systemstackPC && gp == g.m.g0 && gp.m.curg != nil { 221 sp = gp.m.curg.sched.sp 222 stkbar = gp.m.curg.stkbar[gp.m.curg.stkbarPos:] 223 } 224 frame.fp = sp + uintptr(funcspdelta(f, frame.pc, &cache)) 225 if !usesLR { 226 // On x86, call instruction pushes return PC before entering new function. 227 frame.fp += regSize 228 } 229 } 230 var flr *_func 231 if topofstack(f) { 232 frame.lr = 0 233 flr = nil 234 } else if usesLR && f.entry == jmpdeferPC { 235 // jmpdefer modifies SP/LR/PC non-atomically. 236 // If a profiling interrupt arrives during jmpdefer, 237 // the stack unwind may see a mismatched register set 238 // and get confused. Stop if we see PC within jmpdefer 239 // to avoid that confusion. 240 // See golang.org/issue/8153. 241 if callback != nil { 242 throw("traceback_arm: found jmpdefer when tracing with callback") 243 } 244 frame.lr = 0 245 } else { 246 var lrPtr uintptr 247 if usesLR { 248 if n == 0 && frame.sp < frame.fp || frame.lr == 0 { 249 lrPtr = frame.sp 250 frame.lr = *(*uintptr)(unsafe.Pointer(lrPtr)) 251 } 252 } else { 253 if frame.lr == 0 { 254 lrPtr = frame.fp - regSize 255 frame.lr = uintptr(*(*uintreg)(unsafe.Pointer(lrPtr))) 256 } 257 } 258 if frame.lr == stackBarrierPC { 259 // Recover original PC. 260 if stkbar[0].savedLRPtr != lrPtr { 261 print("found next stack barrier at ", hex(lrPtr), "; expected ") 262 gcPrintStkbars(stkbar) 263 print("\n") 264 throw("missed stack barrier") 265 } 266 frame.lr = stkbar[0].savedLRVal 267 stkbar = stkbar[1:] 268 } 269 flr = findfunc(frame.lr) 270 if flr == nil { 271 // This happens if you get a profiling interrupt at just the wrong time. 272 // In that context it is okay to stop early. 273 // But if callback is set, we're doing a garbage collection and must 274 // get everything, so crash loudly. 275 if callback != nil { 276 print("runtime: unexpected return pc for ", funcname(f), " called from ", hex(frame.lr), "\n") 277 throw("unknown caller pc") 278 } 279 } 280 } 281 282 frame.varp = frame.fp 283 if !usesLR { 284 // On x86, call instruction pushes return PC before entering new function. 285 frame.varp -= regSize 286 } 287 288 // If framepointer_enabled and there's a frame, then 289 // there's a saved bp here. 290 if framepointer_enabled && GOARCH == "amd64" && frame.varp > frame.sp { 291 frame.varp -= regSize 292 } 293 294 // Derive size of arguments. 295 // Most functions have a fixed-size argument block, 296 // so we can use metadata about the function f. 297 // Not all, though: there are some variadic functions 298 // in package runtime and reflect, and for those we use call-specific 299 // metadata recorded by f's caller. 300 if callback != nil || printing { 301 frame.argp = frame.fp + minFrameSize 302 setArgInfo(&frame, f, callback != nil) 303 } 304 305 // Determine frame's 'continuation PC', where it can continue. 306 // Normally this is the return address on the stack, but if sigpanic 307 // is immediately below this function on the stack, then the frame 308 // stopped executing due to a trap, and frame.pc is probably not 309 // a safe point for looking up liveness information. In this panicking case, 310 // the function either doesn't return at all (if it has no defers or if the 311 // defers do not recover) or it returns from one of the calls to 312 // deferproc a second time (if the corresponding deferred func recovers). 313 // It suffices to assume that the most recent deferproc is the one that 314 // returns; everything live at earlier deferprocs is still live at that one. 315 frame.continpc = frame.pc 316 if waspanic { 317 if _defer != nil && _defer.sp == frame.sp { 318 frame.continpc = _defer.pc 319 } else { 320 frame.continpc = 0 321 } 322 } 323 324 // Unwind our local defer stack past this frame. 325 for _defer != nil && (_defer.sp == frame.sp || _defer.sp == _NoArgs) { 326 _defer = _defer.link 327 } 328 329 if skip > 0 { 330 skip-- 331 goto skipped 332 } 333 334 if pcbuf != nil { 335 (*[1 << 20]uintptr)(unsafe.Pointer(pcbuf))[n] = frame.pc 336 } 337 if callback != nil { 338 if !callback((*stkframe)(noescape(unsafe.Pointer(&frame))), v) { 339 return n 340 } 341 } 342 if printing { 343 if (flags&_TraceRuntimeFrames) != 0 || showframe(f, gp) { 344 // Print during crash. 345 // main(0x1, 0x2, 0x3) 346 // /home/rsc/go/src/runtime/x.go:23 +0xf 347 // 348 tracepc := frame.pc // back up to CALL instruction for funcline. 349 if (n > 0 || flags&_TraceTrap == 0) && frame.pc > f.entry && !waspanic { 350 tracepc-- 351 } 352 print(funcname(f), "(") 353 argp := (*[100]uintptr)(unsafe.Pointer(frame.argp)) 354 for i := uintptr(0); i < frame.arglen/ptrSize; i++ { 355 if i >= 10 { 356 print(", ...") 357 break 358 } 359 if i != 0 { 360 print(", ") 361 } 362 print(hex(argp[i])) 363 } 364 print(")\n") 365 file, line := funcline(f, tracepc) 366 print("\t", file, ":", line) 367 if frame.pc > f.entry { 368 print(" +", hex(frame.pc-f.entry)) 369 } 370 if g.m.throwing > 0 && gp == g.m.curg || level >= 2 { 371 print(" fp=", hex(frame.fp), " sp=", hex(frame.sp)) 372 } 373 print("\n") 374 nprint++ 375 } 376 } 377 n++ 378 379 skipped: 380 waspanic = f.entry == sigpanicPC 381 382 // Do not unwind past the bottom of the stack. 383 if flr == nil { 384 break 385 } 386 387 // Unwind to next frame. 388 frame.fn = flr 389 frame.pc = frame.lr 390 frame.lr = 0 391 frame.sp = frame.fp 392 frame.fp = 0 393 frame.argmap = nil 394 395 // On link register architectures, sighandler saves the LR on stack 396 // before faking a call to sigpanic. 397 if usesLR && waspanic { 398 x := *(*uintptr)(unsafe.Pointer(frame.sp)) 399 frame.sp += minFrameSize 400 if GOARCH == "arm64" { 401 // arm64 needs 16-byte aligned SP, always 402 frame.sp += ptrSize 403 } 404 f = findfunc(frame.pc) 405 frame.fn = f 406 if f == nil { 407 frame.pc = x 408 } else if funcspdelta(f, frame.pc, &cache) == 0 { 409 frame.lr = x 410 } 411 } 412 } 413 414 if printing { 415 n = nprint 416 } 417 418 // If callback != nil, we're being called to gather stack information during 419 // garbage collection or stack growth. In that context, require that we used 420 // up the entire defer stack. If not, then there is a bug somewhere and the 421 // garbage collection or stack growth may not have seen the correct picture 422 // of the stack. Crash now instead of silently executing the garbage collection 423 // or stack copy incorrectly and setting up for a mysterious crash later. 424 // 425 // Note that panic != nil is okay here: there can be leftover panics, 426 // because the defers on the panic stack do not nest in frame order as 427 // they do on the defer stack. If you have: 428 // 429 // frame 1 defers d1 430 // frame 2 defers d2 431 // frame 3 defers d3 432 // frame 4 panics 433 // frame 4's panic starts running defers 434 // frame 5, running d3, defers d4 435 // frame 5 panics 436 // frame 5's panic starts running defers 437 // frame 6, running d4, garbage collects 438 // frame 6, running d2, garbage collects 439 // 440 // During the execution of d4, the panic stack is d4 -> d3, which 441 // is nested properly, and we'll treat frame 3 as resumable, because we 442 // can find d3. (And in fact frame 3 is resumable. If d4 recovers 443 // and frame 5 continues running, d3, d3 can recover and we'll 444 // resume execution in (returning from) frame 3.) 445 // 446 // During the execution of d2, however, the panic stack is d2 -> d3, 447 // which is inverted. The scan will match d2 to frame 2 but having 448 // d2 on the stack until then means it will not match d3 to frame 3. 449 // This is okay: if we're running d2, then all the defers after d2 have 450 // completed and their corresponding frames are dead. Not finding d3 451 // for frame 3 means we'll set frame 3's continpc == 0, which is correct 452 // (frame 3 is dead). At the end of the walk the panic stack can thus 453 // contain defers (d3 in this case) for dead frames. The inversion here 454 // always indicates a dead frame, and the effect of the inversion on the 455 // scan is to hide those dead frames, so the scan is still okay: 456 // what's left on the panic stack are exactly (and only) the dead frames. 457 // 458 // We require callback != nil here because only when callback != nil 459 // do we know that gentraceback is being called in a "must be correct" 460 // context as opposed to a "best effort" context. The tracebacks with 461 // callbacks only happen when everything is stopped nicely. 462 // At other times, such as when gathering a stack for a profiling signal 463 // or when printing a traceback during a crash, everything may not be 464 // stopped nicely, and the stack walk may not be able to complete. 465 // It's okay in those situations not to use up the entire defer stack: 466 // incomplete information then is still better than nothing. 467 if callback != nil && n < max && _defer != nil { 468 if _defer != nil { 469 print("runtime: g", gp.goid, ": leftover defer sp=", hex(_defer.sp), " pc=", hex(_defer.pc), "\n") 470 } 471 for _defer = gp._defer; _defer != nil; _defer = _defer.link { 472 print("\tdefer ", _defer, " sp=", hex(_defer.sp), " pc=", hex(_defer.pc), "\n") 473 } 474 throw("traceback has leftover defers") 475 } 476 477 if callback != nil && n < max && len(stkbar) > 0 { 478 print("runtime: g", gp.goid, ": leftover stack barriers ") 479 gcPrintStkbars(stkbar) 480 print("\n") 481 throw("traceback has leftover stack barriers") 482 } 483 484 if callback != nil && n < max && frame.sp != gp.stktopsp { 485 print("runtime: g", gp.goid, ": frame.sp=", hex(frame.sp), " top=", hex(gp.stktopsp), "\n") 486 print("\tstack=[", hex(gp.stack.lo), "-", hex(gp.stack.hi), "] n=", n, " max=", max, "\n") 487 throw("traceback did not unwind completely") 488 } 489 490 return n 491 } 492 493 func setArgInfo(frame *stkframe, f *_func, needArgMap bool) { 494 frame.arglen = uintptr(f.args) 495 if needArgMap && f.args == _ArgsSizeUnknown { 496 // Extract argument bitmaps for reflect stubs from the calls they made to reflect. 497 switch funcname(f) { 498 case "reflect.makeFuncStub", "reflect.methodValueCall": 499 arg0 := frame.sp + minFrameSize 500 fn := *(**[2]uintptr)(unsafe.Pointer(arg0)) 501 if fn[0] != f.entry { 502 print("runtime: confused by ", funcname(f), "\n") 503 throw("reflect mismatch") 504 } 505 bv := (*bitvector)(unsafe.Pointer(fn[1])) 506 frame.arglen = uintptr(bv.n * ptrSize) 507 frame.argmap = bv 508 } 509 } 510 } 511 512 func printcreatedby(gp *g) { 513 // Show what created goroutine, except main goroutine (goid 1). 514 pc := gp.gopc 515 f := findfunc(pc) 516 if f != nil && showframe(f, gp) && gp.goid != 1 { 517 print("created by ", funcname(f), "\n") 518 tracepc := pc // back up to CALL instruction for funcline. 519 if pc > f.entry { 520 tracepc -= _PCQuantum 521 } 522 file, line := funcline(f, tracepc) 523 print("\t", file, ":", line) 524 if pc > f.entry { 525 print(" +", hex(pc-f.entry)) 526 } 527 print("\n") 528 } 529 } 530 531 func traceback(pc, sp, lr uintptr, gp *g) { 532 traceback1(pc, sp, lr, gp, 0) 533 } 534 535 // tracebacktrap is like traceback but expects that the PC and SP were obtained 536 // from a trap, not from gp->sched or gp->syscallpc/gp->syscallsp or getcallerpc/getcallersp. 537 // Because they are from a trap instead of from a saved pair, 538 // the initial PC must not be rewound to the previous instruction. 539 // (All the saved pairs record a PC that is a return address, so we 540 // rewind it into the CALL instruction.) 541 func tracebacktrap(pc, sp, lr uintptr, gp *g) { 542 traceback1(pc, sp, lr, gp, _TraceTrap) 543 } 544 545 func traceback1(pc, sp, lr uintptr, gp *g, flags uint) { 546 var n int 547 if readgstatus(gp)&^_Gscan == _Gsyscall { 548 // Override registers if blocked in system call. 549 pc = gp.syscallpc 550 sp = gp.syscallsp 551 flags &^= _TraceTrap 552 } 553 // Print traceback. By default, omits runtime frames. 554 // If that means we print nothing at all, repeat forcing all frames printed. 555 n = gentraceback(pc, sp, lr, gp, 0, nil, _TracebackMaxFrames, nil, nil, flags) 556 if n == 0 && (flags&_TraceRuntimeFrames) == 0 { 557 n = gentraceback(pc, sp, lr, gp, 0, nil, _TracebackMaxFrames, nil, nil, flags|_TraceRuntimeFrames) 558 } 559 if n == _TracebackMaxFrames { 560 print("...additional frames elided...\n") 561 } 562 printcreatedby(gp) 563 } 564 565 func callers(skip int, pcbuf []uintptr) int { 566 sp := getcallersp(unsafe.Pointer(&skip)) 567 pc := uintptr(getcallerpc(unsafe.Pointer(&skip))) 568 gp := getg() 569 var n int 570 systemstack(func() { 571 n = gentraceback(pc, sp, 0, gp, skip, &pcbuf[0], len(pcbuf), nil, nil, 0) 572 }) 573 return n 574 } 575 576 func gcallers(gp *g, skip int, pcbuf []uintptr) int { 577 return gentraceback(^uintptr(0), ^uintptr(0), 0, gp, skip, &pcbuf[0], len(pcbuf), nil, nil, 0) 578 } 579 580 func showframe(f *_func, gp *g) bool { 581 g := getg() 582 if g.m.throwing > 0 && gp != nil && (gp == g.m.curg || gp == g.m.caughtsig.ptr()) { 583 return true 584 } 585 level, _, _ := gotraceback() 586 name := funcname(f) 587 588 // Special case: always show runtime.panic frame, so that we can 589 // see where a panic started in the middle of a stack trace. 590 // See golang.org/issue/5832. 591 if name == "runtime.panic" { 592 return true 593 } 594 595 return level > 1 || f != nil && contains(name, ".") && (!hasprefix(name, "runtime.") || isExportedRuntime(name)) 596 } 597 598 // isExportedRuntime reports whether name is an exported runtime function. 599 // It is only for runtime functions, so ASCII A-Z is fine. 600 func isExportedRuntime(name string) bool { 601 const n = len("runtime.") 602 return len(name) > n && name[:n] == "runtime." && 'A' <= name[n] && name[n] <= 'Z' 603 } 604 605 var gStatusStrings = [...]string{ 606 _Gidle: "idle", 607 _Grunnable: "runnable", 608 _Grunning: "running", 609 _Gsyscall: "syscall", 610 _Gwaiting: "waiting", 611 _Gdead: "dead", 612 _Genqueue: "enqueue", 613 _Gcopystack: "copystack", 614 } 615 616 var gScanStatusStrings = [...]string{ 617 0: "scan", 618 _Grunnable: "scanrunnable", 619 _Grunning: "scanrunning", 620 _Gsyscall: "scansyscall", 621 _Gwaiting: "scanwaiting", 622 _Gdead: "scandead", 623 _Genqueue: "scanenqueue", 624 } 625 626 func goroutineheader(gp *g) { 627 gpstatus := readgstatus(gp) 628 629 // Basic string status 630 var status string 631 if 0 <= gpstatus && gpstatus < uint32(len(gStatusStrings)) { 632 status = gStatusStrings[gpstatus] 633 } else if gpstatus&_Gscan != 0 && 0 <= gpstatus&^_Gscan && gpstatus&^_Gscan < uint32(len(gStatusStrings)) { 634 status = gStatusStrings[gpstatus&^_Gscan] 635 } else { 636 status = "???" 637 } 638 639 // Override. 640 if (gpstatus == _Gwaiting || gpstatus == _Gscanwaiting) && gp.waitreason != "" { 641 status = gp.waitreason 642 } 643 644 // approx time the G is blocked, in minutes 645 var waitfor int64 646 gpstatus &^= _Gscan // drop the scan bit 647 if (gpstatus == _Gwaiting || gpstatus == _Gsyscall) && gp.waitsince != 0 { 648 waitfor = (nanotime() - gp.waitsince) / 60e9 649 } 650 print("goroutine ", gp.goid, " [", status) 651 if waitfor >= 1 { 652 print(", ", waitfor, " minutes") 653 } 654 if gp.lockedm != nil { 655 print(", locked to thread") 656 } 657 print("]:\n") 658 } 659 660 func tracebackothers(me *g) { 661 level, _, _ := gotraceback() 662 663 // Show the current goroutine first, if we haven't already. 664 g := getg() 665 gp := g.m.curg 666 if gp != nil && gp != me { 667 print("\n") 668 goroutineheader(gp) 669 traceback(^uintptr(0), ^uintptr(0), 0, gp) 670 } 671 672 lock(&allglock) 673 for _, gp := range allgs { 674 if gp == me || gp == g.m.curg || readgstatus(gp) == _Gdead || isSystemGoroutine(gp) && level < 2 { 675 continue 676 } 677 print("\n") 678 goroutineheader(gp) 679 // Note: gp.m == g.m occurs when tracebackothers is 680 // called from a signal handler initiated during a 681 // systemstack call. The original G is still in the 682 // running state, and we want to print its stack. 683 if gp.m != g.m && readgstatus(gp)&^_Gscan == _Grunning { 684 print("\tgoroutine running on other thread; stack unavailable\n") 685 printcreatedby(gp) 686 } else { 687 traceback(^uintptr(0), ^uintptr(0), 0, gp) 688 } 689 } 690 unlock(&allglock) 691 } 692 693 // Does f mark the top of a goroutine stack? 694 func topofstack(f *_func) bool { 695 pc := f.entry 696 return pc == goexitPC || 697 pc == mstartPC || 698 pc == mcallPC || 699 pc == morestackPC || 700 pc == rt0_goPC || 701 externalthreadhandlerp != 0 && pc == externalthreadhandlerp 702 } 703 704 // isSystemGoroutine reports whether the goroutine g must be omitted in 705 // stack dumps and deadlock detector. 706 func isSystemGoroutine(gp *g) bool { 707 pc := gp.startpc 708 return pc == runfinqPC && !fingRunning || 709 pc == backgroundgcPC || 710 pc == bgsweepPC || 711 pc == forcegchelperPC || 712 pc == timerprocPC || 713 pc == gcBgMarkWorkerPC 714 }