Last active
February 3, 2022 10:31
-
-
Save fgm/5f88cef697b4bff256ee73aacdc3e7d2 to your computer and use it in GitHub Desktop.
Detect whether a Go process is running from Delve debugger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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