180814287SRaphael Isemann //===-- SingleStepCheck.cpp -----------------------------------------------===//
2605b51b8SPavel Labath //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6605b51b8SPavel Labath //
7605b51b8SPavel Labath //===----------------------------------------------------------------------===//
8605b51b8SPavel Labath 
9605b51b8SPavel Labath #include "SingleStepCheck.h"
10605b51b8SPavel Labath 
1176e47d48SRaphael Isemann #include <csignal>
12605b51b8SPavel Labath #include <sched.h>
13605b51b8SPavel Labath #include <sys/wait.h>
14605b51b8SPavel Labath #include <unistd.h>
15605b51b8SPavel Labath 
16605b51b8SPavel Labath #include "NativeProcessLinux.h"
17605b51b8SPavel Labath 
18605b51b8SPavel Labath #include "llvm/Support/Compiler.h"
197603bd52SAlex Langford #include "llvm/Support/Errno.h"
20605b51b8SPavel Labath 
218abd34f0SPavel Labath #include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
22605b51b8SPavel Labath #include "lldb/Host/linux/Ptrace.h"
2397206d57SZachary Turner #include "lldb/Utility/Status.h"
24605b51b8SPavel Labath 
258abd34f0SPavel Labath using namespace lldb;
268abd34f0SPavel Labath using namespace lldb_private;
27605b51b8SPavel Labath using namespace lldb_private::process_linux;
28605b51b8SPavel Labath 
29605b51b8SPavel Labath #if defined(__arm64__) || defined(__aarch64__)
30b9c1b51eSKate Stone namespace {
31605b51b8SPavel Labath 
Child()3272a83674SFangrui Song [[noreturn]] void Child() {
33605b51b8SPavel Labath   if (ptrace(PTRACE_TRACEME, 0, nullptr, nullptr) == -1)
34605b51b8SPavel Labath     _exit(1);
35605b51b8SPavel Labath 
36b9c1b51eSKate Stone   // We just do an endless loop SIGSTOPPING ourselves until killed. The tracer
378abd34f0SPavel Labath   // will fiddle with our cpu affinities and monitor the behaviour.
38b9c1b51eSKate Stone   for (;;) {
39605b51b8SPavel Labath     raise(SIGSTOP);
40605b51b8SPavel Labath 
41b9c1b51eSKate Stone     // Generate a bunch of instructions here, so that a single-step does not
428abd34f0SPavel Labath     // land in the raise() accidentally. If single-stepping works, we will be
438abd34f0SPavel Labath     // spinning in this loop. If it doesn't, we'll land in the raise() call
448abd34f0SPavel Labath     // above.
45605b51b8SPavel Labath     for (volatile unsigned i = 0; i < CPU_SETSIZE; ++i)
46605b51b8SPavel Labath       ;
47605b51b8SPavel Labath   }
48605b51b8SPavel Labath }
49605b51b8SPavel Labath 
50b9c1b51eSKate Stone struct ChildDeleter {
51605b51b8SPavel Labath   ::pid_t pid;
52605b51b8SPavel Labath 
~ChildDeleter__anone5e65eb70111::ChildDeleter53b9c1b51eSKate Stone   ~ChildDeleter() {
54605b51b8SPavel Labath     int status;
552819136fSMichal Gorny     // Kill the child.
562819136fSMichal Gorny     kill(pid, SIGKILL);
572819136fSMichal Gorny     // Pick up the remains.
582819136fSMichal Gorny     llvm::sys::RetryAfterSignal(-1, waitpid, pid, &status, __WALL);
59605b51b8SPavel Labath   }
60605b51b8SPavel Labath };
61605b51b8SPavel Labath 
WorkaroundNeeded()628abd34f0SPavel Labath bool WorkaroundNeeded() {
63b9c1b51eSKate Stone   // We shall spawn a child, and use it to verify the debug capabilities of the
6405097246SAdrian Prantl   // cpu. We shall iterate through the cpus, bind the child to each one in
6505097246SAdrian Prantl   // turn, and verify that single-stepping works on that cpu. A workaround is
6605097246SAdrian Prantl   // needed if we find at least one broken cpu.
67605b51b8SPavel Labath 
68*4fa1ad05SPavel Labath   Log *log = GetLog(POSIXLog::Thread);
69605b51b8SPavel Labath   ::pid_t child_pid = fork();
70b9c1b51eSKate Stone   if (child_pid == -1) {
7141c99364SZachary Turner     LLDB_LOG(log, "failed to fork(): {0}", Status(errno, eErrorTypePOSIX));
72605b51b8SPavel Labath     return false;
73605b51b8SPavel Labath   }
74605b51b8SPavel Labath   if (child_pid == 0)
75605b51b8SPavel Labath     Child();
76605b51b8SPavel Labath 
77605b51b8SPavel Labath   ChildDeleter child_deleter{child_pid};
78605b51b8SPavel Labath   cpu_set_t available_cpus;
79b9c1b51eSKate Stone   if (sched_getaffinity(child_pid, sizeof available_cpus, &available_cpus) ==
80b9c1b51eSKate Stone       -1) {
818abd34f0SPavel Labath     LLDB_LOG(log, "failed to get available cpus: {0}",
8297206d57SZachary Turner              Status(errno, eErrorTypePOSIX));
83605b51b8SPavel Labath     return false;
84605b51b8SPavel Labath   }
85605b51b8SPavel Labath 
86605b51b8SPavel Labath   int status;
872819136fSMichal Gorny   ::pid_t wpid = llvm::sys::RetryAfterSignal(-1, waitpid,
882819136fSMichal Gorny       child_pid, &status, __WALL);
89b9c1b51eSKate Stone   if (wpid != child_pid || !WIFSTOPPED(status)) {
908abd34f0SPavel Labath     LLDB_LOG(log, "waitpid() failed (status = {0:x}): {1}", status,
9197206d57SZachary Turner              Status(errno, eErrorTypePOSIX));
92605b51b8SPavel Labath     return false;
93605b51b8SPavel Labath   }
94605b51b8SPavel Labath 
95605b51b8SPavel Labath   unsigned cpu;
96b9c1b51eSKate Stone   for (cpu = 0; cpu < CPU_SETSIZE; ++cpu) {
97605b51b8SPavel Labath     if (!CPU_ISSET(cpu, &available_cpus))
98605b51b8SPavel Labath       continue;
99605b51b8SPavel Labath 
100605b51b8SPavel Labath     cpu_set_t cpus;
101605b51b8SPavel Labath     CPU_ZERO(&cpus);
102605b51b8SPavel Labath     CPU_SET(cpu, &cpus);
103b9c1b51eSKate Stone     if (sched_setaffinity(child_pid, sizeof cpus, &cpus) == -1) {
1048abd34f0SPavel Labath       LLDB_LOG(log, "failed to switch to cpu {0}: {1}", cpu,
10597206d57SZachary Turner                Status(errno, eErrorTypePOSIX));
106605b51b8SPavel Labath       continue;
107605b51b8SPavel Labath     }
108605b51b8SPavel Labath 
109605b51b8SPavel Labath     int status;
11097206d57SZachary Turner     Status error =
1118abd34f0SPavel Labath         NativeProcessLinux::PtraceWrapper(PTRACE_SINGLESTEP, child_pid);
112b9c1b51eSKate Stone     if (error.Fail()) {
1138abd34f0SPavel Labath       LLDB_LOG(log, "single step failed: {0}", error);
114605b51b8SPavel Labath       break;
115605b51b8SPavel Labath     }
116605b51b8SPavel Labath 
1172819136fSMichal Gorny     wpid = llvm::sys::RetryAfterSignal(-1, waitpid,
1182819136fSMichal Gorny         child_pid, &status, __WALL);
119b9c1b51eSKate Stone     if (wpid != child_pid || !WIFSTOPPED(status)) {
1208abd34f0SPavel Labath       LLDB_LOG(log, "waitpid() failed (status = {0:x}): {1}", status,
12197206d57SZachary Turner                Status(errno, eErrorTypePOSIX));
122605b51b8SPavel Labath       break;
123605b51b8SPavel Labath     }
124b9c1b51eSKate Stone     if (WSTOPSIG(status) != SIGTRAP) {
1258abd34f0SPavel Labath       LLDB_LOG(log, "single stepping on cpu {0} failed with status {1:x}", cpu,
1268abd34f0SPavel Labath                status);
127605b51b8SPavel Labath       break;
128605b51b8SPavel Labath     }
129605b51b8SPavel Labath   }
130605b51b8SPavel Labath 
131605b51b8SPavel Labath   // cpu is either the index of the first broken cpu, or CPU_SETSIZE.
132b9c1b51eSKate Stone   if (cpu == 0) {
1338abd34f0SPavel Labath     LLDB_LOG(log,
1348abd34f0SPavel Labath              "SINGLE STEPPING ON FIRST CPU IS NOT WORKING. DEBUGGING "
1358abd34f0SPavel Labath              "LIKELY TO BE UNRELIABLE.");
136b9c1b51eSKate Stone     // No point in trying to fiddle with the affinities, just give it our best
137b9c1b51eSKate Stone     // shot and see how it goes.
138605b51b8SPavel Labath     return false;
139605b51b8SPavel Labath   }
140605b51b8SPavel Labath 
141605b51b8SPavel Labath   return cpu != CPU_SETSIZE;
142605b51b8SPavel Labath }
143605b51b8SPavel Labath 
1448abd34f0SPavel Labath } // end anonymous namespace
1458abd34f0SPavel Labath 
Get(::pid_t tid)1467278496cSPavel Labath std::unique_ptr<SingleStepWorkaround> SingleStepWorkaround::Get(::pid_t tid) {
147*4fa1ad05SPavel Labath   Log *log = GetLog(POSIXLog::Thread);
1488abd34f0SPavel Labath 
1498abd34f0SPavel Labath   static bool workaround_needed = WorkaroundNeeded();
1508abd34f0SPavel Labath   if (!workaround_needed) {
1518abd34f0SPavel Labath     LLDB_LOG(log, "workaround for thread {0} not needed", tid);
1527278496cSPavel Labath     return nullptr;
1538abd34f0SPavel Labath   }
1548abd34f0SPavel Labath 
1558abd34f0SPavel Labath   cpu_set_t original_set;
1568abd34f0SPavel Labath   if (sched_getaffinity(tid, sizeof original_set, &original_set) != 0) {
1578abd34f0SPavel Labath     // This should really not fail. But, just in case...
1588abd34f0SPavel Labath     LLDB_LOG(log, "Unable to get cpu affinity for thread {0}: {1}", tid,
15997206d57SZachary Turner              Status(errno, eErrorTypePOSIX));
1607278496cSPavel Labath     return nullptr;
1618abd34f0SPavel Labath   }
1628abd34f0SPavel Labath 
1638abd34f0SPavel Labath   cpu_set_t set;
1648abd34f0SPavel Labath   CPU_ZERO(&set);
1658abd34f0SPavel Labath   CPU_SET(0, &set);
1668abd34f0SPavel Labath   if (sched_setaffinity(tid, sizeof set, &set) != 0) {
1678abd34f0SPavel Labath     // This may fail in very locked down systems, if the thread is not allowed
1688abd34f0SPavel Labath     // to run on cpu 0. If that happens, only thing we can do is it log it and
1698abd34f0SPavel Labath     // continue...
1708abd34f0SPavel Labath     LLDB_LOG(log, "Unable to set cpu affinity for thread {0}: {1}", tid,
17197206d57SZachary Turner              Status(errno, eErrorTypePOSIX));
1728abd34f0SPavel Labath   }
1738abd34f0SPavel Labath 
1748abd34f0SPavel Labath   LLDB_LOG(log, "workaround for thread {0} prepared", tid);
175a8f3ae7cSJonas Devlieghere   return std::make_unique<SingleStepWorkaround>(tid, original_set);
1768abd34f0SPavel Labath }
1778abd34f0SPavel Labath 
~SingleStepWorkaround()1788abd34f0SPavel Labath SingleStepWorkaround::~SingleStepWorkaround() {
179*4fa1ad05SPavel Labath   Log *log = GetLog(POSIXLog::Thread);
180a37bbbd4SPavel Labath   LLDB_LOG(log, "Removing workaround");
1818abd34f0SPavel Labath   if (sched_setaffinity(m_tid, sizeof m_original_set, &m_original_set) != 0) {
1828abd34f0SPavel Labath     LLDB_LOG(log, "Unable to reset cpu affinity for thread {0}: {1}", m_tid,
18397206d57SZachary Turner              Status(errno, eErrorTypePOSIX));
1848abd34f0SPavel Labath   }
1858abd34f0SPavel Labath }
186605b51b8SPavel Labath #endif
187