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