NativeProcessProtocol.cpp revision 314564
1//===-- NativeProcessProtocol.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 10#include "lldb/Host/common/NativeProcessProtocol.h" 11 12#include "lldb/Core/ArchSpec.h" 13#include "lldb/Core/Log.h" 14#include "lldb/Core/ModuleSpec.h" 15#include "lldb/Core/State.h" 16#include "lldb/Host/Host.h" 17#include "lldb/Host/common/NativeRegisterContext.h" 18#include "lldb/Host/common/NativeThreadProtocol.h" 19#include "lldb/Host/common/SoftwareBreakpoint.h" 20#include "lldb/Symbol/ObjectFile.h" 21#include "lldb/Target/Process.h" 22#include "lldb/Utility/LLDBAssert.h" 23#include "lldb/lldb-enumerations.h" 24 25using namespace lldb; 26using namespace lldb_private; 27 28// ----------------------------------------------------------------------------- 29// NativeProcessProtocol Members 30// ----------------------------------------------------------------------------- 31 32NativeProcessProtocol::NativeProcessProtocol(lldb::pid_t pid) 33 : m_pid(pid), m_threads(), m_current_thread_id(LLDB_INVALID_THREAD_ID), 34 m_threads_mutex(), m_state(lldb::eStateInvalid), m_state_mutex(), 35 m_exit_type(eExitTypeInvalid), m_exit_status(0), m_exit_description(), 36 m_delegates_mutex(), m_delegates(), m_breakpoint_list(), 37 m_watchpoint_list(), m_terminal_fd(-1), m_stop_id(0) {} 38 39lldb_private::Error NativeProcessProtocol::Interrupt() { 40 Error error; 41#if !defined(SIGSTOP) 42 error.SetErrorString("local host does not support signaling"); 43 return error; 44#else 45 return Signal(SIGSTOP); 46#endif 47} 48 49lldb_private::Error 50NativeProcessProtocol::GetMemoryRegionInfo(lldb::addr_t load_addr, 51 MemoryRegionInfo &range_info) { 52 // Default: not implemented. 53 return Error("not implemented"); 54} 55 56bool NativeProcessProtocol::GetExitStatus(ExitType *exit_type, int *status, 57 std::string &exit_description) { 58 if (m_state == lldb::eStateExited) { 59 *exit_type = m_exit_type; 60 *status = m_exit_status; 61 exit_description = m_exit_description; 62 return true; 63 } 64 65 *status = 0; 66 return false; 67} 68 69bool NativeProcessProtocol::SetExitStatus(ExitType exit_type, int status, 70 const char *exit_description, 71 bool bNotifyStateChange) { 72 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 73 if (log) 74 log->Printf("NativeProcessProtocol::%s(%d, %d, %s, %s) called", 75 __FUNCTION__, exit_type, status, 76 exit_description ? exit_description : "nullptr", 77 bNotifyStateChange ? "true" : "false"); 78 79 // Exit status already set 80 if (m_state == lldb::eStateExited) { 81 if (log) 82 log->Printf("NativeProcessProtocol::%s exit status already set to %d, " 83 "ignoring new set to %d", 84 __FUNCTION__, m_exit_status, status); 85 return false; 86 } 87 88 m_state = lldb::eStateExited; 89 90 m_exit_type = exit_type; 91 m_exit_status = status; 92 if (exit_description && exit_description[0]) 93 m_exit_description = exit_description; 94 else 95 m_exit_description.clear(); 96 97 if (bNotifyStateChange) 98 SynchronouslyNotifyProcessStateChanged(lldb::eStateExited); 99 100 return true; 101} 102 103NativeThreadProtocolSP NativeProcessProtocol::GetThreadAtIndex(uint32_t idx) { 104 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); 105 if (idx < m_threads.size()) 106 return m_threads[idx]; 107 return NativeThreadProtocolSP(); 108} 109 110NativeThreadProtocolSP 111NativeProcessProtocol::GetThreadByIDUnlocked(lldb::tid_t tid) { 112 for (auto thread_sp : m_threads) { 113 if (thread_sp->GetID() == tid) 114 return thread_sp; 115 } 116 return NativeThreadProtocolSP(); 117} 118 119NativeThreadProtocolSP NativeProcessProtocol::GetThreadByID(lldb::tid_t tid) { 120 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); 121 return GetThreadByIDUnlocked(tid); 122} 123 124bool NativeProcessProtocol::IsAlive() const { 125 return m_state != eStateDetached && m_state != eStateExited && 126 m_state != eStateInvalid && m_state != eStateUnloaded; 127} 128 129bool NativeProcessProtocol::GetByteOrder(lldb::ByteOrder &byte_order) const { 130 ArchSpec process_arch; 131 if (!GetArchitecture(process_arch)) 132 return false; 133 byte_order = process_arch.GetByteOrder(); 134 return true; 135} 136 137const NativeWatchpointList::WatchpointMap & 138NativeProcessProtocol::GetWatchpointMap() const { 139 return m_watchpoint_list.GetWatchpointMap(); 140} 141 142uint32_t NativeProcessProtocol::GetMaxWatchpoints() const { 143 // This default implementation will return the number of 144 // *hardware* breakpoints available. MacOSX and other OS 145 // implementations that support software breakpoints will want to 146 // override this correctly for their implementation. 147 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 148 149 // get any thread 150 NativeThreadProtocolSP thread_sp( 151 const_cast<NativeProcessProtocol *>(this)->GetThreadAtIndex(0)); 152 if (!thread_sp) { 153 if (log) 154 log->Warning("NativeProcessProtocol::%s (): failed to find a thread to " 155 "grab a NativeRegisterContext!", 156 __FUNCTION__); 157 return 0; 158 } 159 160 NativeRegisterContextSP reg_ctx_sp(thread_sp->GetRegisterContext()); 161 if (!reg_ctx_sp) { 162 if (log) 163 log->Warning("NativeProcessProtocol::%s (): failed to get a " 164 "RegisterContextNativeProcess from the first thread!", 165 __FUNCTION__); 166 return 0; 167 } 168 169 return reg_ctx_sp->NumSupportedHardwareWatchpoints(); 170} 171 172Error NativeProcessProtocol::SetWatchpoint(lldb::addr_t addr, size_t size, 173 uint32_t watch_flags, 174 bool hardware) { 175 // This default implementation assumes setting the watchpoint for 176 // the process will require setting the watchpoint for each of the 177 // threads. Furthermore, it will track watchpoints set for the 178 // process and will add them to each thread that is attached to 179 // via the (FIXME implement) OnThreadAttached () method. 180 181 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 182 183 // Update the thread list 184 UpdateThreads(); 185 186 // Keep track of the threads we successfully set the watchpoint 187 // for. If one of the thread watchpoint setting operations fails, 188 // back off and remove the watchpoint for all the threads that 189 // were successfully set so we get back to a consistent state. 190 std::vector<NativeThreadProtocolSP> watchpoint_established_threads; 191 192 // Tell each thread to set a watchpoint. In the event that 193 // hardware watchpoints are requested but the SetWatchpoint fails, 194 // try to set a software watchpoint as a fallback. It's 195 // conceivable that if there are more threads than hardware 196 // watchpoints available, some of the threads will fail to set 197 // hardware watchpoints while software ones may be available. 198 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); 199 for (auto thread_sp : m_threads) { 200 assert(thread_sp && "thread list should not have a NULL thread!"); 201 if (!thread_sp) 202 continue; 203 204 Error thread_error = 205 thread_sp->SetWatchpoint(addr, size, watch_flags, hardware); 206 if (thread_error.Fail() && hardware) { 207 // Try software watchpoints since we failed on hardware watchpoint setting 208 // and we may have just run out of hardware watchpoints. 209 thread_error = thread_sp->SetWatchpoint(addr, size, watch_flags, false); 210 if (thread_error.Success()) { 211 if (log) 212 log->Warning( 213 "hardware watchpoint requested but software watchpoint set"); 214 } 215 } 216 217 if (thread_error.Success()) { 218 // Remember that we set this watchpoint successfully in 219 // case we need to clear it later. 220 watchpoint_established_threads.push_back(thread_sp); 221 } else { 222 // Unset the watchpoint for each thread we successfully 223 // set so that we get back to a consistent state of "not 224 // set" for the watchpoint. 225 for (auto unwatch_thread_sp : watchpoint_established_threads) { 226 Error remove_error = unwatch_thread_sp->RemoveWatchpoint(addr); 227 if (remove_error.Fail() && log) { 228 log->Warning("NativeProcessProtocol::%s (): RemoveWatchpoint failed " 229 "for pid=%" PRIu64 ", tid=%" PRIu64 ": %s", 230 __FUNCTION__, GetID(), unwatch_thread_sp->GetID(), 231 remove_error.AsCString()); 232 } 233 } 234 235 return thread_error; 236 } 237 } 238 return m_watchpoint_list.Add(addr, size, watch_flags, hardware); 239} 240 241Error NativeProcessProtocol::RemoveWatchpoint(lldb::addr_t addr) { 242 // Update the thread list 243 UpdateThreads(); 244 245 Error overall_error; 246 247 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); 248 for (auto thread_sp : m_threads) { 249 assert(thread_sp && "thread list should not have a NULL thread!"); 250 if (!thread_sp) 251 continue; 252 253 const Error thread_error = thread_sp->RemoveWatchpoint(addr); 254 if (thread_error.Fail()) { 255 // Keep track of the first thread error if any threads 256 // fail. We want to try to remove the watchpoint from 257 // every thread, though, even if one or more have errors. 258 if (!overall_error.Fail()) 259 overall_error = thread_error; 260 } 261 } 262 const Error error = m_watchpoint_list.Remove(addr); 263 return overall_error.Fail() ? overall_error : error; 264} 265 266bool NativeProcessProtocol::RegisterNativeDelegate( 267 NativeDelegate &native_delegate) { 268 std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex); 269 if (std::find(m_delegates.begin(), m_delegates.end(), &native_delegate) != 270 m_delegates.end()) 271 return false; 272 273 m_delegates.push_back(&native_delegate); 274 native_delegate.InitializeDelegate(this); 275 return true; 276} 277 278bool NativeProcessProtocol::UnregisterNativeDelegate( 279 NativeDelegate &native_delegate) { 280 std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex); 281 282 const auto initial_size = m_delegates.size(); 283 m_delegates.erase( 284 remove(m_delegates.begin(), m_delegates.end(), &native_delegate), 285 m_delegates.end()); 286 287 // We removed the delegate if the count of delegates shrank after 288 // removing all copies of the given native_delegate from the vector. 289 return m_delegates.size() < initial_size; 290} 291 292void NativeProcessProtocol::SynchronouslyNotifyProcessStateChanged( 293 lldb::StateType state) { 294 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 295 296 std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex); 297 for (auto native_delegate : m_delegates) 298 native_delegate->ProcessStateChanged(this, state); 299 300 if (log) { 301 if (!m_delegates.empty()) { 302 log->Printf("NativeProcessProtocol::%s: sent state notification [%s] " 303 "from process %" PRIu64, 304 __FUNCTION__, lldb_private::StateAsCString(state), GetID()); 305 } else { 306 log->Printf("NativeProcessProtocol::%s: would send state notification " 307 "[%s] from process %" PRIu64 ", but no delegates", 308 __FUNCTION__, lldb_private::StateAsCString(state), GetID()); 309 } 310 } 311} 312 313void NativeProcessProtocol::NotifyDidExec() { 314 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 315 if (log) 316 log->Printf("NativeProcessProtocol::%s - preparing to call delegates", 317 __FUNCTION__); 318 319 { 320 std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex); 321 for (auto native_delegate : m_delegates) 322 native_delegate->DidExec(this); 323 } 324} 325 326Error NativeProcessProtocol::SetSoftwareBreakpoint(lldb::addr_t addr, 327 uint32_t size_hint) { 328 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); 329 if (log) 330 log->Printf("NativeProcessProtocol::%s addr = 0x%" PRIx64, __FUNCTION__, 331 addr); 332 333 return m_breakpoint_list.AddRef( 334 addr, size_hint, false, 335 [this](lldb::addr_t addr, size_t size_hint, bool /* hardware */, 336 NativeBreakpointSP &breakpoint_sp) -> Error { 337 return SoftwareBreakpoint::CreateSoftwareBreakpoint( 338 *this, addr, size_hint, breakpoint_sp); 339 }); 340} 341 342Error NativeProcessProtocol::RemoveBreakpoint(lldb::addr_t addr) { 343 return m_breakpoint_list.DecRef(addr); 344} 345 346Error NativeProcessProtocol::EnableBreakpoint(lldb::addr_t addr) { 347 return m_breakpoint_list.EnableBreakpoint(addr); 348} 349 350Error NativeProcessProtocol::DisableBreakpoint(lldb::addr_t addr) { 351 return m_breakpoint_list.DisableBreakpoint(addr); 352} 353 354lldb::StateType NativeProcessProtocol::GetState() const { 355 std::lock_guard<std::recursive_mutex> guard(m_state_mutex); 356 return m_state; 357} 358 359void NativeProcessProtocol::SetState(lldb::StateType state, 360 bool notify_delegates) { 361 std::lock_guard<std::recursive_mutex> guard(m_state_mutex); 362 363 if (state == m_state) 364 return; 365 366 m_state = state; 367 368 if (StateIsStoppedState(state, false)) { 369 ++m_stop_id; 370 371 // Give process a chance to do any stop id bump processing, such as 372 // clearing cached data that is invalidated each time the process runs. 373 // Note if/when we support some threads running, we'll end up needing 374 // to manage this per thread and per process. 375 DoStopIDBumped(m_stop_id); 376 } 377 378 // Optionally notify delegates of the state change. 379 if (notify_delegates) 380 SynchronouslyNotifyProcessStateChanged(state); 381} 382 383uint32_t NativeProcessProtocol::GetStopID() const { 384 std::lock_guard<std::recursive_mutex> guard(m_state_mutex); 385 return m_stop_id; 386} 387 388void NativeProcessProtocol::DoStopIDBumped(uint32_t /* newBumpId */) { 389 // Default implementation does nothing. 390} 391 392Error NativeProcessProtocol::ResolveProcessArchitecture(lldb::pid_t pid, 393 ArchSpec &arch) { 394 // Grab process info for the running process. 395 ProcessInstanceInfo process_info; 396 if (!Host::GetProcessInfo(pid, process_info)) 397 return Error("failed to get process info"); 398 399 // Resolve the executable module. 400 ModuleSpecList module_specs; 401 if (!ObjectFile::GetModuleSpecifications(process_info.GetExecutableFile(), 0, 402 0, module_specs)) 403 return Error("failed to get module specifications"); 404 lldbassert(module_specs.GetSize() == 1); 405 406 arch = module_specs.GetModuleSpecRefAtIndex(0).GetArchitecture(); 407 if (arch.IsValid()) 408 return Error(); 409 else 410 return Error("failed to retrieve a valid architecture from the exe module"); 411} 412 413#ifndef __linux__ 414// These need to be implemented to support lldb-gdb-server on a given platform. 415// Stubs are 416// provided to make the rest of the code link on non-supported platforms. 417 418Error NativeProcessProtocol::Launch(ProcessLaunchInfo &launch_info, 419 NativeDelegate &native_delegate, 420 MainLoop &mainloop, 421 NativeProcessProtocolSP &process_sp) { 422 llvm_unreachable("Platform has no NativeProcessProtocol support"); 423} 424 425Error NativeProcessProtocol::Attach(lldb::pid_t pid, 426 NativeDelegate &native_delegate, 427 MainLoop &mainloop, 428 NativeProcessProtocolSP &process_sp) { 429 llvm_unreachable("Platform has no NativeProcessProtocol support"); 430} 431 432#endif 433