Skip to content

Instantly share code, notes, and snippets.

@fgm
Last active February 3, 2022 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fgm/5f88cef697b4bff256ee73aacdc3e7d2 to your computer and use it in GitHub Desktop.
Save fgm/5f88cef697b4bff256ee73aacdc3e7d2 to your computer and use it in GitHub Desktop.
Detect whether a Go process is running from Delve debugger
package isdebugging
import (
"os"
"github.com/mitchellh/go-ps"
)
// IsDebugging will return true if the process was launched from Delve or the
// gopls language server debugger.
//
// It does not detect situations where a debugger attached after process start.
func IsDebugging() bool {
pid := os.Getppid()
// We loop in case there were intermediary processes like the gopls language server.
for pid != 0 {
switch p, err := ps.FindProcess(pid); {
case err != nil:
return false
case p.Executable() == "dlv":
return true
default:
pid = p.PPid()
}
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment