debugtrap_windows.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package daemon
  2. import (
  3. "fmt"
  4. "os"
  5. "syscall"
  6. "unsafe"
  7. winio "github.com/Microsoft/go-winio"
  8. "github.com/Sirupsen/logrus"
  9. "github.com/docker/docker/pkg/signal"
  10. "github.com/docker/docker/pkg/system"
  11. )
  12. func (d *Daemon) setupDumpStackTrap(root string) {
  13. // Windows does not support signals like *nix systems. So instead of
  14. // trapping on SIGUSR1 to dump stacks, we wait on a Win32 event to be
  15. // signaled. ACL'd to builtin administrators and local system
  16. ev := "Global\\docker-daemon-" + fmt.Sprint(os.Getpid())
  17. sd, err := winio.SddlToSecurityDescriptor("D:P(A;;GA;;;BA)(A;;GA;;;SY)")
  18. if err != nil {
  19. logrus.Errorf("failed to get security descriptor for debug stackdump event %s: %s", ev, err.Error())
  20. return
  21. }
  22. var sa syscall.SecurityAttributes
  23. sa.Length = uint32(unsafe.Sizeof(sa))
  24. sa.InheritHandle = 1
  25. sa.SecurityDescriptor = uintptr(unsafe.Pointer(&sd[0]))
  26. h, err := system.CreateEvent(&sa, false, false, ev)
  27. if h == 0 || err != nil {
  28. logrus.Errorf("failed to create debug stackdump event %s: %s", ev, err.Error())
  29. return
  30. }
  31. go func() {
  32. logrus.Debugf("Stackdump - waiting signal at %s", ev)
  33. for {
  34. syscall.WaitForSingleObject(h, syscall.INFINITE)
  35. path, err := signal.DumpStacks(root)
  36. if err != nil {
  37. logrus.WithError(err).Error("failed to write goroutines dump")
  38. } else {
  39. logrus.Infof("goroutine stacks written to %s", path)
  40. }
  41. path, err = d.dumpDaemon(root)
  42. if err != nil {
  43. logrus.WithError(err).Error("failed to write daemon datastructure dump")
  44. } else {
  45. logrus.Infof("daemon datastructure dump written to %s", path)
  46. }
  47. }
  48. }()
  49. }