Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1 | //===-- ProcessPOSIX.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 | |
Daniel Malea | 93a6430 | 2012-12-05 00:20:57 +0000 | [diff] [blame] | 10 | #include "lldb/lldb-python.h" |
| 11 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 12 | // C Includes |
| 13 | #include <errno.h> |
| 14 | |
| 15 | // C++ Includes |
| 16 | // Other libraries and framework includes |
Matt Kopec | e9ea0da | 2013-05-07 19:29:28 +0000 | [diff] [blame] | 17 | #include "lldb/Breakpoint/Watchpoint.h" |
Greg Clayton | dcbfd19 | 2012-09-04 14:55:50 +0000 | [diff] [blame] | 18 | #include "lldb/Core/Module.h" |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 19 | #include "lldb/Core/PluginManager.h" |
| 20 | #include "lldb/Core/State.h" |
Daniel Malea | 6217d2a | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 21 | #include "lldb/Host/FileSpec.h" |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 22 | #include "lldb/Host/Host.h" |
| 23 | #include "lldb/Symbol/ObjectFile.h" |
| 24 | #include "lldb/Target/DynamicLoader.h" |
Andrew Kaylor | bf9b4c1 | 2013-05-07 22:46:38 +0000 | [diff] [blame] | 25 | #include "lldb/Target/Platform.h" |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 26 | #include "lldb/Target/Target.h" |
| 27 | |
| 28 | #include "ProcessPOSIX.h" |
| 29 | #include "ProcessPOSIXLog.h" |
| 30 | #include "Plugins/Process/Utility/InferiorCallPOSIX.h" |
| 31 | #include "ProcessMonitor.h" |
| 32 | #include "POSIXThread.h" |
| 33 | |
| 34 | using namespace lldb; |
| 35 | using namespace lldb_private; |
| 36 | |
| 37 | //------------------------------------------------------------------------------ |
| 38 | // Static functions. |
| 39 | #if 0 |
| 40 | Process* |
| 41 | ProcessPOSIX::CreateInstance(Target& target, Listener &listener) |
| 42 | { |
| 43 | return new ProcessPOSIX(target, listener); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | void |
| 48 | ProcessPOSIX::Initialize() |
| 49 | { |
| 50 | static bool g_initialized = false; |
| 51 | |
| 52 | if (!g_initialized) |
| 53 | { |
| 54 | g_initialized = true; |
| 55 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 56 | GetPluginDescriptionStatic(), |
| 57 | CreateInstance); |
| 58 | |
| 59 | Log::Callbacks log_callbacks = { |
| 60 | ProcessPOSIXLog::DisableLog, |
| 61 | ProcessPOSIXLog::EnableLog, |
| 62 | ProcessPOSIXLog::ListLogCategories |
| 63 | }; |
| 64 | |
| 65 | Log::RegisterLogChannel (ProcessPOSIX::GetPluginNameStatic(), log_callbacks); |
| 66 | } |
| 67 | } |
| 68 | #endif |
| 69 | |
| 70 | //------------------------------------------------------------------------------ |
| 71 | // Constructors and destructors. |
| 72 | |
| 73 | ProcessPOSIX::ProcessPOSIX(Target& target, Listener &listener) |
| 74 | : Process(target, listener), |
Johnny Chen | 7b9f93a | 2012-04-14 00:54:42 +0000 | [diff] [blame] | 75 | m_byte_order(lldb::endian::InlHostByteOrder()), |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 76 | m_monitor(NULL), |
| 77 | m_module(NULL), |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 78 | m_message_mutex (Mutex::eMutexTypeRecursive), |
Matt Kopec | b291044 | 2013-07-09 15:09:45 +0000 | [diff] [blame] | 79 | m_exit_now(false), |
| 80 | m_seen_initial_stop() |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 81 | { |
| 82 | // FIXME: Putting this code in the ctor and saving the byte order in a |
| 83 | // member variable is a hack to avoid const qual issues in GetByteOrder. |
Johnny Chen | 7b9f93a | 2012-04-14 00:54:42 +0000 | [diff] [blame] | 84 | lldb::ModuleSP module = GetTarget().GetExecutableModule(); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 85 | if (module && module->GetObjectFile()) |
Johnny Chen | 7b9f93a | 2012-04-14 00:54:42 +0000 | [diff] [blame] | 86 | m_byte_order = module->GetObjectFile()->GetByteOrder(); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | ProcessPOSIX::~ProcessPOSIX() |
| 90 | { |
| 91 | delete m_monitor; |
| 92 | } |
| 93 | |
| 94 | //------------------------------------------------------------------------------ |
| 95 | // Process protocol. |
Andrew Kaylor | bc68b43 | 2013-07-10 21:57:27 +0000 | [diff] [blame] | 96 | void |
| 97 | ProcessPOSIX::Finalize() |
| 98 | { |
| 99 | Process::Finalize(); |
| 100 | |
| 101 | if (m_monitor) |
| 102 | m_monitor->StopMonitor(); |
| 103 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 104 | |
| 105 | bool |
| 106 | ProcessPOSIX::CanDebug(Target &target, bool plugin_specified_by_name) |
| 107 | { |
| 108 | // For now we are just making sure the file exists for a given module |
| 109 | ModuleSP exe_module_sp(target.GetExecutableModule()); |
| 110 | if (exe_module_sp.get()) |
| 111 | return exe_module_sp->GetFileSpec().Exists(); |
Andrew Kaylor | bf9b4c1 | 2013-05-07 22:46:38 +0000 | [diff] [blame] | 112 | // If there is no executable module, we return true since we might be preparing to attach. |
| 113 | return true; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | Error |
| 117 | ProcessPOSIX::DoAttachToProcessWithID(lldb::pid_t pid) |
| 118 | { |
| 119 | Error error; |
| 120 | assert(m_monitor == NULL); |
| 121 | |
Ashok Thirumurthi | 0118635 | 2013-03-28 16:02:31 +0000 | [diff] [blame] | 122 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS)); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 123 | if (log && log->GetMask().Test(POSIX_LOG_VERBOSE)) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 124 | log->Printf ("ProcessPOSIX::%s(pid = %" PRIi64 ")", __FUNCTION__, GetID()); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 125 | |
Andrew Kaylor | 6578cb6 | 2013-07-09 22:36:48 +0000 | [diff] [blame] | 126 | m_monitor = new ProcessMonitor(this, pid, error); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 127 | |
| 128 | if (!error.Success()) |
| 129 | return error; |
| 130 | |
Andrew Kaylor | bf9b4c1 | 2013-05-07 22:46:38 +0000 | [diff] [blame] | 131 | PlatformSP platform_sp (m_target.GetPlatform ()); |
| 132 | assert (platform_sp.get()); |
| 133 | if (!platform_sp) |
| 134 | return error; // FIXME: Detatch? |
| 135 | |
| 136 | // Find out what we can about this process |
| 137 | ProcessInstanceInfo process_info; |
| 138 | platform_sp->GetProcessInfo (pid, process_info); |
| 139 | |
| 140 | // Resolve the executable module |
| 141 | ModuleSP exe_module_sp; |
| 142 | FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths()); |
| 143 | error = platform_sp->ResolveExecutable(process_info.GetExecutableFile(), |
| 144 | m_target.GetArchitecture(), |
| 145 | exe_module_sp, |
| 146 | executable_search_paths.GetSize() ? &executable_search_paths : NULL); |
Ed Maste | be0b55d | 2013-07-04 18:25:34 +0000 | [diff] [blame] | 147 | if (!error.Success()) |
| 148 | return error; |
Andrew Kaylor | bf9b4c1 | 2013-05-07 22:46:38 +0000 | [diff] [blame] | 149 | |
| 150 | // Fix the target architecture if necessary |
| 151 | const ArchSpec &module_arch = exe_module_sp->GetArchitecture(); |
| 152 | if (module_arch.IsValid() && !m_target.GetArchitecture().IsExactMatch(module_arch)) |
| 153 | m_target.SetArchitecture(module_arch); |
| 154 | |
| 155 | // Initialize the target module list |
| 156 | m_target.SetExecutableModule (exe_module_sp, true); |
| 157 | |
Andrew Kaylor | bf9b4c1 | 2013-05-07 22:46:38 +0000 | [diff] [blame] | 158 | SetSTDIOFileDescriptor(m_monitor->GetTerminalFD()); |
| 159 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 160 | SetID(pid); |
Andrew Kaylor | bf9b4c1 | 2013-05-07 22:46:38 +0000 | [diff] [blame] | 161 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 162 | return error; |
| 163 | } |
| 164 | |
| 165 | Error |
Greg Clayton | e2186ed | 2012-09-07 17:51:47 +0000 | [diff] [blame] | 166 | ProcessPOSIX::DoAttachToProcessWithID (lldb::pid_t pid, const ProcessAttachInfo &attach_info) |
| 167 | { |
| 168 | return DoAttachToProcessWithID(pid); |
| 169 | } |
| 170 | |
| 171 | Error |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 172 | ProcessPOSIX::WillLaunch(Module* module) |
| 173 | { |
| 174 | Error error; |
| 175 | return error; |
| 176 | } |
| 177 | |
| 178 | const char * |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 179 | ProcessPOSIX::GetFilePath(const lldb_private::FileAction *file_action, const char *default_path) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 180 | { |
| 181 | const char *pts_name = "/dev/pts/"; |
| 182 | const char *path = NULL; |
| 183 | |
| 184 | if (file_action) |
| 185 | { |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 186 | if (file_action->GetAction() == FileAction::eFileActionOpen) |
Saleem Abdulrasool | 03700de | 2014-03-08 20:47:03 +0000 | [diff] [blame] | 187 | { |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 188 | path = file_action->GetPath(); |
| 189 | // By default the stdio paths passed in will be pseudo-terminal |
| 190 | // (/dev/pts). If so, convert to using a different default path |
| 191 | // instead to redirect I/O to the debugger console. This should |
| 192 | // also handle user overrides to /dev/null or a different file. |
Saleem Abdulrasool | d41fca1 | 2014-03-08 20:47:07 +0000 | [diff] [blame] | 193 | if (!path || ::strncmp(path, pts_name, ::strlen(pts_name)) == 0) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 194 | path = default_path; |
Saleem Abdulrasool | 03700de | 2014-03-08 20:47:03 +0000 | [diff] [blame] | 195 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | return path; |
| 199 | } |
| 200 | |
| 201 | Error |
| 202 | ProcessPOSIX::DoLaunch (Module *module, |
Jean-Daniel Dupas | 7782de9 | 2013-12-09 22:52:50 +0000 | [diff] [blame] | 203 | ProcessLaunchInfo &launch_info) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 204 | { |
| 205 | Error error; |
| 206 | assert(m_monitor == NULL); |
| 207 | |
Daniel Malea | 6217d2a | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 208 | const char* working_dir = launch_info.GetWorkingDirectory(); |
| 209 | if (working_dir) { |
| 210 | FileSpec WorkingDir(working_dir, true); |
| 211 | if (!WorkingDir || WorkingDir.GetFileType() != FileSpec::eFileTypeDirectory) |
| 212 | { |
| 213 | error.SetErrorStringWithFormat("No such file or directory: %s", working_dir); |
| 214 | return error; |
| 215 | } |
| 216 | } |
| 217 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 218 | SetPrivateState(eStateLaunching); |
| 219 | |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 220 | const lldb_private::FileAction *file_action; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 221 | |
| 222 | // Default of NULL will mean to use existing open file descriptors |
| 223 | const char *stdin_path = NULL; |
| 224 | const char *stdout_path = NULL; |
| 225 | const char *stderr_path = NULL; |
Daniel Malea | 6217d2a | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 226 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 227 | file_action = launch_info.GetFileActionForFD (STDIN_FILENO); |
| 228 | stdin_path = GetFilePath(file_action, stdin_path); |
| 229 | |
| 230 | file_action = launch_info.GetFileActionForFD (STDOUT_FILENO); |
| 231 | stdout_path = GetFilePath(file_action, stdout_path); |
| 232 | |
| 233 | file_action = launch_info.GetFileActionForFD (STDERR_FILENO); |
| 234 | stderr_path = GetFilePath(file_action, stderr_path); |
| 235 | |
Andrew Kaylor | 6578cb6 | 2013-07-09 22:36:48 +0000 | [diff] [blame] | 236 | m_monitor = new ProcessMonitor (this, |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 237 | module, |
| 238 | launch_info.GetArguments().GetConstArgumentVector(), |
| 239 | launch_info.GetEnvironmentEntries().GetConstArgumentVector(), |
| 240 | stdin_path, |
| 241 | stdout_path, |
| 242 | stderr_path, |
Daniel Malea | 6217d2a | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 243 | working_dir, |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame^] | 244 | launch_info, |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 245 | error); |
| 246 | |
| 247 | m_module = module; |
| 248 | |
| 249 | if (!error.Success()) |
| 250 | return error; |
| 251 | |
Matt Kopec | 9eb40a9 | 2013-03-14 21:35:26 +0000 | [diff] [blame] | 252 | SetSTDIOFileDescriptor(m_monitor->GetTerminalFD()); |
| 253 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 254 | SetID(m_monitor->GetPID()); |
| 255 | return error; |
| 256 | } |
| 257 | |
| 258 | void |
| 259 | ProcessPOSIX::DidLaunch() |
| 260 | { |
| 261 | } |
| 262 | |
Ed Maste | 30df85e | 2013-12-11 20:43:27 +0000 | [diff] [blame] | 263 | Error |
| 264 | ProcessPOSIX::DoResume() |
| 265 | { |
| 266 | StateType state = GetPrivateState(); |
| 267 | |
| 268 | assert(state == eStateStopped); |
| 269 | |
| 270 | SetPrivateState(eStateRunning); |
| 271 | |
| 272 | bool did_resume = false; |
| 273 | |
| 274 | Mutex::Locker lock(m_thread_list.GetMutex()); |
| 275 | |
| 276 | uint32_t thread_count = m_thread_list.GetSize(false); |
| 277 | for (uint32_t i = 0; i < thread_count; ++i) |
| 278 | { |
| 279 | POSIXThread *thread = static_cast<POSIXThread*>( |
| 280 | m_thread_list.GetThreadAtIndex(i, false).get()); |
| 281 | did_resume = thread->Resume() || did_resume; |
| 282 | } |
| 283 | assert(did_resume && "Process resume failed!"); |
| 284 | |
| 285 | return Error(); |
| 286 | } |
| 287 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 288 | addr_t |
| 289 | ProcessPOSIX::GetImageInfoAddress() |
| 290 | { |
| 291 | Target *target = &GetTarget(); |
| 292 | ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile(); |
Ed Maste | 5480365 | 2013-10-11 17:39:07 +0000 | [diff] [blame] | 293 | Address addr = obj_file->GetImageInfoAddress(target); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 294 | |
Ed Maste | 04a8bab | 2013-10-11 01:16:08 +0000 | [diff] [blame] | 295 | if (addr.IsValid()) |
Ed Maste | 5480365 | 2013-10-11 17:39:07 +0000 | [diff] [blame] | 296 | return addr.GetLoadAddress(target); |
Ed Maste | 04a8bab | 2013-10-11 01:16:08 +0000 | [diff] [blame] | 297 | return LLDB_INVALID_ADDRESS; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | Error |
| 301 | ProcessPOSIX::DoHalt(bool &caused_stop) |
| 302 | { |
| 303 | Error error; |
| 304 | |
| 305 | if (IsStopped()) |
| 306 | { |
| 307 | caused_stop = false; |
| 308 | } |
| 309 | else if (kill(GetID(), SIGSTOP)) |
| 310 | { |
| 311 | caused_stop = false; |
| 312 | error.SetErrorToErrno(); |
| 313 | } |
| 314 | else |
| 315 | { |
| 316 | caused_stop = true; |
| 317 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 318 | return error; |
| 319 | } |
| 320 | |
| 321 | Error |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 322 | ProcessPOSIX::DoSignal(int signal) |
| 323 | { |
| 324 | Error error; |
| 325 | |
| 326 | if (kill(GetID(), signal)) |
| 327 | error.SetErrorToErrno(); |
| 328 | |
| 329 | return error; |
| 330 | } |
| 331 | |
| 332 | Error |
| 333 | ProcessPOSIX::DoDestroy() |
| 334 | { |
| 335 | Error error; |
| 336 | |
| 337 | if (!HasExited()) |
| 338 | { |
Ed Maste | 7088293 | 2014-04-01 14:30:56 +0000 | [diff] [blame] | 339 | assert(m_monitor); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 340 | m_exit_now = true; |
Ed Maste | 7088293 | 2014-04-01 14:30:56 +0000 | [diff] [blame] | 341 | if (!m_monitor->Kill()) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 342 | { |
| 343 | error.SetErrorToErrno(); |
| 344 | return error; |
| 345 | } |
| 346 | |
| 347 | SetPrivateState(eStateExited); |
| 348 | } |
| 349 | |
| 350 | return error; |
| 351 | } |
| 352 | |
| 353 | void |
Matt Kopec | 718be87 | 2013-10-09 19:39:55 +0000 | [diff] [blame] | 354 | ProcessPOSIX::DoDidExec() |
| 355 | { |
| 356 | Target *target = &GetTarget(); |
| 357 | if (target) |
| 358 | { |
| 359 | PlatformSP platform_sp (target->GetPlatform()); |
| 360 | assert (platform_sp.get()); |
| 361 | if (platform_sp) |
| 362 | { |
| 363 | ProcessInstanceInfo process_info; |
| 364 | platform_sp->GetProcessInfo(GetID(), process_info); |
| 365 | ModuleSP exe_module_sp; |
| 366 | FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths()); |
| 367 | Error error = platform_sp->ResolveExecutable(process_info.GetExecutableFile(), |
| 368 | target->GetArchitecture(), |
| 369 | exe_module_sp, |
| 370 | executable_search_paths.GetSize() ? &executable_search_paths : NULL); |
| 371 | if (!error.Success()) |
| 372 | return; |
| 373 | target->SetExecutableModule(exe_module_sp, true); |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
Ed Maste | 30df85e | 2013-12-11 20:43:27 +0000 | [diff] [blame] | 378 | void |
| 379 | ProcessPOSIX::SendMessage(const ProcessMessage &message) |
| 380 | { |
Andrew MacPherson | 82aae0d | 2014-04-02 06:57:45 +0000 | [diff] [blame] | 381 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS)); |
| 382 | |
Ed Maste | 30df85e | 2013-12-11 20:43:27 +0000 | [diff] [blame] | 383 | Mutex::Locker lock(m_message_mutex); |
| 384 | |
| 385 | Mutex::Locker thread_lock(m_thread_list.GetMutex()); |
| 386 | |
| 387 | POSIXThread *thread = static_cast<POSIXThread*>( |
| 388 | m_thread_list.FindThreadByID(message.GetTID(), false).get()); |
| 389 | |
| 390 | switch (message.GetKind()) |
| 391 | { |
| 392 | case ProcessMessage::eInvalidMessage: |
| 393 | return; |
| 394 | |
| 395 | case ProcessMessage::eAttachMessage: |
| 396 | SetPrivateState(eStateStopped); |
| 397 | return; |
| 398 | |
| 399 | case ProcessMessage::eLimboMessage: |
| 400 | assert(thread); |
| 401 | thread->SetState(eStateStopped); |
| 402 | if (message.GetTID() == GetID()) |
| 403 | { |
| 404 | m_exit_status = message.GetExitStatus(); |
| 405 | if (m_exit_now) |
| 406 | { |
| 407 | SetPrivateState(eStateExited); |
| 408 | m_monitor->Detach(GetID()); |
| 409 | } |
| 410 | else |
| 411 | { |
| 412 | StopAllThreads(message.GetTID()); |
| 413 | SetPrivateState(eStateStopped); |
| 414 | } |
| 415 | } |
| 416 | else |
| 417 | { |
| 418 | StopAllThreads(message.GetTID()); |
| 419 | SetPrivateState(eStateStopped); |
| 420 | } |
| 421 | break; |
| 422 | |
| 423 | case ProcessMessage::eExitMessage: |
Andrew MacPherson | 82aae0d | 2014-04-02 06:57:45 +0000 | [diff] [blame] | 424 | if (thread != nullptr) |
| 425 | thread->SetState(eStateExited); |
| 426 | else |
| 427 | { |
| 428 | if (log) |
| 429 | log->Warning ("ProcessPOSIX::%s eExitMessage for TID %" PRIu64 " failed to find a thread to mark as eStateExited, ignoring", __FUNCTION__, message.GetTID ()); |
| 430 | } |
| 431 | |
Ed Maste | 30df85e | 2013-12-11 20:43:27 +0000 | [diff] [blame] | 432 | // FIXME: I'm not sure we need to do this. |
| 433 | if (message.GetTID() == GetID()) |
| 434 | { |
| 435 | m_exit_status = message.GetExitStatus(); |
| 436 | SetExitStatus(m_exit_status, NULL); |
| 437 | } |
| 438 | else if (!IsAThreadRunning()) |
| 439 | SetPrivateState(eStateStopped); |
| 440 | break; |
| 441 | |
| 442 | case ProcessMessage::eSignalMessage: |
| 443 | case ProcessMessage::eSignalDeliveredMessage: |
| 444 | if (message.GetSignal() == SIGSTOP && |
| 445 | AddThreadForInitialStopIfNeeded(message.GetTID())) |
| 446 | return; |
| 447 | // Intentional fall-through |
| 448 | |
| 449 | case ProcessMessage::eBreakpointMessage: |
| 450 | case ProcessMessage::eTraceMessage: |
| 451 | case ProcessMessage::eWatchpointMessage: |
| 452 | case ProcessMessage::eCrashMessage: |
| 453 | assert(thread); |
| 454 | thread->SetState(eStateStopped); |
| 455 | StopAllThreads(message.GetTID()); |
| 456 | SetPrivateState(eStateStopped); |
| 457 | break; |
| 458 | |
| 459 | case ProcessMessage::eNewThreadMessage: |
| 460 | { |
| 461 | lldb::tid_t new_tid = message.GetChildTID(); |
| 462 | if (WaitingForInitialStop(new_tid)) |
| 463 | { |
| 464 | m_monitor->WaitForInitialTIDStop(new_tid); |
| 465 | } |
| 466 | assert(thread); |
| 467 | thread->SetState(eStateStopped); |
| 468 | StopAllThreads(message.GetTID()); |
| 469 | SetPrivateState(eStateStopped); |
| 470 | break; |
| 471 | } |
| 472 | |
| 473 | case ProcessMessage::eExecMessage: |
| 474 | { |
| 475 | assert(thread); |
| 476 | thread->SetState(eStateStopped); |
| 477 | StopAllThreads(message.GetTID()); |
| 478 | SetPrivateState(eStateStopped); |
| 479 | break; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | |
| 484 | m_message_queue.push(message); |
| 485 | } |
| 486 | |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 487 | void |
| 488 | ProcessPOSIX::StopAllThreads(lldb::tid_t stop_tid) |
| 489 | { |
| 490 | // FIXME: Will this work the same way on FreeBSD and Linux? |
| 491 | } |
| 492 | |
Matt Kopec | b291044 | 2013-07-09 15:09:45 +0000 | [diff] [blame] | 493 | bool |
| 494 | ProcessPOSIX::AddThreadForInitialStopIfNeeded(lldb::tid_t stop_tid) |
| 495 | { |
| 496 | bool added_to_set = false; |
| 497 | ThreadStopSet::iterator it = m_seen_initial_stop.find(stop_tid); |
| 498 | if (it == m_seen_initial_stop.end()) |
| 499 | { |
| 500 | m_seen_initial_stop.insert(stop_tid); |
| 501 | added_to_set = true; |
| 502 | } |
| 503 | return added_to_set; |
| 504 | } |
| 505 | |
Andrew Kaylor | d4d5499 | 2013-09-17 00:30:24 +0000 | [diff] [blame] | 506 | bool |
| 507 | ProcessPOSIX::WaitingForInitialStop(lldb::tid_t stop_tid) |
| 508 | { |
| 509 | return (m_seen_initial_stop.find(stop_tid) == m_seen_initial_stop.end()); |
| 510 | } |
| 511 | |
Michael Sartain | 9f822cd | 2013-07-31 23:27:46 +0000 | [diff] [blame] | 512 | POSIXThread * |
| 513 | ProcessPOSIX::CreateNewPOSIXThread(lldb_private::Process &process, lldb::tid_t tid) |
| 514 | { |
| 515 | return new POSIXThread(process, tid); |
| 516 | } |
| 517 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 518 | void |
| 519 | ProcessPOSIX::RefreshStateAfterStop() |
| 520 | { |
Ashok Thirumurthi | 0118635 | 2013-03-28 16:02:31 +0000 | [diff] [blame] | 521 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS)); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 522 | if (log && log->GetMask().Test(POSIX_LOG_VERBOSE)) |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 523 | log->Printf ("ProcessPOSIX::%s(), message_queue size = %d", __FUNCTION__, (int)m_message_queue.size()); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 524 | |
| 525 | Mutex::Locker lock(m_message_mutex); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 526 | |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 527 | // This method used to only handle one message. Changing it to loop allows |
| 528 | // it to handle the case where we hit a breakpoint while handling a different |
| 529 | // breakpoint. |
| 530 | while (!m_message_queue.empty()) |
| 531 | { |
| 532 | ProcessMessage &message = m_message_queue.front(); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 533 | |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 534 | // Resolve the thread this message corresponds to and pass it along. |
| 535 | lldb::tid_t tid = message.GetTID(); |
| 536 | if (log) |
| 537 | log->Printf ("ProcessPOSIX::%s(), message_queue size = %d, pid = %" PRIi64, __FUNCTION__, (int)m_message_queue.size(), tid); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 538 | |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 539 | if (message.GetKind() == ProcessMessage::eNewThreadMessage) |
| 540 | { |
| 541 | if (log) |
| 542 | log->Printf ("ProcessPOSIX::%s() adding thread, tid = %" PRIi64, __FUNCTION__, message.GetChildTID()); |
Matt Kopec | fb6ab54 | 2013-07-10 20:53:11 +0000 | [diff] [blame] | 543 | lldb::tid_t child_tid = message.GetChildTID(); |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 544 | ThreadSP thread_sp; |
Michael Sartain | 9f822cd | 2013-07-31 23:27:46 +0000 | [diff] [blame] | 545 | thread_sp.reset(CreateNewPOSIXThread(*this, child_tid)); |
Matt Kopec | fb6ab54 | 2013-07-10 20:53:11 +0000 | [diff] [blame] | 546 | |
Daniel Malea | a2cb9c4 | 2013-07-24 21:44:30 +0000 | [diff] [blame] | 547 | Mutex::Locker lock(m_thread_list.GetMutex()); |
| 548 | |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 549 | m_thread_list.AddThread(thread_sp); |
| 550 | } |
| 551 | |
| 552 | m_thread_list.RefreshStateAfterStop(); |
| 553 | |
Ed Maste | 685fea9 | 2013-08-29 20:40:11 +0000 | [diff] [blame] | 554 | POSIXThread *thread = static_cast<POSIXThread*>( |
| 555 | GetThreadList().FindThreadByID(tid, false).get()); |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 556 | if (thread) |
| 557 | thread->Notify(message); |
| 558 | |
| 559 | if (message.GetKind() == ProcessMessage::eExitMessage) |
| 560 | { |
| 561 | // FIXME: We should tell the user about this, but the limbo message is probably better for that. |
| 562 | if (log) |
| 563 | log->Printf ("ProcessPOSIX::%s() removing thread, tid = %" PRIi64, __FUNCTION__, tid); |
Daniel Malea | a2cb9c4 | 2013-07-24 21:44:30 +0000 | [diff] [blame] | 564 | |
| 565 | Mutex::Locker lock(m_thread_list.GetMutex()); |
| 566 | |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 567 | ThreadSP thread_sp = m_thread_list.RemoveThreadByID(tid, false); |
| 568 | thread_sp.reset(); |
Matt Kopec | b291044 | 2013-07-09 15:09:45 +0000 | [diff] [blame] | 569 | m_seen_initial_stop.erase(tid); |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | m_message_queue.pop(); |
Matt Kopec | 650648f | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 573 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | bool |
| 577 | ProcessPOSIX::IsAlive() |
| 578 | { |
| 579 | StateType state = GetPrivateState(); |
Daniel Malea | 335bf6f | 2013-04-01 19:48:37 +0000 | [diff] [blame] | 580 | return state != eStateDetached |
| 581 | && state != eStateExited |
| 582 | && state != eStateInvalid |
| 583 | && state != eStateUnloaded; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | size_t |
| 587 | ProcessPOSIX::DoReadMemory(addr_t vm_addr, |
| 588 | void *buf, size_t size, Error &error) |
| 589 | { |
| 590 | assert(m_monitor); |
| 591 | return m_monitor->ReadMemory(vm_addr, buf, size, error); |
| 592 | } |
| 593 | |
| 594 | size_t |
| 595 | ProcessPOSIX::DoWriteMemory(addr_t vm_addr, const void *buf, size_t size, |
| 596 | Error &error) |
| 597 | { |
| 598 | assert(m_monitor); |
| 599 | return m_monitor->WriteMemory(vm_addr, buf, size, error); |
| 600 | } |
| 601 | |
| 602 | addr_t |
| 603 | ProcessPOSIX::DoAllocateMemory(size_t size, uint32_t permissions, |
| 604 | Error &error) |
| 605 | { |
| 606 | addr_t allocated_addr = LLDB_INVALID_ADDRESS; |
| 607 | |
| 608 | unsigned prot = 0; |
| 609 | if (permissions & lldb::ePermissionsReadable) |
| 610 | prot |= eMmapProtRead; |
| 611 | if (permissions & lldb::ePermissionsWritable) |
| 612 | prot |= eMmapProtWrite; |
| 613 | if (permissions & lldb::ePermissionsExecutable) |
| 614 | prot |= eMmapProtExec; |
| 615 | |
| 616 | if (InferiorCallMmap(this, allocated_addr, 0, size, prot, |
| 617 | eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) { |
| 618 | m_addr_to_mmap_size[allocated_addr] = size; |
| 619 | error.Clear(); |
| 620 | } else { |
| 621 | allocated_addr = LLDB_INVALID_ADDRESS; |
| 622 | error.SetErrorStringWithFormat("unable to allocate %zu bytes of memory with permissions %s", size, GetPermissionsAsCString (permissions)); |
| 623 | } |
| 624 | |
| 625 | return allocated_addr; |
| 626 | } |
| 627 | |
| 628 | Error |
| 629 | ProcessPOSIX::DoDeallocateMemory(lldb::addr_t addr) |
| 630 | { |
| 631 | Error error; |
| 632 | MMapMap::iterator pos = m_addr_to_mmap_size.find(addr); |
| 633 | if (pos != m_addr_to_mmap_size.end() && |
| 634 | InferiorCallMunmap(this, addr, pos->second)) |
| 635 | m_addr_to_mmap_size.erase (pos); |
| 636 | else |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 637 | error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, addr); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 638 | |
| 639 | return error; |
| 640 | } |
| 641 | |
| 642 | size_t |
| 643 | ProcessPOSIX::GetSoftwareBreakpointTrapOpcode(BreakpointSite* bp_site) |
| 644 | { |
| 645 | static const uint8_t g_i386_opcode[] = { 0xCC }; |
| 646 | |
| 647 | ArchSpec arch = GetTarget().GetArchitecture(); |
| 648 | const uint8_t *opcode = NULL; |
| 649 | size_t opcode_size = 0; |
| 650 | |
Greg Clayton | 906e9ac | 2014-03-20 21:31:55 +0000 | [diff] [blame] | 651 | switch (arch.GetMachine()) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 652 | { |
| 653 | default: |
| 654 | assert(false && "CPU type not supported!"); |
| 655 | break; |
| 656 | |
Greg Clayton | 906e9ac | 2014-03-20 21:31:55 +0000 | [diff] [blame] | 657 | case llvm::Triple::x86: |
| 658 | case llvm::Triple::x86_64: |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 659 | opcode = g_i386_opcode; |
| 660 | opcode_size = sizeof(g_i386_opcode); |
| 661 | break; |
| 662 | } |
| 663 | |
| 664 | bp_site->SetTrapOpcode(opcode, opcode_size); |
| 665 | return opcode_size; |
| 666 | } |
| 667 | |
| 668 | Error |
Daniel Malea | b7eec01 | 2013-02-15 20:23:25 +0000 | [diff] [blame] | 669 | ProcessPOSIX::EnableBreakpointSite(BreakpointSite *bp_site) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 670 | { |
| 671 | return EnableSoftwareBreakpoint(bp_site); |
| 672 | } |
| 673 | |
| 674 | Error |
Daniel Malea | b7eec01 | 2013-02-15 20:23:25 +0000 | [diff] [blame] | 675 | ProcessPOSIX::DisableBreakpointSite(BreakpointSite *bp_site) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 676 | { |
| 677 | return DisableSoftwareBreakpoint(bp_site); |
| 678 | } |
| 679 | |
Matt Kopec | e9ea0da | 2013-05-07 19:29:28 +0000 | [diff] [blame] | 680 | Error |
| 681 | ProcessPOSIX::EnableWatchpoint(Watchpoint *wp, bool notify) |
| 682 | { |
| 683 | Error error; |
| 684 | if (wp) |
| 685 | { |
| 686 | user_id_t watchID = wp->GetID(); |
| 687 | addr_t addr = wp->GetLoadAddress(); |
| 688 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); |
| 689 | if (log) |
| 690 | log->Printf ("ProcessPOSIX::EnableWatchpoint(watchID = %" PRIu64 ")", |
| 691 | watchID); |
| 692 | if (wp->IsEnabled()) |
| 693 | { |
| 694 | if (log) |
| 695 | log->Printf("ProcessPOSIX::EnableWatchpoint(watchID = %" PRIu64 |
| 696 | ") addr = 0x%8.8" PRIx64 ": watchpoint already enabled.", |
| 697 | watchID, (uint64_t)addr); |
| 698 | return error; |
| 699 | } |
| 700 | |
Matt Kopec | 6f96123 | 2013-06-03 17:40:20 +0000 | [diff] [blame] | 701 | // Try to find a vacant watchpoint slot in the inferiors' main thread |
| 702 | uint32_t wp_hw_index = LLDB_INVALID_INDEX32; |
Daniel Malea | a2cb9c4 | 2013-07-24 21:44:30 +0000 | [diff] [blame] | 703 | Mutex::Locker lock(m_thread_list.GetMutex()); |
Matt Kopec | 6f96123 | 2013-06-03 17:40:20 +0000 | [diff] [blame] | 704 | POSIXThread *thread = static_cast<POSIXThread*>( |
| 705 | m_thread_list.GetThreadAtIndex(0, false).get()); |
| 706 | |
| 707 | if (thread) |
| 708 | wp_hw_index = thread->FindVacantWatchpointIndex(); |
| 709 | |
| 710 | if (wp_hw_index == LLDB_INVALID_INDEX32) |
Matt Kopec | e9ea0da | 2013-05-07 19:29:28 +0000 | [diff] [blame] | 711 | { |
Matt Kopec | 6f96123 | 2013-06-03 17:40:20 +0000 | [diff] [blame] | 712 | error.SetErrorString("Setting hardware watchpoint failed."); |
Matt Kopec | e9ea0da | 2013-05-07 19:29:28 +0000 | [diff] [blame] | 713 | } |
| 714 | else |
| 715 | { |
Matt Kopec | 6f96123 | 2013-06-03 17:40:20 +0000 | [diff] [blame] | 716 | wp->SetHardwareIndex(wp_hw_index); |
| 717 | bool wp_enabled = true; |
| 718 | uint32_t thread_count = m_thread_list.GetSize(false); |
| 719 | for (uint32_t i = 0; i < thread_count; ++i) |
| 720 | { |
| 721 | thread = static_cast<POSIXThread*>( |
| 722 | m_thread_list.GetThreadAtIndex(i, false).get()); |
| 723 | if (thread) |
| 724 | wp_enabled &= thread->EnableHardwareWatchpoint(wp); |
| 725 | else |
| 726 | wp_enabled = false; |
| 727 | } |
| 728 | if (wp_enabled) |
| 729 | { |
| 730 | wp->SetEnabled(true, notify); |
| 731 | return error; |
| 732 | } |
| 733 | else |
| 734 | { |
| 735 | // Watchpoint enabling failed on at least one |
| 736 | // of the threads so roll back all of them |
| 737 | DisableWatchpoint(wp, false); |
| 738 | error.SetErrorString("Setting hardware watchpoint failed"); |
| 739 | } |
Matt Kopec | e9ea0da | 2013-05-07 19:29:28 +0000 | [diff] [blame] | 740 | } |
| 741 | } |
| 742 | else |
| 743 | error.SetErrorString("Watchpoint argument was NULL."); |
| 744 | return error; |
| 745 | } |
| 746 | |
| 747 | Error |
| 748 | ProcessPOSIX::DisableWatchpoint(Watchpoint *wp, bool notify) |
| 749 | { |
| 750 | Error error; |
| 751 | if (wp) |
| 752 | { |
| 753 | user_id_t watchID = wp->GetID(); |
| 754 | addr_t addr = wp->GetLoadAddress(); |
| 755 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); |
| 756 | if (log) |
| 757 | log->Printf("ProcessPOSIX::DisableWatchpoint(watchID = %" PRIu64 ")", |
| 758 | watchID); |
| 759 | if (!wp->IsEnabled()) |
| 760 | { |
| 761 | if (log) |
| 762 | log->Printf("ProcessPOSIX::DisableWatchpoint(watchID = %" PRIu64 |
| 763 | ") addr = 0x%8.8" PRIx64 ": watchpoint already disabled.", |
| 764 | watchID, (uint64_t)addr); |
| 765 | // This is needed (for now) to keep watchpoints disabled correctly |
| 766 | wp->SetEnabled(false, notify); |
| 767 | return error; |
| 768 | } |
| 769 | |
| 770 | if (wp->IsHardware()) |
| 771 | { |
| 772 | bool wp_disabled = true; |
Daniel Malea | a2cb9c4 | 2013-07-24 21:44:30 +0000 | [diff] [blame] | 773 | Mutex::Locker lock(m_thread_list.GetMutex()); |
Matt Kopec | e9ea0da | 2013-05-07 19:29:28 +0000 | [diff] [blame] | 774 | uint32_t thread_count = m_thread_list.GetSize(false); |
| 775 | for (uint32_t i = 0; i < thread_count; ++i) |
| 776 | { |
| 777 | POSIXThread *thread = static_cast<POSIXThread*>( |
| 778 | m_thread_list.GetThreadAtIndex(i, false).get()); |
| 779 | if (thread) |
| 780 | wp_disabled &= thread->DisableHardwareWatchpoint(wp); |
| 781 | else |
| 782 | wp_disabled = false; |
| 783 | } |
| 784 | if (wp_disabled) |
| 785 | { |
Matt Kopec | 6f96123 | 2013-06-03 17:40:20 +0000 | [diff] [blame] | 786 | wp->SetHardwareIndex(LLDB_INVALID_INDEX32); |
Matt Kopec | e9ea0da | 2013-05-07 19:29:28 +0000 | [diff] [blame] | 787 | wp->SetEnabled(false, notify); |
| 788 | return error; |
| 789 | } |
| 790 | else |
| 791 | error.SetErrorString("Disabling hardware watchpoint failed"); |
| 792 | } |
| 793 | } |
| 794 | else |
| 795 | error.SetErrorString("Watchpoint argument was NULL."); |
| 796 | return error; |
| 797 | } |
| 798 | |
| 799 | Error |
| 800 | ProcessPOSIX::GetWatchpointSupportInfo(uint32_t &num) |
| 801 | { |
| 802 | Error error; |
Daniel Malea | a2cb9c4 | 2013-07-24 21:44:30 +0000 | [diff] [blame] | 803 | Mutex::Locker lock(m_thread_list.GetMutex()); |
Matt Kopec | e9ea0da | 2013-05-07 19:29:28 +0000 | [diff] [blame] | 804 | POSIXThread *thread = static_cast<POSIXThread*>( |
| 805 | m_thread_list.GetThreadAtIndex(0, false).get()); |
| 806 | if (thread) |
| 807 | num = thread->NumSupportedHardwareWatchpoints(); |
| 808 | else |
| 809 | error.SetErrorString("Process does not exist."); |
| 810 | return error; |
| 811 | } |
| 812 | |
| 813 | Error |
| 814 | ProcessPOSIX::GetWatchpointSupportInfo(uint32_t &num, bool &after) |
| 815 | { |
| 816 | Error error = GetWatchpointSupportInfo(num); |
| 817 | // Watchpoints trigger and halt the inferior after |
| 818 | // the corresponding instruction has been executed. |
| 819 | after = true; |
| 820 | return error; |
| 821 | } |
| 822 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 823 | uint32_t |
| 824 | ProcessPOSIX::UpdateThreadListIfNeeded() |
| 825 | { |
Daniel Malea | a2cb9c4 | 2013-07-24 21:44:30 +0000 | [diff] [blame] | 826 | Mutex::Locker lock(m_thread_list.GetMutex()); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 827 | // Do not allow recursive updates. |
| 828 | return m_thread_list.GetSize(false); |
| 829 | } |
| 830 | |
Greg Clayton | c3c0b0e | 2012-04-12 19:04:34 +0000 | [diff] [blame] | 831 | bool |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 832 | ProcessPOSIX::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list) |
| 833 | { |
Ashok Thirumurthi | 0118635 | 2013-03-28 16:02:31 +0000 | [diff] [blame] | 834 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD)); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 835 | if (log && log->GetMask().Test(POSIX_LOG_VERBOSE)) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 836 | log->Printf ("ProcessPOSIX::%s() (pid = %" PRIi64 ")", __FUNCTION__, GetID()); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 837 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 838 | bool has_updated = false; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 839 | // Update the process thread list with this new thread. |
| 840 | // FIXME: We should be using tid, not pid. |
| 841 | assert(m_monitor); |
| 842 | ThreadSP thread_sp (old_thread_list.FindThreadByID (GetID(), false)); |
Greg Clayton | 0c90ef4 | 2012-02-21 18:40:07 +0000 | [diff] [blame] | 843 | if (!thread_sp) { |
Michael Sartain | 9f822cd | 2013-07-31 23:27:46 +0000 | [diff] [blame] | 844 | thread_sp.reset(CreateNewPOSIXThread(*this, GetID())); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 845 | has_updated = true; |
Greg Clayton | 0c90ef4 | 2012-02-21 18:40:07 +0000 | [diff] [blame] | 846 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 847 | |
| 848 | if (log && log->GetMask().Test(POSIX_LOG_VERBOSE)) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 849 | log->Printf ("ProcessPOSIX::%s() updated pid = %" PRIi64, __FUNCTION__, GetID()); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 850 | new_thread_list.AddThread(thread_sp); |
| 851 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 852 | return has_updated; // the list has been updated |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | ByteOrder |
| 856 | ProcessPOSIX::GetByteOrder() const |
| 857 | { |
| 858 | // FIXME: We should be able to extract this value directly. See comment in |
| 859 | // ProcessPOSIX(). |
| 860 | return m_byte_order; |
| 861 | } |
| 862 | |
| 863 | size_t |
| 864 | ProcessPOSIX::PutSTDIN(const char *buf, size_t len, Error &error) |
| 865 | { |
| 866 | ssize_t status; |
| 867 | if ((status = write(m_monitor->GetTerminalFD(), buf, len)) < 0) |
| 868 | { |
| 869 | error.SetErrorToErrno(); |
| 870 | return 0; |
| 871 | } |
| 872 | return status; |
| 873 | } |
| 874 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 875 | UnixSignals & |
| 876 | ProcessPOSIX::GetUnixSignals() |
| 877 | { |
| 878 | return m_signals; |
| 879 | } |
| 880 | |
| 881 | //------------------------------------------------------------------------------ |
| 882 | // Utility functions. |
| 883 | |
| 884 | bool |
| 885 | ProcessPOSIX::HasExited() |
| 886 | { |
| 887 | switch (GetPrivateState()) |
| 888 | { |
| 889 | default: |
| 890 | break; |
| 891 | |
| 892 | case eStateDetached: |
| 893 | case eStateExited: |
| 894 | return true; |
| 895 | } |
| 896 | |
| 897 | return false; |
| 898 | } |
| 899 | |
| 900 | bool |
| 901 | ProcessPOSIX::IsStopped() |
| 902 | { |
| 903 | switch (GetPrivateState()) |
| 904 | { |
| 905 | default: |
| 906 | break; |
| 907 | |
| 908 | case eStateStopped: |
| 909 | case eStateCrashed: |
| 910 | case eStateSuspended: |
| 911 | return true; |
| 912 | } |
| 913 | |
| 914 | return false; |
| 915 | } |
Matt Kopec | b291044 | 2013-07-09 15:09:45 +0000 | [diff] [blame] | 916 | |
| 917 | bool |
| 918 | ProcessPOSIX::IsAThreadRunning() |
| 919 | { |
| 920 | bool is_running = false; |
Daniel Malea | a2cb9c4 | 2013-07-24 21:44:30 +0000 | [diff] [blame] | 921 | Mutex::Locker lock(m_thread_list.GetMutex()); |
Matt Kopec | b291044 | 2013-07-09 15:09:45 +0000 | [diff] [blame] | 922 | uint32_t thread_count = m_thread_list.GetSize(false); |
| 923 | for (uint32_t i = 0; i < thread_count; ++i) |
| 924 | { |
| 925 | POSIXThread *thread = static_cast<POSIXThread*>( |
| 926 | m_thread_list.GetThreadAtIndex(i, false).get()); |
| 927 | StateType thread_state = thread->GetState(); |
| 928 | if (thread_state == eStateRunning || thread_state == eStateStepping) |
| 929 | { |
| 930 | is_running = true; |
| 931 | break; |
| 932 | } |
| 933 | } |
| 934 | return is_running; |
| 935 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 936 | |
| 937 | const DataBufferSP |
| 938 | ProcessPOSIX::GetAuxvData () |
| 939 | { |
| 940 | // If we're the local platform, we can ask the host for auxv data. |
| 941 | PlatformSP platform_sp = m_target.GetPlatform (); |
| 942 | if (platform_sp && platform_sp->IsHost ()) |
| 943 | return lldb_private::Host::GetAuxvData(this); |
| 944 | |
| 945 | // Somewhat unexpected - the process is not running locally or we don't have a platform. |
| 946 | assert (false && "no platform or not the host - how did we get here with ProcessPOSIX?"); |
| 947 | return DataBufferSP (); |
| 948 | } |