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