1605b51b8SPavel Labath //===-- SingleStepCheck.cpp ----------------------------------- -*- C++ -*-===//
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 
11605b51b8SPavel Labath #include <sched.h>
12605b51b8SPavel Labath #include <signal.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"
19605b51b8SPavel Labath 
208abd34f0SPavel Labath #include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
21605b51b8SPavel Labath #include "lldb/Host/linux/Ptrace.h"
2297206d57SZachary Turner #include "lldb/Utility/Status.h"
23605b51b8SPavel Labath 
248abd34f0SPavel Labath using namespace lldb;
258abd34f0SPavel Labath using namespace lldb_private;
26605b51b8SPavel Labath using namespace lldb_private::process_linux;
27605b51b8SPavel Labath 
28605b51b8SPavel Labath #if defined(__arm64__) || defined(__aarch64__)
29b9c1b51eSKate Stone namespace {
30605b51b8SPavel Labath 
31b9c1b51eSKate Stone void LLVM_ATTRIBUTE_NORETURN Child() {
32605b51b8SPavel Labath   if (ptrace(PTRACE_TRACEME, 0, nullptr, nullptr) == -1)
33605b51b8SPavel Labath     _exit(1);
34605b51b8SPavel Labath 
35b9c1b51eSKate Stone   // We just do an endless loop SIGSTOPPING ourselves until killed. The tracer
368abd34f0SPavel Labath   // will fiddle with our cpu affinities and monitor the behaviour.
37b9c1b51eSKate Stone   for (;;) {
38605b51b8SPavel Labath     raise(SIGSTOP);
39605b51b8SPavel Labath 
40b9c1b51eSKate Stone     // Generate a bunch of instructions here, so that a single-step does not
418abd34f0SPavel Labath     // land in the raise() accidentally. If single-stepping works, we will be
428abd34f0SPavel Labath     // spinning in this loop. If it doesn't, we'll land in the raise() call
438abd34f0SPavel Labath     // above.
44605b51b8SPavel Labath     for (volatile unsigned i = 0; i < CPU_SETSIZE; ++i)
45605b51b8SPavel Labath       ;
46605b51b8SPavel Labath   }
47605b51b8SPavel Labath }
48605b51b8SPavel Labath 
49b9c1b51eSKate Stone struct ChildDeleter {
50605b51b8SPavel Labath   ::pid_t pid;
51605b51b8SPavel Labath 
52b9c1b51eSKate Stone   ~ChildDeleter() {
53605b51b8SPavel Labath     int status;
54*2819136fSMichal Gorny     // Kill the child.
55*2819136fSMichal Gorny     kill(pid, SIGKILL);
56*2819136fSMichal Gorny     // Pick up the remains.
57*2819136fSMichal Gorny     llvm::sys::RetryAfterSignal(-1, waitpid, pid, &status, __WALL);
58605b51b8SPavel Labath   }
59605b51b8SPavel Labath };
60605b51b8SPavel Labath 
618abd34f0SPavel Labath bool WorkaroundNeeded() {
62b9c1b51eSKate Stone   // We shall spawn a child, and use it to verify the debug capabilities of the
6305097246SAdrian Prantl   // cpu. We shall iterate through the cpus, bind the child to each one in
6405097246SAdrian Prantl   // turn, and verify that single-stepping works on that cpu. A workaround is
6505097246SAdrian Prantl   // needed if we find at least one broken cpu.
66605b51b8SPavel Labath 
678abd34f0SPavel Labath   Log *log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD);
68605b51b8SPavel Labath   ::pid_t child_pid = fork();
69b9c1b51eSKate Stone   if (child_pid == -1) {
7041c99364SZachary Turner     LLDB_LOG(log, "failed to fork(): {0}", Status(errno, eErrorTypePOSIX));
71605b51b8SPavel Labath     return false;
72605b51b8SPavel Labath   }
73605b51b8SPavel Labath   if (child_pid == 0)
74605b51b8SPavel Labath     Child();
75605b51b8SPavel Labath 
76605b51b8SPavel Labath   ChildDeleter child_deleter{child_pid};
77605b51b8SPavel Labath   cpu_set_t available_cpus;
78b9c1b51eSKate Stone   if (sched_getaffinity(child_pid, sizeof available_cpus, &available_cpus) ==
79b9c1b51eSKate Stone       -1) {
808abd34f0SPavel Labath     LLDB_LOG(log, "failed to get available cpus: {0}",
8197206d57SZachary Turner              Status(errno, eErrorTypePOSIX));
82605b51b8SPavel Labath     return false;
83605b51b8SPavel Labath   }
84605b51b8SPavel Labath 
85605b51b8SPavel Labath   int status;
86*2819136fSMichal Gorny   ::pid_t wpid = llvm::sys::RetryAfterSignal(-1, waitpid,
87*2819136fSMichal Gorny       child_pid, &status, __WALL);
88b9c1b51eSKate Stone   if (wpid != child_pid || !WIFSTOPPED(status)) {
898abd34f0SPavel Labath     LLDB_LOG(log, "waitpid() failed (status = {0:x}): {1}", status,
9097206d57SZachary Turner              Status(errno, eErrorTypePOSIX));
91605b51b8SPavel Labath     return false;
92605b51b8SPavel Labath   }
93605b51b8SPavel Labath 
94605b51b8SPavel Labath   unsigned cpu;
95b9c1b51eSKate Stone   for (cpu = 0; cpu < CPU_SETSIZE; ++cpu) {
96605b51b8SPavel Labath     if (!CPU_ISSET(cpu, &available_cpus))
97605b51b8SPavel Labath       continue;
98605b51b8SPavel Labath 
99605b51b8SPavel Labath     cpu_set_t cpus;
100605b51b8SPavel Labath     CPU_ZERO(&cpus);
101605b51b8SPavel Labath     CPU_SET(cpu, &cpus);
102b9c1b51eSKate Stone     if (sched_setaffinity(child_pid, sizeof cpus, &cpus) == -1) {
1038abd34f0SPavel Labath       LLDB_LOG(log, "failed to switch to cpu {0}: {1}", cpu,
10497206d57SZachary Turner                Status(errno, eErrorTypePOSIX));
105605b51b8SPavel Labath       continue;
106605b51b8SPavel Labath     }
107605b51b8SPavel Labath 
108605b51b8SPavel Labath     int status;
10997206d57SZachary Turner     Status error =
1108abd34f0SPavel Labath         NativeProcessLinux::PtraceWrapper(PTRACE_SINGLESTEP, child_pid);
111b9c1b51eSKate Stone     if (error.Fail()) {
1128abd34f0SPavel Labath       LLDB_LOG(log, "single step failed: {0}", error);
113605b51b8SPavel Labath       break;
114605b51b8SPavel Labath     }
115605b51b8SPavel Labath 
116*2819136fSMichal Gorny     wpid = llvm::sys::RetryAfterSignal(-1, waitpid,
117*2819136fSMichal Gorny         child_pid, &status, __WALL);
118b9c1b51eSKate Stone     if (wpid != child_pid || !WIFSTOPPED(status)) {
1198abd34f0SPavel Labath       LLDB_LOG(log, "waitpid() failed (status = {0:x}): {1}", status,
12097206d57SZachary Turner                Status(errno, eErrorTypePOSIX));
121605b51b8SPavel Labath       break;
122605b51b8SPavel Labath     }
123b9c1b51eSKate Stone     if (WSTOPSIG(status) != SIGTRAP) {
1248abd34f0SPavel Labath       LLDB_LOG(log, "single stepping on cpu {0} failed with status {1:x}", cpu,
1258abd34f0SPavel Labath                status);
126605b51b8SPavel Labath       break;
127605b51b8SPavel Labath     }
128605b51b8SPavel Labath   }
129605b51b8SPavel Labath 
130605b51b8SPavel Labath   // cpu is either the index of the first broken cpu, or CPU_SETSIZE.
131b9c1b51eSKate Stone   if (cpu == 0) {
1328abd34f0SPavel Labath     LLDB_LOG(log,
1338abd34f0SPavel Labath              "SINGLE STEPPING ON FIRST CPU IS NOT WORKING. DEBUGGING "
1348abd34f0SPavel Labath              "LIKELY TO BE UNRELIABLE.");
135b9c1b51eSKate Stone     // No point in trying to fiddle with the affinities, just give it our best
136b9c1b51eSKate Stone     // shot and see how it goes.
137605b51b8SPavel Labath     return false;
138605b51b8SPavel Labath   }
139605b51b8SPavel Labath 
140605b51b8SPavel Labath   return cpu != CPU_SETSIZE;
141605b51b8SPavel Labath }
142605b51b8SPavel Labath 
1438abd34f0SPavel Labath } // end anonymous namespace
1448abd34f0SPavel Labath 
1457278496cSPavel Labath std::unique_ptr<SingleStepWorkaround> SingleStepWorkaround::Get(::pid_t tid) {
1468abd34f0SPavel Labath   Log *log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD);
1478abd34f0SPavel Labath 
1488abd34f0SPavel Labath   static bool workaround_needed = WorkaroundNeeded();
1498abd34f0SPavel Labath   if (!workaround_needed) {
1508abd34f0SPavel Labath     LLDB_LOG(log, "workaround for thread {0} not needed", tid);
1517278496cSPavel Labath     return nullptr;
1528abd34f0SPavel Labath   }
1538abd34f0SPavel Labath 
1548abd34f0SPavel Labath   cpu_set_t original_set;
1558abd34f0SPavel Labath   if (sched_getaffinity(tid, sizeof original_set, &original_set) != 0) {
1568abd34f0SPavel Labath     // This should really not fail. But, just in case...
1578abd34f0SPavel Labath     LLDB_LOG(log, "Unable to get cpu affinity for thread {0}: {1}", tid,
15897206d57SZachary Turner              Status(errno, eErrorTypePOSIX));
1597278496cSPavel Labath     return nullptr;
1608abd34f0SPavel Labath   }
1618abd34f0SPavel Labath 
1628abd34f0SPavel Labath   cpu_set_t set;
1638abd34f0SPavel Labath   CPU_ZERO(&set);
1648abd34f0SPavel Labath   CPU_SET(0, &set);
1658abd34f0SPavel Labath   if (sched_setaffinity(tid, sizeof set, &set) != 0) {
1668abd34f0SPavel Labath     // This may fail in very locked down systems, if the thread is not allowed
1678abd34f0SPavel Labath     // to run on cpu 0. If that happens, only thing we can do is it log it and
1688abd34f0SPavel Labath     // continue...
1698abd34f0SPavel Labath     LLDB_LOG(log, "Unable to set cpu affinity for thread {0}: {1}", tid,
17097206d57SZachary Turner              Status(errno, eErrorTypePOSIX));
1718abd34f0SPavel Labath   }
1728abd34f0SPavel Labath 
1738abd34f0SPavel Labath   LLDB_LOG(log, "workaround for thread {0} prepared", tid);
1747278496cSPavel Labath   return llvm::make_unique<SingleStepWorkaround>(tid, original_set);
1758abd34f0SPavel Labath }
1768abd34f0SPavel Labath 
1778abd34f0SPavel Labath SingleStepWorkaround::~SingleStepWorkaround() {
178a37bbbd4SPavel Labath   Log *log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD);
179a37bbbd4SPavel Labath   LLDB_LOG(log, "Removing workaround");
1808abd34f0SPavel Labath   if (sched_setaffinity(m_tid, sizeof m_original_set, &m_original_set) != 0) {
1818abd34f0SPavel Labath     LLDB_LOG(log, "Unable to reset cpu affinity for thread {0}: {1}", m_tid,
18297206d57SZachary Turner              Status(errno, eErrorTypePOSIX));
1838abd34f0SPavel Labath   }
1848abd34f0SPavel Labath }
185605b51b8SPavel Labath #endif
186