Kamil Rytarowski | 1a3d19d | 2017-03-21 17:30:47 +0000 | [diff] [blame] | 1 | //===-- NativeProcessNetBSD.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 "NativeProcessNetBSD.h" |
| 11 | |
| 12 | // C Includes |
| 13 | |
| 14 | // C++ Includes |
| 15 | |
| 16 | // Other libraries and framework includes |
Kamil Rytarowski | 1a3d19d | 2017-03-21 17:30:47 +0000 | [diff] [blame] | 17 | #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 18 | #include "lldb/Core/State.h" |
| 19 | #include "lldb/Host/HostProcess.h" |
| 20 | #include "lldb/Host/common/NativeBreakpoint.h" |
| 21 | #include "lldb/Host/common/NativeRegisterContext.h" |
| 22 | #include "lldb/Host/posix/ProcessLauncherPosixFork.h" |
| 23 | #include "lldb/Target/Process.h" |
Pavel Labath | c1a6b12 | 2017-07-03 09:25:55 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Errno.h" |
Kamil Rytarowski | 1a3d19d | 2017-03-21 17:30:47 +0000 | [diff] [blame] | 25 | |
| 26 | // System includes - They have to be included after framework includes because |
| 27 | // they define some |
| 28 | // macros which collide with variable names in other modules |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 29 | // clang-format off |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/ptrace.h> |
| 32 | #include <sys/sysctl.h> |
| 33 | #include <sys/wait.h> |
| 34 | #include <uvm/uvm_prot.h> |
| 35 | #include <elf.h> |
| 36 | #include <util.h> |
| 37 | // clang-format on |
Kamil Rytarowski | 1a3d19d | 2017-03-21 17:30:47 +0000 | [diff] [blame] | 38 | |
| 39 | using namespace lldb; |
| 40 | using namespace lldb_private; |
| 41 | using namespace lldb_private::process_netbsd; |
| 42 | using namespace llvm; |
| 43 | |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 44 | // Simple helper function to ensure flags are enabled on the given file |
| 45 | // descriptor. |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 46 | static Status EnsureFDFlags(int fd, int flags) { |
| 47 | Status error; |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 48 | |
| 49 | int status = fcntl(fd, F_GETFL); |
| 50 | if (status == -1) { |
| 51 | error.SetErrorToErrno(); |
| 52 | return error; |
| 53 | } |
| 54 | |
| 55 | if (fcntl(fd, F_SETFL, status | flags) == -1) { |
| 56 | error.SetErrorToErrno(); |
| 57 | return error; |
| 58 | } |
| 59 | |
| 60 | return error; |
| 61 | } |
| 62 | |
Kamil Rytarowski | 1a3d19d | 2017-03-21 17:30:47 +0000 | [diff] [blame] | 63 | // ----------------------------------------------------------------------------- |
| 64 | // Public Static Methods |
| 65 | // ----------------------------------------------------------------------------- |
| 66 | |
Pavel Labath | 82abefa | 2017-07-18 09:24:48 +0000 | [diff] [blame] | 67 | llvm::Expected<std::unique_ptr<NativeProcessProtocol>> |
Pavel Labath | 96e600f | 2017-07-07 11:02:19 +0000 | [diff] [blame] | 68 | NativeProcessNetBSD::Factory::Launch(ProcessLaunchInfo &launch_info, |
| 69 | NativeDelegate &native_delegate, |
| 70 | MainLoop &mainloop) const { |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 71 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
| 72 | |
Pavel Labath | 96e600f | 2017-07-07 11:02:19 +0000 | [diff] [blame] | 73 | Status status; |
| 74 | ::pid_t pid = ProcessLauncherPosixFork() |
| 75 | .LaunchProcess(launch_info, status) |
| 76 | .GetProcessId(); |
| 77 | LLDB_LOG(log, "pid = {0:x}", pid); |
| 78 | if (status.Fail()) { |
| 79 | LLDB_LOG(log, "failed to launch process: {0}", status); |
| 80 | return status.ToError(); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Pavel Labath | 96e600f | 2017-07-07 11:02:19 +0000 | [diff] [blame] | 83 | // Wait for the child process to trap on its call to execve. |
| 84 | int wstatus; |
| 85 | ::pid_t wpid = llvm::sys::RetryAfterSignal(-1, ::waitpid, pid, &wstatus, 0); |
| 86 | assert(wpid == pid); |
| 87 | (void)wpid; |
| 88 | if (!WIFSTOPPED(wstatus)) { |
| 89 | LLDB_LOG(log, "Could not sync with inferior process: wstatus={1}", |
| 90 | WaitStatus::Decode(wstatus)); |
| 91 | return llvm::make_error<StringError>("Could not sync with inferior process", |
| 92 | llvm::inconvertibleErrorCode()); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 93 | } |
Pavel Labath | 96e600f | 2017-07-07 11:02:19 +0000 | [diff] [blame] | 94 | LLDB_LOG(log, "inferior started, now in stopped state"); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 95 | |
Pavel Labath | 96e600f | 2017-07-07 11:02:19 +0000 | [diff] [blame] | 96 | ArchSpec arch; |
| 97 | if ((status = ResolveProcessArchitecture(pid, arch)).Fail()) |
| 98 | return status.ToError(); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 99 | |
Pavel Labath | 96e600f | 2017-07-07 11:02:19 +0000 | [diff] [blame] | 100 | // Set the architecture to the exe architecture. |
| 101 | LLDB_LOG(log, "pid = {0:x}, detected architecture {1}", pid, |
| 102 | arch.GetArchitectureName()); |
| 103 | |
Pavel Labath | 82abefa | 2017-07-18 09:24:48 +0000 | [diff] [blame] | 104 | std::unique_ptr<NativeProcessNetBSD> process_up(new NativeProcessNetBSD( |
Pavel Labath | 96e600f | 2017-07-07 11:02:19 +0000 | [diff] [blame] | 105 | pid, launch_info.GetPTY().ReleaseMasterFileDescriptor(), native_delegate, |
| 106 | arch, mainloop)); |
| 107 | |
Pavel Labath | 82abefa | 2017-07-18 09:24:48 +0000 | [diff] [blame] | 108 | status = process_up->ReinitializeThreads(); |
Pavel Labath | 96e600f | 2017-07-07 11:02:19 +0000 | [diff] [blame] | 109 | if (status.Fail()) |
| 110 | return status.ToError(); |
| 111 | |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame^] | 112 | for (const auto &thread : process_up->m_threads) |
| 113 | static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(SIGSTOP); |
Pavel Labath | 82abefa | 2017-07-18 09:24:48 +0000 | [diff] [blame] | 114 | process_up->SetState(StateType::eStateStopped); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 115 | |
Pavel Labath | 82abefa | 2017-07-18 09:24:48 +0000 | [diff] [blame] | 116 | return std::move(process_up); |
Kamil Rytarowski | 1a3d19d | 2017-03-21 17:30:47 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Pavel Labath | 82abefa | 2017-07-18 09:24:48 +0000 | [diff] [blame] | 119 | llvm::Expected<std::unique_ptr<NativeProcessProtocol>> |
| 120 | NativeProcessNetBSD::Factory::Attach( |
Kamil Rytarowski | 1a3d19d | 2017-03-21 17:30:47 +0000 | [diff] [blame] | 121 | lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate, |
Pavel Labath | 96e600f | 2017-07-07 11:02:19 +0000 | [diff] [blame] | 122 | MainLoop &mainloop) const { |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 123 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
| 124 | LLDB_LOG(log, "pid = {0:x}", pid); |
| 125 | |
| 126 | // Retrieve the architecture for the running process. |
Pavel Labath | 96e600f | 2017-07-07 11:02:19 +0000 | [diff] [blame] | 127 | ArchSpec arch; |
| 128 | Status status = ResolveProcessArchitecture(pid, arch); |
| 129 | if (!status.Success()) |
| 130 | return status.ToError(); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 131 | |
Pavel Labath | 82abefa | 2017-07-18 09:24:48 +0000 | [diff] [blame] | 132 | std::unique_ptr<NativeProcessNetBSD> process_up( |
Pavel Labath | 96e600f | 2017-07-07 11:02:19 +0000 | [diff] [blame] | 133 | new NativeProcessNetBSD(pid, -1, native_delegate, arch, mainloop)); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 134 | |
Pavel Labath | 82abefa | 2017-07-18 09:24:48 +0000 | [diff] [blame] | 135 | status = process_up->Attach(); |
Pavel Labath | 96e600f | 2017-07-07 11:02:19 +0000 | [diff] [blame] | 136 | if (!status.Success()) |
| 137 | return status.ToError(); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 138 | |
Pavel Labath | 82abefa | 2017-07-18 09:24:48 +0000 | [diff] [blame] | 139 | return std::move(process_up); |
Kamil Rytarowski | 1a3d19d | 2017-03-21 17:30:47 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | // ----------------------------------------------------------------------------- |
| 143 | // Public Instance Methods |
| 144 | // ----------------------------------------------------------------------------- |
| 145 | |
Pavel Labath | 96e600f | 2017-07-07 11:02:19 +0000 | [diff] [blame] | 146 | NativeProcessNetBSD::NativeProcessNetBSD(::pid_t pid, int terminal_fd, |
| 147 | NativeDelegate &delegate, |
| 148 | const ArchSpec &arch, |
| 149 | MainLoop &mainloop) |
| 150 | : NativeProcessProtocol(pid, terminal_fd, delegate), m_arch(arch) { |
| 151 | if (m_terminal_fd != -1) { |
| 152 | Status status = EnsureFDFlags(m_terminal_fd, O_NONBLOCK); |
| 153 | assert(status.Success()); |
| 154 | } |
| 155 | |
| 156 | Status status; |
| 157 | m_sigchld_handle = mainloop.RegisterSignal( |
| 158 | SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, status); |
| 159 | assert(m_sigchld_handle && status.Success()); |
| 160 | } |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 161 | |
| 162 | // Handles all waitpid events from the inferior process. |
| 163 | void NativeProcessNetBSD::MonitorCallback(lldb::pid_t pid, int signal) { |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 164 | switch (signal) { |
| 165 | case SIGTRAP: |
| 166 | return MonitorSIGTRAP(pid); |
| 167 | case SIGSTOP: |
| 168 | return MonitorSIGSTOP(pid); |
| 169 | default: |
| 170 | return MonitorSignal(pid, signal); |
| 171 | } |
| 172 | } |
| 173 | |
Pavel Labath | 3508fc8 | 2017-06-19 12:47:50 +0000 | [diff] [blame] | 174 | void NativeProcessNetBSD::MonitorExited(lldb::pid_t pid, WaitStatus status) { |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 175 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
| 176 | |
Pavel Labath | 3508fc8 | 2017-06-19 12:47:50 +0000 | [diff] [blame] | 177 | LLDB_LOG(log, "got exit signal({0}) , pid = {1}", status, pid); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 178 | |
| 179 | /* Stop Tracking All Threads attached to Process */ |
| 180 | m_threads.clear(); |
| 181 | |
Pavel Labath | 3508fc8 | 2017-06-19 12:47:50 +0000 | [diff] [blame] | 182 | SetExitStatus(status, true); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 183 | |
| 184 | // Notify delegate that our process has exited. |
| 185 | SetState(StateType::eStateExited, true); |
| 186 | } |
| 187 | |
| 188 | void NativeProcessNetBSD::MonitorSIGSTOP(lldb::pid_t pid) { |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 189 | ptrace_siginfo_t info; |
| 190 | |
| 191 | const auto siginfo_err = |
| 192 | PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info)); |
| 193 | |
| 194 | // Get details on the signal raised. |
| 195 | if (siginfo_err.Success()) { |
| 196 | // Handle SIGSTOP from LLGS (LLDB GDB Server) |
| 197 | if (info.psi_siginfo.si_code == SI_USER && |
| 198 | info.psi_siginfo.si_pid == ::getpid()) { |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame^] | 199 | /* Stop Tracking all Threads attached to Process */ |
| 200 | for (const auto &thread : m_threads) { |
| 201 | static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal( |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 202 | SIGSTOP, &info.psi_siginfo); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | void NativeProcessNetBSD::MonitorSIGTRAP(lldb::pid_t pid) { |
| 209 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
| 210 | ptrace_siginfo_t info; |
| 211 | |
| 212 | const auto siginfo_err = |
| 213 | PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info)); |
| 214 | |
| 215 | // Get details on the signal raised. |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 216 | if (siginfo_err.Fail()) { |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | switch (info.psi_siginfo.si_code) { |
| 221 | case TRAP_BRKPT: |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame^] | 222 | for (const auto &thread : m_threads) { |
| 223 | static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByBreakpoint(); |
| 224 | FixupBreakpointPCAsNeeded(static_cast<NativeThreadNetBSD &>(*thread)); |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 225 | } |
| 226 | SetState(StateType::eStateStopped, true); |
| 227 | break; |
| 228 | case TRAP_TRACE: |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame^] | 229 | for (const auto &thread : m_threads) |
| 230 | static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByTrace(); |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 231 | SetState(StateType::eStateStopped, true); |
| 232 | break; |
| 233 | case TRAP_EXEC: { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 234 | Status error = ReinitializeThreads(); |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 235 | if (error.Fail()) { |
| 236 | SetState(StateType::eStateInvalid); |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | // Let our delegate know we have just exec'd. |
| 241 | NotifyDidExec(); |
| 242 | |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame^] | 243 | for (const auto &thread : m_threads) |
| 244 | static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByExec(); |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 245 | SetState(StateType::eStateStopped, true); |
| 246 | } break; |
| 247 | case TRAP_DBREG: { |
| 248 | // If a watchpoint was hit, report it |
| 249 | uint32_t wp_index; |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame^] | 250 | Status error = static_cast<NativeThreadNetBSD &>(*m_threads[info.psi_lwpid]) |
| 251 | .GetRegisterContext() |
| 252 | ->GetWatchpointHitIndex( |
| 253 | wp_index, (uintptr_t)info.psi_siginfo.si_addr); |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 254 | if (error.Fail()) |
| 255 | LLDB_LOG(log, |
| 256 | "received error while checking for watchpoint hits, pid = " |
| 257 | "{0}, LWP = {1}, error = {2}", |
| 258 | GetID(), info.psi_lwpid, error); |
| 259 | if (wp_index != LLDB_INVALID_INDEX32) { |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame^] | 260 | for (const auto &thread : m_threads) |
| 261 | static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByWatchpoint( |
| 262 | wp_index); |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 263 | SetState(StateType::eStateStopped, true); |
| 264 | break; |
| 265 | } |
| 266 | |
| 267 | // If a breakpoint was hit, report it |
| 268 | uint32_t bp_index; |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame^] | 269 | error = static_cast<NativeThreadNetBSD &>(*m_threads[info.psi_lwpid]) |
| 270 | .GetRegisterContext() |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 271 | ->GetHardwareBreakHitIndex(bp_index, |
| 272 | (uintptr_t)info.psi_siginfo.si_addr); |
| 273 | if (error.Fail()) |
| 274 | LLDB_LOG(log, |
| 275 | "received error while checking for hardware " |
| 276 | "breakpoint hits, pid = {0}, LWP = {1}, error = {2}", |
| 277 | GetID(), info.psi_lwpid, error); |
| 278 | if (bp_index != LLDB_INVALID_INDEX32) { |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame^] | 279 | for (const auto &thread : m_threads) |
| 280 | static_cast<NativeThreadNetBSD &>(*thread).SetStoppedByBreakpoint(); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 281 | SetState(StateType::eStateStopped, true); |
| 282 | break; |
| 283 | } |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 284 | } break; |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
| 288 | void NativeProcessNetBSD::MonitorSignal(lldb::pid_t pid, int signal) { |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 289 | ptrace_siginfo_t info; |
| 290 | const auto siginfo_err = |
| 291 | PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info)); |
| 292 | |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame^] | 293 | for (const auto &thread : m_threads) { |
| 294 | static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal( |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 295 | info.psi_siginfo.si_signo, &info.psi_siginfo); |
| 296 | } |
| 297 | SetState(StateType::eStateStopped, true); |
| 298 | } |
| 299 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 300 | Status NativeProcessNetBSD::PtraceWrapper(int req, lldb::pid_t pid, void *addr, |
| 301 | int data, int *result) { |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 302 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 303 | Status error; |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 304 | int ret; |
| 305 | |
| 306 | errno = 0; |
| 307 | ret = ptrace(req, static_cast<::pid_t>(pid), addr, data); |
| 308 | |
| 309 | if (ret == -1) |
| 310 | error.SetErrorToErrno(); |
| 311 | |
| 312 | if (result) |
| 313 | *result = ret; |
| 314 | |
| 315 | LLDB_LOG(log, "ptrace({0}, {1}, {2}, {3})={4:x}", req, pid, addr, data, ret); |
| 316 | |
| 317 | if (error.Fail()) |
| 318 | LLDB_LOG(log, "ptrace() failed: {0}", error); |
| 319 | |
| 320 | return error; |
| 321 | } |
| 322 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 323 | Status NativeProcessNetBSD::GetSoftwareBreakpointPCOffset( |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 324 | uint32_t &actual_opcode_size) { |
| 325 | // FIXME put this behind a breakpoint protocol class that can be |
| 326 | // set per architecture. Need ARM, MIPS support here. |
| 327 | static const uint8_t g_i386_opcode[] = {0xCC}; |
| 328 | switch (m_arch.GetMachine()) { |
| 329 | case llvm::Triple::x86_64: |
| 330 | actual_opcode_size = static_cast<uint32_t>(sizeof(g_i386_opcode)); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 331 | return Status(); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 332 | default: |
| 333 | assert(false && "CPU type not supported!"); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 334 | return Status("CPU type not supported"); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 338 | Status |
| 339 | NativeProcessNetBSD::FixupBreakpointPCAsNeeded(NativeThreadNetBSD &thread) { |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 340 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_BREAKPOINTS)); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 341 | Status error; |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 342 | // Find out the size of a breakpoint (might depend on where we are in the |
| 343 | // code). |
| 344 | NativeRegisterContextSP context_sp = thread.GetRegisterContext(); |
| 345 | if (!context_sp) { |
| 346 | error.SetErrorString("cannot get a NativeRegisterContext for the thread"); |
| 347 | LLDB_LOG(log, "failed: {0}", error); |
| 348 | return error; |
| 349 | } |
| 350 | uint32_t breakpoint_size = 0; |
| 351 | error = GetSoftwareBreakpointPCOffset(breakpoint_size); |
| 352 | if (error.Fail()) { |
| 353 | LLDB_LOG(log, "GetBreakpointSize() failed: {0}", error); |
| 354 | return error; |
| 355 | } else |
| 356 | LLDB_LOG(log, "breakpoint size: {0}", breakpoint_size); |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 357 | // First try probing for a breakpoint at a software breakpoint location: PC |
| 358 | // - breakpoint size. |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 359 | const lldb::addr_t initial_pc_addr = |
| 360 | context_sp->GetPCfromBreakpointLocation(); |
| 361 | lldb::addr_t breakpoint_addr = initial_pc_addr; |
| 362 | if (breakpoint_size > 0) { |
| 363 | // Do not allow breakpoint probe to wrap around. |
| 364 | if (breakpoint_addr >= breakpoint_size) |
| 365 | breakpoint_addr -= breakpoint_size; |
| 366 | } |
| 367 | // Check if we stopped because of a breakpoint. |
| 368 | NativeBreakpointSP breakpoint_sp; |
| 369 | error = m_breakpoint_list.GetBreakpoint(breakpoint_addr, breakpoint_sp); |
| 370 | if (!error.Success() || !breakpoint_sp) { |
| 371 | // We didn't find one at a software probe location. Nothing to do. |
| 372 | LLDB_LOG(log, |
| 373 | "pid {0} no lldb breakpoint found at current pc with " |
| 374 | "adjustment: {1}", |
| 375 | GetID(), breakpoint_addr); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 376 | return Status(); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 377 | } |
| 378 | // If the breakpoint is not a software breakpoint, nothing to do. |
| 379 | if (!breakpoint_sp->IsSoftwareBreakpoint()) { |
| 380 | LLDB_LOG( |
| 381 | log, |
| 382 | "pid {0} breakpoint found at {1:x}, not software, nothing to adjust", |
| 383 | GetID(), breakpoint_addr); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 384 | return Status(); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 385 | } |
| 386 | // |
| 387 | // We have a software breakpoint and need to adjust the PC. |
| 388 | // |
| 389 | // Sanity check. |
| 390 | if (breakpoint_size == 0) { |
| 391 | // Nothing to do! How did we get here? |
| 392 | LLDB_LOG(log, |
| 393 | "pid {0} breakpoint found at {1:x}, it is software, but the " |
| 394 | "size is zero, nothing to do (unexpected)", |
| 395 | GetID(), breakpoint_addr); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 396 | return Status(); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 397 | } |
| 398 | // |
| 399 | // We have a software breakpoint and need to adjust the PC. |
| 400 | // |
| 401 | // Sanity check. |
| 402 | if (breakpoint_size == 0) { |
| 403 | // Nothing to do! How did we get here? |
| 404 | LLDB_LOG(log, |
| 405 | "pid {0} breakpoint found at {1:x}, it is software, but the " |
| 406 | "size is zero, nothing to do (unexpected)", |
| 407 | GetID(), breakpoint_addr); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 408 | return Status(); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 409 | } |
| 410 | // Change the program counter. |
| 411 | LLDB_LOG(log, "pid {0} tid {1}: changing PC from {2:x} to {3:x}", GetID(), |
| 412 | thread.GetID(), initial_pc_addr, breakpoint_addr); |
| 413 | error = context_sp->SetPC(breakpoint_addr); |
| 414 | if (error.Fail()) { |
| 415 | LLDB_LOG(log, "pid {0} tid {1}: failed to set PC: {2}", GetID(), |
| 416 | thread.GetID(), error); |
| 417 | return error; |
| 418 | } |
| 419 | return error; |
| 420 | } |
| 421 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 422 | Status NativeProcessNetBSD::Resume(const ResumeActionList &resume_actions) { |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 423 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
| 424 | LLDB_LOG(log, "pid {0}", GetID()); |
| 425 | |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame^] | 426 | const auto &thread = m_threads[0]; |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 427 | const ResumeAction *const action = |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame^] | 428 | resume_actions.GetActionForThread(thread->GetID(), true); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 429 | |
| 430 | if (action == nullptr) { |
| 431 | LLDB_LOG(log, "no action specified for pid {0} tid {1}", GetID(), |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame^] | 432 | thread->GetID()); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 433 | return Status(); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 436 | Status error; |
Kamil Rytarowski | 3eef2b5 | 2017-03-30 20:25:29 +0000 | [diff] [blame] | 437 | |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 438 | switch (action->state) { |
| 439 | case eStateRunning: { |
| 440 | // Run the thread, possibly feeding it the signal. |
Kamil Rytarowski | 3eef2b5 | 2017-03-30 20:25:29 +0000 | [diff] [blame] | 441 | error = NativeProcessNetBSD::PtraceWrapper(PT_CONTINUE, GetID(), (void *)1, |
| 442 | action->signal); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 443 | if (!error.Success()) |
| 444 | return error; |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame^] | 445 | for (const auto &thread : m_threads) |
| 446 | static_cast<NativeThreadNetBSD &>(*thread).SetRunning(); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 447 | SetState(eStateRunning, true); |
| 448 | break; |
| 449 | } |
| 450 | case eStateStepping: |
Kamil Rytarowski | 3eef2b5 | 2017-03-30 20:25:29 +0000 | [diff] [blame] | 451 | // Run the thread, possibly feeding it the signal. |
| 452 | error = NativeProcessNetBSD::PtraceWrapper(PT_STEP, GetID(), (void *)1, |
| 453 | action->signal); |
| 454 | if (!error.Success()) |
| 455 | return error; |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame^] | 456 | for (const auto &thread : m_threads) |
| 457 | static_cast<NativeThreadNetBSD &>(*thread).SetStepping(); |
Kamil Rytarowski | 3eef2b5 | 2017-03-30 20:25:29 +0000 | [diff] [blame] | 458 | SetState(eStateStepping, true); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 459 | break; |
| 460 | |
| 461 | case eStateSuspended: |
| 462 | case eStateStopped: |
| 463 | llvm_unreachable("Unexpected state"); |
| 464 | |
| 465 | default: |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 466 | return Status("NativeProcessNetBSD::%s (): unexpected state %s specified " |
| 467 | "for pid %" PRIu64 ", tid %" PRIu64, |
| 468 | __FUNCTION__, StateAsCString(action->state), GetID(), |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame^] | 469 | thread->GetID()); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 472 | return Status(); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 475 | Status NativeProcessNetBSD::Halt() { |
| 476 | Status error; |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 477 | |
| 478 | if (kill(GetID(), SIGSTOP) != 0) |
| 479 | error.SetErrorToErrno(); |
| 480 | |
| 481 | return error; |
| 482 | } |
| 483 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 484 | Status NativeProcessNetBSD::Detach() { |
| 485 | Status error; |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 486 | |
| 487 | // Stop monitoring the inferior. |
| 488 | m_sigchld_handle.reset(); |
| 489 | |
| 490 | // Tell ptrace to detach from the process. |
| 491 | if (GetID() == LLDB_INVALID_PROCESS_ID) |
| 492 | return error; |
| 493 | |
| 494 | return PtraceWrapper(PT_DETACH, GetID()); |
| 495 | } |
| 496 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 497 | Status NativeProcessNetBSD::Signal(int signo) { |
| 498 | Status error; |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 499 | |
| 500 | if (kill(GetID(), signo)) |
| 501 | error.SetErrorToErrno(); |
| 502 | |
| 503 | return error; |
| 504 | } |
| 505 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 506 | Status NativeProcessNetBSD::Kill() { |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 507 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
| 508 | LLDB_LOG(log, "pid {0}", GetID()); |
| 509 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 510 | Status error; |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 511 | |
| 512 | switch (m_state) { |
| 513 | case StateType::eStateInvalid: |
| 514 | case StateType::eStateExited: |
| 515 | case StateType::eStateCrashed: |
| 516 | case StateType::eStateDetached: |
| 517 | case StateType::eStateUnloaded: |
| 518 | // Nothing to do - the process is already dead. |
| 519 | LLDB_LOG(log, "ignored for PID {0} due to current state: {1}", GetID(), |
| 520 | StateAsCString(m_state)); |
| 521 | return error; |
| 522 | |
| 523 | case StateType::eStateConnected: |
| 524 | case StateType::eStateAttaching: |
| 525 | case StateType::eStateLaunching: |
| 526 | case StateType::eStateStopped: |
| 527 | case StateType::eStateRunning: |
| 528 | case StateType::eStateStepping: |
| 529 | case StateType::eStateSuspended: |
| 530 | // We can try to kill a process in these states. |
| 531 | break; |
| 532 | } |
| 533 | |
| 534 | if (kill(GetID(), SIGKILL) != 0) { |
| 535 | error.SetErrorToErrno(); |
| 536 | return error; |
| 537 | } |
| 538 | |
| 539 | return error; |
| 540 | } |
| 541 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 542 | Status NativeProcessNetBSD::GetMemoryRegionInfo(lldb::addr_t load_addr, |
| 543 | MemoryRegionInfo &range_info) { |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 544 | |
| 545 | if (m_supports_mem_region == LazyBool::eLazyBoolNo) { |
| 546 | // We're done. |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 547 | return Status("unsupported"); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 550 | Status error = PopulateMemoryRegionCache(); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 551 | if (error.Fail()) { |
| 552 | return error; |
| 553 | } |
| 554 | |
| 555 | lldb::addr_t prev_base_address = 0; |
| 556 | // FIXME start by finding the last region that is <= target address using |
| 557 | // binary search. Data is sorted. |
| 558 | // There can be a ton of regions on pthreads apps with lots of threads. |
| 559 | for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end(); |
| 560 | ++it) { |
| 561 | MemoryRegionInfo &proc_entry_info = it->first; |
| 562 | // Sanity check assumption that memory map entries are ascending. |
| 563 | assert((proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) && |
| 564 | "descending memory map entries detected, unexpected"); |
| 565 | prev_base_address = proc_entry_info.GetRange().GetRangeBase(); |
| 566 | UNUSED_IF_ASSERT_DISABLED(prev_base_address); |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 567 | // If the target address comes before this entry, indicate distance to |
| 568 | // next region. |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 569 | if (load_addr < proc_entry_info.GetRange().GetRangeBase()) { |
| 570 | range_info.GetRange().SetRangeBase(load_addr); |
| 571 | range_info.GetRange().SetByteSize( |
| 572 | proc_entry_info.GetRange().GetRangeBase() - load_addr); |
| 573 | range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); |
| 574 | range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); |
| 575 | range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); |
| 576 | range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); |
| 577 | return error; |
| 578 | } else if (proc_entry_info.GetRange().Contains(load_addr)) { |
| 579 | // The target address is within the memory region we're processing here. |
| 580 | range_info = proc_entry_info; |
| 581 | return error; |
| 582 | } |
| 583 | // The target memory address comes somewhere after the region we just |
| 584 | // parsed. |
| 585 | } |
| 586 | // If we made it here, we didn't find an entry that contained the given |
| 587 | // address. Return the |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 588 | // load_addr as start and the amount of bytes betwwen load address and the |
| 589 | // end of the memory as size. |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 590 | range_info.GetRange().SetRangeBase(load_addr); |
| 591 | range_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS); |
| 592 | range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); |
| 593 | range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); |
| 594 | range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); |
| 595 | range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); |
| 596 | return error; |
| 597 | } |
| 598 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 599 | Status NativeProcessNetBSD::PopulateMemoryRegionCache() { |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 600 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
| 601 | // If our cache is empty, pull the latest. There should always be at least |
| 602 | // one memory region if memory region handling is supported. |
| 603 | if (!m_mem_region_cache.empty()) { |
| 604 | LLDB_LOG(log, "reusing {0} cached memory region entries", |
| 605 | m_mem_region_cache.size()); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 606 | return Status(); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | struct kinfo_vmentry *vm; |
| 610 | size_t count, i; |
| 611 | vm = kinfo_getvmmap(GetID(), &count); |
| 612 | if (vm == NULL) { |
| 613 | m_supports_mem_region = LazyBool::eLazyBoolNo; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 614 | Status error; |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 615 | error.SetErrorString("not supported"); |
| 616 | return error; |
| 617 | } |
| 618 | for (i = 0; i < count; i++) { |
| 619 | MemoryRegionInfo info; |
| 620 | info.Clear(); |
| 621 | info.GetRange().SetRangeBase(vm[i].kve_start); |
| 622 | info.GetRange().SetRangeEnd(vm[i].kve_end); |
| 623 | info.SetMapped(MemoryRegionInfo::OptionalBool::eYes); |
| 624 | |
| 625 | if (vm[i].kve_protection & VM_PROT_READ) |
| 626 | info.SetReadable(MemoryRegionInfo::OptionalBool::eYes); |
| 627 | else |
| 628 | info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); |
| 629 | |
| 630 | if (vm[i].kve_protection & VM_PROT_WRITE) |
| 631 | info.SetWritable(MemoryRegionInfo::OptionalBool::eYes); |
| 632 | else |
| 633 | info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); |
| 634 | |
| 635 | if (vm[i].kve_protection & VM_PROT_EXECUTE) |
| 636 | info.SetExecutable(MemoryRegionInfo::OptionalBool::eYes); |
| 637 | else |
| 638 | info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); |
| 639 | |
| 640 | if (vm[i].kve_path[0]) |
| 641 | info.SetName(vm[i].kve_path); |
| 642 | |
| 643 | m_mem_region_cache.emplace_back( |
| 644 | info, FileSpec(info.GetName().GetCString(), true)); |
| 645 | } |
| 646 | free(vm); |
| 647 | |
| 648 | if (m_mem_region_cache.empty()) { |
| 649 | // No entries after attempting to read them. This shouldn't happen. |
| 650 | // Assume we don't support map entries. |
| 651 | LLDB_LOG(log, "failed to find any vmmap entries, assuming no support " |
| 652 | "for memory region metadata retrieval"); |
| 653 | m_supports_mem_region = LazyBool::eLazyBoolNo; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 654 | Status error; |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 655 | error.SetErrorString("not supported"); |
| 656 | return error; |
| 657 | } |
| 658 | LLDB_LOG(log, "read {0} memory region entries from process {1}", |
| 659 | m_mem_region_cache.size(), GetID()); |
| 660 | // We support memory retrieval, remember that. |
| 661 | m_supports_mem_region = LazyBool::eLazyBoolYes; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 662 | return Status(); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 665 | Status NativeProcessNetBSD::AllocateMemory(size_t size, uint32_t permissions, |
| 666 | lldb::addr_t &addr) { |
| 667 | return Status("Unimplemented"); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 670 | Status NativeProcessNetBSD::DeallocateMemory(lldb::addr_t addr) { |
| 671 | return Status("Unimplemented"); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | lldb::addr_t NativeProcessNetBSD::GetSharedLibraryInfoAddress() { |
| 675 | // punt on this for now |
| 676 | return LLDB_INVALID_ADDRESS; |
| 677 | } |
| 678 | |
| 679 | size_t NativeProcessNetBSD::UpdateThreads() { return m_threads.size(); } |
| 680 | |
| 681 | bool NativeProcessNetBSD::GetArchitecture(ArchSpec &arch) const { |
| 682 | arch = m_arch; |
| 683 | return true; |
| 684 | } |
| 685 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 686 | Status NativeProcessNetBSD::SetBreakpoint(lldb::addr_t addr, uint32_t size, |
| 687 | bool hardware) { |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 688 | if (hardware) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 689 | return Status("NativeProcessNetBSD does not support hardware breakpoints"); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 690 | else |
| 691 | return SetSoftwareBreakpoint(addr, size); |
| 692 | } |
| 693 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 694 | Status NativeProcessNetBSD::GetSoftwareBreakpointTrapOpcode( |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 695 | size_t trap_opcode_size_hint, size_t &actual_opcode_size, |
| 696 | const uint8_t *&trap_opcode_bytes) { |
| 697 | static const uint8_t g_i386_opcode[] = {0xCC}; |
| 698 | |
| 699 | switch (m_arch.GetMachine()) { |
| 700 | case llvm::Triple::x86: |
| 701 | case llvm::Triple::x86_64: |
| 702 | trap_opcode_bytes = g_i386_opcode; |
| 703 | actual_opcode_size = sizeof(g_i386_opcode); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 704 | return Status(); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 705 | default: |
| 706 | assert(false && "CPU type not supported!"); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 707 | return Status("CPU type not supported"); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 711 | Status NativeProcessNetBSD::GetLoadedModuleFileSpec(const char *module_path, |
| 712 | FileSpec &file_spec) { |
| 713 | return Status("Unimplemented"); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 716 | Status NativeProcessNetBSD::GetFileLoadAddress(const llvm::StringRef &file_name, |
| 717 | lldb::addr_t &load_addr) { |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 718 | load_addr = LLDB_INVALID_ADDRESS; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 719 | return Status(); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 722 | void NativeProcessNetBSD::SigchldHandler() { |
| 723 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
| 724 | // Process all pending waitpid notifications. |
| 725 | int status; |
Pavel Labath | c1a6b12 | 2017-07-03 09:25:55 +0000 | [diff] [blame] | 726 | ::pid_t wait_pid = |
| 727 | llvm::sys::RetryAfterSignal(-1, waitpid, GetID(), &status, WALLSIG | WNOHANG); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 728 | |
| 729 | if (wait_pid == 0) |
| 730 | return; // We are done. |
| 731 | |
| 732 | if (wait_pid == -1) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 733 | Status error(errno, eErrorTypePOSIX); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 734 | LLDB_LOG(log, "waitpid ({0}, &status, _) failed: {1}", GetID(), error); |
| 735 | } |
| 736 | |
Pavel Labath | 3508fc8 | 2017-06-19 12:47:50 +0000 | [diff] [blame] | 737 | WaitStatus wait_status = WaitStatus::Decode(status); |
| 738 | bool exited = wait_status.type == WaitStatus::Exit || |
| 739 | (wait_status.type == WaitStatus::Signal && |
| 740 | wait_pid == static_cast<::pid_t>(GetID())); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 741 | |
| 742 | LLDB_LOG(log, |
Pavel Labath | 3508fc8 | 2017-06-19 12:47:50 +0000 | [diff] [blame] | 743 | "waitpid ({0}, &status, _) => pid = {1}, status = {2}, exited = {3}", |
| 744 | GetID(), wait_pid, status, exited); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 745 | |
| 746 | if (exited) |
Pavel Labath | 3508fc8 | 2017-06-19 12:47:50 +0000 | [diff] [blame] | 747 | MonitorExited(wait_pid, wait_status); |
| 748 | else { |
Kamil Rytarowski | 4bb7441 | 2017-06-20 13:51:06 +0000 | [diff] [blame] | 749 | assert(wait_status.type == WaitStatus::Stop); |
Pavel Labath | 3508fc8 | 2017-06-19 12:47:50 +0000 | [diff] [blame] | 750 | MonitorCallback(wait_pid, wait_status.status); |
| 751 | } |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Kamil Rytarowski | 269eec0 | 2017-05-24 23:59:50 +0000 | [diff] [blame] | 754 | bool NativeProcessNetBSD::HasThreadNoLock(lldb::tid_t thread_id) { |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame^] | 755 | for (const auto &thread : m_threads) { |
| 756 | assert(thread && "thread list should not contain NULL threads"); |
| 757 | if (thread->GetID() == thread_id) { |
Kamil Rytarowski | 269eec0 | 2017-05-24 23:59:50 +0000 | [diff] [blame] | 758 | // We have this thread. |
| 759 | return true; |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | // We don't have this thread. |
| 764 | return false; |
| 765 | } |
| 766 | |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame^] | 767 | NativeThreadNetBSD &NativeProcessNetBSD::AddThread(lldb::tid_t thread_id) { |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 768 | |
| 769 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD)); |
| 770 | LLDB_LOG(log, "pid {0} adding thread with tid {1}", GetID(), thread_id); |
| 771 | |
| 772 | assert(!HasThreadNoLock(thread_id) && |
| 773 | "attempted to add a thread by id that already exists"); |
| 774 | |
| 775 | // If this is the first thread, save it as the current thread |
| 776 | if (m_threads.empty()) |
| 777 | SetCurrentThreadID(thread_id); |
| 778 | |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame^] | 779 | m_threads.push_back(llvm::make_unique<NativeThreadNetBSD>(*this, thread_id)); |
| 780 | return static_cast<NativeThreadNetBSD &>(*m_threads.back()); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Pavel Labath | 96e600f | 2017-07-07 11:02:19 +0000 | [diff] [blame] | 783 | Status NativeProcessNetBSD::Attach() { |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 784 | // Attach to the requested process. |
| 785 | // An attach will cause the thread to stop with a SIGSTOP. |
Pavel Labath | 96e600f | 2017-07-07 11:02:19 +0000 | [diff] [blame] | 786 | Status status = PtraceWrapper(PT_ATTACH, m_pid); |
| 787 | if (status.Fail()) |
| 788 | return status; |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 789 | |
Pavel Labath | 96e600f | 2017-07-07 11:02:19 +0000 | [diff] [blame] | 790 | int wstatus; |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 791 | // Need to use WALLSIG otherwise we receive an error with errno=ECHLD |
| 792 | // At this point we should have a thread stopped if waitpid succeeds. |
Pavel Labath | 96e600f | 2017-07-07 11:02:19 +0000 | [diff] [blame] | 793 | if ((wstatus = waitpid(m_pid, NULL, WALLSIG)) < 0) |
| 794 | return Status(errno, eErrorTypePOSIX); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 795 | |
| 796 | /* Initialize threads */ |
Pavel Labath | 96e600f | 2017-07-07 11:02:19 +0000 | [diff] [blame] | 797 | status = ReinitializeThreads(); |
| 798 | if (status.Fail()) |
| 799 | return status; |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 800 | |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame^] | 801 | for (const auto &thread : m_threads) |
| 802 | static_cast<NativeThreadNetBSD &>(*thread).SetStoppedBySignal(SIGSTOP); |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 803 | |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 804 | // Let our process instance know the thread has stopped. |
| 805 | SetState(StateType::eStateStopped); |
Pavel Labath | 96e600f | 2017-07-07 11:02:19 +0000 | [diff] [blame] | 806 | return Status(); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 809 | Status NativeProcessNetBSD::ReadMemory(lldb::addr_t addr, void *buf, |
| 810 | size_t size, size_t &bytes_read) { |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 811 | unsigned char *dst = static_cast<unsigned char *>(buf); |
| 812 | struct ptrace_io_desc io; |
| 813 | |
| 814 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); |
| 815 | LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); |
| 816 | |
| 817 | bytes_read = 0; |
| 818 | io.piod_op = PIOD_READ_D; |
| 819 | io.piod_len = size; |
| 820 | |
| 821 | do { |
| 822 | io.piod_offs = (void *)(addr + bytes_read); |
| 823 | io.piod_addr = dst + bytes_read; |
| 824 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 825 | Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 826 | if (error.Fail()) |
| 827 | return error; |
| 828 | |
| 829 | bytes_read = io.piod_len; |
| 830 | io.piod_len = size - bytes_read; |
| 831 | } while (bytes_read < size); |
| 832 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 833 | return Status(); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 836 | Status NativeProcessNetBSD::ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, |
| 837 | size_t size, |
| 838 | size_t &bytes_read) { |
| 839 | Status error = ReadMemory(addr, buf, size, bytes_read); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 840 | if (error.Fail()) |
| 841 | return error; |
| 842 | return m_breakpoint_list.RemoveTrapsFromBuffer(addr, buf, size); |
| 843 | } |
| 844 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 845 | Status NativeProcessNetBSD::WriteMemory(lldb::addr_t addr, const void *buf, |
| 846 | size_t size, size_t &bytes_written) { |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 847 | const unsigned char *src = static_cast<const unsigned char *>(buf); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 848 | Status error; |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 849 | struct ptrace_io_desc io; |
| 850 | |
| 851 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); |
| 852 | LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); |
| 853 | |
| 854 | bytes_written = 0; |
| 855 | io.piod_op = PIOD_WRITE_D; |
| 856 | io.piod_len = size; |
| 857 | |
| 858 | do { |
Kamil Rytarowski | 269eec0 | 2017-05-24 23:59:50 +0000 | [diff] [blame] | 859 | io.piod_addr = const_cast<void *>(static_cast<const void *>(src + bytes_written)); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 860 | io.piod_offs = (void *)(addr + bytes_written); |
| 861 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 862 | Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 863 | if (error.Fail()) |
| 864 | return error; |
| 865 | |
| 866 | bytes_written = io.piod_len; |
| 867 | io.piod_len = size - bytes_written; |
| 868 | } while (bytes_written < size); |
| 869 | |
| 870 | return error; |
| 871 | } |
| 872 | |
| 873 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> |
| 874 | NativeProcessNetBSD::GetAuxvData() const { |
| 875 | /* |
| 876 | * ELF_AUX_ENTRIES is currently restricted to kernel |
| 877 | * (<sys/exec_elf.h> r. 1.155 specifies 15) |
| 878 | * |
| 879 | * ptrace(2) returns the whole AUXV including extra fiels after AT_NULL this |
| 880 | * information isn't needed. |
| 881 | */ |
| 882 | size_t auxv_size = 100 * sizeof(AuxInfo); |
| 883 | |
| 884 | ErrorOr<std::unique_ptr<MemoryBuffer>> buf = |
| 885 | llvm::MemoryBuffer::getNewMemBuffer(auxv_size); |
| 886 | |
Kamil Rytarowski | 269eec0 | 2017-05-24 23:59:50 +0000 | [diff] [blame] | 887 | struct ptrace_io_desc io; |
| 888 | io.piod_op = PIOD_READ_AUXV; |
| 889 | io.piod_offs = 0; |
| 890 | io.piod_addr = const_cast<void *>(static_cast<const void *>(buf.get()->getBufferStart())); |
| 891 | io.piod_len = auxv_size; |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 892 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 893 | Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 894 | |
| 895 | if (error.Fail()) |
| 896 | return std::error_code(error.GetError(), std::generic_category()); |
| 897 | |
| 898 | if (io.piod_len < 1) |
| 899 | return std::error_code(ECANCELED, std::generic_category()); |
| 900 | |
| 901 | return buf; |
| 902 | } |
Kamil Rytarowski | 3eef2b5 | 2017-03-30 20:25:29 +0000 | [diff] [blame] | 903 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 904 | Status NativeProcessNetBSD::ReinitializeThreads() { |
Kamil Rytarowski | 3eef2b5 | 2017-03-30 20:25:29 +0000 | [diff] [blame] | 905 | // Clear old threads |
| 906 | m_threads.clear(); |
| 907 | |
| 908 | // Initialize new thread |
| 909 | struct ptrace_lwpinfo info = {}; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 910 | Status error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info)); |
Kamil Rytarowski | 3eef2b5 | 2017-03-30 20:25:29 +0000 | [diff] [blame] | 911 | if (error.Fail()) { |
| 912 | return error; |
| 913 | } |
| 914 | // Reinitialize from scratch threads and register them in process |
| 915 | while (info.pl_lwpid != 0) { |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame^] | 916 | AddThread(info.pl_lwpid); |
Kamil Rytarowski | 3eef2b5 | 2017-03-30 20:25:29 +0000 | [diff] [blame] | 917 | error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info)); |
| 918 | if (error.Fail()) { |
| 919 | return error; |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | return error; |
| 924 | } |