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(pid, SIGKILL);            // Kill the child.
55     waitpid(pid, &status, __WALL); // Pick up the remains.
56   }
57 };
58 
59 bool WorkaroundNeeded() {
60   // We shall spawn a child, and use it to verify the debug capabilities of the
61   // cpu. We shall iterate through the cpus, bind the child to each one in
62   // turn, and verify that single-stepping works on that cpu. A workaround is
63   // needed if we find at least one broken cpu.
64 
65   Log *log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD);
66   ::pid_t child_pid = fork();
67   if (child_pid == -1) {
68     LLDB_LOG(log, "failed to fork(): {0}", Status(errno, eErrorTypePOSIX));
69     return false;
70   }
71   if (child_pid == 0)
72     Child();
73 
74   ChildDeleter child_deleter{child_pid};
75   cpu_set_t available_cpus;
76   if (sched_getaffinity(child_pid, sizeof available_cpus, &available_cpus) ==
77       -1) {
78     LLDB_LOG(log, "failed to get available cpus: {0}",
79              Status(errno, eErrorTypePOSIX));
80     return false;
81   }
82 
83   int status;
84   ::pid_t wpid = waitpid(child_pid, &status, __WALL);
85   if (wpid != child_pid || !WIFSTOPPED(status)) {
86     LLDB_LOG(log, "waitpid() failed (status = {0:x}): {1}", status,
87              Status(errno, eErrorTypePOSIX));
88     return false;
89   }
90 
91   unsigned cpu;
92   for (cpu = 0; cpu < CPU_SETSIZE; ++cpu) {
93     if (!CPU_ISSET(cpu, &available_cpus))
94       continue;
95 
96     cpu_set_t cpus;
97     CPU_ZERO(&cpus);
98     CPU_SET(cpu, &cpus);
99     if (sched_setaffinity(child_pid, sizeof cpus, &cpus) == -1) {
100       LLDB_LOG(log, "failed to switch to cpu {0}: {1}", cpu,
101                Status(errno, eErrorTypePOSIX));
102       continue;
103     }
104 
105     int status;
106     Status error =
107         NativeProcessLinux::PtraceWrapper(PTRACE_SINGLESTEP, child_pid);
108     if (error.Fail()) {
109       LLDB_LOG(log, "single step failed: {0}", error);
110       break;
111     }
112 
113     wpid = waitpid(child_pid, &status, __WALL);
114     if (wpid != child_pid || !WIFSTOPPED(status)) {
115       LLDB_LOG(log, "waitpid() failed (status = {0:x}): {1}", status,
116                Status(errno, eErrorTypePOSIX));
117       break;
118     }
119     if (WSTOPSIG(status) != SIGTRAP) {
120       LLDB_LOG(log, "single stepping on cpu {0} failed with status {1:x}", cpu,
121                status);
122       break;
123     }
124   }
125 
126   // cpu is either the index of the first broken cpu, or CPU_SETSIZE.
127   if (cpu == 0) {
128     LLDB_LOG(log,
129              "SINGLE STEPPING ON FIRST CPU IS NOT WORKING. DEBUGGING "
130              "LIKELY TO BE UNRELIABLE.");
131     // No point in trying to fiddle with the affinities, just give it our best
132     // shot and see how it goes.
133     return false;
134   }
135 
136   return cpu != CPU_SETSIZE;
137 }
138 
139 } // end anonymous namespace
140 
141 std::unique_ptr<SingleStepWorkaround> SingleStepWorkaround::Get(::pid_t tid) {
142   Log *log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD);
143 
144   static bool workaround_needed = WorkaroundNeeded();
145   if (!workaround_needed) {
146     LLDB_LOG(log, "workaround for thread {0} not needed", tid);
147     return nullptr;
148   }
149 
150   cpu_set_t original_set;
151   if (sched_getaffinity(tid, sizeof original_set, &original_set) != 0) {
152     // This should really not fail. But, just in case...
153     LLDB_LOG(log, "Unable to get cpu affinity for thread {0}: {1}", tid,
154              Status(errno, eErrorTypePOSIX));
155     return nullptr;
156   }
157 
158   cpu_set_t set;
159   CPU_ZERO(&set);
160   CPU_SET(0, &set);
161   if (sched_setaffinity(tid, sizeof set, &set) != 0) {
162     // This may fail in very locked down systems, if the thread is not allowed
163     // to run on cpu 0. If that happens, only thing we can do is it log it and
164     // continue...
165     LLDB_LOG(log, "Unable to set cpu affinity for thread {0}: {1}", tid,
166              Status(errno, eErrorTypePOSIX));
167   }
168 
169   LLDB_LOG(log, "workaround for thread {0} prepared", tid);
170   return llvm::make_unique<SingleStepWorkaround>(tid, original_set);
171 }
172 
173 SingleStepWorkaround::~SingleStepWorkaround() {
174   Log *log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD);
175   LLDB_LOG(log, "Removing workaround");
176   if (sched_setaffinity(m_tid, sizeof m_original_set, &m_original_set) != 0) {
177     LLDB_LOG(log, "Unable to reset cpu affinity for thread {0}: {1}", m_tid,
178              Status(errno, eErrorTypePOSIX));
179   }
180 }
181 #endif
182