Index: runtime/vm/thread_interrupter_macos.cc |
diff --git a/runtime/vm/thread_interrupter_macos.cc b/runtime/vm/thread_interrupter_macos.cc |
index fb9e145f019fd5a565fb56858176a437879093ba..dd481deba77dd05c1035ca1f851f5fd484805d53 100644 |
--- a/runtime/vm/thread_interrupter_macos.cc |
+++ b/runtime/vm/thread_interrupter_macos.cc |
@@ -6,6 +6,11 @@ |
#if defined(TARGET_OS_MACOS) |
#include <errno.h> // NOLINT |
+#include <assert.h> // NOLINT |
+#include <stdbool.h> // NOLINT |
+#include <sys/types.h> // NOLINT |
+#include <unistd.h> // NOLINT |
+#include <sys/sysctl.h> // NOLINT |
#include "vm/flags.h" |
#include "vm/os.h" |
@@ -20,6 +25,34 @@ namespace dart { |
DECLARE_FLAG(bool, thread_interrupter); |
DECLARE_FLAG(bool, trace_thread_interrupter); |
+// Returns true if the current process is being debugged (either |
+// running under the debugger or has a debugger attached post facto). |
+// Code from https://developer.apple.com/library/content/qa/qa1361/_index.html |
+bool ThreadInterrupter::IsDebuggerAttached() { |
+ int junk; |
+ int mib[4]; |
+ struct kinfo_proc info; |
+ size_t size; |
+ |
+ // Initialize the flags so that, if sysctl fails for some bizarre |
+ // reason, we get a predictable result. |
+ info.kp_proc.p_flag = 0; |
+ |
+ // Initialize mib, which tells sysctl the info we want, in this case |
+ // we're looking for information about a specific process ID. |
+ mib[0] = CTL_KERN; |
+ mib[1] = KERN_PROC; |
+ mib[2] = KERN_PROC_PID; |
+ mib[3] = getpid(); |
+ |
+ // Call sysctl. |
+ size = sizeof(info); |
Vyacheslav Egorov (Google)
2017/02/09 17:14:05
Maybe better structure this code like this:
struc
Cutch
2017/02/09 18:34:07
Done.
|
+ junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0); |
+ ASSERT(junk == 0); |
+ // We're being debugged if the P_TRACED flag is set. |
+ return ((info.kp_proc.p_flag & P_TRACED) != 0); |
+} |
+ |
class ThreadInterrupterMacOS : public AllStatic { |
public: |
static void ThreadInterruptSignalHandler(int signal, |