Skip to content

Commit ebf596d

Browse files
committed
[debugserver] Honor the cpu sub type if specified
Use the newly added spawnattr API, posix_spawnattr_setarchpref_np, to select a slice preferences per cpu and subcpu types, instead of just cpu with posix_spawnattr_setarchpref_np. rdar://16094957 Differential revision: https://reviews.llvm.org/D92712 (cherry picked from commit 0db3757)
1 parent ade3acc commit ebf596d

File tree

5 files changed

+95
-42
lines changed

5 files changed

+95
-42
lines changed

lldb/tools/debugserver/source/DNB.cpp

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,19 +1723,52 @@ nub_bool_t DNBSetArchitecture(const char *arch) {
17231723
if (arch && arch[0]) {
17241724
if (strcasecmp(arch, "i386") == 0)
17251725
return DNBArchProtocol::SetArchitecture(CPU_TYPE_I386);
1726-
else if ((strcasecmp(arch, "x86_64") == 0) ||
1727-
(strcasecmp(arch, "x86_64h") == 0))
1728-
return DNBArchProtocol::SetArchitecture(CPU_TYPE_X86_64);
1729-
else if (strstr(arch, "arm64_32") == arch ||
1726+
else if (strcasecmp(arch, "x86_64") == 0)
1727+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_X86_64,
1728+
CPU_SUBTYPE_X86_64_ALL);
1729+
else if (strcasecmp(arch, "x86_64h") == 0)
1730+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_X86_64,
1731+
CPU_SUBTYPE_X86_64_H);
1732+
else if (strstr(arch, "arm64_32") == arch ||
17301733
strstr(arch, "aarch64_32") == arch)
17311734
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM64_32);
17321735
else if (strstr(arch, "arm64e") == arch)
1733-
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM64);
1734-
else if (strstr(arch, "arm64") == arch || strstr(arch, "armv8") == arch ||
1735-
strstr(arch, "aarch64") == arch)
1736-
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM64);
1736+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM64,
1737+
CPU_SUBTYPE_ARM64E);
1738+
else if (strstr(arch, "arm64") == arch || strstr(arch, "aarch64") == arch)
1739+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM64,
1740+
CPU_SUBTYPE_ARM64_ALL);
1741+
else if (strstr(arch, "armv8") == arch)
1742+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM64,
1743+
CPU_SUBTYPE_ARM64_V8);
1744+
else if (strstr(arch, "armv7em") == arch)
1745+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM,
1746+
CPU_SUBTYPE_ARM_V7EM);
1747+
else if (strstr(arch, "armv7m") == arch)
1748+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM,
1749+
CPU_SUBTYPE_ARM_V7M);
1750+
else if (strstr(arch, "armv7k") == arch)
1751+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM,
1752+
CPU_SUBTYPE_ARM_V7K);
1753+
else if (strstr(arch, "armv7s") == arch)
1754+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM,
1755+
CPU_SUBTYPE_ARM_V7S);
1756+
else if (strstr(arch, "armv7") == arch)
1757+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7);
1758+
else if (strstr(arch, "armv6m") == arch)
1759+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM,
1760+
CPU_SUBTYPE_ARM_V6M);
1761+
else if (strstr(arch, "armv6") == arch)
1762+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V6);
1763+
else if (strstr(arch, "armv5") == arch)
1764+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM,
1765+
CPU_SUBTYPE_ARM_V5TEJ);
1766+
else if (strstr(arch, "armv4t") == arch)
1767+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM,
1768+
CPU_SUBTYPE_ARM_V4T);
17371769
else if (strstr(arch, "arm") == arch)
1738-
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM);
1770+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM,
1771+
CPU_SUBTYPE_ARM_ALL);
17391772
}
17401773
return false;
17411774
}

lldb/tools/debugserver/source/DNBArch.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
typedef std::map<uint32_t, DNBArchPluginInfo> CPUPluginInfoMap;
2222

2323
static uint32_t g_current_cpu_type = 0;
24+
static uint32_t g_current_cpu_subtype = 0;
2425
CPUPluginInfoMap g_arch_plugins;
2526

