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"
161e209fccSTamas Berghammer #include "NativeRegisterContextLinux_arm64.h"
172850b1beSTodd Fiala #include "NativeRegisterContextLinux_x86_64.h"
183df471c3SMohit K. Bhakkad #include "NativeRegisterContextLinux_mips64.h"
192850b1beSTodd Fiala 
20af245d11STodd Fiala #include "lldb/Core/Log.h"
21af245d11STodd Fiala #include "lldb/Core/State.h"
22af245d11STodd Fiala #include "lldb/Host/Host.h"
2313b18261SZachary Turner #include "lldb/Host/HostInfo.h"
2439de3110SZachary Turner #include "lldb/Host/HostNativeThread.h"
25c16f5dcaSChaoren Lin #include "lldb/Utility/LLDBAssert.h"
26af245d11STodd Fiala #include "lldb/lldb-enumerations.h"
2739de3110SZachary Turner 
2839de3110SZachary Turner #include "llvm/ADT/SmallString.h"
2939de3110SZachary Turner 
3028e57429SChaoren Lin #include "Plugins/Process/POSIX/CrashReason.h"
3128e57429SChaoren Lin 
32b71e89e9STodd Fiala #include "Plugins/Process/Utility/RegisterContextLinux_arm64.h"
33af245d11STodd Fiala #include "Plugins/Process/Utility/RegisterContextLinux_i386.h"
34af245d11STodd Fiala #include "Plugins/Process/Utility/RegisterContextLinux_x86_64.h"
353df471c3SMohit K. Bhakkad #include "Plugins/Process/Utility/RegisterContextLinux_mips64.h"
36af245d11STodd Fiala #include "Plugins/Process/Utility/RegisterInfoInterface.h"
37af245d11STodd Fiala 
38af245d11STodd Fiala using namespace lldb;
39af245d11STodd Fiala using namespace lldb_private;
40*db264a6dSTamas Berghammer using namespace lldb_private::process_linux;
41af245d11STodd Fiala 
42af245d11STodd Fiala namespace
43af245d11STodd Fiala {
44af245d11STodd Fiala     void LogThreadStopInfo (Log &log, const ThreadStopInfo &stop_info, const char *const header)
45af245d11STodd Fiala     {
46af245d11STodd Fiala         switch (stop_info.reason)
47af245d11STodd Fiala         {
4812fd3756SPavel Labath             case eStopReasonNone:
4912fd3756SPavel Labath                 log.Printf ("%s: %s no stop reason", __FUNCTION__, header);
5012fd3756SPavel Labath                 return;
5112fd3756SPavel Labath             case eStopReasonTrace:
5212fd3756SPavel Labath                 log.Printf ("%s: %s trace, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo);
5312fd3756SPavel Labath                 return;
5412fd3756SPavel Labath             case eStopReasonBreakpoint:
5512fd3756SPavel Labath                 log.Printf ("%s: %s breakpoint, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo);
5612fd3756SPavel Labath                 return;
5712fd3756SPavel Labath             case eStopReasonWatchpoint:
5812fd3756SPavel Labath                 log.Printf ("%s: %s watchpoint, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo);
5912fd3756SPavel Labath                 return;
60af245d11STodd Fiala             case eStopReasonSignal:
61ae29d395SChaoren Lin                 log.Printf ("%s: %s signal 0x%02" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo);
62af245d11STodd Fiala                 return;
63af245d11STodd Fiala             case eStopReasonException:
64ae29d395SChaoren Lin                 log.Printf ("%s: %s exception type 0x%02" PRIx64, __FUNCTION__, header, stop_info.details.exception.type);
65a9882ceeSTodd Fiala                 return;
66a9882ceeSTodd Fiala             case eStopReasonExec:
67a9882ceeSTodd Fiala                 log.Printf ("%s: %s exec, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo);
68af245d11STodd Fiala                 return;
6912fd3756SPavel Labath             case eStopReasonPlanComplete:
7012fd3756SPavel Labath                 log.Printf ("%s: %s plan complete", __FUNCTION__, header);
7112fd3756SPavel Labath                 return;
7212fd3756SPavel Labath             case eStopReasonThreadExiting:
7312fd3756SPavel Labath                 log.Printf ("%s: %s thread exiting", __FUNCTION__, header);
7412fd3756SPavel Labath                 return;
7512fd3756SPavel Labath             case eStopReasonInstrumentation:
7612fd3756SPavel Labath                 log.Printf ("%s: %s instrumentation", __FUNCTION__, header);
7712fd3756SPavel Labath                 return;
78af245d11STodd Fiala             default:
79a9882ceeSTodd Fiala                 log.Printf ("%s: %s invalid stop reason %" PRIu32, __FUNCTION__, header, static_cast<uint32_t> (stop_info.reason));
80af245d11STodd Fiala         }
81af245d11STodd Fiala     }
82af245d11STodd Fiala }
83af245d11STodd Fiala 
84af245d11STodd Fiala NativeThreadLinux::NativeThreadLinux (NativeProcessLinux *process, lldb::tid_t tid) :
85af245d11STodd Fiala     NativeThreadProtocol (process, tid),
86af245d11STodd Fiala     m_state (StateType::eStateInvalid),
87af245d11STodd Fiala     m_stop_info (),
8828e57429SChaoren Lin     m_reg_context_sp (),
8928e57429SChaoren Lin     m_stop_description ()
90af245d11STodd Fiala {
91af245d11STodd Fiala }
92af245d11STodd Fiala 
937206c6d1STodd Fiala std::string
94af245d11STodd Fiala NativeThreadLinux::GetName()
95af245d11STodd Fiala {
96af245d11STodd Fiala     NativeProcessProtocolSP process_sp = m_process_wp.lock ();
97af245d11STodd Fiala     if (!process_sp)
98af245d11STodd Fiala         return "<unknown: no process>";
99af245d11STodd Fiala 
100af245d11STodd Fiala     // const NativeProcessLinux *const process = reinterpret_cast<NativeProcessLinux*> (process_sp->get ());
10139de3110SZachary Turner     llvm::SmallString<32> thread_name;
10239de3110SZachary Turner     HostNativeThread::GetName(GetID(), thread_name);
10339de3110SZachary Turner     return thread_name.c_str();
104af245d11STodd Fiala }
105af245d11STodd Fiala 
106af245d11STodd Fiala lldb::StateType
107af245d11STodd Fiala NativeThreadLinux::GetState ()
108af245d11STodd Fiala {
109af245d11STodd Fiala     return m_state;
110af245d11STodd Fiala }
111af245d11STodd Fiala 
112af245d11STodd Fiala 
113af245d11STodd Fiala bool
11428e57429SChaoren Lin NativeThreadLinux::GetStopReason (ThreadStopInfo &stop_info, std::string& description)
115af245d11STodd Fiala {
116af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
11728e57429SChaoren Lin 
11828e57429SChaoren Lin     description.clear();
11928e57429SChaoren Lin 
120af245d11STodd Fiala     switch (m_state)
121af245d11STodd Fiala     {
122af245d11STodd Fiala     case eStateStopped:
123af245d11STodd Fiala     case eStateCrashed:
124af245d11STodd Fiala     case eStateExited:
125af245d11STodd Fiala     case eStateSuspended:
126af245d11STodd Fiala     case eStateUnloaded:
127af245d11STodd Fiala         if (log)
128af245d11STodd Fiala             LogThreadStopInfo (*log, m_stop_info, "m_stop_info in thread:");
129af245d11STodd Fiala         stop_info = m_stop_info;
13018fe6404SChaoren Lin         switch (m_stop_info.reason)
13118fe6404SChaoren Lin         {
13218fe6404SChaoren Lin             case StopReason::eStopReasonException:
13318fe6404SChaoren Lin             case StopReason::eStopReasonBreakpoint:
13418fe6404SChaoren Lin             case StopReason::eStopReasonWatchpoint:
13528e57429SChaoren Lin                 description = m_stop_description;
13618fe6404SChaoren Lin             default:
13718fe6404SChaoren Lin                 break;
13818fe6404SChaoren Lin         }
139af245d11STodd Fiala         if (log)
140af245d11STodd Fiala             LogThreadStopInfo (*log, stop_info, "returned stop_info:");
14128e57429SChaoren Lin 
142af245d11STodd Fiala         return true;
143af245d11STodd Fiala 
144af245d11STodd Fiala     case eStateInvalid:
145af245d11STodd Fiala     case eStateConnected:
146af245d11STodd Fiala     case eStateAttaching:
147af245d11STodd Fiala     case eStateLaunching:
148af245d11STodd Fiala     case eStateRunning:
149af245d11STodd Fiala     case eStateStepping:
150af245d11STodd Fiala     case eStateDetached:
151af245d11STodd Fiala         if (log)
152af245d11STodd Fiala         {
153af245d11STodd Fiala             log->Printf ("NativeThreadLinux::%s tid %" PRIu64 " in state %s cannot answer stop reason",
154af245d11STodd Fiala                     __FUNCTION__, GetID (), StateAsCString (m_state));
155af245d11STodd Fiala         }
156af245d11STodd Fiala         return false;
157af245d11STodd Fiala     }
1588faf9370SDavid Majnemer     llvm_unreachable("unhandled StateType!");
159af245d11STodd Fiala }
160af245d11STodd Fiala 
161*db264a6dSTamas Berghammer NativeRegisterContextSP
162af245d11STodd Fiala NativeThreadLinux::GetRegisterContext ()
163af245d11STodd Fiala {
164af245d11STodd Fiala     // Return the register context if we already created it.
165af245d11STodd Fiala     if (m_reg_context_sp)
166af245d11STodd Fiala         return m_reg_context_sp;
167af245d11STodd Fiala 
168af245d11STodd Fiala     // First select the appropriate RegisterInfoInterface.
169af245d11STodd Fiala     RegisterInfoInterface *reg_interface = nullptr;
170af245d11STodd Fiala     NativeProcessProtocolSP m_process_sp = m_process_wp.lock ();
171af245d11STodd Fiala     if (!m_process_sp)
172af245d11STodd Fiala         return NativeRegisterContextSP ();
173af245d11STodd Fiala 
174af245d11STodd Fiala     ArchSpec target_arch;
175af245d11STodd Fiala     if (!m_process_sp->GetArchitecture (target_arch))
176af245d11STodd Fiala         return NativeRegisterContextSP ();
177af245d11STodd Fiala 
178af245d11STodd Fiala     switch (target_arch.GetTriple().getOS())
179af245d11STodd Fiala     {
180af245d11STodd Fiala         case llvm::Triple::Linux:
181af245d11STodd Fiala             switch (target_arch.GetMachine())
182af245d11STodd Fiala             {
183b71e89e9STodd Fiala             case llvm::Triple::aarch64:
184b71e89e9STodd Fiala                 assert((HostInfo::GetArchitecture ().GetAddressByteSize() == 8) && "Register setting path assumes this is a 64-bit host");
185b71e89e9STodd Fiala                 reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_arm64(target_arch));
186b71e89e9STodd Fiala                 break;
187af245d11STodd Fiala             case llvm::Triple::x86:
188af245d11STodd Fiala             case llvm::Triple::x86_64:
18913b18261SZachary Turner                 if (HostInfo::GetArchitecture().GetAddressByteSize() == 4)
190af245d11STodd Fiala                 {
191af245d11STodd Fiala                     // 32-bit hosts run with a RegisterContextLinux_i386 context.
192af245d11STodd Fiala                     reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_i386(target_arch));
193af245d11STodd Fiala                 }
194af245d11STodd Fiala                 else
195af245d11STodd Fiala                 {
19613b18261SZachary Turner                     assert((HostInfo::GetArchitecture().GetAddressByteSize() == 8) &&
19713b18261SZachary Turner                            "Register setting path assumes this is a 64-bit host");
198af245d11STodd Fiala                     // X86_64 hosts know how to work with 64-bit and 32-bit EXEs using the x86_64 register context.
199af245d11STodd Fiala                     reg_interface = static_cast<RegisterInfoInterface*> (new RegisterContextLinux_x86_64 (target_arch));
200af245d11STodd Fiala                 }
201af245d11STodd Fiala                 break;
2023df471c3SMohit K. Bhakkad             case llvm::Triple::mips64:
2033df471c3SMohit K. Bhakkad             case llvm::Triple::mips64el:
2043df471c3SMohit K. Bhakkad                 assert((HostInfo::GetArchitecture ().GetAddressByteSize() == 8)
2053df471c3SMohit K. Bhakkad                     && "Register setting path assumes this is a 64-bit host");
2063df471c3SMohit K. Bhakkad                 reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_mips64 (target_arch));
2073df471c3SMohit K. Bhakkad                 break;
208af245d11STodd Fiala             default:
209af245d11STodd Fiala                 break;
210af245d11STodd Fiala             }
211af245d11STodd Fiala             break;
212af245d11STodd Fiala         default:
213af245d11STodd Fiala             break;
214af245d11STodd Fiala     }
215af245d11STodd Fiala 
216af245d11STodd Fiala     assert(reg_interface && "OS or CPU not supported!");
217af245d11STodd Fiala     if (!reg_interface)
218af245d11STodd Fiala         return NativeRegisterContextSP ();
219af245d11STodd Fiala 
220af245d11STodd Fiala     // Now create the register context.
221af245d11STodd Fiala     switch (target_arch.GetMachine())
222af245d11STodd Fiala     {
223af245d11STodd Fiala #if 0
224af245d11STodd Fiala         case llvm::Triple::mips64:
225af245d11STodd Fiala         {
226af245d11STodd Fiala             RegisterContextPOSIXProcessMonitor_mips64 *reg_ctx = new RegisterContextPOSIXProcessMonitor_mips64(*this, 0, reg_interface);
227af245d11STodd Fiala             m_posix_thread = reg_ctx;
228af245d11STodd Fiala             m_reg_context_sp.reset(reg_ctx);
229af245d11STodd Fiala             break;
230af245d11STodd Fiala         }
231af245d11STodd Fiala #endif
2323df471c3SMohit K. Bhakkad         case llvm::Triple::mips64:
2333df471c3SMohit K. Bhakkad         case llvm::Triple::mips64el:
2343df471c3SMohit K. Bhakkad         {
2353df471c3SMohit K. Bhakkad             const uint32_t concrete_frame_idx = 0;
2363df471c3SMohit K. Bhakkad             m_reg_context_sp.reset (new NativeRegisterContextLinux_mips64 (*this, concrete_frame_idx, reg_interface));
2373df471c3SMohit K. Bhakkad             break;
2383df471c3SMohit K. Bhakkad         }
2391e209fccSTamas Berghammer         case llvm::Triple::aarch64:
2401e209fccSTamas Berghammer         {
2411e209fccSTamas Berghammer             const uint32_t concrete_frame_idx = 0;
2421e209fccSTamas Berghammer             m_reg_context_sp.reset (new NativeRegisterContextLinux_arm64(*this, concrete_frame_idx, reg_interface));
2431e209fccSTamas Berghammer             break;
2441e209fccSTamas Berghammer         }
245af245d11STodd Fiala         case llvm::Triple::x86:
246af245d11STodd Fiala         case llvm::Triple::x86_64:
247af245d11STodd Fiala         {
248af245d11STodd Fiala             const uint32_t concrete_frame_idx = 0;
249af245d11STodd Fiala             m_reg_context_sp.reset (new NativeRegisterContextLinux_x86_64(*this, concrete_frame_idx, reg_interface));
250af245d11STodd Fiala             break;
251af245d11STodd Fiala         }
252af245d11STodd Fiala         default:
253af245d11STodd Fiala             break;
254af245d11STodd Fiala     }
255af245d11STodd Fiala 
256af245d11STodd Fiala     return m_reg_context_sp;
257af245d11STodd Fiala }
258af245d11STodd Fiala 
259af245d11STodd Fiala Error
260af245d11STodd Fiala NativeThreadLinux::SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware)
261af245d11STodd Fiala {
26218fe6404SChaoren Lin     if (!hardware)
263af245d11STodd Fiala         return Error ("not implemented");
264f591f69fSChaoren Lin     if (m_state == eStateLaunching)
265f591f69fSChaoren Lin         return Error ();
26618fe6404SChaoren Lin     Error error = RemoveWatchpoint(addr);
26718fe6404SChaoren Lin     if (error.Fail()) return error;
26818fe6404SChaoren Lin     NativeRegisterContextSP reg_ctx = GetRegisterContext ();
26918fe6404SChaoren Lin     uint32_t wp_index =
27018fe6404SChaoren Lin         reg_ctx->SetHardwareWatchpoint (addr, size, watch_flags);
27118fe6404SChaoren Lin     if (wp_index == LLDB_INVALID_INDEX32)
27218fe6404SChaoren Lin         return Error ("Setting hardware watchpoint failed.");
27318fe6404SChaoren Lin     m_watchpoint_index_map.insert({addr, wp_index});
27418fe6404SChaoren Lin     return Error ();
275af245d11STodd Fiala }
276af245d11STodd Fiala 
277af245d11STodd Fiala Error
278af245d11STodd Fiala NativeThreadLinux::RemoveWatchpoint (lldb::addr_t addr)
279af245d11STodd Fiala {
28018fe6404SChaoren Lin     auto wp = m_watchpoint_index_map.find(addr);
28118fe6404SChaoren Lin     if (wp == m_watchpoint_index_map.end())
28218fe6404SChaoren Lin         return Error ();
28318fe6404SChaoren Lin     uint32_t wp_index = wp->second;
28418fe6404SChaoren Lin     m_watchpoint_index_map.erase(wp);
28518fe6404SChaoren Lin     if (GetRegisterContext()->ClearHardwareWatchpoint(wp_index))
28618fe6404SChaoren Lin         return Error ();
28718fe6404SChaoren Lin     return Error ("Clearing hardware watchpoint failed.");
288af245d11STodd Fiala }
289af245d11STodd Fiala 
290af245d11STodd Fiala void
291af245d11STodd Fiala NativeThreadLinux::SetLaunching ()
292af245d11STodd Fiala {
293af245d11STodd Fiala     const StateType new_state = StateType::eStateLaunching;
294af245d11STodd Fiala     MaybeLogStateChange (new_state);
295af245d11STodd Fiala     m_state = new_state;
296af245d11STodd Fiala 
297af245d11STodd Fiala     // Also mark it as stopped since launching temporarily stops the newly created thread
298af245d11STodd Fiala     // in the ptrace machinery.
299af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonSignal;
300af245d11STodd Fiala     m_stop_info.details.signal.signo = SIGSTOP;
301af245d11STodd Fiala }
302af245d11STodd Fiala 
303af245d11STodd Fiala 
304af245d11STodd Fiala void
305af245d11STodd Fiala NativeThreadLinux::SetRunning ()
306af245d11STodd Fiala {
307af245d11STodd Fiala     const StateType new_state = StateType::eStateRunning;
308af245d11STodd Fiala     MaybeLogStateChange (new_state);
309af245d11STodd Fiala     m_state = new_state;
310af245d11STodd Fiala 
311af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonNone;
31228e57429SChaoren Lin     m_stop_description.clear();
31318fe6404SChaoren Lin 
31418fe6404SChaoren Lin     // If watchpoints have been set, but none on this thread,
31518fe6404SChaoren Lin     // then this is a new thread. So set all existing watchpoints.
31618fe6404SChaoren Lin     if (m_watchpoint_index_map.empty())
31718fe6404SChaoren Lin     {
3188bc34f4dSOleksiy Vyalov         const auto process_sp = GetProcess();
3198bc34f4dSOleksiy Vyalov         if (process_sp)
3208bc34f4dSOleksiy Vyalov         {
3218bc34f4dSOleksiy Vyalov             const auto &watchpoint_map = process_sp->GetWatchpointMap();
32218fe6404SChaoren Lin             if (watchpoint_map.empty()) return;
32318fe6404SChaoren Lin             GetRegisterContext()->ClearAllHardwareWatchpoints();
32418fe6404SChaoren Lin             for (const auto &pair : watchpoint_map)
32518fe6404SChaoren Lin             {
32618fe6404SChaoren Lin                 const auto& wp = pair.second;
32718fe6404SChaoren Lin                 SetWatchpoint(wp.m_addr, wp.m_size, wp.m_watch_flags, wp.m_hardware);
32818fe6404SChaoren Lin             }
32918fe6404SChaoren Lin         }
330af245d11STodd Fiala     }
3318bc34f4dSOleksiy Vyalov }
332af245d11STodd Fiala 
333af245d11STodd Fiala void
334af245d11STodd Fiala NativeThreadLinux::SetStepping ()
335af245d11STodd Fiala {
336af245d11STodd Fiala     const StateType new_state = StateType::eStateStepping;
337af245d11STodd Fiala     MaybeLogStateChange (new_state);
338af245d11STodd Fiala     m_state = new_state;
339af245d11STodd Fiala 
340af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonNone;
341af245d11STodd Fiala }
342af245d11STodd Fiala 
343af245d11STodd Fiala void
344af245d11STodd Fiala NativeThreadLinux::SetStoppedBySignal (uint32_t signo)
345af245d11STodd Fiala {
346af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
347af245d11STodd Fiala     if (log)
348b8af31d4SChaoren Lin         log->Printf ("NativeThreadLinux::%s called with signal 0x%02" PRIx32, __FUNCTION__, signo);
349af245d11STodd Fiala 
350af245d11STodd Fiala     const StateType new_state = StateType::eStateStopped;
351af245d11STodd Fiala     MaybeLogStateChange (new_state);
352af245d11STodd Fiala     m_state = new_state;
353af245d11STodd Fiala 
354af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonSignal;
355af245d11STodd Fiala     m_stop_info.details.signal.signo = signo;
356af245d11STodd Fiala }
357af245d11STodd Fiala 
358511e5cdcSTodd Fiala bool
359511e5cdcSTodd Fiala NativeThreadLinux::IsStopped (int *signo)
360511e5cdcSTodd Fiala {
361511e5cdcSTodd Fiala     if (!StateIsStoppedState (m_state, false))
362511e5cdcSTodd Fiala         return false;
363511e5cdcSTodd Fiala 
364511e5cdcSTodd Fiala     // If we are stopped by a signal, return the signo.
365511e5cdcSTodd Fiala     if (signo &&
366511e5cdcSTodd Fiala         m_state == StateType::eStateStopped &&
367511e5cdcSTodd Fiala         m_stop_info.reason == StopReason::eStopReasonSignal)
368511e5cdcSTodd Fiala     {
369511e5cdcSTodd Fiala         *signo = m_stop_info.details.signal.signo;
370511e5cdcSTodd Fiala     }
371511e5cdcSTodd Fiala 
372511e5cdcSTodd Fiala     // Regardless, we are stopped.
373511e5cdcSTodd Fiala     return true;
374511e5cdcSTodd Fiala }
375511e5cdcSTodd Fiala 
376511e5cdcSTodd Fiala 
377af245d11STodd Fiala void
378a9882ceeSTodd Fiala NativeThreadLinux::SetStoppedByExec ()
379a9882ceeSTodd Fiala {
380a9882ceeSTodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
381a9882ceeSTodd Fiala     if (log)
382a9882ceeSTodd Fiala         log->Printf ("NativeThreadLinux::%s()", __FUNCTION__);
383a9882ceeSTodd Fiala 
384a9882ceeSTodd Fiala     const StateType new_state = StateType::eStateStopped;
385a9882ceeSTodd Fiala     MaybeLogStateChange (new_state);
386a9882ceeSTodd Fiala     m_state = new_state;
387a9882ceeSTodd Fiala 
388a9882ceeSTodd Fiala     m_stop_info.reason = StopReason::eStopReasonExec;
389a9882ceeSTodd Fiala     m_stop_info.details.signal.signo = SIGSTOP;
390a9882ceeSTodd Fiala }
391a9882ceeSTodd Fiala 
392a9882ceeSTodd Fiala void
393af245d11STodd Fiala NativeThreadLinux::SetStoppedByBreakpoint ()
394af245d11STodd Fiala {
395af245d11STodd Fiala     const StateType new_state = StateType::eStateStopped;
396af245d11STodd Fiala     MaybeLogStateChange (new_state);
397af245d11STodd Fiala     m_state = new_state;
398af245d11STodd Fiala 
39928e57429SChaoren Lin     m_stop_info.reason = StopReason::eStopReasonBreakpoint;
400af245d11STodd Fiala     m_stop_info.details.signal.signo = SIGTRAP;
40118fe6404SChaoren Lin     m_stop_description.clear();
40218fe6404SChaoren Lin }
40318fe6404SChaoren Lin 
40418fe6404SChaoren Lin void
405c16f5dcaSChaoren Lin NativeThreadLinux::SetStoppedByWatchpoint (uint32_t wp_index)
40618fe6404SChaoren Lin {
40718fe6404SChaoren Lin     const StateType new_state = StateType::eStateStopped;
40818fe6404SChaoren Lin     MaybeLogStateChange (new_state);
40918fe6404SChaoren Lin     m_state = new_state;
41018fe6404SChaoren Lin     m_stop_description.clear ();
411c16f5dcaSChaoren Lin 
412c16f5dcaSChaoren Lin     lldbassert(wp_index != LLDB_INVALID_INDEX32 &&
413c16f5dcaSChaoren Lin                "wp_index cannot be invalid");
414eadb2a9eSTamas Berghammer 
41518fe6404SChaoren Lin     std::ostringstream ostr;
416c16f5dcaSChaoren Lin     ostr << GetRegisterContext()->GetWatchpointAddress(wp_index) << " ";
417c16f5dcaSChaoren Lin     ostr << wp_index;
41818fe6404SChaoren Lin     m_stop_description = ostr.str();
419eadb2a9eSTamas Berghammer 
420eadb2a9eSTamas Berghammer     m_stop_info.reason = StopReason::eStopReasonWatchpoint;
421eadb2a9eSTamas Berghammer     m_stop_info.details.signal.signo = SIGTRAP;
422af245d11STodd Fiala }
423af245d11STodd Fiala 
424af245d11STodd Fiala bool
425af245d11STodd Fiala NativeThreadLinux::IsStoppedAtBreakpoint ()
426af245d11STodd Fiala {
42718fe6404SChaoren Lin     return GetState () == StateType::eStateStopped &&
42818fe6404SChaoren Lin         m_stop_info.reason == StopReason::eStopReasonBreakpoint;
42918fe6404SChaoren Lin }
430af245d11STodd Fiala 
43118fe6404SChaoren Lin bool
43218fe6404SChaoren Lin NativeThreadLinux::IsStoppedAtWatchpoint ()
43318fe6404SChaoren Lin {
43418fe6404SChaoren Lin     return GetState () == StateType::eStateStopped &&
43518fe6404SChaoren Lin         m_stop_info.reason == StopReason::eStopReasonWatchpoint;
436af245d11STodd Fiala }
437af245d11STodd Fiala 
438af245d11STodd Fiala void
43928e57429SChaoren Lin NativeThreadLinux::SetStoppedByTrace ()
44028e57429SChaoren Lin {
44128e57429SChaoren Lin     const StateType new_state = StateType::eStateStopped;
44228e57429SChaoren Lin     MaybeLogStateChange (new_state);
44328e57429SChaoren Lin     m_state = new_state;
44428e57429SChaoren Lin 
44528e57429SChaoren Lin     m_stop_info.reason = StopReason::eStopReasonTrace;
44628e57429SChaoren Lin     m_stop_info.details.signal.signo = SIGTRAP;
44728e57429SChaoren Lin }
44828e57429SChaoren Lin 
44928e57429SChaoren Lin void
45028e57429SChaoren Lin NativeThreadLinux::SetCrashedWithException (const siginfo_t& info)
451af245d11STodd Fiala {
452af245d11STodd Fiala     const StateType new_state = StateType::eStateCrashed;
453af245d11STodd Fiala     MaybeLogStateChange (new_state);
454af245d11STodd Fiala     m_state = new_state;
455af245d11STodd Fiala 
456af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonException;
45728e57429SChaoren Lin     m_stop_info.details.signal.signo = info.si_signo;
458af245d11STodd Fiala 
45928e57429SChaoren Lin     const auto reason = GetCrashReason (info);
46028e57429SChaoren Lin     m_stop_description = GetCrashReasonString (reason, reinterpret_cast<lldb::addr_t> (info.si_addr));
46128e57429SChaoren Lin }
462af245d11STodd Fiala 
463af245d11STodd Fiala void
464af245d11STodd Fiala NativeThreadLinux::SetSuspended ()
465af245d11STodd Fiala {
466af245d11STodd Fiala     const StateType new_state = StateType::eStateSuspended;
467af245d11STodd Fiala     MaybeLogStateChange (new_state);
468af245d11STodd Fiala     m_state = new_state;
469af245d11STodd Fiala 
470af245d11STodd Fiala     // FIXME what makes sense here? Do we need a suspended StopReason?
471af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonNone;
472af245d11STodd Fiala }
473af245d11STodd Fiala 
474af245d11STodd Fiala void
475af245d11STodd Fiala NativeThreadLinux::SetExited ()
476af245d11STodd Fiala {
477af245d11STodd Fiala     const StateType new_state = StateType::eStateExited;
478af245d11STodd Fiala     MaybeLogStateChange (new_state);
479af245d11STodd Fiala     m_state = new_state;
480af245d11STodd Fiala 
481af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonThreadExiting;
482af245d11STodd Fiala }
483af245d11STodd Fiala 
484af245d11STodd Fiala void
485af245d11STodd Fiala NativeThreadLinux::MaybeLogStateChange (lldb::StateType new_state)
486af245d11STodd Fiala {
487af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
488af245d11STodd Fiala     // If we're not logging, we're done.
489af245d11STodd Fiala     if (!log)
490af245d11STodd Fiala         return;
491af245d11STodd Fiala 
492af245d11STodd Fiala     // If this is a state change to the same state, we're done.
493af245d11STodd Fiala     lldb::StateType old_state = m_state;
494af245d11STodd Fiala     if (new_state == old_state)
495af245d11STodd Fiala         return;
496af245d11STodd Fiala 
497af245d11STodd Fiala     NativeProcessProtocolSP m_process_sp = m_process_wp.lock ();
498af245d11STodd Fiala     lldb::pid_t pid = m_process_sp ? m_process_sp->GetID () : LLDB_INVALID_PROCESS_ID;
499af245d11STodd Fiala 
500af245d11STodd Fiala     // Log it.
501af245d11STodd Fiala     log->Printf ("NativeThreadLinux: thread (pid=%" PRIu64 ", tid=%" PRIu64 ") changing from state %s to %s", pid, GetID (), StateAsCString (old_state), StateAsCString (new_state));
502af245d11STodd Fiala }
503