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