JDevlieghere updated this revision to Diff 321005.
JDevlieghere added a comment.

Add test


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D95922/new/

https://reviews.llvm.org/D95922

Files:
  lldb/source/Host/macosx/objcxx/Host.mm
  lldb/test/API/macosx/posix_spawn/Makefile
  lldb/test/API/macosx/posix_spawn/TestLaunchProcessPosixSpawn.py
  lldb/test/API/macosx/posix_spawn/arm64.c
  lldb/test/API/macosx/posix_spawn/x86_64.c
  lldb/test/API/macosx/posix_spawn/x86_64h.c

Index: lldb/test/API/macosx/posix_spawn/x86_64h.c
===================================================================
--- /dev/null
+++ lldb/test/API/macosx/posix_spawn/x86_64h.c
@@ -0,0 +1,5 @@
+#include <stdio.h>
+int main() {
+  printf("slice: x86_64h\n");
+  return 0;
+}
Index: lldb/test/API/macosx/posix_spawn/x86_64.c
===================================================================
--- /dev/null
+++ lldb/test/API/macosx/posix_spawn/x86_64.c
@@ -0,0 +1,5 @@
+#include <stdio.h>
+int main() {
+  printf("slice: x86_64\n");
+  return 0;
+}
Index: lldb/test/API/macosx/posix_spawn/arm64.c
===================================================================
--- /dev/null
+++ lldb/test/API/macosx/posix_spawn/arm64.c
@@ -0,0 +1,5 @@
+#include <stdio.h>
+int main() {
+  printf("slice: arm64\n");
+  return 0;
+}
Index: lldb/test/API/macosx/posix_spawn/TestLaunchProcessPosixSpawn.py
===================================================================
--- /dev/null
+++ lldb/test/API/macosx/posix_spawn/TestLaunchProcessPosixSpawn.py
@@ -0,0 +1,65 @@
+import unittest2
+import os
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+
+
+def haswell():
+    features = subprocess.check_output(["sysctl", "machdep.cpu"])
+    return "AVX2" in features.decode('utf-8')
+
+
+def apple_silicon():
+    features = subprocess.check_output(["sysctl", "machdep.cpu"])
+    return "Apple processor" in features.decode('utf-8')
+
+
+class TestLaunchProcessPosixSpawn(TestBase):
+    NO_DEBUG_INFO_TESTCASE = True
+    mydir = TestBase.compute_mydir(__file__)
+
+    def no_haswell(self):
+        if not haswell():
+            return "Current CPU is not Haswell"
+        return None
+
+    def no_apple_silicon(self):
+        if not apple_silicon():
+            return "Current CPU is not Apple Silicon"
+        return None
+
+    @skipUnlessDarwin
+    @skipIfDarwinEmbedded
+    @skipTestIfFn(no_haswell)
+    def test_haswell(self):
+        self.build()
+        exe = self.getBuildArtifact("fat.out")
+
+        self.runCmd('target create -arch x86_64 {}'.format(exe))
+        self.runCmd("run")
+        stdout = self.dbg.GetSelectedTarget().process.GetSTDOUT(1000)
+        self.assertIn('slice: x86_64', stdout)
+
+        self.runCmd('target create -arch x86_64h {}'.format(exe))
+        self.runCmd("run")
+        stdout = self.dbg.GetSelectedTarget().process.GetSTDOUT(1000)
+        self.assertIn('slice: x86_64h', stdout)
+
+    @skipUnlessDarwin
+    @skipIfDarwinEmbedded
+    @skipTestIfFn(no_apple_silicon)
+    def test_apple_silicon(self):
+        self.build()
+        exe = self.getBuildArtifact("fat.out")
+
+        self.runCmd('target create -arch x86_64 {}'.format(exe))
+        self.runCmd("run")
+        stdout = self.dbg.GetSelectedTarget().process.GetSTDOUT(1000)
+        self.assertIn('slice: x86_64', stdout)
+
+        self.runCmd('target create -arch arm64 {}'.format(exe))
+        self.runCmd("run")
+        stdout = self.dbg.GetSelectedTarget().process.GetSTDOUT(1000)
+        self.assertIn('slice: arm64', stdout)
Index: lldb/test/API/macosx/posix_spawn/Makefile
===================================================================
--- /dev/null
+++ lldb/test/API/macosx/posix_spawn/Makefile
@@ -0,0 +1,18 @@
+EXE := fat.out
+LIPO=lipo
+
+include Makefile.rules
+
+all: fat.out
+
+x86_64.out: x86_64.c
+	$(CC) -isysroot $(SDKROOT) -target x86_64-apple-macosx11 -o x86_64.out $<
+
+x86_64h.out: x86_64h.c
+	$(CC) -isysroot $(SDKROOT) -target x86_64h-apple-macosx11 -o x86_64.out $<
+
+arm64.out: arm64.c
+	$(CC) -isysroot $(SDKROOT) -target arm64-apple-macosx11 -o arm64.out $<
+
+fat.out: x86_64.out x86_64h.out arm64.out
+	$(LIPO) -o fat.out -create $<
Index: lldb/source/Host/macosx/objcxx/Host.mm
===================================================================
--- lldb/source/Host/macosx/objcxx/Host.mm
+++ lldb/source/Host/macosx/objcxx/Host.mm
@@ -38,6 +38,7 @@
 
 #include <asl.h>
 #include <crt_externs.h>
