diff options
Diffstat (limited to 'libgo/go/runtime/print.go')
-rw-r--r-- | libgo/go/runtime/print.go | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/libgo/go/runtime/print.go b/libgo/go/runtime/print.go index 371cec5..4db726a 100644 --- a/libgo/go/runtime/print.go +++ b/libgo/go/runtime/print.go @@ -4,7 +4,10 @@ package runtime -import "unsafe" +import ( + "runtime/internal/atomic" + "unsafe" +) // For gccgo, use go:linkname to rename compiler-called functions to // themselves, so that the compiler will export them. @@ -41,6 +44,36 @@ func bytes(s string) (ret []byte) { return } +var ( + // printBacklog is a circular buffer of messages written with the builtin + // print* functions, for use in postmortem analysis of core dumps. + printBacklog [512]byte + printBacklogIndex int +) + +// recordForPanic maintains a circular buffer of messages written by the +// runtime leading up to a process crash, allowing the messages to be +// extracted from a core dump. +// +// The text written during a process crash (following "panic" or "fatal +// error") is not saved, since the goroutine stacks will generally be readable +// from the runtime datastructures in the core file. +func recordForPanic(b []byte) { + printlock() + + if atomic.Load(&panicking) == 0 { + // Not actively crashing: maintain circular buffer of print output. + for i := 0; i < len(b); { + n := copy(printBacklog[printBacklogIndex:], b[i:]) + i += n + printBacklogIndex += n + printBacklogIndex %= len(printBacklog) + } + } + + printunlock() +} + var debuglock mutex // The compiler emits calls to printlock and printunlock around @@ -75,6 +108,7 @@ func gwrite(b []byte) { if len(b) == 0 { return } + recordForPanic(b) gp := getg() if gp == nil || gp.writebuf == nil { writeErr(b) |