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 * |
| 179 | ProcessPOSIX::GetFilePath( |
| 180 | const lldb_private::ProcessLaunchInfo::FileAction *file_action, |
| 181 | const char *default_path) |
| 182 | { |
| 183 | const char *pts_name = "/dev/pts/"; |
| 184 | const char *path = NULL; |
| 185 | |
| 186 | if (file_action) |
| 187 | { |
| 188 | if (file_action->GetAction () == ProcessLaunchInfo::FileAction::eFileActionOpen) |
| 189 | path = file_action->GetPath(); |
| 190 | // By default the stdio paths passed in will be pseudo-terminal |
| 191 | // (/dev/pts). If so, convert to using a different default path |
| 192 | // instead to redirect I/O to the debugger console. This should |
| 193 | // also handle user overrides to /dev/null or a different file. |
| 194 | if (::strncmp(path, pts_name, ::strlen(pts_name)) == 0) |
| 195 | path = default_path; |
| 196 | } |
| 197 | |
| 198 | return path; |
| 199 | } |
| 200 | |
| 201 | Error |
| 202 | ProcessPOSIX::DoLaunch (Module *module, |
| 203 | const ProcessLaunchInfo &launch_info) |
| 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 | |
| 220 | const lldb_private::ProcessLaunchInfo::FileAction *file_action; |
| 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, |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 244 | error); |
| 245 | |
| 246 | m_module = module; |
| 247 | |
| 248 | if (!error.Success()) |
| 249 | return error; |
| 250 | |
Matt Kopec | 9eb40a9 | 2013-03-14 21:35:26 +0000 | [diff] [blame] | 251 | SetSTDIOFileDescriptor(m_monitor->GetTerminalFD()); |
| 252 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 253 | SetID(m_monitor->GetPID()); |
| 254 | return error; |
| 255 | } |
| 256 | |
| 257 | void |
| 258 | ProcessPOSIX::DidLaunch() |
| 259 | { |
| 260 | } |
| 261 | |
| 262 | Error |
| 263 | ProcessPOSIX::DoResume() |
| 264 | { |
| 265 | StateType state = GetPrivateState(); |
| 266 | |
Matt Kopec | eb7f231 | 2013-06-26 18:46:08 +0000 | [diff] [blame] | 267 | assert(state == eStateStopped); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 268 | |
Matt Kopec | eb7f231 | 2013-06-26 18:46:08 +0000 | [diff] [blame] | 269 | SetPrivateState(eStateRunning); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 270 | |
| 271 | bool did_resume = false; |
Daniel Malea | a2cb9c4 | 2013-07-24 21:44:30 +0000 | [diff] [blame] | 272 | |
| 273 | Mutex::Locker lock(m_thread_list.GetMutex()); |
| 274 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 275 | uint32_t thread_count = m_thread_list.GetSize(false); |
| 276 | for (uint32_t i = 0; i < thread_count; ++i) |
| 277 | { |
| 278 | POSIXThread *thread = static_cast<POSIXThread*>( |
| 279 | m_thread_list.GetThreadAtIndex(i, false).get()); |
| 280 | did_resume = thread->Resume() || did_resume; |
| 281 | } |
| 282 | assert(did_resume && "Process resume failed!"); |
| 283 | |
| 284 | return Error(); |
| 285 | } |
| 286 | |
| 287 | addr_t |
| 288 | ProcessPOSIX::GetImageInfoAddress() |
| 289 | { |
| 290 | Target *target = &GetTarget(); |
| 291 | ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile(); |
Ed Maste | 04a8bab | 2013-10-11 01:16:08 +0000 | [diff] [blame^] | 292 | bool indirect; |
| 293 | Address addr = obj_file->GetImageInfoAddress(indirect); |
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()) |
| 296 | { |
| 297 | if (indirect) |
| 298 | { |
| 299 | Address ind_addr; |
| 300 | Error error; |
| 301 | if (target->ReadPointerFromMemory(addr.GetLoadAddress(target), false, error, ind_addr)) |
| 302 | return ind_addr.GetLoadAddress(target); |
| 303 | } |
| 304 | else |
| 305 | { |
| 306 | return addr.GetLoadAddress(target); |
| 307 | } |
| 308 | } |
| 309 | return LLDB_INVALID_ADDRESS; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | Error |
| 313 | ProcessPOSIX::DoHalt(bool &caused_stop) |
| 314 | { |
| 315 | Error error; |
| 316 | |
| 317 | if (IsStopped()) |
| 318 | { |
| 319 | caused_stop = false; |
| 320 | } |
| 321 | else if (kill(GetID(), SIGSTOP)) |
| 322 | { |
| 323 | caused_stop = false; |
| 324 | error.SetErrorToErrno(); |
| 325 | } |
| 326 | else |
| 327 | { |
| 328 | caused_stop = true; |
| 329 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 330 | return error; |
| 331 | } |
| 332 | |
| 333 | Error |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 334 | ProcessPOSIX::DoSignal(int signal) |
| 335 | { |
| 336 | Error error; |
| 337 | |
| 338 | if (kill(GetID(), signal)) |
| 339 | error.SetErrorToErrno(); |
| 340 | |
| 341 | return error; |
| 342 | } |
| 343 | |
| 344 | Error |
| 345 | ProcessPOSIX::DoDestroy() |
| 346 | { |
| 347 | Error error; |
| 348 | |
| 349 | if (!HasExited()) |
| 350 | { |
| 351 | // Drive the exit event to completion (do not keep the inferior in |
| 352 | // limbo). |
| 353 | m_exit_now = true; |
| 354 | |
Greg Clayton | ab950c3 | 2012-03-30 19:56:32 +0000 | [diff] [blame] | 355 | if ((m_monitor == NULL || kill(m_monitor->GetPID(), SIGKILL)) && error.Success()) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 356 | { |
| 357 | error.SetErrorToErrno(); |
| 358 | return error; |
| 359 | } |
| 360 | |
| 361 | SetPrivateState(eStateExited); |
| 362 | } |
| 363 | |
| 364 | return error; |
| 365 | } |
| 366 | |
| 367 | void |
Matt Kopec | 718be87 | 2013-10-09 19:39:55 +0000 | [diff] [blame] | 368 | ProcessPOSIX::DoDidExec() |
| 369 | { |
| 370 | Target *target = &GetTarget(); |
| 371 | if (target) |
| 372 | { |
| 373 | PlatformSP platform_sp (target->GetPlatform()); |
| 374 | assert (platform_sp.get()); |
| 375 | if (platform_sp) |
| 376 | { |
| 377 | ProcessInstanceInfo process_info; |
| 378 | platform_sp->GetProcessInfo(GetID(), process_info); |
| 379 | ModuleSP exe_module_sp; |
| 380 | FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths()); |
| 381 | Error error = platform_sp->ResolveExecutable(process_info.GetExecutableFile(), |
| 382 | target->GetArchitecture(), |
| 383 | exe_module_sp, |
| 384 | executable_search_paths.GetSize() ? &executable_search_paths : NULL); |
| 385 | if (!error.Success()) |
| 386 | return; |
| 387 | target->SetExecutableModule(exe_module_sp, true); |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | void |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 393 | ProcessPOSIX::SendMessage(const ProcessMessage &message) |
| 394 | { |
| 395 | Mutex::Locker lock(m_message_mutex); |
| 396 | |
Daniel Malea | a2cb9c4 | 2013-07-24 21:44:30 +0000 | [diff] [blame] | 397 | Mutex::Locker thread_lock(m_thread_list.GetMutex()); |
| 398 | |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 399 | POSIXThread *thread = static_cast<POSIXThread*>( |
| 400 | m_thread_list.FindThreadByID(message.GetTID(), false).get()); |
| 401 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 402 | switch (message.GetKind()) |
| 403 | { |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 404 | case ProcessMessage::eInvalidMessage: |
| 405 | return; |
| 406 | |
Ed Maste | e544143 | 2013-09-03 23:55:30 +0000 | [diff] [blame] | 407 | case ProcessMessage::eAttachMessage: |
| 408 | SetPrivateState(eStateStopped); |
| 409 | return; |
| 410 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 411 | case ProcessMessage::eLimboMessage: |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 412 | assert(thread); |
| 413 | thread->SetState(eStateStopped); |
| 414 | if (message.GetTID() == GetID()) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 415 | { |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 416 | m_exit_status = message.GetExitStatus(); |
| 417 | if (m_exit_now) |
| 418 | { |
| 419 | SetPrivateState(eStateExited); |
Matt Kopec | 085d6ce | 2013-05-31 22:00:07 +0000 | [diff] [blame] | 420 | m_monitor->Detach(GetID()); |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 421 | } |
| 422 | else |
| 423 | { |
| 424 | StopAllThreads(message.GetTID()); |
| 425 | SetPrivateState(eStateStopped); |
| 426 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 427 | } |
| 428 | else |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 429 | { |
| 430 | StopAllThreads(message.GetTID()); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 431 | SetPrivateState(eStateStopped); |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 432 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 433 | break; |
| 434 | |
| 435 | case ProcessMessage::eExitMessage: |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 436 | assert(thread); |
| 437 | thread->SetState(eStateExited); |
| 438 | // FIXME: I'm not sure we need to do this. |
| 439 | if (message.GetTID() == GetID()) |
| 440 | { |
| 441 | m_exit_status = message.GetExitStatus(); |
| 442 | SetExitStatus(m_exit_status, NULL); |
| 443 | } |
Matt Kopec | b291044 | 2013-07-09 15:09:45 +0000 | [diff] [blame] | 444 | else if (!IsAThreadRunning()) |
| 445 | SetPrivateState(eStateStopped); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 446 | break; |
| 447 | |
Matt Kopec | b291044 | 2013-07-09 15:09:45 +0000 | [diff] [blame] | 448 | case ProcessMessage::eSignalMessage: |
| 449 | case ProcessMessage::eSignalDeliveredMessage: |
| 450 | if (message.GetSignal() == SIGSTOP && |
| 451 | AddThreadForInitialStopIfNeeded(message.GetTID())) |
| 452 | return; |
| 453 | // Intentional fall-through |
| 454 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 455 | case ProcessMessage::eBreakpointMessage: |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 456 | case ProcessMessage::eTraceMessage: |
Matt Kopec | e9ea0da | 2013-05-07 19:29:28 +0000 | [diff] [blame] | 457 | case ProcessMessage::eWatchpointMessage: |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 458 | case ProcessMessage::eCrashMessage: |
| 459 | assert(thread); |
| 460 | thread->SetState(eStateStopped); |
| 461 | StopAllThreads(message.GetTID()); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 462 | SetPrivateState(eStateStopped); |
| 463 | break; |
Andrew Kaylor | d4d5499 | 2013-09-17 00:30:24 +0000 | [diff] [blame] | 464 | |
| 465 | case ProcessMessage::eNewThreadMessage: |
Matt Kopec | 718be87 | 2013-10-09 19:39:55 +0000 | [diff] [blame] | 466 | { |
Andrew Kaylor | d4d5499 | 2013-09-17 00:30:24 +0000 | [diff] [blame] | 467 | lldb::tid_t new_tid = message.GetChildTID(); |
| 468 | if (WaitingForInitialStop(new_tid)) |
| 469 | { |
| 470 | m_monitor->WaitForInitialTIDStop(new_tid); |
| 471 | } |
| 472 | assert(thread); |
| 473 | thread->SetState(eStateStopped); |
| 474 | StopAllThreads(message.GetTID()); |
| 475 | SetPrivateState(eStateStopped); |
| 476 | break; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Matt Kopec | 718be87 | 2013-10-09 19:39:55 +0000 | [diff] [blame] | 479 | case ProcessMessage::eExecMessage: |
| 480 | { |
| 481 | assert(thread); |
| 482 | thread->SetState(eStateStopped); |
| 483 | StopAllThreads(message.GetTID()); |
| 484 | SetPrivateState(eStateStopped); |
| 485 | break; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 490 | m_message_queue.push(message); |
| 491 | } |
| 492 | |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 493 | void |
| 494 | ProcessPOSIX::StopAllThreads(lldb::tid_t stop_tid) |
| 495 | { |
| 496 | // FIXME: Will this work the same way on FreeBSD and Linux? |
| 497 | } |
| 498 | |
Matt Kopec | b291044 | 2013-07-09 15:09:45 +0000 | [diff] [blame] | 499 | bool |
| 500 | ProcessPOSIX::AddThreadForInitialStopIfNeeded(lldb::tid_t stop_tid) |
| 501 | { |
| 502 | bool added_to_set = false; |
| 503 | ThreadStopSet::iterator it = m_seen_initial_stop.find(stop_tid); |
| 504 | if (it == m_seen_initial_stop.end()) |
| 505 | { |
| 506 | m_seen_initial_stop.insert(stop_tid); |
| 507 | added_to_set = true; |
| 508 | } |
| 509 | return added_to_set; |
| 510 | } |
| 511 | |
Andrew Kaylor | d4d5499 | 2013-09-17 00:30:24 +0000 | [diff] [blame] | 512 | bool |
| 513 | ProcessPOSIX::WaitingForInitialStop(lldb::tid_t stop_tid) |
| 514 | { |
| 515 | return (m_seen_initial_stop.find(stop_tid) == m_seen_initial_stop.end()); |
| 516 | } |
| 517 | |
Michael Sartain | 9f822cd | 2013-07-31 23:27:46 +0000 | [diff] [blame] | 518 | POSIXThread * |
| 519 | ProcessPOSIX::CreateNewPOSIXThread(lldb_private::Process &process, lldb::tid_t tid) |
| 520 | { |
| 521 | return new POSIXThread(process, tid); |
| 522 | } |
| 523 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 524 | void |
| 525 | ProcessPOSIX::RefreshStateAfterStop() |
| 526 | { |
Ashok Thirumurthi | 0118635 | 2013-03-28 16:02:31 +0000 | [diff] [blame] | 527 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS)); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 528 | if (log && log->GetMask().Test(POSIX_LOG_VERBOSE)) |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 529 | 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] | 530 | |
| 531 | Mutex::Locker lock(m_message_mutex); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 532 | |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 533 | // This method used to only handle one message. Changing it to loop allows |
| 534 | // it to handle the case where we hit a breakpoint while handling a different |
| 535 | // breakpoint. |
| 536 | while (!m_message_queue.empty()) |
| 537 | { |
| 538 | ProcessMessage &message = m_message_queue.front(); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 539 | |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 540 | // Resolve the thread this message corresponds to and pass it along. |
| 541 | lldb::tid_t tid = message.GetTID(); |
| 542 | if (log) |
| 543 | 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] | 544 | |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 545 | if (message.GetKind() == ProcessMessage::eNewThreadMessage) |
| 546 | { |
| 547 | if (log) |
| 548 | log->Printf ("ProcessPOSIX::%s() adding thread, tid = %" PRIi64, __FUNCTION__, message.GetChildTID()); |
Matt Kopec | fb6ab54 | 2013-07-10 20:53:11 +0000 | [diff] [blame] | 549 | lldb::tid_t child_tid = message.GetChildTID(); |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 550 | ThreadSP thread_sp; |
Michael Sartain | 9f822cd | 2013-07-31 23:27:46 +0000 | [diff] [blame] | 551 | thread_sp.reset(CreateNewPOSIXThread(*this, child_tid)); |
Matt Kopec | fb6ab54 | 2013-07-10 20:53:11 +0000 | [diff] [blame] | 552 | |
Daniel Malea | a2cb9c4 | 2013-07-24 21:44:30 +0000 | [diff] [blame] | 553 | Mutex::Locker lock(m_thread_list.GetMutex()); |
| 554 | |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 555 | m_thread_list.AddThread(thread_sp); |
| 556 | } |
| 557 | |
| 558 | m_thread_list.RefreshStateAfterStop(); |
| 559 | |
Ed Maste | 685fea9 | 2013-08-29 20:40:11 +0000 | [diff] [blame] | 560 | POSIXThread *thread = static_cast<POSIXThread*>( |
| 561 | GetThreadList().FindThreadByID(tid, false).get()); |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 562 | if (thread) |
| 563 | thread->Notify(message); |
| 564 | |
| 565 | if (message.GetKind() == ProcessMessage::eExitMessage) |
| 566 | { |
| 567 | // FIXME: We should tell the user about this, but the limbo message is probably better for that. |
| 568 | if (log) |
| 569 | log->Printf ("ProcessPOSIX::%s() removing thread, tid = %" PRIi64, __FUNCTION__, tid); |
Daniel Malea | a2cb9c4 | 2013-07-24 21:44:30 +0000 | [diff] [blame] | 570 | |
| 571 | Mutex::Locker lock(m_thread_list.GetMutex()); |
| 572 | |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 573 | ThreadSP thread_sp = m_thread_list.RemoveThreadByID(tid, false); |
| 574 | thread_sp.reset(); |
Matt Kopec | b291044 | 2013-07-09 15:09:45 +0000 | [diff] [blame] | 575 | m_seen_initial_stop.erase(tid); |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | m_message_queue.pop(); |
Matt Kopec | 650648f | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 579 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | bool |
| 583 | ProcessPOSIX::IsAlive() |
| 584 | { |
| 585 | StateType state = GetPrivateState(); |
Daniel Malea | 335bf6f | 2013-04-01 19:48:37 +0000 | [diff] [blame] | 586 | return state != eStateDetached |
| 587 | && state != eStateExited |
| 588 | && state != eStateInvalid |
| 589 | && state != eStateUnloaded; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | size_t |
| 593 | ProcessPOSIX::DoReadMemory(addr_t vm_addr, |
| 594 | void *buf, size_t size, Error &error) |
| 595 | { |
| 596 | assert(m_monitor); |
| 597 | return m_monitor->ReadMemory(vm_addr, buf, size, error); |
| 598 | } |
| 599 | |
| 600 | size_t |
| 601 | ProcessPOSIX::DoWriteMemory(addr_t vm_addr, const void *buf, size_t size, |
| 602 | Error &error) |
| 603 | { |
| 604 | assert(m_monitor); |
| 605 | return m_monitor->WriteMemory(vm_addr, buf, size, error); |
| 606 | } |
| 607 | |
| 608 | addr_t |
| 609 | ProcessPOSIX::DoAllocateMemory(size_t size, uint32_t permissions, |
| 610 | Error &error) |
| 611 | { |
| 612 | addr_t allocated_addr = LLDB_INVALID_ADDRESS; |
| 613 | |
| 614 | unsigned prot = 0; |
| 615 | if (permissions & lldb::ePermissionsReadable) |
| 616 | prot |= eMmapProtRead; |
| 617 | if (permissions & lldb::ePermissionsWritable) |
| 618 | prot |= eMmapProtWrite; |
| 619 | if (permissions & lldb::ePermissionsExecutable) |
| 620 | prot |= eMmapProtExec; |
| 621 | |
| 622 | if (InferiorCallMmap(this, allocated_addr, 0, size, prot, |
| 623 | eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) { |
| 624 | m_addr_to_mmap_size[allocated_addr] = size; |
| 625 | error.Clear(); |
| 626 | } else { |
| 627 | allocated_addr = LLDB_INVALID_ADDRESS; |
| 628 | error.SetErrorStringWithFormat("unable to allocate %zu bytes of memory with permissions %s", size, GetPermissionsAsCString (permissions)); |
| 629 | } |
| 630 | |
| 631 | return allocated_addr; |
| 632 | } |
| 633 | |
| 634 | Error |
| 635 | ProcessPOSIX::DoDeallocateMemory(lldb::addr_t addr) |
| 636 | { |
| 637 | Error error; |
| 638 | MMapMap::iterator pos = m_addr_to_mmap_size.find(addr); |
| 639 | if (pos != m_addr_to_mmap_size.end() && |
| 640 | InferiorCallMunmap(this, addr, pos->second)) |
| 641 | m_addr_to_mmap_size.erase (pos); |
| 642 | else |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 643 | error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, addr); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 644 | |
| 645 | return error; |
| 646 | } |
| 647 | |
Matt Kopec | 00049b8 | 2013-02-27 20:13:38 +0000 | [diff] [blame] | 648 | addr_t |
| 649 | ProcessPOSIX::ResolveIndirectFunction(const Address *address, Error &error) |
| 650 | { |
| 651 | addr_t function_addr = LLDB_INVALID_ADDRESS; |
| 652 | if (address == NULL) { |
| 653 | error.SetErrorStringWithFormat("unable to determine direct function call for NULL address"); |
| 654 | } else if (!InferiorCall(this, address, function_addr)) { |
| 655 | function_addr = LLDB_INVALID_ADDRESS; |
Matt Kopec | 66fd4b1 | 2013-03-01 17:44:31 +0000 | [diff] [blame] | 656 | error.SetErrorStringWithFormat("unable to determine direct function call for indirect function %s", |
| 657 | address->CalculateSymbolContextSymbol()->GetName().AsCString()); |
Matt Kopec | 00049b8 | 2013-02-27 20:13:38 +0000 | [diff] [blame] | 658 | } |
| 659 | return function_addr; |
| 660 | } |
| 661 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 662 | size_t |
| 663 | ProcessPOSIX::GetSoftwareBreakpointTrapOpcode(BreakpointSite* bp_site) |
| 664 | { |
| 665 | static const uint8_t g_i386_opcode[] = { 0xCC }; |
| 666 | |
| 667 | ArchSpec arch = GetTarget().GetArchitecture(); |
| 668 | const uint8_t *opcode = NULL; |
| 669 | size_t opcode_size = 0; |
| 670 | |
| 671 | switch (arch.GetCore()) |
| 672 | { |
| 673 | default: |
| 674 | assert(false && "CPU type not supported!"); |
| 675 | break; |
| 676 | |
| 677 | case ArchSpec::eCore_x86_32_i386: |
| 678 | case ArchSpec::eCore_x86_64_x86_64: |
| 679 | opcode = g_i386_opcode; |
| 680 | opcode_size = sizeof(g_i386_opcode); |
| 681 | break; |
| 682 | } |
| 683 | |
| 684 | bp_site->SetTrapOpcode(opcode, opcode_size); |
| 685 | return opcode_size; |
| 686 | } |
| 687 | |
| 688 | Error |
Daniel Malea | b7eec01 | 2013-02-15 20:23:25 +0000 | [diff] [blame] | 689 | ProcessPOSIX::EnableBreakpointSite(BreakpointSite *bp_site) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 690 | { |
| 691 | return EnableSoftwareBreakpoint(bp_site); |
| 692 | } |
| 693 | |
| 694 | Error |
Daniel Malea | b7eec01 | 2013-02-15 20:23:25 +0000 | [diff] [blame] | 695 | ProcessPOSIX::DisableBreakpointSite(BreakpointSite *bp_site) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 696 | { |
| 697 | return DisableSoftwareBreakpoint(bp_site); |
| 698 | } |
| 699 | |
Matt Kopec | e9ea0da | 2013-05-07 19:29:28 +0000 | [diff] [blame] | 700 | Error |
| 701 | ProcessPOSIX::EnableWatchpoint(Watchpoint *wp, bool notify) |
| 702 | { |
| 703 | Error error; |
| 704 | if (wp) |
| 705 | { |
| 706 | user_id_t watchID = wp->GetID(); |
| 707 | addr_t addr = wp->GetLoadAddress(); |
| 708 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); |
| 709 | if (log) |
| 710 | log->Printf ("ProcessPOSIX::EnableWatchpoint(watchID = %" PRIu64 ")", |
| 711 | watchID); |
| 712 | if (wp->IsEnabled()) |
| 713 | { |
| 714 | if (log) |
| 715 | log->Printf("ProcessPOSIX::EnableWatchpoint(watchID = %" PRIu64 |
| 716 | ") addr = 0x%8.8" PRIx64 ": watchpoint already enabled.", |
| 717 | watchID, (uint64_t)addr); |
| 718 | return error; |
| 719 | } |
| 720 | |
Matt Kopec | 6f96123 | 2013-06-03 17:40:20 +0000 | [diff] [blame] | 721 | // Try to find a vacant watchpoint slot in the inferiors' main thread |
| 722 | uint32_t wp_hw_index = LLDB_INVALID_INDEX32; |
Daniel Malea | a2cb9c4 | 2013-07-24 21:44:30 +0000 | [diff] [blame] | 723 | Mutex::Locker lock(m_thread_list.GetMutex()); |
Matt Kopec | 6f96123 | 2013-06-03 17:40:20 +0000 | [diff] [blame] | 724 | POSIXThread *thread = static_cast<POSIXThread*>( |
| 725 | m_thread_list.GetThreadAtIndex(0, false).get()); |
| 726 | |
| 727 | if (thread) |
| 728 | wp_hw_index = thread->FindVacantWatchpointIndex(); |
| 729 | |
| 730 | if (wp_hw_index == LLDB_INVALID_INDEX32) |
Matt Kopec | e9ea0da | 2013-05-07 19:29:28 +0000 | [diff] [blame] | 731 | { |
Matt Kopec | 6f96123 | 2013-06-03 17:40:20 +0000 | [diff] [blame] | 732 | error.SetErrorString("Setting hardware watchpoint failed."); |
Matt Kopec | e9ea0da | 2013-05-07 19:29:28 +0000 | [diff] [blame] | 733 | } |
| 734 | else |
| 735 | { |
Matt Kopec | 6f96123 | 2013-06-03 17:40:20 +0000 | [diff] [blame] | 736 | wp->SetHardwareIndex(wp_hw_index); |
| 737 | bool wp_enabled = true; |
| 738 | uint32_t thread_count = m_thread_list.GetSize(false); |
| 739 | for (uint32_t i = 0; i < thread_count; ++i) |
| 740 | { |
| 741 | thread = static_cast<POSIXThread*>( |
| 742 | m_thread_list.GetThreadAtIndex(i, false).get()); |
| 743 | if (thread) |
| 744 | wp_enabled &= thread->EnableHardwareWatchpoint(wp); |
| 745 | else |
| 746 | wp_enabled = false; |
| 747 | } |
| 748 | if (wp_enabled) |
| 749 | { |
| 750 | wp->SetEnabled(true, notify); |
| 751 | return error; |
| 752 | } |
| 753 | else |
| 754 | { |
| 755 | // Watchpoint enabling failed on at least one |
| 756 | // of the threads so roll back all of them |
| 757 | DisableWatchpoint(wp, false); |
| 758 | error.SetErrorString("Setting hardware watchpoint failed"); |
| 759 | } |
Matt Kopec | e9ea0da | 2013-05-07 19:29:28 +0000 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | else |
| 763 | error.SetErrorString("Watchpoint argument was NULL."); |
| 764 | return error; |
| 765 | } |
| 766 | |
| 767 | Error |
| 768 | ProcessPOSIX::DisableWatchpoint(Watchpoint *wp, bool notify) |
| 769 | { |
| 770 | Error error; |
| 771 | if (wp) |
| 772 | { |
| 773 | user_id_t watchID = wp->GetID(); |
| 774 | addr_t addr = wp->GetLoadAddress(); |
| 775 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); |
| 776 | if (log) |
| 777 | log->Printf("ProcessPOSIX::DisableWatchpoint(watchID = %" PRIu64 ")", |
| 778 | watchID); |
| 779 | if (!wp->IsEnabled()) |
| 780 | { |
| 781 | if (log) |
| 782 | log->Printf("ProcessPOSIX::DisableWatchpoint(watchID = %" PRIu64 |
| 783 | ") addr = 0x%8.8" PRIx64 ": watchpoint already disabled.", |
| 784 | watchID, (uint64_t)addr); |
| 785 | // This is needed (for now) to keep watchpoints disabled correctly |
| 786 | wp->SetEnabled(false, notify); |
| 787 | return error; |
| 788 | } |
| 789 | |
| 790 | if (wp->IsHardware()) |
| 791 | { |
| 792 | bool wp_disabled = true; |
Daniel Malea | a2cb9c4 | 2013-07-24 21:44:30 +0000 | [diff] [blame] | 793 | Mutex::Locker lock(m_thread_list.GetMutex()); |
Matt Kopec | e9ea0da | 2013-05-07 19:29:28 +0000 | [diff] [blame] | 794 | uint32_t thread_count = m_thread_list.GetSize(false); |
| 795 | for (uint32_t i = 0; i < thread_count; ++i) |
| 796 | { |
| 797 | POSIXThread *thread = static_cast<POSIXThread*>( |
| 798 | m_thread_list.GetThreadAtIndex(i, false).get()); |
| 799 | if (thread) |
| 800 | wp_disabled &= thread->DisableHardwareWatchpoint(wp); |
| 801 | else |
| 802 | wp_disabled = false; |
| 803 | } |
| 804 | if (wp_disabled) |
| 805 | { |
Matt Kopec | 6f96123 | 2013-06-03 17:40:20 +0000 | [diff] [blame] | 806 | wp->SetHardwareIndex(LLDB_INVALID_INDEX32); |
Matt Kopec | e9ea0da | 2013-05-07 19:29:28 +0000 | [diff] [blame] | 807 | wp->SetEnabled(false, notify); |
| 808 | return error; |
| 809 | } |
| 810 | else |
| 811 | error.SetErrorString("Disabling hardware watchpoint failed"); |
| 812 | } |
| 813 | } |
| 814 | else |
| 815 | error.SetErrorString("Watchpoint argument was NULL."); |
| 816 | return error; |
| 817 | } |
| 818 | |
| 819 | Error |
| 820 | ProcessPOSIX::GetWatchpointSupportInfo(uint32_t &num) |
| 821 | { |
| 822 | Error error; |
Daniel Malea | a2cb9c4 | 2013-07-24 21:44:30 +0000 | [diff] [blame] | 823 | Mutex::Locker lock(m_thread_list.GetMutex()); |
Matt Kopec | e9ea0da | 2013-05-07 19:29:28 +0000 | [diff] [blame] | 824 | POSIXThread *thread = static_cast<POSIXThread*>( |
| 825 | m_thread_list.GetThreadAtIndex(0, false).get()); |
| 826 | if (thread) |
| 827 | num = thread->NumSupportedHardwareWatchpoints(); |
| 828 | else |
| 829 | error.SetErrorString("Process does not exist."); |
| 830 | return error; |
| 831 | } |
| 832 | |
| 833 | Error |
| 834 | ProcessPOSIX::GetWatchpointSupportInfo(uint32_t &num, bool &after) |
| 835 | { |
| 836 | Error error = GetWatchpointSupportInfo(num); |
| 837 | // Watchpoints trigger and halt the inferior after |
| 838 | // the corresponding instruction has been executed. |
| 839 | after = true; |
| 840 | return error; |
| 841 | } |
| 842 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 843 | uint32_t |
| 844 | ProcessPOSIX::UpdateThreadListIfNeeded() |
| 845 | { |
Daniel Malea | a2cb9c4 | 2013-07-24 21:44:30 +0000 | [diff] [blame] | 846 | Mutex::Locker lock(m_thread_list.GetMutex()); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 847 | // Do not allow recursive updates. |
| 848 | return m_thread_list.GetSize(false); |
| 849 | } |
| 850 | |
Greg Clayton | c3c0b0e | 2012-04-12 19:04:34 +0000 | [diff] [blame] | 851 | bool |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 852 | ProcessPOSIX::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list) |
| 853 | { |
Ashok Thirumurthi | 0118635 | 2013-03-28 16:02:31 +0000 | [diff] [blame] | 854 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD)); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 855 | if (log && log->GetMask().Test(POSIX_LOG_VERBOSE)) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 856 | log->Printf ("ProcessPOSIX::%s() (pid = %" PRIi64 ")", __FUNCTION__, GetID()); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 857 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 858 | bool has_updated = false; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 859 | // Update the process thread list with this new thread. |
| 860 | // FIXME: We should be using tid, not pid. |
| 861 | assert(m_monitor); |
| 862 | ThreadSP thread_sp (old_thread_list.FindThreadByID (GetID(), false)); |
Greg Clayton | 0c90ef4 | 2012-02-21 18:40:07 +0000 | [diff] [blame] | 863 | if (!thread_sp) { |
Michael Sartain | 9f822cd | 2013-07-31 23:27:46 +0000 | [diff] [blame] | 864 | thread_sp.reset(CreateNewPOSIXThread(*this, GetID())); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 865 | has_updated = true; |
Greg Clayton | 0c90ef4 | 2012-02-21 18:40:07 +0000 | [diff] [blame] | 866 | } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 867 | |
| 868 | if (log && log->GetMask().Test(POSIX_LOG_VERBOSE)) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 869 | log->Printf ("ProcessPOSIX::%s() updated pid = %" PRIi64, __FUNCTION__, GetID()); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 870 | new_thread_list.AddThread(thread_sp); |
| 871 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 872 | return has_updated; // the list has been updated |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | ByteOrder |
| 876 | ProcessPOSIX::GetByteOrder() const |
| 877 | { |
| 878 | // FIXME: We should be able to extract this value directly. See comment in |
| 879 | // ProcessPOSIX(). |
| 880 | return m_byte_order; |
| 881 | } |
| 882 | |
| 883 | size_t |
| 884 | ProcessPOSIX::PutSTDIN(const char *buf, size_t len, Error &error) |
| 885 | { |
| 886 | ssize_t status; |
| 887 | if ((status = write(m_monitor->GetTerminalFD(), buf, len)) < 0) |
| 888 | { |
| 889 | error.SetErrorToErrno(); |
| 890 | return 0; |
| 891 | } |
| 892 | return status; |
| 893 | } |
| 894 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 895 | UnixSignals & |
| 896 | ProcessPOSIX::GetUnixSignals() |
| 897 | { |
| 898 | return m_signals; |
| 899 | } |
| 900 | |
| 901 | //------------------------------------------------------------------------------ |
| 902 | // Utility functions. |
| 903 | |
| 904 | bool |
| 905 | ProcessPOSIX::HasExited() |
| 906 | { |
| 907 | switch (GetPrivateState()) |
| 908 | { |
| 909 | default: |
| 910 | break; |
| 911 | |
| 912 | case eStateDetached: |
| 913 | case eStateExited: |
| 914 | return true; |
| 915 | } |
| 916 | |
| 917 | return false; |
| 918 | } |
| 919 | |
| 920 | bool |
| 921 | ProcessPOSIX::IsStopped() |
| 922 | { |
| 923 | switch (GetPrivateState()) |
| 924 | { |
| 925 | default: |
| 926 | break; |
| 927 | |
| 928 | case eStateStopped: |
| 929 | case eStateCrashed: |
| 930 | case eStateSuspended: |
| 931 | return true; |
| 932 | } |
| 933 | |
| 934 | return false; |
| 935 | } |
Matt Kopec | b291044 | 2013-07-09 15:09:45 +0000 | [diff] [blame] | 936 | |
| 937 | bool |
| 938 | ProcessPOSIX::IsAThreadRunning() |
| 939 | { |
| 940 | bool is_running = false; |
Daniel Malea | a2cb9c4 | 2013-07-24 21:44:30 +0000 | [diff] [blame] | 941 | Mutex::Locker lock(m_thread_list.GetMutex()); |
Matt Kopec | b291044 | 2013-07-09 15:09:45 +0000 | [diff] [blame] | 942 | uint32_t thread_count = m_thread_list.GetSize(false); |
| 943 | for (uint32_t i = 0; i < thread_count; ++i) |
| 944 | { |
| 945 | POSIXThread *thread = static_cast<POSIXThread*>( |
| 946 | m_thread_list.GetThreadAtIndex(i, false).get()); |
| 947 | StateType thread_state = thread->GetState(); |
| 948 | if (thread_state == eStateRunning || thread_state == eStateStepping) |
| 949 | { |
| 950 | is_running = true; |
| 951 | break; |
| 952 | } |
| 953 | } |
| 954 | return is_running; |
| 955 | } |