runtime: restore "goroutine in C code" message
[official-gcc.git] / libgo / go / runtime / traceback_gccgo.go
blob37c569887b0c6aba704460c33be670d3de054085
1 // Copyright 2016 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.
5 // Traceback support for gccgo.
6 // The actual traceback code is written in C.
8 package runtime
10 import (
11 "runtime/internal/sys"
12 _ "unsafe" // for go:linkname
15 func printcreatedby(gp *g) {
16 // Show what created goroutine, except main goroutine (goid 1).
17 pc := gp.gopc
18 tracepc := pc // back up to CALL instruction for funcfileline.
19 entry := funcentry(tracepc)
20 if entry != 0 && tracepc > entry {
21 tracepc -= sys.PCQuantum
23 function, file, line := funcfileline(tracepc, -1)
24 if function != "" && showframe(function, gp) && gp.goid != 1 {
25 print("created by ", function, "\n")
26 print("\t", file, ":", line)
27 if entry != 0 && pc > entry {
28 print(" +", hex(pc-entry))
30 print("\n")
34 // tracebackg is used to collect stack traces from other goroutines.
35 type tracebackg struct {
36 gp *g
37 locbuf [_TracebackMaxFrames]location
38 c int
41 // location is a location in the program, used for backtraces.
42 type location struct {
43 pc uintptr
44 filename string
45 function string
46 lineno int
49 //extern runtime_callers
50 func c_callers(skip int32, locbuf *location, max int32, keepThunks bool) int32
52 // callers returns a stack trace of the current goroutine.
53 // The gc version of callers takes []uintptr, but we take []location.
54 func callers(skip int, locbuf []location) int {
55 n := c_callers(int32(skip), &locbuf[0], int32(len(locbuf)), false)
56 return int(n)
59 // traceback prints a traceback of the current goroutine.
60 // This differs from the gc version, which is given pc, sp, lr and g and
61 // can print a traceback of any goroutine.
62 func traceback(skip int32) {
63 var locbuf [100]location
64 c := c_callers(skip+1, &locbuf[0], int32(len(locbuf)), false)
65 printtrace(locbuf[:c], getg())
66 printcreatedby(getg())
69 // printtrace prints a traceback from locbuf.
70 func printtrace(locbuf []location, gp *g) {
71 for i := range locbuf {
72 if showframe(locbuf[i].function, gp) {
73 name := locbuf[i].function
74 if name == "runtime.gopanic" {
75 name = "panic"
77 print(name, "\n\t", locbuf[i].filename, ":", locbuf[i].lineno, "\n")
82 // showframe returns whether to print a frame in a traceback.
83 // name is the function name.
84 func showframe(name string, gp *g) bool {
85 g := getg()
86 if g.m.throwing > 0 && gp != nil && (gp == g.m.curg || gp == g.m.caughtsig.ptr()) {
87 return true
90 // Gccgo can trace back through C functions called via cgo.
91 // We want to print those in the traceback.
92 // But unless GOTRACEBACK > 1 (checked below), still skip
93 // internal C functions and cgo-generated functions.
94 if name != "" && !contains(name, ".") && !hasprefix(name, "__go_") && !hasprefix(name, "_cgo_") {
95 return true
98 level, _, _ := gotraceback()
100 // Special case: always show runtime.gopanic frame, so that we can
101 // see where a panic started in the middle of a stack trace.
102 // See golang.org/issue/5832.
103 // __go_panic is the current gccgo name.
104 if name == "runtime.gopanic" || name == "__go_panic" {
105 return true
108 return level > 1 || contains(name, ".") && (!hasprefix(name, "runtime.") || isExportedRuntime(name))
111 // isExportedRuntime reports whether name is an exported runtime function.
112 // It is only for runtime functions, so ASCII A-Z is fine.
113 func isExportedRuntime(name string) bool {
114 const n = len("runtime.")
115 return len(name) > n && name[:n] == "runtime." && 'A' <= name[n] && name[n] <= 'Z'
118 var gStatusStrings = [...]string{
119 _Gidle: "idle",
120 _Grunnable: "runnable",
121 _Grunning: "running",
122 _Gsyscall: "syscall",
123 _Gwaiting: "waiting",
124 _Gdead: "dead",
125 _Gcopystack: "copystack",
128 func goroutineheader(gp *g) {
129 gpstatus := readgstatus(gp)
131 isScan := gpstatus&_Gscan != 0
132 gpstatus &^= _Gscan // drop the scan bit
134 // Basic string status
135 var status string
136 if 0 <= gpstatus && gpstatus < uint32(len(gStatusStrings)) {
137 status = gStatusStrings[gpstatus]
138 } else {
139 status = "???"
142 // Override.
143 if gpstatus == _Gwaiting && gp.waitreason != "" {
144 status = gp.waitreason
147 // approx time the G is blocked, in minutes
148 var waitfor int64
149 if (gpstatus == _Gwaiting || gpstatus == _Gsyscall) && gp.waitsince != 0 {
150 waitfor = (nanotime() - gp.waitsince) / 60e9
152 print("goroutine ", gp.goid, " [", status)
153 if isScan {
154 print(" (scan)")
156 if waitfor >= 1 {
157 print(", ", waitfor, " minutes")
159 if gp.lockedm != nil {
160 print(", locked to thread")
162 print("]:\n")
165 // isSystemGoroutine reports whether the goroutine g must be omitted in
166 // stack dumps and deadlock detector.
167 func isSystemGoroutine(gp *g) bool {
168 return gp.isSystemGoroutine
171 func tracebackothers(me *g) {
172 var tb tracebackg
173 tb.gp = me
175 // The getTraceback function will modify me's stack context.
176 // Preserve it in case we have been called via systemstack.
177 context := me.context
178 stackcontext := me.stackcontext
180 level, _, _ := gotraceback()
182 // Show the current goroutine first, if we haven't already.
183 g := getg()
184 gp := g.m.curg
185 if gp != nil && gp != me {
186 print("\n")
187 goroutineheader(gp)
188 gp.traceback = &tb
189 getTraceback(me, gp)
190 printtrace(tb.locbuf[:tb.c], nil)
191 printcreatedby(gp)
194 lock(&allglock)
195 for _, gp := range allgs {
196 if gp == me || gp == g.m.curg || readgstatus(gp) == _Gdead || isSystemGoroutine(gp) && level < 2 {
197 continue
199 print("\n")
200 goroutineheader(gp)
202 // gccgo's only mechanism for doing a stack trace is
203 // _Unwind_Backtrace. And that only works for the
204 // current thread, not for other random goroutines.
205 // So we need to switch context to the goroutine, get
206 // the backtrace, and then switch back.
208 // This means that if g is running or in a syscall, we
209 // can't reliably print a stack trace. FIXME.
211 // Note: gp.m == g.m occurs when tracebackothers is
212 // called from a signal handler initiated during a
213 // systemstack call. The original G is still in the
214 // running state, and we want to print its stack.
215 if gp.m != g.m && readgstatus(gp)&^_Gscan == _Grunning {
216 print("\tgoroutine running on other thread; stack unavailable\n")
217 printcreatedby(gp)
218 } else if readgstatus(gp)&^_Gscan == _Gsyscall {
219 print("\tgoroutine in C code; stack unavailable\n")
220 printcreatedby(gp)
221 } else {
222 gp.traceback = &tb
223 getTraceback(me, gp)
224 printtrace(tb.locbuf[:tb.c], nil)
225 printcreatedby(gp)
228 unlock(&allglock)
230 me.context = context
231 me.stackcontext = stackcontext