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