debugtrap.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package daemon
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "time"
  8. "github.com/davecgh/go-spew/spew"
  9. "github.com/pkg/errors"
  10. )
  11. const dataStructuresLogNameTemplate = "daemon-data-%s.log"
  12. // dumpDaemon appends the daemon datastructures into file in dir and returns full path
  13. // to that file.
  14. func (d *Daemon) dumpDaemon(dir string) (string, error) {
  15. // Ensure we recover from a panic as we are doing this without any locking
  16. defer func() {
  17. recover()
  18. }()
  19. path := filepath.Join(dir, fmt.Sprintf(dataStructuresLogNameTemplate, strings.Replace(time.Now().Format(time.RFC3339), ":", "", -1)))
  20. f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0666)
  21. if err != nil {
  22. return "", errors.Wrap(err, "failed to open file to write the daemon datastructure dump")
  23. }
  24. defer f.Close()
  25. dump := struct {
  26. containers interface{}
  27. names interface{}
  28. links interface{}
  29. execs interface{}
  30. volumes interface{}
  31. images interface{}
  32. layers interface{}
  33. imageReferences interface{}
  34. downloads interface{}
  35. uploads interface{}
  36. registry interface{}
  37. plugins interface{}
  38. }{
  39. containers: d.containers,
  40. execs: d.execCommands,
  41. volumes: d.volumes,
  42. images: d.imageStore,
  43. layers: d.layerStore,
  44. imageReferences: d.referenceStore,
  45. downloads: d.downloadManager,
  46. uploads: d.uploadManager,
  47. registry: d.RegistryService,
  48. plugins: d.PluginStore,
  49. names: d.nameIndex,
  50. links: d.linkIndex,
  51. }
  52. spew.Fdump(f, dump) // Does not return an error
  53. f.Sync()
  54. return path, nil
  55. }