Skip to content

Commit 0db3757

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
1 parent 315fab4 commit 0db3757

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
@@ -1762,19 +1762,52 @@ nub_bool_t DNBSetArchitecture(const char *arch) {
17621762
if (arch && arch[0]) {
17631763
if (strcasecmp(arch, "i386") == 0)
17641764
return DNBArchProtocol::SetArchitecture(CPU_TYPE_I386);
1765-
else if ((strcasecmp(arch, "x86_64") == 0) ||
1766-
(strcasecmp(arch, "x86_64h") == 0))
1767-
return DNBArchProtocol::SetArchitecture(CPU_TYPE_X86_64);
1768-
else if (strstr(arch, "arm64_32") == arch ||
1765+
else if (strcasecmp(arch, "x86_64") == 0)
1766+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_X86_64,
1767+
CPU_SUBTYPE_X86_64_ALL);
1768+
else if (strcasecmp(arch, "x86_64h") == 0)
1769+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_X86_64,
1770+
CPU_SUBTYPE_X86_64_H);
1771+
else if (strstr(arch, "arm64_32") == arch ||
17691772
strstr(arch, "aarch64_32") == arch)
17701773
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM64_32);
17711774
else if (strstr(arch, "arm64e") == arch)
1772-
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM64);
1773-
else if (strstr(arch, "arm64") == arch || strstr(arch, "armv8") == arch ||
1774-
strstr(arch, "aarch64") == arch)
1775-
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM64);
1775+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM64,
1776+
CPU_SUBTYPE_ARM64E);
1777+
else if (strstr(arch, "arm64") == arch || strstr(arch, "aarch64") == arch)
1778+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM64,
1779+
CPU_SUBTYPE_ARM64_ALL);
1780+
else if (strstr(arch, "armv8") == arch)
1781+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM64,
1782+
CPU_SUBTYPE_ARM64_V8);
1783+
else if (strstr(arch, "armv7em") == arch)
1784+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM,
1785+
CPU_SUBTYPE_ARM_V7EM);
1786+
else if (strstr(arch, "armv7m") == arch)
1787+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM,
1788+
CPU_SUBTYPE_ARM_V7M);
1789+
else if (strstr(arch, "armv7k") == arch)
1790+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM,
1791+
CPU_SUBTYPE_ARM_V7K);
1792+
else if (strstr(arch, "armv7s") == arch)
1793+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM,
1794+
CPU_SUBTYPE_ARM_V7S);
1795+
else if (strstr(arch, "armv7") == arch)
1796+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7);
1797+
else if (strstr(arch, "armv6m") == arch)
1798+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM,
1799+
CPU_SUBTYPE_ARM_V6M);
1800+
else if (strstr(arch, "armv6") == arch)
1801+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V6);
1802+
else if (strstr(arch, "armv5") == arch)
1803+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM,
1804+
CPU_SUBTYPE_ARM_V5TEJ);
1805+
else if (strstr(arch, "armv4t") == arch)
1806+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM,
1807+
CPU_SUBTYPE_ARM_V4T);
17761808
else if (strstr(arch, "arm") == arch)
1777-
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM);
1809+
return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM,
1810+
CPU_SUBTYPE_ARM_ALL);
17781811
}
17791812
return false;
17801813
}

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
@@ -92,10 +92,10 @@ class MachProcess {
9292
char const *envp[],
9393
MachProcess *process, DNBError &err);
9494
static pid_t PosixSpawnChildForPTraceDebugging(
95-
const char *path, cpu_type_t cpu_type, char const *argv[],
96-
char const *envp[], const char *working_directory, const char *stdin_path,
97-
const char *stdout_path, const char *stderr_path, bool no_stdio,
98-
MachProcess *process, int disable_aslr, DNBError &err);
95+
const char *path, cpu_type_t cpu_type, cpu_subtype_t cpu_subtype,
96+
char const *argv[], char const *envp[], const char *working_directory,
97+
const char *stdin_path, const char *stdout_path, const char *stderr_path,
98+
bool no_stdio, MachProcess *process, int disable_aslr, DNBError &err);
9999
nub_addr_t GetDYLDAllImageInfosAddress();
100100
static const void *PrepareForAttach(const char *path,
101101
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
@@ -3160,9 +3160,9 @@ static bool FBSAddEventDataToOptions(NSMutableDictionary *options,
31603160

31613161
case eLaunchFlavorPosixSpawn:
31623162
m_pid = MachProcess::PosixSpawnChildForPTraceDebugging(
3163-
path, DNBArchProtocol::GetArchitecture(), argv, envp, working_directory,
3164-
stdin_path, stdout_path, stderr_path, no_stdio, this, disable_aslr,
3165-
launch_err);
3163+
path, DNBArchProtocol::GetCPUType(), DNBArchProtocol::GetCPUSubType(),
3164+
argv, envp, working_directory, stdin_path, stdout_path, stderr_path,
3165+
no_stdio, this, disable_aslr, launch_err);
31663166
break;
31673167

31683168
default:
@@ -3222,10 +3222,10 @@ static bool FBSAddEventDataToOptions(NSMutableDictionary *options,
32223222
}
32233223

32243224
pid_t MachProcess::PosixSpawnChildForPTraceDebugging(
3225-
const char *path, cpu_type_t cpu_type, char const *argv[],
3226-
char const *envp[], const char *working_directory, const char *stdin_path,
3227-
const char *stdout_path, const char *stderr_path, bool no_stdio,
3228-
MachProcess *process, int disable_aslr, DNBError &err) {
3225+
const char *path, cpu_type_t cpu_type, cpu_subtype_t cpu_subtype,
3226+
char const *argv[], char const *envp[], const char *working_directory,
3227+
const char *stdin_path, const char *stdout_path, const char *stderr_path,
3228+
bool no_stdio, MachProcess *process, int disable_aslr, DNBError &err) {
32293229
posix_spawnattr_t attr;
32303230
short flags;
32313231
DNBLogThreadedIf(LOG_PROCESS,
@@ -3268,24 +3268,40 @@ static bool FBSAddEventDataToOptions(NSMutableDictionary *options,
32683268

32693269
// On SnowLeopard we should set "DYLD_NO_PIE" in the inferior environment....
32703270

3271-
#if !defined(__arm__)
3272-
3273-
// We don't need to do this for ARM, and we really shouldn't now that we
3274-
// have multiple CPU subtypes and no posix_spawnattr call that allows us
3275-
// to set which CPU subtype to launch...
32763271
if (cpu_type != 0) {
32773272
size_t ocount = 0;
3278-
err.SetError(::posix_spawnattr_setbinpref_np(&attr, 1, &cpu_type, &ocount),
3279-
DNBError::POSIX);
3280-
if (err.Fail() || DNBLogCheckLogBit(LOG_PROCESS))
3281-
err.LogThreaded("::posix_spawnattr_setbinpref_np ( &attr, 1, cpu_type = "
3282-
"0x%8.8x, count => %llu )",
3283-
cpu_type, (uint64_t)ocount);
3273+
bool slice_preference_set = false;
3274+
3275+
if (cpu_subtype != 0) {
3276+
if (@available(macOS 10.16, ios 10.14, watchos 7.0, tvos 14.0,
3277+
bridgeos 5.0, *)) {
3278+
err.SetError(posix_spawnattr_setarchpref_np(&attr, 1, &cpu_type,
3279+
&cpu_subtype, &ocount));
3280+
slice_preference_set = err.Success();
3281+
if (err.Fail() || DNBLogCheckLogBit(LOG_PROCESS))
3282+
err.LogThreaded(
3283+
"::posix_spawnattr_setarchpref_np ( &attr, 1, cpu_type = "
3284+
"0x%8.8x, cpu_subtype = 0x%8.8x, count => %llu )",
3285+
cpu_type, cpu_subtype, (uint64_t)ocount);
3286+
if (err.Fail() != 0 || ocount != 1)
3287+
return INVALID_NUB_PROCESS;
3288+
}
3289+
}
32843290

3285-
if (err.Fail() != 0 || ocount != 1)
3286-
return INVALID_NUB_PROCESS;
3291+
if (!slice_preference_set) {
3292+
err.SetError(
3293+
::posix_spawnattr_setbinpref_np(&attr, 1, &cpu_type, &ocount),
3294+
DNBError::POSIX);
3295+
if (err.Fail() || DNBLogCheckLogBit(LOG_PROCESS))
3296+
err.LogThreaded(
3297+
"::posix_spawnattr_setbinpref_np ( &attr, 1, cpu_type = "
3298+
"0x%8.8x, count => %llu )",
3299+
cpu_type, (uint64_t)ocount);
3300+
3301+
if (err.Fail() != 0 || ocount != 1)
3302+
return INVALID_NUB_PROCESS;
3303+
}
32873304
}
3288-
#endif
32893305

32903306
PseudoTerminal pty;
32913307

0 commit comments

Comments
 (0)