forked from evergreen-ci/evergreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
82 lines (71 loc) · 2.12 KB
/
debug.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
71
72
73
74
75
76
77
78
79
80
81
82
package agent
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/signal"
"syscall"
"time"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/util"
)
const FilenameTimestamp = "2006-01-02_15_04_05"
// DumpStackOnSIGQUIT listens for a SIGQUIT signal and writes stack dump to the
// given io.Writer when one is received. Blocks, so spawn it as a goroutine.
func DumpStackOnSIGQUIT(curAgent **Agent) {
in := make(chan os.Signal)
signal.Notify(in, syscall.SIGQUIT)
for _ = range in {
agt := *curAgent
stackBytes := util.DebugTrace()
task, command := taskAndCommand(agt)
// we dump to files and logs without blocking, in case our logging is deadlocked or broken
go dumpToDisk(task, command, stackBytes)
go dumpToLogs(task, command, stackBytes, agt)
}
}
func taskAndCommand(agt *Agent) (string, string) {
task := "no running task"
command := "no running command"
if agt != nil {
if agt.taskConfig != nil {
task = agt.taskConfig.Task.Id
}
if cmd := agt.GetCurrentCommand(); cmd.Command != "" {
command = cmd.Command
}
}
return task, command
}
func dumpToLogs(task, command string, stack []byte, agt *Agent) {
if agt != nil && agt.logger != nil {
logWriter := evergreen.NewInfoLoggingWriter(agt.logger.System)
dumpDebugInfo(task, command, stack, logWriter)
}
}
func dumpToDisk(task, command string, stack []byte) {
wd, err := os.Getwd()
if err != nil {
return
}
dumpFile, err := ioutil.TempFile(wd, newDumpFilename())
if err != nil {
return // fail silently -- things are very wrong
}
defer dumpFile.Close()
dumpDebugInfo(task, command, stack, dumpFile)
}
func newDumpFilename() string {
// e.g. "evergreen_agent_46672_dump_2015-09-08T23:26:49-04:00..."
return fmt.Sprintf("evergreen_agent_%v_dump_%v_", os.Getpid(),
time.Now().Format(FilenameTimestamp))
}
func dumpDebugInfo(task, command string, stack []byte, w io.Writer) {
out := bytes.Buffer{}
out.WriteString(fmt.Sprintf("Agent stack dump taken on %v.\n\n", time.Now().Format(time.UnixDate)))
out.WriteString(fmt.Sprintf("Running command '%v' for task '%v'.\n\n", command, task))
out.Write(stack)
w.Write(out.Bytes())
}