1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Spew debugging

Signed-off-by: John Howard <jhoward@microsoft.com>
This commit is contained in:
John Howard 2016-10-12 15:29:47 -07:00
parent 3b6cd20b0b
commit 4c62b12636
6 changed files with 59 additions and 14 deletions

View file

@ -476,10 +476,6 @@ func NewDaemon(config *Config, registryService registry.Service, containerdRemot
return nil, err return nil, err
} }
// set up SIGUSR1 handler on Unix-like systems, or a Win32 global event
// on Windows to dump Go routine stacks
setupDumpStackTrap(config.Root)
uidMaps, gidMaps, err := setupRemappedRoot(config) uidMaps, gidMaps, err := setupRemappedRoot(config)
if err != nil { if err != nil {
return nil, err return nil, err
@ -699,6 +695,10 @@ func NewDaemon(config *Config, registryService registry.Service, containerdRemot
engineCpus.Set(float64(info.NCPU)) engineCpus.Set(float64(info.NCPU))
engineMemory.Set(float64(info.MemTotal)) engineMemory.Set(float64(info.MemTotal))
// set up SIGUSR1 handler on Unix-like systems, or a Win32 global event
// on Windows to dump Go routine stacks
d.setupDumpStackTrap(config.Root)
return d, nil return d, nil
} }

33
daemon/debugtrap.go Normal file
View file

@ -0,0 +1,33 @@
package daemon
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/pkg/errors"
)
const dataStructuresLogNameTemplate = "daemon-data-%s.log"
// dumpDaemon appends the daemon datastructures into file in dir and returns full path
// to that file.
func (d *Daemon) dumpDaemon(dir string) (string, error) {
// Ensure we recover from a panic as we are doing this without any locking
defer func() {
recover()
}()
path := filepath.Join(dir, fmt.Sprintf(dataStructuresLogNameTemplate, strings.Replace(time.Now().Format(time.RFC3339), ":", "", -1)))
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
return "", errors.Wrap(err, "failed to open file to write the daemon datastructure dump")
}
defer f.Close()
spew.Fdump(f, d) // Does not return an error
f.Sync()
return path, nil
}

View file

@ -7,12 +7,11 @@ import (
"os/signal" "os/signal"
"syscall" "syscall"
stackdump "github.com/docker/docker/pkg/signal"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
stackdump "github.com/docker/docker/pkg/signal"
) )
func setupDumpStackTrap(root string) { func (d *Daemon) setupDumpStackTrap(root string) {
c := make(chan os.Signal, 1) c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGUSR1) signal.Notify(c, syscall.SIGUSR1)
go func() { go func() {
@ -20,9 +19,15 @@ func setupDumpStackTrap(root string) {
path, err := stackdump.DumpStacks(root) path, err := stackdump.DumpStacks(root)
if err != nil { if err != nil {
logrus.WithError(err).Error("failed to write goroutines dump") logrus.WithError(err).Error("failed to write goroutines dump")
continue } else {
logrus.Infof("goroutine stacks written to %s", path)
}
path, err = d.dumpDaemon(root)
if err != nil {
logrus.WithError(err).Error("failed to write daemon datastructure dump")
} else {
logrus.Infof("daemon datastructure dump written to %s", path)
} }
logrus.Infof("goroutine stacks written to %s", path)
} }
}() }()
} }

View file

@ -2,6 +2,6 @@
package daemon package daemon
func setupDumpStackTrap(_ string) { func (d *Daemon) setupDumpStackTrap(_ string) {
return return
} }

View file

@ -12,7 +12,7 @@ import (
"github.com/docker/docker/pkg/system" "github.com/docker/docker/pkg/system"
) )
func setupDumpStackTrap(root string) { func (d *Daemon) setupDumpStackTrap(root string) {
// Windows does not support signals like *nix systems. So instead of // Windows does not support signals like *nix systems. So instead of
// trapping on SIGUSR1 to dump stacks, we wait on a Win32 event to be // trapping on SIGUSR1 to dump stacks, we wait on a Win32 event to be
// signaled. ACL'd to builtin administrators and local system // signaled. ACL'd to builtin administrators and local system
@ -38,9 +38,15 @@ func setupDumpStackTrap(root string) {
path, err := signal.DumpStacks(root) path, err := signal.DumpStacks(root)
if err != nil { if err != nil {
logrus.WithError(err).Error("failed to write goroutines dump") logrus.WithError(err).Error("failed to write goroutines dump")
continue } else {
logrus.Infof("goroutine stacks written to %s", path)
}
path, err = d.dumpDaemon(root)
if err != nil {
logrus.WithError(err).Error("failed to write daemon datastructure dump")
} else {
logrus.Infof("daemon datastructure dump written to %s", path)
} }
logrus.Infof("goroutine stacks written to %s", path)
} }
}() }()
} }

View file

@ -6,6 +6,7 @@ import (
gosignal "os/signal" gosignal "os/signal"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings"
"sync/atomic" "sync/atomic"
"syscall" "syscall"
"time" "time"
@ -82,7 +83,7 @@ func DumpStacks(dir string) (string, error) {
bufferLen *= 2 bufferLen *= 2
} }
buf = buf[:stackSize] buf = buf[:stackSize]
path := filepath.Join(dir, fmt.Sprintf(stacksLogNameTemplate, time.Now().Format(time.RFC3339))) path := filepath.Join(dir, fmt.Sprintf(stacksLogNameTemplate, strings.Replace(time.Now().Format(time.RFC3339), ":", "", -1)))
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0666) f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0666)
if err != nil { if err != nil {
return "", errors.Wrap(err, "failed to open file to write the goroutine stacks") return "", errors.Wrap(err, "failed to open file to write the goroutine stacks")