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> 1318fe6404SChaoren Lin #include <sstream> 14af245d11STodd Fiala 15af245d11STodd Fiala #include "NativeProcessLinux.h" 163f57216cSOmair Javaid #include "NativeRegisterContextLinux_arm.h" 171e209fccSTamas Berghammer #include "NativeRegisterContextLinux_arm64.h" 182850b1beSTodd Fiala #include "NativeRegisterContextLinux_x86_64.h" 193df471c3SMohit K. Bhakkad #include "NativeRegisterContextLinux_mips64.h" 202850b1beSTodd Fiala 21af245d11STodd Fiala #include "lldb/Core/Log.h" 22af245d11STodd Fiala #include "lldb/Core/State.h" 23af245d11STodd Fiala #include "lldb/Host/Host.h" 2413b18261SZachary Turner #include "lldb/Host/HostInfo.h" 2539de3110SZachary Turner #include "lldb/Host/HostNativeThread.h" 26c16f5dcaSChaoren Lin #include "lldb/Utility/LLDBAssert.h" 27af245d11STodd Fiala #include "lldb/lldb-enumerations.h" 2839de3110SZachary Turner 2939de3110SZachary Turner #include "llvm/ADT/SmallString.h" 3039de3110SZachary Turner 3128e57429SChaoren Lin #include "Plugins/Process/POSIX/CrashReason.h" 3228e57429SChaoren Lin 333f57216cSOmair Javaid #include "Plugins/Process/Utility/RegisterContextLinux_arm.h" 34b71e89e9STodd Fiala #include "Plugins/Process/Utility/RegisterContextLinux_arm64.h" 35af245d11STodd Fiala #include "Plugins/Process/Utility/RegisterContextLinux_i386.h" 36af245d11STodd Fiala #include "Plugins/Process/Utility/RegisterContextLinux_x86_64.h" 373df471c3SMohit K. Bhakkad #include "Plugins/Process/Utility/RegisterContextLinux_mips64.h" 38af245d11STodd Fiala #include "Plugins/Process/Utility/RegisterInfoInterface.h" 39af245d11STodd Fiala 40*8c8ff7afSPavel Labath #include <sys/syscall.h> 41*8c8ff7afSPavel Labath // Try to define a macro to encapsulate the tgkill syscall 42*8c8ff7afSPavel Labath #define tgkill(pid, tid, sig) \ 43*8c8ff7afSPavel Labath syscall(SYS_tgkill, static_cast<::pid_t>(pid), static_cast<::pid_t>(tid), sig) 44*8c8ff7afSPavel Labath 45af245d11STodd Fiala using namespace lldb; 46af245d11STodd Fiala using namespace lldb_private; 47db264a6dSTamas Berghammer using namespace lldb_private::process_linux; 48af245d11STodd Fiala 49af245d11STodd Fiala namespace 50af245d11STodd Fiala { 51af245d11STodd Fiala void LogThreadStopInfo (Log &log, const ThreadStopInfo &stop_info, const char *const header) 52af245d11STodd Fiala { 53af245d11STodd Fiala switch (stop_info.reason) 54af245d11STodd Fiala { 5512fd3756SPavel Labath case eStopReasonNone: 5612fd3756SPavel Labath log.Printf ("%s: %s no stop reason", __FUNCTION__, header); 5712fd3756SPavel Labath return; 5812fd3756SPavel Labath case eStopReasonTrace: 5912fd3756SPavel Labath log.Printf ("%s: %s trace, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); 6012fd3756SPavel Labath return; 6112fd3756SPavel Labath case eStopReasonBreakpoint: 6212fd3756SPavel Labath log.Printf ("%s: %s breakpoint, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); 6312fd3756SPavel Labath return; 6412fd3756SPavel Labath case eStopReasonWatchpoint: 6512fd3756SPavel Labath log.Printf ("%s: %s watchpoint, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); 6612fd3756SPavel Labath return; 67af245d11STodd Fiala case eStopReasonSignal: 68ae29d395SChaoren Lin log.Printf ("%s: %s signal 0x%02" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); 69af245d11STodd Fiala return; 70af245d11STodd Fiala case eStopReasonException: 71ae29d395SChaoren Lin log.Printf ("%s: %s exception type 0x%02" PRIx64, __FUNCTION__, header, stop_info.details.exception.type); 72a9882ceeSTodd Fiala return; 73a9882ceeSTodd Fiala case eStopReasonExec: 74a9882ceeSTodd Fiala log.Printf ("%s: %s exec, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); 75af245d11STodd Fiala return; 7612fd3756SPavel Labath case eStopReasonPlanComplete: 7712fd3756SPavel Labath log.Printf ("%s: %s plan complete", __FUNCTION__, header); 7812fd3756SPavel Labath return; 7912fd3756SPavel Labath case eStopReasonThreadExiting: 8012fd3756SPavel Labath log.Printf ("%s: %s thread exiting", __FUNCTION__, header); 8112fd3756SPavel Labath return; 8212fd3756SPavel Labath case eStopReasonInstrumentation: 8312fd3756SPavel Labath log.Printf ("%s: %s instrumentation", __FUNCTION__, header); 8412fd3756SPavel Labath return; 85af245d11STodd Fiala default: 86a9882ceeSTodd Fiala log.Printf ("%s: %s invalid stop reason %" PRIu32, __FUNCTION__, header, static_cast<uint32_t> (stop_info.reason)); 87af245d11STodd Fiala } 88af245d11STodd Fiala } 89af245d11STodd Fiala } 90af245d11STodd Fiala 91af245d11STodd Fiala NativeThreadLinux::NativeThreadLinux (NativeProcessLinux *process, lldb::tid_t tid) : 92af245d11STodd Fiala NativeThreadProtocol (process, tid), 93af245d11STodd Fiala m_state (StateType::eStateInvalid), 94af245d11STodd Fiala m_stop_info (), 9528e57429SChaoren Lin m_reg_context_sp (), 9628e57429SChaoren Lin m_stop_description () 97af245d11STodd Fiala { 98af245d11STodd Fiala } 99af245d11STodd Fiala 1007206c6d1STodd Fiala std::string 101af245d11STodd Fiala NativeThreadLinux::GetName() 102af245d11STodd Fiala { 103af245d11STodd Fiala NativeProcessProtocolSP process_sp = m_process_wp.lock (); 104af245d11STodd Fiala if (!process_sp) 105af245d11STodd Fiala return "<unknown: no process>"; 106af245d11STodd Fiala 107af245d11STodd Fiala // const NativeProcessLinux *const process = reinterpret_cast<NativeProcessLinux*> (process_sp->get ()); 10839de3110SZachary Turner llvm::SmallString<32> thread_name; 10939de3110SZachary Turner HostNativeThread::GetName(GetID(), thread_name); 11039de3110SZachary Turner return thread_name.c_str(); 111af245d11STodd Fiala } 112af245d11STodd Fiala 113af245d11STodd Fiala lldb::StateType 114af245d11STodd Fiala NativeThreadLinux::GetState () 115af245d11STodd Fiala { 116af245d11STodd Fiala return m_state; 117af245d11STodd Fiala } 118af245d11STodd Fiala 119af245d11STodd Fiala 120af245d11STodd Fiala bool 12128e57429SChaoren Lin NativeThreadLinux::GetStopReason (ThreadStopInfo &stop_info, std::string& description) 122af245d11STodd Fiala { 123af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 12428e57429SChaoren Lin 12528e57429SChaoren Lin description.clear(); 12628e57429SChaoren Lin 127af245d11STodd Fiala switch (m_state) 128af245d11STodd Fiala { 129af245d11STodd Fiala case eStateStopped: 130af245d11STodd Fiala case eStateCrashed: 131af245d11STodd Fiala case eStateExited: 132af245d11STodd Fiala case eStateSuspended: 133af245d11STodd Fiala case eStateUnloaded: 134af245d11STodd Fiala if (log) 135af245d11STodd Fiala LogThreadStopInfo (*log, m_stop_info, "m_stop_info in thread:"); 136af245d11STodd Fiala stop_info = m_stop_info; 13718fe6404SChaoren Lin switch (m_stop_info.reason) 13818fe6404SChaoren Lin { 13918fe6404SChaoren Lin case StopReason::eStopReasonException: 14018fe6404SChaoren Lin case StopReason::eStopReasonBreakpoint: 14118fe6404SChaoren Lin case StopReason::eStopReasonWatchpoint: 14228e57429SChaoren Lin description = m_stop_description; 14318fe6404SChaoren Lin default: 14418fe6404SChaoren Lin break; 14518fe6404SChaoren Lin } 146af245d11STodd Fiala if (log) 147af245d11STodd Fiala LogThreadStopInfo (*log, stop_info, "returned stop_info:"); 14828e57429SChaoren Lin 149af245d11STodd Fiala return true; 150af245d11STodd Fiala 151af245d11STodd Fiala case eStateInvalid: 152af245d11STodd Fiala case eStateConnected: 153af245d11STodd Fiala case eStateAttaching: 154af245d11STodd Fiala case eStateLaunching: 155af245d11STodd Fiala case eStateRunning: 156af245d11STodd Fiala case eStateStepping: 157af245d11STodd Fiala case eStateDetached: 158af245d11STodd Fiala if (log) 159af245d11STodd Fiala { 160af245d11STodd Fiala log->Printf ("NativeThreadLinux::%s tid %" PRIu64 " in state %s cannot answer stop reason", 161af245d11STodd Fiala __FUNCTION__, GetID (), StateAsCString (m_state)); 162af245d11STodd Fiala } 163af245d11STodd Fiala return false; 164af245d11STodd Fiala } 1658faf9370SDavid Majnemer llvm_unreachable("unhandled StateType!"); 166af245d11STodd Fiala } 167af245d11STodd Fiala 168db264a6dSTamas Berghammer NativeRegisterContextSP 169af245d11STodd Fiala NativeThreadLinux::GetRegisterContext () 170af245d11STodd Fiala { 171af245d11STodd Fiala // Return the register context if we already created it. 172af245d11STodd Fiala if (m_reg_context_sp) 173af245d11STodd Fiala return m_reg_context_sp; 174af245d11STodd Fiala 175af245d11STodd Fiala // First select the appropriate RegisterInfoInterface. 176af245d11STodd Fiala RegisterInfoInterface *reg_interface = nullptr; 177af245d11STodd Fiala NativeProcessProtocolSP m_process_sp = m_process_wp.lock (); 178af245d11STodd Fiala if (!m_process_sp) 179af245d11STodd Fiala return NativeRegisterContextSP (); 180af245d11STodd Fiala 181af245d11STodd Fiala ArchSpec target_arch; 182af245d11STodd Fiala if (!m_process_sp->GetArchitecture (target_arch)) 183af245d11STodd Fiala return NativeRegisterContextSP (); 184af245d11STodd Fiala 185af245d11STodd Fiala switch (target_arch.GetTriple().getOS()) 186af245d11STodd Fiala { 187af245d11STodd Fiala case llvm::Triple::Linux: 188af245d11STodd Fiala switch (target_arch.GetMachine()) 189af245d11STodd Fiala { 190b71e89e9STodd Fiala case llvm::Triple::aarch64: 191b71e89e9STodd Fiala assert((HostInfo::GetArchitecture ().GetAddressByteSize() == 8) && "Register setting path assumes this is a 64-bit host"); 192b71e89e9STodd Fiala reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_arm64(target_arch)); 193b71e89e9STodd Fiala break; 1943f57216cSOmair Javaid case llvm::Triple::arm: 1953f57216cSOmair Javaid assert(HostInfo::GetArchitecture ().GetAddressByteSize() == 4); 1963f57216cSOmair Javaid reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_arm(target_arch)); 1973f57216cSOmair Javaid break; 198af245d11STodd Fiala case llvm::Triple::x86: 199af245d11STodd Fiala case llvm::Triple::x86_64: 20013b18261SZachary Turner if (HostInfo::GetArchitecture().GetAddressByteSize() == 4) 201af245d11STodd Fiala { 202af245d11STodd Fiala // 32-bit hosts run with a RegisterContextLinux_i386 context. 203af245d11STodd Fiala reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_i386(target_arch)); 204af245d11STodd Fiala } 205af245d11STodd Fiala else 206af245d11STodd Fiala { 20713b18261SZachary Turner assert((HostInfo::GetArchitecture().GetAddressByteSize() == 8) && 20813b18261SZachary Turner "Register setting path assumes this is a 64-bit host"); 209af245d11STodd Fiala // X86_64 hosts know how to work with 64-bit and 32-bit EXEs using the x86_64 register context. 210af245d11STodd Fiala reg_interface = static_cast<RegisterInfoInterface*> (new RegisterContextLinux_x86_64 (target_arch)); 211af245d11STodd Fiala } 212af245d11STodd Fiala break; 2133df471c3SMohit K. Bhakkad case llvm::Triple::mips64: 2143df471c3SMohit K. Bhakkad case llvm::Triple::mips64el: 2153df471c3SMohit K. Bhakkad assert((HostInfo::GetArchitecture ().GetAddressByteSize() == 8) 2163df471c3SMohit K. Bhakkad && "Register setting path assumes this is a 64-bit host"); 2173df471c3SMohit K. Bhakkad reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_mips64 (target_arch)); 2183df471c3SMohit K. Bhakkad break; 219af245d11STodd Fiala default: 220af245d11STodd Fiala break; 221af245d11STodd Fiala } 222af245d11STodd Fiala break; 223af245d11STodd Fiala default: 224af245d11STodd Fiala break; 225af245d11STodd Fiala } 226af245d11STodd Fiala 227af245d11STodd Fiala assert(reg_interface && "OS or CPU not supported!"); 228af245d11STodd Fiala if (!reg_interface) 229af245d11STodd Fiala return NativeRegisterContextSP (); 230af245d11STodd Fiala 231af245d11STodd Fiala // Now create the register context. 232af245d11STodd Fiala switch (target_arch.GetMachine()) 233af245d11STodd Fiala { 234af245d11STodd Fiala #if 0 235af245d11STodd Fiala case llvm::Triple::mips64: 236af245d11STodd Fiala { 237af245d11STodd Fiala RegisterContextPOSIXProcessMonitor_mips64 *reg_ctx = new RegisterContextPOSIXProcessMonitor_mips64(*this, 0, reg_interface); 238af245d11STodd Fiala m_posix_thread = reg_ctx; 239af245d11STodd Fiala m_reg_context_sp.reset(reg_ctx); 240af245d11STodd Fiala break; 241af245d11STodd Fiala } 242af245d11STodd Fiala #endif 2433df471c3SMohit K. Bhakkad case llvm::Triple::mips64: 2443df471c3SMohit K. Bhakkad case llvm::Triple::mips64el: 2453df471c3SMohit K. Bhakkad { 2463df471c3SMohit K. Bhakkad const uint32_t concrete_frame_idx = 0; 2473df471c3SMohit K. Bhakkad m_reg_context_sp.reset (new NativeRegisterContextLinux_mips64 (*this, concrete_frame_idx, reg_interface)); 2483df471c3SMohit K. Bhakkad break; 2493df471c3SMohit K. Bhakkad } 2501e209fccSTamas Berghammer case llvm::Triple::aarch64: 2511e209fccSTamas Berghammer { 2521e209fccSTamas Berghammer const uint32_t concrete_frame_idx = 0; 2531e209fccSTamas Berghammer m_reg_context_sp.reset (new NativeRegisterContextLinux_arm64(*this, concrete_frame_idx, reg_interface)); 2541e209fccSTamas Berghammer break; 2551e209fccSTamas Berghammer } 2563f57216cSOmair Javaid case llvm::Triple::arm: 2573f57216cSOmair Javaid { 2583f57216cSOmair Javaid const uint32_t concrete_frame_idx = 0; 2593f57216cSOmair Javaid m_reg_context_sp.reset (new NativeRegisterContextLinux_arm(*this, concrete_frame_idx, reg_interface)); 2603f57216cSOmair Javaid break; 2613f57216cSOmair Javaid } 262af245d11STodd Fiala case llvm::Triple::x86: 263af245d11STodd Fiala case llvm::Triple::x86_64: 264af245d11STodd Fiala { 265af245d11STodd Fiala const uint32_t concrete_frame_idx = 0; 266af245d11STodd Fiala m_reg_context_sp.reset (new NativeRegisterContextLinux_x86_64(*this, concrete_frame_idx, reg_interface)); 267af245d11STodd Fiala break; 268af245d11STodd Fiala } 269af245d11STodd Fiala default: 270af245d11STodd Fiala break; 271af245d11STodd Fiala } 272af245d11STodd Fiala 273af245d11STodd Fiala return m_reg_context_sp; 274af245d11STodd Fiala } 275af245d11STodd Fiala 276af245d11STodd Fiala Error 277af245d11STodd Fiala NativeThreadLinux::SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware) 278af245d11STodd Fiala { 27918fe6404SChaoren Lin if (!hardware) 280af245d11STodd Fiala return Error ("not implemented"); 281f591f69fSChaoren Lin if (m_state == eStateLaunching) 282f591f69fSChaoren Lin return Error (); 28318fe6404SChaoren Lin Error error = RemoveWatchpoint(addr); 28418fe6404SChaoren Lin if (error.Fail()) return error; 28518fe6404SChaoren Lin NativeRegisterContextSP reg_ctx = GetRegisterContext (); 28618fe6404SChaoren Lin uint32_t wp_index = 28718fe6404SChaoren Lin reg_ctx->SetHardwareWatchpoint (addr, size, watch_flags); 28818fe6404SChaoren Lin if (wp_index == LLDB_INVALID_INDEX32) 28918fe6404SChaoren Lin return Error ("Setting hardware watchpoint failed."); 29018fe6404SChaoren Lin m_watchpoint_index_map.insert({addr, wp_index}); 29118fe6404SChaoren Lin return Error (); 292af245d11STodd Fiala } 293af245d11STodd Fiala 294af245d11STodd Fiala Error 295af245d11STodd Fiala NativeThreadLinux::RemoveWatchpoint (lldb::addr_t addr) 296af245d11STodd Fiala { 29718fe6404SChaoren Lin auto wp = m_watchpoint_index_map.find(addr); 29818fe6404SChaoren Lin if (wp == m_watchpoint_index_map.end()) 29918fe6404SChaoren Lin return Error (); 30018fe6404SChaoren Lin uint32_t wp_index = wp->second; 30118fe6404SChaoren Lin m_watchpoint_index_map.erase(wp); 30218fe6404SChaoren Lin if (GetRegisterContext()->ClearHardwareWatchpoint(wp_index)) 30318fe6404SChaoren Lin return Error (); 30418fe6404SChaoren Lin return Error ("Clearing hardware watchpoint failed."); 305af245d11STodd Fiala } 306af245d11STodd Fiala 307af245d11STodd Fiala void 308af245d11STodd Fiala NativeThreadLinux::SetRunning () 309af245d11STodd Fiala { 310af245d11STodd Fiala const StateType new_state = StateType::eStateRunning; 311af245d11STodd Fiala MaybeLogStateChange (new_state); 312af245d11STodd Fiala m_state = new_state; 313af245d11STodd Fiala 314af245d11STodd Fiala m_stop_info.reason = StopReason::eStopReasonNone; 31528e57429SChaoren Lin m_stop_description.clear(); 31618fe6404SChaoren Lin 31718fe6404SChaoren Lin // If watchpoints have been set, but none on this thread, 31818fe6404SChaoren Lin // then this is a new thread. So set all existing watchpoints. 31918fe6404SChaoren Lin if (m_watchpoint_index_map.empty()) 32018fe6404SChaoren Lin { 3218bc34f4dSOleksiy Vyalov const auto process_sp = GetProcess(); 3228bc34f4dSOleksiy Vyalov if (process_sp) 3238bc34f4dSOleksiy Vyalov { 3248bc34f4dSOleksiy Vyalov const auto &watchpoint_map = process_sp->GetWatchpointMap(); 32518fe6404SChaoren Lin if (watchpoint_map.empty()) return; 32618fe6404SChaoren Lin GetRegisterContext()->ClearAllHardwareWatchpoints(); 32718fe6404SChaoren Lin for (const auto &pair : watchpoint_map) 32818fe6404SChaoren Lin { 32918fe6404SChaoren Lin const auto& wp = pair.second; 33018fe6404SChaoren Lin SetWatchpoint(wp.m_addr, wp.m_size, wp.m_watch_flags, wp.m_hardware); 33118fe6404SChaoren Lin } 33218fe6404SChaoren Lin } 333af245d11STodd Fiala } 3348bc34f4dSOleksiy Vyalov } 335af245d11STodd Fiala 336af245d11STodd Fiala void 337af245d11STodd Fiala NativeThreadLinux::SetStepping () 338af245d11STodd Fiala { 339af245d11STodd Fiala const StateType new_state = StateType::eStateStepping; 340af245d11STodd Fiala MaybeLogStateChange (new_state); 341af245d11STodd Fiala m_state = new_state; 342af245d11STodd Fiala 343af245d11STodd Fiala m_stop_info.reason = StopReason::eStopReasonNone; 344af245d11STodd Fiala } 345af245d11STodd Fiala 346af245d11STodd Fiala void 347af245d11STodd Fiala NativeThreadLinux::SetStoppedBySignal (uint32_t signo) 348af245d11STodd Fiala { 349af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 350af245d11STodd Fiala if (log) 351b8af31d4SChaoren Lin log->Printf ("NativeThreadLinux::%s called with signal 0x%02" PRIx32, __FUNCTION__, signo); 352af245d11STodd Fiala 353af245d11STodd Fiala const StateType new_state = StateType::eStateStopped; 354af245d11STodd Fiala MaybeLogStateChange (new_state); 355af245d11STodd Fiala m_state = new_state; 356af245d11STodd Fiala 357af245d11STodd Fiala m_stop_info.reason = StopReason::eStopReasonSignal; 358af245d11STodd Fiala m_stop_info.details.signal.signo = signo; 359af245d11STodd Fiala } 360af245d11STodd Fiala 361511e5cdcSTodd Fiala bool 362511e5cdcSTodd Fiala NativeThreadLinux::IsStopped (int *signo) 363511e5cdcSTodd Fiala { 364511e5cdcSTodd Fiala if (!StateIsStoppedState (m_state, false)) 365511e5cdcSTodd Fiala return false; 366511e5cdcSTodd Fiala 367511e5cdcSTodd Fiala // If we are stopped by a signal, return the signo. 368511e5cdcSTodd Fiala if (signo && 369511e5cdcSTodd Fiala m_state == StateType::eStateStopped && 370511e5cdcSTodd Fiala m_stop_info.reason == StopReason::eStopReasonSignal) 371511e5cdcSTodd Fiala { 372511e5cdcSTodd Fiala *signo = m_stop_info.details.signal.signo; 373511e5cdcSTodd Fiala } 374511e5cdcSTodd Fiala 375511e5cdcSTodd Fiala // Regardless, we are stopped. 376511e5cdcSTodd Fiala return true; 377511e5cdcSTodd Fiala } 378511e5cdcSTodd Fiala 379511e5cdcSTodd Fiala 380af245d11STodd Fiala void 381a9882ceeSTodd Fiala NativeThreadLinux::SetStoppedByExec () 382a9882ceeSTodd Fiala { 383a9882ceeSTodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 384a9882ceeSTodd Fiala if (log) 385a9882ceeSTodd Fiala log->Printf ("NativeThreadLinux::%s()", __FUNCTION__); 386a9882ceeSTodd Fiala 387a9882ceeSTodd Fiala const StateType new_state = StateType::eStateStopped; 388a9882ceeSTodd Fiala MaybeLogStateChange (new_state); 389a9882ceeSTodd Fiala m_state = new_state; 390a9882ceeSTodd Fiala 391a9882ceeSTodd Fiala m_stop_info.reason = StopReason::eStopReasonExec; 392a9882ceeSTodd Fiala m_stop_info.details.signal.signo = SIGSTOP; 393a9882ceeSTodd Fiala } 394a9882ceeSTodd Fiala 395a9882ceeSTodd Fiala void 396af245d11STodd Fiala NativeThreadLinux::SetStoppedByBreakpoint () 397af245d11STodd Fiala { 398af245d11STodd Fiala const StateType new_state = StateType::eStateStopped; 399af245d11STodd Fiala MaybeLogStateChange (new_state); 400af245d11STodd Fiala m_state = new_state; 401af245d11STodd Fiala 40228e57429SChaoren Lin m_stop_info.reason = StopReason::eStopReasonBreakpoint; 403af245d11STodd Fiala m_stop_info.details.signal.signo = SIGTRAP; 40418fe6404SChaoren Lin m_stop_description.clear(); 40518fe6404SChaoren Lin } 40618fe6404SChaoren Lin 40718fe6404SChaoren Lin void 408c16f5dcaSChaoren Lin NativeThreadLinux::SetStoppedByWatchpoint (uint32_t wp_index) 40918fe6404SChaoren Lin { 41018fe6404SChaoren Lin const StateType new_state = StateType::eStateStopped; 41118fe6404SChaoren Lin MaybeLogStateChange (new_state); 41218fe6404SChaoren Lin m_state = new_state; 41318fe6404SChaoren Lin m_stop_description.clear (); 414c16f5dcaSChaoren Lin 415c16f5dcaSChaoren Lin lldbassert(wp_index != LLDB_INVALID_INDEX32 && 416c16f5dcaSChaoren Lin "wp_index cannot be invalid"); 417eadb2a9eSTamas Berghammer 41818fe6404SChaoren Lin std::ostringstream ostr; 419c16f5dcaSChaoren Lin ostr << GetRegisterContext()->GetWatchpointAddress(wp_index) << " "; 420c16f5dcaSChaoren Lin ostr << wp_index; 42118fe6404SChaoren Lin m_stop_description = ostr.str(); 422eadb2a9eSTamas Berghammer 423eadb2a9eSTamas Berghammer m_stop_info.reason = StopReason::eStopReasonWatchpoint; 424eadb2a9eSTamas Berghammer m_stop_info.details.signal.signo = SIGTRAP; 425af245d11STodd Fiala } 426af245d11STodd Fiala 427af245d11STodd Fiala bool 428af245d11STodd Fiala NativeThreadLinux::IsStoppedAtBreakpoint () 429af245d11STodd Fiala { 43018fe6404SChaoren Lin return GetState () == StateType::eStateStopped && 43118fe6404SChaoren Lin m_stop_info.reason == StopReason::eStopReasonBreakpoint; 43218fe6404SChaoren Lin } 433af245d11STodd Fiala 43418fe6404SChaoren Lin bool 43518fe6404SChaoren Lin NativeThreadLinux::IsStoppedAtWatchpoint () 43618fe6404SChaoren Lin { 43718fe6404SChaoren Lin return GetState () == StateType::eStateStopped && 43818fe6404SChaoren Lin m_stop_info.reason == StopReason::eStopReasonWatchpoint; 439af245d11STodd Fiala } 440af245d11STodd Fiala 441af245d11STodd Fiala void 44228e57429SChaoren Lin NativeThreadLinux::SetStoppedByTrace () 44328e57429SChaoren Lin { 44428e57429SChaoren Lin const StateType new_state = StateType::eStateStopped; 44528e57429SChaoren Lin MaybeLogStateChange (new_state); 44628e57429SChaoren Lin m_state = new_state; 44728e57429SChaoren Lin 44828e57429SChaoren Lin m_stop_info.reason = StopReason::eStopReasonTrace; 44928e57429SChaoren Lin m_stop_info.details.signal.signo = SIGTRAP; 45028e57429SChaoren Lin } 45128e57429SChaoren Lin 45228e57429SChaoren Lin void 45328e57429SChaoren Lin NativeThreadLinux::SetCrashedWithException (const siginfo_t& info) 454af245d11STodd Fiala { 455af245d11STodd Fiala const StateType new_state = StateType::eStateCrashed; 456af245d11STodd Fiala MaybeLogStateChange (new_state); 457af245d11STodd Fiala m_state = new_state; 458af245d11STodd Fiala 459af245d11STodd Fiala m_stop_info.reason = StopReason::eStopReasonException; 46028e57429SChaoren Lin m_stop_info.details.signal.signo = info.si_signo; 461af245d11STodd Fiala 46228e57429SChaoren Lin const auto reason = GetCrashReason (info); 4631fab7b97STamas Berghammer m_stop_description = GetCrashReasonString (reason, reinterpret_cast<uintptr_t>(info.si_addr)); 46428e57429SChaoren Lin } 465af245d11STodd Fiala 466af245d11STodd Fiala void 467af245d11STodd Fiala NativeThreadLinux::SetSuspended () 468af245d11STodd Fiala { 469af245d11STodd Fiala const StateType new_state = StateType::eStateSuspended; 470af245d11STodd Fiala MaybeLogStateChange (new_state); 471af245d11STodd Fiala m_state = new_state; 472af245d11STodd Fiala 473af245d11STodd Fiala // FIXME what makes sense here? Do we need a suspended StopReason? 474af245d11STodd Fiala m_stop_info.reason = StopReason::eStopReasonNone; 475af245d11STodd Fiala } 476af245d11STodd Fiala 477af245d11STodd Fiala void 478af245d11STodd Fiala NativeThreadLinux::SetExited () 479af245d11STodd Fiala { 480af245d11STodd Fiala const StateType new_state = StateType::eStateExited; 481af245d11STodd Fiala MaybeLogStateChange (new_state); 482af245d11STodd Fiala m_state = new_state; 483af245d11STodd Fiala 484af245d11STodd Fiala m_stop_info.reason = StopReason::eStopReasonThreadExiting; 485af245d11STodd Fiala } 486af245d11STodd Fiala 487*8c8ff7afSPavel Labath Error 488*8c8ff7afSPavel Labath NativeThreadLinux::RequestStop () 489*8c8ff7afSPavel Labath { 490*8c8ff7afSPavel Labath Log* log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 491*8c8ff7afSPavel Labath 492*8c8ff7afSPavel Labath const auto process_sp = GetProcess(); 493*8c8ff7afSPavel Labath if (! process_sp) 494*8c8ff7afSPavel Labath return Error("Process is null."); 495*8c8ff7afSPavel Labath 496*8c8ff7afSPavel Labath lldb::pid_t pid = process_sp->GetID(); 497*8c8ff7afSPavel Labath lldb::tid_t tid = GetID(); 498*8c8ff7afSPavel Labath 499*8c8ff7afSPavel Labath if (log) 500*8c8ff7afSPavel Labath log->Printf ("NativeThreadLinux::%s requesting thread stop(pid: %" PRIu64 ", tid: %" PRIu64 ")", __FUNCTION__, pid, tid); 501*8c8ff7afSPavel Labath 502*8c8ff7afSPavel Labath Error err; 503*8c8ff7afSPavel Labath errno = 0; 504*8c8ff7afSPavel Labath if (::tgkill (pid, tid, SIGSTOP) != 0) 505*8c8ff7afSPavel Labath { 506*8c8ff7afSPavel Labath err.SetErrorToErrno (); 507*8c8ff7afSPavel Labath if (log) 508*8c8ff7afSPavel Labath log->Printf ("NativeThreadLinux::%s tgkill(%" PRIu64 ", %" PRIu64 ", SIGSTOP) failed: %s", __FUNCTION__, pid, tid, err.AsCString ()); 509*8c8ff7afSPavel Labath } 510*8c8ff7afSPavel Labath else 511*8c8ff7afSPavel Labath m_thread_context.stop_requested = true; 512*8c8ff7afSPavel Labath 513*8c8ff7afSPavel Labath return err; 514*8c8ff7afSPavel Labath } 515*8c8ff7afSPavel Labath 516af245d11STodd Fiala void 517af245d11STodd Fiala NativeThreadLinux::MaybeLogStateChange (lldb::StateType new_state) 518af245d11STodd Fiala { 519af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 520af245d11STodd Fiala // If we're not logging, we're done. 521af245d11STodd Fiala if (!log) 522af245d11STodd Fiala return; 523af245d11STodd Fiala 524af245d11STodd Fiala // If this is a state change to the same state, we're done. 525af245d11STodd Fiala lldb::StateType old_state = m_state; 526af245d11STodd Fiala if (new_state == old_state) 527af245d11STodd Fiala return; 528af245d11STodd Fiala 529af245d11STodd Fiala NativeProcessProtocolSP m_process_sp = m_process_wp.lock (); 530af245d11STodd Fiala lldb::pid_t pid = m_process_sp ? m_process_sp->GetID () : LLDB_INVALID_PROCESS_ID; 531af245d11STodd Fiala 532af245d11STodd Fiala // Log it. 533af245d11STodd Fiala log->Printf ("NativeThreadLinux: thread (pid=%" PRIu64 ", tid=%" PRIu64 ") changing from state %s to %s", pid, GetID (), StateAsCString (old_state), StateAsCString (new_state)); 534af245d11STodd Fiala } 535