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

27 lines
797 B
Go

// Package debugdetect provides utilities for detecting if a program
// is running under a debugger.
//
// This is useful for applications that need to modify their behavior
// when being debugged, such as TUI applications that need to wait for
// debugger attachment before continuing startup.
//
// Example usage:
//
// attached, err := debugdetect.IsDebuggerAttached()
// if err != nil {
// log.Fatalf("Failed to detect debugger: %v", err)
// }
// if !attached {
// fmt.Println("Waiting for debugger...")
// for {
// if attached, _ := debugdetect.IsDebuggerAttached(); attached {
// break
// }
// time.Sleep(100 * time.Millisecond)
// }
// }
//
// Supported platforms: linux, darwin, windows, freebsd
// Detects: ptrace-based debuggers (Delve, gdb, lldb, etc.)
package debugdetect