2627
static const DNBArchPluginInfo *GetArchInfo() {
@@ -31,15 +32,17 @@ static const DNBArchPluginInfo *GetArchInfo() {
3132
return NULL;
3233
}
3334

34-
uint32_t DNBArchProtocol::GetArchitecture() { return g_current_cpu_type; }
35+
uint32_t DNBArchProtocol::GetCPUType() { return g_current_cpu_type; }
36+
uint32_t DNBArchProtocol::GetCPUSubType() { return g_current_cpu_subtype; }
3537

36-
bool DNBArchProtocol::SetArchitecture(uint32_t cpu_type) {
38+
bool DNBArchProtocol::SetArchitecture(uint32_t cpu_type, uint32_t cpu_subtype) {
3739
g_current_cpu_type = cpu_type;
40+
g_current_cpu_subtype = cpu_subtype;
3841
bool result = g_arch_plugins.find(g_current_cpu_type) != g_arch_plugins.end();
39-
DNBLogThreadedIf(
40-
LOG_PROCESS,
41-
"DNBArchProtocol::SetDefaultArchitecture (cpu_type=0x%8.8x) => %i",
42-
cpu_type, result);
42+
DNBLogThreadedIf(LOG_PROCESS,
43+
"DNBArchProtocol::SetDefaultArchitecture (cpu_type=0x%8.8x, "
44+
"cpu_subtype=0x%8.8x) => %i",
45+
cpu_type, cpu_subtype, result);
4346
return result;
4447
}
4548

lldb/tools/debugserver/source/DNBArch.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ class DNBArchProtocol {
4949

5050
static void RegisterArchPlugin(const DNBArchPluginInfo &arch_info);
5151

52-
static uint32_t GetArchitecture();
52+
static uint32_t GetCPUType();
53+
static uint32_t GetCPUSubType();
5354

54-
static bool SetArchitecture(uint32_t cpu_type);
55+
static bool SetArchitecture(uint32_t cpu_type, uint32_t cpu_subtype = 0);
5556

5657
DNBArchProtocol() : m_save_id(0) {}
5758

lldb/tools/debugserver/source/MacOSX/MachProcess.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ class MachProcess {
9090
char const *envp[],
9191
MachProcess *process, DNBError &err);
9292
static pid_t PosixSpawnChildForPTraceDebugging(
93-
const char *path, cpu_type_t cpu_type, char const *argv[],
94-
char const *envp[], const char *working_directory, const char *stdin_path,
95-
const char *stdout_path, const char *stderr_path, bool no_stdio,
96-
MachProcess *process, int disable_aslr, DNBError &err);
93+
const char *path, cpu_type_t cpu_type, cpu_subtype_t cpu_subtype,
94+
char const *argv[], char const *envp[], const char *working_directory,
95+
const char *stdin_path, const char *stdout_path, const char *stderr_path,
96+
bool no_stdio, MachProcess *process, int disable_aslr, DNBError &err);
9797
nub_addr_t GetDYLDAllImageInfosAddress();
9898
static const void *PrepareForAttach(const char *path,
9999
nub_launch_flavor_t launch_flavor,

lldb/tools/debugserver/source/MacOSX/MachProcess.mm

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3148,9 +3148,9 @@ static bool FBSAddEventDataToOptions(NSMutableDictionary *options,
31483148

31493149
case eLaunchFlavorPosixSpawn:
31503150
m_pid = MachProcess::PosixSpawnChildForPTraceDebugging(
3151-
path, DNBArchProtocol::GetArchitecture(), argv, envp, working_directory,
3152-
stdin_path, stdout_path, stderr_path, no_stdio, this, disable_aslr,
3153-
launch_err);
3151+
path, DNBArchProtocol::GetCPUType(), DNBArchProtocol::GetCPUSubType(),
3152+
argv, envp, working_directory, stdin_path, stdout_path, stderr_path,
3153+
no_stdio, this, disable_aslr, launch_err);
31543154
break;
31553155

31563156
default:
@@ -3210,10 +3210,10 @@ static bool FBSAddEventDataToOptions(NSMutableDictionary *options,
32103210
}
32113211

32123212
pid_t MachProcess::PosixSpawnChildForPTraceDebugging(
3213-
const char *path, cpu_type_t cpu_type, char const *argv[],
3214-
char const *envp[], const char *working_directory, const char *stdin_path,
3215-
const char *stdout_path, const char *stderr_path, bool no_stdio,
3216-
MachProcess *process, int disable_aslr, DNBError &err) {
3213+
const char *path, cpu_type_t cpu_type, cpu_subtype_t cpu_subtype,
3214+
char const *argv[], char const *envp[], const char *working_directory,
3215+
const char *stdin_path, const char *stdout_path, const char *stderr_path,
3216+
bool no_stdio, MachProcess *process, int disable_aslr, DNBError &err) {
32173217
posix_spawnattr_t attr;
32183218
short flags;
32193219
DNBLogThreadedIf(LOG_PROCESS,
@@ -3256,24 +3256,40 @@ static bool FBSAddEventDataToOptions(NSMutableDictionary *options,
32563256

32573257
// On SnowLeopard we should set "DYLD_NO_PIE" in the inferior environment....
32583258

3259-
#if !defined(__arm__)
3260-
3261-
// We don't need to do this for ARM, and we really shouldn't now that we
3262-
// have multiple CPU subtypes and no posix_spawnattr call that allows us
3263-
// to set which CPU subtype to launch...
32643259
if (cpu_type != 0) {
32653260
size_t ocount = 0;
3266-
err.SetError(::posix_spawnattr_setbinpref_np(&attr, 1, &cpu_type, &ocount),
3267-
DNBError::POSIX);
3268-
if (err.Fail() || DNBLogCheckLogBit(LOG_PROCESS))
3269-
err.LogThreaded("::posix_spawnattr_setbinpref_np ( &attr, 1, cpu_type = "
3270-
"0x%8.8x, count => %llu )",
3271-
cpu_type, (uint64_t)ocount);
3261+
bool slice_preference_set = false;
3262+
3263+
if (cpu_subtype != 0) {
3264+
if (@available(macOS 10.16, ios 10.14, watchos 7.0, tvos 14.0,
3265+
bridgeos 5.0, *)) {
3266+
err.SetError(posix_spawnattr_setarchpref_np(&attr, 1, &cpu_type,
3267+
&cpu_subtype, &ocount));
3268+
slice_preference_set = err.Success();
3269+
if (err.Fail() || DNBLogCheckLogBit(LOG_PROCESS))
3270+
err.LogThreaded(
3271+
"::posix_spawnattr_setarchpref_np ( &attr, 1, cpu_type = "
3272+
"0x%8.8x, cpu_subtype = 0x%8.8x, count => %llu )",
3273+
cpu_type, cpu_subtype, (uint64_t)ocount);
3274+
if (err.Fail() != 0 || ocount != 1)
3275+
return INVALID_NUB_PROCESS;
3276+
}
3277+
}
32723278

3273-
if (err.Fail() != 0 || ocount != 1)
3274-
return INVALID_NUB_PROCESS;
3279+
if (!slice_preference_set) {
3280+
err.SetError(
3281+
::posix_spawnattr_setbinpref_np(&attr, 1, &cpu_type, &ocount),
3282+
DNBError::POSIX);
3283+
if (err.Fail() || DNBLogCheckLogBit(LOG_PROCESS))
3284+
err.LogThreaded(
3285+
"::posix_spawnattr_setbinpref_np ( &attr, 1, cpu_type = "
3286+
"0x%8.8x, count => %llu )",
3287+
cpu_type, (uint64_t)ocount);
3288+
3289+
if (err.Fail() != 0 || ocount != 1)
3290+
return INVALID_NUB_PROCESS;
3291+
}
32753292
}
3276-
#endif
32773293

32783294
PseudoTerminal pty;
32793295

0 commit comments

Comments
 (0)