[Lldb-commits] [lldb] [lldb] checks beforehand if lldb can trace/attach a process on FreeBSD. (PR #79662)

2024-02-11 Thread David CARLIER via lldb-commits

https://github.com/devnexen updated 
https://github.com/llvm/llvm-project/pull/79662

>From e2da8be542e7116369ab91cb0ce25163e18e76f3 Mon Sep 17 00:00:00 2001
From: David Carlier 
Date: Fri, 26 Jan 2024 22:47:15 +
Subject: [PATCH] [lldb] checks if lldb can trace/attach/set a breakpoint a
 process or load a file to debug on FreeBSD.

before having the generic EINVAL message, we check if the
`security.bsd.unprivileged_proc_debug` allows process debugging.

close #79634
---
 .../Process/FreeBSD/NativeProcessFreeBSD.cpp  | 39 ---
 1 file changed, 34 insertions(+), 5 deletions(-)

diff --git a/lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.cpp 
b/lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.cpp
index 19e0986ace31ff..9c620e4807e344 100644
--- a/lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.cpp
+++ b/lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.cpp
@@ -48,20 +48,38 @@ static Status EnsureFDFlags(int fd, int flags) {
   return error;
 }
 
+static Status CanTrace() {
+  int proc_debug, ret;
+  size_t len = sizeof(proc_debug);
+  ret = ::sysctlbyname("security.bsd.unprivileged_proc_debug", &proc_debug,
+   &len, nullptr, 0);
+  if (ret != 0)
+return Status("sysctlbyname() security.bsd.unprivileged_proc_debug 
failed");
+
+  if (proc_debug < 1)
+return Status(
+"process debug disabled by security.bsd.unprivileged_proc_debug oid");
+
+  return {};
+}
+
 // Public Static Methods
 
 llvm::Expected>
 NativeProcessFreeBSD::Manager::Launch(ProcessLaunchInfo &launch_info,
   NativeDelegate &native_delegate) {
   Log *log = GetLog(POSIXLog::Process);
-
   Status status;
+
   ::pid_t pid = ProcessLauncherPosixFork()
 .LaunchProcess(launch_info, status)
 .GetProcessId();
   LLDB_LOG(log, "pid = {0:x}", pid);
   if (status.Fail()) {
+auto error = CanTrace();
 LLDB_LOG(log, "failed to launch process: {0}", status);
+if (status.Fail())
+  return error.ToError();
 return status.ToError();
   }
 
@@ -392,8 +410,11 @@ Status NativeProcessFreeBSD::PtraceWrapper(int req, 
lldb::pid_t pid, void *addr,
   ret =
   ptrace(req, static_cast<::pid_t>(pid), static_cast(addr), data);
 
-  if (ret == -1)
-error.SetErrorToErrno();
+  if (ret == -1) {
+error = CanTrace();
+if (error.Success())
+  error.SetErrorToErrno();
+  }
 
   if (result)
 *result = ret;
@@ -707,8 +728,12 @@ Status NativeProcessFreeBSD::SetBreakpoint(lldb::addr_t 
addr, uint32_t size,
 Status NativeProcessFreeBSD::GetLoadedModuleFileSpec(const char *module_path,
  FileSpec &file_spec) {
   Status error = PopulateMemoryRegionCache();
-  if (error.Fail())
+  if (error.Fail()) {
+auto status = CanTrace();
+if (status.Fail())
+  return status;
 return error;
+  }
 
   FileSpec module_file_spec(module_path);
   FileSystem::Instance().Resolve(module_file_spec);
@@ -729,8 +754,12 @@ NativeProcessFreeBSD::GetFileLoadAddress(const 
llvm::StringRef &file_name,
  lldb::addr_t &load_addr) {
   load_addr = LLDB_INVALID_ADDRESS;
   Status error = PopulateMemoryRegionCache();
-  if (error.Fail())
+  if (error.Fail()) {
+auto status = CanTrace();
+if (status.Fail())
+  return status;
 return error;
+  }
 
   FileSpec file(file_name);
   for (const auto &it : m_mem_region_cache) {

___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] 535da10 - [lldb] checks if lldb can trace/attach/set a breakpoint a process or load a file to debug on FreeBSD.

2024-02-11 Thread via lldb-commits

Author: David CARLIER
Date: 2024-02-11T08:17:53Z
New Revision: 535da10842c7309e9eeaf9828cf6bb034fecaf16

URL: 
https://github.com/llvm/llvm-project/commit/535da10842c7309e9eeaf9828cf6bb034fecaf16
DIFF: 
https://github.com/llvm/llvm-project/commit/535da10842c7309e9eeaf9828cf6bb034fecaf16.diff

LOG: [lldb] checks if lldb can trace/attach/set a breakpoint a process or load 
a file to debug on FreeBSD.

before having the generic EINVAL message, we check if the
`security.bsd.unprivileged_proc_debug` allows process debugging.

close #79634

Added: 


Modified: 
lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.cpp

Removed: 




diff  --git a/lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.cpp 
b/lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.cpp
index 19e0986ace31ff..9c620e4807e344 100644
--- a/lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.cpp
+++ b/lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.cpp
@@ -48,20 +48,38 @@ static Status EnsureFDFlags(int fd, int flags) {
   return error;
 }
 
+static Status CanTrace() {
+  int proc_debug, ret;
+  size_t len = sizeof(proc_debug);
+  ret = ::sysctlbyname("security.bsd.unprivileged_proc_debug", &proc_debug,
+   &len, nullptr, 0);
+  if (ret != 0)
+return Status("sysctlbyname() security.bsd.unprivileged_proc_debug 
failed");
+
+  if (proc_debug < 1)
+return Status(
+"process debug disabled by security.bsd.unprivileged_proc_debug oid");
+
+  return {};
+}
+
 // Public Static Methods
 
 llvm::Expected>
 NativeProcessFreeBSD::Manager::Launch(ProcessLaunchInfo &launch_info,
   NativeDelegate &native_delegate) {
   Log *log = GetLog(POSIXLog::Process);
-
   Status status;
+
   ::pid_t pid = ProcessLauncherPosixFork()
 .LaunchProcess(launch_info, status)
 .GetProcessId();
   LLDB_LOG(log, "pid = {0:x}", pid);
   if (status.Fail()) {
+auto error = CanTrace();
 LLDB_LOG(log, "failed to launch process: {0}", status);
+if (status.Fail())
+  return error.ToError();
 return status.ToError();
   }
 
@@ -392,8 +410,11 @@ Status NativeProcessFreeBSD::PtraceWrapper(int req, 
lldb::pid_t pid, void *addr,
   ret =
   ptrace(req, static_cast<::pid_t>(pid), static_cast(addr), data);
 
-  if (ret == -1)
-error.SetErrorToErrno();
+  if (ret == -1) {
+error = CanTrace();
+if (error.Success())
+  error.SetErrorToErrno();
+  }
 
   if (result)
 *result = ret;
@@ -707,8 +728,12 @@ Status NativeProcessFreeBSD::SetBreakpoint(lldb::addr_t 
addr, uint32_t size,
 Status NativeProcessFreeBSD::GetLoadedModuleFileSpec(const char *module_path,
  FileSpec &file_spec) {
   Status error = PopulateMemoryRegionCache();
-  if (error.Fail())
+  if (error.Fail()) {
+auto status = CanTrace();
+if (status.Fail())
+  return status;
 return error;
+  }
 
   FileSpec module_file_spec(module_path);
   FileSystem::Instance().Resolve(module_file_spec);
@@ -729,8 +754,12 @@ NativeProcessFreeBSD::GetFileLoadAddress(const 
llvm::StringRef &file_name,
  lldb::addr_t &load_addr) {
   load_addr = LLDB_INVALID_ADDRESS;
   Status error = PopulateMemoryRegionCache();
-  if (error.Fail())
+  if (error.Fail()) {
+auto status = CanTrace();
+if (status.Fail())
+  return status;
 return error;
+  }
 
   FileSpec file(file_name);
   for (const auto &it : m_mem_region_cache) {



___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb] checks beforehand if lldb can trace/attach a process on FreeBSD. (PR #79662)

2024-02-11 Thread David CARLIER via lldb-commits

https://github.com/devnexen closed 
https://github.com/llvm/llvm-project/pull/79662
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb][progress] Add progress manager class (PR #81319)

2024-02-11 Thread Greg Clayton via lldb-commits


@@ -66,3 +66,47 @@ void Progress::ReportProgress() {
  m_debugger_id);
   }
 }
+
+void ProgressManager::Initialize() {
+  lldbassert(!InstanceImpl() && "A progress report manager already exists.");
+  InstanceImpl().emplace();
+}
+
+void ProgressManager::Terminate() {
+  lldbassert(InstanceImpl() &&
+ "A progress report manager has already been terminated.");
+  InstanceImpl().reset();
+}
+
+std::optional &ProgressManager::InstanceImpl() {
+  static std::optional g_progress_manager;
+  return g_progress_manager;
+}
+
+ProgressManager::ProgressManager() : m_progress_map() {}
+
+ProgressManager::~ProgressManager() {}
+
+ProgressManager &ProgressManager::Instance() { return *InstanceImpl(); }

clayborg wrote:

Works for me as long as all clients check the optional and use it correctly. It 
allows for multi-thread safety so that is good. Not sure if std::optional is 
actually threadsafe though, but the timing required to make this fail would be 
pretty low percentage. If it were me I would make it bullet proof as just leak 
the map as when we tear down the process, any allocated memory will just go 
poof anyway and it will actually speed up the process teardown process as no 
work will need to be done to clear this map, though it should really be empty 
anyway.

https://github.com/llvm/llvm-project/pull/81319
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits