JDevlieghere created this revision.
JDevlieghere added a reviewer: jasonmolenda.
Herald added a subscriber: pengfei.
JDevlieghere requested review of this revision.

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`. In the patch it only does this for `x86_64` 
and `x86_64h`, but I would like to extend the parsing to more subtypes.

Tested by compiling a fat binary with different print statements depending on 
the architecture. I considered adding a test for it, but it doesn't seem worth 
the hassle to figure out the host architecture and the availability.


https://reviews.llvm.org/D92712

Files:
  lldb/tools/debugserver/source/DNB.cpp
  lldb/tools/debugserver/source/DNBArch.cpp
  lldb/tools/debugserver/source/DNBArch.h
  lldb/tools/debugserver/source/MacOSX/MachProcess.h
  lldb/tools/debugserver/source/MacOSX/MachProcess.mm

Index: lldb/tools/debugserver/source/MacOSX/MachProcess.mm
===================================================================
--- lldb/tools/debugserver/source/MacOSX/MachProcess.mm
+++ lldb/tools/debugserver/source/MacOSX/MachProcess.mm
@@ -3160,9 +3160,9 @@
 
   case eLaunchFlavorPosixSpawn:
     m_pid = MachProcess::PosixSpawnChildForPTraceDebugging(
-        path, DNBArchProtocol::GetArchitecture(), argv, envp, working_directory,
-        stdin_path, stdout_path, stderr_path, no_stdio, this, disable_aslr,
-        launch_err);
+        path, DNBArchProtocol::GetCPUType(), DNBArchProtocol::GetCPUSubType(),
+        argv, envp, working_directory, stdin_path, stdout_path, stderr_path,
+        no_stdio, this, disable_aslr, launch_err);
     break;
 
   default:
@@ -3222,10 +3222,10 @@
 }
 
 pid_t MachProcess::PosixSpawnChildForPTraceDebugging(
-    const char *path, cpu_type_t cpu_type, char const *argv[],
-    char const *envp[], const char *working_directory, const char *stdin_path,
-    const char *stdout_path, const char *stderr_path, bool no_stdio,
-    MachProcess *process, int disable_aslr, DNBError &err) {
+    const char *path, cpu_type_t cpu_type, cpu_subtype_t cpu_subtype,
+    char const *argv[], char const *envp[], const char *working_directory,
+    const char *stdin_path, const char *stdout_path, const char *stderr_path,
+    bool no_stdio, MachProcess *process, int disable_aslr, DNBError &err) {
   posix_spawnattr_t attr;
   short flags;
   DNBLogThreadedIf(LOG_PROCESS,
@@ -3268,24 +3268,36 @@
 
 // On SnowLeopard we should set "DYLD_NO_PIE" in the inferior environment....
 
-#if !defined(__arm__)
-
-  // We don't need to do this for ARM, and we really shouldn't now that we
-  // have multiple CPU subtypes and no posix_spawnattr call that allows us
-  // to set which CPU subtype to launch...
   if (cpu_type != 0) {
     size_t ocount = 0;
-    err.SetError(::posix_spawnattr_setbinpref_np(&attr, 1, &cpu_type, &ocount),
-                 DNBError::POSIX);
-    if (err.Fail() || DNBLogCheckLogBit(LOG_PROCESS))
-      err.LogThreaded("::posix_spawnattr_setbinpref_np ( &attr, 1, cpu_type = "
-                      "0x%8.8x, count => %llu )",
-                      cpu_type, (uint64_t)ocount);
 
-    if (err.Fail() != 0 || ocount != 1)
-      return INVALID_NUB_PROCESS;
+    if (cpu_subtype != 0) {
+      if (@available(macOS 10.16, ios 10.14, watchos 7.0, tvos 14.0,
+                     bridgeos 5.0, *)) {
+        err.SetError(posix_spawnattr_setarchpref_np(&attr, 1, &cpu_type,
+                                                    &cpu_subtype, &ocount));
+        if (err.Fail() || DNBLogCheckLogBit(LOG_PROCESS))
+          err.LogThreaded(
+              "::posix_spawnattr_setarchpref_np ( &attr, 1, cpu_type = "
+              "0x%8.8x, cpu_subtype = 0x%8.8x, count => %llu )",
+              cpu_type, cpu_subtype, (uint64_t)ocount);
+        if (err.Fail() != 0 || ocount != 1)
+          return INVALID_NUB_PROCESS;
+      }
+    } else {
+      err.SetError(
+          ::posix_spawnattr_setbinpref_np(&attr, 1, &cpu_type, &ocount),
+          DNBError::POSIX);
+      if (err.Fail() || DNBLogCheckLogBit(LOG_PROCESS))
+        err.LogThreaded(
+            "::posix_spawnattr_setbinpref_np ( &attr, 1, cpu_type = "
+            "0x%8.8x, count => %llu )",
+            cpu_type, (uint64_t)ocount);
+
+      if (err.Fail() != 0 || ocount != 1)
+        return INVALID_NUB_PROCESS;
+    }
   }
-#endif
 
   PseudoTerminal pty;
 
Index: lldb/tools/debugserver/source/MacOSX/MachProcess.h
===================================================================
--- lldb/tools/debugserver/source/MacOSX/MachProcess.h
+++ lldb/tools/debugserver/source/MacOSX/MachProcess.h
@@ -92,10 +92,10 @@
                                            char const *envp[],
                                            MachProcess *process, DNBError &err);
   static pid_t PosixSpawnChildForPTraceDebugging(
-      const char *path, cpu_type_t cpu_type, char const *argv[],
-      char const *envp[], const char *working_directory, const char *stdin_path,
-      const char *stdout_path, const char *stderr_path, bool no_stdio,
-      MachProcess *process, int disable_aslr, DNBError &err);
+      const char *path, cpu_type_t cpu_type, cpu_subtype_t cpu_subtype,
+      char const *argv[], char const *envp[], const char *working_directory,
+      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();
   static const void *PrepareForAttach(const char *path,
                                       nub_launch_flavor_t launch_flavor,
Index: lldb/tools/debugserver/source/DNBArch.h
===================================================================
--- lldb/tools/debugserver/source/DNBArch.h
+++ lldb/tools/debugserver/source/DNBArch.h
@@ -49,9 +49,10 @@
 
   static void RegisterArchPlugin(const DNBArchPluginInfo &arch_info);
 
-  static uint32_t GetArchitecture();
+  static uint32_t GetCPUType();
+  static uint32_t GetCPUSubType();
 
-  static bool SetArchitecture(uint32_t cpu_type);
+  static bool SetArchitecture(uint32_t cpu_type, uint32_t cpu_subtype = 0);
 
   DNBArchProtocol() : m_save_id(0) {}
 
Index: lldb/tools/debugserver/source/DNBArch.cpp
===================================================================
--- lldb/tools/debugserver/source/DNBArch.cpp
+++ lldb/tools/debugserver/source/DNBArch.cpp
@@ -21,6 +21,7 @@
 typedef std::map<uint32_t, DNBArchPluginInfo> CPUPluginInfoMap;
 
 static uint32_t g_current_cpu_type = 0;
+static uint32_t g_current_cpu_subtype = 0;
 CPUPluginInfoMap g_arch_plugins;
 
 static const DNBArchPluginInfo *GetArchInfo() {
@@ -31,15 +32,17 @@
   return NULL;
 }
 
-uint32_t DNBArchProtocol::GetArchitecture() { return g_current_cpu_type; }
+uint32_t DNBArchProtocol::GetCPUType() { return g_current_cpu_type; }
+uint32_t DNBArchProtocol::GetCPUSubType() { return g_current_cpu_subtype; }
 
-bool DNBArchProtocol::SetArchitecture(uint32_t cpu_type) {
+bool DNBArchProtocol::SetArchitecture(uint32_t cpu_type, uint32_t cpu_subtype) {
   g_current_cpu_type = cpu_type;
+  g_current_cpu_subtype = cpu_subtype;
   bool result = g_arch_plugins.find(g_current_cpu_type) != g_arch_plugins.end();
-  DNBLogThreadedIf(
-      LOG_PROCESS,
-      "DNBArchProtocol::SetDefaultArchitecture (cpu_type=0x%8.8x) => %i",
-      cpu_type, result);
+  DNBLogThreadedIf(LOG_PROCESS,
+                   "DNBArchProtocol::SetDefaultArchitecture (cpu_type=0x%8.8x, "
+                   "cpu_subtype=0x%8.8x) => %i",
+                   cpu_type, cpu_subtype, result);
   return result;
 }
 
Index: lldb/tools/debugserver/source/DNB.cpp
===================================================================
--- lldb/tools/debugserver/source/DNB.cpp
+++ lldb/tools/debugserver/source/DNB.cpp
@@ -1762,9 +1762,12 @@
   if (arch && arch[0]) {
     if (strcasecmp(arch, "i386") == 0)
       return DNBArchProtocol::SetArchitecture(CPU_TYPE_I386);
-    else if ((strcasecmp(arch, "x86_64") == 0) ||
-             (strcasecmp(arch, "x86_64h") == 0))
-      return DNBArchProtocol::SetArchitecture(CPU_TYPE_X86_64);
+    else if (strcasecmp(arch, "x86_64") == 0)
+      return DNBArchProtocol::SetArchitecture(CPU_TYPE_X86_64,
+                                              CPU_SUBTYPE_X86_64_ALL);
+    else if (strcasecmp(arch, "x86_64h") == 0)
+      return DNBArchProtocol::SetArchitecture(CPU_TYPE_X86_64,
+                                              CPU_SUBTYPE_X86_64_H);
     else if (strstr(arch, "arm64_32") == arch || 
              strstr(arch, "aarch64_32") == arch)
       return DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM64_32);
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to