Skip to content

pkg/debugdetect: add package for detecting debugger attachment#4258

Merged
aarzilli merged 7 commits into
go-delve:masterfrom
derekparker:feature/detect-debugger
Feb 16, 2026
Merged

pkg/debugdetect: add package for detecting debugger attachment#4258
aarzilli merged 7 commits into
go-delve:masterfrom
derekparker:feature/detect-debugger

Conversation

@derekparker

Copy link
Copy Markdown
Member

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

@derekparker derekparker marked this pull request as draft February 10, 2026 00:08
derekparker and others added 3 commits February 10, 2026 16:01
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

Related to go-delve#4243

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@derekparker derekparker force-pushed the feature/detect-debugger branch from 46f408c to 1ee6484 Compare February 11, 2026 00:01
@derekparker derekparker marked this pull request as ready for review February 11, 2026 19:06
Comment thread pkg/debugdetect/detect_darwin.go Outdated
Comment thread pkg/debugdetect/detect_freebsd.go Outdated
Comment thread pkg/debugdetect/detect_freebsd.go Outdated
Comment thread pkg/debugdetect/detect_freebsd.go Outdated
Comment thread pkg/debugdetect/detect_test.go Outdated
Comment thread pkg/debugdetect/doc.go
//
// Example usage:
//
// attached, err := debugdetect.IsDebuggerAttached()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should make this an helper function.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that but it seems easy enough for an end user to implement and there's too many potential variables for that the API should look like. Should it accept a context? Should it accept a timeout param? How opinionated should it be? Seems like users writing a solution on top of our building block thats suitable for their use case is better. Happy to discuss further though.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say just the basic loop:

for {
    attached, err := IsDebuggerAttached()
    if attached || err != nil {
        return err
    }
    time.Sleep(500 * time.Millisecond)
}

anything different written by the user using IsDebuggerAttached.

@aarzilli aarzilli left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@aarzilli aarzilli merged commit 57b6496 into go-delve:master Feb 16, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add helper package to detect if program is currently being debugged

2 participants