Author: Davide Italiano Date: 2020-07-30T16:21:05-07:00 New Revision: 57605758b5de3726eec1d6e587de1003af1ab5b7
URL: https://github.com/llvm/llvm-project/commit/57605758b5de3726eec1d6e587de1003af1ab5b7 DIFF: https://github.com/llvm/llvm-project/commit/57605758b5de3726eec1d6e587de1003af1ab5b7.diff LOG: [debugserver/Apple Silicon] Handoff connections when attaching to translated processes When we detect a process that the native debugserver cannot handle, handoff the connection fd to the translated debugserver. Added: Modified: lldb/tools/debugserver/source/DNB.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 0830ea36a91a..3c1cd85dc310 100644 --- a/lldb/tools/debugserver/source/DNB.cpp +++ b/lldb/tools/debugserver/source/DNB.cpp @@ -442,6 +442,39 @@ nub_process_t DNBProcessAttach(nub_process_t attach_pid, 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()) { diff --git a/lldb/tools/debugserver/source/debugserver.cpp b/lldb/tools/debugserver/source/debugserver.cpp index 42205dedf4bb..410ea7c310fa 100644 --- a/lldb/tools/debugserver/source/debugserver.cpp +++ b/lldb/tools/debugserver/source/debugserver.cpp @@ -878,6 +878,8 @@ static struct option g_long_options[] = { // -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 main(int argc, char *argv[]) { 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; _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits