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