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