runtime: scan register backing store on ia64
[official-gcc.git] / libgo / go / runtime / traceback_gccgo.go
blob8551ec19ac30ea7dad3d927870bcde81f3276419
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"
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 //go:noescape
50 //extern runtime_callers
51 func c_callers(skip int32, locbuf *location, max int32, keepThunks bool) int32
53 // callers returns a stack trace of the current goroutine.
54 // The gc version of callers takes []uintptr, but we take []location.
55 func callers(skip int, locbuf []location) int {
56 n := c_callers(int32(skip)+1, &locbuf[0], int32(len(locbuf)), false)
57 return int(n)
60 // traceback prints a traceback of the current goroutine.
61 // This differs from the gc version, which is given pc, sp, lr and g and
62 // can print a traceback of any goroutine.
63 func traceback(skip int32) {
64 var locbuf [100]location
65 c := c_callers(skip+1, &locbuf[0], int32(len(locbuf)), false)
66 printtrace(locbuf[:c], getg())
67 printcreatedby(getg())
70 // printtrace prints a traceback from locbuf.
71 func printtrace(locbuf []location, gp *g) {
72 for i := range locbuf {
73 if showframe(locbuf[i].function, gp) {
74 name := locbuf[i].function
75 if name == "runtime.gopanic" {
76 name = "panic"
78 print(name, "\n\t", locbuf[i].filename, ":", locbuf[i].lineno, "\n")
83 // showframe returns whether to print a frame in a traceback.
84 // name is the function name.
85 func showframe(name string, gp *g) bool {
86 g := getg()
87 if g.m.throwing > 0 && gp != nil && (gp == g.m.curg || gp == g.m.caughtsig.ptr()) {
88 return true
91 // Gccgo can trace back through C functions called via cgo.
92 // We want to print those in the traceback.
93 // But unless GOTRACEBACK > 1 (checked below), still skip
94 // internal C functions and cgo-generated functions.
95 if name != "" && !contains(name, ".") && !hasprefix(name, "__go_") && !hasprefix(name, "_cgo_") {
96 return true
99 level, _, _ := gotraceback()
101 // Special case: always show runtime.gopanic frame, so that we can
102 // see where a panic started in the middle of a stack trace.
103 // See golang.org/issue/5832.
104 // __go_panic is the current gccgo name.
105 if name == "runtime.gopanic" || name == "__go_panic" {
106 return true
109 return level > 1 || contains(name, ".") && (!hasprefix(name, "runtime.") || isExportedRuntime(name))
112 // isExportedRuntime reports whether name is an exported runtime function.
113 // It is only for runtime functions, so ASCII A-Z is fine.
114 func isExportedRuntime(name string) bool {
115 const n = len("runtime.")
116 return len(name) > n && name[:n] == "runtime." && 'A' <= name[n] && name[n] <= 'Z'
119 var gStatusStrings = [...]string{
120 _Gidle: "idle",
121 _Grunnable: "runnable",
122 _Grunning: "running",
123 _Gsyscall: "syscall",
124 _Gwaiting: "waiting",
125 _Gdead: "dead",
126 _Gcopystack: "copystack",
129 func goroutineheader(gp *g) {
130 gpstatus := readgstatus(gp)
132 isScan := gpstatus&_Gscan != 0
133 gpstatus &^= _Gscan // drop the scan bit
135 // Basic string status
136 var status string
137 if 0 <= gpstatus && gpstatus < uint32(len(gStatusStrings)) {
138 status = gStatusStrings[gpstatus]
139 } else {
140 status = "???"
143 // Override.
144 if gpstatus == _Gwaiting && gp.waitreason != "" {
145 status = gp.waitreason
148 // approx time the G is blocked, in minutes
149 var waitfor int64
150 if (gpstatus == _Gwaiting || gpstatus == _Gsyscall) && gp.waitsince != 0 {
151 waitfor = (nanotime() - gp.waitsince) / 60e9
153 print("goroutine ", gp.goid, " [", status)
154 if isScan {
155 print(" (scan)")
157 if waitfor >= 1 {
158 print(", ", waitfor, " minutes")
160 if gp.lockedm != 0 {
161 print(", locked to thread")
163 print("]:\n")
166 // isSystemGoroutine reports whether the goroutine g must be omitted in
167 // stack dumps and deadlock detector.
168 func isSystemGoroutine(gp *g) bool {
169 return gp.isSystemGoroutine
172 func tracebackothers(me *g) {
173 var tb tracebackg
174 tb.gp = me
176 // The getTraceback function will modify me's stack context.
177 // Preserve it in case we have been called via systemstack.
178 context := me.context
179 stackcontext := me.stackcontext
181 level, _, _ := gotraceback()
183 // Show the current goroutine first, if we haven't already.
184 g := getg()
185 gp := g.m.curg
186 if gp != nil && gp != me {
187 print("\n")
188 goroutineheader(gp)
189 gp.traceback = (*tracebackg)(noescape(unsafe.Pointer(&tb)))
190 getTraceback(me, gp)
191 printtrace(tb.locbuf[:tb.c], nil)
192 printcreatedby(gp)
195 lock(&allglock)
196 for _, gp := range allgs {
197 if gp == me || gp == g.m.curg || readgstatus(gp) == _Gdead || isSystemGoroutine(gp) && level < 2 {
198 continue
200 print("\n")
201 goroutineheader(gp)
203 // gccgo's only mechanism for doing a stack trace is
204 // _Unwind_Backtrace. And that only works for the
205 // current thread, not for other random goroutines.
206 // So we need to switch context to the goroutine, get
207 // the backtrace, and then switch back.
209 // This means that if g is running or in a syscall, we
210 // can't reliably print a stack trace. FIXME.
212 // Note: gp.m == g.m occurs when tracebackothers is
213 // called from a signal handler initiated during a
214 // systemstack call. The original G is still in the
215 // running state, and we want to print its stack.
216 if gp.m != g.m && readgstatus(gp)&^_Gscan == _Grunning {
217 print("\tgoroutine running on other thread; stack unavailable\n")
218 printcreatedby(gp)
219 } else if readgstatus(gp)&^_Gscan == _Gsyscall {
220 print("\tgoroutine in C code; stack unavailable\n")
221 printcreatedby(gp)
222 } else {
223 gp.traceback = (*tracebackg)(noescape(unsafe.Pointer(&tb)))
224 getTraceback(me, gp)
225 printtrace(tb.locbuf[:tb.c], nil)
226 printcreatedby(gp)
229 unlock(&allglock)
231 me.context = context
232 me.stackcontext = stackcontext