Johnny Chen | 2341d35 | 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 | d891f9b | 2012-12-05 00:20:57 +0000 | [diff] [blame] | 10 | #include "lldb/lldb-python.h" |
| 11 | |
Johnny Chen | 2341d35 | 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 | 3d4d51c | 2013-05-07 19:29:28 +0000 | [diff] [blame] | 17 | #include "lldb/Breakpoint/Watchpoint.h" |
Greg Clayton | 8ac035d | 2012-09-04 14:55:50 +0000 | [diff] [blame] | 18 | #include "lldb/Core/Module.h" |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 19 | #include "lldb/Core/PluginManager.h" |
| 20 | #include "lldb/Core/State.h" |
Daniel Malea | 1e44fdd | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 21 | #include "lldb/Host/FileSpec.h" |
Johnny Chen | 2341d35 | 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 | 6c1a8cf | 2013-05-07 22:46:38 +0000 | [diff] [blame] | 25 | #include "lldb/Target/Platform.h" |
Johnny Chen | 2341d35 | 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 | 78dae82 | 2012-04-14 00:54:42 +0000 | [diff] [blame] | 75 | m_byte_order(lldb::endian::InlHostByteOrder()), |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 76 | m_monitor(NULL), |
| 77 | m_module(NULL), |
Andrew Kaylor | 3bd2ebd | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 78 | m_message_mutex (Mutex::eMutexTypeRecursive), |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 79 | m_in_limbo(false), |
| 80 | m_exit_now(false) |
| 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 | 78dae82 | 2012-04-14 00:54:42 +0000 | [diff] [blame] | 84 | lldb::ModuleSP module = GetTarget().GetExecutableModule(); |
| 85 | if (module != NULL && module->GetObjectFile() != NULL) |
| 86 | m_byte_order = module->GetObjectFile()->GetByteOrder(); |
Johnny Chen | 2341d35 | 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. |
| 96 | |
| 97 | bool |
| 98 | ProcessPOSIX::CanDebug(Target &target, bool plugin_specified_by_name) |
| 99 | { |
| 100 | // For now we are just making sure the file exists for a given module |
| 101 | ModuleSP exe_module_sp(target.GetExecutableModule()); |
| 102 | if (exe_module_sp.get()) |
| 103 | return exe_module_sp->GetFileSpec().Exists(); |
Andrew Kaylor | 6c1a8cf | 2013-05-07 22:46:38 +0000 | [diff] [blame] | 104 | // If there is no executable module, we return true since we might be preparing to attach. |
| 105 | return true; |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | Error |
| 109 | ProcessPOSIX::DoAttachToProcessWithID(lldb::pid_t pid) |
| 110 | { |
| 111 | Error error; |
| 112 | assert(m_monitor == NULL); |
| 113 | |
Ashok Thirumurthi | d8f6b64 | 2013-03-28 16:02:31 +0000 | [diff] [blame] | 114 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS)); |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 115 | if (log && log->GetMask().Test(POSIX_LOG_VERBOSE)) |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 116 | log->Printf ("ProcessPOSIX::%s(pid = %" PRIi64 ")", __FUNCTION__, GetID()); |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 117 | |
| 118 | m_monitor = new ProcessMonitor(this, pid, error); |
| 119 | |
| 120 | if (!error.Success()) |
| 121 | return error; |
| 122 | |
Andrew Kaylor | 6c1a8cf | 2013-05-07 22:46:38 +0000 | [diff] [blame] | 123 | PlatformSP platform_sp (m_target.GetPlatform ()); |
| 124 | assert (platform_sp.get()); |
| 125 | if (!platform_sp) |
| 126 | return error; // FIXME: Detatch? |
| 127 | |
| 128 | // Find out what we can about this process |
| 129 | ProcessInstanceInfo process_info; |
| 130 | platform_sp->GetProcessInfo (pid, process_info); |
| 131 | |
| 132 | // Resolve the executable module |
| 133 | ModuleSP exe_module_sp; |
| 134 | FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths()); |
| 135 | error = platform_sp->ResolveExecutable(process_info.GetExecutableFile(), |
| 136 | m_target.GetArchitecture(), |
| 137 | exe_module_sp, |
| 138 | executable_search_paths.GetSize() ? &executable_search_paths : NULL); |
| 139 | |
| 140 | // Fix the target architecture if necessary |
| 141 | const ArchSpec &module_arch = exe_module_sp->GetArchitecture(); |
| 142 | if (module_arch.IsValid() && !m_target.GetArchitecture().IsExactMatch(module_arch)) |
| 143 | m_target.SetArchitecture(module_arch); |
| 144 | |
| 145 | // Initialize the target module list |
| 146 | m_target.SetExecutableModule (exe_module_sp, true); |
| 147 | |
| 148 | if (!error.Success()) |
| 149 | return error; |
| 150 | |
| 151 | SetSTDIOFileDescriptor(m_monitor->GetTerminalFD()); |
| 152 | |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 153 | SetID(pid); |
Andrew Kaylor | 6c1a8cf | 2013-05-07 22:46:38 +0000 | [diff] [blame] | 154 | |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 155 | return error; |
| 156 | } |
| 157 | |
| 158 | Error |
Greg Clayton | c643077 | 2012-09-07 17:51:47 +0000 | [diff] [blame] | 159 | ProcessPOSIX::DoAttachToProcessWithID (lldb::pid_t pid, const ProcessAttachInfo &attach_info) |
| 160 | { |
| 161 | return DoAttachToProcessWithID(pid); |
| 162 | } |
| 163 | |
| 164 | Error |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 165 | ProcessPOSIX::WillLaunch(Module* module) |
| 166 | { |
| 167 | Error error; |
| 168 | return error; |
| 169 | } |
| 170 | |
| 171 | const char * |
| 172 | ProcessPOSIX::GetFilePath( |
| 173 | const lldb_private::ProcessLaunchInfo::FileAction *file_action, |
| 174 | const char *default_path) |
| 175 | { |
| 176 | const char *pts_name = "/dev/pts/"; |
| 177 | const char *path = NULL; |
| 178 | |
| 179 | if (file_action) |
| 180 | { |
| 181 | if (file_action->GetAction () == ProcessLaunchInfo::FileAction::eFileActionOpen) |
| 182 | path = file_action->GetPath(); |
| 183 | // By default the stdio paths passed in will be pseudo-terminal |
| 184 | // (/dev/pts). If so, convert to using a different default path |
| 185 | // instead to redirect I/O to the debugger console. This should |
| 186 | // also handle user overrides to /dev/null or a different file. |
| 187 | if (::strncmp(path, pts_name, ::strlen(pts_name)) == 0) |
| 188 | path = default_path; |
| 189 | } |
| 190 | |
| 191 | return path; |
| 192 | } |
| 193 | |
| 194 | Error |
| 195 | ProcessPOSIX::DoLaunch (Module *module, |
| 196 | const ProcessLaunchInfo &launch_info) |
| 197 | { |
| 198 | Error error; |
| 199 | assert(m_monitor == NULL); |
| 200 | |
Daniel Malea | 1e44fdd | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 201 | const char* working_dir = launch_info.GetWorkingDirectory(); |
| 202 | if (working_dir) { |
| 203 | FileSpec WorkingDir(working_dir, true); |
| 204 | if (!WorkingDir || WorkingDir.GetFileType() != FileSpec::eFileTypeDirectory) |
| 205 | { |
| 206 | error.SetErrorStringWithFormat("No such file or directory: %s", working_dir); |
| 207 | return error; |
| 208 | } |
| 209 | } |
| 210 | |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 211 | SetPrivateState(eStateLaunching); |
| 212 | |
| 213 | const lldb_private::ProcessLaunchInfo::FileAction *file_action; |
| 214 | |
| 215 | // Default of NULL will mean to use existing open file descriptors |
| 216 | const char *stdin_path = NULL; |
| 217 | const char *stdout_path = NULL; |
| 218 | const char *stderr_path = NULL; |
Daniel Malea | 1e44fdd | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 219 | |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 220 | file_action = launch_info.GetFileActionForFD (STDIN_FILENO); |
| 221 | stdin_path = GetFilePath(file_action, stdin_path); |
| 222 | |
| 223 | file_action = launch_info.GetFileActionForFD (STDOUT_FILENO); |
| 224 | stdout_path = GetFilePath(file_action, stdout_path); |
| 225 | |
| 226 | file_action = launch_info.GetFileActionForFD (STDERR_FILENO); |
| 227 | stderr_path = GetFilePath(file_action, stderr_path); |
| 228 | |
| 229 | m_monitor = new ProcessMonitor (this, |
| 230 | module, |
| 231 | launch_info.GetArguments().GetConstArgumentVector(), |
| 232 | launch_info.GetEnvironmentEntries().GetConstArgumentVector(), |
| 233 | stdin_path, |
| 234 | stdout_path, |
| 235 | stderr_path, |
Daniel Malea | 1e44fdd | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 236 | working_dir, |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 237 | error); |
| 238 | |
| 239 | m_module = module; |
| 240 | |
| 241 | if (!error.Success()) |
| 242 | return error; |
| 243 | |
Matt Kopec | a7cd95d | 2013-03-14 21:35:26 +0000 | [diff] [blame] | 244 | SetSTDIOFileDescriptor(m_monitor->GetTerminalFD()); |
| 245 | |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 246 | SetID(m_monitor->GetPID()); |
| 247 | return error; |
| 248 | } |
| 249 | |
| 250 | void |
| 251 | ProcessPOSIX::DidLaunch() |
| 252 | { |
| 253 | } |
| 254 | |
| 255 | Error |
| 256 | ProcessPOSIX::DoResume() |
| 257 | { |
| 258 | StateType state = GetPrivateState(); |
| 259 | |
| 260 | assert(state == eStateStopped || state == eStateCrashed); |
| 261 | |
| 262 | // We are about to resume a thread that will cause the process to exit so |
| 263 | // set our exit status now. Do not change our state if the inferior |
| 264 | // crashed. |
| 265 | if (state == eStateStopped) |
| 266 | { |
| 267 | if (m_in_limbo) |
| 268 | SetExitStatus(m_exit_status, NULL); |
| 269 | else |
| 270 | SetPrivateState(eStateRunning); |
| 271 | } |
| 272 | |
| 273 | bool did_resume = false; |
| 274 | uint32_t thread_count = m_thread_list.GetSize(false); |
| 275 | for (uint32_t i = 0; i < thread_count; ++i) |
| 276 | { |
| 277 | POSIXThread *thread = static_cast<POSIXThread*>( |
| 278 | m_thread_list.GetThreadAtIndex(i, false).get()); |
| 279 | did_resume = thread->Resume() || did_resume; |
| 280 | } |
| 281 | assert(did_resume && "Process resume failed!"); |
| 282 | |
| 283 | return Error(); |
| 284 | } |
| 285 | |
| 286 | addr_t |
| 287 | ProcessPOSIX::GetImageInfoAddress() |
| 288 | { |
| 289 | Target *target = &GetTarget(); |
| 290 | ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile(); |
| 291 | Address addr = obj_file->GetImageInfoAddress(); |
| 292 | |
| 293 | if (addr.IsValid()) |
| 294 | return addr.GetLoadAddress(target); |
| 295 | else |
| 296 | return LLDB_INVALID_ADDRESS; |
| 297 | } |
| 298 | |
| 299 | Error |
| 300 | ProcessPOSIX::DoHalt(bool &caused_stop) |
| 301 | { |
| 302 | Error error; |
| 303 | |
| 304 | if (IsStopped()) |
| 305 | { |
| 306 | caused_stop = false; |
| 307 | } |
| 308 | else if (kill(GetID(), SIGSTOP)) |
| 309 | { |
| 310 | caused_stop = false; |
| 311 | error.SetErrorToErrno(); |
| 312 | } |
| 313 | else |
| 314 | { |
| 315 | caused_stop = true; |
| 316 | } |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 317 | return error; |
| 318 | } |
| 319 | |
| 320 | Error |
Jim Ingham | 761afb8 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 321 | ProcessPOSIX::DoDetach(bool keep_stopped) |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 322 | { |
| 323 | Error error; |
Jim Ingham | 761afb8 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 324 | if (keep_stopped) |
| 325 | { |
| 326 | // FIXME: If you want to implement keep_stopped on Linux, |
| 327 | // this would be the place to do it. |
| 328 | error.SetErrorString("Detaching with keep_stopped true is not currently supported on Linux."); |
| 329 | return error; |
| 330 | } |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 331 | |
Matt Kopec | c350e13 | 2013-05-31 22:00:07 +0000 | [diff] [blame^] | 332 | uint32_t thread_count = m_thread_list.GetSize(false); |
| 333 | for (uint32_t i = 0; i < thread_count; ++i) |
| 334 | { |
| 335 | POSIXThread *thread = static_cast<POSIXThread*>( |
| 336 | m_thread_list.GetThreadAtIndex(i, false).get()); |
| 337 | error = m_monitor->Detach(thread->GetID()); |
| 338 | } |
| 339 | |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 340 | if (error.Success()) |
| 341 | SetPrivateState(eStateDetached); |
| 342 | |
| 343 | return error; |
| 344 | } |
| 345 | |
| 346 | Error |
| 347 | ProcessPOSIX::DoSignal(int signal) |
| 348 | { |
| 349 | Error error; |
| 350 | |
| 351 | if (kill(GetID(), signal)) |
| 352 | error.SetErrorToErrno(); |
| 353 | |
| 354 | return error; |
| 355 | } |
| 356 | |
| 357 | Error |
| 358 | ProcessPOSIX::DoDestroy() |
| 359 | { |
| 360 | Error error; |
| 361 | |
| 362 | if (!HasExited()) |
| 363 | { |
| 364 | // Drive the exit event to completion (do not keep the inferior in |
| 365 | // limbo). |
| 366 | m_exit_now = true; |
| 367 | |
Greg Clayton | 972c438 | 2012-03-30 19:56:32 +0000 | [diff] [blame] | 368 | if ((m_monitor == NULL || kill(m_monitor->GetPID(), SIGKILL)) && error.Success()) |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 369 | { |
| 370 | error.SetErrorToErrno(); |
| 371 | return error; |
| 372 | } |
| 373 | |
| 374 | SetPrivateState(eStateExited); |
| 375 | } |
| 376 | |
| 377 | return error; |
| 378 | } |
| 379 | |
| 380 | void |
| 381 | ProcessPOSIX::SendMessage(const ProcessMessage &message) |
| 382 | { |
| 383 | Mutex::Locker lock(m_message_mutex); |
| 384 | |
Andrew Kaylor | 3bd2ebd | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 385 | POSIXThread *thread = static_cast<POSIXThread*>( |
| 386 | m_thread_list.FindThreadByID(message.GetTID(), false).get()); |
| 387 | |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 388 | switch (message.GetKind()) |
| 389 | { |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 390 | case ProcessMessage::eInvalidMessage: |
| 391 | return; |
| 392 | |
| 393 | case ProcessMessage::eLimboMessage: |
Andrew Kaylor | 3bd2ebd | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 394 | assert(thread); |
| 395 | thread->SetState(eStateStopped); |
| 396 | if (message.GetTID() == GetID()) |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 397 | { |
Andrew Kaylor | 3bd2ebd | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 398 | m_in_limbo = true; |
| 399 | m_exit_status = message.GetExitStatus(); |
| 400 | if (m_exit_now) |
| 401 | { |
| 402 | SetPrivateState(eStateExited); |
Matt Kopec | c350e13 | 2013-05-31 22:00:07 +0000 | [diff] [blame^] | 403 | m_monitor->Detach(GetID()); |
Andrew Kaylor | 3bd2ebd | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 404 | } |
| 405 | else |
| 406 | { |
| 407 | StopAllThreads(message.GetTID()); |
| 408 | SetPrivateState(eStateStopped); |
| 409 | } |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 410 | } |
| 411 | else |
Andrew Kaylor | 3bd2ebd | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 412 | { |
| 413 | StopAllThreads(message.GetTID()); |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 414 | SetPrivateState(eStateStopped); |
Andrew Kaylor | 3bd2ebd | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 415 | } |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 416 | break; |
| 417 | |
| 418 | case ProcessMessage::eExitMessage: |
Andrew Kaylor | 3bd2ebd | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 419 | assert(thread); |
| 420 | thread->SetState(eStateExited); |
| 421 | // FIXME: I'm not sure we need to do this. |
| 422 | if (message.GetTID() == GetID()) |
| 423 | { |
| 424 | m_exit_status = message.GetExitStatus(); |
| 425 | SetExitStatus(m_exit_status, NULL); |
| 426 | } |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 427 | break; |
| 428 | |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 429 | case ProcessMessage::eBreakpointMessage: |
Andrew Kaylor | 3bd2ebd | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 430 | case ProcessMessage::eTraceMessage: |
Matt Kopec | 3d4d51c | 2013-05-07 19:29:28 +0000 | [diff] [blame] | 431 | case ProcessMessage::eWatchpointMessage: |
Andrew Kaylor | 3bd2ebd | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 432 | case ProcessMessage::eNewThreadMessage: |
| 433 | case ProcessMessage::eCrashMessage: |
| 434 | assert(thread); |
| 435 | thread->SetState(eStateStopped); |
| 436 | StopAllThreads(message.GetTID()); |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 437 | SetPrivateState(eStateStopped); |
| 438 | break; |
| 439 | |
| 440 | case ProcessMessage::eSignalMessage: |
| 441 | case ProcessMessage::eSignalDeliveredMessage: |
Matt Kopec | f1fda37 | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 442 | { |
| 443 | lldb::tid_t tid = message.GetTID(); |
| 444 | lldb::tid_t pid = GetID(); |
| 445 | if (tid == pid) { |
Andrew Kaylor | 3bd2ebd | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 446 | assert(thread); |
| 447 | thread->SetState(eStateStopped); |
| 448 | StopAllThreads(message.GetTID()); |
Matt Kopec | f1fda37 | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 449 | SetPrivateState(eStateStopped); |
| 450 | break; |
| 451 | } else { |
| 452 | // FIXME: Ignore any signals generated by children. |
| 453 | return; |
| 454 | } |
| 455 | } |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 456 | |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | m_message_queue.push(message); |
| 460 | } |
| 461 | |
Andrew Kaylor | 3bd2ebd | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 462 | void |
| 463 | ProcessPOSIX::StopAllThreads(lldb::tid_t stop_tid) |
| 464 | { |
| 465 | // FIXME: Will this work the same way on FreeBSD and Linux? |
| 466 | } |
| 467 | |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 468 | void |
| 469 | ProcessPOSIX::RefreshStateAfterStop() |
| 470 | { |
Ashok Thirumurthi | d8f6b64 | 2013-03-28 16:02:31 +0000 | [diff] [blame] | 471 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS)); |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 472 | if (log && log->GetMask().Test(POSIX_LOG_VERBOSE)) |
Andrew Kaylor | 3bd2ebd | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 473 | log->Printf ("ProcessPOSIX::%s(), message_queue size = %d", __FUNCTION__, (int)m_message_queue.size()); |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 474 | |
| 475 | Mutex::Locker lock(m_message_mutex); |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 476 | |
Andrew Kaylor | 3bd2ebd | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 477 | // This method used to only handle one message. Changing it to loop allows |
| 478 | // it to handle the case where we hit a breakpoint while handling a different |
| 479 | // breakpoint. |
| 480 | while (!m_message_queue.empty()) |
| 481 | { |
| 482 | ProcessMessage &message = m_message_queue.front(); |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 483 | |
Andrew Kaylor | 3bd2ebd | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 484 | // Resolve the thread this message corresponds to and pass it along. |
| 485 | lldb::tid_t tid = message.GetTID(); |
| 486 | if (log) |
| 487 | log->Printf ("ProcessPOSIX::%s(), message_queue size = %d, pid = %" PRIi64, __FUNCTION__, (int)m_message_queue.size(), tid); |
| 488 | POSIXThread *thread = static_cast<POSIXThread*>( |
| 489 | GetThreadList().FindThreadByID(tid, false).get()); |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 490 | |
Andrew Kaylor | 3bd2ebd | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 491 | if (message.GetKind() == ProcessMessage::eNewThreadMessage) |
| 492 | { |
| 493 | if (log) |
| 494 | log->Printf ("ProcessPOSIX::%s() adding thread, tid = %" PRIi64, __FUNCTION__, message.GetChildTID()); |
| 495 | ThreadSP thread_sp; |
| 496 | thread_sp.reset(new POSIXThread(*this, message.GetChildTID())); |
| 497 | m_thread_list.AddThread(thread_sp); |
| 498 | } |
| 499 | |
| 500 | m_thread_list.RefreshStateAfterStop(); |
| 501 | |
| 502 | if (thread) |
| 503 | thread->Notify(message); |
| 504 | |
| 505 | if (message.GetKind() == ProcessMessage::eExitMessage) |
| 506 | { |
| 507 | // FIXME: We should tell the user about this, but the limbo message is probably better for that. |
| 508 | if (log) |
| 509 | log->Printf ("ProcessPOSIX::%s() removing thread, tid = %" PRIi64, __FUNCTION__, tid); |
| 510 | ThreadSP thread_sp = m_thread_list.RemoveThreadByID(tid, false); |
| 511 | thread_sp.reset(); |
| 512 | } |
| 513 | |
| 514 | m_message_queue.pop(); |
Matt Kopec | f1fda37 | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 515 | } |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | bool |
| 519 | ProcessPOSIX::IsAlive() |
| 520 | { |
| 521 | StateType state = GetPrivateState(); |
Daniel Malea | 0247cf5 | 2013-04-01 19:48:37 +0000 | [diff] [blame] | 522 | return state != eStateDetached |
| 523 | && state != eStateExited |
| 524 | && state != eStateInvalid |
| 525 | && state != eStateUnloaded; |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | size_t |
| 529 | ProcessPOSIX::DoReadMemory(addr_t vm_addr, |
| 530 | void *buf, size_t size, Error &error) |
| 531 | { |
| 532 | assert(m_monitor); |
| 533 | return m_monitor->ReadMemory(vm_addr, buf, size, error); |
| 534 | } |
| 535 | |
| 536 | size_t |
| 537 | ProcessPOSIX::DoWriteMemory(addr_t vm_addr, const void *buf, size_t size, |
| 538 | Error &error) |
| 539 | { |
| 540 | assert(m_monitor); |
| 541 | return m_monitor->WriteMemory(vm_addr, buf, size, error); |
| 542 | } |
| 543 | |
| 544 | addr_t |
| 545 | ProcessPOSIX::DoAllocateMemory(size_t size, uint32_t permissions, |
| 546 | Error &error) |
| 547 | { |
| 548 | addr_t allocated_addr = LLDB_INVALID_ADDRESS; |
| 549 | |
| 550 | unsigned prot = 0; |
| 551 | if (permissions & lldb::ePermissionsReadable) |
| 552 | prot |= eMmapProtRead; |
| 553 | if (permissions & lldb::ePermissionsWritable) |
| 554 | prot |= eMmapProtWrite; |
| 555 | if (permissions & lldb::ePermissionsExecutable) |
| 556 | prot |= eMmapProtExec; |
| 557 | |
| 558 | if (InferiorCallMmap(this, allocated_addr, 0, size, prot, |
| 559 | eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) { |
| 560 | m_addr_to_mmap_size[allocated_addr] = size; |
| 561 | error.Clear(); |
| 562 | } else { |
| 563 | allocated_addr = LLDB_INVALID_ADDRESS; |
| 564 | error.SetErrorStringWithFormat("unable to allocate %zu bytes of memory with permissions %s", size, GetPermissionsAsCString (permissions)); |
| 565 | } |
| 566 | |
| 567 | return allocated_addr; |
| 568 | } |
| 569 | |
| 570 | Error |
| 571 | ProcessPOSIX::DoDeallocateMemory(lldb::addr_t addr) |
| 572 | { |
| 573 | Error error; |
| 574 | MMapMap::iterator pos = m_addr_to_mmap_size.find(addr); |
| 575 | if (pos != m_addr_to_mmap_size.end() && |
| 576 | InferiorCallMunmap(this, addr, pos->second)) |
| 577 | m_addr_to_mmap_size.erase (pos); |
| 578 | else |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 579 | error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, addr); |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 580 | |
| 581 | return error; |
| 582 | } |
| 583 | |
Matt Kopec | 4f9103f | 2013-02-27 20:13:38 +0000 | [diff] [blame] | 584 | addr_t |
| 585 | ProcessPOSIX::ResolveIndirectFunction(const Address *address, Error &error) |
| 586 | { |
| 587 | addr_t function_addr = LLDB_INVALID_ADDRESS; |
| 588 | if (address == NULL) { |
| 589 | error.SetErrorStringWithFormat("unable to determine direct function call for NULL address"); |
| 590 | } else if (!InferiorCall(this, address, function_addr)) { |
| 591 | function_addr = LLDB_INVALID_ADDRESS; |
Matt Kopec | 7012548 | 2013-03-01 17:44:31 +0000 | [diff] [blame] | 592 | error.SetErrorStringWithFormat("unable to determine direct function call for indirect function %s", |
| 593 | address->CalculateSymbolContextSymbol()->GetName().AsCString()); |
Matt Kopec | 4f9103f | 2013-02-27 20:13:38 +0000 | [diff] [blame] | 594 | } |
| 595 | return function_addr; |
| 596 | } |
| 597 | |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 598 | size_t |
| 599 | ProcessPOSIX::GetSoftwareBreakpointTrapOpcode(BreakpointSite* bp_site) |
| 600 | { |
| 601 | static const uint8_t g_i386_opcode[] = { 0xCC }; |
| 602 | |
| 603 | ArchSpec arch = GetTarget().GetArchitecture(); |
| 604 | const uint8_t *opcode = NULL; |
| 605 | size_t opcode_size = 0; |
| 606 | |
| 607 | switch (arch.GetCore()) |
| 608 | { |
| 609 | default: |
| 610 | assert(false && "CPU type not supported!"); |
| 611 | break; |
| 612 | |
| 613 | case ArchSpec::eCore_x86_32_i386: |
| 614 | case ArchSpec::eCore_x86_64_x86_64: |
| 615 | opcode = g_i386_opcode; |
| 616 | opcode_size = sizeof(g_i386_opcode); |
| 617 | break; |
| 618 | } |
| 619 | |
| 620 | bp_site->SetTrapOpcode(opcode, opcode_size); |
| 621 | return opcode_size; |
| 622 | } |
| 623 | |
| 624 | Error |
Daniel Malea | 52d8dd9 | 2013-02-15 20:23:25 +0000 | [diff] [blame] | 625 | ProcessPOSIX::EnableBreakpointSite(BreakpointSite *bp_site) |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 626 | { |
| 627 | return EnableSoftwareBreakpoint(bp_site); |
| 628 | } |
| 629 | |
| 630 | Error |
Daniel Malea | 52d8dd9 | 2013-02-15 20:23:25 +0000 | [diff] [blame] | 631 | ProcessPOSIX::DisableBreakpointSite(BreakpointSite *bp_site) |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 632 | { |
| 633 | return DisableSoftwareBreakpoint(bp_site); |
| 634 | } |
| 635 | |
Matt Kopec | 3d4d51c | 2013-05-07 19:29:28 +0000 | [diff] [blame] | 636 | Error |
| 637 | ProcessPOSIX::EnableWatchpoint(Watchpoint *wp, bool notify) |
| 638 | { |
| 639 | Error error; |
| 640 | if (wp) |
| 641 | { |
| 642 | user_id_t watchID = wp->GetID(); |
| 643 | addr_t addr = wp->GetLoadAddress(); |
| 644 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); |
| 645 | if (log) |
| 646 | log->Printf ("ProcessPOSIX::EnableWatchpoint(watchID = %" PRIu64 ")", |
| 647 | watchID); |
| 648 | if (wp->IsEnabled()) |
| 649 | { |
| 650 | if (log) |
| 651 | log->Printf("ProcessPOSIX::EnableWatchpoint(watchID = %" PRIu64 |
| 652 | ") addr = 0x%8.8" PRIx64 ": watchpoint already enabled.", |
| 653 | watchID, (uint64_t)addr); |
| 654 | return error; |
| 655 | } |
| 656 | |
| 657 | bool wp_enabled = true; |
| 658 | uint32_t thread_count = m_thread_list.GetSize(false); |
| 659 | for (uint32_t i = 0; i < thread_count; ++i) |
| 660 | { |
| 661 | POSIXThread *thread = static_cast<POSIXThread*>( |
| 662 | m_thread_list.GetThreadAtIndex(i, false).get()); |
| 663 | if (thread) |
| 664 | wp_enabled &= thread->EnableHardwareWatchpoint(wp); |
| 665 | else |
| 666 | { |
| 667 | wp_enabled = false; |
| 668 | break; |
| 669 | } |
| 670 | } |
| 671 | if (wp_enabled) |
| 672 | { |
| 673 | wp->SetEnabled(true, notify); |
| 674 | return error; |
| 675 | } |
| 676 | else |
| 677 | { |
| 678 | // Watchpoint enabling failed on at least one |
| 679 | // of the threads so roll back all of them |
| 680 | DisableWatchpoint(wp, false); |
| 681 | error.SetErrorString("Setting hardware watchpoint failed"); |
| 682 | } |
| 683 | } |
| 684 | else |
| 685 | error.SetErrorString("Watchpoint argument was NULL."); |
| 686 | return error; |
| 687 | } |
| 688 | |
| 689 | Error |
| 690 | ProcessPOSIX::DisableWatchpoint(Watchpoint *wp, bool notify) |
| 691 | { |
| 692 | Error error; |
| 693 | if (wp) |
| 694 | { |
| 695 | user_id_t watchID = wp->GetID(); |
| 696 | addr_t addr = wp->GetLoadAddress(); |
| 697 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); |
| 698 | if (log) |
| 699 | log->Printf("ProcessPOSIX::DisableWatchpoint(watchID = %" PRIu64 ")", |
| 700 | watchID); |
| 701 | if (!wp->IsEnabled()) |
| 702 | { |
| 703 | if (log) |
| 704 | log->Printf("ProcessPOSIX::DisableWatchpoint(watchID = %" PRIu64 |
| 705 | ") addr = 0x%8.8" PRIx64 ": watchpoint already disabled.", |
| 706 | watchID, (uint64_t)addr); |
| 707 | // This is needed (for now) to keep watchpoints disabled correctly |
| 708 | wp->SetEnabled(false, notify); |
| 709 | return error; |
| 710 | } |
| 711 | |
| 712 | if (wp->IsHardware()) |
| 713 | { |
| 714 | bool wp_disabled = true; |
| 715 | uint32_t thread_count = m_thread_list.GetSize(false); |
| 716 | for (uint32_t i = 0; i < thread_count; ++i) |
| 717 | { |
| 718 | POSIXThread *thread = static_cast<POSIXThread*>( |
| 719 | m_thread_list.GetThreadAtIndex(i, false).get()); |
| 720 | if (thread) |
| 721 | wp_disabled &= thread->DisableHardwareWatchpoint(wp); |
| 722 | else |
| 723 | wp_disabled = false; |
| 724 | } |
| 725 | if (wp_disabled) |
| 726 | { |
| 727 | wp->SetEnabled(false, notify); |
| 728 | return error; |
| 729 | } |
| 730 | else |
| 731 | error.SetErrorString("Disabling hardware watchpoint failed"); |
| 732 | } |
| 733 | } |
| 734 | else |
| 735 | error.SetErrorString("Watchpoint argument was NULL."); |
| 736 | return error; |
| 737 | } |
| 738 | |
| 739 | Error |
| 740 | ProcessPOSIX::GetWatchpointSupportInfo(uint32_t &num) |
| 741 | { |
| 742 | Error error; |
| 743 | POSIXThread *thread = static_cast<POSIXThread*>( |
| 744 | m_thread_list.GetThreadAtIndex(0, false).get()); |
| 745 | if (thread) |
| 746 | num = thread->NumSupportedHardwareWatchpoints(); |
| 747 | else |
| 748 | error.SetErrorString("Process does not exist."); |
| 749 | return error; |
| 750 | } |
| 751 | |
| 752 | Error |
| 753 | ProcessPOSIX::GetWatchpointSupportInfo(uint32_t &num, bool &after) |
| 754 | { |
| 755 | Error error = GetWatchpointSupportInfo(num); |
| 756 | // Watchpoints trigger and halt the inferior after |
| 757 | // the corresponding instruction has been executed. |
| 758 | after = true; |
| 759 | return error; |
| 760 | } |
| 761 | |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 762 | uint32_t |
| 763 | ProcessPOSIX::UpdateThreadListIfNeeded() |
| 764 | { |
| 765 | // Do not allow recursive updates. |
| 766 | return m_thread_list.GetSize(false); |
| 767 | } |
| 768 | |
Greg Clayton | c8dd570 | 2012-04-12 19:04:34 +0000 | [diff] [blame] | 769 | bool |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 770 | ProcessPOSIX::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list) |
| 771 | { |
Ashok Thirumurthi | d8f6b64 | 2013-03-28 16:02:31 +0000 | [diff] [blame] | 772 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD)); |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 773 | if (log && log->GetMask().Test(POSIX_LOG_VERBOSE)) |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 774 | log->Printf ("ProcessPOSIX::%s() (pid = %" PRIi64 ")", __FUNCTION__, GetID()); |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 775 | |
| 776 | // Update the process thread list with this new thread. |
| 777 | // FIXME: We should be using tid, not pid. |
| 778 | assert(m_monitor); |
| 779 | ThreadSP thread_sp (old_thread_list.FindThreadByID (GetID(), false)); |
Greg Clayton | e5eaa30 | 2012-02-21 18:40:07 +0000 | [diff] [blame] | 780 | if (!thread_sp) { |
Greg Clayton | 5e91e37 | 2012-10-12 16:23:23 +0000 | [diff] [blame] | 781 | thread_sp.reset(new POSIXThread(*this, GetID())); |
Greg Clayton | e5eaa30 | 2012-02-21 18:40:07 +0000 | [diff] [blame] | 782 | } |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 783 | |
| 784 | if (log && log->GetMask().Test(POSIX_LOG_VERBOSE)) |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 785 | log->Printf ("ProcessPOSIX::%s() updated pid = %" PRIi64, __FUNCTION__, GetID()); |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 786 | new_thread_list.AddThread(thread_sp); |
| 787 | |
Greg Clayton | c8dd570 | 2012-04-12 19:04:34 +0000 | [diff] [blame] | 788 | return new_thread_list.GetSize(false) > 0; |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | ByteOrder |
| 792 | ProcessPOSIX::GetByteOrder() const |
| 793 | { |
| 794 | // FIXME: We should be able to extract this value directly. See comment in |
| 795 | // ProcessPOSIX(). |
| 796 | return m_byte_order; |
| 797 | } |
| 798 | |
| 799 | size_t |
| 800 | ProcessPOSIX::PutSTDIN(const char *buf, size_t len, Error &error) |
| 801 | { |
| 802 | ssize_t status; |
| 803 | if ((status = write(m_monitor->GetTerminalFD(), buf, len)) < 0) |
| 804 | { |
| 805 | error.SetErrorToErrno(); |
| 806 | return 0; |
| 807 | } |
| 808 | return status; |
| 809 | } |
| 810 | |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 811 | UnixSignals & |
| 812 | ProcessPOSIX::GetUnixSignals() |
| 813 | { |
| 814 | return m_signals; |
| 815 | } |
| 816 | |
| 817 | //------------------------------------------------------------------------------ |
| 818 | // Utility functions. |
| 819 | |
| 820 | bool |
| 821 | ProcessPOSIX::HasExited() |
| 822 | { |
| 823 | switch (GetPrivateState()) |
| 824 | { |
| 825 | default: |
| 826 | break; |
| 827 | |
| 828 | case eStateDetached: |
| 829 | case eStateExited: |
| 830 | return true; |
| 831 | } |
| 832 | |
| 833 | return false; |
| 834 | } |
| 835 | |
| 836 | bool |
| 837 | ProcessPOSIX::IsStopped() |
| 838 | { |
| 839 | switch (GetPrivateState()) |
| 840 | { |
| 841 | default: |
| 842 | break; |
| 843 | |
| 844 | case eStateStopped: |
| 845 | case eStateCrashed: |
| 846 | case eStateSuspended: |
| 847 | return true; |
| 848 | } |
| 849 | |
| 850 | return false; |
| 851 | } |