NativeProcessProtocol.cpp revision 288943
1239922Sgonzo//===-- NativeProcessProtocol.cpp -------------------------------*- C++ -*-===// 2239922Sgonzo// 3239922Sgonzo// The LLVM Compiler Infrastructure 4239922Sgonzo// 5239922Sgonzo// This file is distributed under the University of Illinois Open Source 6239922Sgonzo// License. See LICENSE.TXT for details. 7239922Sgonzo// 8239922Sgonzo//===----------------------------------------------------------------------===// 9239922Sgonzo 10239922Sgonzo#include "lldb/Host/common/NativeProcessProtocol.h" 11239922Sgonzo 12239922Sgonzo#include "lldb/lldb-enumerations.h" 13239922Sgonzo#include "lldb/Core/ArchSpec.h" 14239922Sgonzo#include "lldb/Core/Log.h" 15239922Sgonzo#include "lldb/Core/State.h" 16239922Sgonzo#include "lldb/Host/Host.h" 17239922Sgonzo#include "lldb/Host/common/NativeRegisterContext.h" 18239922Sgonzo 19239922Sgonzo#include "lldb/Host/common/NativeThreadProtocol.h" 20239922Sgonzo#include "lldb/Host/common/SoftwareBreakpoint.h" 21239922Sgonzo 22253355Srpaulousing namespace lldb; 23239922Sgonzousing namespace lldb_private; 24266328Sian 25239922Sgonzo// ----------------------------------------------------------------------------- 26239922Sgonzo// NativeProcessProtocol Members 27266328Sian// ----------------------------------------------------------------------------- 28266328Sian 29266328SianNativeProcessProtocol::NativeProcessProtocol (lldb::pid_t pid) : 30266331Sian m_pid (pid), 31266328Sian m_threads (), 32266328Sian m_current_thread_id (LLDB_INVALID_THREAD_ID), 33266328Sian m_threads_mutex (Mutex::eMutexTypeRecursive), 34266328Sian m_state (lldb::eStateInvalid), 35266328Sian m_state_mutex (Mutex::eMutexTypeRecursive), 36266328Sian m_exit_type (eExitTypeInvalid), 37266331Sian m_exit_status (0), 38266328Sian m_exit_description (), 39239922Sgonzo m_delegates_mutex (Mutex::eMutexTypeRecursive), 40239922Sgonzo m_delegates (), 41266328Sian m_breakpoint_list (), 42243701Sgonzo m_watchpoint_list (), 43266328Sian m_terminal_fd (-1), 44240572Sjmg m_stop_id (0) 45240572Sjmg{ 46240572Sjmg} 47240572Sjmg 48240572Sjmglldb_private::Error 49239922SgonzoNativeProcessProtocol::Interrupt () 50266328Sian{ 51266328Sian Error error; 52266328Sian#if !defined (SIGSTOP) 53266328Sian error.SetErrorString ("local host does not support signaling"); 54266328Sian return error; 55266328Sian#else 56266328Sian return Signal (SIGSTOP); 57266328Sian#endif 58239922Sgonzo} 59242321Sgonzo 60239922Sgonzolldb_private::Error 61239922SgonzoNativeProcessProtocol::GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) 62239922Sgonzo{ 63239922Sgonzo // Default: not implemented. 64239922Sgonzo return Error ("not implemented"); 65239922Sgonzo} 66239922Sgonzo 67239922Sgonzobool 68239922SgonzoNativeProcessProtocol::GetExitStatus (ExitType *exit_type, int *status, std::string &exit_description) 69239922Sgonzo{ 70239922Sgonzo if (m_state == lldb::eStateExited) 71243701Sgonzo { 72266022Sian *exit_type = m_exit_type; 73243701Sgonzo *status = m_exit_status; 74266328Sian exit_description = m_exit_description; 75243701Sgonzo return true; 76243701Sgonzo } 77239922Sgonzo 78242321Sgonzo *status = 0; 79242321Sgonzo return false; 80242321Sgonzo} 81242321Sgonzo 82243465Sgonzobool 83243465SgonzoNativeProcessProtocol::SetExitStatus (ExitType exit_type, int status, const char *exit_description, bool bNotifyStateChange) 84243465Sgonzo{ 85261078Sloos Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 86261078Sloos if (log) 87261078Sloos log->Printf ("NativeProcessProtocol::%s(%d, %d, %s, %s) called", 88261078Sloos __FUNCTION__, 89261078Sloos exit_type, 90239922Sgonzo status, 91266328Sian exit_description ? exit_description : "nullptr", 92271428Sian bNotifyStateChange ? "true" : "false"); 93271428Sian 94239922Sgonzo // Exit status already set 95239922Sgonzo if (m_state == lldb::eStateExited) 96266328Sian { 97239922Sgonzo if (log) 98240483Shselasky log->Printf ("NativeProcessProtocol::%s exit status already set to %d, ignoring new set to %d", __FUNCTION__, m_exit_status, status); 99240483Shselasky return false; 100240483Shselasky } 101266328Sian 102239922Sgonzo m_state = lldb::eStateExited; 103240483Shselasky 104240483Shselasky m_exit_type = exit_type; 105240483Shselasky m_exit_status = status; 106240483Shselasky if (exit_description && exit_description[0]) 107239922Sgonzo m_exit_description = exit_description; 108240483Shselasky else 109240483Shselasky m_exit_description.clear(); 110240483Shselasky 111240483Shselasky if (bNotifyStateChange) 112239922Sgonzo SynchronouslyNotifyProcessStateChanged (lldb::eStateExited); 113259325Sian 114259325Sian return true; 115259325Sian} 116259325Sian 117239922SgonzoNativeThreadProtocolSP 118239922SgonzoNativeProcessProtocol::GetThreadAtIndex (uint32_t idx) 119247304Skientzle{ 120247304Skientzle Mutex::Locker locker (m_threads_mutex); 121247304Skientzle if (idx < m_threads.size ()) 122252440Srpaulo return m_threads[idx]; 123252522Sandrew return NativeThreadProtocolSP (); 124266328Sian} 125 126NativeThreadProtocolSP 127NativeProcessProtocol::GetThreadByIDUnlocked (lldb::tid_t tid) 128{ 129 for (auto thread_sp : m_threads) 130 { 131 if (thread_sp->GetID() == tid) 132 return thread_sp; 133 } 134 return NativeThreadProtocolSP (); 135} 136 137NativeThreadProtocolSP 138NativeProcessProtocol::GetThreadByID (lldb::tid_t tid) 139{ 140 Mutex::Locker locker (m_threads_mutex); 141 return GetThreadByIDUnlocked (tid); 142} 143 144bool 145NativeProcessProtocol::IsAlive () const 146{ 147 return m_state != eStateDetached 148 && m_state != eStateExited 149 && m_state != eStateInvalid 150 && m_state != eStateUnloaded; 151} 152 153bool 154NativeProcessProtocol::GetByteOrder (lldb::ByteOrder &byte_order) const 155{ 156 ArchSpec process_arch; 157 if (!GetArchitecture (process_arch)) 158 return false; 159 byte_order = process_arch.GetByteOrder (); 160 return true; 161} 162 163const NativeWatchpointList::WatchpointMap& 164NativeProcessProtocol::GetWatchpointMap () const 165{ 166 return m_watchpoint_list.GetWatchpointMap(); 167} 168 169uint32_t 170NativeProcessProtocol::GetMaxWatchpoints () const 171{ 172 // This default implementation will return the number of 173 // *hardware* breakpoints available. MacOSX and other OS 174 // implementations that support software breakpoints will want to 175 // override this correctly for their implementation. 176 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 177 178 // get any thread 179 NativeThreadProtocolSP thread_sp (const_cast<NativeProcessProtocol*> (this)->GetThreadAtIndex (0)); 180 if (!thread_sp) 181 { 182 if (log) 183 log->Warning ("NativeProcessProtocol::%s (): failed to find a thread to grab a NativeRegisterContext!", __FUNCTION__); 184 return 0; 185 } 186 187 NativeRegisterContextSP reg_ctx_sp (thread_sp->GetRegisterContext ()); 188 if (!reg_ctx_sp) 189 { 190 if (log) 191 log->Warning ("NativeProcessProtocol::%s (): failed to get a RegisterContextNativeProcess from the first thread!", __FUNCTION__); 192 return 0; 193 } 194 195 return reg_ctx_sp->NumSupportedHardwareWatchpoints (); 196} 197 198Error 199NativeProcessProtocol::SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware) 200{ 201 // This default implementation assumes setting the watchpoint for 202 // the process will require setting the watchpoint for each of the 203 // threads. Furthermore, it will track watchpoints set for the 204 // process and will add them to each thread that is attached to 205 // via the (FIXME implement) OnThreadAttached () method. 206 207 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 208 209 // Update the thread list 210 UpdateThreads (); 211 212 // Keep track of the threads we successfully set the watchpoint 213 // for. If one of the thread watchpoint setting operations fails, 214 // back off and remove the watchpoint for all the threads that 215 // were successfully set so we get back to a consistent state. 216 std::vector<NativeThreadProtocolSP> watchpoint_established_threads; 217 218 // Tell each thread to set a watchpoint. In the event that 219 // hardware watchpoints are requested but the SetWatchpoint fails, 220 // try to set a software watchpoint as a fallback. It's 221 // conceivable that if there are more threads than hardware 222 // watchpoints available, some of the threads will fail to set 223 // hardware watchpoints while software ones may be available. 224 Mutex::Locker locker (m_threads_mutex); 225 for (auto thread_sp : m_threads) 226 { 227 assert (thread_sp && "thread list should not have a NULL thread!"); 228 if (!thread_sp) 229 continue; 230 231 Error thread_error = thread_sp->SetWatchpoint (addr, size, watch_flags, hardware); 232 if (thread_error.Fail () && hardware) 233 { 234 // Try software watchpoints since we failed on hardware watchpoint setting 235 // and we may have just run out of hardware watchpoints. 236 thread_error = thread_sp->SetWatchpoint (addr, size, watch_flags, false); 237 if (thread_error.Success ()) 238 { 239 if (log) 240 log->Warning ("hardware watchpoint requested but software watchpoint set"); 241 } 242 } 243 244 if (thread_error.Success ()) 245 { 246 // Remember that we set this watchpoint successfully in 247 // case we need to clear it later. 248 watchpoint_established_threads.push_back (thread_sp); 249 } 250 else 251 { 252 // Unset the watchpoint for each thread we successfully 253 // set so that we get back to a consistent state of "not 254 // set" for the watchpoint. 255 for (auto unwatch_thread_sp : watchpoint_established_threads) 256 { 257 Error remove_error = unwatch_thread_sp->RemoveWatchpoint (addr); 258 if (remove_error.Fail () && log) 259 { 260 log->Warning ("NativeProcessProtocol::%s (): RemoveWatchpoint failed for pid=%" PRIu64 ", tid=%" PRIu64 ": %s", 261 __FUNCTION__, GetID (), unwatch_thread_sp->GetID (), remove_error.AsCString ()); 262 } 263 } 264 265 return thread_error; 266 } 267 } 268 return m_watchpoint_list.Add (addr, size, watch_flags, hardware); 269} 270 271Error 272NativeProcessProtocol::RemoveWatchpoint (lldb::addr_t addr) 273{ 274 // Update the thread list 275 UpdateThreads (); 276 277 Error overall_error; 278 279 Mutex::Locker locker (m_threads_mutex); 280 for (auto thread_sp : m_threads) 281 { 282 assert (thread_sp && "thread list should not have a NULL thread!"); 283 if (!thread_sp) 284 continue; 285 286 const Error thread_error = thread_sp->RemoveWatchpoint (addr); 287 if (thread_error.Fail ()) 288 { 289 // Keep track of the first thread error if any threads 290 // fail. We want to try to remove the watchpoint from 291 // every thread, though, even if one or more have errors. 292 if (!overall_error.Fail ()) 293 overall_error = thread_error; 294 } 295 } 296 const Error error = m_watchpoint_list.Remove(addr); 297 return overall_error.Fail() ? overall_error : error; 298} 299 300bool 301NativeProcessProtocol::RegisterNativeDelegate (NativeDelegate &native_delegate) 302{ 303 Mutex::Locker locker (m_delegates_mutex); 304 if (std::find (m_delegates.begin (), m_delegates.end (), &native_delegate) != m_delegates.end ()) 305 return false; 306 307 m_delegates.push_back (&native_delegate); 308 native_delegate.InitializeDelegate (this); 309 return true; 310} 311 312bool 313NativeProcessProtocol::UnregisterNativeDelegate (NativeDelegate &native_delegate) 314{ 315 Mutex::Locker locker (m_delegates_mutex); 316 317 const auto initial_size = m_delegates.size (); 318 m_delegates.erase (remove (m_delegates.begin (), m_delegates.end (), &native_delegate), m_delegates.end ()); 319 320 // We removed the delegate if the count of delegates shrank after 321 // removing all copies of the given native_delegate from the vector. 322 return m_delegates.size () < initial_size; 323} 324 325void 326NativeProcessProtocol::SynchronouslyNotifyProcessStateChanged (lldb::StateType state) 327{ 328 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 329 330 Mutex::Locker locker (m_delegates_mutex); 331 for (auto native_delegate: m_delegates) 332 native_delegate->ProcessStateChanged (this, state); 333 334 if (log) 335 { 336 if (!m_delegates.empty ()) 337 { 338 log->Printf ("NativeProcessProtocol::%s: sent state notification [%s] from process %" PRIu64, 339 __FUNCTION__, lldb_private::StateAsCString (state), GetID ()); 340 } 341 else 342 { 343 log->Printf ("NativeProcessProtocol::%s: would send state notification [%s] from process %" PRIu64 ", but no delegates", 344 __FUNCTION__, lldb_private::StateAsCString (state), GetID ()); 345 } 346 } 347} 348 349void 350NativeProcessProtocol::NotifyDidExec () 351{ 352 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 353 if (log) 354 log->Printf ("NativeProcessProtocol::%s - preparing to call delegates", __FUNCTION__); 355 356 { 357 Mutex::Locker locker (m_delegates_mutex); 358 for (auto native_delegate: m_delegates) 359 native_delegate->DidExec (this); 360 } 361} 362 363 364Error 365NativeProcessProtocol::SetSoftwareBreakpoint (lldb::addr_t addr, uint32_t size_hint) 366{ 367 Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 368 if (log) 369 log->Printf ("NativeProcessProtocol::%s addr = 0x%" PRIx64, __FUNCTION__, addr); 370 371 return m_breakpoint_list.AddRef (addr, size_hint, false, 372 [this] (lldb::addr_t addr, size_t size_hint, bool /* hardware */, NativeBreakpointSP &breakpoint_sp)->Error 373 { return SoftwareBreakpoint::CreateSoftwareBreakpoint (*this, addr, size_hint, breakpoint_sp); }); 374} 375 376Error 377NativeProcessProtocol::RemoveBreakpoint (lldb::addr_t addr) 378{ 379 return m_breakpoint_list.DecRef (addr); 380} 381 382Error 383NativeProcessProtocol::EnableBreakpoint (lldb::addr_t addr) 384{ 385 return m_breakpoint_list.EnableBreakpoint (addr); 386} 387 388Error 389NativeProcessProtocol::DisableBreakpoint (lldb::addr_t addr) 390{ 391 return m_breakpoint_list.DisableBreakpoint (addr); 392} 393 394lldb::StateType 395NativeProcessProtocol::GetState () const 396{ 397 Mutex::Locker locker (m_state_mutex); 398 return m_state; 399} 400 401void 402NativeProcessProtocol::SetState (lldb::StateType state, bool notify_delegates) 403{ 404 Mutex::Locker locker (m_state_mutex); 405 406 if (state == m_state) 407 return; 408 409 m_state = state; 410 411 if (StateIsStoppedState (state, false)) 412 { 413 ++m_stop_id; 414 415 // Give process a chance to do any stop id bump processing, such as 416 // clearing cached data that is invalidated each time the process runs. 417 // Note if/when we support some threads running, we'll end up needing 418 // to manage this per thread and per process. 419 DoStopIDBumped (m_stop_id); 420 } 421 422 // Optionally notify delegates of the state change. 423 if (notify_delegates) 424 SynchronouslyNotifyProcessStateChanged (state); 425} 426 427uint32_t NativeProcessProtocol::GetStopID () const 428{ 429 Mutex::Locker locker (m_state_mutex); 430 return m_stop_id; 431} 432 433void 434NativeProcessProtocol::DoStopIDBumped (uint32_t /* newBumpId */) 435{ 436 // Default implementation does nothing. 437} 438 439void 440NativeProcessProtocol::Terminate () 441{ 442 // Default implementation does nothing. 443} 444 445#ifndef __linux__ 446// These need to be implemented to support lldb-gdb-server on a given platform. Stubs are 447// provided to make the rest of the code link on non-supported platforms. 448 449Error 450NativeProcessProtocol::Launch (ProcessLaunchInfo &launch_info, 451 NativeDelegate &native_delegate, 452 NativeProcessProtocolSP &process_sp) 453{ 454 llvm_unreachable("Platform has no NativeProcessProtocol support"); 455} 456 457Error 458NativeProcessProtocol::Attach (lldb::pid_t pid, 459 NativeDelegate &native_delegate, 460 NativeProcessProtocolSP &process_sp) 461{ 462 llvm_unreachable("Platform has no NativeProcessProtocol support"); 463} 464 465#endif 466