1605b51b8SPavel Labath //===-- SingleStepCheck.cpp ----------------------------------- -*- C++ -*-===//
2605b51b8SPavel Labath //
3605b51b8SPavel Labath //                     The LLVM Compiler Infrastructure
4605b51b8SPavel Labath //
5605b51b8SPavel Labath // This file is distributed under the University of Illinois Open Source
6605b51b8SPavel Labath // License. See LICENSE.TXT for details.
7605b51b8SPavel Labath //
8605b51b8SPavel Labath //===----------------------------------------------------------------------===//
9605b51b8SPavel Labath 
10605b51b8SPavel Labath #include "SingleStepCheck.h"
11605b51b8SPavel Labath 
12605b51b8SPavel Labath #include <sched.h>
13605b51b8SPavel Labath #include <signal.h>
14605b51b8SPavel Labath #include <sys/wait.h>
15605b51b8SPavel Labath #include <unistd.h>
16605b51b8SPavel Labath 
17605b51b8SPavel Labath #include "NativeProcessLinux.h"
18605b51b8SPavel Labath 
19605b51b8SPavel Labath #include "llvm/Support/Compiler.h"
20605b51b8SPavel Labath 
218abd34f0SPavel Labath #include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
22605b51b8SPavel Labath #include "lldb/Host/linux/Ptrace.h"
23bf9a7730SZachary Turner #include "lldb/Utility/Error.h"
24605b51b8SPavel Labath 
258abd34f0SPavel Labath using namespace lldb;
268abd34f0SPavel Labath using namespace lldb_private;
27605b51b8SPavel Labath using namespace lldb_private::process_linux;
28605b51b8SPavel Labath 
29605b51b8SPavel Labath #if defined(__arm64__) || defined(__aarch64__)
30b9c1b51eSKate Stone namespace {
31605b51b8SPavel Labath 
32b9c1b51eSKate Stone void LLVM_ATTRIBUTE_NORETURN Child() {
33605b51b8SPavel Labath   if (ptrace(PTRACE_TRACEME, 0, nullptr, nullptr) == -1)
34605b51b8SPavel Labath     _exit(1);
35605b51b8SPavel Labath 
36b9c1b51eSKate Stone   // We just do an endless loop SIGSTOPPING ourselves until killed. The tracer
378abd34f0SPavel Labath   // will fiddle with our cpu affinities and monitor the behaviour.
38b9c1b51eSKate Stone   for (;;) {
39605b51b8SPavel Labath     raise(SIGSTOP);
40605b51b8SPavel Labath 
41b9c1b51eSKate Stone     // Generate a bunch of instructions here, so that a single-step does not
428abd34f0SPavel Labath     // land in the raise() accidentally. If single-stepping works, we will be
438abd34f0SPavel Labath     // spinning in this loop. If it doesn't, we'll land in the raise() call
448abd34f0SPavel Labath     // above.
45605b51b8SPavel Labath     for (volatile unsigned i = 0; i < CPU_SETSIZE; ++i)
46605b51b8SPavel Labath       ;
47605b51b8SPavel Labath   }
48605b51b8SPavel Labath }
49605b51b8SPavel Labath 
50b9c1b51eSKate Stone struct ChildDeleter {
51605b51b8SPavel Labath   ::pid_t pid;
52605b51b8SPavel Labath 
53b9c1b51eSKate Stone   ~ChildDeleter() {
54605b51b8SPavel Labath     int status;
55605b51b8SPavel Labath     kill(pid, SIGKILL);            // Kill the child.
56605b51b8SPavel Labath     waitpid(pid, &status, __WALL); // Pick up the remains.
57605b51b8SPavel Labath   }
58605b51b8SPavel Labath };
59605b51b8SPavel Labath 
608abd34f0SPavel Labath bool WorkaroundNeeded() {
61b9c1b51eSKate Stone   // We shall spawn a child, and use it to verify the debug capabilities of the
628abd34f0SPavel Labath   // cpu. We shall iterate through the cpus, bind the child to each one in turn,
638abd34f0SPavel Labath   // and verify that single-stepping works on that cpu. A workaround is needed
648abd34f0SPavel Labath   // if we find at least one broken cpu.
65605b51b8SPavel Labath 
668abd34f0SPavel Labath   Log *log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD);
67605b51b8SPavel Labath   ::pid_t child_pid = fork();
68b9c1b51eSKate Stone   if (child_pid == -1) {
698abd34f0SPavel Labath     LLDB_LOG(log, "failed to fork(): {0}", Error(errno, eErrorTypePOSIX));
70605b51b8SPavel Labath     return false;
71605b51b8SPavel Labath   }
72605b51b8SPavel Labath   if (child_pid == 0)
73605b51b8SPavel Labath     Child();
74605b51b8SPavel Labath 
75605b51b8SPavel Labath   ChildDeleter child_deleter{child_pid};
76605b51b8SPavel Labath   cpu_set_t available_cpus;
77b9c1b51eSKate Stone   if (sched_getaffinity(child_pid, sizeof available_cpus, &available_cpus) ==
78b9c1b51eSKate Stone       -1) {
798abd34f0SPavel Labath     LLDB_LOG(log, "failed to get available cpus: {0}",
808abd34f0SPavel Labath              Error(errno, eErrorTypePOSIX));
81605b51b8SPavel Labath     return false;
82605b51b8SPavel Labath   }
83605b51b8SPavel Labath 
84605b51b8SPavel Labath   int status;
85605b51b8SPavel Labath   ::pid_t wpid = waitpid(child_pid, &status, __WALL);
86b9c1b51eSKate Stone   if (wpid != child_pid || !WIFSTOPPED(status)) {
878abd34f0SPavel Labath     LLDB_LOG(log, "waitpid() failed (status = {0:x}): {1}", status,
888abd34f0SPavel Labath              Error(errno, eErrorTypePOSIX));
89605b51b8SPavel Labath     return false;
90605b51b8SPavel Labath   }
91605b51b8SPavel Labath 
92605b51b8SPavel Labath   unsigned cpu;
93b9c1b51eSKate Stone   for (cpu = 0; cpu < CPU_SETSIZE; ++cpu) {
94605b51b8SPavel Labath     if (!CPU_ISSET(cpu, &available_cpus))
95605b51b8SPavel Labath       continue;
96605b51b8SPavel Labath 
97605b51b8SPavel Labath     cpu_set_t cpus;
98605b51b8SPavel Labath     CPU_ZERO(&cpus);
99605b51b8SPavel Labath     CPU_SET(cpu, &cpus);
100b9c1b51eSKate Stone     if (sched_setaffinity(child_pid, sizeof cpus, &cpus) == -1) {
1018abd34f0SPavel Labath       LLDB_LOG(log, "failed to switch to cpu {0}: {1}", cpu,
1028abd34f0SPavel Labath                Error(errno, eErrorTypePOSIX));
103605b51b8SPavel Labath       continue;
104605b51b8SPavel Labath     }
105605b51b8SPavel Labath 
106605b51b8SPavel Labath     int status;
1078abd34f0SPavel Labath     Error error =
1088abd34f0SPavel Labath         NativeProcessLinux::PtraceWrapper(PTRACE_SINGLESTEP, child_pid);
109b9c1b51eSKate Stone     if (error.Fail()) {
1108abd34f0SPavel Labath       LLDB_LOG(log, "single step failed: {0}", error);
111605b51b8SPavel Labath       break;
112605b51b8SPavel Labath     }
113605b51b8SPavel Labath 
114605b51b8SPavel Labath     wpid = waitpid(child_pid, &status, __WALL);
115b9c1b51eSKate Stone     if (wpid != child_pid || !WIFSTOPPED(status)) {
1168abd34f0SPavel Labath       LLDB_LOG(log, "waitpid() failed (status = {0:x}): {1}", status,
1178abd34f0SPavel Labath                Error(errno, eErrorTypePOSIX));
118605b51b8SPavel Labath       break;
119605b51b8SPavel Labath     }
120b9c1b51eSKate Stone     if (WSTOPSIG(status) != SIGTRAP) {
1218abd34f0SPavel Labath       LLDB_LOG(log, "single stepping on cpu {0} failed with status {1:x}", cpu,
1228abd34f0SPavel Labath                status);
123605b51b8SPavel Labath       break;
124605b51b8SPavel Labath     }
125605b51b8SPavel Labath   }
126605b51b8SPavel Labath 
127605b51b8SPavel Labath   // cpu is either the index of the first broken cpu, or CPU_SETSIZE.
128b9c1b51eSKate Stone   if (cpu == 0) {
1298abd34f0SPavel Labath     LLDB_LOG(log,
1308abd34f0SPavel Labath              "SINGLE STEPPING ON FIRST CPU IS NOT WORKING. DEBUGGING "
1318abd34f0SPavel Labath              "LIKELY TO BE UNRELIABLE.");
132b9c1b51eSKate Stone     // No point in trying to fiddle with the affinities, just give it our best
133b9c1b51eSKate Stone     // shot and see how it goes.
134605b51b8SPavel Labath     return false;
135605b51b8SPavel Labath   }
136605b51b8SPavel Labath 
137605b51b8SPavel Labath   return cpu != CPU_SETSIZE;
138605b51b8SPavel Labath }
139605b51b8SPavel Labath 
1408abd34f0SPavel Labath } // end anonymous namespace
1418abd34f0SPavel Labath 
1427278496cSPavel Labath std::unique_ptr<SingleStepWorkaround> SingleStepWorkaround::Get(::pid_t tid) {
1438abd34f0SPavel Labath   Log *log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD);
1448abd34f0SPavel Labath 
1458abd34f0SPavel Labath   static bool workaround_needed = WorkaroundNeeded();
1468abd34f0SPavel Labath   if (!workaround_needed) {
1478abd34f0SPavel Labath     LLDB_LOG(log, "workaround for thread {0} not needed", tid);
1487278496cSPavel Labath     return nullptr;
1498abd34f0SPavel Labath   }
1508abd34f0SPavel Labath 
1518abd34f0SPavel Labath   cpu_set_t original_set;
1528abd34f0SPavel Labath   if (sched_getaffinity(tid, sizeof original_set, &original_set) != 0) {
1538abd34f0SPavel Labath     // This should really not fail. But, just in case...
1548abd34f0SPavel Labath     LLDB_LOG(log, "Unable to get cpu affinity for thread {0}: {1}", tid,
1558abd34f0SPavel Labath              Error(errno, eErrorTypePOSIX));
1567278496cSPavel Labath     return nullptr;
1578abd34f0SPavel Labath   }
1588abd34f0SPavel Labath 
1598abd34f0SPavel Labath   cpu_set_t set;
1608abd34f0SPavel Labath   CPU_ZERO(&set);
1618abd34f0SPavel Labath   CPU_SET(0, &set);
1628abd34f0SPavel Labath   if (sched_setaffinity(tid, sizeof set, &set) != 0) {
1638abd34f0SPavel Labath     // This may fail in very locked down systems, if the thread is not allowed
1648abd34f0SPavel Labath     // to run on cpu 0. If that happens, only thing we can do is it log it and
1658abd34f0SPavel Labath     // continue...
1668abd34f0SPavel Labath     LLDB_LOG(log, "Unable to set cpu affinity for thread {0}: {1}", tid,
1678abd34f0SPavel Labath              Error(errno, eErrorTypePOSIX));
1688abd34f0SPavel Labath   }
1698abd34f0SPavel Labath 
1708abd34f0SPavel Labath   LLDB_LOG(log, "workaround for thread {0} prepared", tid);
1717278496cSPavel Labath   return llvm::make_unique<SingleStepWorkaround>(tid, original_set);
1728abd34f0SPavel Labath }
1738abd34f0SPavel Labath 
1748abd34f0SPavel Labath SingleStepWorkaround::~SingleStepWorkaround() {
175*a37bbbd4SPavel Labath   Log *log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD);
176*a37bbbd4SPavel Labath   LLDB_LOG(log, "Removing workaround");
1778abd34f0SPavel Labath   if (sched_setaffinity(m_tid, sizeof m_original_set, &m_original_set) != 0) {
1788abd34f0SPavel Labath     LLDB_LOG(log, "Unable to reset cpu affinity for thread {0}: {1}", m_tid,
1798abd34f0SPavel Labath              Error(errno, eErrorTypePOSIX));
1808abd34f0SPavel Labath   }
1818abd34f0SPavel Labath }
182605b51b8SPavel Labath #endif
183