os: fix debugger_present() for non Windows OSes (#21573)

This commit is contained in:
Bastian Buck 2024-05-25 23:46:43 +02:00 committed by GitHub
parent 0fdc32e225
commit b4c560d4b8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 84 additions and 12 deletions

View file

@ -5,16 +5,40 @@ module os
fn C.ptrace(u32, u32, voidptr, voidptr) u64
// debugger_present returns a bool indicating if the process is being debugged
@[inline]
pub fn debugger_present() bool {
$if cross ? {
return false
}
$if !cross ? {
// check if the parent could trace its process,
// if not a debugger must be present
$if linux {
return C.ptrace(C.PTRACE_TRACEME, 0, 1, 0) == -1
// check if a child process could trace its parent process,
// if not a debugger must be present
pid := fork()
if pid == 0 {
ppid := getppid()
if C.ptrace(C.PTRACE_ATTACH, ppid, 0, 0) == 0 {
C.waitpid(ppid, 0, 0)
// detach ptrace, otherwise further checks would indicate a debugger is present (ptrace is the Debugger then)
C.ptrace(C.PTRACE_DETACH, ppid, 0, 0)
// no external debugger
exit(0)
} else {
// an error occured, a external debugger must be present
exit(1)
}
} else {
mut status := 0
// wait until the child process dies
C.waitpid(pid, &status, 0)
// check the exit code of the child process check
if C.WEXITSTATUS(status) == 0 {
return false
} else {
return true
}
}
}
}
return false