1 //===-- SingleStepCheck.cpp ----------------------------------- -*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "SingleStepCheck.h"
10 
11 #include <sched.h>
12 #include <signal.h>
13 #include <sys/wait.h>
14 #include <unistd.h>
15 
16 #include "NativeProcessLinux.h"
17 
18 #include "llvm/Support/Compiler.h"
19 
20 #include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
21 #include "lldb/Host/linux/Ptrace.h"
22 #include "lldb/Utility/Status.h"
23 
24 using namespace lldb;
25 using namespace lldb_private;
26 using namespace lldb_private::process_linux;
27 
28 #if defined(__arm64__) || defined(__aarch64__)
29 namespace {
30 
31 void LLVM_ATTRIBUTE_NORETURN Child() {
32   if (ptrace(PTRACE_TRACEME, 0, nullptr, nullptr) == -1)
33     _exit(1);
34 
35   // We just do an endless loop SIGSTOPPING ourselves until killed. The tracer
36   // will fiddle with our cpu affinities and monitor the behaviour.
37   for (;;) {
38     raise(SIGSTOP);
39 
40     // Generate a bunch of instructions here, so that a single-step does not
41     // land in the raise() accidentally. If single-stepping works, we will be
42     // spinning in this loop. If it doesn't, we'll land in the raise() call
43     // above.
44     for (volatile unsigned i = 0; i < CPU_SETSIZE; ++i)
45       ;
46   }
47 }
48 
49 struct ChildDeleter {
50   ::pid_t pid;
51 
52   ~ChildDeleter() {
53     int status;
54     // Kill the child.
55     kill(pid, SIGKILL);
56     // Pick up the remains.
57     llvm::sys::RetryAfterSignal(-1, waitpid, pid, &status, __WALL);
58   }
59 };
60 
61 bool WorkaroundNeeded() {
62   // We shall spawn a child, and use it to verify the debug capabilities of the
63   // cpu. We shall iterate through the cpus, bind the child to each one in
64   // turn, and verify that single-stepping works on that cpu. A workaround is
65   // needed if we find at least one broken cpu.
66 
67   Log *log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD);
68   ::pid_t child_pid = fork();
69   if (child_pid == -1) {
70     LLDB_LOG(log, "failed to fork(): {0}", Status(errno, eErrorTypePOSIX));
71     return false;
72   }
73   if (child_pid == 0)
74     Child();
75 
76   ChildDeleter child_deleter{child_pid};
77   cpu_set_t available_cpus;
78   if (sched_getaffinity(child_pid, sizeof available_cpus, &available_cpus) ==
79       -1) {
80     LLDB_LOG(log, "failed to get available cpus: {0}",
81              Status(errno, eErrorTypePOSIX));
82     return false;
83   }
84 
85   int status;
86   ::pid_t wpid = llvm::sys::RetryAfterSignal(-1, waitpid,
87       child_pid, &status, __WALL);
88   if (wpid != child_pid || !WIFSTOPPED(status)) {
89     LLDB_LOG(log, "waitpid() failed (status = {0:x}): {1}", status,
90              Status(errno, eErrorTypePOSIX));
91     return false;
92   }
93 
94   unsigned cpu;
95   for (cpu = 0; cpu < CPU_SETSIZE; ++cpu) {
96     if (!CPU_ISSET(cpu, &available_cpus))
97       continue;
98 
99     cpu_set_t cpus;
100     CPU_ZERO(&cpus);
101     CPU_SET(cpu, &cpus);
102     if (sched_setaffinity(child_pid, sizeof cpus, &cpus) == -1) {
103       LLDB_LOG(log, "failed to switch to cpu {0}: {1}", cpu,
104                Status(errno, eErrorTypePOSIX));
105       continue;
106     }
107 
108     int status;
109     Status error =
110         NativeProcessLinux::PtraceWrapper(PTRACE_SINGLESTEP, child_pid);
111     if (error.Fail()) {
112       LLDB_LOG(log, "single step failed: {0}", error);
113       break;
114     }
115 
116     wpid = llvm::sys::RetryAfterSignal(-1, waitpid,
117         child_pid, &status, __WALL);
118     if (wpid != child_pid || !WIFSTOPPED(status)) {
119       LLDB_LOG(log, "waitpid() failed (status = {0:x}): {1}", status,
120                Status(errno, eErrorTypePOSIX));
121       break;
122     }
123     if (WSTOPSIG(status) != SIGTRAP) {
124       LLDB_LOG(log, "single stepping on cpu {0} failed with status {1:x}", cpu,
125                status);
126       break;
127     }
128   }
129 
130   // cpu is either the index of the first broken cpu, or CPU_SETSIZE.
131   if (cpu == 0) {
132     LLDB_LOG(log,
133              "SINGLE STEPPING ON FIRST CPU IS NOT WORKING. DEBUGGING "
134              "LIKELY TO BE UNRELIABLE.");
135     // No point in trying to fiddle with the affinities, just give it our best
136     // shot and see how it goes.
137     return false;
138   }
139 
140   return cpu != CPU_SETSIZE;
141 }
142 
143 } // end anonymous namespace
144 
145 std::unique_ptr<SingleStepWorkaround> SingleStepWorkaround::Get(::pid_t tid) {
146   Log *log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD);
147 
148   static bool workaround_needed = WorkaroundNeeded();
149   if (!workaround_needed) {
150     LLDB_LOG(log, "workaround for thread {0} not needed", tid);
151     return nullptr;
152   }
153 
154   cpu_set_t original_set;
155   if (sched_getaffinity(tid, sizeof original_set, &original_set) != 0) {
156     // This should really not fail. But, just in case...
157     LLDB_LOG(log, "Unable to get cpu affinity for thread {0}: {1}", tid,
158              Status(errno, eErrorTypePOSIX));
159     return nullptr;
160   }
161 
162   cpu_set_t set;
163   CPU_ZERO(&set);
164   CPU_SET(0, &set);
165   if (sched_setaffinity(tid, sizeof set, &set) != 0) {
166     // This may fail in very locked down systems, if the thread is not allowed
167     // to run on cpu 0. If that happens, only thing we can do is it log it and
168     // continue...
169     LLDB_LOG(log, "Unable to set cpu affinity for thread {0}: {1}", tid,
170              Status(errno, eErrorTypePOSIX));
171   }
172 
173   LLDB_LOG(log, "workaround for thread {0} prepared", tid);
174   return llvm::make_unique<SingleStepWorkaround>(tid, original_set);
175 }
176 
177 SingleStepWorkaround::~SingleStepWorkaround() {
178   Log *log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD);
179   LLDB_LOG(log, "Removing workaround");
180   if (sched_setaffinity(m_tid, sizeof m_original_set, &m_original_set) != 0) {
181     LLDB_LOG(log, "Unable to reset cpu affinity for thread {0}: {1}", m_tid,
182              Status(errno, eErrorTypePOSIX));
183   }
184 }
185 #endif
186