This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
signal: Switch to standard signal handling
Change from using the golang "debug" package for providing backtraces to using the Kata Project standard signal handling code: - A trace is written to the agents logger if a fatal signal is received or an internal error is detected. - For consistency with other components, it is possible to enable a coredump on fatal error. However, this is not desirable even with debug enabled so the agent will only attempt to dump core if the new developer mode (enabled by specifying `agent.devmode` on the guest kernel command-line) is enabled. Signed-off-by: James O. D. Hunt <[email protected]>
- Loading branch information
1 parent
f8081ca
commit a0880aa
Showing
4 changed files
with
219 additions
and
10 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
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,87 @@ | ||
// Copyright 2018 Intel Corporation. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
"runtime/pprof" | ||
"strings" | ||
"syscall" | ||
) | ||
|
||
// List of handled signals. | ||
// | ||
// The value is true if receiving the signal should be fatal. | ||
var handledSignalsMap = 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) | ||
agentLog.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") { | ||
agentLog.Error(line) | ||
} | ||
} | ||
|
||
func fatalSignal(sig syscall.Signal) bool { | ||
s, exists := handledSignalsMap[sig] | ||
if !exists { | ||
return false | ||
} | ||
|
||
return s | ||
} | ||
|
||
func handledSignals() []syscall.Signal { | ||
var signals []syscall.Signal | ||
|
||
for sig := range handledSignalsMap { | ||
signals = append(signals, sig) | ||
} | ||
|
||
return signals | ||
} | ||
|
||
func die() { | ||
backtrace() | ||
|
||
if crashOnError { | ||
signal.Reset(syscall.SIGABRT) | ||
syscall.Kill(0, syscall.SIGABRT) | ||
} | ||
|
||
os.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright (c) 2018 Intel Corporation | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"reflect" | ||
goruntime "runtime" | ||
"sort" | ||
"strings" | ||
"syscall" | ||
"testing" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSignalHandledSignalsMap(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
for sig, fatal := range handledSignalsMap { | ||
result := fatalSignal(sig) | ||
if fatal { | ||
assert.True(result) | ||
} else { | ||
assert.False(result) | ||
} | ||
} | ||
} | ||
|
||
func TestSignalHandledSignals(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
var expected []syscall.Signal | ||
|
||
for sig := range handledSignalsMap { | ||
expected = append(expected, sig) | ||
} | ||
|
||
got := handledSignals() | ||
|
||
sort.Slice(expected, func(i, j int) bool { | ||
return int(expected[i]) < int(expected[j]) | ||
}) | ||
|
||
sort.Slice(got, func(i, j int) bool { | ||
return int(got[i]) < int(got[j]) | ||
}) | ||
|
||
assert.True(reflect.DeepEqual(expected, got)) | ||
} | ||
|
||
func TestSignalFatalSignalInvalidSignal(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
sig := syscall.SIGXCPU | ||
|
||
result := fatalSignal(sig) | ||
assert.False(result) | ||
} | ||
|
||
func TestSignalBacktrace(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
// create buffer to save logger output | ||
buf := &bytes.Buffer{} | ||
|
||
savedLog := agentLog | ||
defer func() { | ||
agentLog = savedLog | ||
}() | ||
|
||
agentLog = logrus.WithField("test-agent-logger", true) | ||
|
||
agentLog.Logger.Formatter = &logrus.TextFormatter{ | ||
DisableColors: true, | ||
} | ||
|
||
// capture output to buffer | ||
agentLog.Logger.Out = buf | ||
|
||
// determine name of *this* function | ||
pc := make([]uintptr, 1) | ||
goruntime.Callers(1, pc) | ||
fn := goruntime.FuncForPC(pc[0]) | ||
name := fn.Name() | ||
|
||
backtrace() | ||
|
||
b := buf.String() | ||
|
||
// very basic tests to check if a backtrace was produced | ||
assert.True(strings.Contains(b, "contention:")) | ||
assert.True(strings.Contains(b, `level=error`)) | ||
assert.True(strings.Contains(b, name)) | ||
} |