Skip to content

Commit 394e7de

Browse files
authored
[LLDB, x86, FreeBSD] Fix Architecture parsing by reading the ELF header. (#162811)
Currently, LLDB in FreeBSD host sets the Process Architecture used by lldbserver as Default one. Which cause problem when trying to debug a 32bit binary on amd64 platform since the lldb itself will found mismatch architecture with lldbserver's return. Notice that this patch is only a partial fix for the debugging problem. We are still unable to debug x86 on x86_64 so that we don't provide testcase in this patch. See: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=289945
1 parent 1e84cb5 commit 394e7de

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

lldb/source/Host/freebsd/Host.cpp

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
#include <sys/sysctl.h>
1515
#include <sys/user.h>
1616

17-
#include <machine/elf.h>
18-
1917
#include <cstdio>
2018
#include <dlfcn.h>
2119
#include <execinfo.h>
2220

21+
#include "llvm/Object/ELF.h"
22+
23+
#include "lldb/Host/FileSystem.h"
2324
#include "lldb/Host/Host.h"
2425
#include "lldb/Host/HostInfo.h"
2526
#include "lldb/Utility/DataBufferHeap.h"
@@ -97,17 +98,33 @@ GetFreeBSDProcessArgs(const ProcessInstanceInfoMatch *match_info_ptr,
9798
proc_args.AppendArgument(llvm::StringRef(cstr));
9899
}
99100

100-
return true;
101-
}
102-
103-
static bool GetFreeBSDProcessCPUType(ProcessInstanceInfo &process_info) {
104-
if (process_info.ProcessIDIsValid()) {
105-
process_info.GetArchitecture() =
106-
HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
101+
auto buffer_sp = FileSystem::Instance().CreateDataBuffer(pathname, 0x20, 0);
102+
if (!buffer_sp) {
103+
process_info.Clear();
107104
return true;
108105
}
109-
process_info.GetArchitecture().Clear();
110-
return false;
106+
uint8_t exe_class =
107+
llvm::object::getElfArchType(
108+
{reinterpret_cast<const char *>(buffer_sp->GetBytes()),
109+
size_t(buffer_sp->GetByteSize())})
110+
.first;
111+
112+
switch (exe_class) {
113+
case llvm::ELF::ELFCLASS32:
114+
process_info.SetArchitecture(
115+
HostInfo::GetArchitecture(HostInfo::eArchKind32));
116+
break;
117+
case llvm::ELF::ELFCLASS64:
118+
process_info.SetArchitecture(
119+
HostInfo::GetArchitecture(HostInfo::eArchKind64));
120+
break;
121+
case llvm::ELF::ELFCLASSNONE:
122+
process_info.SetArchitecture(
123+
HostInfo::GetArchitecture(HostInfo::eArchKindDefault));
124+
break;
125+
}
126+
127+
return true;
111128
}
112129

113130
static bool GetFreeBSDProcessUserAndGroup(ProcessInstanceInfo &process_info) {
@@ -214,7 +231,6 @@ uint32_t Host::FindProcessesImpl(const ProcessInstanceInfoMatch &match_info,
214231
// Make sure our info matches before we go fetch the name and cpu type
215232
if (match_info_noname.Matches(process_info) &&
216233
GetFreeBSDProcessArgs(&match_info, process_info)) {
217-
GetFreeBSDProcessCPUType(process_info);
218234
if (match_info.Matches(process_info))
219235
process_infos.push_back(process_info);
220236
}
@@ -228,7 +244,6 @@ bool Host::GetProcessInfo(lldb::pid_t pid, ProcessInstanceInfo &process_info) {
228244

229245
if (GetFreeBSDProcessArgs(NULL, process_info)) {
230246
// should use libprocstat instead of going right into sysctl?
231-
GetFreeBSDProcessCPUType(process_info);
232247
GetFreeBSDProcessUserAndGroup(process_info);
233248
return true;
234249
}

0 commit comments

Comments
 (0)