Skip to content

Commit d2ce366

Browse files
committed
address review feedback
1 parent 5eb580a commit d2ce366

3 files changed

Lines changed: 1 addition & 83 deletions

File tree

pkg/debugdetect/detect_darwin.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ const (
1818
)
1919

2020
func detectDebuggerAttached() (bool, error) {
21-
// Use sysctl to get process info and check P_TRACED flag
2221
var info unix.KinfoProc
2322

2423
mib := [4]int32{unix.CTL_KERN, kernProc, kernProcPID, int32(os.Getpid())}
@@ -38,6 +37,5 @@ func detectDebuggerAttached() (bool, error) {
3837
return false, fmt.Errorf("sysctl failed: %w", errno)
3938
}
4039

41-
// Check if P_TRACED flag is set
4240
return (info.Proc.P_flag & pTracedFlag) != 0, nil
4341
}

pkg/debugdetect/detect_freebsd.go

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ package debugdetect
99
#cgo LDFLAGS: -lutil
1010
*/
1111
import "C"
12+
1213
import (
13-
"bufio"
1414
"fmt"
1515
"os"
16-
"strconv"
17-
"strings"
1816
"unsafe"
1917
)
2018

@@ -24,62 +22,11 @@ const (
2422
)
2523

2624
func detectDebuggerAttached() (bool, error) {
27-
// Try sysctl first (more reliable)
28-
attached, err := detectFreeBSDSysctl()
29-
if err == nil {
30-
return attached, nil
31-
}
32-
33-
// Fall back to procfs if available
34-
attachedProcfs, errProcfs := detectFreeBSDProcfs()
35-
if errProcfs == nil {
36-
return attachedProcfs, nil
37-
}
38-
39-
// Both methods failed
40-
return false, fmt.Errorf("sysctl failed: %v; procfs fallback failed: %v", err, errProcfs)
41-
}
42-
43-
func detectFreeBSDSysctl() (bool, error) {
44-
// Use kinfo_getproc to get process information
4525
kp, err := C.kinfo_getproc(C.int(os.Getpid()))
4626
if err != nil {
4727
return false, fmt.Errorf("kinfo_getproc failed: %v", err)
4828
}
4929
defer C.free(unsafe.Pointer(kp))
5030

51-
// Check if P_TRACED flag is set in ki_flag
52-
// ki_flag contains the process flags including P_TRACED
5331
return (int(kp.ki_flag) & pTracedFlag) != 0, nil
5432
}
55-
56-
func detectFreeBSDProcfs() (bool, error) {
57-
// Try reading /proc/curproc/status (similar to Linux)
58-
f, err := os.Open("/proc/curproc/status")
59-
if err != nil {
60-
return false, fmt.Errorf("failed to read /proc/curproc/status: %w", err)
61-
}
62-
defer f.Close()
63-
64-
scanner := bufio.NewScanner(f)
65-
for scanner.Scan() {
66-
line := scanner.Text()
67-
if strings.HasPrefix(line, "TracerPid:") {
68-
fields := strings.Fields(line)
69-
if len(fields) < 2 {
70-
return false, fmt.Errorf("malformed TracerPid line: %s", line)
71-
}
72-
pid, err := strconv.Atoi(fields[1])
73-
if err != nil {
74-
return false, fmt.Errorf("failed to parse TracerPid: %w", err)
75-
}
76-
return pid != 0, nil
77-
}
78-
}
79-
80-
if err := scanner.Err(); err != nil {
81-
return false, fmt.Errorf("error reading /proc/curproc/status: %w", err)
82-
}
83-
84-
return false, fmt.Errorf("TracerPid not found in /proc/curproc/status")
85-
}

pkg/debugdetect/detect_test.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,33 +39,6 @@ func TestIntegration_NotAttached(t *testing.T) {
3939
}
4040
}
4141

42-
func TestIsDebuggerAttached_NotAttached(t *testing.T) {
43-
// This test should pass on all supported platforms when not debugging
44-
attached, err := IsDebuggerAttached()
45-
46-
// On supported platforms, should not error
47-
switch runtime.GOOS {
48-
case "linux", "darwin", "windows", "freebsd":
49-
if err != nil {
50-
t.Fatalf("unexpected error on %s: %v", runtime.GOOS, err)
51-
}
52-
// We're running in normal test mode, should not be attached
53-
// (unless someone is debugging the test itself, which is unlikely)
54-
if attached {
55-
t.Logf("WARNING: detected debugger attachment during test on %s", runtime.GOOS)
56-
t.Logf("This is unusual but not necessarily wrong if you're debugging tests")
57-
}
58-
default:
59-
// Unsupported platform - should return error
60-
if err == nil {
61-
t.Fatalf("expected error on unsupported platform %s", runtime.GOOS)
62-
}
63-
if attached {
64-
t.Errorf("expected attached=false on unsupported platform, got true")
65-
}
66-
}
67-
}
68-
6942
func TestIntegration_Attached(t *testing.T) {
7043
// This test verifies that IsDebuggerAttached() returns true when
7144
// the process is actually running under a debugger by using

0 commit comments

Comments
 (0)