This repository has been archived by the owner on May 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
If the runtime detects an internal error or if it receives a fatal signal, write a stack trace to the log and exit. If debug is enabled also dump core. Note: This replaces the previous `debug.SetTraceback()` call which logs to `stderr`, which is not desirable. Fixes #1053. Signed-off-by: James O. D. Hunt <[email protected]>
- Loading branch information
1 parent
68570d9
commit cee7ef7
Showing
3 changed files
with
111 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2018 Intel Corporation. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os/signal" | ||
"runtime/pprof" | ||
"strings" | ||
"syscall" | ||
) | ||
|
||
// List of fatal signals | ||
var sigFatal = map[syscall.Signal]bool{ | ||
syscall.SIGABRT: true, | ||
syscall.SIGBUS: true, | ||
syscall.SIGILL: true, | ||
syscall.SIGQUIT: true, | ||
syscall.SIGSEGV: true, | ||
syscall.SIGSTKFLT: true, | ||
syscall.SIGSYS: true, | ||
syscall.SIGTRAP: true, | ||
} | ||
|
||
func handlePanic() { | ||
r := recover() | ||
|
||
if r != nil { | ||
msg := fmt.Sprintf("%s", r) | ||
ccLog.WithField("panic", msg).Error("fatal error") | ||
|
||
die() | ||
} | ||
} | ||
|
||
func backtrace() { | ||
profiles := pprof.Profiles() | ||
|
||
buf := &bytes.Buffer{} | ||
|
||
for _, p := range profiles { | ||
// The magic number requests a full stacktrace. See | ||
// https://golang.org/pkg/runtime/pprof/#Profile.WriteTo. | ||
pprof.Lookup(p.Name()).WriteTo(buf, 2) | ||
} | ||
|
||
for _, line := range strings.Split(buf.String(), "\n") { | ||
ccLog.Error(line) | ||
} | ||
} | ||
|
||
func fatalSignal(sig syscall.Signal) bool { | ||
return sigFatal[sig] | ||
} | ||
|
||
func fatalSignals() []syscall.Signal { | ||
var signals []syscall.Signal | ||
|
||
for sig := range sigFatal { | ||
signals = append(signals, sig) | ||
|
||
} | ||
|
||
return signals | ||
} | ||
|
||
func die() { | ||
backtrace() | ||
|
||
if crashOnError { | ||
signal.Reset(syscall.SIGABRT) | ||
syscall.Kill(0, syscall.SIGABRT) | ||
} | ||
|
||
exit(1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters