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