Author: Jason Molenda
Date: 2021-05-12T22:18:24-07:00
New Revision: 0c443e92d3b9bc5a25214f2c8418b72501a15a00

URL: 
https://github.com/llvm/llvm-project/commit/0c443e92d3b9bc5a25214f2c8418b72501a15a00
DIFF: 
https://github.com/llvm/llvm-project/commit/0c443e92d3b9bc5a25214f2c8418b72501a15a00.diff

LOG: Add some warnings when debugserver is running in translation

A debugserver launched x86_64 cannot control an arm64/arm64e
process on an Apple Silicon system.  Warn when this situation
has happened and return an error for the most common case of
attach.  I think there will be refinements to this in the
future, but start out by making it easy to spot the problem
when it happens.

rdar://76630595

Added: 
    

Modified: 
    lldb/tools/debugserver/source/DNB.cpp
    lldb/tools/debugserver/source/DNB.h
    lldb/tools/debugserver/source/RNBRemote.cpp
    lldb/tools/debugserver/source/debugserver.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/tools/debugserver/source/DNB.cpp 
b/lldb/tools/debugserver/source/DNB.cpp
index 7048bec4832a..37c8cf5e6542 100644
--- a/lldb/tools/debugserver/source/DNB.cpp
+++ b/lldb/tools/debugserver/source/DNB.cpp
@@ -1812,3 +1812,11 @@ nub_bool_t DNBSetArchitecture(const char *arch) {
   }
   return false;
 }
+
+bool DNBDebugserverIsTranslated() {
+  int ret = 0;
+  size_t size = sizeof(ret);
+  if (sysctlbyname("sysctl.proc_translated", &ret, &size, NULL, 0) == -1)
+    return false;
+  return ret == 1;
+}

diff  --git a/lldb/tools/debugserver/source/DNB.h 
b/lldb/tools/debugserver/source/DNB.h
index 069c62dc41d8..8763b0cae697 100644
--- a/lldb/tools/debugserver/source/DNB.h
+++ b/lldb/tools/debugserver/source/DNB.h
@@ -239,4 +239,8 @@ nub_bool_t DNBResolveExecutablePath(const char *path, char 
*resolved_path,
 bool DNBGetOSVersionNumbers(uint64_t *major, uint64_t *minor, uint64_t *patch);
 /// \return the iOSSupportVersion of the host OS.
 std::string DNBGetMacCatalystVersionString();
+
+/// \return true if debugserver is running in translation
+/// (is an x86_64 process on arm64)
+bool DNBDebugserverIsTranslated();
 #endif

diff  --git a/lldb/tools/debugserver/source/RNBRemote.cpp 
b/lldb/tools/debugserver/source/RNBRemote.cpp
index 586336a21b6b..876681aa5d6e 100644
--- a/lldb/tools/debugserver/source/RNBRemote.cpp
+++ b/lldb/tools/debugserver/source/RNBRemote.cpp
@@ -3920,6 +3920,17 @@ rnb_err_t RNBRemote::HandlePacket_v(const char *p) {
     char err_str[1024] = {'\0'};
     std::string attach_name;
 
+    if (DNBDebugserverIsTranslated()) {
+      DNBLogError("debugserver is x86_64 binary running in translation, attach 
"
+                  "failed.");
+      std::string return_message = "E96;";
+      return_message +=
+          cstring_to_asciihex_string("debugserver is x86_64 binary running in "
+                                     "translation, attached failed.");
+      SendPacket(return_message.c_str());
+      return rnb_err;
+    }
+
     if (strstr(p, "vAttachWait;") == p) {
       p += strlen("vAttachWait;");
       if (!GetProcessNameFrom_vAttach(p, attach_name)) {

diff  --git a/lldb/tools/debugserver/source/debugserver.cpp 
b/lldb/tools/debugserver/source/debugserver.cpp
index d76f4f00aff8..a0a30473254a 100644
--- a/lldb/tools/debugserver/source/debugserver.cpp
+++ b/lldb/tools/debugserver/source/debugserver.cpp
@@ -809,8 +809,11 @@ void FileLogCallback(void *baton, uint32_t flags, const 
char *format,
 }
 
 void show_version_and_exit(int exit_code) {
-  printf("%s-%s for %s.\n", DEBUGSERVER_PROGRAM_NAME, DEBUGSERVER_VERSION_STR,
-         RNB_ARCH);
+  const char *in_translation = "";
+  if (DNBDebugserverIsTranslated())
+    in_translation = " (running under translation)";
+  printf("%s-%s for %s%s.\n", DEBUGSERVER_PROGRAM_NAME, 
DEBUGSERVER_VERSION_STR,
+         RNB_ARCH, in_translation);
   exit(exit_code);
 }
 
@@ -1353,8 +1356,12 @@ int main(int argc, char *argv[]) {
   // as long as we're dropping remotenub in as a replacement for gdbserver,
   // explicitly note that this is not gdbserver.
 
-  RNBLogSTDOUT("%s-%s %sfor %s.\n", DEBUGSERVER_PROGRAM_NAME,
-               DEBUGSERVER_VERSION_STR, compile_options.c_str(), RNB_ARCH);
+  const char *in_translation = "";
+  if (DNBDebugserverIsTranslated())
+    in_translation = " (running under translation)";
+  RNBLogSTDOUT("%s-%s %sfor %s%s.\n", DEBUGSERVER_PROGRAM_NAME,
+               DEBUGSERVER_VERSION_STR, compile_options.c_str(), RNB_ARCH,
+               in_translation);
 
   std::string host;
   int port = INT32_MAX;


        
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to