5
5
#if defined(__CYGWIN__)
6
6
#include < Windows.h>
7
7
8
- FORCE_INLINE void check_debugger (bool full) {
8
+ FORCE_INLINE void check_debugger (bool full, bool parent) {
9
+ if (parent)
10
+ return ;
9
11
if (IsDebuggerPresent ()) {
10
- LOGD (" debugger present!" );
12
+ LOGD (" debugger present on self process!" );
13
+ sleep (5 );
11
14
exit (1 );
12
15
}
13
16
}
14
17
15
18
#elif defined(__APPLE__)
16
19
#include < sys/sysctl.h>
17
20
18
- FORCE_INLINE void check_debugger (bool full) {
21
+ FORCE_INLINE void check_debugger (bool full, bool parent ) {
19
22
struct kinfo_proc info;
20
23
info.kp_proc .p_flag = 0 ;
21
24
size_t size = sizeof (info);
22
- int mib[4 ] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid () };
25
+ int mib[4 ] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, parent ? getppid () : getpid () };
23
26
if (sysctl (mib, 4 , &info, &size, nullptr , 0 ) == 0 &&
24
27
(info.kp_proc .p_flag & P_TRACED) != 0 ) {
25
- LOGD (" debugger present!" );
28
+ LOGD (" debugger present on %s process!" , parent ? " parent" : " self" );
29
+ sleep (5 );
26
30
exit (1 );
27
31
}
28
32
}
@@ -46,9 +50,11 @@ FORCE_INLINE void check_debugger(bool full) {
46
50
#endif
47
51
#endif
48
52
49
- FORCE_INLINE void check_debugger (bool full) {
53
+ FORCE_INLINE void check_debugger (bool full, bool parent ) {
50
54
#ifdef __linux__
51
- std::ifstream ifs (OBF (" /proc/self/status" ));
55
+ char path[128 ];
56
+ snprintf (path, sizeof (path), OBF (" /proc/%d/status" ), parent ? getppid () : getpid ());
57
+ std::ifstream ifs (path);
52
58
std::string line, needle = OBF (" TracerPid:\t " );
53
59
int tracer_pid = 0 ;
54
60
while (std::getline (ifs, line)) {
@@ -60,7 +66,8 @@ FORCE_INLINE void check_debugger(bool full) {
60
66
}
61
67
ifs.close ();
62
68
if (tracer_pid != 0 ) {
63
- LOGD (" found tracer. tracer_pid=%d" , tracer_pid);
69
+ LOGD (" found tracer on %s process. tracer_pid=%d" , parent ? " parent" : " self" , tracer_pid);
70
+ sleep (5 );
64
71
exit (1 );
65
72
}
66
73
if (!full) {
@@ -75,22 +82,36 @@ FORCE_INLINE void check_debugger(bool full) {
75
82
return ;
76
83
}
77
84
#endif
78
- int ppid = getpid ();
79
- int p = fork ();
80
- if (p < 0 ) {
81
- LOGE (" fork failed" );
82
- exit (1 );
83
- } else if (p > 0 ) { // parent process
84
- waitpid (p, 0 , 0 );
85
- } else {
86
- if (ptrace (PT_ATTACHEXC, ppid, 0 , 0 ) == 0 ) {
85
+ if (parent) {
86
+ auto pid = getppid ();
87
+ if (ptrace (PT_ATTACHEXC, pid, 0 , 0 ) == 0 ) {
87
88
wait (0 );
88
- ptrace (PT_DETACH, ppid, 0 , 0 );
89
+ ptrace (PT_DETACH, pid, 0 , 0 );
90
+ } else {
91
+ LOGD (" parent process being traced!" );
92
+ sleep (5 );
93
+ kill (pid, SIGKILL);
94
+ exit (1 );
95
+ }
96
+ } else {
97
+ auto pid = getpid ();
98
+ int p = fork ();
99
+ if (p < 0 ) {
100
+ LOGE (" fork failed" );
101
+ exit (1 );
102
+ } else if (p > 0 ) { // parent process
103
+ waitpid (p, 0 , 0 );
89
104
} else {
90
- LOGD (" being traced!" );
91
- kill (ppid, SIGKILL);
105
+ if (ptrace (PT_ATTACHEXC, pid, 0 , 0 ) == 0 ) {
106
+ wait (0 );
107
+ ptrace (PT_DETACH, pid, 0 , 0 );
108
+ } else {
109
+ LOGD (" self process being traced!" );
110
+ sleep (5 );
111
+ kill (pid, SIGKILL);
112
+ }
113
+ _Exit (0 );
92
114
}
93
- _Exit (0 );
94
115
}
95
116
}
96
117
#endif
0 commit comments