From 40eea802b009dcdd36ae60ffedcf937b27bbcd9b Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sat, 6 Jul 2019 12:55:56 +0100 Subject: [PATCH 1/5] Detecting if we are debugged FreeBSD/MacOS. --- Zend/zend_gdb.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/Zend/zend_gdb.c b/Zend/zend_gdb.c index 0fff7420486f..ca015bd43708 100644 --- a/Zend/zend_gdb.c +++ b/Zend/zend_gdb.c @@ -22,6 +22,10 @@ #include #include +#if defined(__APPLE__) || defined(__FreeBSD__) +#include +#include +#endif #include #include @@ -105,6 +109,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) { @@ -136,6 +141,44 @@ ZEND_API int zend_gdb_present(void) close(fd); } +#elif defined(__APPLE__) || defined(__FreeBSD__) +#if defined(__APPLE__) +#define KINFO_PROC_FLAG (cproc->kp_proc.p_flag) +#define KINFO_PROC_PPID (cproc->kp_proc.p_oppid) +#else +#define KINFO_PROC_FLAG (cproc->ki_flag) +#define KINFO_PROC_PPID (cproc->ki_ppid) +#endif + struct kinfo_proc *cproc; + size_t cproclen = 0; + int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()}; + if (sysctl(mib, 4, 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; +#if defined(__FreeBSD__) + char path[PATH_MAX]; + size_t pathlen = sizeof(path); + int smib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, ppid}; + if (sysctl(smib, 4, path, &pathlen, NULL, 0) == 0) { + if (strstr(path, "gdb")) { + ret = 1; + } + } +#else + // Does not seem there is a way to get a full path + // but only from the current process ... + // here considered "gdb-ish" + // open to debate if we throw away the mac support altogether... + ret = 1; +#endif + } + } + free(cproc); +#endif return ret; } From 32962f4cc0f77418c2f7f52623d190f46add6610 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sat, 6 Jul 2019 13:41:03 +0100 Subject: [PATCH 2/5] Keeping the parent in case... --- Zend/zend_gdb.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Zend/zend_gdb.c b/Zend/zend_gdb.c index ca015bd43708..6d137a62d3b6 100644 --- a/Zend/zend_gdb.c +++ b/Zend/zend_gdb.c @@ -171,8 +171,10 @@ ZEND_API int zend_gdb_present(void) #else // Does not seem there is a way to get a full path // but only from the current process ... + // unless there is "off the book" technique to get it ; // here considered "gdb-ish" // open to debate if we throw away the mac support altogether... + (void)ppid; ret = 1; #endif } From 7d783ba917d429bbd06c5ab93b56794136f7b842 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sat, 6 Jul 2019 14:33:45 +0100 Subject: [PATCH 3/5] Find more common grounds accross BSDs. Checking the proc title might be enough. --- Zend/zend_gdb.c | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/Zend/zend_gdb.c b/Zend/zend_gdb.c index 6d137a62d3b6..02f89a06fb2b 100644 --- a/Zend/zend_gdb.c +++ b/Zend/zend_gdb.c @@ -145,9 +145,11 @@ ZEND_API int zend_gdb_present(void) #if defined(__APPLE__) #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) #else #define KINFO_PROC_FLAG (cproc->ki_flag) #define KINFO_PROC_PPID (cproc->ki_ppid) +#define KINFO_PROC_COMM (pcproc->ki_comm) #endif struct kinfo_proc *cproc; size_t cproclen = 0; @@ -159,24 +161,15 @@ ZEND_API int zend_gdb_present(void) if (sysctl(mib, 4, cproc, &cproclen, NULL, 0) == 0) { if ((KINFO_PROC_FLAG & P_TRACED) != 0) { pid_t ppid = KINFO_PROC_PPID; -#if defined(__FreeBSD__) - char path[PATH_MAX]; - size_t pathlen = sizeof(path); - int smib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, ppid}; - if (sysctl(smib, 4, path, &pathlen, NULL, 0) == 0) { - if (strstr(path, "gdb")) { + mib[3] = ppid; + struct kinfo_proc *pcproc = (struct kinfo_proc *)malloc(cproclen); + if (sysctl(mib, 4, pcproc, &cproclen, NULL, 0) == 0) { + // Could be prefixed with package systems e.g. egdb + if (strstr(KINFO_PROC_COMM, "gdb")) { ret = 1; } } -#else - // Does not seem there is a way to get a full path - // but only from the current process ... - // unless there is "off the book" technique to get it ; - // here considered "gdb-ish" - // open to debate if we throw away the mac support altogether... - (void)ppid; - ret = 1; -#endif + free(pcproc); } } free(cproc); From c9db6211b3affd9fa0795ad88c2e66e143c37431 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sun, 7 Jul 2019 09:02:37 +0100 Subject: [PATCH 4/5] OpenBSD support --- Zend/zend_gdb.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/Zend/zend_gdb.c b/Zend/zend_gdb.c index 02f89a06fb2b..6bc34ec8d0c7 100644 --- a/Zend/zend_gdb.c +++ b/Zend/zend_gdb.c @@ -22,9 +22,10 @@ #include #include -#if defined(__APPLE__) || defined(__FreeBSD__) +#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) #include #include +#include #endif #include #include @@ -141,20 +142,29 @@ ZEND_API int zend_gdb_present(void) close(fd); } -#elif defined(__APPLE__) || defined(__FreeBSD__) +#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) #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) -#else +#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) #endif struct kinfo_proc *cproc; size_t cproclen = 0; - int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()}; - if (sysctl(mib, 4, NULL, &cproclen, NULL, 0) != 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); @@ -163,7 +173,7 @@ ZEND_API int zend_gdb_present(void) pid_t ppid = KINFO_PROC_PPID; mib[3] = ppid; struct kinfo_proc *pcproc = (struct kinfo_proc *)malloc(cproclen); - if (sysctl(mib, 4, pcproc, &cproclen, NULL, 0) == 0) { + 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; From 0b348e82b0ff6f1857bde0a3bb40793e434325af Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sun, 7 Jul 2019 09:45:35 +0100 Subject: [PATCH 5/5] NetBSD support and fixes --- Zend/zend_gdb.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Zend/zend_gdb.c b/Zend/zend_gdb.c index 6bc34ec8d0c7..727a2bec227a 100644 --- a/Zend/zend_gdb.c +++ b/Zend/zend_gdb.c @@ -22,7 +22,7 @@ #include #include -#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) +#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) #include #include #include @@ -142,14 +142,14 @@ ZEND_API int zend_gdb_present(void) close(fd); } -#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) +#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_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_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) @@ -159,6 +159,12 @@ ZEND_API int zend_gdb_present(void) #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;