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"
162850b1beSTodd Fiala #include "NativeRegisterContextLinux_x86_64.h"
172850b1beSTodd Fiala 
18af245d11STodd Fiala #include "lldb/Core/Log.h"
19af245d11STodd Fiala #include "lldb/Core/State.h"
20af245d11STodd Fiala #include "lldb/Host/Host.h"
2113b18261SZachary Turner #include "lldb/Host/HostInfo.h"
2239de3110SZachary Turner #include "lldb/Host/HostNativeThread.h"
23af245d11STodd Fiala #include "lldb/lldb-enumerations.h"
24af245d11STodd Fiala #include "lldb/lldb-private-log.h"
2539de3110SZachary Turner 
2639de3110SZachary Turner #include "llvm/ADT/SmallString.h"
2739de3110SZachary Turner 
2828e57429SChaoren Lin #include "Plugins/Process/POSIX/CrashReason.h"
2928e57429SChaoren Lin 
30b71e89e9STodd Fiala #include "Plugins/Process/Utility/RegisterContextLinux_arm64.h"
31af245d11STodd Fiala #include "Plugins/Process/Utility/RegisterContextLinux_i386.h"
32af245d11STodd Fiala #include "Plugins/Process/Utility/RegisterContextLinux_x86_64.h"
33af245d11STodd Fiala #include "Plugins/Process/Utility/RegisterInfoInterface.h"
34af245d11STodd Fiala 
35af245d11STodd Fiala using namespace lldb;
36af245d11STodd Fiala using namespace lldb_private;
37af245d11STodd Fiala 
38af245d11STodd Fiala namespace
39af245d11STodd Fiala {
40af245d11STodd Fiala     void LogThreadStopInfo (Log &log, const ThreadStopInfo &stop_info, const char *const header)
41af245d11STodd Fiala     {
42af245d11STodd Fiala         switch (stop_info.reason)
43af245d11STodd Fiala         {
44af245d11STodd Fiala             case eStopReasonSignal:
45ae29d395SChaoren Lin                 log.Printf ("%s: %s signal 0x%02" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo);
46af245d11STodd Fiala                 return;
47af245d11STodd Fiala             case eStopReasonException:
48ae29d395SChaoren Lin                 log.Printf ("%s: %s exception type 0x%02" PRIx64, __FUNCTION__, header, stop_info.details.exception.type);
49a9882ceeSTodd Fiala                 return;
50a9882ceeSTodd Fiala             case eStopReasonExec:
51a9882ceeSTodd Fiala                 log.Printf ("%s: %s exec, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo);
52af245d11STodd Fiala                 return;
53af245d11STodd Fiala             default:
54a9882ceeSTodd Fiala                 log.Printf ("%s: %s invalid stop reason %" PRIu32, __FUNCTION__, header, static_cast<uint32_t> (stop_info.reason));
55af245d11STodd Fiala         }
56af245d11STodd Fiala     }
57af245d11STodd Fiala }
58af245d11STodd Fiala 
59af245d11STodd Fiala NativeThreadLinux::NativeThreadLinux (NativeProcessLinux *process, lldb::tid_t tid) :
60af245d11STodd Fiala     NativeThreadProtocol (process, tid),
61af245d11STodd Fiala     m_state (StateType::eStateInvalid),
62af245d11STodd Fiala     m_stop_info (),
6328e57429SChaoren Lin     m_reg_context_sp (),
6428e57429SChaoren Lin     m_stop_description ()
65af245d11STodd Fiala {
66af245d11STodd Fiala }
67af245d11STodd Fiala 
687206c6d1STodd Fiala std::string
69af245d11STodd Fiala NativeThreadLinux::GetName()
70af245d11STodd Fiala {
71af245d11STodd Fiala     NativeProcessProtocolSP process_sp = m_process_wp.lock ();
72af245d11STodd Fiala     if (!process_sp)
73af245d11STodd Fiala         return "<unknown: no process>";
74af245d11STodd Fiala 
75af245d11STodd Fiala     // const NativeProcessLinux *const process = reinterpret_cast<NativeProcessLinux*> (process_sp->get ());
7639de3110SZachary Turner     llvm::SmallString<32> thread_name;
7739de3110SZachary Turner     HostNativeThread::GetName(GetID(), thread_name);
7839de3110SZachary Turner     return thread_name.c_str();
79af245d11STodd Fiala }
80af245d11STodd Fiala 
81af245d11STodd Fiala lldb::StateType
82af245d11STodd Fiala NativeThreadLinux::GetState ()
83af245d11STodd Fiala {
84af245d11STodd Fiala     return m_state;
85af245d11STodd Fiala }
86af245d11STodd Fiala 
87af245d11STodd Fiala 
88af245d11STodd Fiala bool
8928e57429SChaoren Lin NativeThreadLinux::GetStopReason (ThreadStopInfo &stop_info, std::string& description)
90af245d11STodd Fiala {
91af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
9228e57429SChaoren Lin 
9328e57429SChaoren Lin     description.clear();
9428e57429SChaoren Lin 
95af245d11STodd Fiala     switch (m_state)
96af245d11STodd Fiala     {
97af245d11STodd Fiala     case eStateStopped:
98af245d11STodd Fiala     case eStateCrashed:
99af245d11STodd Fiala     case eStateExited:
100af245d11STodd Fiala     case eStateSuspended:
101af245d11STodd Fiala     case eStateUnloaded:
102af245d11STodd Fiala         if (log)
103af245d11STodd Fiala             LogThreadStopInfo (*log, m_stop_info, "m_stop_info in thread:");
104af245d11STodd Fiala         stop_info = m_stop_info;
10518fe6404SChaoren Lin         switch (m_stop_info.reason)
10618fe6404SChaoren Lin         {
10718fe6404SChaoren Lin             case StopReason::eStopReasonException:
10818fe6404SChaoren Lin             case StopReason::eStopReasonBreakpoint:
10918fe6404SChaoren Lin             case StopReason::eStopReasonWatchpoint:
11028e57429SChaoren Lin                 description = m_stop_description;
11118fe6404SChaoren Lin             default:
11218fe6404SChaoren Lin                 break;
11318fe6404SChaoren Lin         }
114af245d11STodd Fiala         if (log)
115af245d11STodd Fiala             LogThreadStopInfo (*log, stop_info, "returned stop_info:");
11628e57429SChaoren Lin 
117af245d11STodd Fiala         return true;
118af245d11STodd Fiala 
119af245d11STodd Fiala     case eStateInvalid:
120af245d11STodd Fiala     case eStateConnected:
121af245d11STodd Fiala     case eStateAttaching:
122af245d11STodd Fiala     case eStateLaunching:
123af245d11STodd Fiala     case eStateRunning:
124af245d11STodd Fiala     case eStateStepping:
125af245d11STodd Fiala     case eStateDetached:
126af245d11STodd Fiala         if (log)
127af245d11STodd Fiala         {
128af245d11STodd Fiala             log->Printf ("NativeThreadLinux::%s tid %" PRIu64 " in state %s cannot answer stop reason",
129af245d11STodd Fiala                     __FUNCTION__, GetID (), StateAsCString (m_state));
130af245d11STodd Fiala         }
131af245d11STodd Fiala         return false;
132af245d11STodd Fiala     }
1338faf9370SDavid Majnemer     llvm_unreachable("unhandled StateType!");
134af245d11STodd Fiala }
135af245d11STodd Fiala 
136af245d11STodd Fiala lldb_private::NativeRegisterContextSP
137af245d11STodd Fiala NativeThreadLinux::GetRegisterContext ()
138af245d11STodd Fiala {
139af245d11STodd Fiala     // Return the register context if we already created it.
140af245d11STodd Fiala     if (m_reg_context_sp)
141af245d11STodd Fiala         return m_reg_context_sp;
142af245d11STodd Fiala 
143af245d11STodd Fiala     // First select the appropriate RegisterInfoInterface.
144af245d11STodd Fiala     RegisterInfoInterface *reg_interface = nullptr;
145af245d11STodd Fiala     NativeProcessProtocolSP m_process_sp = m_process_wp.lock ();
146af245d11STodd Fiala     if (!m_process_sp)
147af245d11STodd Fiala         return NativeRegisterContextSP ();
148af245d11STodd Fiala 
149af245d11STodd Fiala     ArchSpec target_arch;
150af245d11STodd Fiala     if (!m_process_sp->GetArchitecture (target_arch))
151af245d11STodd Fiala         return NativeRegisterContextSP ();
152af245d11STodd Fiala 
153af245d11STodd Fiala     switch (target_arch.GetTriple().getOS())
154af245d11STodd Fiala     {
155af245d11STodd Fiala         case llvm::Triple::Linux:
156af245d11STodd Fiala             switch (target_arch.GetMachine())
157af245d11STodd Fiala             {
158b71e89e9STodd Fiala             case llvm::Triple::aarch64:
159b71e89e9STodd Fiala                 assert((HostInfo::GetArchitecture ().GetAddressByteSize() == 8) && "Register setting path assumes this is a 64-bit host");
160b71e89e9STodd Fiala                 reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_arm64(target_arch));
161b71e89e9STodd Fiala                 break;
162af245d11STodd Fiala             case llvm::Triple::x86:
163af245d11STodd Fiala             case llvm::Triple::x86_64:
16413b18261SZachary Turner                 if (HostInfo::GetArchitecture().GetAddressByteSize() == 4)
165af245d11STodd Fiala                 {
166af245d11STodd Fiala                     // 32-bit hosts run with a RegisterContextLinux_i386 context.
167af245d11STodd Fiala                     reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_i386(target_arch));
168af245d11STodd Fiala                 }
169af245d11STodd Fiala                 else
170af245d11STodd Fiala                 {
17113b18261SZachary Turner                     assert((HostInfo::GetArchitecture().GetAddressByteSize() == 8) &&
17213b18261SZachary Turner                            "Register setting path assumes this is a 64-bit host");
173af245d11STodd Fiala                     // X86_64 hosts know how to work with 64-bit and 32-bit EXEs using the x86_64 register context.
174af245d11STodd Fiala                     reg_interface = static_cast<RegisterInfoInterface*> (new RegisterContextLinux_x86_64 (target_arch));
175af245d11STodd Fiala                 }
176af245d11STodd Fiala                 break;
177af245d11STodd Fiala             default:
178af245d11STodd Fiala                 break;
179af245d11STodd Fiala             }
180af245d11STodd Fiala             break;
181af245d11STodd Fiala         default:
182af245d11STodd Fiala             break;
183af245d11STodd Fiala     }
184af245d11STodd Fiala 
185af245d11STodd Fiala     assert(reg_interface && "OS or CPU not supported!");
186af245d11STodd Fiala     if (!reg_interface)
187af245d11STodd Fiala         return NativeRegisterContextSP ();
188af245d11STodd Fiala 
189af245d11STodd Fiala     // Now create the register context.
190af245d11STodd Fiala     switch (target_arch.GetMachine())
191af245d11STodd Fiala     {
192af245d11STodd Fiala #if 0
193af245d11STodd Fiala         case llvm::Triple::mips64:
194af245d11STodd Fiala         {
195af245d11STodd Fiala             RegisterContextPOSIXProcessMonitor_mips64 *reg_ctx = new RegisterContextPOSIXProcessMonitor_mips64(*this, 0, reg_interface);
196af245d11STodd Fiala             m_posix_thread = reg_ctx;
197af245d11STodd Fiala             m_reg_context_sp.reset(reg_ctx);
198af245d11STodd Fiala             break;
199af245d11STodd Fiala         }
200af245d11STodd Fiala #endif
2011c6a1ea9STamas Berghammer 
202af245d11STodd Fiala         case llvm::Triple::x86:
203af245d11STodd Fiala         case llvm::Triple::x86_64:
204af245d11STodd Fiala         {
205af245d11STodd Fiala             const uint32_t concrete_frame_idx = 0;
206af245d11STodd Fiala             m_reg_context_sp.reset (new NativeRegisterContextLinux_x86_64(*this, concrete_frame_idx, reg_interface));
207af245d11STodd Fiala             break;
208af245d11STodd Fiala         }
209af245d11STodd Fiala         default:
210af245d11STodd Fiala             break;
211af245d11STodd Fiala     }
212af245d11STodd Fiala 
213af245d11STodd Fiala     return m_reg_context_sp;
214af245d11STodd Fiala }
215af245d11STodd Fiala 
216af245d11STodd Fiala Error
217af245d11STodd Fiala NativeThreadLinux::SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware)
218af245d11STodd Fiala {
21918fe6404SChaoren Lin     if (!hardware)
220af245d11STodd Fiala         return Error ("not implemented");
221*f591f69fSChaoren Lin     if (m_state == eStateLaunching)
222*f591f69fSChaoren Lin         return Error ();
22318fe6404SChaoren Lin     Error error = RemoveWatchpoint(addr);
22418fe6404SChaoren Lin     if (error.Fail()) return error;
22518fe6404SChaoren Lin     NativeRegisterContextSP reg_ctx = GetRegisterContext ();
22618fe6404SChaoren Lin     uint32_t wp_index =
22718fe6404SChaoren Lin         reg_ctx->SetHardwareWatchpoint (addr, size, watch_flags);
22818fe6404SChaoren Lin     if (wp_index == LLDB_INVALID_INDEX32)
22918fe6404SChaoren Lin         return Error ("Setting hardware watchpoint failed.");
23018fe6404SChaoren Lin     m_watchpoint_index_map.insert({addr, wp_index});
23118fe6404SChaoren Lin     return Error ();
232af245d11STodd Fiala }
233af245d11STodd Fiala 
234af245d11STodd Fiala Error
235af245d11STodd Fiala NativeThreadLinux::RemoveWatchpoint (lldb::addr_t addr)
236af245d11STodd Fiala {
23718fe6404SChaoren Lin     auto wp = m_watchpoint_index_map.find(addr);
23818fe6404SChaoren Lin     if (wp == m_watchpoint_index_map.end())
23918fe6404SChaoren Lin         return Error ();
24018fe6404SChaoren Lin     uint32_t wp_index = wp->second;
24118fe6404SChaoren Lin     m_watchpoint_index_map.erase(wp);
24218fe6404SChaoren Lin     if (GetRegisterContext()->ClearHardwareWatchpoint(wp_index))
24318fe6404SChaoren Lin         return Error ();
24418fe6404SChaoren Lin     return Error ("Clearing hardware watchpoint failed.");
245af245d11STodd Fiala }
246af245d11STodd Fiala 
247af245d11STodd Fiala void
248af245d11STodd Fiala NativeThreadLinux::SetLaunching ()
249af245d11STodd Fiala {
250af245d11STodd Fiala     const StateType new_state = StateType::eStateLaunching;
251af245d11STodd Fiala     MaybeLogStateChange (new_state);
252af245d11STodd Fiala     m_state = new_state;
253af245d11STodd Fiala 
254af245d11STodd Fiala     // Also mark it as stopped since launching temporarily stops the newly created thread
255af245d11STodd Fiala     // in the ptrace machinery.
256af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonSignal;
257af245d11STodd Fiala     m_stop_info.details.signal.signo = SIGSTOP;
258af245d11STodd Fiala }
259af245d11STodd Fiala 
260af245d11STodd Fiala 
261af245d11STodd Fiala void
262af245d11STodd Fiala NativeThreadLinux::SetRunning ()
263af245d11STodd Fiala {
264af245d11STodd Fiala     const StateType new_state = StateType::eStateRunning;
265af245d11STodd Fiala     MaybeLogStateChange (new_state);
266af245d11STodd Fiala     m_state = new_state;
267af245d11STodd Fiala 
268af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonNone;
26928e57429SChaoren Lin     m_stop_description.clear();
27018fe6404SChaoren Lin 
27118fe6404SChaoren Lin     // If watchpoints have been set, but none on this thread,
27218fe6404SChaoren Lin     // then this is a new thread. So set all existing watchpoints.
27318fe6404SChaoren Lin     if (m_watchpoint_index_map.empty())
27418fe6404SChaoren Lin     {
2758bc34f4dSOleksiy Vyalov         const auto process_sp = GetProcess();
2768bc34f4dSOleksiy Vyalov         if (process_sp)
2778bc34f4dSOleksiy Vyalov         {
2788bc34f4dSOleksiy Vyalov             const auto &watchpoint_map = process_sp->GetWatchpointMap();
27918fe6404SChaoren Lin             if (watchpoint_map.empty()) return;
28018fe6404SChaoren Lin             GetRegisterContext()->ClearAllHardwareWatchpoints();
28118fe6404SChaoren Lin             for (const auto &pair : watchpoint_map)
28218fe6404SChaoren Lin             {
28318fe6404SChaoren Lin                 const auto& wp = pair.second;
28418fe6404SChaoren Lin                 SetWatchpoint(wp.m_addr, wp.m_size, wp.m_watch_flags, wp.m_hardware);
28518fe6404SChaoren Lin             }
28618fe6404SChaoren Lin         }
287af245d11STodd Fiala     }
2888bc34f4dSOleksiy Vyalov }
289af245d11STodd Fiala 
290af245d11STodd Fiala void
291af245d11STodd Fiala NativeThreadLinux::SetStepping ()
292af245d11STodd Fiala {
293af245d11STodd Fiala     const StateType new_state = StateType::eStateStepping;
294af245d11STodd Fiala     MaybeLogStateChange (new_state);
295af245d11STodd Fiala     m_state = new_state;
296af245d11STodd Fiala 
297af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonNone;
298af245d11STodd Fiala }
299af245d11STodd Fiala 
300af245d11STodd Fiala void
301af245d11STodd Fiala NativeThreadLinux::SetStoppedBySignal (uint32_t signo)
302af245d11STodd Fiala {
303af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
304af245d11STodd Fiala     if (log)
305b8af31d4SChaoren Lin         log->Printf ("NativeThreadLinux::%s called with signal 0x%02" PRIx32, __FUNCTION__, signo);
306af245d11STodd Fiala 
307af245d11STodd Fiala     const StateType new_state = StateType::eStateStopped;
308af245d11STodd Fiala     MaybeLogStateChange (new_state);
309af245d11STodd Fiala     m_state = new_state;
310af245d11STodd Fiala 
311af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonSignal;
312af245d11STodd Fiala     m_stop_info.details.signal.signo = signo;
313af245d11STodd Fiala }
314af245d11STodd Fiala 
315511e5cdcSTodd Fiala bool
316511e5cdcSTodd Fiala NativeThreadLinux::IsStopped (int *signo)
317511e5cdcSTodd Fiala {
318511e5cdcSTodd Fiala     if (!StateIsStoppedState (m_state, false))
319511e5cdcSTodd Fiala         return false;
320511e5cdcSTodd Fiala 
321511e5cdcSTodd Fiala     // If we are stopped by a signal, return the signo.
322511e5cdcSTodd Fiala     if (signo &&
323511e5cdcSTodd Fiala         m_state == StateType::eStateStopped &&
324511e5cdcSTodd Fiala         m_stop_info.reason == StopReason::eStopReasonSignal)
325511e5cdcSTodd Fiala     {
326511e5cdcSTodd Fiala         *signo = m_stop_info.details.signal.signo;
327511e5cdcSTodd Fiala     }
328511e5cdcSTodd Fiala 
329511e5cdcSTodd Fiala     // Regardless, we are stopped.
330511e5cdcSTodd Fiala     return true;
331511e5cdcSTodd Fiala }
332511e5cdcSTodd Fiala 
333511e5cdcSTodd Fiala 
334af245d11STodd Fiala void
335a9882ceeSTodd Fiala NativeThreadLinux::SetStoppedByExec ()
336a9882ceeSTodd Fiala {
337a9882ceeSTodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
338a9882ceeSTodd Fiala     if (log)
339a9882ceeSTodd Fiala         log->Printf ("NativeThreadLinux::%s()", __FUNCTION__);
340a9882ceeSTodd Fiala 
341a9882ceeSTodd Fiala     const StateType new_state = StateType::eStateStopped;
342a9882ceeSTodd Fiala     MaybeLogStateChange (new_state);
343a9882ceeSTodd Fiala     m_state = new_state;
344a9882ceeSTodd Fiala 
345a9882ceeSTodd Fiala     m_stop_info.reason = StopReason::eStopReasonExec;
346a9882ceeSTodd Fiala     m_stop_info.details.signal.signo = SIGSTOP;
347a9882ceeSTodd Fiala }
348a9882ceeSTodd Fiala 
349a9882ceeSTodd Fiala void
350af245d11STodd Fiala NativeThreadLinux::SetStoppedByBreakpoint ()
351af245d11STodd Fiala {
352af245d11STodd Fiala     const StateType new_state = StateType::eStateStopped;
353af245d11STodd Fiala     MaybeLogStateChange (new_state);
354af245d11STodd Fiala     m_state = new_state;
355af245d11STodd Fiala 
35628e57429SChaoren Lin     m_stop_info.reason = StopReason::eStopReasonBreakpoint;
357af245d11STodd Fiala     m_stop_info.details.signal.signo = SIGTRAP;
35818fe6404SChaoren Lin     m_stop_description.clear();
35918fe6404SChaoren Lin }
36018fe6404SChaoren Lin 
36118fe6404SChaoren Lin void
36218fe6404SChaoren Lin NativeThreadLinux::SetStoppedByWatchpoint ()
36318fe6404SChaoren Lin {
36418fe6404SChaoren Lin     const StateType new_state = StateType::eStateStopped;
36518fe6404SChaoren Lin     MaybeLogStateChange (new_state);
36618fe6404SChaoren Lin     m_state = new_state;
36718fe6404SChaoren Lin 
36818fe6404SChaoren Lin     m_stop_info.reason = StopReason::eStopReasonWatchpoint;
36918fe6404SChaoren Lin     m_stop_info.details.signal.signo = SIGTRAP;
37018fe6404SChaoren Lin 
37118fe6404SChaoren Lin     NativeRegisterContextLinux_x86_64 *reg_ctx =
37218fe6404SChaoren Lin         reinterpret_cast<NativeRegisterContextLinux_x86_64*> (GetRegisterContext().get());
37318fe6404SChaoren Lin     const uint32_t num_hw_watchpoints =
37418fe6404SChaoren Lin         reg_ctx->NumSupportedHardwareWatchpoints();
37518fe6404SChaoren Lin 
37618fe6404SChaoren Lin     m_stop_description.clear ();
37718fe6404SChaoren Lin     for (uint32_t wp_index = 0; wp_index < num_hw_watchpoints; ++wp_index)
37818fe6404SChaoren Lin         if (reg_ctx->IsWatchpointHit(wp_index).Success())
37918fe6404SChaoren Lin         {
38018fe6404SChaoren Lin             std::ostringstream ostr;
38118fe6404SChaoren Lin             ostr << reg_ctx->GetWatchpointAddress(wp_index) << " " << wp_index;
38218fe6404SChaoren Lin             m_stop_description = ostr.str();
38318fe6404SChaoren Lin             return;
38418fe6404SChaoren Lin         }
38518fe6404SChaoren Lin     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
38618fe6404SChaoren Lin     if (log)
38718fe6404SChaoren Lin     {
38818fe6404SChaoren Lin         NativeProcessProtocolSP m_process_sp = m_process_wp.lock ();
38918fe6404SChaoren Lin         lldb::pid_t pid = m_process_sp ? m_process_sp->GetID () : LLDB_INVALID_PROCESS_ID;
39018fe6404SChaoren Lin         log->Printf ("NativeThreadLinux: thread (pid=%" PRIu64 ", tid=%" PRIu64 ") "
39118fe6404SChaoren Lin                 "stopped by a watchpoint, but failed to find it",
39218fe6404SChaoren Lin                 pid, GetID ());
39318fe6404SChaoren Lin     }
394af245d11STodd Fiala }
395af245d11STodd Fiala 
396af245d11STodd Fiala bool
397af245d11STodd Fiala NativeThreadLinux::IsStoppedAtBreakpoint ()
398af245d11STodd Fiala {
39918fe6404SChaoren Lin     return GetState () == StateType::eStateStopped &&
40018fe6404SChaoren Lin         m_stop_info.reason == StopReason::eStopReasonBreakpoint;
40118fe6404SChaoren Lin }
402af245d11STodd Fiala 
40318fe6404SChaoren Lin bool
40418fe6404SChaoren Lin NativeThreadLinux::IsStoppedAtWatchpoint ()
40518fe6404SChaoren Lin {
40618fe6404SChaoren Lin     return GetState () == StateType::eStateStopped &&
40718fe6404SChaoren Lin         m_stop_info.reason == StopReason::eStopReasonWatchpoint;
408af245d11STodd Fiala }
409af245d11STodd Fiala 
410af245d11STodd Fiala void
41128e57429SChaoren Lin NativeThreadLinux::SetStoppedByTrace ()
41228e57429SChaoren Lin {
41328e57429SChaoren Lin     const StateType new_state = StateType::eStateStopped;
41428e57429SChaoren Lin     MaybeLogStateChange (new_state);
41528e57429SChaoren Lin     m_state = new_state;
41628e57429SChaoren Lin 
41728e57429SChaoren Lin     m_stop_info.reason = StopReason::eStopReasonTrace;
41828e57429SChaoren Lin     m_stop_info.details.signal.signo = SIGTRAP;
41928e57429SChaoren Lin }
42028e57429SChaoren Lin 
42128e57429SChaoren Lin void
42228e57429SChaoren Lin NativeThreadLinux::SetCrashedWithException (const siginfo_t& info)
423af245d11STodd Fiala {
424af245d11STodd Fiala     const StateType new_state = StateType::eStateCrashed;
425af245d11STodd Fiala     MaybeLogStateChange (new_state);
426af245d11STodd Fiala     m_state = new_state;
427af245d11STodd Fiala 
428af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonException;
42928e57429SChaoren Lin     m_stop_info.details.signal.signo = info.si_signo;
430af245d11STodd Fiala 
43128e57429SChaoren Lin     const auto reason = GetCrashReason (info);
43228e57429SChaoren Lin     m_stop_description = GetCrashReasonString (reason, reinterpret_cast<lldb::addr_t> (info.si_addr));
43328e57429SChaoren Lin }
434af245d11STodd Fiala 
435af245d11STodd Fiala void
436af245d11STodd Fiala NativeThreadLinux::SetSuspended ()
437af245d11STodd Fiala {
438af245d11STodd Fiala     const StateType new_state = StateType::eStateSuspended;
439af245d11STodd Fiala     MaybeLogStateChange (new_state);
440af245d11STodd Fiala     m_state = new_state;
441af245d11STodd Fiala 
442af245d11STodd Fiala     // FIXME what makes sense here? Do we need a suspended StopReason?
443af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonNone;
444af245d11STodd Fiala }
445af245d11STodd Fiala 
446af245d11STodd Fiala void
447af245d11STodd Fiala NativeThreadLinux::SetExited ()
448af245d11STodd Fiala {
449af245d11STodd Fiala     const StateType new_state = StateType::eStateExited;
450af245d11STodd Fiala     MaybeLogStateChange (new_state);
451af245d11STodd Fiala     m_state = new_state;
452af245d11STodd Fiala 
453af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonThreadExiting;
454af245d11STodd Fiala }
455af245d11STodd Fiala 
456af245d11STodd Fiala void
457af245d11STodd Fiala NativeThreadLinux::MaybeLogStateChange (lldb::StateType new_state)
458af245d11STodd Fiala {
459af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
460af245d11STodd Fiala     // If we're not logging, we're done.
461af245d11STodd Fiala     if (!log)
462af245d11STodd Fiala         return;
463af245d11STodd Fiala 
464af245d11STodd Fiala     // If this is a state change to the same state, we're done.
465af245d11STodd Fiala     lldb::StateType old_state = m_state;
466af245d11STodd Fiala     if (new_state == old_state)
467af245d11STodd Fiala         return;
468af245d11STodd Fiala 
469af245d11STodd Fiala     NativeProcessProtocolSP m_process_sp = m_process_wp.lock ();
470af245d11STodd Fiala     lldb::pid_t pid = m_process_sp ? m_process_sp->GetID () : LLDB_INVALID_PROCESS_ID;
471af245d11STodd Fiala 
472af245d11STodd Fiala     // Log it.
473af245d11STodd Fiala     log->Printf ("NativeThreadLinux: thread (pid=%" PRIu64 ", tid=%" PRIu64 ") changing from state %s to %s", pid, GetID (), StateAsCString (old_state), StateAsCString (new_state));
474af245d11STodd Fiala }
475