+#include <dlfcn.h>
 #include <grp.h>
 #include <libproc.h>
 #include <pwd.h>
@@ -1108,6 +1109,49 @@
     }
   }
 
+  // Don't set the binpref if a shell was provided. After all, that's only
+  // going to affect what version of the shell is launched, not what fork of
+  // the binary is launched.  We insert "arch --arch <ARCH> as part of the
+  // shell invocation to do that job on OSX.
+  if (launch_info.GetShell() == FileSpec()) {
+    const ArchSpec &arch_spec = launch_info.GetArchitecture();
+    cpu_type_t cpu_type = arch_spec.GetMachOCPUType();
+    cpu_type_t cpu_subtype = arch_spec.GetMachOCPUSubType();
+    if (cpu_type != 0 && cpu_type != static_cast<cpu_type_t>(UINT32_MAX) &&
+        cpu_type != static_cast<cpu_type_t>(LLDB_INVALID_CPUTYPE)) {
+      size_t ocount = 0;
+      typedef int (*posix_spawnattr_setarchpref_np_t)(
+          posix_spawnattr_t *, size_t, cpu_type_t *, cpu_subtype_t *, size_t *);
+      posix_spawnattr_setarchpref_np_t posix_spawnattr_setarchpref_np_fn =
+          (posix_spawnattr_setarchpref_np_t)dlsym(
+              RTLD_DEFAULT, "posix_spawnattr_setarchpref_np");
+      if (posix_spawnattr_setarchpref_np_fn) {
+        error.SetError((*posix_spawnattr_setarchpref_np_fn)(
+                           &attr, 1, &cpu_type, &cpu_subtype, &ocount),
+                       eErrorTypePOSIX);
+        if (error.Fail())
+          LLDB_LOG(log,
+                   "error: {0}, ::posix_spawnattr_setarchpref_np ( &attr, 1, "
+                   "cpu_type = {1:x}, cpu_subtype = {1:x}, count => {2} )",
+                   error, cpu_type, cpu_subtype, ocount);
+
+        if (error.Fail() || ocount != 1)
+          return error;
+      } else {
+        error.SetError(
+            ::posix_spawnattr_setbinpref_np(&attr, 1, &cpu_type, &ocount),
+            eErrorTypePOSIX);
+        if (error.Fail())
+          LLDB_LOG(log,
+                   "error: {0}, ::posix_spawnattr_setbinpref_np ( &attr, 1, "
+                   "cpu_type = {1:x}, count => {2} )",
+                   error, cpu_type, ocount);
+        if (error.Fail() || ocount != 1)
+          return error;
+      }
+    }
+  }
+
   const char *tmp_argv[2];
   char *const *argv = const_cast<char *const *>(
       launch_info.GetArguments().GetConstArgumentVector());
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to