Skip to content

Commit 535da10

Browse files
authored
[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
1 parent ab2cef5 commit 535da10

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

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

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,38 @@ static Status EnsureFDFlags(int fd, int flags) {
4848
return error;
4949
}
5050

51+
static Status CanTrace() {
52+
int proc_debug, ret;
53+
size_t len = sizeof(proc_debug);
54+
ret = ::sysctlbyname("security.bsd.unprivileged_proc_debug", &proc_debug,
55+
&len, nullptr, 0);
56+
if (ret != 0)
57+
return Status("sysctlbyname() security.bsd.unprivileged_proc_debug failed");
58+
59+
if (proc_debug < 1)
60+
return Status(
61+
"process debug disabled by security.bsd.unprivileged_proc_debug oid");
62+
63+
return {};
64+
}
65+
5166
// Public Static Methods
5267

5368
llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
5469
NativeProcessFreeBSD::Manager::Launch(ProcessLaunchInfo &launch_info,
5570
NativeDelegate &native_delegate) {
5671
Log *log = GetLog(POSIXLog::Process);
57-
5872
Status status;
73+
5974
::pid_t pid = ProcessLauncherPosixFork()
6075
.LaunchProcess(launch_info, status)
6176
.GetProcessId();
6277
LLDB_LOG(log, "pid = {0:x}", pid);
6378
if (status.Fail()) {
79+
auto error = CanTrace();
6480
LLDB_LOG(log, "failed to launch process: {0}", status);
81+
if (status.Fail())
82+
return error.ToError();
6583
return status.ToError();
6684
}
6785

@@ -392,8 +410,11 @@ Status NativeProcessFreeBSD::PtraceWrapper(int req, lldb::pid_t pid, void *addr,
392410
ret =
393411
ptrace(req, static_cast<::pid_t>(pid), static_cast<caddr_t>(addr), data);
394412

395-
if (ret == -1)
396-
error.SetErrorToErrno();
413+
if (ret == -1) {
414+
error = CanTrace();
415+
if (error.Success())
416+
error.SetErrorToErrno();
417+
}
397418

398419
if (result)
399420
*result = ret;
@@ -707,8 +728,12 @@ Status NativeProcessFreeBSD::SetBreakpoint(lldb::addr_t addr, uint32_t size,
707728
Status NativeProcessFreeBSD::GetLoadedModuleFileSpec(const char *module_path,
708729
FileSpec &file_spec) {
709730
Status error = PopulateMemoryRegionCache();
710-
if (error.Fail())
731+
if (error.Fail()) {
732+
auto status = CanTrace();
733+
if (status.Fail())
734+
return status;
711735
return error;
736+
}
712737

713738
FileSpec module_file_spec(module_path);
714739
FileSystem::Instance().Resolve(module_file_spec);
@@ -729,8 +754,12 @@ NativeProcessFreeBSD::GetFileLoadAddress(const llvm::StringRef &file_name,
729754
lldb::addr_t &load_addr) {
730755
load_addr = LLDB_INVALID_ADDRESS;
731756
Status error = PopulateMemoryRegionCache();
732-
if (error.Fail())
757+
if (error.Fail()) {
758+
auto status = CanTrace();
759+
if (status.Fail())
760+
return status;
733761
return error;
762+
}
734763

735764
FileSpec file(file_name);
736765
for (const auto &it : m_mem_region_cache) {

0 commit comments

Comments
 (0)