| 1 | module os |
| 2 | |
| 3 | #include <sys/ptrace.h> |
| 4 | |
| 5 | fn C.ptrace(i32, u32, voidptr, i32) i32 |
| 6 | |
| 7 | // debugger_present returns a bool indicating if the process is being debugged. |
| 8 | pub fn debugger_present() bool { |
| 9 | $if freebsd { |
| 10 | // check if a child process could trace its parent process, |
| 11 | // if not a debugger must be present |
| 12 | pid := fork() |
| 13 | if pid == 0 { |
| 14 | ppid := getppid() |
| 15 | if C.ptrace(C.PT_TRACE_ME, ppid, unsafe { nil }, 0) == 0 { |
| 16 | C.waitpid(ppid, 0, 0) |
| 17 | |
| 18 | // detach ptrace, otherwise further checks would indicate a debugger is present (ptrace is the Debugger then) |
| 19 | C.ptrace(C.PT_DETACH, ppid, 0, 0) |
| 20 | |
| 21 | // no external debugger |
| 22 | exit(0) |
| 23 | } else { |
| 24 | // an error occurred, a external debugger must be present |
| 25 | exit(1) |
| 26 | } |
| 27 | } else { |
| 28 | mut status := 0 |
| 29 | // wait until the child process dies |
| 30 | C.waitpid(pid, &status, 0) |
| 31 | // check the exit code of the child process check |
| 32 | if posix_wait_status_exit_code(status) == 0 { |
| 33 | return false |
| 34 | } else { |
| 35 | return true |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | return false |
| 40 | } |
| 41 | |