Skip to content

Detecting if we are debugged BSD/MacOS. #4367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions Zend/zend_gdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@

#include <sys/types.h>
#include <sys/stat.h>
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
#include <sys/sysctl.h>
#include <sys/user.h>
#include <sys/proc.h>
#endif
#include <fcntl.h>
#include <unistd.h>

Expand Down Expand Up @@ -105,6 +110,7 @@ ZEND_API void zend_gdb_unregister_all(void)
ZEND_API int zend_gdb_present(void)
{
int ret = 0;
#if defined(__linux__)
int fd = open("/proc/self/status", O_RDONLY);

if (fd > 0) {
Expand Down Expand Up @@ -136,6 +142,54 @@ ZEND_API int zend_gdb_present(void)

close(fd);
}
#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
#if defined(__APPLE__)
#define KINFO_MIB int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()};
#define KINFO_PROC_FLAG (cproc->kp_proc.p_flag)
#define KINFO_PROC_PPID (cproc->kp_proc.p_oppid)
#define KINFO_PROC_COMM (pcproc->kp_proc.p_comm)
#elif defined(__FreeBSD__)
#define KINFO_MIB int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()};
#define KINFO_PROC_FLAG (cproc->ki_flag)
#define KINFO_PROC_PPID (cproc->ki_ppid)
#define KINFO_PROC_COMM (pcproc->ki_comm)
#elif defined(__OpenBSD__)
#define P_TRACED PS_TRACED
#define KINFO_MIB int mib[6] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid(), sizeof(struct kinfo_proc), 1};
#define KINFO_PROC_FLAG (cproc->p_psflags)
#define KINFO_PROC_PPID (cproc->p_ppid)
#define KINFO_PROC_COMM (pcproc->p_comm)
#elif defined(__NetBSD__)
#define kinfo_proc kinfo_proc2
#define KINFO_MIB int mib[6] = {CTL_KERN, KERN_PROC2, KERN_PROC_PID, getpid(), sizeof(struct kinfo_proc2), 1};
#define KINFO_PROC_FLAG (cproc->p_flag)
#define KINFO_PROC_PPID (cproc->p_ppid)
#define KINFO_PROC_COMM (pcproc->p_comm)
#endif
struct kinfo_proc *cproc;
size_t cproclen = 0;
KINFO_MIB
const size_t miblen = sizeof(mib)/sizeof(mib[0]);
if (sysctl(mib, miblen, NULL, &cproclen, NULL, 0) != 0) {
return 0;
}
cproc = (struct kinfo_proc *)malloc(cproclen);
if (sysctl(mib, 4, cproc, &cproclen, NULL, 0) == 0) {
if ((KINFO_PROC_FLAG & P_TRACED) != 0) {
pid_t ppid = KINFO_PROC_PPID;
mib[3] = ppid;
struct kinfo_proc *pcproc = (struct kinfo_proc *)malloc(cproclen);
if (sysctl(mib, miblen, pcproc, &cproclen, NULL, 0) == 0) {
// Could be prefixed with package systems e.g. egdb
if (strstr(KINFO_PROC_COMM, "gdb")) {
ret = 1;
}
}
free(pcproc);
}
}
free(cproc);
#endif

return ret;
}