forked from kata-containers/shim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfatal.go
70 lines (56 loc) · 1.14 KB
/
fatal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2018 Intel Corporation.
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"bytes"
"fmt"
"os"
"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)
logger().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") {
logger().Error(line)
}
}
func fatalSignal(sig syscall.Signal) bool {
return sigFatal[sig]
}
func die() {
backtrace()
if crashOnError {
signal.Reset(syscall.SIGABRT)
syscall.Kill(0, syscall.SIGABRT)
}
os.Exit(1)
}