Pavel Labath | 605b51b | 2016-02-23 13:56:30 +0000 | [diff] [blame] | 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 | |
Pavel Labath | 8abd34f | 2017-01-25 11:19:45 +0000 | [diff] [blame] | 21 | #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" |
Pavel Labath | 605b51b | 2016-02-23 13:56:30 +0000 | [diff] [blame] | 22 | #include "lldb/Host/linux/Ptrace.h" |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 23 | #include "lldb/Utility/Status.h" |
Pavel Labath | 605b51b | 2016-02-23 13:56:30 +0000 | [diff] [blame] | 24 | |
Pavel Labath | 8abd34f | 2017-01-25 11:19:45 +0000 | [diff] [blame] | 25 | using namespace lldb; |
| 26 | using namespace lldb_private; |
Pavel Labath | 605b51b | 2016-02-23 13:56:30 +0000 | [diff] [blame] | 27 | using namespace lldb_private::process_linux; |
| 28 | |
| 29 | #if defined(__arm64__) || defined(__aarch64__) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 30 | namespace { |
Pavel Labath | 605b51b | 2016-02-23 13:56:30 +0000 | [diff] [blame] | 31 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 32 | void LLVM_ATTRIBUTE_NORETURN Child() { |
| 33 | if (ptrace(PTRACE_TRACEME, 0, nullptr, nullptr) == -1) |
| 34 | _exit(1); |
Pavel Labath | 605b51b | 2016-02-23 13:56:30 +0000 | [diff] [blame] | 35 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 36 | // We just do an endless loop SIGSTOPPING ourselves until killed. The tracer |
Pavel Labath | 8abd34f | 2017-01-25 11:19:45 +0000 | [diff] [blame] | 37 | // will fiddle with our cpu affinities and monitor the behaviour. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 38 | for (;;) { |
| 39 | raise(SIGSTOP); |
Pavel Labath | 605b51b | 2016-02-23 13:56:30 +0000 | [diff] [blame] | 40 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 41 | // Generate a bunch of instructions here, so that a single-step does not |
Pavel Labath | 8abd34f | 2017-01-25 11:19:45 +0000 | [diff] [blame] | 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. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 45 | for (volatile unsigned i = 0; i < CPU_SETSIZE; ++i) |
| 46 | ; |
| 47 | } |
Pavel Labath | 605b51b | 2016-02-23 13:56:30 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 50 | struct ChildDeleter { |
| 51 | ::pid_t pid; |
Pavel Labath | 605b51b | 2016-02-23 13:56:30 +0000 | [diff] [blame] | 52 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | ~ChildDeleter() { |
| 54 | int status; |
| 55 | kill(pid, SIGKILL); // Kill the child. |
| 56 | waitpid(pid, &status, __WALL); // Pick up the remains. |
| 57 | } |
Pavel Labath | 605b51b | 2016-02-23 13:56:30 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
Pavel Labath | 8abd34f | 2017-01-25 11:19:45 +0000 | [diff] [blame] | 60 | bool WorkaroundNeeded() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 61 | // We shall spawn a child, and use it to verify the debug capabilities of the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 62 | // cpu. We shall iterate through the cpus, bind the child to each one in |
| 63 | // turn, and verify that single-stepping works on that cpu. A workaround is |
| 64 | // needed if we find at least one broken cpu. |
Pavel Labath | 605b51b | 2016-02-23 13:56:30 +0000 | [diff] [blame] | 65 | |
Pavel Labath | 8abd34f | 2017-01-25 11:19:45 +0000 | [diff] [blame] | 66 | Log *log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 67 | ::pid_t child_pid = fork(); |
| 68 | if (child_pid == -1) { |
Zachary Turner | 41c9936 | 2017-05-12 05:48:54 +0000 | [diff] [blame] | 69 | LLDB_LOG(log, "failed to fork(): {0}", Status(errno, eErrorTypePOSIX)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 70 | return false; |
| 71 | } |
| 72 | if (child_pid == 0) |
| 73 | Child(); |
Pavel Labath | 605b51b | 2016-02-23 13:56:30 +0000 | [diff] [blame] | 74 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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) { |
Pavel Labath | 8abd34f | 2017-01-25 11:19:45 +0000 | [diff] [blame] | 79 | LLDB_LOG(log, "failed to get available cpus: {0}", |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 80 | Status(errno, eErrorTypePOSIX)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 81 | return false; |
| 82 | } |
| 83 | |
| 84 | int status; |
| 85 | ::pid_t wpid = waitpid(child_pid, &status, __WALL); |
| 86 | if (wpid != child_pid || !WIFSTOPPED(status)) { |
Pavel Labath | 8abd34f | 2017-01-25 11:19:45 +0000 | [diff] [blame] | 87 | LLDB_LOG(log, "waitpid() failed (status = {0:x}): {1}", status, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 88 | Status(errno, eErrorTypePOSIX)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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) { |
Pavel Labath | 8abd34f | 2017-01-25 11:19:45 +0000 | [diff] [blame] | 101 | LLDB_LOG(log, "failed to switch to cpu {0}: {1}", cpu, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 102 | Status(errno, eErrorTypePOSIX)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 103 | continue; |
Pavel Labath | 605b51b | 2016-02-23 13:56:30 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | int status; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 107 | Status error = |
Pavel Labath | 8abd34f | 2017-01-25 11:19:45 +0000 | [diff] [blame] | 108 | NativeProcessLinux::PtraceWrapper(PTRACE_SINGLESTEP, child_pid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 109 | if (error.Fail()) { |
Pavel Labath | 8abd34f | 2017-01-25 11:19:45 +0000 | [diff] [blame] | 110 | LLDB_LOG(log, "single step failed: {0}", error); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 111 | break; |
Pavel Labath | 605b51b | 2016-02-23 13:56:30 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 114 | wpid = waitpid(child_pid, &status, __WALL); |
| 115 | if (wpid != child_pid || !WIFSTOPPED(status)) { |
Pavel Labath | 8abd34f | 2017-01-25 11:19:45 +0000 | [diff] [blame] | 116 | LLDB_LOG(log, "waitpid() failed (status = {0:x}): {1}", status, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 117 | Status(errno, eErrorTypePOSIX)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 118 | break; |
Pavel Labath | 605b51b | 2016-02-23 13:56:30 +0000 | [diff] [blame] | 119 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 120 | if (WSTOPSIG(status) != SIGTRAP) { |
Pavel Labath | 8abd34f | 2017-01-25 11:19:45 +0000 | [diff] [blame] | 121 | LLDB_LOG(log, "single stepping on cpu {0} failed with status {1:x}", cpu, |
| 122 | status); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 123 | break; |
Pavel Labath | 605b51b | 2016-02-23 13:56:30 +0000 | [diff] [blame] | 124 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 125 | } |
Pavel Labath | 605b51b | 2016-02-23 13:56:30 +0000 | [diff] [blame] | 126 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 127 | // cpu is either the index of the first broken cpu, or CPU_SETSIZE. |
| 128 | if (cpu == 0) { |
Pavel Labath | 8abd34f | 2017-01-25 11:19:45 +0000 | [diff] [blame] | 129 | LLDB_LOG(log, |
| 130 | "SINGLE STEPPING ON FIRST CPU IS NOT WORKING. DEBUGGING " |
| 131 | "LIKELY TO BE UNRELIABLE."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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; |
Pavel Labath | 605b51b | 2016-02-23 13:56:30 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Pavel Labath | 8abd34f | 2017-01-25 11:19:45 +0000 | [diff] [blame] | 140 | } // end anonymous namespace |
| 141 | |
Pavel Labath | 7278496 | 2017-02-16 18:12:04 +0000 | [diff] [blame] | 142 | std::unique_ptr<SingleStepWorkaround> SingleStepWorkaround::Get(::pid_t tid) { |
Pavel Labath | 8abd34f | 2017-01-25 11:19:45 +0000 | [diff] [blame] | 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); |
Pavel Labath | 7278496 | 2017-02-16 18:12:04 +0000 | [diff] [blame] | 148 | return nullptr; |
Pavel Labath | 8abd34f | 2017-01-25 11:19:45 +0000 | [diff] [blame] | 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, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 155 | Status(errno, eErrorTypePOSIX)); |
Pavel Labath | 7278496 | 2017-02-16 18:12:04 +0000 | [diff] [blame] | 156 | return nullptr; |
Pavel Labath | 8abd34f | 2017-01-25 11:19:45 +0000 | [diff] [blame] | 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, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 167 | Status(errno, eErrorTypePOSIX)); |
Pavel Labath | 8abd34f | 2017-01-25 11:19:45 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | LLDB_LOG(log, "workaround for thread {0} prepared", tid); |
Pavel Labath | 7278496 | 2017-02-16 18:12:04 +0000 | [diff] [blame] | 171 | return llvm::make_unique<SingleStepWorkaround>(tid, original_set); |
Pavel Labath | 8abd34f | 2017-01-25 11:19:45 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | SingleStepWorkaround::~SingleStepWorkaround() { |
Pavel Labath | a37bbbd | 2017-02-17 11:48:34 +0000 | [diff] [blame] | 175 | Log *log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); |
| 176 | LLDB_LOG(log, "Removing workaround"); |
Pavel Labath | 8abd34f | 2017-01-25 11:19:45 +0000 | [diff] [blame] | 177 | if (sched_setaffinity(m_tid, sizeof m_original_set, &m_original_set) != 0) { |
Pavel Labath | 8abd34f | 2017-01-25 11:19:45 +0000 | [diff] [blame] | 178 | LLDB_LOG(log, "Unable to reset cpu affinity for thread {0}: {1}", m_tid, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 179 | Status(errno, eErrorTypePOSIX)); |
Pavel Labath | 8abd34f | 2017-01-25 11:19:45 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
Pavel Labath | 605b51b | 2016-02-23 13:56:30 +0000 | [diff] [blame] | 182 | #endif |