NetBSD: Add support for PAL_IsDebuggerPresent()
authorKamil Rytarowski <n54@gmx.com>
Fri, 15 Apr 2016 11:23:31 +0000 (13:23 +0200)
committerKamil Rytarowski <n54@gmx.com>
Fri, 15 Apr 2016 22:05:23 +0000 (00:05 +0200)
Reuse the kvm(3) interface to grab "struct kinfo_proc".

NAME
     kvm - kernel memory interface

LIBRARY
     Kernel Data Access Library (libkvm, -lkvm)

DESCRIPTION
     The kvm library provides a uniform interface for accessing kernel virtual
     memory images, including live systems and crash dumps.  Access to live
     systems is via /dev/mem while crash dumps can be examined via the core
     file generated by savecore(8).  The interface behaves identically in both
     cases.  Memory can be read and written, kernel symbol addresses can be
     looked up efficiently, and information about user processes can be
     gathered.

     kvm_open() is first called to obtain a descriptor for all subsequent
     calls

Commit migrated from https://github.com/dotnet/coreclr/commit/06ff0ff180dc0aa512481e5e4885b1f16e785a81

src/coreclr/src/pal/src/CMakeLists.txt
src/coreclr/src/pal/src/init/pal.cpp

index e8e1655..15dab11 100644 (file)
@@ -271,13 +271,16 @@ if(CMAKE_SYSTEM_NAME STREQUAL Linux)
 endif(CMAKE_SYSTEM_NAME STREQUAL Linux)
 
 if(CMAKE_SYSTEM_NAME STREQUAL NetBSD)
+  add_definitions(-D_KMEMUSER)
   find_library(UNWIND unwind)
   find_library(INTL intl)
+  find_library(KVM kvm)
   target_link_libraries(coreclrpal
     pthread
     rt
     ${UNWIND}
     ${INTL}
+    ${KVM}
   )
 endif(CMAKE_SYSTEM_NAME STREQUAL NetBSD)
 
index 0357ad9..afbee79 100644 (file)
@@ -72,6 +72,13 @@ int CacheLineSize;
 #include <mach-o/dyld.h>
 #endif // __APPLE__
 
+#ifdef __NetBSD__
+#include <sys/cdefs.h>
+#include <sys/param.h>
+#include <sys/sysctl.h>
+#include <kvm.h>
+#endif
+
 using namespace CorUnix;
 
 //
@@ -706,6 +713,31 @@ PAL_IsDebuggerPresent()
         return ((info.kp_proc.p_flag & P_TRACED) != 0);
 
     return FALSE;
+#elif defined(__NetBSD__)
+    int traced;
+    kvm_t *kd;
+    int cnt;
+
+    struct kinfo_proc *info;
+
+    kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, "kvm_open");
+    if (kd == NULL)
+        return FALSE;
+
+    info = kvm_getprocs(kd, KERN_PROC_PID, getpid(), &cnt);
+    if (info == NULL || cnt < 1)
+    {
+        kvm_close(kd);
+        return FALSE;
+    }
+
+    traced = info->kp_proc.p_slflag & PSL_TRACED;
+    kvm_close(kd);
+
+    if (traced != 0)
+        return TRUE;
+    else
+        return FALSE;
 #else
     return FALSE;
 #endif