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