Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1 | //===-- NativeProcessLinux.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 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 10 | #include "NativeProcessLinux.h" |
| 11 | |
| 12 | // C Includes |
| 13 | #include <errno.h> |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 14 | #include <stdint.h> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 15 | #include <string.h> |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 16 | #include <unistd.h> |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 17 | |
| 18 | // C++ Includes |
| 19 | #include <fstream> |
Pavel Labath | df7c699 | 2015-06-17 18:38:49 +0000 | [diff] [blame] | 20 | #include <mutex> |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 21 | #include <sstream> |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 22 | #include <string> |
Pavel Labath | 5b981ab | 2015-05-29 12:53:54 +0000 | [diff] [blame] | 23 | #include <unordered_map> |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 24 | |
| 25 | // Other libraries and framework includes |
Tamas Berghammer | d8c338d | 2015-04-15 09:47:02 +0000 | [diff] [blame] | 26 | #include "lldb/Core/EmulateInstruction.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 27 | #include "lldb/Core/Error.h" |
Oleksiy Vyalov | 6edef20 | 2014-11-17 22:16:42 +0000 | [diff] [blame] | 28 | #include "lldb/Core/ModuleSpec.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 29 | #include "lldb/Core/RegisterValue.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 30 | #include "lldb/Core/State.h" |
| 31 | #include "lldb/Host/Host.h" |
Pavel Labath | 5ad891f | 2016-07-21 14:54:03 +0000 | [diff] [blame] | 32 | #include "lldb/Host/HostProcess.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 33 | #include "lldb/Host/ThreadLauncher.h" |
Pavel Labath | 2a86b55 | 2016-06-14 17:30:52 +0000 | [diff] [blame] | 34 | #include "lldb/Host/common/NativeBreakpoint.h" |
| 35 | #include "lldb/Host/common/NativeRegisterContext.h" |
Pavel Labath | 5ad891f | 2016-07-21 14:54:03 +0000 | [diff] [blame] | 36 | #include "lldb/Host/linux/ProcessLauncherLinux.h" |
Pavel Labath | 2a86b55 | 2016-06-14 17:30:52 +0000 | [diff] [blame] | 37 | #include "lldb/Symbol/ObjectFile.h" |
Zachary Turner | 90aff47 | 2015-03-03 23:36:51 +0000 | [diff] [blame] | 38 | #include "lldb/Target/Process.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 39 | #include "lldb/Target/ProcessLaunchInfo.h" |
Pavel Labath | 5b981ab | 2015-05-29 12:53:54 +0000 | [diff] [blame] | 40 | #include "lldb/Target/Target.h" |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 41 | #include "lldb/Utility/LLDBAssert.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 42 | #include "lldb/Utility/PseudoTerminal.h" |
Pavel Labath | f805e19 | 2015-07-07 10:08:41 +0000 | [diff] [blame] | 43 | #include "lldb/Utility/StringExtractor.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 44 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 45 | #include "NativeThreadLinux.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 46 | #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 47 | #include "ProcFileReader.h" |
Tamas Berghammer | 1e209fc | 2015-03-13 11:36:47 +0000 | [diff] [blame] | 48 | #include "Procfs.h" |
Todd Fiala | cacde7d | 2014-09-27 16:54:22 +0000 | [diff] [blame] | 49 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 50 | // System includes - They have to be included after framework includes because |
| 51 | // they define some |
Tamas Berghammer | d858487 | 2015-02-06 10:57:40 +0000 | [diff] [blame] | 52 | // macros which collide with variable names in other modules |
| 53 | #include <linux/unistd.h> |
Tamas Berghammer | d858487 | 2015-02-06 10:57:40 +0000 | [diff] [blame] | 54 | #include <sys/socket.h> |
Vince Harron | 8b33567 | 2015-05-12 01:10:56 +0000 | [diff] [blame] | 55 | |
Pavel Labath | df7c699 | 2015-06-17 18:38:49 +0000 | [diff] [blame] | 56 | #include <sys/syscall.h> |
Tamas Berghammer | d858487 | 2015-02-06 10:57:40 +0000 | [diff] [blame] | 57 | #include <sys/types.h> |
Tamas Berghammer | d858487 | 2015-02-06 10:57:40 +0000 | [diff] [blame] | 58 | #include <sys/user.h> |
| 59 | #include <sys/wait.h> |
| 60 | |
Vince Harron | 8b33567 | 2015-05-12 01:10:56 +0000 | [diff] [blame] | 61 | #include "lldb/Host/linux/Personality.h" |
| 62 | #include "lldb/Host/linux/Ptrace.h" |
Pavel Labath | df7c699 | 2015-06-17 18:38:49 +0000 | [diff] [blame] | 63 | #include "lldb/Host/linux/Uio.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 64 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 65 | // Support hardware breakpoints in case it has not been defined |
| 66 | #ifndef TRAP_HWBKPT |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 67 | #define TRAP_HWBKPT 4 |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 68 | #endif |
| 69 | |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 70 | using namespace lldb; |
| 71 | using namespace lldb_private; |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 72 | using namespace lldb_private::process_linux; |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 73 | using namespace llvm; |
| 74 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 75 | // Private bits we only need internally. |
Pavel Labath | df7c699 | 2015-06-17 18:38:49 +0000 | [diff] [blame] | 76 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 77 | static bool ProcessVmReadvSupported() { |
| 78 | static bool is_supported; |
| 79 | static std::once_flag flag; |
Pavel Labath | df7c699 | 2015-06-17 18:38:49 +0000 | [diff] [blame] | 80 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 81 | std::call_once(flag, [] { |
Pavel Labath | 4abe5d6 | 2016-07-15 10:18:15 +0000 | [diff] [blame] | 82 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Pavel Labath | 4abe5d6 | 2016-07-15 10:18:15 +0000 | [diff] [blame] | 83 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 84 | uint32_t source = 0x47424742; |
| 85 | uint32_t dest = 0; |
Pavel Labath | 4abe5d6 | 2016-07-15 10:18:15 +0000 | [diff] [blame] | 86 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 87 | struct iovec local, remote; |
| 88 | remote.iov_base = &source; |
| 89 | local.iov_base = &dest; |
| 90 | remote.iov_len = local.iov_len = sizeof source; |
Pavel Labath | 4abe5d6 | 2016-07-15 10:18:15 +0000 | [diff] [blame] | 91 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 92 | // We shall try if cross-process-memory reads work by attempting to read a |
| 93 | // value from our own process. |
| 94 | ssize_t res = process_vm_readv(getpid(), &local, 1, &remote, 1, 0); |
| 95 | is_supported = (res == sizeof(source) && source == dest); |
| 96 | if (log) { |
| 97 | if (is_supported) |
| 98 | log->Printf("%s: Detected kernel support for process_vm_readv syscall. " |
| 99 | "Fast memory reads enabled.", |
| 100 | __FUNCTION__); |
| 101 | else |
| 102 | log->Printf("%s: syscall process_vm_readv failed (error: %s). Fast " |
| 103 | "memory reads disabled.", |
| 104 | __FUNCTION__, strerror(errno)); |
| 105 | } |
| 106 | }); |
Pavel Labath | 4abe5d6 | 2016-07-15 10:18:15 +0000 | [diff] [blame] | 107 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 108 | return is_supported; |
Pavel Labath | 4abe5d6 | 2016-07-15 10:18:15 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 111 | namespace { |
| 112 | void MaybeLogLaunchInfo(const ProcessLaunchInfo &info) { |
| 113 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 114 | if (!log) |
| 115 | return; |
| 116 | |
| 117 | if (const FileAction *action = info.GetFileActionForFD(STDIN_FILENO)) |
| 118 | log->Printf("%s: setting STDIN to '%s'", __FUNCTION__, |
| 119 | action->GetFileSpec().GetCString()); |
| 120 | else |
| 121 | log->Printf("%s leaving STDIN as is", __FUNCTION__); |
| 122 | |
| 123 | if (const FileAction *action = info.GetFileActionForFD(STDOUT_FILENO)) |
| 124 | log->Printf("%s setting STDOUT to '%s'", __FUNCTION__, |
| 125 | action->GetFileSpec().GetCString()); |
| 126 | else |
| 127 | log->Printf("%s leaving STDOUT as is", __FUNCTION__); |
| 128 | |
| 129 | if (const FileAction *action = info.GetFileActionForFD(STDERR_FILENO)) |
| 130 | log->Printf("%s setting STDERR to '%s'", __FUNCTION__, |
| 131 | action->GetFileSpec().GetCString()); |
| 132 | else |
| 133 | log->Printf("%s leaving STDERR as is", __FUNCTION__); |
| 134 | |
| 135 | int i = 0; |
| 136 | for (const char **args = info.GetArguments().GetConstArgumentVector(); *args; |
| 137 | ++args, ++i) |
| 138 | log->Printf("%s arg %d: \"%s\"", __FUNCTION__, i, |
| 139 | *args ? *args : "nullptr"); |
Pavel Labath | 4abe5d6 | 2016-07-15 10:18:15 +0000 | [diff] [blame] | 140 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 141 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 142 | void DisplayBytes(StreamString &s, void *bytes, uint32_t count) { |
| 143 | uint8_t *ptr = (uint8_t *)bytes; |
| 144 | const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count); |
| 145 | for (uint32_t i = 0; i < loop_count; i++) { |
| 146 | s.Printf("[%x]", *ptr); |
| 147 | ptr++; |
| 148 | } |
| 149 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 150 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 151 | void PtraceDisplayBytes(int &req, void *data, size_t data_size) { |
| 152 | StreamString buf; |
| 153 | Log *verbose_log(ProcessPOSIXLog::GetLogIfAllCategoriesSet( |
| 154 | POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE)); |
| 155 | |
| 156 | if (verbose_log) { |
| 157 | switch (req) { |
| 158 | case PTRACE_POKETEXT: { |
| 159 | DisplayBytes(buf, &data, 8); |
| 160 | verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData()); |
| 161 | break; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 162 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 163 | case PTRACE_POKEDATA: { |
| 164 | DisplayBytes(buf, &data, 8); |
| 165 | verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData()); |
| 166 | break; |
| 167 | } |
| 168 | case PTRACE_POKEUSER: { |
| 169 | DisplayBytes(buf, &data, 8); |
| 170 | verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData()); |
| 171 | break; |
| 172 | } |
| 173 | case PTRACE_SETREGS: { |
| 174 | DisplayBytes(buf, data, data_size); |
| 175 | verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData()); |
| 176 | break; |
| 177 | } |
| 178 | case PTRACE_SETFPREGS: { |
| 179 | DisplayBytes(buf, data, data_size); |
| 180 | verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData()); |
| 181 | break; |
| 182 | } |
| 183 | case PTRACE_SETSIGINFO: { |
| 184 | DisplayBytes(buf, data, sizeof(siginfo_t)); |
| 185 | verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData()); |
| 186 | break; |
| 187 | } |
| 188 | case PTRACE_SETREGSET: { |
| 189 | // Extract iov_base from data, which is a pointer to the struct IOVEC |
| 190 | DisplayBytes(buf, *(void **)data, data_size); |
| 191 | verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData()); |
| 192 | break; |
| 193 | } |
| 194 | default: {} |
| 195 | } |
| 196 | } |
| 197 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 198 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 199 | static constexpr unsigned k_ptrace_word_size = sizeof(void *); |
| 200 | static_assert(sizeof(long) >= k_ptrace_word_size, |
| 201 | "Size of long must be larger than ptrace word size"); |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 202 | } // end of anonymous namespace |
| 203 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 204 | // Simple helper function to ensure flags are enabled on the given file |
| 205 | // descriptor. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 206 | static Error EnsureFDFlags(int fd, int flags) { |
| 207 | Error error; |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 208 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 209 | int status = fcntl(fd, F_GETFL); |
| 210 | if (status == -1) { |
| 211 | error.SetErrorToErrno(); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 212 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | if (fcntl(fd, F_SETFL, status | flags) == -1) { |
| 216 | error.SetErrorToErrno(); |
| 217 | return error; |
| 218 | } |
| 219 | |
| 220 | return error; |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 223 | // ----------------------------------------------------------------------------- |
| 224 | // Public Static Methods |
| 225 | // ----------------------------------------------------------------------------- |
| 226 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 227 | Error NativeProcessProtocol::Launch( |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 228 | ProcessLaunchInfo &launch_info, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 229 | NativeProcessProtocol::NativeDelegate &native_delegate, MainLoop &mainloop, |
| 230 | NativeProcessProtocolSP &native_process_sp) { |
| 231 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 232 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 233 | Error error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 234 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 235 | // Verify the working directory is valid if one was specified. |
| 236 | FileSpec working_dir{launch_info.GetWorkingDirectory()}; |
| 237 | if (working_dir && |
| 238 | (!working_dir.ResolvePath() || |
| 239 | working_dir.GetFileType() != FileSpec::eFileTypeDirectory)) { |
| 240 | error.SetErrorStringWithFormat("No such file or directory: %s", |
| 241 | working_dir.GetCString()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 242 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | // Create the NativeProcessLinux in launch mode. |
| 246 | native_process_sp.reset(new NativeProcessLinux()); |
| 247 | |
| 248 | if (!native_process_sp->RegisterNativeDelegate(native_delegate)) { |
| 249 | native_process_sp.reset(); |
| 250 | error.SetErrorStringWithFormat("failed to register the native delegate"); |
| 251 | return error; |
| 252 | } |
| 253 | |
| 254 | error = std::static_pointer_cast<NativeProcessLinux>(native_process_sp) |
| 255 | ->LaunchInferior(mainloop, launch_info); |
| 256 | |
| 257 | if (error.Fail()) { |
| 258 | native_process_sp.reset(); |
| 259 | if (log) |
| 260 | log->Printf("NativeProcessLinux::%s failed to launch process: %s", |
| 261 | __FUNCTION__, error.AsCString()); |
| 262 | return error; |
| 263 | } |
| 264 | |
| 265 | launch_info.SetProcessID(native_process_sp->GetID()); |
| 266 | |
| 267 | return error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 270 | Error NativeProcessProtocol::Attach( |
| 271 | lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate, |
| 272 | MainLoop &mainloop, NativeProcessProtocolSP &native_process_sp) { |
| 273 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 274 | if (log && log->GetMask().Test(POSIX_LOG_VERBOSE)) |
| 275 | log->Printf("NativeProcessLinux::%s(pid = %" PRIi64 ")", __FUNCTION__, pid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 276 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 277 | // Retrieve the architecture for the running process. |
| 278 | ArchSpec process_arch; |
| 279 | Error error = ResolveProcessArchitecture(pid, process_arch); |
| 280 | if (!error.Success()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 281 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 282 | |
| 283 | std::shared_ptr<NativeProcessLinux> native_process_linux_sp( |
| 284 | new NativeProcessLinux()); |
| 285 | |
| 286 | if (!native_process_linux_sp->RegisterNativeDelegate(native_delegate)) { |
| 287 | error.SetErrorStringWithFormat("failed to register the native delegate"); |
| 288 | return error; |
| 289 | } |
| 290 | |
| 291 | native_process_linux_sp->AttachToInferior(mainloop, pid, error); |
| 292 | if (!error.Success()) |
| 293 | return error; |
| 294 | |
| 295 | native_process_sp = native_process_linux_sp; |
| 296 | return error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | // ----------------------------------------------------------------------------- |
| 300 | // Public Instance Methods |
| 301 | // ----------------------------------------------------------------------------- |
| 302 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 303 | NativeProcessLinux::NativeProcessLinux() |
| 304 | : NativeProcessProtocol(LLDB_INVALID_PROCESS_ID), m_arch(), |
| 305 | m_supports_mem_region(eLazyBoolCalculate), m_mem_region_cache(), |
| 306 | m_pending_notification_tid(LLDB_INVALID_THREAD_ID) {} |
| 307 | |
| 308 | void NativeProcessLinux::AttachToInferior(MainLoop &mainloop, lldb::pid_t pid, |
| 309 | Error &error) { |
| 310 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 311 | if (log) |
| 312 | log->Printf("NativeProcessLinux::%s (pid = %" PRIi64 ")", __FUNCTION__, |
| 313 | pid); |
| 314 | |
| 315 | m_sigchld_handle = mainloop.RegisterSignal( |
| 316 | SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, error); |
| 317 | if (!m_sigchld_handle) |
| 318 | return; |
| 319 | |
| 320 | error = ResolveProcessArchitecture(pid, m_arch); |
| 321 | if (!error.Success()) |
| 322 | return; |
| 323 | |
| 324 | // Set the architecture to the exe architecture. |
| 325 | if (log) |
| 326 | log->Printf("NativeProcessLinux::%s (pid = %" PRIi64 |
| 327 | ") detected architecture %s", |
| 328 | __FUNCTION__, pid, m_arch.GetArchitectureName()); |
| 329 | |
| 330 | m_pid = pid; |
| 331 | SetState(eStateAttaching); |
| 332 | |
| 333 | Attach(pid, error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 336 | Error NativeProcessLinux::LaunchInferior(MainLoop &mainloop, |
| 337 | ProcessLaunchInfo &launch_info) { |
| 338 | Error error; |
| 339 | m_sigchld_handle = mainloop.RegisterSignal( |
| 340 | SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, error); |
| 341 | if (!m_sigchld_handle) |
| 342 | return error; |
| 343 | |
| 344 | SetState(eStateLaunching); |
| 345 | |
| 346 | MaybeLogLaunchInfo(launch_info); |
| 347 | |
| 348 | ::pid_t pid = |
| 349 | ProcessLauncherLinux().LaunchProcess(launch_info, error).GetProcessId(); |
| 350 | if (error.Fail()) |
| 351 | return error; |
| 352 | |
| 353 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 354 | |
| 355 | // Wait for the child process to trap on its call to execve. |
| 356 | ::pid_t wpid; |
| 357 | int status; |
| 358 | if ((wpid = waitpid(pid, &status, 0)) < 0) { |
| 359 | error.SetErrorToErrno(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 360 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 361 | log->Printf("NativeProcessLinux::%s waitpid for inferior failed with %s", |
| 362 | __FUNCTION__, error.AsCString()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 363 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 364 | // Mark the inferior as invalid. |
| 365 | // FIXME this could really use a new state - eStateLaunchFailure. For now, |
| 366 | // using eStateInvalid. |
| 367 | SetState(StateType::eStateInvalid); |
Pavel Labath | 19cbe96 | 2015-07-21 13:20:32 +0000 | [diff] [blame] | 368 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 369 | return error; |
| 370 | } |
| 371 | assert(WIFSTOPPED(status) && (wpid == static_cast<::pid_t>(pid)) && |
| 372 | "Could not sync with inferior process."); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 373 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 374 | if (log) |
| 375 | log->Printf("NativeProcessLinux::%s inferior started, now in stopped state", |
| 376 | __FUNCTION__); |
| 377 | |
| 378 | error = SetDefaultPtraceOpts(pid); |
| 379 | if (error.Fail()) { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 380 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 381 | log->Printf("NativeProcessLinux::%s inferior failed to set default " |
| 382 | "ptrace options: %s", |
| 383 | __FUNCTION__, error.AsCString()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 384 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 385 | // Mark the inferior as invalid. |
| 386 | // FIXME this could really use a new state - eStateLaunchFailure. For now, |
| 387 | // using eStateInvalid. |
| 388 | SetState(StateType::eStateInvalid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 389 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 390 | return error; |
| 391 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 392 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 393 | // Release the master terminal descriptor and pass it off to the |
| 394 | // NativeProcessLinux instance. Similarly stash the inferior pid. |
| 395 | m_terminal_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor(); |
| 396 | m_pid = pid; |
| 397 | launch_info.SetProcessID(pid); |
Pavel Labath | 4abe5d6 | 2016-07-15 10:18:15 +0000 | [diff] [blame] | 398 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 399 | if (m_terminal_fd != -1) { |
| 400 | error = EnsureFDFlags(m_terminal_fd, O_NONBLOCK); |
| 401 | if (error.Fail()) { |
| 402 | if (log) |
| 403 | log->Printf("NativeProcessLinux::%s inferior EnsureFDFlags failed for " |
| 404 | "ensuring terminal O_NONBLOCK setting: %s", |
Pavel Labath | 5ad891f | 2016-07-21 14:54:03 +0000 | [diff] [blame] | 405 | __FUNCTION__, error.AsCString()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 406 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 407 | // Mark the inferior as invalid. |
| 408 | // FIXME this could really use a new state - eStateLaunchFailure. For |
| 409 | // now, using eStateInvalid. |
| 410 | SetState(StateType::eStateInvalid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 411 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 412 | return error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 413 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 414 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 415 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 416 | if (log) |
| 417 | log->Printf("NativeProcessLinux::%s() adding pid = %" PRIu64, __FUNCTION__, |
| 418 | uint64_t(pid)); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 419 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 420 | ResolveProcessArchitecture(m_pid, m_arch); |
| 421 | NativeThreadLinuxSP thread_sp = AddThread(pid); |
| 422 | assert(thread_sp && "AddThread() returned a nullptr thread"); |
| 423 | thread_sp->SetStoppedBySignal(SIGSTOP); |
| 424 | ThreadWasCreated(*thread_sp); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 425 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 426 | // Let our process instance know the thread has stopped. |
| 427 | SetCurrentThreadID(thread_sp->GetID()); |
| 428 | SetState(StateType::eStateStopped); |
| 429 | |
| 430 | if (log) { |
| 431 | if (error.Success()) |
| 432 | log->Printf("NativeProcessLinux::%s inferior launching succeeded", |
| 433 | __FUNCTION__); |
| 434 | else |
| 435 | log->Printf("NativeProcessLinux::%s inferior launching failed: %s", |
| 436 | __FUNCTION__, error.AsCString()); |
| 437 | } |
| 438 | return error; |
| 439 | } |
| 440 | |
| 441 | ::pid_t NativeProcessLinux::Attach(lldb::pid_t pid, Error &error) { |
| 442 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 443 | |
| 444 | // Use a map to keep track of the threads which we have attached/need to |
| 445 | // attach. |
| 446 | Host::TidMap tids_to_attach; |
| 447 | if (pid <= 1) { |
| 448 | error.SetErrorToGenericError(); |
| 449 | error.SetErrorString("Attaching to process 1 is not allowed."); |
| 450 | return -1; |
| 451 | } |
| 452 | |
| 453 | while (Host::FindProcessThreads(pid, tids_to_attach)) { |
| 454 | for (Host::TidMap::iterator it = tids_to_attach.begin(); |
| 455 | it != tids_to_attach.end();) { |
| 456 | if (it->second == false) { |
| 457 | lldb::tid_t tid = it->first; |
| 458 | |
| 459 | // Attach to the requested process. |
| 460 | // An attach will cause the thread to stop with a SIGSTOP. |
| 461 | error = PtraceWrapper(PTRACE_ATTACH, tid); |
| 462 | if (error.Fail()) { |
| 463 | // No such thread. The thread may have exited. |
| 464 | // More error handling may be needed. |
| 465 | if (error.GetError() == ESRCH) { |
| 466 | it = tids_to_attach.erase(it); |
| 467 | continue; |
| 468 | } else |
| 469 | return -1; |
| 470 | } |
| 471 | |
| 472 | int status; |
| 473 | // Need to use __WALL otherwise we receive an error with errno=ECHLD |
| 474 | // At this point we should have a thread stopped if waitpid succeeds. |
| 475 | if ((status = waitpid(tid, NULL, __WALL)) < 0) { |
| 476 | // No such thread. The thread may have exited. |
| 477 | // More error handling may be needed. |
| 478 | if (errno == ESRCH) { |
| 479 | it = tids_to_attach.erase(it); |
| 480 | continue; |
| 481 | } else { |
| 482 | error.SetErrorToErrno(); |
| 483 | return -1; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | error = SetDefaultPtraceOpts(tid); |
| 488 | if (error.Fail()) |
| 489 | return -1; |
| 490 | |
| 491 | if (log) |
| 492 | log->Printf("NativeProcessLinux::%s() adding tid = %" PRIu64, |
| 493 | __FUNCTION__, tid); |
| 494 | |
| 495 | it->second = true; |
| 496 | |
| 497 | // Create the thread, mark it as stopped. |
| 498 | NativeThreadLinuxSP thread_sp(AddThread(static_cast<lldb::tid_t>(tid))); |
| 499 | assert(thread_sp && "AddThread() returned a nullptr"); |
| 500 | |
| 501 | // This will notify this is a new thread and tell the system it is |
| 502 | // stopped. |
| 503 | thread_sp->SetStoppedBySignal(SIGSTOP); |
| 504 | ThreadWasCreated(*thread_sp); |
| 505 | SetCurrentThreadID(thread_sp->GetID()); |
| 506 | } |
| 507 | |
| 508 | // move the loop forward |
| 509 | ++it; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | if (tids_to_attach.size() > 0) { |
| 514 | m_pid = pid; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 515 | // Let our process instance know the thread has stopped. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 516 | SetState(StateType::eStateStopped); |
| 517 | } else { |
| 518 | error.SetErrorToGenericError(); |
| 519 | error.SetErrorString("No such process."); |
| 520 | return -1; |
| 521 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 522 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 523 | return pid; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 526 | Error NativeProcessLinux::SetDefaultPtraceOpts(lldb::pid_t pid) { |
| 527 | long ptrace_opts = 0; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 528 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 529 | // Have the child raise an event on exit. This is used to keep the child in |
| 530 | // limbo until it is destroyed. |
| 531 | ptrace_opts |= PTRACE_O_TRACEEXIT; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 532 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 533 | // Have the tracer trace threads which spawn in the inferior process. |
| 534 | // TODO: if we want to support tracing the inferiors' child, add the |
| 535 | // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK) |
| 536 | ptrace_opts |= PTRACE_O_TRACECLONE; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 537 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 538 | // Have the tracer notify us before execve returns |
| 539 | // (needed to disable legacy SIGTRAP generation) |
| 540 | ptrace_opts |= PTRACE_O_TRACEEXEC; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 541 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 542 | return PtraceWrapper(PTRACE_SETOPTIONS, pid, nullptr, (void *)ptrace_opts); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 545 | static ExitType convert_pid_status_to_exit_type(int status) { |
| 546 | if (WIFEXITED(status)) |
| 547 | return ExitType::eExitTypeExit; |
| 548 | else if (WIFSIGNALED(status)) |
| 549 | return ExitType::eExitTypeSignal; |
| 550 | else if (WIFSTOPPED(status)) |
| 551 | return ExitType::eExitTypeStop; |
| 552 | else { |
| 553 | // We don't know what this is. |
| 554 | return ExitType::eExitTypeInvalid; |
| 555 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 558 | static int convert_pid_status_to_return_code(int status) { |
| 559 | if (WIFEXITED(status)) |
| 560 | return WEXITSTATUS(status); |
| 561 | else if (WIFSIGNALED(status)) |
| 562 | return WTERMSIG(status); |
| 563 | else if (WIFSTOPPED(status)) |
| 564 | return WSTOPSIG(status); |
| 565 | else { |
| 566 | // We don't know what this is. |
| 567 | return ExitType::eExitTypeInvalid; |
| 568 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 571 | // Handles all waitpid events from the inferior process. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 572 | void NativeProcessLinux::MonitorCallback(lldb::pid_t pid, bool exited, |
| 573 | int signal, int status) { |
| 574 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 575 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 576 | // Certain activities differ based on whether the pid is the tid of the main |
| 577 | // thread. |
| 578 | const bool is_main_thread = (pid == GetID()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 579 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 580 | // Handle when the thread exits. |
| 581 | if (exited) { |
| 582 | if (log) |
| 583 | log->Printf( |
| 584 | "NativeProcessLinux::%s() got exit signal(%d) , tid = %" PRIu64 |
| 585 | " (%s main thread)", |
| 586 | __FUNCTION__, signal, pid, is_main_thread ? "is" : "is not"); |
| 587 | |
| 588 | // This is a thread that exited. Ensure we're not tracking it anymore. |
| 589 | const bool thread_found = StopTrackingThread(pid); |
| 590 | |
| 591 | if (is_main_thread) { |
| 592 | // We only set the exit status and notify the delegate if we haven't |
| 593 | // already set the process |
| 594 | // state to an exited state. We normally should have received a SIGTRAP | |
| 595 | // (PTRACE_EVENT_EXIT << 8) |
| 596 | // for the main thread. |
| 597 | const bool already_notified = (GetState() == StateType::eStateExited) || |
| 598 | (GetState() == StateType::eStateCrashed); |
| 599 | if (!already_notified) { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 600 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 601 | log->Printf("NativeProcessLinux::%s() tid = %" PRIu64 |
| 602 | " handling main thread exit (%s), expected exit state " |
| 603 | "already set but state was %s instead, setting exit " |
| 604 | "state now", |
| 605 | __FUNCTION__, pid, |
| 606 | thread_found ? "stopped tracking thread metadata" |
| 607 | : "thread metadata not found", |
| 608 | StateAsCString(GetState())); |
| 609 | // The main thread exited. We're done monitoring. Report to delegate. |
| 610 | SetExitStatus(convert_pid_status_to_exit_type(status), |
| 611 | convert_pid_status_to_return_code(status), nullptr, true); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 612 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 613 | // Notify delegate that our process has exited. |
| 614 | SetState(StateType::eStateExited, true); |
| 615 | } else { |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 616 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 617 | log->Printf("NativeProcessLinux::%s() tid = %" PRIu64 |
| 618 | " main thread now exited (%s)", |
| 619 | __FUNCTION__, pid, |
| 620 | thread_found ? "stopped tracking thread metadata" |
| 621 | : "thread metadata not found"); |
| 622 | } |
| 623 | } else { |
| 624 | // Do we want to report to the delegate in this case? I think not. If |
| 625 | // this was an orderly |
| 626 | // thread exit, we would already have received the SIGTRAP | |
| 627 | // (PTRACE_EVENT_EXIT << 8) signal, |
| 628 | // and we would have done an all-stop then. |
| 629 | if (log) |
| 630 | log->Printf("NativeProcessLinux::%s() tid = %" PRIu64 |
| 631 | " handling non-main thread exit (%s)", |
| 632 | __FUNCTION__, pid, |
| 633 | thread_found ? "stopped tracking thread metadata" |
| 634 | : "thread metadata not found"); |
| 635 | } |
| 636 | return; |
| 637 | } |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 638 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 639 | siginfo_t info; |
| 640 | const auto info_err = GetSignalInfo(pid, &info); |
| 641 | auto thread_sp = GetThreadByID(pid); |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 642 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 643 | if (!thread_sp) { |
| 644 | // Normally, the only situation when we cannot find the thread is if we have |
| 645 | // just |
| 646 | // received a new thread notification. This is indicated by GetSignalInfo() |
| 647 | // returning |
| 648 | // si_code == SI_USER and si_pid == 0 |
| 649 | if (log) |
| 650 | log->Printf("NativeProcessLinux::%s received notification about an " |
| 651 | "unknown tid %" PRIu64 ".", |
| 652 | __FUNCTION__, pid); |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 653 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 654 | if (info_err.Fail()) { |
| 655 | if (log) |
| 656 | log->Printf("NativeProcessLinux::%s (tid %" PRIu64 |
| 657 | ") GetSignalInfo failed (%s). Ingoring this notification.", |
| 658 | __FUNCTION__, pid, info_err.AsCString()); |
| 659 | return; |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 662 | if (log && (info.si_code != SI_USER || info.si_pid != 0)) |
| 663 | log->Printf("NativeProcessLinux::%s (tid %" PRIu64 |
| 664 | ") unexpected signal info (si_code: %d, si_pid: %d). " |
| 665 | "Treating as a new thread notification anyway.", |
| 666 | __FUNCTION__, pid, info.si_code, info.si_pid); |
| 667 | |
| 668 | auto thread_sp = AddThread(pid); |
| 669 | // Resume the newly created thread. |
| 670 | ResumeThread(*thread_sp, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER); |
| 671 | ThreadWasCreated(*thread_sp); |
| 672 | return; |
| 673 | } |
| 674 | |
| 675 | // Get details on the signal raised. |
| 676 | if (info_err.Success()) { |
| 677 | // We have retrieved the signal info. Dispatch appropriately. |
| 678 | if (info.si_signo == SIGTRAP) |
| 679 | MonitorSIGTRAP(info, *thread_sp); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 680 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 681 | MonitorSignal(info, *thread_sp, exited); |
| 682 | } else { |
| 683 | if (info_err.GetError() == EINVAL) { |
| 684 | // This is a group stop reception for this tid. |
| 685 | // We can reach here if we reinject SIGSTOP, SIGSTP, SIGTTIN or SIGTTOU |
| 686 | // into the |
| 687 | // tracee, triggering the group-stop mechanism. Normally receiving these |
| 688 | // would stop |
| 689 | // the process, pending a SIGCONT. Simulating this state in a debugger is |
| 690 | // hard and is |
| 691 | // generally not needed (one use case is debugging background task being |
| 692 | // managed by a |
| 693 | // shell). For general use, it is sufficient to stop the process in a |
| 694 | // signal-delivery |
| 695 | // stop which happens before the group stop. This done by MonitorSignal |
| 696 | // and works |
| 697 | // correctly for all signals. |
| 698 | if (log) |
| 699 | log->Printf( |
| 700 | "NativeProcessLinux::%s received a group stop for pid %" PRIu64 |
| 701 | " tid %" PRIu64 ". Transparent handling of group stops not " |
| 702 | "supported, resuming the thread.", |
| 703 | __FUNCTION__, GetID(), pid); |
| 704 | ResumeThread(*thread_sp, thread_sp->GetState(), |
| 705 | LLDB_INVALID_SIGNAL_NUMBER); |
| 706 | } else { |
| 707 | // ptrace(GETSIGINFO) failed (but not due to group-stop). |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 708 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 709 | // A return value of ESRCH means the thread/process is no longer on the |
| 710 | // system, |
| 711 | // so it was killed somehow outside of our control. Either way, we can't |
| 712 | // do anything |
| 713 | // with it anymore. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 714 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 715 | // Stop tracking the metadata for the thread since it's entirely off the |
| 716 | // system now. |
| 717 | const bool thread_found = StopTrackingThread(pid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 718 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 719 | if (log) |
| 720 | log->Printf( |
| 721 | "NativeProcessLinux::%s GetSignalInfo failed: %s, tid = %" PRIu64 |
| 722 | ", signal = %d, status = %d (%s, %s, %s)", |
| 723 | __FUNCTION__, info_err.AsCString(), pid, signal, status, |
| 724 | info_err.GetError() == ESRCH ? "thread/process killed" |
| 725 | : "unknown reason", |
| 726 | is_main_thread ? "is main thread" : "is not main thread", |
| 727 | thread_found ? "thread metadata removed" |
| 728 | : "thread metadata not found"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 729 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 730 | if (is_main_thread) { |
| 731 | // Notify the delegate - our process is not available but appears to |
| 732 | // have been killed outside |
| 733 | // our control. Is eStateExited the right exit state in this case? |
| 734 | SetExitStatus(convert_pid_status_to_exit_type(status), |
| 735 | convert_pid_status_to_return_code(status), nullptr, true); |
| 736 | SetState(StateType::eStateExited, true); |
| 737 | } else { |
| 738 | // This thread was pulled out from underneath us. Anything to do here? |
| 739 | // Do we want to do an all stop? |
| 740 | if (log) |
| 741 | log->Printf("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 |
| 742 | " non-main thread exit occurred, didn't tell delegate " |
| 743 | "anything since thread disappeared out from underneath " |
| 744 | "us", |
| 745 | __FUNCTION__, GetID(), pid); |
| 746 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 747 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 748 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 751 | void NativeProcessLinux::WaitForNewThread(::pid_t tid) { |
| 752 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Pavel Labath | 426bdf8 | 2015-04-28 07:51:52 +0000 | [diff] [blame] | 753 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 754 | NativeThreadLinuxSP new_thread_sp = GetThreadByID(tid); |
Pavel Labath | 426bdf8 | 2015-04-28 07:51:52 +0000 | [diff] [blame] | 755 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 756 | if (new_thread_sp) { |
| 757 | // We are already tracking the thread - we got the event on the new thread |
| 758 | // (see |
| 759 | // MonitorSignal) before this one. We are done. |
| 760 | return; |
| 761 | } |
Pavel Labath | 426bdf8 | 2015-04-28 07:51:52 +0000 | [diff] [blame] | 762 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 763 | // The thread is not tracked yet, let's wait for it to appear. |
| 764 | int status = -1; |
| 765 | ::pid_t wait_pid; |
| 766 | do { |
Pavel Labath | 426bdf8 | 2015-04-28 07:51:52 +0000 | [diff] [blame] | 767 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 768 | log->Printf("NativeProcessLinux::%s() received thread creation event for " |
| 769 | "tid %" PRIu32 |
| 770 | ". tid not tracked yet, waiting for thread to appear...", |
| 771 | __FUNCTION__, tid); |
| 772 | wait_pid = waitpid(tid, &status, __WALL); |
| 773 | } while (wait_pid == -1 && errno == EINTR); |
| 774 | // Since we are waiting on a specific tid, this must be the creation event. |
| 775 | // But let's do |
| 776 | // some checks just in case. |
| 777 | if (wait_pid != tid) { |
| 778 | if (log) |
| 779 | log->Printf( |
| 780 | "NativeProcessLinux::%s() waiting for tid %" PRIu32 |
| 781 | " failed. Assuming the thread has disappeared in the meantime", |
| 782 | __FUNCTION__, tid); |
| 783 | // The only way I know of this could happen is if the whole process was |
| 784 | // SIGKILLed in the mean time. In any case, we can't do anything about that |
| 785 | // now. |
| 786 | return; |
| 787 | } |
| 788 | if (WIFEXITED(status)) { |
| 789 | if (log) |
| 790 | log->Printf("NativeProcessLinux::%s() waiting for tid %" PRIu32 |
| 791 | " returned an 'exited' event. Not tracking the thread.", |
| 792 | __FUNCTION__, tid); |
| 793 | // Also a very improbable event. |
| 794 | return; |
| 795 | } |
Pavel Labath | 426bdf8 | 2015-04-28 07:51:52 +0000 | [diff] [blame] | 796 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 797 | siginfo_t info; |
| 798 | Error error = GetSignalInfo(tid, &info); |
| 799 | if (error.Fail()) { |
| 800 | if (log) |
| 801 | log->Printf( |
| 802 | "NativeProcessLinux::%s() GetSignalInfo for tid %" PRIu32 |
| 803 | " failed. Assuming the thread has disappeared in the meantime.", |
| 804 | __FUNCTION__, tid); |
| 805 | return; |
| 806 | } |
| 807 | |
| 808 | if (((info.si_pid != 0) || (info.si_code != SI_USER)) && log) { |
| 809 | // We should be getting a thread creation signal here, but we received |
| 810 | // something |
| 811 | // else. There isn't much we can do about it now, so we will just log that. |
| 812 | // Since the |
| 813 | // thread is alive and we are receiving events from it, we shall pretend |
| 814 | // that it was |
| 815 | // created properly. |
| 816 | log->Printf("NativeProcessLinux::%s() GetSignalInfo for tid %" PRIu32 |
| 817 | " received unexpected signal with code %d from pid %d.", |
| 818 | __FUNCTION__, tid, info.si_code, info.si_pid); |
| 819 | } |
| 820 | |
| 821 | if (log) |
| 822 | log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 |
| 823 | ": tracking new thread tid %" PRIu32, |
| 824 | __FUNCTION__, GetID(), tid); |
| 825 | |
| 826 | new_thread_sp = AddThread(tid); |
| 827 | ResumeThread(*new_thread_sp, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER); |
| 828 | ThreadWasCreated(*new_thread_sp); |
Pavel Labath | 426bdf8 | 2015-04-28 07:51:52 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 831 | void NativeProcessLinux::MonitorSIGTRAP(const siginfo_t &info, |
| 832 | NativeThreadLinux &thread) { |
| 833 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 834 | const bool is_main_thread = (thread.GetID() == GetID()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 835 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 836 | assert(info.si_signo == SIGTRAP && "Unexpected child signal!"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 837 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 838 | switch (info.si_code) { |
| 839 | // TODO: these two cases are required if we want to support tracing of the |
| 840 | // inferiors' children. We'd need this to debug a monitor. |
| 841 | // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)): |
| 842 | // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)): |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 843 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 844 | case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)): { |
| 845 | // This is the notification on the parent thread which informs us of new |
| 846 | // thread |
| 847 | // creation. |
| 848 | // We don't want to do anything with the parent thread so we just resume it. |
| 849 | // In case we |
| 850 | // want to implement "break on thread creation" functionality, we would need |
| 851 | // to stop |
| 852 | // here. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 853 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 854 | unsigned long event_message = 0; |
| 855 | if (GetEventMessage(thread.GetID(), &event_message).Fail()) { |
| 856 | if (log) |
| 857 | log->Printf("NativeProcessLinux::%s() pid %" PRIu64 |
| 858 | " received thread creation event but GetEventMessage " |
| 859 | "failed so we don't know the new tid", |
| 860 | __FUNCTION__, thread.GetID()); |
| 861 | } else |
| 862 | WaitForNewThread(event_message); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 863 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 864 | ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER); |
| 865 | break; |
| 866 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 867 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 868 | case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)): { |
| 869 | NativeThreadLinuxSP main_thread_sp; |
| 870 | if (log) |
| 871 | log->Printf("NativeProcessLinux::%s() received exec event, code = %d", |
| 872 | __FUNCTION__, info.si_code ^ SIGTRAP); |
| 873 | |
| 874 | // Exec clears any pending notifications. |
| 875 | m_pending_notification_tid = LLDB_INVALID_THREAD_ID; |
| 876 | |
| 877 | // Remove all but the main thread here. Linux fork creates a new process |
| 878 | // which only copies the main thread. |
| 879 | if (log) |
| 880 | log->Printf("NativeProcessLinux::%s exec received, stop tracking all but " |
| 881 | "main thread", |
| 882 | __FUNCTION__); |
| 883 | |
| 884 | for (auto thread_sp : m_threads) { |
| 885 | const bool is_main_thread = thread_sp && thread_sp->GetID() == GetID(); |
| 886 | if (is_main_thread) { |
| 887 | main_thread_sp = std::static_pointer_cast<NativeThreadLinux>(thread_sp); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 888 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 889 | log->Printf( |
| 890 | "NativeProcessLinux::%s found main thread with tid %" PRIu64 |
| 891 | ", keeping", |
| 892 | __FUNCTION__, main_thread_sp->GetID()); |
| 893 | } else { |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 894 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 895 | log->Printf( |
| 896 | "NativeProcessLinux::%s discarding non-main-thread tid %" PRIu64 |
| 897 | " due to exec", |
| 898 | __FUNCTION__, thread_sp->GetID()); |
| 899 | } |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 900 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 901 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 902 | m_threads.clear(); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 903 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 904 | if (main_thread_sp) { |
| 905 | m_threads.push_back(main_thread_sp); |
| 906 | SetCurrentThreadID(main_thread_sp->GetID()); |
| 907 | main_thread_sp->SetStoppedByExec(); |
| 908 | } else { |
| 909 | SetCurrentThreadID(LLDB_INVALID_THREAD_ID); |
| 910 | if (log) |
| 911 | log->Printf("NativeProcessLinux::%s pid %" PRIu64 |
| 912 | "no main thread found, discarded all threads, we're in a " |
| 913 | "no-thread state!", |
| 914 | __FUNCTION__, GetID()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 917 | // Tell coordinator about about the "new" (since exec) stopped main thread. |
| 918 | ThreadWasCreated(*main_thread_sp); |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 919 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 920 | // Let our delegate know we have just exec'd. |
| 921 | NotifyDidExec(); |
| 922 | |
| 923 | // If we have a main thread, indicate we are stopped. |
| 924 | assert(main_thread_sp && "exec called during ptraced process but no main " |
| 925 | "thread metadata tracked"); |
| 926 | |
| 927 | // Let the process know we're stopped. |
| 928 | StopRunningThreads(main_thread_sp->GetID()); |
| 929 | |
| 930 | break; |
| 931 | } |
| 932 | |
| 933 | case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)): { |
| 934 | // The inferior process or one of its threads is about to exit. |
| 935 | // We don't want to do anything with the thread so we just resume it. In |
| 936 | // case we |
| 937 | // want to implement "break on thread exit" functionality, we would need to |
| 938 | // stop |
| 939 | // here. |
| 940 | |
| 941 | unsigned long data = 0; |
| 942 | if (GetEventMessage(thread.GetID(), &data).Fail()) |
| 943 | data = -1; |
| 944 | |
| 945 | if (log) { |
| 946 | log->Printf("NativeProcessLinux::%s() received PTRACE_EVENT_EXIT, data = " |
| 947 | "%lx (WIFEXITED=%s,WIFSIGNALED=%s), pid = %" PRIu64 " (%s)", |
| 948 | __FUNCTION__, data, WIFEXITED(data) ? "true" : "false", |
| 949 | WIFSIGNALED(data) ? "true" : "false", thread.GetID(), |
| 950 | is_main_thread ? "is main thread" : "not main thread"); |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 951 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 952 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 953 | if (is_main_thread) { |
| 954 | SetExitStatus(convert_pid_status_to_exit_type(data), |
| 955 | convert_pid_status_to_return_code(data), nullptr, true); |
| 956 | } |
| 957 | |
| 958 | StateType state = thread.GetState(); |
| 959 | if (!StateIsRunningState(state)) { |
| 960 | // Due to a kernel bug, we may sometimes get this stop after the inferior |
| 961 | // gets a |
| 962 | // SIGKILL. This confuses our state tracking logic in ResumeThread(), |
| 963 | // since normally, |
| 964 | // we should not be receiving any ptrace events while the inferior is |
| 965 | // stopped. This |
| 966 | // makes sure that the inferior is resumed and exits normally. |
| 967 | state = eStateRunning; |
| 968 | } |
| 969 | ResumeThread(thread, state, LLDB_INVALID_SIGNAL_NUMBER); |
| 970 | |
| 971 | break; |
| 972 | } |
| 973 | |
| 974 | case 0: |
| 975 | case TRAP_TRACE: // We receive this on single stepping. |
| 976 | case TRAP_HWBKPT: // We receive this on watchpoint hit |
| 977 | { |
| 978 | // If a watchpoint was hit, report it |
| 979 | uint32_t wp_index; |
| 980 | Error error = thread.GetRegisterContext()->GetWatchpointHitIndex( |
| 981 | wp_index, (uintptr_t)info.si_addr); |
| 982 | if (error.Fail() && log) |
| 983 | log->Printf("NativeProcessLinux::%s() " |
| 984 | "received error while checking for watchpoint hits, " |
| 985 | "pid = %" PRIu64 " error = %s", |
| 986 | __FUNCTION__, thread.GetID(), error.AsCString()); |
| 987 | if (wp_index != LLDB_INVALID_INDEX32) { |
| 988 | MonitorWatchpoint(thread, wp_index); |
| 989 | break; |
| 990 | } |
| 991 | |
| 992 | // Otherwise, report step over |
| 993 | MonitorTrace(thread); |
| 994 | break; |
| 995 | } |
| 996 | |
| 997 | case SI_KERNEL: |
Mohit K. Bhakkad | 3579996 | 2015-06-18 04:53:18 +0000 | [diff] [blame] | 998 | #if defined __mips__ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 999 | // For mips there is no special signal for watchpoint |
| 1000 | // So we check for watchpoint in kernel trap |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 1001 | { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1002 | // If a watchpoint was hit, report it |
| 1003 | uint32_t wp_index; |
| 1004 | Error error = thread.GetRegisterContext()->GetWatchpointHitIndex( |
| 1005 | wp_index, LLDB_INVALID_ADDRESS); |
| 1006 | if (error.Fail() && log) |
| 1007 | log->Printf("NativeProcessLinux::%s() " |
| 1008 | "received error while checking for watchpoint hits, " |
| 1009 | "pid = %" PRIu64 " error = %s", |
| 1010 | __FUNCTION__, thread.GetID(), error.AsCString()); |
| 1011 | if (wp_index != LLDB_INVALID_INDEX32) { |
| 1012 | MonitorWatchpoint(thread, wp_index); |
| 1013 | break; |
| 1014 | } |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 1015 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1016 | // NO BREAK |
Mohit K. Bhakkad | 3579996 | 2015-06-18 04:53:18 +0000 | [diff] [blame] | 1017 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1018 | case TRAP_BRKPT: |
| 1019 | MonitorBreakpoint(thread); |
| 1020 | break; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1021 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1022 | case SIGTRAP: |
| 1023 | case (SIGTRAP | 0x80): |
| 1024 | if (log) |
| 1025 | log->Printf("NativeProcessLinux::%s() received unknown SIGTRAP system " |
| 1026 | "call stop event, pid %" PRIu64 "tid %" PRIu64 ", resuming", |
| 1027 | __FUNCTION__, GetID(), thread.GetID()); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 1028 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1029 | // Ignore these signals until we know more about them. |
| 1030 | ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER); |
| 1031 | break; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1032 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1033 | default: |
| 1034 | assert(false && "Unexpected SIGTRAP code!"); |
| 1035 | if (log) |
| 1036 | log->Printf("NativeProcessLinux::%s() pid %" PRIu64 "tid %" PRIu64 |
| 1037 | " received unhandled SIGTRAP code: 0x%d", |
| 1038 | __FUNCTION__, GetID(), thread.GetID(), info.si_code); |
| 1039 | break; |
| 1040 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1043 | void NativeProcessLinux::MonitorTrace(NativeThreadLinux &thread) { |
| 1044 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 1045 | if (log) |
| 1046 | log->Printf("NativeProcessLinux::%s() received trace event, pid = %" PRIu64 |
| 1047 | " (single stepping)", |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 1048 | __FUNCTION__, thread.GetID()); |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 1049 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1050 | // This thread is currently stopped. |
| 1051 | thread.SetStoppedByTrace(); |
| 1052 | |
| 1053 | StopRunningThreads(thread.GetID()); |
| 1054 | } |
| 1055 | |
| 1056 | void NativeProcessLinux::MonitorBreakpoint(NativeThreadLinux &thread) { |
| 1057 | Log *log( |
| 1058 | GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); |
| 1059 | if (log) |
| 1060 | log->Printf( |
| 1061 | "NativeProcessLinux::%s() received breakpoint event, pid = %" PRIu64, |
| 1062 | __FUNCTION__, thread.GetID()); |
| 1063 | |
| 1064 | // Mark the thread as stopped at breakpoint. |
| 1065 | thread.SetStoppedByBreakpoint(); |
| 1066 | Error error = FixupBreakpointPCAsNeeded(thread); |
| 1067 | if (error.Fail()) |
| 1068 | if (log) |
| 1069 | log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 " fixup: %s", |
| 1070 | __FUNCTION__, thread.GetID(), error.AsCString()); |
| 1071 | |
| 1072 | if (m_threads_stepping_with_breakpoint.find(thread.GetID()) != |
| 1073 | m_threads_stepping_with_breakpoint.end()) |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 1074 | thread.SetStoppedByTrace(); |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 1075 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1076 | StopRunningThreads(thread.GetID()); |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1079 | void NativeProcessLinux::MonitorWatchpoint(NativeThreadLinux &thread, |
| 1080 | uint32_t wp_index) { |
| 1081 | Log *log( |
| 1082 | GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_WATCHPOINTS)); |
| 1083 | if (log) |
| 1084 | log->Printf("NativeProcessLinux::%s() received watchpoint event, " |
| 1085 | "pid = %" PRIu64 ", wp_index = %" PRIu32, |
| 1086 | __FUNCTION__, thread.GetID(), wp_index); |
| 1087 | |
| 1088 | // Mark the thread as stopped at watchpoint. |
| 1089 | // The address is at (lldb::addr_t)info->si_addr if we need it. |
| 1090 | thread.SetStoppedByWatchpoint(wp_index); |
| 1091 | |
| 1092 | // We need to tell all other running threads before we notify the delegate |
| 1093 | // about this stop. |
| 1094 | StopRunningThreads(thread.GetID()); |
| 1095 | } |
| 1096 | |
| 1097 | void NativeProcessLinux::MonitorSignal(const siginfo_t &info, |
| 1098 | NativeThreadLinux &thread, bool exited) { |
| 1099 | const int signo = info.si_signo; |
| 1100 | const bool is_from_llgs = info.si_pid == getpid(); |
| 1101 | |
| 1102 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 1103 | |
| 1104 | // POSIX says that process behaviour is undefined after it ignores a SIGFPE, |
| 1105 | // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a |
| 1106 | // kill(2) or raise(3). Similarly for tgkill(2) on Linux. |
| 1107 | // |
| 1108 | // IOW, user generated signals never generate what we consider to be a |
| 1109 | // "crash". |
| 1110 | // |
| 1111 | // Similarly, ACK signals generated by this monitor. |
| 1112 | |
| 1113 | // Handle the signal. |
| 1114 | if (info.si_code == SI_TKILL || info.si_code == SI_USER) { |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 1115 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1116 | log->Printf("NativeProcessLinux::%s() received signal %s (%d) with code " |
| 1117 | "%s, (siginfo pid = %d (%s), waitpid pid = %" PRIu64 ")", |
| 1118 | __FUNCTION__, Host::GetSignalAsCString(signo), signo, |
| 1119 | (info.si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"), |
| 1120 | info.si_pid, is_from_llgs ? "from llgs" : "not from llgs", |
| 1121 | thread.GetID()); |
| 1122 | } |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 1123 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1124 | // Check for thread stop notification. |
| 1125 | if (is_from_llgs && (info.si_code == SI_TKILL) && (signo == SIGSTOP)) { |
| 1126 | // This is a tgkill()-based stop. |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 1127 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1128 | log->Printf("NativeProcessLinux::%s() pid %" PRIu64 " tid %" PRIu64 |
| 1129 | ", thread stopped", |
| 1130 | __FUNCTION__, GetID(), thread.GetID()); |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 1131 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1132 | // Check that we're not already marked with a stop reason. |
| 1133 | // Note this thread really shouldn't already be marked as stopped - if we |
| 1134 | // were, that would imply that |
| 1135 | // the kernel signaled us with the thread stopping which we handled and |
| 1136 | // marked as stopped, |
| 1137 | // and that, without an intervening resume, we received another stop. It is |
| 1138 | // more likely |
| 1139 | // that we are missing the marking of a run state somewhere if we find that |
| 1140 | // the thread was |
| 1141 | // marked as stopped. |
| 1142 | const StateType thread_state = thread.GetState(); |
| 1143 | if (!StateIsStoppedState(thread_state, false)) { |
| 1144 | // An inferior thread has stopped because of a SIGSTOP we have sent it. |
| 1145 | // Generally, these are not important stops and we don't want to report |
| 1146 | // them as |
| 1147 | // they are just used to stop other threads when one thread (the one with |
| 1148 | // the |
| 1149 | // *real* stop reason) hits a breakpoint (watchpoint, etc...). However, in |
| 1150 | // the |
| 1151 | // case of an asynchronous Interrupt(), this *is* the real stop reason, so |
| 1152 | // we |
| 1153 | // leave the signal intact if this is the thread that was chosen as the |
| 1154 | // triggering thread. |
| 1155 | if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID) { |
| 1156 | if (m_pending_notification_tid == thread.GetID()) |
| 1157 | thread.SetStoppedBySignal(SIGSTOP, &info); |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 1158 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1159 | thread.SetStoppedWithNoReason(); |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 1160 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1161 | SetCurrentThreadID(thread.GetID()); |
| 1162 | SignalIfAllThreadsStopped(); |
| 1163 | } else { |
| 1164 | // We can end up here if stop was initiated by LLGS but by this time a |
| 1165 | // thread stop has occurred - maybe initiated by another event. |
| 1166 | Error error = ResumeThread(thread, thread.GetState(), 0); |
| 1167 | if (error.Fail() && log) { |
| 1168 | log->Printf( |
| 1169 | "NativeProcessLinux::%s failed to resume thread tid %" PRIu64 |
| 1170 | ": %s", |
| 1171 | __FUNCTION__, thread.GetID(), error.AsCString()); |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 1172 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1173 | } |
| 1174 | } else { |
| 1175 | if (log) { |
| 1176 | // Retrieve the signal name if the thread was stopped by a signal. |
| 1177 | int stop_signo = 0; |
| 1178 | const bool stopped_by_signal = thread.IsStopped(&stop_signo); |
| 1179 | const char *signal_name = stopped_by_signal |
| 1180 | ? Host::GetSignalAsCString(stop_signo) |
| 1181 | : "<not stopped by signal>"; |
| 1182 | if (!signal_name) |
| 1183 | signal_name = "<no-signal-name>"; |
Todd Fiala | 58a2f66 | 2014-08-12 17:02:07 +0000 | [diff] [blame] | 1184 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1185 | log->Printf("NativeProcessLinux::%s() pid %" PRIu64 " tid %" PRIu64 |
| 1186 | ", thread was already marked as a stopped state (state=%s, " |
| 1187 | "signal=%d (%s)), leaving stop signal as is", |
| 1188 | __FUNCTION__, GetID(), thread.GetID(), |
| 1189 | StateAsCString(thread_state), stop_signo, signal_name); |
| 1190 | } |
| 1191 | SignalIfAllThreadsStopped(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1194 | // Done handling. |
| 1195 | return; |
| 1196 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1197 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1198 | if (log) |
| 1199 | log->Printf("NativeProcessLinux::%s() received signal %s", __FUNCTION__, |
| 1200 | Host::GetSignalAsCString(signo)); |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 1201 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1202 | // This thread is stopped. |
| 1203 | thread.SetStoppedBySignal(signo, &info); |
| 1204 | |
| 1205 | // Send a stop to the debugger after we get all other threads to stop. |
| 1206 | StopRunningThreads(thread.GetID()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1209 | namespace { |
| 1210 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1211 | struct EmulatorBaton { |
| 1212 | NativeProcessLinux *m_process; |
| 1213 | NativeRegisterContext *m_reg_context; |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1214 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1215 | // eRegisterKindDWARF -> RegsiterValue |
| 1216 | std::unordered_map<uint32_t, RegisterValue> m_register_values; |
Pavel Labath | 6648fcc | 2015-04-27 09:21:14 +0000 | [diff] [blame] | 1217 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1218 | EmulatorBaton(NativeProcessLinux *process, NativeRegisterContext *reg_context) |
| 1219 | : m_process(process), m_reg_context(reg_context) {} |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1220 | }; |
| 1221 | |
| 1222 | } // anonymous namespace |
| 1223 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1224 | static size_t ReadMemoryCallback(EmulateInstruction *instruction, void *baton, |
| 1225 | const EmulateInstruction::Context &context, |
| 1226 | lldb::addr_t addr, void *dst, size_t length) { |
| 1227 | EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1228 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1229 | size_t bytes_read; |
| 1230 | emulator_baton->m_process->ReadMemory(addr, dst, length, bytes_read); |
| 1231 | return bytes_read; |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1234 | static bool ReadRegisterCallback(EmulateInstruction *instruction, void *baton, |
| 1235 | const RegisterInfo *reg_info, |
| 1236 | RegisterValue ®_value) { |
| 1237 | EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1238 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1239 | auto it = emulator_baton->m_register_values.find( |
| 1240 | reg_info->kinds[eRegisterKindDWARF]); |
| 1241 | if (it != emulator_baton->m_register_values.end()) { |
| 1242 | reg_value = it->second; |
| 1243 | return true; |
| 1244 | } |
| 1245 | |
| 1246 | // The emulator only fill in the dwarf regsiter numbers (and in some case |
| 1247 | // the generic register numbers). Get the full register info from the |
| 1248 | // register context based on the dwarf register numbers. |
| 1249 | const RegisterInfo *full_reg_info = |
| 1250 | emulator_baton->m_reg_context->GetRegisterInfo( |
| 1251 | eRegisterKindDWARF, reg_info->kinds[eRegisterKindDWARF]); |
| 1252 | |
| 1253 | Error error = |
| 1254 | emulator_baton->m_reg_context->ReadRegister(full_reg_info, reg_value); |
| 1255 | if (error.Success()) |
| 1256 | return true; |
| 1257 | |
| 1258 | return false; |
| 1259 | } |
| 1260 | |
| 1261 | static bool WriteRegisterCallback(EmulateInstruction *instruction, void *baton, |
| 1262 | const EmulateInstruction::Context &context, |
| 1263 | const RegisterInfo *reg_info, |
| 1264 | const RegisterValue ®_value) { |
| 1265 | EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); |
| 1266 | emulator_baton->m_register_values[reg_info->kinds[eRegisterKindDWARF]] = |
| 1267 | reg_value; |
| 1268 | return true; |
| 1269 | } |
| 1270 | |
| 1271 | static size_t WriteMemoryCallback(EmulateInstruction *instruction, void *baton, |
| 1272 | const EmulateInstruction::Context &context, |
| 1273 | lldb::addr_t addr, const void *dst, |
| 1274 | size_t length) { |
| 1275 | return length; |
| 1276 | } |
| 1277 | |
| 1278 | static lldb::addr_t ReadFlags(NativeRegisterContext *regsiter_context) { |
| 1279 | const RegisterInfo *flags_info = regsiter_context->GetRegisterInfo( |
| 1280 | eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); |
| 1281 | return regsiter_context->ReadRegisterAsUnsigned(flags_info, |
| 1282 | LLDB_INVALID_ADDRESS); |
| 1283 | } |
| 1284 | |
| 1285 | Error NativeProcessLinux::SetupSoftwareSingleStepping( |
| 1286 | NativeThreadLinux &thread) { |
| 1287 | Error error; |
| 1288 | NativeRegisterContextSP register_context_sp = thread.GetRegisterContext(); |
| 1289 | |
| 1290 | std::unique_ptr<EmulateInstruction> emulator_ap( |
| 1291 | EmulateInstruction::FindPlugin(m_arch, eInstructionTypePCModifying, |
| 1292 | nullptr)); |
| 1293 | |
| 1294 | if (emulator_ap == nullptr) |
| 1295 | return Error("Instruction emulator not found!"); |
| 1296 | |
| 1297 | EmulatorBaton baton(this, register_context_sp.get()); |
| 1298 | emulator_ap->SetBaton(&baton); |
| 1299 | emulator_ap->SetReadMemCallback(&ReadMemoryCallback); |
| 1300 | emulator_ap->SetReadRegCallback(&ReadRegisterCallback); |
| 1301 | emulator_ap->SetWriteMemCallback(&WriteMemoryCallback); |
| 1302 | emulator_ap->SetWriteRegCallback(&WriteRegisterCallback); |
| 1303 | |
| 1304 | if (!emulator_ap->ReadInstruction()) |
| 1305 | return Error("Read instruction failed!"); |
| 1306 | |
| 1307 | bool emulation_result = |
| 1308 | emulator_ap->EvaluateInstruction(eEmulateInstructionOptionAutoAdvancePC); |
| 1309 | |
| 1310 | const RegisterInfo *reg_info_pc = register_context_sp->GetRegisterInfo( |
| 1311 | eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); |
| 1312 | const RegisterInfo *reg_info_flags = register_context_sp->GetRegisterInfo( |
| 1313 | eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); |
| 1314 | |
| 1315 | auto pc_it = |
| 1316 | baton.m_register_values.find(reg_info_pc->kinds[eRegisterKindDWARF]); |
| 1317 | auto flags_it = |
| 1318 | baton.m_register_values.find(reg_info_flags->kinds[eRegisterKindDWARF]); |
| 1319 | |
| 1320 | lldb::addr_t next_pc; |
| 1321 | lldb::addr_t next_flags; |
| 1322 | if (emulation_result) { |
| 1323 | assert(pc_it != baton.m_register_values.end() && |
| 1324 | "Emulation was successfull but PC wasn't updated"); |
| 1325 | next_pc = pc_it->second.GetAsUInt64(); |
| 1326 | |
| 1327 | if (flags_it != baton.m_register_values.end()) |
| 1328 | next_flags = flags_it->second.GetAsUInt64(); |
| 1329 | else |
| 1330 | next_flags = ReadFlags(register_context_sp.get()); |
| 1331 | } else if (pc_it == baton.m_register_values.end()) { |
| 1332 | // Emulate instruction failed and it haven't changed PC. Advance PC |
| 1333 | // with the size of the current opcode because the emulation of all |
| 1334 | // PC modifying instruction should be successful. The failure most |
| 1335 | // likely caused by a not supported instruction which don't modify PC. |
| 1336 | next_pc = |
| 1337 | register_context_sp->GetPC() + emulator_ap->GetOpcode().GetByteSize(); |
| 1338 | next_flags = ReadFlags(register_context_sp.get()); |
| 1339 | } else { |
| 1340 | // The instruction emulation failed after it modified the PC. It is an |
| 1341 | // unknown error where we can't continue because the next instruction is |
| 1342 | // modifying the PC but we don't know how. |
| 1343 | return Error("Instruction emulation failed unexpectedly."); |
| 1344 | } |
| 1345 | |
| 1346 | if (m_arch.GetMachine() == llvm::Triple::arm) { |
| 1347 | if (next_flags & 0x20) { |
| 1348 | // Thumb mode |
| 1349 | error = SetSoftwareBreakpoint(next_pc, 2); |
| 1350 | } else { |
| 1351 | // Arm mode |
| 1352 | error = SetSoftwareBreakpoint(next_pc, 4); |
Pavel Labath | 6648fcc | 2015-04-27 09:21:14 +0000 | [diff] [blame] | 1353 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1354 | } else if (m_arch.GetMachine() == llvm::Triple::mips64 || |
| 1355 | m_arch.GetMachine() == llvm::Triple::mips64el || |
| 1356 | m_arch.GetMachine() == llvm::Triple::mips || |
| 1357 | m_arch.GetMachine() == llvm::Triple::mipsel) |
| 1358 | error = SetSoftwareBreakpoint(next_pc, 4); |
| 1359 | else { |
| 1360 | // No size hint is given for the next breakpoint |
| 1361 | error = SetSoftwareBreakpoint(next_pc, 0); |
| 1362 | } |
Pavel Labath | 6648fcc | 2015-04-27 09:21:14 +0000 | [diff] [blame] | 1363 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1364 | if (error.Fail()) |
| 1365 | return error; |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1366 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1367 | m_threads_stepping_with_breakpoint.insert({thread.GetID(), next_pc}); |
Mohit K. Bhakkad | cdc22a8 | 2015-05-07 05:56:27 +0000 | [diff] [blame] | 1368 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1369 | return Error(); |
| 1370 | } |
| 1371 | |
| 1372 | bool NativeProcessLinux::SupportHardwareSingleStepping() const { |
| 1373 | if (m_arch.GetMachine() == llvm::Triple::arm || |
| 1374 | m_arch.GetMachine() == llvm::Triple::mips64 || |
| 1375 | m_arch.GetMachine() == llvm::Triple::mips64el || |
| 1376 | m_arch.GetMachine() == llvm::Triple::mips || |
| 1377 | m_arch.GetMachine() == llvm::Triple::mipsel) |
Pavel Labath | 6648fcc | 2015-04-27 09:21:14 +0000 | [diff] [blame] | 1378 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1379 | return true; |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1382 | Error NativeProcessLinux::Resume(const ResumeActionList &resume_actions) { |
| 1383 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); |
| 1384 | if (log) |
| 1385 | log->Printf("NativeProcessLinux::%s called: pid %" PRIu64, __FUNCTION__, |
| 1386 | GetID()); |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1387 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1388 | bool software_single_step = !SupportHardwareSingleStepping(); |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1389 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1390 | if (software_single_step) { |
| 1391 | for (auto thread_sp : m_threads) { |
| 1392 | assert(thread_sp && "thread list should not contain NULL threads"); |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1393 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1394 | const ResumeAction *const action = |
| 1395 | resume_actions.GetActionForThread(thread_sp->GetID(), true); |
| 1396 | if (action == nullptr) |
| 1397 | continue; |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1398 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1399 | if (action->state == eStateStepping) { |
| 1400 | Error error = SetupSoftwareSingleStepping( |
| 1401 | static_cast<NativeThreadLinux &>(*thread_sp)); |
| 1402 | if (error.Fail()) |
| 1403 | return error; |
| 1404 | } |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1405 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | for (auto thread_sp : m_threads) { |
| 1409 | assert(thread_sp && "thread list should not contain NULL threads"); |
| 1410 | |
| 1411 | const ResumeAction *const action = |
| 1412 | resume_actions.GetActionForThread(thread_sp->GetID(), true); |
| 1413 | |
| 1414 | if (action == nullptr) { |
| 1415 | if (log) |
| 1416 | log->Printf( |
| 1417 | "NativeProcessLinux::%s no action specified for pid %" PRIu64 |
| 1418 | " tid %" PRIu64, |
| 1419 | __FUNCTION__, GetID(), thread_sp->GetID()); |
| 1420 | continue; |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1423 | if (log) { |
| 1424 | log->Printf("NativeProcessLinux::%s processing resume action state %s " |
| 1425 | "for pid %" PRIu64 " tid %" PRIu64, |
| 1426 | __FUNCTION__, StateAsCString(action->state), GetID(), |
| 1427 | thread_sp->GetID()); |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1430 | switch (action->state) { |
| 1431 | case eStateRunning: |
| 1432 | case eStateStepping: { |
| 1433 | // Run the thread, possibly feeding it the signal. |
| 1434 | const int signo = action->signal; |
| 1435 | ResumeThread(static_cast<NativeThreadLinux &>(*thread_sp), action->state, |
| 1436 | signo); |
| 1437 | break; |
| 1438 | } |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1439 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1440 | case eStateSuspended: |
| 1441 | case eStateStopped: |
| 1442 | lldbassert(0 && "Unexpected state"); |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1443 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1444 | default: |
| 1445 | return Error("NativeProcessLinux::%s (): unexpected state %s specified " |
| 1446 | "for pid %" PRIu64 ", tid %" PRIu64, |
| 1447 | __FUNCTION__, StateAsCString(action->state), GetID(), |
| 1448 | thread_sp->GetID()); |
| 1449 | } |
| 1450 | } |
| 1451 | |
| 1452 | return Error(); |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1455 | Error NativeProcessLinux::Halt() { |
| 1456 | Error error; |
| 1457 | |
| 1458 | if (kill(GetID(), SIGSTOP) != 0) |
| 1459 | error.SetErrorToErrno(); |
| 1460 | |
| 1461 | return error; |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1464 | Error NativeProcessLinux::Detach() { |
| 1465 | Error error; |
| 1466 | |
| 1467 | // Stop monitoring the inferior. |
| 1468 | m_sigchld_handle.reset(); |
| 1469 | |
| 1470 | // Tell ptrace to detach from the process. |
| 1471 | if (GetID() == LLDB_INVALID_PROCESS_ID) |
| 1472 | return error; |
| 1473 | |
| 1474 | for (auto thread_sp : m_threads) { |
| 1475 | Error e = Detach(thread_sp->GetID()); |
| 1476 | if (e.Fail()) |
| 1477 | error = |
| 1478 | e; // Save the error, but still attempt to detach from other threads. |
| 1479 | } |
| 1480 | |
| 1481 | return error; |
| 1482 | } |
| 1483 | |
| 1484 | Error NativeProcessLinux::Signal(int signo) { |
| 1485 | Error error; |
| 1486 | |
| 1487 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 1488 | if (log) |
| 1489 | log->Printf( |
| 1490 | "NativeProcessLinux::%s: sending signal %d (%s) to pid %" PRIu64, |
| 1491 | __FUNCTION__, signo, Host::GetSignalAsCString(signo), GetID()); |
| 1492 | |
| 1493 | if (kill(GetID(), signo)) |
| 1494 | error.SetErrorToErrno(); |
| 1495 | |
| 1496 | return error; |
| 1497 | } |
| 1498 | |
| 1499 | Error NativeProcessLinux::Interrupt() { |
| 1500 | // Pick a running thread (or if none, a not-dead stopped thread) as |
| 1501 | // the chosen thread that will be the stop-reason thread. |
| 1502 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 1503 | |
| 1504 | NativeThreadProtocolSP running_thread_sp; |
| 1505 | NativeThreadProtocolSP stopped_thread_sp; |
| 1506 | |
| 1507 | if (log) |
| 1508 | log->Printf( |
| 1509 | "NativeProcessLinux::%s selecting running thread for interrupt target", |
| 1510 | __FUNCTION__); |
| 1511 | |
| 1512 | for (auto thread_sp : m_threads) { |
| 1513 | // The thread shouldn't be null but lets just cover that here. |
| 1514 | if (!thread_sp) |
| 1515 | continue; |
| 1516 | |
| 1517 | // If we have a running or stepping thread, we'll call that the |
| 1518 | // target of the interrupt. |
| 1519 | const auto thread_state = thread_sp->GetState(); |
| 1520 | if (thread_state == eStateRunning || thread_state == eStateStepping) { |
| 1521 | running_thread_sp = thread_sp; |
| 1522 | break; |
| 1523 | } else if (!stopped_thread_sp && StateIsStoppedState(thread_state, true)) { |
| 1524 | // Remember the first non-dead stopped thread. We'll use that as a backup |
| 1525 | // if there are no running threads. |
| 1526 | stopped_thread_sp = thread_sp; |
| 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | if (!running_thread_sp && !stopped_thread_sp) { |
| 1531 | Error error("found no running/stepping or live stopped threads as target " |
| 1532 | "for interrupt"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1533 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1534 | log->Printf("NativeProcessLinux::%s skipping due to error: %s", |
| 1535 | __FUNCTION__, error.AsCString()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1536 | |
| 1537 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | NativeThreadProtocolSP deferred_signal_thread_sp = |
| 1541 | running_thread_sp ? running_thread_sp : stopped_thread_sp; |
| 1542 | |
| 1543 | if (log) |
| 1544 | log->Printf("NativeProcessLinux::%s pid %" PRIu64 " %s tid %" PRIu64 |
| 1545 | " chosen for interrupt target", |
| 1546 | __FUNCTION__, GetID(), |
| 1547 | running_thread_sp ? "running" : "stopped", |
| 1548 | deferred_signal_thread_sp->GetID()); |
| 1549 | |
| 1550 | StopRunningThreads(deferred_signal_thread_sp->GetID()); |
| 1551 | |
| 1552 | return Error(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1555 | Error NativeProcessLinux::Kill() { |
| 1556 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 1557 | if (log) |
| 1558 | log->Printf("NativeProcessLinux::%s called for PID %" PRIu64, __FUNCTION__, |
| 1559 | GetID()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1560 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1561 | Error error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1562 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1563 | switch (m_state) { |
| 1564 | case StateType::eStateInvalid: |
| 1565 | case StateType::eStateExited: |
| 1566 | case StateType::eStateCrashed: |
| 1567 | case StateType::eStateDetached: |
| 1568 | case StateType::eStateUnloaded: |
| 1569 | // Nothing to do - the process is already dead. |
| 1570 | if (log) |
| 1571 | log->Printf("NativeProcessLinux::%s ignored for PID %" PRIu64 |
| 1572 | " due to current state: %s", |
| 1573 | __FUNCTION__, GetID(), StateAsCString(m_state)); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1574 | return error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1575 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1576 | case StateType::eStateConnected: |
| 1577 | case StateType::eStateAttaching: |
| 1578 | case StateType::eStateLaunching: |
| 1579 | case StateType::eStateStopped: |
| 1580 | case StateType::eStateRunning: |
| 1581 | case StateType::eStateStepping: |
| 1582 | case StateType::eStateSuspended: |
| 1583 | // We can try to kill a process in these states. |
| 1584 | break; |
| 1585 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1586 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1587 | if (kill(GetID(), SIGKILL) != 0) { |
| 1588 | error.SetErrorToErrno(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1589 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1590 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1591 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1592 | return error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | static Error |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1596 | ParseMemoryRegionInfoFromProcMapsLine(const std::string &maps_line, |
| 1597 | MemoryRegionInfo &memory_region_info) { |
| 1598 | memory_region_info.Clear(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1599 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1600 | StringExtractor line_extractor(maps_line.c_str()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1601 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1602 | // Format: {address_start_hex}-{address_end_hex} perms offset dev inode |
| 1603 | // pathname |
| 1604 | // perms: rwxp (letter is present if set, '-' if not, final character is |
| 1605 | // p=private, s=shared). |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1606 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1607 | // Parse out the starting address |
| 1608 | lldb::addr_t start_address = line_extractor.GetHexMaxU64(false, 0); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1609 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1610 | // Parse out hyphen separating start and end address from range. |
| 1611 | if (!line_extractor.GetBytesLeft() || (line_extractor.GetChar() != '-')) |
| 1612 | return Error( |
| 1613 | "malformed /proc/{pid}/maps entry, missing dash between address range"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1614 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1615 | // Parse out the ending address |
| 1616 | lldb::addr_t end_address = line_extractor.GetHexMaxU64(false, start_address); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1617 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1618 | // Parse out the space after the address. |
| 1619 | if (!line_extractor.GetBytesLeft() || (line_extractor.GetChar() != ' ')) |
| 1620 | return Error("malformed /proc/{pid}/maps entry, missing space after range"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1621 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1622 | // Save the range. |
| 1623 | memory_region_info.GetRange().SetRangeBase(start_address); |
| 1624 | memory_region_info.GetRange().SetRangeEnd(end_address); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1625 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1626 | // Any memory region in /proc/{pid}/maps is by definition mapped into the |
| 1627 | // process. |
| 1628 | memory_region_info.SetMapped(MemoryRegionInfo::OptionalBool::eYes); |
Howard Hellyer | ad00756 | 2016-07-07 08:21:28 +0000 | [diff] [blame] | 1629 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1630 | // Parse out each permission entry. |
| 1631 | if (line_extractor.GetBytesLeft() < 4) |
| 1632 | return Error("malformed /proc/{pid}/maps entry, missing some portion of " |
| 1633 | "permissions"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1634 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1635 | // Handle read permission. |
| 1636 | const char read_perm_char = line_extractor.GetChar(); |
| 1637 | if (read_perm_char == 'r') |
| 1638 | memory_region_info.SetReadable(MemoryRegionInfo::OptionalBool::eYes); |
| 1639 | else if (read_perm_char == '-') |
| 1640 | memory_region_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); |
| 1641 | else |
| 1642 | return Error("unexpected /proc/{pid}/maps read permission char"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1643 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1644 | // Handle write permission. |
| 1645 | const char write_perm_char = line_extractor.GetChar(); |
| 1646 | if (write_perm_char == 'w') |
| 1647 | memory_region_info.SetWritable(MemoryRegionInfo::OptionalBool::eYes); |
| 1648 | else if (write_perm_char == '-') |
| 1649 | memory_region_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); |
| 1650 | else |
| 1651 | return Error("unexpected /proc/{pid}/maps write permission char"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1652 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1653 | // Handle execute permission. |
| 1654 | const char exec_perm_char = line_extractor.GetChar(); |
| 1655 | if (exec_perm_char == 'x') |
| 1656 | memory_region_info.SetExecutable(MemoryRegionInfo::OptionalBool::eYes); |
| 1657 | else if (exec_perm_char == '-') |
| 1658 | memory_region_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); |
| 1659 | else |
| 1660 | return Error("unexpected /proc/{pid}/maps exec permission char"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1661 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1662 | line_extractor.GetChar(); // Read the private bit |
| 1663 | line_extractor.SkipSpaces(); // Skip the separator |
| 1664 | line_extractor.GetHexMaxU64(false, 0); // Read the offset |
| 1665 | line_extractor.GetHexMaxU64(false, 0); // Read the major device number |
| 1666 | line_extractor.GetChar(); // Read the device id separator |
| 1667 | line_extractor.GetHexMaxU64(false, 0); // Read the major device number |
| 1668 | line_extractor.SkipSpaces(); // Skip the separator |
| 1669 | line_extractor.GetU64(0, 10); // Read the inode number |
Tamas Berghammer | d7d69f8 | 2016-07-22 12:55:35 +0000 | [diff] [blame] | 1670 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1671 | line_extractor.SkipSpaces(); |
| 1672 | const char *name = line_extractor.Peek(); |
| 1673 | if (name) |
| 1674 | memory_region_info.SetName(name); |
Tamas Berghammer | d7d69f8 | 2016-07-22 12:55:35 +0000 | [diff] [blame] | 1675 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1676 | return Error(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1679 | Error NativeProcessLinux::GetMemoryRegionInfo(lldb::addr_t load_addr, |
| 1680 | MemoryRegionInfo &range_info) { |
| 1681 | // FIXME review that the final memory region returned extends to the end of |
| 1682 | // the virtual address space, |
| 1683 | // with no perms if it is not mapped. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1684 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1685 | // Use an approach that reads memory regions from /proc/{pid}/maps. |
| 1686 | // Assume proc maps entries are in ascending order. |
| 1687 | // FIXME assert if we find differently. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1688 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1689 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 1690 | Error error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1691 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1692 | if (m_supports_mem_region == LazyBool::eLazyBoolNo) { |
| 1693 | // We're done. |
| 1694 | error.SetErrorString("unsupported"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1695 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1696 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1697 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1698 | // If our cache is empty, pull the latest. There should always be at least |
| 1699 | // one memory region |
| 1700 | // if memory region handling is supported. |
| 1701 | if (m_mem_region_cache.empty()) { |
| 1702 | error = ProcFileReader::ProcessLineByLine( |
| 1703 | GetID(), "maps", [&](const std::string &line) -> bool { |
| 1704 | MemoryRegionInfo info; |
| 1705 | const Error parse_error = |
| 1706 | ParseMemoryRegionInfoFromProcMapsLine(line, info); |
| 1707 | if (parse_error.Success()) { |
| 1708 | m_mem_region_cache.push_back(info); |
| 1709 | return true; |
| 1710 | } else { |
| 1711 | if (log) |
| 1712 | log->Printf("NativeProcessLinux::%s failed to parse proc maps " |
| 1713 | "line '%s': %s", |
| 1714 | __FUNCTION__, line.c_str(), error.AsCString()); |
| 1715 | return false; |
| 1716 | } |
| 1717 | }); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1718 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1719 | // If we had an error, we'll mark unsupported. |
| 1720 | if (error.Fail()) { |
| 1721 | m_supports_mem_region = LazyBool::eLazyBoolNo; |
| 1722 | return error; |
| 1723 | } else if (m_mem_region_cache.empty()) { |
| 1724 | // No entries after attempting to read them. This shouldn't happen if |
| 1725 | // /proc/{pid}/maps |
| 1726 | // is supported. Assume we don't support map entries via procfs. |
| 1727 | if (log) |
| 1728 | log->Printf("NativeProcessLinux::%s failed to find any procfs maps " |
| 1729 | "entries, assuming no support for memory region metadata " |
| 1730 | "retrieval", |
| 1731 | __FUNCTION__); |
| 1732 | m_supports_mem_region = LazyBool::eLazyBoolNo; |
| 1733 | error.SetErrorString("not supported"); |
| 1734 | return error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1735 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1736 | |
| 1737 | if (log) |
| 1738 | log->Printf("NativeProcessLinux::%s read %" PRIu64 |
| 1739 | " memory region entries from /proc/%" PRIu64 "/maps", |
| 1740 | __FUNCTION__, |
| 1741 | static_cast<uint64_t>(m_mem_region_cache.size()), GetID()); |
| 1742 | |
| 1743 | // We support memory retrieval, remember that. |
| 1744 | m_supports_mem_region = LazyBool::eLazyBoolYes; |
| 1745 | } else { |
| 1746 | if (log) |
| 1747 | log->Printf("NativeProcessLinux::%s reusing %" PRIu64 |
| 1748 | " cached memory region entries", |
| 1749 | __FUNCTION__, |
| 1750 | static_cast<uint64_t>(m_mem_region_cache.size())); |
| 1751 | } |
| 1752 | |
| 1753 | lldb::addr_t prev_base_address = 0; |
| 1754 | |
| 1755 | // FIXME start by finding the last region that is <= target address using |
| 1756 | // binary search. Data is sorted. |
| 1757 | // There can be a ton of regions on pthreads apps with lots of threads. |
| 1758 | for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end(); |
| 1759 | ++it) { |
| 1760 | MemoryRegionInfo &proc_entry_info = *it; |
| 1761 | |
| 1762 | // Sanity check assumption that /proc/{pid}/maps entries are ascending. |
| 1763 | assert((proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) && |
| 1764 | "descending /proc/pid/maps entries detected, unexpected"); |
| 1765 | prev_base_address = proc_entry_info.GetRange().GetRangeBase(); |
| 1766 | |
| 1767 | // If the target address comes before this entry, indicate distance to next |
| 1768 | // region. |
| 1769 | if (load_addr < proc_entry_info.GetRange().GetRangeBase()) { |
| 1770 | range_info.GetRange().SetRangeBase(load_addr); |
| 1771 | range_info.GetRange().SetByteSize( |
| 1772 | proc_entry_info.GetRange().GetRangeBase() - load_addr); |
| 1773 | range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); |
| 1774 | range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); |
| 1775 | range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); |
| 1776 | range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); |
| 1777 | |
| 1778 | return error; |
| 1779 | } else if (proc_entry_info.GetRange().Contains(load_addr)) { |
| 1780 | // The target address is within the memory region we're processing here. |
| 1781 | range_info = proc_entry_info; |
| 1782 | return error; |
| 1783 | } |
| 1784 | |
| 1785 | // The target memory address comes somewhere after the region we just |
| 1786 | // parsed. |
| 1787 | } |
| 1788 | |
| 1789 | // If we made it here, we didn't find an entry that contained the given |
| 1790 | // address. Return the |
| 1791 | // load_addr as start and the amount of bytes betwwen load address and the end |
| 1792 | // of the memory as |
| 1793 | // size. |
| 1794 | range_info.GetRange().SetRangeBase(load_addr); |
| 1795 | range_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS); |
| 1796 | range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); |
| 1797 | range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); |
| 1798 | range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); |
| 1799 | range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); |
| 1800 | return error; |
| 1801 | } |
| 1802 | |
| 1803 | void NativeProcessLinux::DoStopIDBumped(uint32_t newBumpId) { |
| 1804 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 1805 | if (log) |
| 1806 | log->Printf("NativeProcessLinux::%s(newBumpId=%" PRIu32 ") called", |
| 1807 | __FUNCTION__, newBumpId); |
| 1808 | |
| 1809 | if (log) |
| 1810 | log->Printf("NativeProcessLinux::%s clearing %" PRIu64 |
| 1811 | " entries from the cache", |
| 1812 | __FUNCTION__, static_cast<uint64_t>(m_mem_region_cache.size())); |
| 1813 | m_mem_region_cache.clear(); |
| 1814 | } |
| 1815 | |
| 1816 | Error NativeProcessLinux::AllocateMemory(size_t size, uint32_t permissions, |
| 1817 | lldb::addr_t &addr) { |
| 1818 | // FIXME implementing this requires the equivalent of |
| 1819 | // InferiorCallPOSIX::InferiorCallMmap, which depends on |
| 1820 | // functional ThreadPlans working with Native*Protocol. |
| 1821 | #if 1 |
| 1822 | return Error("not implemented yet"); |
| 1823 | #else |
| 1824 | addr = LLDB_INVALID_ADDRESS; |
| 1825 | |
| 1826 | unsigned prot = 0; |
| 1827 | if (permissions & lldb::ePermissionsReadable) |
| 1828 | prot |= eMmapProtRead; |
| 1829 | if (permissions & lldb::ePermissionsWritable) |
| 1830 | prot |= eMmapProtWrite; |
| 1831 | if (permissions & lldb::ePermissionsExecutable) |
| 1832 | prot |= eMmapProtExec; |
| 1833 | |
| 1834 | // TODO implement this directly in NativeProcessLinux |
| 1835 | // (and lift to NativeProcessPOSIX if/when that class is |
| 1836 | // refactored out). |
| 1837 | if (InferiorCallMmap(this, addr, 0, size, prot, |
| 1838 | eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) { |
| 1839 | m_addr_to_mmap_size[addr] = size; |
| 1840 | return Error(); |
| 1841 | } else { |
| 1842 | addr = LLDB_INVALID_ADDRESS; |
| 1843 | return Error("unable to allocate %" PRIu64 |
| 1844 | " bytes of memory with permissions %s", |
| 1845 | size, GetPermissionsAsCString(permissions)); |
| 1846 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1847 | #endif |
| 1848 | } |
| 1849 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1850 | Error NativeProcessLinux::DeallocateMemory(lldb::addr_t addr) { |
| 1851 | // FIXME see comments in AllocateMemory - required lower-level |
| 1852 | // bits not in place yet (ThreadPlans) |
| 1853 | return Error("not implemented"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1854 | } |
| 1855 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1856 | lldb::addr_t NativeProcessLinux::GetSharedLibraryInfoAddress() { |
| 1857 | // punt on this for now |
| 1858 | return LLDB_INVALID_ADDRESS; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1859 | } |
| 1860 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1861 | size_t NativeProcessLinux::UpdateThreads() { |
| 1862 | // The NativeProcessLinux monitoring threads are always up to date |
| 1863 | // with respect to thread state and they keep the thread list |
| 1864 | // populated properly. All this method needs to do is return the |
| 1865 | // thread count. |
| 1866 | return m_threads.size(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1869 | bool NativeProcessLinux::GetArchitecture(ArchSpec &arch) const { |
| 1870 | arch = m_arch; |
| 1871 | return true; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1872 | } |
| 1873 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1874 | Error NativeProcessLinux::GetSoftwareBreakpointPCOffset( |
| 1875 | uint32_t &actual_opcode_size) { |
| 1876 | // FIXME put this behind a breakpoint protocol class that can be |
| 1877 | // set per architecture. Need ARM, MIPS support here. |
| 1878 | static const uint8_t g_i386_opcode[] = {0xCC}; |
| 1879 | static const uint8_t g_s390x_opcode[] = {0x00, 0x01}; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1880 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1881 | switch (m_arch.GetMachine()) { |
| 1882 | case llvm::Triple::x86: |
| 1883 | case llvm::Triple::x86_64: |
| 1884 | actual_opcode_size = static_cast<uint32_t>(sizeof(g_i386_opcode)); |
| 1885 | return Error(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1886 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1887 | case llvm::Triple::systemz: |
| 1888 | actual_opcode_size = static_cast<uint32_t>(sizeof(g_s390x_opcode)); |
| 1889 | return Error(); |
Ulrich Weigand | bb00d0b | 2016-04-14 14:28:34 +0000 | [diff] [blame] | 1890 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1891 | case llvm::Triple::arm: |
| 1892 | case llvm::Triple::aarch64: |
| 1893 | case llvm::Triple::mips64: |
| 1894 | case llvm::Triple::mips64el: |
| 1895 | case llvm::Triple::mips: |
| 1896 | case llvm::Triple::mipsel: |
| 1897 | // On these architectures the PC don't get updated for breakpoint hits |
| 1898 | actual_opcode_size = 0; |
| 1899 | return Error(); |
| 1900 | |
| 1901 | default: |
| 1902 | assert(false && "CPU type not supported!"); |
| 1903 | return Error("CPU type not supported"); |
| 1904 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1905 | } |
| 1906 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1907 | Error NativeProcessLinux::SetBreakpoint(lldb::addr_t addr, uint32_t size, |
| 1908 | bool hardware) { |
| 1909 | if (hardware) |
| 1910 | return Error("NativeProcessLinux does not support hardware breakpoints"); |
| 1911 | else |
| 1912 | return SetSoftwareBreakpoint(addr, size); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1913 | } |
| 1914 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1915 | Error NativeProcessLinux::GetSoftwareBreakpointTrapOpcode( |
| 1916 | size_t trap_opcode_size_hint, size_t &actual_opcode_size, |
| 1917 | const uint8_t *&trap_opcode_bytes) { |
| 1918 | // FIXME put this behind a breakpoint protocol class that can be set per |
| 1919 | // architecture. Need MIPS support here. |
| 1920 | static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4}; |
| 1921 | // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the |
| 1922 | // linux kernel does otherwise. |
| 1923 | static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7}; |
| 1924 | static const uint8_t g_i386_opcode[] = {0xCC}; |
| 1925 | static const uint8_t g_mips64_opcode[] = {0x00, 0x00, 0x00, 0x0d}; |
| 1926 | static const uint8_t g_mips64el_opcode[] = {0x0d, 0x00, 0x00, 0x00}; |
| 1927 | static const uint8_t g_s390x_opcode[] = {0x00, 0x01}; |
| 1928 | static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde}; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1929 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1930 | switch (m_arch.GetMachine()) { |
| 1931 | case llvm::Triple::aarch64: |
| 1932 | trap_opcode_bytes = g_aarch64_opcode; |
| 1933 | actual_opcode_size = sizeof(g_aarch64_opcode); |
| 1934 | return Error(); |
Todd Fiala | 2afc596 | 2014-08-21 16:42:31 +0000 | [diff] [blame] | 1935 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1936 | case llvm::Triple::arm: |
| 1937 | switch (trap_opcode_size_hint) { |
| 1938 | case 2: |
| 1939 | trap_opcode_bytes = g_thumb_breakpoint_opcode; |
| 1940 | actual_opcode_size = sizeof(g_thumb_breakpoint_opcode); |
| 1941 | return Error(); |
| 1942 | case 4: |
| 1943 | trap_opcode_bytes = g_arm_breakpoint_opcode; |
| 1944 | actual_opcode_size = sizeof(g_arm_breakpoint_opcode); |
| 1945 | return Error(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1946 | default: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1947 | assert(false && "Unrecognised trap opcode size hint!"); |
| 1948 | return Error("Unrecognised trap opcode size hint!"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1949 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1950 | |
| 1951 | case llvm::Triple::x86: |
| 1952 | case llvm::Triple::x86_64: |
| 1953 | trap_opcode_bytes = g_i386_opcode; |
| 1954 | actual_opcode_size = sizeof(g_i386_opcode); |
| 1955 | return Error(); |
| 1956 | |
| 1957 | case llvm::Triple::mips: |
| 1958 | case llvm::Triple::mips64: |
| 1959 | trap_opcode_bytes = g_mips64_opcode; |
| 1960 | actual_opcode_size = sizeof(g_mips64_opcode); |
| 1961 | return Error(); |
| 1962 | |
| 1963 | case llvm::Triple::mipsel: |
| 1964 | case llvm::Triple::mips64el: |
| 1965 | trap_opcode_bytes = g_mips64el_opcode; |
| 1966 | actual_opcode_size = sizeof(g_mips64el_opcode); |
| 1967 | return Error(); |
| 1968 | |
| 1969 | case llvm::Triple::systemz: |
| 1970 | trap_opcode_bytes = g_s390x_opcode; |
| 1971 | actual_opcode_size = sizeof(g_s390x_opcode); |
| 1972 | return Error(); |
| 1973 | |
| 1974 | default: |
| 1975 | assert(false && "CPU type not supported!"); |
| 1976 | return Error("CPU type not supported"); |
| 1977 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1978 | } |
| 1979 | |
| 1980 | #if 0 |
| 1981 | ProcessMessage::CrashReason |
| 1982 | NativeProcessLinux::GetCrashReasonForSIGSEGV(const siginfo_t *info) |
| 1983 | { |
| 1984 | ProcessMessage::CrashReason reason; |
| 1985 | assert(info->si_signo == SIGSEGV); |
| 1986 | |
| 1987 | reason = ProcessMessage::eInvalidCrashReason; |
| 1988 | |
| 1989 | switch (info->si_code) |
| 1990 | { |
| 1991 | default: |
| 1992 | assert(false && "unexpected si_code for SIGSEGV"); |
| 1993 | break; |
| 1994 | case SI_KERNEL: |
| 1995 | // Linux will occasionally send spurious SI_KERNEL codes. |
| 1996 | // (this is poorly documented in sigaction) |
| 1997 | // One way to get this is via unaligned SIMD loads. |
| 1998 | reason = ProcessMessage::eInvalidAddress; // for lack of anything better |
| 1999 | break; |
| 2000 | case SEGV_MAPERR: |
| 2001 | reason = ProcessMessage::eInvalidAddress; |
| 2002 | break; |
| 2003 | case SEGV_ACCERR: |
| 2004 | reason = ProcessMessage::ePrivilegedAddress; |
| 2005 | break; |
| 2006 | } |
| 2007 | |
| 2008 | return reason; |
| 2009 | } |
| 2010 | #endif |
| 2011 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2012 | #if 0 |
| 2013 | ProcessMessage::CrashReason |
| 2014 | NativeProcessLinux::GetCrashReasonForSIGILL(const siginfo_t *info) |
| 2015 | { |
| 2016 | ProcessMessage::CrashReason reason; |
| 2017 | assert(info->si_signo == SIGILL); |
| 2018 | |
| 2019 | reason = ProcessMessage::eInvalidCrashReason; |
| 2020 | |
| 2021 | switch (info->si_code) |
| 2022 | { |
| 2023 | default: |
| 2024 | assert(false && "unexpected si_code for SIGILL"); |
| 2025 | break; |
| 2026 | case ILL_ILLOPC: |
| 2027 | reason = ProcessMessage::eIllegalOpcode; |
| 2028 | break; |
| 2029 | case ILL_ILLOPN: |
| 2030 | reason = ProcessMessage::eIllegalOperand; |
| 2031 | break; |
| 2032 | case ILL_ILLADR: |
| 2033 | reason = ProcessMessage::eIllegalAddressingMode; |
| 2034 | break; |
| 2035 | case ILL_ILLTRP: |
| 2036 | reason = ProcessMessage::eIllegalTrap; |
| 2037 | break; |
| 2038 | case ILL_PRVOPC: |
| 2039 | reason = ProcessMessage::ePrivilegedOpcode; |
| 2040 | break; |
| 2041 | case ILL_PRVREG: |
| 2042 | reason = ProcessMessage::ePrivilegedRegister; |
| 2043 | break; |
| 2044 | case ILL_COPROC: |
| 2045 | reason = ProcessMessage::eCoprocessorError; |
| 2046 | break; |
| 2047 | case ILL_BADSTK: |
| 2048 | reason = ProcessMessage::eInternalStackError; |
| 2049 | break; |
| 2050 | } |
| 2051 | |
| 2052 | return reason; |
| 2053 | } |
| 2054 | #endif |
| 2055 | |
| 2056 | #if 0 |
| 2057 | ProcessMessage::CrashReason |
| 2058 | NativeProcessLinux::GetCrashReasonForSIGFPE(const siginfo_t *info) |
| 2059 | { |
| 2060 | ProcessMessage::CrashReason reason; |
| 2061 | assert(info->si_signo == SIGFPE); |
| 2062 | |
| 2063 | reason = ProcessMessage::eInvalidCrashReason; |
| 2064 | |
| 2065 | switch (info->si_code) |
| 2066 | { |
| 2067 | default: |
| 2068 | assert(false && "unexpected si_code for SIGFPE"); |
| 2069 | break; |
| 2070 | case FPE_INTDIV: |
| 2071 | reason = ProcessMessage::eIntegerDivideByZero; |
| 2072 | break; |
| 2073 | case FPE_INTOVF: |
| 2074 | reason = ProcessMessage::eIntegerOverflow; |
| 2075 | break; |
| 2076 | case FPE_FLTDIV: |
| 2077 | reason = ProcessMessage::eFloatDivideByZero; |
| 2078 | break; |
| 2079 | case FPE_FLTOVF: |
| 2080 | reason = ProcessMessage::eFloatOverflow; |
| 2081 | break; |
| 2082 | case FPE_FLTUND: |
| 2083 | reason = ProcessMessage::eFloatUnderflow; |
| 2084 | break; |
| 2085 | case FPE_FLTRES: |
| 2086 | reason = ProcessMessage::eFloatInexactResult; |
| 2087 | break; |
| 2088 | case FPE_FLTINV: |
| 2089 | reason = ProcessMessage::eFloatInvalidOperation; |
| 2090 | break; |
| 2091 | case FPE_FLTSUB: |
| 2092 | reason = ProcessMessage::eFloatSubscriptRange; |
| 2093 | break; |
| 2094 | } |
| 2095 | |
| 2096 | return reason; |
| 2097 | } |
| 2098 | #endif |
| 2099 | |
| 2100 | #if 0 |
| 2101 | ProcessMessage::CrashReason |
| 2102 | NativeProcessLinux::GetCrashReasonForSIGBUS(const siginfo_t *info) |
| 2103 | { |
| 2104 | ProcessMessage::CrashReason reason; |
| 2105 | assert(info->si_signo == SIGBUS); |
| 2106 | |
| 2107 | reason = ProcessMessage::eInvalidCrashReason; |
| 2108 | |
| 2109 | switch (info->si_code) |
| 2110 | { |
| 2111 | default: |
| 2112 | assert(false && "unexpected si_code for SIGBUS"); |
| 2113 | break; |
| 2114 | case BUS_ADRALN: |
| 2115 | reason = ProcessMessage::eIllegalAlignment; |
| 2116 | break; |
| 2117 | case BUS_ADRERR: |
| 2118 | reason = ProcessMessage::eIllegalAddress; |
| 2119 | break; |
| 2120 | case BUS_OBJERR: |
| 2121 | reason = ProcessMessage::eHardwareError; |
| 2122 | break; |
| 2123 | } |
| 2124 | |
| 2125 | return reason; |
| 2126 | } |
| 2127 | #endif |
| 2128 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2129 | Error NativeProcessLinux::ReadMemory(lldb::addr_t addr, void *buf, size_t size, |
| 2130 | size_t &bytes_read) { |
| 2131 | if (ProcessVmReadvSupported()) { |
| 2132 | // The process_vm_readv path is about 50 times faster than ptrace api. We |
| 2133 | // want to use |
| 2134 | // this syscall if it is supported. |
Pavel Labath | df7c699 | 2015-06-17 18:38:49 +0000 | [diff] [blame] | 2135 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2136 | const ::pid_t pid = GetID(); |
Pavel Labath | df7c699 | 2015-06-17 18:38:49 +0000 | [diff] [blame] | 2137 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2138 | struct iovec local_iov, remote_iov; |
| 2139 | local_iov.iov_base = buf; |
| 2140 | local_iov.iov_len = size; |
| 2141 | remote_iov.iov_base = reinterpret_cast<void *>(addr); |
| 2142 | remote_iov.iov_len = size; |
Pavel Labath | df7c699 | 2015-06-17 18:38:49 +0000 | [diff] [blame] | 2143 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2144 | bytes_read = process_vm_readv(pid, &local_iov, 1, &remote_iov, 1, 0); |
| 2145 | const bool success = bytes_read == size; |
Pavel Labath | df7c699 | 2015-06-17 18:38:49 +0000 | [diff] [blame] | 2146 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2147 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Pavel Labath | 19cbe96 | 2015-07-21 13:20:32 +0000 | [diff] [blame] | 2148 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2149 | log->Printf("NativeProcessLinux::%s using process_vm_readv to read %zd " |
| 2150 | "bytes from inferior address 0x%" PRIx64 ": %s", |
| 2151 | __FUNCTION__, size, addr, |
| 2152 | success ? "Success" : strerror(errno)); |
Pavel Labath | 19cbe96 | 2015-07-21 13:20:32 +0000 | [diff] [blame] | 2153 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2154 | if (success) |
| 2155 | return Error(); |
| 2156 | // else |
| 2157 | // the call failed for some reason, let's retry the read using ptrace |
| 2158 | // api. |
| 2159 | } |
Pavel Labath | 19cbe96 | 2015-07-21 13:20:32 +0000 | [diff] [blame] | 2160 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2161 | unsigned char *dst = static_cast<unsigned char *>(buf); |
| 2162 | size_t remainder; |
| 2163 | long data; |
Pavel Labath | 19cbe96 | 2015-07-21 13:20:32 +0000 | [diff] [blame] | 2164 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2165 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_ALL)); |
| 2166 | if (log) |
| 2167 | ProcessPOSIXLog::IncNestLevel(); |
| 2168 | if (log && ProcessPOSIXLog::AtTopNestLevel() && |
| 2169 | log->GetMask().Test(POSIX_LOG_MEMORY)) |
| 2170 | log->Printf("NativeProcessLinux::%s(%p, %p, %zd, _)", __FUNCTION__, |
| 2171 | (void *)addr, buf, size); |
Pavel Labath | 19cbe96 | 2015-07-21 13:20:32 +0000 | [diff] [blame] | 2172 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2173 | for (bytes_read = 0; bytes_read < size; bytes_read += remainder) { |
| 2174 | Error error = NativeProcessLinux::PtraceWrapper( |
| 2175 | PTRACE_PEEKDATA, GetID(), (void *)addr, nullptr, 0, &data); |
| 2176 | if (error.Fail()) { |
| 2177 | if (log) |
Pavel Labath | 19cbe96 | 2015-07-21 13:20:32 +0000 | [diff] [blame] | 2178 | ProcessPOSIXLog::DecNestLevel(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2179 | return error; |
| 2180 | } |
| 2181 | |
| 2182 | remainder = size - bytes_read; |
| 2183 | remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; |
| 2184 | |
| 2185 | // Copy the data into our buffer |
| 2186 | memcpy(dst, &data, remainder); |
| 2187 | |
| 2188 | if (log && ProcessPOSIXLog::AtTopNestLevel() && |
| 2189 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || |
| 2190 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && |
| 2191 | size <= POSIX_LOG_MEMORY_SHORT_BYTES))) { |
| 2192 | uintptr_t print_dst = 0; |
| 2193 | // Format bytes from data by moving into print_dst for log output |
| 2194 | for (unsigned i = 0; i < remainder; ++i) |
| 2195 | print_dst |= (((data >> i * 8) & 0xFF) << i * 8); |
| 2196 | log->Printf("NativeProcessLinux::%s() [0x%" PRIx64 "]:0x%" PRIx64 |
| 2197 | " (0x%" PRIx64 ")", |
| 2198 | __FUNCTION__, addr, uint64_t(print_dst), uint64_t(data)); |
| 2199 | } |
| 2200 | addr += k_ptrace_word_size; |
| 2201 | dst += k_ptrace_word_size; |
| 2202 | } |
| 2203 | |
| 2204 | if (log) |
| 2205 | ProcessPOSIXLog::DecNestLevel(); |
| 2206 | return Error(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2207 | } |
| 2208 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2209 | Error NativeProcessLinux::ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, |
| 2210 | size_t size, |
| 2211 | size_t &bytes_read) { |
| 2212 | Error error = ReadMemory(addr, buf, size, bytes_read); |
| 2213 | if (error.Fail()) |
Pavel Labath | 19cbe96 | 2015-07-21 13:20:32 +0000 | [diff] [blame] | 2214 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2215 | return m_breakpoint_list.RemoveTrapsFromBuffer(addr, buf, size); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2216 | } |
| 2217 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2218 | Error NativeProcessLinux::WriteMemory(lldb::addr_t addr, const void *buf, |
| 2219 | size_t size, size_t &bytes_written) { |
| 2220 | const unsigned char *src = static_cast<const unsigned char *>(buf); |
| 2221 | size_t remainder; |
| 2222 | Error error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2223 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2224 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_ALL)); |
| 2225 | if (log) |
| 2226 | ProcessPOSIXLog::IncNestLevel(); |
| 2227 | if (log && ProcessPOSIXLog::AtTopNestLevel() && |
| 2228 | log->GetMask().Test(POSIX_LOG_MEMORY)) |
| 2229 | log->Printf("NativeProcessLinux::%s(0x%" PRIx64 ", %p, %zu)", __FUNCTION__, |
| 2230 | addr, buf, size); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2231 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2232 | for (bytes_written = 0; bytes_written < size; bytes_written += remainder) { |
| 2233 | remainder = size - bytes_written; |
| 2234 | remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 2235 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2236 | if (remainder == k_ptrace_word_size) { |
| 2237 | unsigned long data = 0; |
| 2238 | memcpy(&data, src, k_ptrace_word_size); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2239 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2240 | if (log && ProcessPOSIXLog::AtTopNestLevel() && |
| 2241 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || |
| 2242 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && |
| 2243 | size <= POSIX_LOG_MEMORY_SHORT_BYTES))) |
| 2244 | log->Printf("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, |
| 2245 | (void *)addr, *(const unsigned long *)src, data); |
| 2246 | |
| 2247 | error = NativeProcessLinux::PtraceWrapper(PTRACE_POKEDATA, GetID(), |
| 2248 | (void *)addr, (void *)data); |
| 2249 | if (error.Fail()) { |
| 2250 | if (log) |
| 2251 | ProcessPOSIXLog::DecNestLevel(); |
| 2252 | return error; |
| 2253 | } |
| 2254 | } else { |
| 2255 | unsigned char buff[8]; |
| 2256 | size_t bytes_read; |
| 2257 | error = ReadMemory(addr, buff, k_ptrace_word_size, bytes_read); |
| 2258 | if (error.Fail()) { |
| 2259 | if (log) |
| 2260 | ProcessPOSIXLog::DecNestLevel(); |
| 2261 | return error; |
| 2262 | } |
| 2263 | |
| 2264 | memcpy(buff, src, remainder); |
| 2265 | |
| 2266 | size_t bytes_written_rec; |
| 2267 | error = WriteMemory(addr, buff, k_ptrace_word_size, bytes_written_rec); |
| 2268 | if (error.Fail()) { |
| 2269 | if (log) |
| 2270 | ProcessPOSIXLog::DecNestLevel(); |
| 2271 | return error; |
| 2272 | } |
| 2273 | |
| 2274 | if (log && ProcessPOSIXLog::AtTopNestLevel() && |
| 2275 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || |
| 2276 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && |
| 2277 | size <= POSIX_LOG_MEMORY_SHORT_BYTES))) |
| 2278 | log->Printf("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, |
| 2279 | (void *)addr, *(const unsigned long *)src, |
| 2280 | *(unsigned long *)buff); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2281 | } |
| 2282 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2283 | addr += k_ptrace_word_size; |
| 2284 | src += k_ptrace_word_size; |
| 2285 | } |
| 2286 | if (log) |
| 2287 | ProcessPOSIXLog::DecNestLevel(); |
| 2288 | return error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2289 | } |
| 2290 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2291 | Error NativeProcessLinux::GetSignalInfo(lldb::tid_t tid, void *siginfo) { |
| 2292 | return PtraceWrapper(PTRACE_GETSIGINFO, tid, nullptr, siginfo); |
| 2293 | } |
Pavel Labath | 1dbc6c9 | 2015-05-12 08:35:33 +0000 | [diff] [blame] | 2294 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2295 | Error NativeProcessLinux::GetEventMessage(lldb::tid_t tid, |
| 2296 | unsigned long *message) { |
| 2297 | return PtraceWrapper(PTRACE_GETEVENTMSG, tid, nullptr, message); |
| 2298 | } |
Pavel Labath | 1dbc6c9 | 2015-05-12 08:35:33 +0000 | [diff] [blame] | 2299 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2300 | Error NativeProcessLinux::Detach(lldb::tid_t tid) { |
| 2301 | if (tid == LLDB_INVALID_THREAD_ID) |
| 2302 | return Error(); |
Pavel Labath | 1dbc6c9 | 2015-05-12 08:35:33 +0000 | [diff] [blame] | 2303 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2304 | return PtraceWrapper(PTRACE_DETACH, tid); |
| 2305 | } |
| 2306 | |
| 2307 | bool NativeProcessLinux::HasThreadNoLock(lldb::tid_t thread_id) { |
| 2308 | for (auto thread_sp : m_threads) { |
| 2309 | assert(thread_sp && "thread list should not contain NULL threads"); |
| 2310 | if (thread_sp->GetID() == thread_id) { |
| 2311 | // We have this thread. |
| 2312 | return true; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2313 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2314 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2315 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2316 | // We don't have this thread. |
| 2317 | return false; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2318 | } |
| 2319 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2320 | bool NativeProcessLinux::StopTrackingThread(lldb::tid_t thread_id) { |
| 2321 | Log *const log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2322 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2323 | if (log) |
| 2324 | log->Printf("NativeProcessLinux::%s (tid: %" PRIu64 ")", __FUNCTION__, |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2325 | thread_id); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2326 | |
| 2327 | bool found = false; |
| 2328 | |
| 2329 | for (auto it = m_threads.begin(); it != m_threads.end(); ++it) { |
| 2330 | if (*it && ((*it)->GetID() == thread_id)) { |
| 2331 | m_threads.erase(it); |
| 2332 | found = true; |
| 2333 | break; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2334 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2335 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2336 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2337 | SignalIfAllThreadsStopped(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2338 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2339 | return found; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2340 | } |
| 2341 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2342 | NativeThreadLinuxSP NativeProcessLinux::AddThread(lldb::tid_t thread_id) { |
| 2343 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2344 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2345 | if (log) { |
| 2346 | log->Printf("NativeProcessLinux::%s pid %" PRIu64 |
| 2347 | " adding thread with tid %" PRIu64, |
| 2348 | __FUNCTION__, GetID(), thread_id); |
| 2349 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2350 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2351 | assert(!HasThreadNoLock(thread_id) && |
| 2352 | "attempted to add a thread by id that already exists"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2353 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2354 | // If this is the first thread, save it as the current thread |
| 2355 | if (m_threads.empty()) |
| 2356 | SetCurrentThreadID(thread_id); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2357 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2358 | auto thread_sp = std::make_shared<NativeThreadLinux>(this, thread_id); |
| 2359 | m_threads.push_back(thread_sp); |
| 2360 | return thread_sp; |
| 2361 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2362 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2363 | Error NativeProcessLinux::FixupBreakpointPCAsNeeded(NativeThreadLinux &thread) { |
| 2364 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2365 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2366 | Error error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2367 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2368 | // Find out the size of a breakpoint (might depend on where we are in the |
| 2369 | // code). |
| 2370 | NativeRegisterContextSP context_sp = thread.GetRegisterContext(); |
| 2371 | if (!context_sp) { |
| 2372 | error.SetErrorString("cannot get a NativeRegisterContext for the thread"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2373 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2374 | log->Printf("NativeProcessLinux::%s failed: %s", __FUNCTION__, |
| 2375 | error.AsCString()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2376 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2377 | } |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2378 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2379 | uint32_t breakpoint_size = 0; |
| 2380 | error = GetSoftwareBreakpointPCOffset(breakpoint_size); |
| 2381 | if (error.Fail()) { |
Pavel Labath | 1dbc6c9 | 2015-05-12 08:35:33 +0000 | [diff] [blame] | 2382 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2383 | log->Printf("NativeProcessLinux::%s GetBreakpointSize() failed: %s", |
| 2384 | __FUNCTION__, error.AsCString()); |
| 2385 | return error; |
| 2386 | } else { |
| 2387 | if (log) |
| 2388 | log->Printf("NativeProcessLinux::%s breakpoint size: %" PRIu32, |
| 2389 | __FUNCTION__, breakpoint_size); |
| 2390 | } |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 2391 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2392 | // First try probing for a breakpoint at a software breakpoint location: PC - |
| 2393 | // breakpoint size. |
| 2394 | const lldb::addr_t initial_pc_addr = |
| 2395 | context_sp->GetPCfromBreakpointLocation(); |
| 2396 | lldb::addr_t breakpoint_addr = initial_pc_addr; |
| 2397 | if (breakpoint_size > 0) { |
| 2398 | // Do not allow breakpoint probe to wrap around. |
| 2399 | if (breakpoint_addr >= breakpoint_size) |
| 2400 | breakpoint_addr -= breakpoint_size; |
| 2401 | } |
| 2402 | |
| 2403 | // Check if we stopped because of a breakpoint. |
| 2404 | NativeBreakpointSP breakpoint_sp; |
| 2405 | error = m_breakpoint_list.GetBreakpoint(breakpoint_addr, breakpoint_sp); |
| 2406 | if (!error.Success() || !breakpoint_sp) { |
| 2407 | // We didn't find one at a software probe location. Nothing to do. |
| 2408 | if (log) |
| 2409 | log->Printf( |
| 2410 | "NativeProcessLinux::%s pid %" PRIu64 |
| 2411 | " no lldb breakpoint found at current pc with adjustment: 0x%" PRIx64, |
| 2412 | __FUNCTION__, GetID(), breakpoint_addr); |
| 2413 | return Error(); |
| 2414 | } |
| 2415 | |
| 2416 | // If the breakpoint is not a software breakpoint, nothing to do. |
| 2417 | if (!breakpoint_sp->IsSoftwareBreakpoint()) { |
| 2418 | if (log) |
| 2419 | log->Printf("NativeProcessLinux::%s pid %" PRIu64 |
| 2420 | " breakpoint found at 0x%" PRIx64 |
| 2421 | ", not software, nothing to adjust", |
| 2422 | __FUNCTION__, GetID(), breakpoint_addr); |
| 2423 | return Error(); |
| 2424 | } |
| 2425 | |
| 2426 | // |
| 2427 | // We have a software breakpoint and need to adjust the PC. |
| 2428 | // |
| 2429 | |
| 2430 | // Sanity check. |
| 2431 | if (breakpoint_size == 0) { |
| 2432 | // Nothing to do! How did we get here? |
| 2433 | if (log) |
| 2434 | log->Printf( |
| 2435 | "NativeProcessLinux::%s pid %" PRIu64 |
| 2436 | " breakpoint found at 0x%" PRIx64 |
| 2437 | ", it is software, but the size is zero, nothing to do (unexpected)", |
| 2438 | __FUNCTION__, GetID(), breakpoint_addr); |
| 2439 | return Error(); |
| 2440 | } |
| 2441 | |
| 2442 | // Change the program counter. |
| 2443 | if (log) |
| 2444 | log->Printf("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 |
| 2445 | ": changing PC from 0x%" PRIx64 " to 0x%" PRIx64, |
| 2446 | __FUNCTION__, GetID(), thread.GetID(), initial_pc_addr, |
| 2447 | breakpoint_addr); |
| 2448 | |
| 2449 | error = context_sp->SetPC(breakpoint_addr); |
| 2450 | if (error.Fail()) { |
| 2451 | if (log) |
| 2452 | log->Printf("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 |
| 2453 | ": failed to set PC: %s", |
| 2454 | __FUNCTION__, GetID(), thread.GetID(), error.AsCString()); |
| 2455 | return error; |
| 2456 | } |
| 2457 | |
| 2458 | return error; |
| 2459 | } |
| 2460 | |
| 2461 | Error NativeProcessLinux::GetLoadedModuleFileSpec(const char *module_path, |
| 2462 | FileSpec &file_spec) { |
| 2463 | FileSpec module_file_spec(module_path, true); |
| 2464 | |
| 2465 | bool found = false; |
| 2466 | file_spec.Clear(); |
| 2467 | ProcFileReader::ProcessLineByLine( |
| 2468 | GetID(), "maps", [&](const std::string &line) { |
| 2469 | SmallVector<StringRef, 16> columns; |
| 2470 | StringRef(line).split(columns, " ", -1, false); |
| 2471 | if (columns.size() < 6) |
| 2472 | return true; // continue searching |
| 2473 | |
| 2474 | FileSpec this_file_spec(columns[5].str().c_str(), false); |
| 2475 | if (this_file_spec.GetFilename() != module_file_spec.GetFilename()) |
| 2476 | return true; // continue searching |
| 2477 | |
| 2478 | file_spec = this_file_spec; |
| 2479 | found = true; |
| 2480 | return false; // we are done |
| 2481 | }); |
| 2482 | |
| 2483 | if (!found) |
| 2484 | return Error("Module file (%s) not found in /proc/%" PRIu64 "/maps file!", |
| 2485 | module_file_spec.GetFilename().AsCString(), GetID()); |
| 2486 | |
| 2487 | return Error(); |
| 2488 | } |
| 2489 | |
| 2490 | Error NativeProcessLinux::GetFileLoadAddress(const llvm::StringRef &file_name, |
| 2491 | lldb::addr_t &load_addr) { |
| 2492 | load_addr = LLDB_INVALID_ADDRESS; |
| 2493 | Error error = ProcFileReader::ProcessLineByLine( |
| 2494 | GetID(), "maps", [&](const std::string &line) -> bool { |
| 2495 | StringRef maps_row(line); |
| 2496 | |
| 2497 | SmallVector<StringRef, 16> maps_columns; |
| 2498 | maps_row.split(maps_columns, StringRef(" "), -1, false); |
| 2499 | |
| 2500 | if (maps_columns.size() < 6) { |
| 2501 | // Return true to continue reading the proc file |
| 2502 | return true; |
| 2503 | } |
| 2504 | |
| 2505 | if (maps_columns[5] == file_name) { |
| 2506 | StringExtractor addr_extractor(maps_columns[0].str().c_str()); |
| 2507 | load_addr = addr_extractor.GetHexMaxU64(false, LLDB_INVALID_ADDRESS); |
| 2508 | |
| 2509 | // Return false to stop reading the proc file further |
| 2510 | return false; |
| 2511 | } |
| 2512 | |
| 2513 | // Return true to continue reading the proc file |
| 2514 | return true; |
| 2515 | }); |
| 2516 | return error; |
| 2517 | } |
| 2518 | |
| 2519 | NativeThreadLinuxSP NativeProcessLinux::GetThreadByID(lldb::tid_t tid) { |
| 2520 | return std::static_pointer_cast<NativeThreadLinux>( |
| 2521 | NativeProcessProtocol::GetThreadByID(tid)); |
| 2522 | } |
| 2523 | |
| 2524 | Error NativeProcessLinux::ResumeThread(NativeThreadLinux &thread, |
| 2525 | lldb::StateType state, int signo) { |
| 2526 | Log *const log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD); |
| 2527 | |
| 2528 | if (log) |
| 2529 | log->Printf("NativeProcessLinux::%s (tid: %" PRIu64 ")", __FUNCTION__, |
| 2530 | thread.GetID()); |
| 2531 | |
| 2532 | // Before we do the resume below, first check if we have a pending |
| 2533 | // stop notification that is currently waiting for |
| 2534 | // all threads to stop. This is potentially a buggy situation since |
| 2535 | // we're ostensibly waiting for threads to stop before we send out the |
| 2536 | // pending notification, and here we are resuming one before we send |
| 2537 | // out the pending stop notification. |
| 2538 | if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID && log) { |
| 2539 | log->Printf("NativeProcessLinux::%s about to resume tid %" PRIu64 |
| 2540 | " per explicit request but we have a pending stop notification " |
| 2541 | "(tid %" PRIu64 ") that is actively waiting for this thread to " |
| 2542 | "stop. Valid sequence of events?", |
| 2543 | __FUNCTION__, thread.GetID(), m_pending_notification_tid); |
| 2544 | } |
| 2545 | |
| 2546 | // Request a resume. We expect this to be synchronous and the system |
| 2547 | // to reflect it is running after this completes. |
| 2548 | switch (state) { |
| 2549 | case eStateRunning: { |
| 2550 | const auto resume_result = thread.Resume(signo); |
| 2551 | if (resume_result.Success()) |
| 2552 | SetState(eStateRunning, true); |
| 2553 | return resume_result; |
| 2554 | } |
| 2555 | case eStateStepping: { |
| 2556 | const auto step_result = thread.SingleStep(signo); |
| 2557 | if (step_result.Success()) |
| 2558 | SetState(eStateRunning, true); |
| 2559 | return step_result; |
| 2560 | } |
| 2561 | default: |
| 2562 | if (log) |
| 2563 | log->Printf("NativeProcessLinux::%s Unhandled state %s.", __FUNCTION__, |
| 2564 | StateAsCString(state)); |
| 2565 | llvm_unreachable("Unhandled state for resume"); |
| 2566 | } |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 2567 | } |
| 2568 | |
| 2569 | //===----------------------------------------------------------------------===// |
| 2570 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2571 | void NativeProcessLinux::StopRunningThreads(const lldb::tid_t triggering_tid) { |
| 2572 | Log *const log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD); |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 2573 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2574 | if (log) { |
| 2575 | log->Printf("NativeProcessLinux::%s about to process event: " |
| 2576 | "(triggering_tid: %" PRIu64 ")", |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 2577 | __FUNCTION__, triggering_tid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2578 | } |
| 2579 | |
| 2580 | m_pending_notification_tid = triggering_tid; |
| 2581 | |
| 2582 | // Request a stop for all the thread stops that need to be stopped |
| 2583 | // and are not already known to be stopped. |
| 2584 | for (const auto &thread_sp : m_threads) { |
| 2585 | if (StateIsRunningState(thread_sp->GetState())) |
| 2586 | static_pointer_cast<NativeThreadLinux>(thread_sp)->RequestStop(); |
| 2587 | } |
| 2588 | |
| 2589 | SignalIfAllThreadsStopped(); |
| 2590 | |
| 2591 | if (log) { |
| 2592 | log->Printf("NativeProcessLinux::%s event processing done", __FUNCTION__); |
| 2593 | } |
| 2594 | } |
| 2595 | |
| 2596 | void NativeProcessLinux::SignalIfAllThreadsStopped() { |
| 2597 | if (m_pending_notification_tid == LLDB_INVALID_THREAD_ID) |
| 2598 | return; // No pending notification. Nothing to do. |
| 2599 | |
| 2600 | for (const auto &thread_sp : m_threads) { |
| 2601 | if (StateIsRunningState(thread_sp->GetState())) |
| 2602 | return; // Some threads are still running. Don't signal yet. |
| 2603 | } |
| 2604 | |
| 2605 | // We have a pending notification and all threads have stopped. |
| 2606 | Log *log( |
| 2607 | GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); |
| 2608 | |
| 2609 | // Clear any temporary breakpoints we used to implement software single |
| 2610 | // stepping. |
| 2611 | for (const auto &thread_info : m_threads_stepping_with_breakpoint) { |
| 2612 | Error error = RemoveBreakpoint(thread_info.second); |
| 2613 | if (error.Fail()) |
| 2614 | if (log) |
| 2615 | log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 |
| 2616 | " remove stepping breakpoint: %s", |
| 2617 | __FUNCTION__, thread_info.first, error.AsCString()); |
| 2618 | } |
| 2619 | m_threads_stepping_with_breakpoint.clear(); |
| 2620 | |
| 2621 | // Notify the delegate about the stop |
| 2622 | SetCurrentThreadID(m_pending_notification_tid); |
| 2623 | SetState(StateType::eStateStopped, true); |
| 2624 | m_pending_notification_tid = LLDB_INVALID_THREAD_ID; |
| 2625 | } |
| 2626 | |
| 2627 | void NativeProcessLinux::ThreadWasCreated(NativeThreadLinux &thread) { |
| 2628 | Log *const log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD); |
| 2629 | |
| 2630 | if (log) |
| 2631 | log->Printf("NativeProcessLinux::%s (tid: %" PRIu64 ")", __FUNCTION__, |
| 2632 | thread.GetID()); |
| 2633 | |
| 2634 | if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID && |
| 2635 | StateIsRunningState(thread.GetState())) { |
| 2636 | // We will need to wait for this new thread to stop as well before firing |
| 2637 | // the |
| 2638 | // notification. |
| 2639 | thread.RequestStop(); |
| 2640 | } |
| 2641 | } |
| 2642 | |
| 2643 | void NativeProcessLinux::SigchldHandler() { |
| 2644 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 2645 | // Process all pending waitpid notifications. |
| 2646 | while (true) { |
| 2647 | int status = -1; |
| 2648 | ::pid_t wait_pid = waitpid(-1, &status, __WALL | __WNOTHREAD | WNOHANG); |
| 2649 | |
| 2650 | if (wait_pid == 0) |
| 2651 | break; // We are done. |
| 2652 | |
| 2653 | if (wait_pid == -1) { |
| 2654 | if (errno == EINTR) |
| 2655 | continue; |
| 2656 | |
| 2657 | Error error(errno, eErrorTypePOSIX); |
| 2658 | if (log) |
| 2659 | log->Printf("NativeProcessLinux::%s waitpid (-1, &status, __WALL | " |
| 2660 | "__WNOTHREAD | WNOHANG) failed: %s", |
| 2661 | __FUNCTION__, error.AsCString()); |
| 2662 | break; |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 2663 | } |
| 2664 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2665 | bool exited = false; |
| 2666 | int signal = 0; |
| 2667 | int exit_status = 0; |
| 2668 | const char *status_cstr = nullptr; |
| 2669 | if (WIFSTOPPED(status)) { |
| 2670 | signal = WSTOPSIG(status); |
| 2671 | status_cstr = "STOPPED"; |
| 2672 | } else if (WIFEXITED(status)) { |
| 2673 | exit_status = WEXITSTATUS(status); |
| 2674 | status_cstr = "EXITED"; |
| 2675 | exited = true; |
| 2676 | } else if (WIFSIGNALED(status)) { |
| 2677 | signal = WTERMSIG(status); |
| 2678 | status_cstr = "SIGNALED"; |
| 2679 | if (wait_pid == static_cast<::pid_t>(GetID())) { |
| 2680 | exited = true; |
| 2681 | exit_status = -1; |
| 2682 | } |
| 2683 | } else |
| 2684 | status_cstr = "(\?\?\?)"; |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 2685 | |
Pavel Labath | 5eb721e | 2015-05-07 08:30:31 +0000 | [diff] [blame] | 2686 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2687 | log->Printf("NativeProcessLinux::%s: waitpid (-1, &status, __WALL | " |
| 2688 | "__WNOTHREAD | WNOHANG)" |
| 2689 | "=> pid = %" PRIi32 |
| 2690 | ", status = 0x%8.8x (%s), signal = %i, exit_state = %i", |
| 2691 | __FUNCTION__, wait_pid, status, status_cstr, signal, |
| 2692 | exit_status); |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 2693 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2694 | MonitorCallback(wait_pid, exited, signal, exit_status); |
| 2695 | } |
Tamas Berghammer | 068f8a7 | 2015-05-26 11:58:52 +0000 | [diff] [blame] | 2696 | } |
| 2697 | |
| 2698 | // Wrapper for ptrace to catch errors and log calls. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2699 | // Note that ptrace sets errno on error because -1 can be a valid result (i.e. |
| 2700 | // for PTRACE_PEEK*) |
| 2701 | Error NativeProcessLinux::PtraceWrapper(int req, lldb::pid_t pid, void *addr, |
| 2702 | void *data, size_t data_size, |
| 2703 | long *result) { |
| 2704 | Error error; |
| 2705 | long int ret; |
Tamas Berghammer | 068f8a7 | 2015-05-26 11:58:52 +0000 | [diff] [blame] | 2706 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2707 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); |
Tamas Berghammer | 068f8a7 | 2015-05-26 11:58:52 +0000 | [diff] [blame] | 2708 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2709 | PtraceDisplayBytes(req, data, data_size); |
Tamas Berghammer | 068f8a7 | 2015-05-26 11:58:52 +0000 | [diff] [blame] | 2710 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2711 | errno = 0; |
| 2712 | if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET) |
| 2713 | ret = ptrace(static_cast<__ptrace_request>(req), static_cast<::pid_t>(pid), |
| 2714 | *(unsigned int *)addr, data); |
| 2715 | else |
| 2716 | ret = ptrace(static_cast<__ptrace_request>(req), static_cast<::pid_t>(pid), |
| 2717 | addr, data); |
Tamas Berghammer | 068f8a7 | 2015-05-26 11:58:52 +0000 | [diff] [blame] | 2718 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2719 | if (ret == -1) |
| 2720 | error.SetErrorToErrno(); |
Tamas Berghammer | 068f8a7 | 2015-05-26 11:58:52 +0000 | [diff] [blame] | 2721 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2722 | if (result) |
| 2723 | *result = ret; |
Pavel Labath | 4a9babb | 2015-06-30 17:04:49 +0000 | [diff] [blame] | 2724 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2725 | if (log) |
| 2726 | log->Printf("ptrace(%d, %" PRIu64 ", %p, %p, %zu)=%lX", req, pid, addr, |
| 2727 | data, data_size, ret); |
Tamas Berghammer | 068f8a7 | 2015-05-26 11:58:52 +0000 | [diff] [blame] | 2728 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2729 | PtraceDisplayBytes(req, data, data_size); |
Tamas Berghammer | 068f8a7 | 2015-05-26 11:58:52 +0000 | [diff] [blame] | 2730 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2731 | if (log && error.GetError() != 0) { |
| 2732 | const char *str; |
| 2733 | switch (error.GetError()) { |
| 2734 | case ESRCH: |
| 2735 | str = "ESRCH"; |
| 2736 | break; |
| 2737 | case EINVAL: |
| 2738 | str = "EINVAL"; |
| 2739 | break; |
| 2740 | case EBUSY: |
| 2741 | str = "EBUSY"; |
| 2742 | break; |
| 2743 | case EPERM: |
| 2744 | str = "EPERM"; |
| 2745 | break; |
| 2746 | default: |
| 2747 | str = error.AsCString(); |
Tamas Berghammer | 068f8a7 | 2015-05-26 11:58:52 +0000 | [diff] [blame] | 2748 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2749 | log->Printf("ptrace() failed; errno=%d (%s)", error.GetError(), str); |
| 2750 | } |
Tamas Berghammer | 068f8a7 | 2015-05-26 11:58:52 +0000 | [diff] [blame] | 2751 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2752 | return error; |
Tamas Berghammer | 068f8a7 | 2015-05-26 11:58:52 +0000 | [diff] [blame] | 2753 | } |