[Lldb-commits] [PATCH] D84263: [debugserver/Apple Silicon] Handoff connections when attaching to translated processes

Davide Italiano via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Thu Jul 30 16:07:30 PDT 2020


davide updated this revision to Diff 282073.
davide added a comment.

Added the check that Jason requested.


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

https://reviews.llvm.org/D84263

Files:
  lldb/tools/debugserver/source/DNB.cpp
  lldb/tools/debugserver/source/debugserver.cpp


Index: lldb/tools/debugserver/source/debugserver.cpp
===================================================================
--- lldb/tools/debugserver/source/debugserver.cpp
+++ lldb/tools/debugserver/source/debugserver.cpp
@@ -878,6 +878,8 @@
            // -F localhost:1234 -- /bin/ls"
     {NULL, 0, NULL, 0}};
 
+int communication_fd = -1;
+
 // main
 int main(int argc, char *argv[]) {
   // If debugserver is launched with DYLD_INSERT_LIBRARIES, unset it so we
@@ -944,7 +946,6 @@
   int ch;
   int long_option_index = 0;
   int debug = 0;
-  int communication_fd = -1;
   std::string compile_options;
   std::string waitfor_pid_name; // Wait for a process that starts with this name
   std::string attach_pid_name;
Index: lldb/tools/debugserver/source/DNB.cpp
===================================================================
--- lldb/tools/debugserver/source/DNB.cpp
+++ lldb/tools/debugserver/source/DNB.cpp
@@ -442,6 +442,39 @@
   if (err_str && err_len > 0)
     err_str[0] = '\0';
 
+  if (getenv("LLDB_DEBUGSERVER_PATH") == NULL) {
+    int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID,
+                 static_cast<int>(attach_pid)};
+    struct kinfo_proc processInfo;
+    size_t bufsize = sizeof(processInfo);
+    if (sysctl(mib, (unsigned)(sizeof(mib) / sizeof(int)), &processInfo,
+               &bufsize, NULL, 0) == 0 &&
+        bufsize > 0) {
+
+      if ((processInfo.kp_proc.p_flag & P_TRANSLATED) == P_TRANSLATED) {
+        const char *translated_debugserver =
+            "/Library/Apple/usr/libexec/oah/debugserver";
+        char fdstr[16];
+        char pidstr[16];
+        extern int communication_fd;
+
+        if (communication_fd == -1) {
+          fprintf(stderr, "Trying to attach to a translated process with the "
+                          "native debugserver, exiting...\n");
+          exit(1);
+        }
+
+        snprintf(fdstr, sizeof(fdstr), "--fd=%d", communication_fd);
+        snprintf(pidstr, sizeof(pidstr), "--attach=%d", attach_pid);
+        execl(translated_debugserver, "--native-regs", "--setsid", fdstr,
+              "--handoff-attach-from-native", pidstr, (char *)0);
+        DNBLogThreadedIf(LOG_PROCESS, "Failed to launch debugserver for "
+                         "translated process: ", errno, strerror(errno));
+        __builtin_trap();
+      }
+    }
+  }
+
   pid_t pid = INVALID_NUB_PROCESS;
   MachProcessSP processSP(new MachProcess);
   if (processSP.get()) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D84263.282073.patch
Type: text/x-patch
Size: 2445 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20200730/7bc7abf0/attachment.bin>


More information about the lldb-commits mailing list