1605b51b8SPavel Labath //===-- SingleStepCheck.cpp ----------------------------------- -*- C++ -*-===//
2605b51b8SPavel Labath //
3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler 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;
54605b51b8SPavel Labath     kill(pid, SIGKILL);            // Kill the child.
55605b51b8SPavel Labath     waitpid(pid, &status, __WALL); // Pick up the remains.
56605b51b8SPavel Labath   }
57605b51b8SPavel Labath };
58605b51b8SPavel Labath 
598abd34f0SPavel Labath bool WorkaroundNeeded() {
60b9c1b51eSKate Stone   // We shall spawn a child, and use it to verify the debug capabilities of the
6105097246SAdrian Prantl   // cpu. We shall iterate through the cpus, bind the child to each one in
6205097246SAdrian Prantl   // turn, and verify that single-stepping works on that cpu. A workaround is
6305097246SAdrian Prantl   // needed if we find at least one broken cpu.
64605b51b8SPavel Labath 
658abd34f0SPavel Labath   Log *log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD);
66605b51b8SPavel Labath   ::pid_t child_pid = fork();
67b9c1b51eSKate Stone   if (child_pid == -1) {
6841c99364SZachary Turner     LLDB_LOG(log, "failed to fork(): {0}", Status(errno, eErrorTypePOSIX));
69605b51b8SPavel Labath     return false;
70605b51b8SPavel Labath   }
71605b51b8SPavel Labath   if (child_pid == 0)
72605b51b8SPavel Labath     Child();
73605b51b8SPavel Labath 
74605b51b8SPavel Labath   ChildDeleter child_deleter{child_pid};
75605b51b8SPavel Labath   cpu_set_t available_cpus;
76b9c1b51eSKate Stone   if (sched_getaffinity(child_pid, sizeof available_cpus, &available_cpus) ==
77b9c1b51eSKate Stone       -1) {
788abd34f0SPavel Labath     LLDB_LOG(log, "failed to get available cpus: {0}",
7997206d57SZachary Turner              Status(errno, eErrorTypePOSIX));
80605b51b8SPavel Labath     return false;
81605b51b8SPavel Labath   }
82605b51b8SPavel Labath 
83605b51b8SPavel Labath   int status;
84605b51b8SPavel Labath   ::pid_t wpid = waitpid(child_pid, &status, __WALL);
85b9c1b51eSKate Stone   if (wpid != child_pid || !WIFSTOPPED(status)) {
868abd34f0SPavel Labath     LLDB_LOG(log, "waitpid() failed (status = {0:x}): {1}", status,
8797206d57SZachary Turner              Status(errno, eErrorTypePOSIX));
88605b51b8SPavel Labath     return false;
89605b51b8SPavel Labath   }
90605b51b8SPavel Labath 
91605b51b8SPavel Labath   unsigned cpu;
92b9c1b51eSKate Stone   for (cpu = 0; cpu < CPU_SETSIZE; ++cpu) {
93605b51b8SPavel Labath     if (!CPU_ISSET(cpu, &available_cpus))
94605b51b8SPavel Labath       continue;
95605b51b8SPavel Labath 
96605b51b8SPavel Labath     cpu_set_t cpus;
97605b51b8SPavel Labath     CPU_ZERO(&cpus);
98605b51b8SPavel Labath     CPU_SET(cpu, &cpus);
99b9c1b51eSKate Stone     if (sched_setaffinity(child_pid, sizeof cpus, &cpus) == -1) {
1008abd34f0SPavel Labath       LLDB_LOG(log, "failed to switch to cpu {0}: {1}", cpu,
10197206d57SZachary Turner                Status(errno, eErrorTypePOSIX));
102605b51b8SPavel Labath       continue;
103605b51b8SPavel Labath     }
104605b51b8SPavel Labath 
105605b51b8SPavel Labath     int status;
10697206d57SZachary Turner     Status error =
1078abd34f0SPavel Labath         NativeProcessLinux::PtraceWrapper(PTRACE_SINGLESTEP, child_pid);
108b9c1b51eSKate Stone     if (error.Fail()) {
1098abd34f0SPavel Labath       LLDB_LOG(log, "single step failed: {0}", error);
110605b51b8SPavel Labath       break;
111605b51b8SPavel Labath     }
112605b51b8SPavel Labath 
113605b51b8SPavel Labath     wpid = waitpid(child_pid, &status, __WALL);
114b9c1b51eSKate Stone     if (wpid != child_pid || !WIFSTOPPED(status)) {
1158abd34f0SPavel Labath       LLDB_LOG(log, "waitpid() failed (status = {0:x}): {1}", status,
11697206d57SZachary Turner                Status(errno, eErrorTypePOSIX));
117605b51b8SPavel Labath       break;
118605b51b8SPavel Labath     }
119b9c1b51eSKate Stone     if (WSTOPSIG(status) != SIGTRAP) {
1208abd34f0SPavel Labath       LLDB_LOG(log, "single stepping on cpu {0} failed with status {1:x}", cpu,
1218abd34f0SPavel Labath                status);
122605b51b8SPavel Labath       break;
123605b51b8SPavel Labath     }
124605b51b8SPavel Labath   }
125605b51b8SPavel Labath 
126605b51b8SPavel Labath   // cpu is either the index of the first broken cpu, or CPU_SETSIZE.
127b9c1b51eSKate Stone   if (cpu == 0) {
1288abd34f0SPavel Labath     LLDB_LOG(log,
1298abd34f0SPavel Labath              "SINGLE STEPPING ON FIRST CPU IS NOT WORKING. DEBUGGING "
1308abd34f0SPavel Labath              "LIKELY TO BE UNRELIABLE.");
131b9c1b51eSKate Stone     // No point in trying to fiddle with the affinities, just give it our best
132b9c1b51eSKate Stone     // shot and see how it goes.
133605b51b8SPavel Labath     return false;
134605b51b8SPavel Labath   }
135605b51b8SPavel Labath 
136605b51b8SPavel Labath   return cpu != CPU_SETSIZE;
137605b51b8SPavel Labath }
138605b51b8SPavel Labath 
1398abd34f0SPavel Labath } // end anonymous namespace
1408abd34f0SPavel Labath 
1417278496cSPavel Labath std::unique_ptr<SingleStepWorkaround> SingleStepWorkaround::Get(::pid_t tid) {
1428abd34f0SPavel Labath   Log *log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD);
1438abd34f0SPavel Labath 
1448abd34f0SPavel Labath   static bool workaround_needed = WorkaroundNeeded();
1458abd34f0SPavel Labath   if (!workaround_needed) {
1468abd34f0SPavel Labath     LLDB_LOG(log, "workaround for thread {0} not needed", tid);
1477278496cSPavel Labath     return nullptr;
1488abd34f0SPavel Labath   }
1498abd34f0SPavel Labath 
1508abd34f0SPavel Labath   cpu_set_t original_set;
1518abd34f0SPavel Labath   if (sched_getaffinity(tid, sizeof original_set, &original_set) != 0) {
1528abd34f0SPavel Labath     // This should really not fail. But, just in case...
1538abd34f0SPavel Labath     LLDB_LOG(log, "Unable to get cpu affinity for thread {0}: {1}", tid,
15497206d57SZachary Turner              Status(errno, eErrorTypePOSIX));
1557278496cSPavel Labath     return nullptr;
1568abd34f0SPavel Labath   }
1578abd34f0SPavel Labath 
1588abd34f0SPavel Labath   cpu_set_t set;
1598abd34f0SPavel Labath   CPU_ZERO(&set);
1608abd34f0SPavel Labath   CPU_SET(0, &set);
1618abd34f0SPavel Labath   if (sched_setaffinity(tid, sizeof set, &set) != 0) {
1628abd34f0SPavel Labath     // This may fail in very locked down systems, if the thread is not allowed
1638abd34f0SPavel Labath     // to run on cpu 0. If that happens, only thing we can do is it log it and
1648abd34f0SPavel Labath     // continue...
1658abd34f0SPavel Labath     LLDB_LOG(log, "Unable to set cpu affinity for thread {0}: {1}", tid,
16697206d57SZachary Turner              Status(errno, eErrorTypePOSIX));
1678abd34f0SPavel Labath   }
1688abd34f0SPavel Labath 
1698abd34f0SPavel Labath   LLDB_LOG(log, "workaround for thread {0} prepared", tid);
1707278496cSPavel Labath   return llvm::make_unique<SingleStepWorkaround>(tid, original_set);
1718abd34f0SPavel Labath }
1728abd34f0SPavel Labath 
1738abd34f0SPavel Labath SingleStepWorkaround::~SingleStepWorkaround() {
174a37bbbd4SPavel Labath   Log *log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD);
175a37bbbd4SPavel Labath   LLDB_LOG(log, "Removing workaround");
1768abd34f0SPavel Labath   if (sched_setaffinity(m_tid, sizeof m_original_set, &m_original_set) != 0) {
1778abd34f0SPavel Labath     LLDB_LOG(log, "Unable to reset cpu affinity for thread {0}: {1}", m_tid,
17897206d57SZachary Turner              Status(errno, eErrorTypePOSIX));
1798abd34f0SPavel Labath   }
1808abd34f0SPavel Labath }
181605b51b8SPavel Labath #endif
182