1af245d11STodd Fiala //===-- NativeThreadLinux.cpp --------------------------------- -*- C++ -*-===// 2af245d11STodd Fiala // 3af245d11STodd Fiala // The LLVM Compiler Infrastructure 4af245d11STodd Fiala // 5af245d11STodd Fiala // This file is distributed under the University of Illinois Open Source 6af245d11STodd Fiala // License. See LICENSE.TXT for details. 7af245d11STodd Fiala // 8af245d11STodd Fiala //===----------------------------------------------------------------------===// 9af245d11STodd Fiala 10af245d11STodd Fiala #include "NativeThreadLinux.h" 11af245d11STodd Fiala 12af245d11STodd Fiala #include <signal.h> 13af245d11STodd Fiala 14af245d11STodd Fiala #include "NativeProcessLinux.h" 152850b1beSTodd Fiala #include "NativeRegisterContextLinux_x86_64.h" 162850b1beSTodd Fiala 17af245d11STodd Fiala #include "lldb/Core/Log.h" 18af245d11STodd Fiala #include "lldb/Core/State.h" 19af245d11STodd Fiala #include "lldb/Host/Host.h" 2013b18261SZachary Turner #include "lldb/Host/HostInfo.h" 2139de3110SZachary Turner #include "lldb/Host/HostNativeThread.h" 22af245d11STodd Fiala #include "lldb/lldb-enumerations.h" 23af245d11STodd Fiala #include "lldb/lldb-private-log.h" 2439de3110SZachary Turner 2539de3110SZachary Turner #include "llvm/ADT/SmallString.h" 2639de3110SZachary Turner 27b71e89e9STodd Fiala #include "Plugins/Process/Utility/RegisterContextLinux_arm64.h" 28af245d11STodd Fiala #include "Plugins/Process/Utility/RegisterContextLinux_i386.h" 29af245d11STodd Fiala #include "Plugins/Process/Utility/RegisterContextLinux_x86_64.h" 30af245d11STodd Fiala #include "Plugins/Process/Utility/RegisterInfoInterface.h" 31af245d11STodd Fiala 32af245d11STodd Fiala using namespace lldb; 33af245d11STodd Fiala using namespace lldb_private; 34af245d11STodd Fiala 35af245d11STodd Fiala namespace 36af245d11STodd Fiala { 37af245d11STodd Fiala void LogThreadStopInfo (Log &log, const ThreadStopInfo &stop_info, const char *const header) 38af245d11STodd Fiala { 39af245d11STodd Fiala switch (stop_info.reason) 40af245d11STodd Fiala { 41af245d11STodd Fiala case eStopReasonSignal: 42*ae29d395SChaoren Lin log.Printf ("%s: %s signal 0x%02" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); 43af245d11STodd Fiala return; 44af245d11STodd Fiala case eStopReasonException: 45*ae29d395SChaoren Lin log.Printf ("%s: %s exception type 0x%02" PRIx64, __FUNCTION__, header, stop_info.details.exception.type); 46a9882ceeSTodd Fiala return; 47a9882ceeSTodd Fiala case eStopReasonExec: 48a9882ceeSTodd Fiala log.Printf ("%s: %s exec, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); 49af245d11STodd Fiala return; 50af245d11STodd Fiala default: 51a9882ceeSTodd Fiala log.Printf ("%s: %s invalid stop reason %" PRIu32, __FUNCTION__, header, static_cast<uint32_t> (stop_info.reason)); 52af245d11STodd Fiala } 53af245d11STodd Fiala } 54af245d11STodd Fiala } 55af245d11STodd Fiala 56af245d11STodd Fiala NativeThreadLinux::NativeThreadLinux (NativeProcessLinux *process, lldb::tid_t tid) : 57af245d11STodd Fiala NativeThreadProtocol (process, tid), 58af245d11STodd Fiala m_state (StateType::eStateInvalid), 59af245d11STodd Fiala m_stop_info (), 60af245d11STodd Fiala m_reg_context_sp () 61af245d11STodd Fiala { 62af245d11STodd Fiala } 63af245d11STodd Fiala 647206c6d1STodd Fiala std::string 65af245d11STodd Fiala NativeThreadLinux::GetName() 66af245d11STodd Fiala { 67af245d11STodd Fiala NativeProcessProtocolSP process_sp = m_process_wp.lock (); 68af245d11STodd Fiala if (!process_sp) 69af245d11STodd Fiala return "<unknown: no process>"; 70af245d11STodd Fiala 71af245d11STodd Fiala // const NativeProcessLinux *const process = reinterpret_cast<NativeProcessLinux*> (process_sp->get ()); 7239de3110SZachary Turner llvm::SmallString<32> thread_name; 7339de3110SZachary Turner HostNativeThread::GetName(GetID(), thread_name); 7439de3110SZachary Turner return thread_name.c_str(); 75af245d11STodd Fiala } 76af245d11STodd Fiala 77af245d11STodd Fiala lldb::StateType 78af245d11STodd Fiala NativeThreadLinux::GetState () 79af245d11STodd Fiala { 80af245d11STodd Fiala return m_state; 81af245d11STodd Fiala } 82af245d11STodd Fiala 83af245d11STodd Fiala 84af245d11STodd Fiala bool 85af245d11STodd Fiala NativeThreadLinux::GetStopReason (ThreadStopInfo &stop_info) 86af245d11STodd Fiala { 87af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 88af245d11STodd Fiala switch (m_state) 89af245d11STodd Fiala { 90af245d11STodd Fiala case eStateStopped: 91af245d11STodd Fiala case eStateCrashed: 92af245d11STodd Fiala case eStateExited: 93af245d11STodd Fiala case eStateSuspended: 94af245d11STodd Fiala case eStateUnloaded: 95af245d11STodd Fiala if (log) 96af245d11STodd Fiala LogThreadStopInfo (*log, m_stop_info, "m_stop_info in thread:"); 97af245d11STodd Fiala stop_info = m_stop_info; 98af245d11STodd Fiala if (log) 99af245d11STodd Fiala LogThreadStopInfo (*log, stop_info, "returned stop_info:"); 100af245d11STodd Fiala return true; 101af245d11STodd Fiala 102af245d11STodd Fiala case eStateInvalid: 103af245d11STodd Fiala case eStateConnected: 104af245d11STodd Fiala case eStateAttaching: 105af245d11STodd Fiala case eStateLaunching: 106af245d11STodd Fiala case eStateRunning: 107af245d11STodd Fiala case eStateStepping: 108af245d11STodd Fiala case eStateDetached: 109af245d11STodd Fiala if (log) 110af245d11STodd Fiala { 111af245d11STodd Fiala log->Printf ("NativeThreadLinux::%s tid %" PRIu64 " in state %s cannot answer stop reason", 112af245d11STodd Fiala __FUNCTION__, GetID (), StateAsCString (m_state)); 113af245d11STodd Fiala } 114af245d11STodd Fiala return false; 115af245d11STodd Fiala } 1168faf9370SDavid Majnemer llvm_unreachable("unhandled StateType!"); 117af245d11STodd Fiala } 118af245d11STodd Fiala 119af245d11STodd Fiala lldb_private::NativeRegisterContextSP 120af245d11STodd Fiala NativeThreadLinux::GetRegisterContext () 121af245d11STodd Fiala { 122af245d11STodd Fiala // Return the register context if we already created it. 123af245d11STodd Fiala if (m_reg_context_sp) 124af245d11STodd Fiala return m_reg_context_sp; 125af245d11STodd Fiala 126af245d11STodd Fiala // First select the appropriate RegisterInfoInterface. 127af245d11STodd Fiala RegisterInfoInterface *reg_interface = nullptr; 128af245d11STodd Fiala NativeProcessProtocolSP m_process_sp = m_process_wp.lock (); 129af245d11STodd Fiala if (!m_process_sp) 130af245d11STodd Fiala return NativeRegisterContextSP (); 131af245d11STodd Fiala 132af245d11STodd Fiala ArchSpec target_arch; 133af245d11STodd Fiala if (!m_process_sp->GetArchitecture (target_arch)) 134af245d11STodd Fiala return NativeRegisterContextSP (); 135af245d11STodd Fiala 136af245d11STodd Fiala switch (target_arch.GetTriple().getOS()) 137af245d11STodd Fiala { 138af245d11STodd Fiala case llvm::Triple::Linux: 139af245d11STodd Fiala switch (target_arch.GetMachine()) 140af245d11STodd Fiala { 141b71e89e9STodd Fiala case llvm::Triple::aarch64: 142b71e89e9STodd Fiala assert((HostInfo::GetArchitecture ().GetAddressByteSize() == 8) && "Register setting path assumes this is a 64-bit host"); 143b71e89e9STodd Fiala reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_arm64(target_arch)); 144b71e89e9STodd Fiala break; 145af245d11STodd Fiala case llvm::Triple::x86: 146af245d11STodd Fiala case llvm::Triple::x86_64: 14713b18261SZachary Turner if (HostInfo::GetArchitecture().GetAddressByteSize() == 4) 148af245d11STodd Fiala { 149af245d11STodd Fiala // 32-bit hosts run with a RegisterContextLinux_i386 context. 150af245d11STodd Fiala reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_i386(target_arch)); 151af245d11STodd Fiala } 152af245d11STodd Fiala else 153af245d11STodd Fiala { 15413b18261SZachary Turner assert((HostInfo::GetArchitecture().GetAddressByteSize() == 8) && 15513b18261SZachary Turner "Register setting path assumes this is a 64-bit host"); 156af245d11STodd Fiala // X86_64 hosts know how to work with 64-bit and 32-bit EXEs using the x86_64 register context. 157af245d11STodd Fiala reg_interface = static_cast<RegisterInfoInterface*> (new RegisterContextLinux_x86_64 (target_arch)); 158af245d11STodd Fiala } 159af245d11STodd Fiala break; 160af245d11STodd Fiala default: 161af245d11STodd Fiala break; 162af245d11STodd Fiala } 163af245d11STodd Fiala break; 164af245d11STodd Fiala default: 165af245d11STodd Fiala break; 166af245d11STodd Fiala } 167af245d11STodd Fiala 168af245d11STodd Fiala assert(reg_interface && "OS or CPU not supported!"); 169af245d11STodd Fiala if (!reg_interface) 170af245d11STodd Fiala return NativeRegisterContextSP (); 171af245d11STodd Fiala 172af245d11STodd Fiala // Now create the register context. 173af245d11STodd Fiala switch (target_arch.GetMachine()) 174af245d11STodd Fiala { 175af245d11STodd Fiala #if 0 176af245d11STodd Fiala case llvm::Triple::mips64: 177af245d11STodd Fiala { 178af245d11STodd Fiala RegisterContextPOSIXProcessMonitor_mips64 *reg_ctx = new RegisterContextPOSIXProcessMonitor_mips64(*this, 0, reg_interface); 179af245d11STodd Fiala m_posix_thread = reg_ctx; 180af245d11STodd Fiala m_reg_context_sp.reset(reg_ctx); 181af245d11STodd Fiala break; 182af245d11STodd Fiala } 183af245d11STodd Fiala #endif 184af245d11STodd Fiala #if 0 185af245d11STodd Fiala case llvm::Triple::x86: 186af245d11STodd Fiala #endif 187af245d11STodd Fiala case llvm::Triple::x86_64: 188af245d11STodd Fiala { 189af245d11STodd Fiala const uint32_t concrete_frame_idx = 0; 190af245d11STodd Fiala m_reg_context_sp.reset (new NativeRegisterContextLinux_x86_64(*this, concrete_frame_idx, reg_interface)); 191af245d11STodd Fiala break; 192af245d11STodd Fiala } 193af245d11STodd Fiala default: 194af245d11STodd Fiala break; 195af245d11STodd Fiala } 196af245d11STodd Fiala 197af245d11STodd Fiala return m_reg_context_sp; 198af245d11STodd Fiala } 199af245d11STodd Fiala 200af245d11STodd Fiala Error 201af245d11STodd Fiala NativeThreadLinux::SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware) 202af245d11STodd Fiala { 203af245d11STodd Fiala // TODO implement 204af245d11STodd Fiala return Error ("not implemented"); 205af245d11STodd Fiala } 206af245d11STodd Fiala 207af245d11STodd Fiala Error 208af245d11STodd Fiala NativeThreadLinux::RemoveWatchpoint (lldb::addr_t addr) 209af245d11STodd Fiala { 210af245d11STodd Fiala // TODO implement 211af245d11STodd Fiala return Error ("not implemented"); 212af245d11STodd Fiala } 213af245d11STodd Fiala 214af245d11STodd Fiala void 215af245d11STodd Fiala NativeThreadLinux::SetLaunching () 216af245d11STodd Fiala { 217af245d11STodd Fiala const StateType new_state = StateType::eStateLaunching; 218af245d11STodd Fiala MaybeLogStateChange (new_state); 219af245d11STodd Fiala m_state = new_state; 220af245d11STodd Fiala 221af245d11STodd Fiala // Also mark it as stopped since launching temporarily stops the newly created thread 222af245d11STodd Fiala // in the ptrace machinery. 223af245d11STodd Fiala m_stop_info.reason = StopReason::eStopReasonSignal; 224af245d11STodd Fiala m_stop_info.details.signal.signo = SIGSTOP; 225af245d11STodd Fiala } 226af245d11STodd Fiala 227af245d11STodd Fiala 228af245d11STodd Fiala void 229af245d11STodd Fiala NativeThreadLinux::SetRunning () 230af245d11STodd Fiala { 231af245d11STodd Fiala const StateType new_state = StateType::eStateRunning; 232af245d11STodd Fiala MaybeLogStateChange (new_state); 233af245d11STodd Fiala m_state = new_state; 234af245d11STodd Fiala 235af245d11STodd Fiala m_stop_info.reason = StopReason::eStopReasonNone; 236af245d11STodd Fiala } 237af245d11STodd Fiala 238af245d11STodd Fiala void 239af245d11STodd Fiala NativeThreadLinux::SetStepping () 240af245d11STodd Fiala { 241af245d11STodd Fiala const StateType new_state = StateType::eStateStepping; 242af245d11STodd Fiala MaybeLogStateChange (new_state); 243af245d11STodd Fiala m_state = new_state; 244af245d11STodd Fiala 245af245d11STodd Fiala m_stop_info.reason = StopReason::eStopReasonNone; 246af245d11STodd Fiala } 247af245d11STodd Fiala 248af245d11STodd Fiala void 249af245d11STodd Fiala NativeThreadLinux::SetStoppedBySignal (uint32_t signo) 250af245d11STodd Fiala { 251af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 252af245d11STodd Fiala if (log) 253af245d11STodd Fiala log->Printf ("NativeThreadLinux::%s called with signal 0x%" PRIx32, __FUNCTION__, signo); 254af245d11STodd Fiala 255af245d11STodd Fiala const StateType new_state = StateType::eStateStopped; 256af245d11STodd Fiala MaybeLogStateChange (new_state); 257af245d11STodd Fiala m_state = new_state; 258af245d11STodd Fiala 259af245d11STodd Fiala m_stop_info.reason = StopReason::eStopReasonSignal; 260af245d11STodd Fiala m_stop_info.details.signal.signo = signo; 261af245d11STodd Fiala } 262af245d11STodd Fiala 263511e5cdcSTodd Fiala bool 264511e5cdcSTodd Fiala NativeThreadLinux::IsStopped (int *signo) 265511e5cdcSTodd Fiala { 266511e5cdcSTodd Fiala if (!StateIsStoppedState (m_state, false)) 267511e5cdcSTodd Fiala return false; 268511e5cdcSTodd Fiala 269511e5cdcSTodd Fiala // If we are stopped by a signal, return the signo. 270511e5cdcSTodd Fiala if (signo && 271511e5cdcSTodd Fiala m_state == StateType::eStateStopped && 272511e5cdcSTodd Fiala m_stop_info.reason == StopReason::eStopReasonSignal) 273511e5cdcSTodd Fiala { 274511e5cdcSTodd Fiala *signo = m_stop_info.details.signal.signo; 275511e5cdcSTodd Fiala } 276511e5cdcSTodd Fiala 277511e5cdcSTodd Fiala // Regardless, we are stopped. 278511e5cdcSTodd Fiala return true; 279511e5cdcSTodd Fiala } 280511e5cdcSTodd Fiala 281511e5cdcSTodd Fiala 282af245d11STodd Fiala void 283a9882ceeSTodd Fiala NativeThreadLinux::SetStoppedByExec () 284a9882ceeSTodd Fiala { 285a9882ceeSTodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 286a9882ceeSTodd Fiala if (log) 287a9882ceeSTodd Fiala log->Printf ("NativeThreadLinux::%s()", __FUNCTION__); 288a9882ceeSTodd Fiala 289a9882ceeSTodd Fiala const StateType new_state = StateType::eStateStopped; 290a9882ceeSTodd Fiala MaybeLogStateChange (new_state); 291a9882ceeSTodd Fiala m_state = new_state; 292a9882ceeSTodd Fiala 293a9882ceeSTodd Fiala m_stop_info.reason = StopReason::eStopReasonExec; 294a9882ceeSTodd Fiala m_stop_info.details.signal.signo = SIGSTOP; 295a9882ceeSTodd Fiala } 296a9882ceeSTodd Fiala 297a9882ceeSTodd Fiala void 298af245d11STodd Fiala NativeThreadLinux::SetStoppedByBreakpoint () 299af245d11STodd Fiala { 300af245d11STodd Fiala const StateType new_state = StateType::eStateStopped; 301af245d11STodd Fiala MaybeLogStateChange (new_state); 302af245d11STodd Fiala m_state = new_state; 303af245d11STodd Fiala 304af245d11STodd Fiala m_stop_info.reason = StopReason::eStopReasonSignal; 305af245d11STodd Fiala m_stop_info.details.signal.signo = SIGTRAP; 306af245d11STodd Fiala } 307af245d11STodd Fiala 308af245d11STodd Fiala bool 309af245d11STodd Fiala NativeThreadLinux::IsStoppedAtBreakpoint () 310af245d11STodd Fiala { 311af245d11STodd Fiala // Are we stopped? If not, this can't be a breakpoint. 312af245d11STodd Fiala if (GetState () != StateType::eStateStopped) 313af245d11STodd Fiala return false; 314af245d11STodd Fiala 315af245d11STodd Fiala // Was the stop reason a signal with signal number SIGTRAP? If not, not a breakpoint. 316af245d11STodd Fiala return (m_stop_info.reason == StopReason::eStopReasonSignal) && 317af245d11STodd Fiala (m_stop_info.details.signal.signo == SIGTRAP); 318af245d11STodd Fiala } 319af245d11STodd Fiala 320af245d11STodd Fiala void 321af245d11STodd Fiala NativeThreadLinux::SetCrashedWithException (uint64_t exception_type, lldb::addr_t exception_addr) 322af245d11STodd Fiala { 323af245d11STodd Fiala const StateType new_state = StateType::eStateCrashed; 324af245d11STodd Fiala MaybeLogStateChange (new_state); 325af245d11STodd Fiala m_state = new_state; 326af245d11STodd Fiala 327af245d11STodd Fiala m_stop_info.reason = StopReason::eStopReasonException; 328af245d11STodd Fiala m_stop_info.details.exception.type = exception_type; 329af245d11STodd Fiala m_stop_info.details.exception.data_count = 1; 330af245d11STodd Fiala m_stop_info.details.exception.data[0] = exception_addr; 331af245d11STodd Fiala } 332af245d11STodd Fiala 333af245d11STodd Fiala 334af245d11STodd Fiala void 335af245d11STodd Fiala NativeThreadLinux::SetSuspended () 336af245d11STodd Fiala { 337af245d11STodd Fiala const StateType new_state = StateType::eStateSuspended; 338af245d11STodd Fiala MaybeLogStateChange (new_state); 339af245d11STodd Fiala m_state = new_state; 340af245d11STodd Fiala 341af245d11STodd Fiala // FIXME what makes sense here? Do we need a suspended StopReason? 342af245d11STodd Fiala m_stop_info.reason = StopReason::eStopReasonNone; 343af245d11STodd Fiala } 344af245d11STodd Fiala 345af245d11STodd Fiala void 346af245d11STodd Fiala NativeThreadLinux::SetExited () 347af245d11STodd Fiala { 348af245d11STodd Fiala const StateType new_state = StateType::eStateExited; 349af245d11STodd Fiala MaybeLogStateChange (new_state); 350af245d11STodd Fiala m_state = new_state; 351af245d11STodd Fiala 352af245d11STodd Fiala m_stop_info.reason = StopReason::eStopReasonThreadExiting; 353af245d11STodd Fiala } 354af245d11STodd Fiala 355af245d11STodd Fiala void 356af245d11STodd Fiala NativeThreadLinux::MaybeLogStateChange (lldb::StateType new_state) 357af245d11STodd Fiala { 358af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 359af245d11STodd Fiala // If we're not logging, we're done. 360af245d11STodd Fiala if (!log) 361af245d11STodd Fiala return; 362af245d11STodd Fiala 363af245d11STodd Fiala // If this is a state change to the same state, we're done. 364af245d11STodd Fiala lldb::StateType old_state = m_state; 365af245d11STodd Fiala if (new_state == old_state) 366af245d11STodd Fiala return; 367af245d11STodd Fiala 368af245d11STodd Fiala NativeProcessProtocolSP m_process_sp = m_process_wp.lock (); 369af245d11STodd Fiala lldb::pid_t pid = m_process_sp ? m_process_sp->GetID () : LLDB_INVALID_PROCESS_ID; 370af245d11STodd Fiala 371af245d11STodd Fiala // Log it. 372af245d11STodd Fiala log->Printf ("NativeThreadLinux: thread (pid=%" PRIu64 ", tid=%" PRIu64 ") changing from state %s to %s", pid, GetID (), StateAsCString (old_state), StateAsCString (new_state)); 373af245d11STodd Fiala } 374af245d11STodd Fiala 375af245d11STodd Fiala uint32_t 376af245d11STodd Fiala NativeThreadLinux::TranslateStopInfoToGdbSignal (const ThreadStopInfo &stop_info) const 377af245d11STodd Fiala { 378af245d11STodd Fiala switch (stop_info.reason) 379af245d11STodd Fiala { 380af245d11STodd Fiala case eStopReasonSignal: 38122972a7cSTodd Fiala // No translation. 38222972a7cSTodd Fiala return stop_info.details.signal.signo; 383af245d11STodd Fiala 384af245d11STodd Fiala case eStopReasonException: 38522972a7cSTodd Fiala { 38622972a7cSTodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 38722972a7cSTodd Fiala // FIXME I think the eStopReasonException is a xnu/Mach exception, which we 38822972a7cSTodd Fiala // shouldn't see on Linux. 38922972a7cSTodd Fiala // No translation. 39022972a7cSTodd Fiala if (log) 39122972a7cSTodd Fiala log->Printf ("NativeThreadLinux::%s saw an exception stop type (signo %" 39222972a7cSTodd Fiala PRIu64 "), not expecting to see exceptions on Linux", 39322972a7cSTodd Fiala __FUNCTION__, 39422972a7cSTodd Fiala stop_info.details.exception.type); 39522972a7cSTodd Fiala return static_cast<uint32_t> (stop_info.details.exception.type); 39622972a7cSTodd Fiala } 397af245d11STodd Fiala 398af245d11STodd Fiala default: 399af245d11STodd Fiala assert (0 && "unexpected stop_info.reason found"); 400af245d11STodd Fiala return 0; 401af245d11STodd Fiala } 402af245d11STodd Fiala } 403af245d11STodd Fiala 404