|
22 | 22 |
|
23 | 23 | #include <sys/types.h>
|
24 | 24 | #include <sys/stat.h>
|
| 25 | +#if defined(__APPLE__) || defined(__FreeBSD__) |
| 26 | +#include <sys/sysctl.h> |
| 27 | +#include <sys/user.h> |
| 28 | +#endif |
25 | 29 | #include <fcntl.h>
|
26 | 30 | #include <unistd.h>
|
27 | 31 |
|
@@ -105,6 +109,7 @@ ZEND_API void zend_gdb_unregister_all(void)
|
105 | 109 | ZEND_API int zend_gdb_present(void)
|
106 | 110 | {
|
107 | 111 | int ret = 0;
|
| 112 | +#if defined(__linux__) |
108 | 113 | int fd = open("/proc/self/status", O_RDONLY);
|
109 | 114 |
|
110 | 115 | if (fd > 0) {
|
@@ -136,6 +141,44 @@ ZEND_API int zend_gdb_present(void)
|
136 | 141 |
|
137 | 142 | close(fd);
|
138 | 143 | }
|
| 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 |
139 | 182 |
|
140 | 183 | return ret;
|
141 | 184 | }
|
0 commit comments