[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
Wed May 1 00:56:37 PDT 2024
================
@@ -0,0 +1,201 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 <__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 <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 <fstream>
+# include <string>
+#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
+
+# if defined(_LIBCPP_WIN32API)
+
+static void __breakpoint() noexcept { DebugBreak(); }
+
+# elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__linux__) || defined(_AIX)
+
+static void __breakpoint() {
+# if __has_builtin(__builtin_debugtrap)
+ __builtin_debugtrap();
+# else
+ raise(SIGTRAP);
+# endif
+}
+
+# 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;
+# elif defined(_LIBCPP_HAS_NO_LOCALIZATION)
+ _LIBCPP_ASSERT_INTERNAL(false,
+ "Function is not available. Could not open '/proc/self/status' for reading, libc++ was "
+ "compiled with _LIBCPP_HAS_NO_LOCALIZATION.");
+ 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.
+ ifstream status_file{"/proc/self/status"};
+ if (!status_file.is_open()) {
+ _LIBCPP_ASSERT_INTERNAL(false, "Could not open '/proc/self/status' for reading.");
+ return false;
+ }
+
+ std::string token;
+ while (status_file >> token) {
+ // If the process is being debugged "TracerPid"'s value is non-zero.
+ if (token == "TracerPid:") {
+ int pid;
+ status_file >> pid;
+ return pid != 0;
+ }
+ getline(status_file, token);
+ }
+
+ return false;
+# endif // _LIBCPP_HAS_NO_FILESYSTEM
+}
+
+# elif defined(_AIX)
+
+_LIBCPP_HIDE_FROM_ABI bool __is_debugger_present() noexcept {
+ // Get the status information of a process by memory mapping the file /proc/PID/status.
+ // https://www.ibm.com/docs/en/aix/7.3?topic=files-proc-file
+ char filename[] = "/proc/4294967295/status";
----------------
H-G-Hristov wrote:
```suggestion
char filename[] = "/proc/4294967295/status";
char filename[] = "/proc/XXXXXXXXXX/status";
```
Does this value hold any significance? Can we do this instead?
https://github.com/llvm/llvm-project/pull/81447
More information about the libcxx-commits
mailing list