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