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");
22118fe6404SChaoren Lin     Error error = RemoveWatchpoint(addr);
22218fe6404SChaoren Lin     if (error.Fail()) return error;
22318fe6404SChaoren Lin     NativeRegisterContextSP reg_ctx = GetRegisterContext ();
22418fe6404SChaoren Lin     uint32_t wp_index =
22518fe6404SChaoren Lin         reg_ctx->SetHardwareWatchpoint (addr, size, watch_flags);
22618fe6404SChaoren Lin     if (wp_index == LLDB_INVALID_INDEX32)
22718fe6404SChaoren Lin         return Error ("Setting hardware watchpoint failed.");
22818fe6404SChaoren Lin     m_watchpoint_index_map.insert({addr, wp_index});
22918fe6404SChaoren Lin     return Error ();
230af245d11STodd Fiala }
231af245d11STodd Fiala 
232af245d11STodd Fiala Error
233af245d11STodd Fiala NativeThreadLinux::RemoveWatchpoint (lldb::addr_t addr)
234af245d11STodd Fiala {
23518fe6404SChaoren Lin     auto wp = m_watchpoint_index_map.find(addr);
23618fe6404SChaoren Lin     if (wp == m_watchpoint_index_map.end())
23718fe6404SChaoren Lin         return Error ();
23818fe6404SChaoren Lin     uint32_t wp_index = wp->second;
23918fe6404SChaoren Lin     m_watchpoint_index_map.erase(wp);
24018fe6404SChaoren Lin     if (GetRegisterContext()->ClearHardwareWatchpoint(wp_index))
24118fe6404SChaoren Lin         return Error ();
24218fe6404SChaoren Lin     return Error ("Clearing hardware watchpoint failed.");
243af245d11STodd Fiala }
244af245d11STodd Fiala 
245af245d11STodd Fiala void
246af245d11STodd Fiala NativeThreadLinux::SetLaunching ()
247af245d11STodd Fiala {
248af245d11STodd Fiala     const StateType new_state = StateType::eStateLaunching;
249af245d11STodd Fiala     MaybeLogStateChange (new_state);
250af245d11STodd Fiala     m_state = new_state;
251af245d11STodd Fiala 
252af245d11STodd Fiala     // Also mark it as stopped since launching temporarily stops the newly created thread
253af245d11STodd Fiala     // in the ptrace machinery.
254af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonSignal;
255af245d11STodd Fiala     m_stop_info.details.signal.signo = SIGSTOP;
256af245d11STodd Fiala }
257af245d11STodd Fiala 
258af245d11STodd Fiala 
259af245d11STodd Fiala void
260af245d11STodd Fiala NativeThreadLinux::SetRunning ()
261af245d11STodd Fiala {
262af245d11STodd Fiala     const StateType new_state = StateType::eStateRunning;
263af245d11STodd Fiala     MaybeLogStateChange (new_state);
264af245d11STodd Fiala     m_state = new_state;
265af245d11STodd Fiala 
266af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonNone;
26728e57429SChaoren Lin     m_stop_description.clear();
26818fe6404SChaoren Lin 
26918fe6404SChaoren Lin     // If watchpoints have been set, but none on this thread,
27018fe6404SChaoren Lin     // then this is a new thread. So set all existing watchpoints.
27118fe6404SChaoren Lin     if (m_watchpoint_index_map.empty())
27218fe6404SChaoren Lin     {
273*8bc34f4dSOleksiy Vyalov         const auto process_sp = GetProcess();
274*8bc34f4dSOleksiy Vyalov         if (process_sp)
275*8bc34f4dSOleksiy Vyalov         {
276*8bc34f4dSOleksiy Vyalov             const auto &watchpoint_map = process_sp->GetWatchpointMap();
27718fe6404SChaoren Lin             if (watchpoint_map.empty()) return;
27818fe6404SChaoren Lin             GetRegisterContext()->ClearAllHardwareWatchpoints();
27918fe6404SChaoren Lin             for (const auto &pair : watchpoint_map)
28018fe6404SChaoren Lin             {
28118fe6404SChaoren Lin                 const auto& wp = pair.second;
28218fe6404SChaoren Lin                 SetWatchpoint(wp.m_addr, wp.m_size, wp.m_watch_flags, wp.m_hardware);
28318fe6404SChaoren Lin             }
28418fe6404SChaoren Lin         }
285af245d11STodd Fiala     }
286*8bc34f4dSOleksiy Vyalov }
287af245d11STodd Fiala 
288af245d11STodd Fiala void
289af245d11STodd Fiala NativeThreadLinux::SetStepping ()
290af245d11STodd Fiala {
291af245d11STodd Fiala     const StateType new_state = StateType::eStateStepping;
292af245d11STodd Fiala     MaybeLogStateChange (new_state);
293af245d11STodd Fiala     m_state = new_state;
294af245d11STodd Fiala 
295af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonNone;
296af245d11STodd Fiala }
297af245d11STodd Fiala 
298af245d11STodd Fiala void
299af245d11STodd Fiala NativeThreadLinux::SetStoppedBySignal (uint32_t signo)
300af245d11STodd Fiala {
301af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
302af245d11STodd Fiala     if (log)
303b8af31d4SChaoren Lin         log->Printf ("NativeThreadLinux::%s called with signal 0x%02" PRIx32, __FUNCTION__, signo);
304af245d11STodd Fiala 
305af245d11STodd Fiala     const StateType new_state = StateType::eStateStopped;
306af245d11STodd Fiala     MaybeLogStateChange (new_state);
307af245d11STodd Fiala     m_state = new_state;
308af245d11STodd Fiala 
309af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonSignal;
310af245d11STodd Fiala     m_stop_info.details.signal.signo = signo;
311af245d11STodd Fiala }
312af245d11STodd Fiala 
313511e5cdcSTodd Fiala bool
314511e5cdcSTodd Fiala NativeThreadLinux::IsStopped (int *signo)
315511e5cdcSTodd Fiala {
316511e5cdcSTodd Fiala     if (!StateIsStoppedState (m_state, false))
317511e5cdcSTodd Fiala         return false;
318511e5cdcSTodd Fiala 
319511e5cdcSTodd Fiala     // If we are stopped by a signal, return the signo.
320511e5cdcSTodd Fiala     if (signo &&
321511e5cdcSTodd Fiala         m_state == StateType::eStateStopped &&
322511e5cdcSTodd Fiala         m_stop_info.reason == StopReason::eStopReasonSignal)
323511e5cdcSTodd Fiala     {
324511e5cdcSTodd Fiala         *signo = m_stop_info.details.signal.signo;
325511e5cdcSTodd Fiala     }
326511e5cdcSTodd Fiala 
327511e5cdcSTodd Fiala     // Regardless, we are stopped.
328511e5cdcSTodd Fiala     return true;
329511e5cdcSTodd Fiala }
330511e5cdcSTodd Fiala 
331511e5cdcSTodd Fiala 
332af245d11STodd Fiala void
333a9882ceeSTodd Fiala NativeThreadLinux::SetStoppedByExec ()
334a9882ceeSTodd Fiala {
335a9882ceeSTodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
336a9882ceeSTodd Fiala     if (log)
337a9882ceeSTodd Fiala         log->Printf ("NativeThreadLinux::%s()", __FUNCTION__);
338a9882ceeSTodd Fiala 
339a9882ceeSTodd Fiala     const StateType new_state = StateType::eStateStopped;
340a9882ceeSTodd Fiala     MaybeLogStateChange (new_state);
341a9882ceeSTodd Fiala     m_state = new_state;
342a9882ceeSTodd Fiala 
343a9882ceeSTodd Fiala     m_stop_info.reason = StopReason::eStopReasonExec;
344a9882ceeSTodd Fiala     m_stop_info.details.signal.signo = SIGSTOP;
345a9882ceeSTodd Fiala }
346a9882ceeSTodd Fiala 
347a9882ceeSTodd Fiala void
348af245d11STodd Fiala NativeThreadLinux::SetStoppedByBreakpoint ()
349af245d11STodd Fiala {
350af245d11STodd Fiala     const StateType new_state = StateType::eStateStopped;
351af245d11STodd Fiala     MaybeLogStateChange (new_state);
352af245d11STodd Fiala     m_state = new_state;
353af245d11STodd Fiala 
35428e57429SChaoren Lin     m_stop_info.reason = StopReason::eStopReasonBreakpoint;
355af245d11STodd Fiala     m_stop_info.details.signal.signo = SIGTRAP;
35618fe6404SChaoren Lin     m_stop_description.clear();
35718fe6404SChaoren Lin }
35818fe6404SChaoren Lin 
35918fe6404SChaoren Lin void
36018fe6404SChaoren Lin NativeThreadLinux::SetStoppedByWatchpoint ()
36118fe6404SChaoren Lin {
36218fe6404SChaoren Lin     const StateType new_state = StateType::eStateStopped;
36318fe6404SChaoren Lin     MaybeLogStateChange (new_state);
36418fe6404SChaoren Lin     m_state = new_state;
36518fe6404SChaoren Lin 
36618fe6404SChaoren Lin     m_stop_info.reason = StopReason::eStopReasonWatchpoint;
36718fe6404SChaoren Lin     m_stop_info.details.signal.signo = SIGTRAP;
36818fe6404SChaoren Lin 
36918fe6404SChaoren Lin     NativeRegisterContextLinux_x86_64 *reg_ctx =
37018fe6404SChaoren Lin         reinterpret_cast<NativeRegisterContextLinux_x86_64*> (GetRegisterContext().get());
37118fe6404SChaoren Lin     const uint32_t num_hw_watchpoints =
37218fe6404SChaoren Lin         reg_ctx->NumSupportedHardwareWatchpoints();
37318fe6404SChaoren Lin 
37418fe6404SChaoren Lin     m_stop_description.clear ();
37518fe6404SChaoren Lin     for (uint32_t wp_index = 0; wp_index < num_hw_watchpoints; ++wp_index)
37618fe6404SChaoren Lin         if (reg_ctx->IsWatchpointHit(wp_index).Success())
37718fe6404SChaoren Lin         {
37818fe6404SChaoren Lin             std::ostringstream ostr;
37918fe6404SChaoren Lin             ostr << reg_ctx->GetWatchpointAddress(wp_index) << " " << wp_index;
38018fe6404SChaoren Lin             m_stop_description = ostr.str();
38118fe6404SChaoren Lin             return;
38218fe6404SChaoren Lin         }
38318fe6404SChaoren Lin     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
38418fe6404SChaoren Lin     if (log)
38518fe6404SChaoren Lin     {
38618fe6404SChaoren Lin         NativeProcessProtocolSP m_process_sp = m_process_wp.lock ();
38718fe6404SChaoren Lin         lldb::pid_t pid = m_process_sp ? m_process_sp->GetID () : LLDB_INVALID_PROCESS_ID;
38818fe6404SChaoren Lin         log->Printf ("NativeThreadLinux: thread (pid=%" PRIu64 ", tid=%" PRIu64 ") "
38918fe6404SChaoren Lin                 "stopped by a watchpoint, but failed to find it",
39018fe6404SChaoren Lin                 pid, GetID ());
39118fe6404SChaoren Lin     }
392af245d11STodd Fiala }
393af245d11STodd Fiala 
394af245d11STodd Fiala bool
395af245d11STodd Fiala NativeThreadLinux::IsStoppedAtBreakpoint ()
396af245d11STodd Fiala {
39718fe6404SChaoren Lin     return GetState () == StateType::eStateStopped &&
39818fe6404SChaoren Lin         m_stop_info.reason == StopReason::eStopReasonBreakpoint;
39918fe6404SChaoren Lin }
400af245d11STodd Fiala 
40118fe6404SChaoren Lin bool
40218fe6404SChaoren Lin NativeThreadLinux::IsStoppedAtWatchpoint ()
40318fe6404SChaoren Lin {
40418fe6404SChaoren Lin     return GetState () == StateType::eStateStopped &&
40518fe6404SChaoren Lin         m_stop_info.reason == StopReason::eStopReasonWatchpoint;
406af245d11STodd Fiala }
407af245d11STodd Fiala 
408af245d11STodd Fiala void
40928e57429SChaoren Lin NativeThreadLinux::SetStoppedByTrace ()
41028e57429SChaoren Lin {
41128e57429SChaoren Lin     const StateType new_state = StateType::eStateStopped;
41228e57429SChaoren Lin     MaybeLogStateChange (new_state);
41328e57429SChaoren Lin     m_state = new_state;
41428e57429SChaoren Lin 
41528e57429SChaoren Lin     m_stop_info.reason = StopReason::eStopReasonTrace;
41628e57429SChaoren Lin     m_stop_info.details.signal.signo = SIGTRAP;
41728e57429SChaoren Lin }
41828e57429SChaoren Lin 
41928e57429SChaoren Lin void
42028e57429SChaoren Lin NativeThreadLinux::SetCrashedWithException (const siginfo_t& info)
421af245d11STodd Fiala {
422af245d11STodd Fiala     const StateType new_state = StateType::eStateCrashed;
423af245d11STodd Fiala     MaybeLogStateChange (new_state);
424af245d11STodd Fiala     m_state = new_state;
425af245d11STodd Fiala 
426af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonException;
42728e57429SChaoren Lin     m_stop_info.details.signal.signo = info.si_signo;
428af245d11STodd Fiala 
42928e57429SChaoren Lin     const auto reason = GetCrashReason (info);
43028e57429SChaoren Lin     m_stop_description = GetCrashReasonString (reason, reinterpret_cast<lldb::addr_t> (info.si_addr));
43128e57429SChaoren Lin }
432af245d11STodd Fiala 
433af245d11STodd Fiala void
434af245d11STodd Fiala NativeThreadLinux::SetSuspended ()
435af245d11STodd Fiala {
436af245d11STodd Fiala     const StateType new_state = StateType::eStateSuspended;
437af245d11STodd Fiala     MaybeLogStateChange (new_state);
438af245d11STodd Fiala     m_state = new_state;
439af245d11STodd Fiala 
440af245d11STodd Fiala     // FIXME what makes sense here? Do we need a suspended StopReason?
441af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonNone;
442af245d11STodd Fiala }
443af245d11STodd Fiala 
444af245d11STodd Fiala void
445af245d11STodd Fiala NativeThreadLinux::SetExited ()
446af245d11STodd Fiala {
447af245d11STodd Fiala     const StateType new_state = StateType::eStateExited;
448af245d11STodd Fiala     MaybeLogStateChange (new_state);
449af245d11STodd Fiala     m_state = new_state;
450af245d11STodd Fiala 
451af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonThreadExiting;
452af245d11STodd Fiala }
453af245d11STodd Fiala 
454af245d11STodd Fiala void
455af245d11STodd Fiala NativeThreadLinux::MaybeLogStateChange (lldb::StateType new_state)
456af245d11STodd Fiala {
457af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
458af245d11STodd Fiala     // If we're not logging, we're done.
459af245d11STodd Fiala     if (!log)
460af245d11STodd Fiala         return;
461af245d11STodd Fiala 
462af245d11STodd Fiala     // If this is a state change to the same state, we're done.
463af245d11STodd Fiala     lldb::StateType old_state = m_state;
464af245d11STodd Fiala     if (new_state == old_state)
465af245d11STodd Fiala         return;
466af245d11STodd Fiala 
467af245d11STodd Fiala     NativeProcessProtocolSP m_process_sp = m_process_wp.lock ();
468af245d11STodd Fiala     lldb::pid_t pid = m_process_sp ? m_process_sp->GetID () : LLDB_INVALID_PROCESS_ID;
469af245d11STodd Fiala 
470af245d11STodd Fiala     // Log it.
471af245d11STodd Fiala     log->Printf ("NativeThreadLinux: thread (pid=%" PRIu64 ", tid=%" PRIu64 ") changing from state %s to %s", pid, GetID (), StateAsCString (old_state), StateAsCString (new_state));
472af245d11STodd Fiala }
473