Skip to content

Replace ArchSpec::PiecewiseCompare() with Triple::operator==() #8280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions lldb/include/lldb/Utility/ArchSpec.h
Original file line number Diff line number Diff line change
Expand Up @@ -505,11 +505,6 @@ class ArchSpec {

bool IsFullySpecifiedTriple() const;

void PiecewiseTripleCompare(const ArchSpec &other, bool &arch_different,
bool &vendor_different, bool &os_different,
bool &os_version_different,
bool &env_different) const;

/// Detect whether this architecture uses thumb code exclusively
///
/// Some embedded ARM chips (e.g. the ARM Cortex M0-7 line) can only execute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "lldb/Core/ModuleSpec.h"
#include "lldb/Host/HostInfo.h"
#include "lldb/Host/SafeMachO.h"
#include "lldb/Host/XML.h"
#include "lldb/Symbol/Symbol.h"
#include "lldb/Target/MemoryRegionInfo.h"
Expand Down Expand Up @@ -2147,8 +2148,20 @@ bool GDBRemoteCommunicationClient::GetCurrentProcessInfo(bool allow_lazy) {
if (!value.getAsInteger(16, cpu))
++num_keys_decoded;
} else if (name.equals("cpusubtype")) {
if (!value.getAsInteger(16, sub))
if (!value.getAsInteger(16, sub)) {
++num_keys_decoded;
// Workaround for for pre-2024 Apple debugserver, which always
// returns arm64e on arm64e-capable hardware regardless of
// what the process is. This can be deleted at some point in
// the future.
if (cpu == llvm::MachO::CPU_TYPE_ARM64 &&
sub == llvm::MachO::CPU_SUBTYPE_ARM64E) {
if (GetGDBServerVersion())
if (m_gdb_server_version >= 1000 &&
m_gdb_server_version <= 1504)
sub = 0;
}
}
} else if (name.equals("triple")) {
StringExtractor extractor(value);
extractor.GetHexByteString(triple);
Expand Down
8 changes: 1 addition & 7 deletions lldb/source/Target/Target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1641,14 +1641,8 @@ bool Target::SetArchitecture(const ArchSpec &arch_spec, bool set_platform,

if (m_arch.GetSpec().IsCompatibleMatch(other)) {
compatible_local_arch = true;
bool arch_changed, vendor_changed, os_changed, os_ver_changed,
env_changed;

m_arch.GetSpec().PiecewiseTripleCompare(other, arch_changed,
vendor_changed, os_changed,
os_ver_changed, env_changed);

if (!arch_changed && !vendor_changed && !os_changed && !env_changed)
if (m_arch.GetSpec().GetTriple() == other.GetTriple())
replace_local_arch = false;
}
}
Expand Down
17 changes: 0 additions & 17 deletions lldb/source/Utility/ArchSpec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1421,23 +1421,6 @@ bool ArchSpec::IsFullySpecifiedTriple() const {
return true;
}

void ArchSpec::PiecewiseTripleCompare(
const ArchSpec &other, bool &arch_different, bool &vendor_different,
bool &os_different, bool &os_version_different, bool &env_different) const {
const llvm::Triple &me(GetTriple());
const llvm::Triple &them(other.GetTriple());

arch_different = (me.getArch() != them.getArch());

vendor_different = (me.getVendor() != them.getVendor());

os_different = (me.getOS() != them.getOS());

os_version_different = (me.getOSMajorVersion() != them.getOSMajorVersion());

env_different = (me.getEnvironment() != them.getEnvironment());
}

bool ArchSpec::IsAlwaysThumbInstructions() const {
std::string Status;
if (GetTriple().getArch() == llvm::Triple::arm ||
Expand Down
7 changes: 7 additions & 0 deletions lldb/test/API/macosx/arm64e-attach/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
C_SOURCES := main.c

# Uncomment this for local debugging.
#all:
# xcrun clang -g $(SRCDIR)/main.c -o a.out -target arm64e-apple-macosx

include Makefile.rules
35 changes: 35 additions & 0 deletions lldb/test/API/macosx/arm64e-attach/TestArm64eAttach.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil


class TestArm64eAttach(TestBase):
NO_DEBUG_INFO_TESTCASE = True

# On Darwin systems, arch arm64e means ARMv8.3 with ptrauth ABI used.
@skipIf(archs=no_match(["arm64e"]))
def test(self):
# Skip this test if not running on AArch64 target that supports PAC
if not self.isAArch64PAuth():
self.skipTest("Target must support pointer authentication.")

self.build()
popen = self.spawnSubprocess(self.getBuildArtifact(), [])

# This simulates how Xcode attaches to a process by pid/name.
error = lldb.SBError()
target = self.dbg.CreateTarget("", "arm64", "", True, error)
listener = lldb.SBListener("my.attach.listener")
process = target.AttachToProcessWithID(listener, popen.pid, error)
self.assertSuccess(error)
self.assertTrue(process, PROCESS_IS_VALID)
self.assertEqual(target.GetTriple().split('-')[0], "arm64e",
"target triple is updated correctly")

self.expect('process plugin packet send qProcessInfo',
"debugserver returns correct triple",
substrs=['cputype:100000c', 'cpusubtype:2', 'ptrsize:8'])

error = process.Kill()
self.assertSuccess(error)
2 changes: 2 additions & 0 deletions lldb/test/API/macosx/arm64e-attach/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
int getchar();
int main() { return getchar(); }
8 changes: 8 additions & 0 deletions lldb/tools/debugserver/source/DNB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,14 @@ DNBGetTSDAddressForThread(nub_process_t pid, nub_thread_t tid,
return INVALID_NUB_ADDRESS;
}

std::optional<std::pair<cpu_type_t, cpu_subtype_t>>
DNBGetMainBinaryCPUTypes(nub_process_t pid) {
MachProcessSP procSP;
if (GetProcessSP(pid, procSP))
return procSP->GetMainBinaryCPUTypes(pid);
return {};
}

JSONGenerator::ObjectSP
DNBGetAllLoadedLibrariesInfos(nub_process_t pid, bool report_load_commands) {
MachProcessSP procSP;
Expand Down
2 changes: 2 additions & 0 deletions lldb/tools/debugserver/source/DNB.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ DNBGetTSDAddressForThread(nub_process_t pid, nub_thread_t tid,
uint64_t plo_pthread_tsd_base_address_offset,
uint64_t plo_pthread_tsd_base_offset,
uint64_t plo_pthread_tsd_entry_size);
std::optional<std::pair<cpu_type_t, cpu_subtype_t>>
DNBGetMainBinaryCPUTypes(nub_process_t pid);
JSONGenerator::ObjectSP
DNBGetAllLoadedLibrariesInfos(nub_process_t pid, bool report_load_commands);
JSONGenerator::ObjectSP
Expand Down
2 changes: 2 additions & 0 deletions lldb/tools/debugserver/source/MacOSX/MachProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ class MachProcess {
const char *stdin_path, const char *stdout_path, const char *stderr_path,
bool no_stdio, MachProcess *process, int disable_aslr, DNBError &err);
nub_addr_t GetDYLDAllImageInfosAddress();
std::optional<std::pair<cpu_type_t, cpu_subtype_t>>
GetMainBinaryCPUTypes(nub_process_t pid);
static const void *PrepareForAttach(const char *path,
nub_launch_flavor_t launch_flavor,
bool waitfor, DNBError &err_str);
Expand Down
17 changes: 17 additions & 0 deletions lldb/tools/debugserver/source/MacOSX/MachProcess.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,23 @@ static bool mach_header_validity_test(uint32_t magic, uint32_t cputype) {
return FormatDynamicLibrariesIntoJSON(image_infos, report_load_commands);
}

std::optional<std::pair<cpu_type_t, cpu_subtype_t>>
MachProcess::GetMainBinaryCPUTypes(nub_process_t pid) {
int pointer_size = GetInferiorAddrSize(pid);
std::vector<struct binary_image_information> image_infos;
GetAllLoadedBinariesViaDYLDSPI(image_infos);
uint32_t platform = GetPlatform();
for (auto &image_info : image_infos)
if (GetMachOInformationFromMemory(platform, image_info.load_address,
pointer_size, image_info.macho_info))
if (image_info.macho_info.mach_header.filetype == MH_EXECUTE)
return {
{static_cast<cpu_type_t>(image_info.macho_info.mach_header.cputype),
static_cast<cpu_subtype_t>(
image_info.macho_info.mach_header.cpusubtype)}};
return {};
}

// Fetch information about the shared libraries at the given load addresses
// using the
// dyld SPIs that exist in macOS 10.12, iOS 10, tvOS 10, watchOS 3 and newer.
Expand Down
106 changes: 58 additions & 48 deletions lldb/tools/debugserver/source/RNBRemote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6216,59 +6216,14 @@ rnb_err_t RNBRemote::HandlePacket_qSymbol(const char *command) {
}
}

// Note that all numeric values returned by qProcessInfo are hex encoded,
// including the pid and the cpu type.

rnb_err_t RNBRemote::HandlePacket_qProcessInfo(const char *p) {
nub_process_t pid;
std::ostringstream rep;

// If we haven't run the process yet, return an error.
if (!m_ctx.HasValidProcessID())
return SendPacket("E68");

pid = m_ctx.ProcessID();

rep << "pid:" << std::hex << pid << ';';

int procpid_mib[4];
procpid_mib[0] = CTL_KERN;
procpid_mib[1] = KERN_PROC;
procpid_mib[2] = KERN_PROC_PID;
procpid_mib[3] = pid;
struct kinfo_proc proc_kinfo;
size_t proc_kinfo_size = sizeof(struct kinfo_proc);

if (::sysctl(procpid_mib, 4, &proc_kinfo, &proc_kinfo_size, NULL, 0) == 0) {
if (proc_kinfo_size > 0) {
rep << "parent-pid:" << std::hex << proc_kinfo.kp_eproc.e_ppid << ';';
rep << "real-uid:" << std::hex << proc_kinfo.kp_eproc.e_pcred.p_ruid
<< ';';
rep << "real-gid:" << std::hex << proc_kinfo.kp_eproc.e_pcred.p_rgid
<< ';';
rep << "effective-uid:" << std::hex << proc_kinfo.kp_eproc.e_ucred.cr_uid
<< ';';
if (proc_kinfo.kp_eproc.e_ucred.cr_ngroups > 0)
rep << "effective-gid:" << std::hex
<< proc_kinfo.kp_eproc.e_ucred.cr_groups[0] << ';';
}
}

static std::pair<cpu_type_t, cpu_subtype_t>
GetCPUTypesFromHost(nub_process_t pid) {
cpu_type_t cputype = DNBProcessGetCPUType(pid);
if (cputype == 0) {
DNBLog("Unable to get the process cpu_type, making a best guess.");
cputype = best_guess_cpu_type();
}

uint32_t addr_size = 0;
if (cputype != 0) {
rep << "cputype:" << std::hex << cputype << ";";
if (cputype & CPU_ARCH_ABI64)
addr_size = 8;
else
addr_size = 4;
}

bool host_cpu_is_64bit = false;
uint32_t is64bit_capable;
size_t is64bit_capable_len = sizeof(is64bit_capable);
Expand Down Expand Up @@ -6309,14 +6264,69 @@ rnb_err_t RNBRemote::HandlePacket_qProcessInfo(const char *p) {
if (cputype == CPU_TYPE_ARM64_32 && cpusubtype == 2)
cpusubtype = CPU_SUBTYPE_ARM64_32_V8;
#endif
}

return {cputype, cpusubtype};
}

// Note that all numeric values returned by qProcessInfo are hex encoded,
// including the pid and the cpu type.

rnb_err_t RNBRemote::HandlePacket_qProcessInfo(const char *p) {
nub_process_t pid;
std::ostringstream rep;

// If we haven't run the process yet, return an error.
if (!m_ctx.HasValidProcessID())
return SendPacket("E68");

pid = m_ctx.ProcessID();

rep << "pid:" << std::hex << pid << ';';

int procpid_mib[4];
procpid_mib[0] = CTL_KERN;
procpid_mib[1] = KERN_PROC;
procpid_mib[2] = KERN_PROC_PID;
procpid_mib[3] = pid;
struct kinfo_proc proc_kinfo;
size_t proc_kinfo_size = sizeof(struct kinfo_proc);

if (::sysctl(procpid_mib, 4, &proc_kinfo, &proc_kinfo_size, NULL, 0) == 0) {
if (proc_kinfo_size > 0) {
rep << "parent-pid:" << std::hex << proc_kinfo.kp_eproc.e_ppid << ';';
rep << "real-uid:" << std::hex << proc_kinfo.kp_eproc.e_pcred.p_ruid
<< ';';
rep << "real-gid:" << std::hex << proc_kinfo.kp_eproc.e_pcred.p_rgid
<< ';';
rep << "effective-uid:" << std::hex << proc_kinfo.kp_eproc.e_ucred.cr_uid
<< ';';
if (proc_kinfo.kp_eproc.e_ucred.cr_ngroups > 0)
rep << "effective-gid:" << std::hex
<< proc_kinfo.kp_eproc.e_ucred.cr_groups[0] << ';';
}
}

cpu_type_t cputype;
cpu_subtype_t cpusubtype;
if (auto cputypes = DNBGetMainBinaryCPUTypes(pid))
std::tie(cputype, cpusubtype) = *cputypes;
else
std::tie(cputype, cpusubtype) = GetCPUTypesFromHost(pid);

uint32_t addr_size = 0;
if (cputype != 0) {
rep << "cputype:" << std::hex << cputype << ";";
rep << "cpusubtype:" << std::hex << cpusubtype << ';';
if (cputype & CPU_ARCH_ABI64)
addr_size = 8;
else
addr_size = 4;
}

bool os_handled = false;
if (addr_size > 0) {
rep << "ptrsize:" << std::dec << addr_size << ';';

#if defined(TARGET_OS_OSX) && TARGET_OS_OSX == 1
// Try and get the OS type by looking at the load commands in the main
// executable and looking for a LC_VERSION_MIN load command. This is the
Expand Down