[libcxx-commits] [libcxx] WIP - [libc++][debugging] P2546R5: Debugging support & P2810R4: `is_debugger_present` `is_replaceable` (PR #81447)

Hristo Hristov via libcxx-commits libcxx-commits at lists.llvm.org
Sun May 5 01:56:59 PDT 2024


================
@@ -0,0 +1,189 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include <__assert>
+#include <__config>
+#include <debugging>
+
+#if defined(_LIBCPP_WIN32API)
+#  define WIN32_LEAN_AND_MEAN
+#  define NOMINMAX
+#  include <windows.h>
+#elif defined(__APPLE__) || defined(__FreeBSD__)
+#  if defined(__FreeBSD__) // Include order matters.
+#    include <libutil.h>
+#    include <sys/param.h>
+#    include <sys/proc.h>
+#    include <sys/user.h>
+#  endif // defined(__FreeBSD__)
+#  include <array>
+#  include <csignal>
+#  include <sys/sysctl.h>
+#  include <sys/types.h>
+#  include <unistd.h>
+#elif defined(__linux__)
+#  include <csignal>
+#  include <cstdio>
+#  include <cstdlib>
+#  include <cstring>
+#elif defined(_AIX)
+#  include <charconv>
+#  include <csignal>
+#  include <cstring>
+#  include <fcntl.h>
+#  include <sys/mman.h>
+#  include <sys/proc.h>
+#  include <sys/procfs.h>
+#  include <sys/types.h>
+#  include <unistd.h>
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if defined(_LIBCPP_HAS_DEBUGGING)
+
+// `breakpoint()` implementation
+
+_LIBCPP_EXPORTED_FROM_ABI void __breakpoint() noexcept {
+#  if defined(_LIBCPP_WIN32API)
+  DebugBreak();
+#  elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__linux__) || defined(_AIX)
+  raise(SIGTRAP);
+#  else
+#    error "'std::breakpoint()' is not implemented on this platform."
+#  endif // defined(_LIBCPP_WIN32API)
+}
+
+// `is_debugger_present()` implementation
+
+#  if defined(_LIBCPP_WIN32API)
+
+static bool __is_debugger_present() noexcept { return IsDebuggerPresent(); }
+
+#  elif defined(__APPLE__) || defined(__FreeBSD__)
+
+// Returns true if the current process is being debugged (either
+// running under the debugger or has a debugger attached post facto).
+static bool __is_debugger_present() noexcept {
+  // Technical Q&A QA1361: Detecting the Debugger
+  // https://developer.apple.com/library/archive/qa/qa1361/_index.html
+
+  // Initialize mib, which tells 'sysctl' to fetch the information about the current process.
+
+  array __mib{CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()};
+
+  // Initialize the flags so that, if 'sysctl' fails for some bizarre
+  // reason, we get a predictable result.
+
+  struct kinfo_proc __info {};
+
+  // Call sysctl.
+  // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/sysctl.3.html
+
+  size_t __info_size = sizeof(__info);
+  if (sysctl(__mib.data(), __mib.size(), &__info, &__info_size, nullptr, 0) != 0) {
+    _LIBCPP_ASSERT_INTERNAL(false, "'sysctl' runtime error");
+    return false;
+  }
+
+  // If the process is being debugged if the 'P_TRACED' flag is set.
+  // https://github.com/freebsd/freebsd-src/blob/7f3184ba797452703904d33377dada5f0f8eae96/sys/sys/proc.h#L822
+
+#    if defined(__FreeBSD__)
+  const auto __p_flag = __info.ki_flag;
+#    else // __APPLE__
+  const auto __p_flag = __info.kp_proc.p_flag;
+#    endif
+
+  return ((__p_flag & P_TRACED) != 0);
+}
+
+#  elif defined(__linux__)
+
+static bool __is_debugger_present() noexcept {
+#    if defined(_LIBCPP_HAS_NO_FILESYSTEM)
+  _LIBCPP_ASSERT_INTERNAL(false,
+                          "Function is not available. Could not open '/proc/self/status' for reading, libc++ was "
+                          "compiled with _LIBCPP_HAS_NO_FILESYSTEM.");
+  return false;
+#    else
+  // https://docs.kernel.org/filesystems/proc.html
+
+  // Get the status information of a process by reading the file /proc/PID/status.
+  // The link 'self' points to the process reading the file system.
+  FILE* __proc_status_fp = fopen("/proc/self/status", "r");
+  if (__proc_status_fp == nullptr) {
+    _LIBCPP_ASSERT_INTERNAL(false, "Could not open '/proc/self/status' for reading.");
----------------
H-G-Hristov wrote:

Note: I don't have an idea how to test these internal assert here, above and below.

https://github.com/llvm/llvm-project/pull/81447


More information about the libcxx-commits mailing list