blob: a7837daa833f5967a80243b5976544f4a5a9260c [file] [log] [blame]
Pavel Labath605b51b2016-02-23 13:56:30 +00001//===-- SingleStepCheck.cpp ----------------------------------- -*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Pavel Labath605b51b2016-02-23 13:56:30 +00006//
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"
Alex Langford7603bd52019-04-16 21:21:28 +000019#include "llvm/Support/Errno.h"
Pavel Labath605b51b2016-02-23 13:56:30 +000020
Pavel Labath8abd34f2017-01-25 11:19:45 +000021#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
Pavel Labath605b51b2016-02-23 13:56:30 +000022#include "lldb/Host/linux/Ptrace.h"
Zachary Turner97206d52017-05-12 04:51:55 +000023#include "lldb/Utility/Status.h"
Pavel Labath605b51b2016-02-23 13:56:30 +000024
Pavel Labath8abd34f2017-01-25 11:19:45 +000025using namespace lldb;
26using namespace lldb_private;
Pavel Labath605b51b2016-02-23 13:56:30 +000027using namespace lldb_private::process_linux;
28
29#if defined(__arm64__) || defined(__aarch64__)
Kate Stoneb9c1b512016-09-06 20:57:50 +000030namespace {
Pavel Labath605b51b2016-02-23 13:56:30 +000031
Kate Stoneb9c1b512016-09-06 20:57:50 +000032void LLVM_ATTRIBUTE_NORETURN Child() {
33 if (ptrace(PTRACE_TRACEME, 0, nullptr, nullptr) == -1)
34 _exit(1);
Pavel Labath605b51b2016-02-23 13:56:30 +000035
Kate Stoneb9c1b512016-09-06 20:57:50 +000036 // We just do an endless loop SIGSTOPPING ourselves until killed. The tracer
Pavel Labath8abd34f2017-01-25 11:19:45 +000037 // will fiddle with our cpu affinities and monitor the behaviour.
Kate Stoneb9c1b512016-09-06 20:57:50 +000038 for (;;) {
39 raise(SIGSTOP);
Pavel Labath605b51b2016-02-23 13:56:30 +000040
Kate Stoneb9c1b512016-09-06 20:57:50 +000041 // Generate a bunch of instructions here, so that a single-step does not
Pavel Labath8abd34f2017-01-25 11:19:45 +000042 // 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 Stoneb9c1b512016-09-06 20:57:50 +000045 for (volatile unsigned i = 0; i < CPU_SETSIZE; ++i)
46 ;
47 }
Pavel Labath605b51b2016-02-23 13:56:30 +000048}
49
Kate Stoneb9c1b512016-09-06 20:57:50 +000050struct ChildDeleter {
51 ::pid_t pid;
Pavel Labath605b51b2016-02-23 13:56:30 +000052
Kate Stoneb9c1b512016-09-06 20:57:50 +000053 ~ChildDeleter() {
54 int status;
Michal Gorny28191362019-03-21 19:35:55 +000055 // Kill the child.
56 kill(pid, SIGKILL);
57 // Pick up the remains.
58 llvm::sys::RetryAfterSignal(-1, waitpid, pid, &status, __WALL);
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 }
Pavel Labath605b51b2016-02-23 13:56:30 +000060};
61
Pavel Labath8abd34f2017-01-25 11:19:45 +000062bool WorkaroundNeeded() {
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 // We shall spawn a child, and use it to verify the debug capabilities of the
Adrian Prantl05097242018-04-30 16:49:04 +000064 // cpu. We shall iterate through the cpus, bind the child to each one in
65 // turn, and verify that single-stepping works on that cpu. A workaround is
66 // needed if we find at least one broken cpu.
Pavel Labath605b51b2016-02-23 13:56:30 +000067
Pavel Labath8abd34f2017-01-25 11:19:45 +000068 Log *log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD);
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 ::pid_t child_pid = fork();
70 if (child_pid == -1) {
Zachary Turner41c99362017-05-12 05:48:54 +000071 LLDB_LOG(log, "failed to fork(): {0}", Status(errno, eErrorTypePOSIX));
Kate Stoneb9c1b512016-09-06 20:57:50 +000072 return false;
73 }
74 if (child_pid == 0)
75 Child();
Pavel Labath605b51b2016-02-23 13:56:30 +000076
Kate Stoneb9c1b512016-09-06 20:57:50 +000077 ChildDeleter child_deleter{child_pid};
78 cpu_set_t available_cpus;
79 if (sched_getaffinity(child_pid, sizeof available_cpus, &available_cpus) ==
80 -1) {
Pavel Labath8abd34f2017-01-25 11:19:45 +000081 LLDB_LOG(log, "failed to get available cpus: {0}",
Zachary Turner97206d52017-05-12 04:51:55 +000082 Status(errno, eErrorTypePOSIX));
Kate Stoneb9c1b512016-09-06 20:57:50 +000083 return false;
84 }
85
86 int status;
Michal Gorny28191362019-03-21 19:35:55 +000087 ::pid_t wpid = llvm::sys::RetryAfterSignal(-1, waitpid,
88 child_pid, &status, __WALL);
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 if (wpid != child_pid || !WIFSTOPPED(status)) {
Pavel Labath8abd34f2017-01-25 11:19:45 +000090 LLDB_LOG(log, "waitpid() failed (status = {0:x}): {1}", status,
Zachary Turner97206d52017-05-12 04:51:55 +000091 Status(errno, eErrorTypePOSIX));
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 return false;
93 }
94
95 unsigned cpu;
96 for (cpu = 0; cpu < CPU_SETSIZE; ++cpu) {
97 if (!CPU_ISSET(cpu, &available_cpus))
98 continue;
99
100 cpu_set_t cpus;
101 CPU_ZERO(&cpus);
102 CPU_SET(cpu, &cpus);
103 if (sched_setaffinity(child_pid, sizeof cpus, &cpus) == -1) {
Pavel Labath8abd34f2017-01-25 11:19:45 +0000104 LLDB_LOG(log, "failed to switch to cpu {0}: {1}", cpu,
Zachary Turner97206d52017-05-12 04:51:55 +0000105 Status(errno, eErrorTypePOSIX));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 continue;
Pavel Labath605b51b2016-02-23 13:56:30 +0000107 }
108
109 int status;
Zachary Turner97206d52017-05-12 04:51:55 +0000110 Status error =
Pavel Labath8abd34f2017-01-25 11:19:45 +0000111 NativeProcessLinux::PtraceWrapper(PTRACE_SINGLESTEP, child_pid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112 if (error.Fail()) {
Pavel Labath8abd34f2017-01-25 11:19:45 +0000113 LLDB_LOG(log, "single step failed: {0}", error);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114 break;
Pavel Labath605b51b2016-02-23 13:56:30 +0000115 }
116
Michal Gorny28191362019-03-21 19:35:55 +0000117 wpid = llvm::sys::RetryAfterSignal(-1, waitpid,
118 child_pid, &status, __WALL);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119 if (wpid != child_pid || !WIFSTOPPED(status)) {
Pavel Labath8abd34f2017-01-25 11:19:45 +0000120 LLDB_LOG(log, "waitpid() failed (status = {0:x}): {1}", status,
Zachary Turner97206d52017-05-12 04:51:55 +0000121 Status(errno, eErrorTypePOSIX));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000122 break;
Pavel Labath605b51b2016-02-23 13:56:30 +0000123 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124 if (WSTOPSIG(status) != SIGTRAP) {
Pavel Labath8abd34f2017-01-25 11:19:45 +0000125 LLDB_LOG(log, "single stepping on cpu {0} failed with status {1:x}", cpu,
126 status);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127 break;
Pavel Labath605b51b2016-02-23 13:56:30 +0000128 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 }
Pavel Labath605b51b2016-02-23 13:56:30 +0000130
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131 // cpu is either the index of the first broken cpu, or CPU_SETSIZE.
132 if (cpu == 0) {
Pavel Labath8abd34f2017-01-25 11:19:45 +0000133 LLDB_LOG(log,
134 "SINGLE STEPPING ON FIRST CPU IS NOT WORKING. DEBUGGING "
135 "LIKELY TO BE UNRELIABLE.");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 // No point in trying to fiddle with the affinities, just give it our best
137 // shot and see how it goes.
138 return false;
139 }
140
141 return cpu != CPU_SETSIZE;
Pavel Labath605b51b2016-02-23 13:56:30 +0000142}
143
Pavel Labath8abd34f2017-01-25 11:19:45 +0000144} // end anonymous namespace
145
Pavel Labath72784962017-02-16 18:12:04 +0000146std::unique_ptr<SingleStepWorkaround> SingleStepWorkaround::Get(::pid_t tid) {
Pavel Labath8abd34f2017-01-25 11:19:45 +0000147 Log *log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD);
148
149 static bool workaround_needed = WorkaroundNeeded();
150 if (!workaround_needed) {
151 LLDB_LOG(log, "workaround for thread {0} not needed", tid);
Pavel Labath72784962017-02-16 18:12:04 +0000152 return nullptr;
Pavel Labath8abd34f2017-01-25 11:19:45 +0000153 }
154
155 cpu_set_t original_set;
156 if (sched_getaffinity(tid, sizeof original_set, &original_set) != 0) {
157 // This should really not fail. But, just in case...
158 LLDB_LOG(log, "Unable to get cpu affinity for thread {0}: {1}", tid,
Zachary Turner97206d52017-05-12 04:51:55 +0000159 Status(errno, eErrorTypePOSIX));
Pavel Labath72784962017-02-16 18:12:04 +0000160 return nullptr;
Pavel Labath8abd34f2017-01-25 11:19:45 +0000161 }
162
163 cpu_set_t set;
164 CPU_ZERO(&set);
165 CPU_SET(0, &set);
166 if (sched_setaffinity(tid, sizeof set, &set) != 0) {
167 // This may fail in very locked down systems, if the thread is not allowed
168 // to run on cpu 0. If that happens, only thing we can do is it log it and
169 // continue...
170 LLDB_LOG(log, "Unable to set cpu affinity for thread {0}: {1}", tid,
Zachary Turner97206d52017-05-12 04:51:55 +0000171 Status(errno, eErrorTypePOSIX));
Pavel Labath8abd34f2017-01-25 11:19:45 +0000172 }
173
174 LLDB_LOG(log, "workaround for thread {0} prepared", tid);
Pavel Labath72784962017-02-16 18:12:04 +0000175 return llvm::make_unique<SingleStepWorkaround>(tid, original_set);
Pavel Labath8abd34f2017-01-25 11:19:45 +0000176}
177
178SingleStepWorkaround::~SingleStepWorkaround() {
Pavel Labatha37bbbd2017-02-17 11:48:34 +0000179 Log *log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD);
180 LLDB_LOG(log, "Removing workaround");
Pavel Labath8abd34f2017-01-25 11:19:45 +0000181 if (sched_setaffinity(m_tid, sizeof m_original_set, &m_original_set) != 0) {
Pavel Labath8abd34f2017-01-25 11:19:45 +0000182 LLDB_LOG(log, "Unable to reset cpu affinity for thread {0}: {1}", m_tid,
Zachary Turner97206d52017-05-12 04:51:55 +0000183 Status(errno, eErrorTypePOSIX));
Pavel Labath8abd34f2017-01-25 11:19:45 +0000184 }
185}
Pavel Labath605b51b2016-02-23 13:56:30 +0000186#endif