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