Skip to content

Commit 40eea80

Browse files
committed
Detecting if we are debugged FreeBSD/MacOS.
1 parent 0707caf commit 40eea80

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Zend/zend_gdb.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222

2323
#include <sys/types.h>
2424
#include <sys/stat.h>
25+
#if defined(__APPLE__) || defined(__FreeBSD__)
26+
#include <sys/sysctl.h>
27+
#include <sys/user.h>
28+
#endif
2529
#include <fcntl.h>
2630
#include <unistd.h>
2731

@@ -105,6 +109,7 @@ ZEND_API void zend_gdb_unregister_all(void)
105109
ZEND_API int zend_gdb_present(void)
106110
{
107111
int ret = 0;
112+
#if defined(__linux__)
108113
int fd = open("/proc/self/status", O_RDONLY);
109114

110115
if (fd > 0) {
@@ -136,6 +141,44 @@ ZEND_API int zend_gdb_present(void)
136141

137142
close(fd);
138143
}
144+
#elif defined(__APPLE__) || defined(__FreeBSD__)
145+
#if defined(__APPLE__)
146+
#define KINFO_PROC_FLAG (cproc->kp_proc.p_flag)
147+
#define KINFO_PROC_PPID (cproc->kp_proc.p_oppid)
148+
#else
149+
#define KINFO_PROC_FLAG (cproc->ki_flag)
150+
#define KINFO_PROC_PPID (cproc->ki_ppid)
151+
#endif
152+
struct kinfo_proc *cproc;
153+
size_t cproclen = 0;
154+
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()};
155+
if (sysctl(mib, 4, NULL, &cproclen, NULL, 0) != 0) {
156+
return 0;
157+
}
158+
cproc = (struct kinfo_proc *)malloc(cproclen);
159+
if (sysctl(mib, 4, cproc, &cproclen, NULL, 0) == 0) {
160+
if ((KINFO_PROC_FLAG & P_TRACED) != 0) {
161+
pid_t ppid = KINFO_PROC_PPID;
162+
#if defined(__FreeBSD__)
163+
char path[PATH_MAX];
164+
size_t pathlen = sizeof(path);
165+
int smib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, ppid};
166+
if (sysctl(smib, 4, path, &pathlen, NULL, 0) == 0) {
167+
if (strstr(path, "gdb")) {
168+
ret = 1;
169+
}
170+
}
171+
#else
172+
// Does not seem there is a way to get a full path
173+
// but only from the current process ...
174+
// here considered "gdb-ish"
175+
// open to debate if we throw away the mac support altogether...
176+
ret = 1;
177+
#endif
178+
}
179+
}
180+
free(cproc);
181+
#endif
139182

140183
return ret;
141184
}

0 commit comments

Comments
 (0)