mirror of
https://github.com/go-delve/delve.git
synced 2026-05-05 20:26:14 +02:00
* 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>
33 lines
537 B
Go
33 lines
537 B
Go
package debugdetect
|
|
|
|
/*
|
|
#include <sys/types.h>
|
|
#include <sys/sysctl.h>
|
|
#include <sys/user.h>
|
|
#include <libutil.h>
|
|
#include <stdlib.h>
|
|
#cgo LDFLAGS: -lutil
|
|
*/
|
|
import "C"
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"unsafe"
|
|
)
|
|
|
|
const (
|
|
// P_TRACED flag from sys/proc.h
|
|
pTracedFlag = 0x00000800
|
|
)
|
|
|
|
func detectDebuggerAttached() (bool, error) {
|
|
kp, err := C.kinfo_getproc(C.int(os.Getpid()))
|
|
if err != nil {
|
|
return false, fmt.Errorf("kinfo_getproc failed: %v", err)
|
|
}
|
|
defer C.free(unsafe.Pointer(kp))
|
|
|
|
return (int(kp.ki_flag) & pTracedFlag) != 0, nil
|
|
}
|