Derek Parker 57b6496c2e
pkg/debugdetect: add package for detecting debugger attachment (#4258)
* pkg/debugdetect: add package for detecting debugger attachment

Implement new pkg/debugdetect package that detects if a program is
running under a debugger on Linux, macOS, Windows, and FreeBSD.

Platform-specific detection methods:
- Linux: Parse /proc/self/status for TracerPid field
- macOS: Use sysctl with KERN_PROC_PID to check P_TRACED flag
- Windows: Call IsDebuggerPresent() Win32 API
- FreeBSD: Try sysctl first, fall back to /proc/curproc/status

Fixes #4243

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-16 10:34:01 +01:00

32 lines
727 B
Go

package debugdetect
import (
"fmt"
"runtime"
"time"
)
// IsDebuggerAttached returns true if the current process is being debugged
// by a ptrace-based debugger (Delve, gdb, lldb, etc.).
//
// Returns an error if the debugger state cannot be determined.
// Supported platforms: linux, darwin, windows, freebsd
func IsDebuggerAttached() (bool, error) {
switch runtime.GOOS {
case "linux", "darwin", "windows", "freebsd":
return detectDebuggerAttached()
default:
return false, fmt.Errorf("debugger detection not supported on %s", runtime.GOOS)
}
}
func WaitForDebugger() error {
for {
attached, err := IsDebuggerAttached()
if attached || err != nil {
return err
}
time.Sleep(500 * time.Millisecond)
}
}