In C# the executing program can detect if it\'s running in the debugger using:
System.Diagnostics.Debugger.IsAttached
Is there an equivalen
If you assume that the debugger used will be Delve, you can check on the Delve process(es). There are at least two cases to consider (maybe more).
os.Getppid()
to get the pid of your parent process, that process will be Delve.os.Getpid()
. This relies on the assumption that you're not finding an old Delve, running with an older PID that happens to match yours. (I forget what the rules on on reuse of PIDs by the OS). Note that the os functions used by 1 and 2 are different. One gets the parent PID, the other gets your PID.
Some very basic code to do 1 looks like this:
func isLaunchedByDebugger() bool {
// gops executable must be in the path. See https://github.com/google/gops
gopsOut, err := exec.Command("gops", strconv.Itoa(os.Getppid())).Output()
if err == nil && strings.Contains(string(gopsOut), "\\dlv.exe") {
// our parent process is (probably) the Delve debugger
return true
}
return false
}