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"
25af245d11STodd Fiala #include "lldb/lldb-enumerations.h"
26af245d11STodd Fiala #include "lldb/lldb-private-log.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;
40af245d11STodd Fiala 
41af245d11STodd Fiala namespace
42af245d11STodd Fiala {
43af245d11STodd Fiala     void LogThreadStopInfo (Log &log, const ThreadStopInfo &stop_info, const char *const header)
44af245d11STodd Fiala     {
45af245d11STodd Fiala         switch (stop_info.reason)
46af245d11STodd Fiala         {
47af245d11STodd Fiala             case eStopReasonSignal:
48ae29d395SChaoren Lin                 log.Printf ("%s: %s signal 0x%02" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo);
49af245d11STodd Fiala                 return;
50af245d11STodd Fiala             case eStopReasonException:
51ae29d395SChaoren Lin                 log.Printf ("%s: %s exception type 0x%02" PRIx64, __FUNCTION__, header, stop_info.details.exception.type);
52a9882ceeSTodd Fiala                 return;
53a9882ceeSTodd Fiala             case eStopReasonExec:
54a9882ceeSTodd Fiala                 log.Printf ("%s: %s exec, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo);
55af245d11STodd Fiala                 return;
56af245d11STodd Fiala             default:
57a9882ceeSTodd Fiala                 log.Printf ("%s: %s invalid stop reason %" PRIu32, __FUNCTION__, header, static_cast<uint32_t> (stop_info.reason));
58af245d11STodd Fiala         }
59af245d11STodd Fiala     }
60af245d11STodd Fiala }
61af245d11STodd Fiala 
62af245d11STodd Fiala NativeThreadLinux::NativeThreadLinux (NativeProcessLinux *process, lldb::tid_t tid) :
63af245d11STodd Fiala     NativeThreadProtocol (process, tid),
64af245d11STodd Fiala     m_state (StateType::eStateInvalid),
65af245d11STodd Fiala     m_stop_info (),
6628e57429SChaoren Lin     m_reg_context_sp (),
6728e57429SChaoren Lin     m_stop_description ()
68af245d11STodd Fiala {
69af245d11STodd Fiala }
70af245d11STodd Fiala 
717206c6d1STodd Fiala std::string
72af245d11STodd Fiala NativeThreadLinux::GetName()
73af245d11STodd Fiala {
74af245d11STodd Fiala     NativeProcessProtocolSP process_sp = m_process_wp.lock ();
75af245d11STodd Fiala     if (!process_sp)
76af245d11STodd Fiala         return "<unknown: no process>";
77af245d11STodd Fiala 
78af245d11STodd Fiala     // const NativeProcessLinux *const process = reinterpret_cast<NativeProcessLinux*> (process_sp->get ());
7939de3110SZachary Turner     llvm::SmallString<32> thread_name;
8039de3110SZachary Turner     HostNativeThread::GetName(GetID(), thread_name);
8139de3110SZachary Turner     return thread_name.c_str();
82af245d11STodd Fiala }
83af245d11STodd Fiala 
84af245d11STodd Fiala lldb::StateType
85af245d11STodd Fiala NativeThreadLinux::GetState ()
86af245d11STodd Fiala {
87af245d11STodd Fiala     return m_state;
88af245d11STodd Fiala }
89af245d11STodd Fiala 
90af245d11STodd Fiala 
91af245d11STodd Fiala bool
9228e57429SChaoren Lin NativeThreadLinux::GetStopReason (ThreadStopInfo &stop_info, std::string& description)
93af245d11STodd Fiala {
94af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
9528e57429SChaoren Lin 
9628e57429SChaoren Lin     description.clear();
9728e57429SChaoren Lin 
98af245d11STodd Fiala     switch (m_state)
99af245d11STodd Fiala     {
100af245d11STodd Fiala     case eStateStopped:
101af245d11STodd Fiala     case eStateCrashed:
102af245d11STodd Fiala     case eStateExited:
103af245d11STodd Fiala     case eStateSuspended:
104af245d11STodd Fiala     case eStateUnloaded:
105af245d11STodd Fiala         if (log)
106af245d11STodd Fiala             LogThreadStopInfo (*log, m_stop_info, "m_stop_info in thread:");
107af245d11STodd Fiala         stop_info = m_stop_info;
10818fe6404SChaoren Lin         switch (m_stop_info.reason)
10918fe6404SChaoren Lin         {
11018fe6404SChaoren Lin             case StopReason::eStopReasonException:
11118fe6404SChaoren Lin             case StopReason::eStopReasonBreakpoint:
11218fe6404SChaoren Lin             case StopReason::eStopReasonWatchpoint:
11328e57429SChaoren Lin                 description = m_stop_description;
11418fe6404SChaoren Lin             default:
11518fe6404SChaoren Lin                 break;
11618fe6404SChaoren Lin         }
117af245d11STodd Fiala         if (log)
118af245d11STodd Fiala             LogThreadStopInfo (*log, stop_info, "returned stop_info:");
11928e57429SChaoren Lin 
120af245d11STodd Fiala         return true;
121af245d11STodd Fiala 
122af245d11STodd Fiala     case eStateInvalid:
123af245d11STodd Fiala     case eStateConnected:
124af245d11STodd Fiala     case eStateAttaching:
125af245d11STodd Fiala     case eStateLaunching:
126af245d11STodd Fiala     case eStateRunning:
127af245d11STodd Fiala     case eStateStepping:
128af245d11STodd Fiala     case eStateDetached:
129af245d11STodd Fiala         if (log)
130af245d11STodd Fiala         {
131af245d11STodd Fiala             log->Printf ("NativeThreadLinux::%s tid %" PRIu64 " in state %s cannot answer stop reason",
132af245d11STodd Fiala                     __FUNCTION__, GetID (), StateAsCString (m_state));
133af245d11STodd Fiala         }
134af245d11STodd Fiala         return false;
135af245d11STodd Fiala     }
1368faf9370SDavid Majnemer     llvm_unreachable("unhandled StateType!");
137af245d11STodd Fiala }
138af245d11STodd Fiala 
139af245d11STodd Fiala lldb_private::NativeRegisterContextSP
140af245d11STodd Fiala NativeThreadLinux::GetRegisterContext ()
141af245d11STodd Fiala {
142af245d11STodd Fiala     // Return the register context if we already created it.
143af245d11STodd Fiala     if (m_reg_context_sp)
144af245d11STodd Fiala         return m_reg_context_sp;
145af245d11STodd Fiala 
146af245d11STodd Fiala     // First select the appropriate RegisterInfoInterface.
147af245d11STodd Fiala     RegisterInfoInterface *reg_interface = nullptr;
148af245d11STodd Fiala     NativeProcessProtocolSP m_process_sp = m_process_wp.lock ();
149af245d11STodd Fiala     if (!m_process_sp)
150af245d11STodd Fiala         return NativeRegisterContextSP ();
151af245d11STodd Fiala 
152af245d11STodd Fiala     ArchSpec target_arch;
153af245d11STodd Fiala     if (!m_process_sp->GetArchitecture (target_arch))
154af245d11STodd Fiala         return NativeRegisterContextSP ();
155af245d11STodd Fiala 
156af245d11STodd Fiala     switch (target_arch.GetTriple().getOS())
157af245d11STodd Fiala     {
158af245d11STodd Fiala         case llvm::Triple::Linux:
159af245d11STodd Fiala             switch (target_arch.GetMachine())
160af245d11STodd Fiala             {
161b71e89e9STodd Fiala             case llvm::Triple::aarch64:
162b71e89e9STodd Fiala                 assert((HostInfo::GetArchitecture ().GetAddressByteSize() == 8) && "Register setting path assumes this is a 64-bit host");
163b71e89e9STodd Fiala                 reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_arm64(target_arch));
164b71e89e9STodd Fiala                 break;
165af245d11STodd Fiala             case llvm::Triple::x86:
166af245d11STodd Fiala             case llvm::Triple::x86_64:
16713b18261SZachary Turner                 if (HostInfo::GetArchitecture().GetAddressByteSize() == 4)
168af245d11STodd Fiala                 {
169af245d11STodd Fiala                     // 32-bit hosts run with a RegisterContextLinux_i386 context.
170af245d11STodd Fiala                     reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_i386(target_arch));
171af245d11STodd Fiala                 }
172af245d11STodd Fiala                 else
173af245d11STodd Fiala                 {
17413b18261SZachary Turner                     assert((HostInfo::GetArchitecture().GetAddressByteSize() == 8) &&
17513b18261SZachary Turner                            "Register setting path assumes this is a 64-bit host");
176af245d11STodd Fiala                     // X86_64 hosts know how to work with 64-bit and 32-bit EXEs using the x86_64 register context.
177af245d11STodd Fiala                     reg_interface = static_cast<RegisterInfoInterface*> (new RegisterContextLinux_x86_64 (target_arch));
178af245d11STodd Fiala                 }
179af245d11STodd Fiala                 break;
1803df471c3SMohit K. Bhakkad             case llvm::Triple::mips64:
1813df471c3SMohit K. Bhakkad             case llvm::Triple::mips64el:
1823df471c3SMohit K. Bhakkad                 assert((HostInfo::GetArchitecture ().GetAddressByteSize() == 8)
1833df471c3SMohit K. Bhakkad                     && "Register setting path assumes this is a 64-bit host");
1843df471c3SMohit K. Bhakkad                 reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_mips64 (target_arch));
1853df471c3SMohit K. Bhakkad                 break;
186af245d11STodd Fiala             default:
187af245d11STodd Fiala                 break;
188af245d11STodd Fiala             }
189af245d11STodd Fiala             break;
190af245d11STodd Fiala         default:
191af245d11STodd Fiala             break;
192af245d11STodd Fiala     }
193af245d11STodd Fiala 
194af245d11STodd Fiala     assert(reg_interface && "OS or CPU not supported!");
195af245d11STodd Fiala     if (!reg_interface)
196af245d11STodd Fiala         return NativeRegisterContextSP ();
197af245d11STodd Fiala 
198af245d11STodd Fiala     // Now create the register context.
199af245d11STodd Fiala     switch (target_arch.GetMachine())
200af245d11STodd Fiala     {
201af245d11STodd Fiala #if 0
202af245d11STodd Fiala         case llvm::Triple::mips64:
203af245d11STodd Fiala         {
204af245d11STodd Fiala             RegisterContextPOSIXProcessMonitor_mips64 *reg_ctx = new RegisterContextPOSIXProcessMonitor_mips64(*this, 0, reg_interface);
205af245d11STodd Fiala             m_posix_thread = reg_ctx;
206af245d11STodd Fiala             m_reg_context_sp.reset(reg_ctx);
207af245d11STodd Fiala             break;
208af245d11STodd Fiala         }
209af245d11STodd Fiala #endif
2103df471c3SMohit K. Bhakkad         case llvm::Triple::mips64:
2113df471c3SMohit K. Bhakkad         case llvm::Triple::mips64el:
2123df471c3SMohit K. Bhakkad         {
2133df471c3SMohit K. Bhakkad             const uint32_t concrete_frame_idx = 0;
2143df471c3SMohit K. Bhakkad             m_reg_context_sp.reset (new NativeRegisterContextLinux_mips64 (*this, concrete_frame_idx, reg_interface));
2153df471c3SMohit K. Bhakkad             break;
2163df471c3SMohit K. Bhakkad         }
2171e209fccSTamas Berghammer         case llvm::Triple::aarch64:
2181e209fccSTamas Berghammer         {
2191e209fccSTamas Berghammer             const uint32_t concrete_frame_idx = 0;
2201e209fccSTamas Berghammer             m_reg_context_sp.reset (new NativeRegisterContextLinux_arm64(*this, concrete_frame_idx, reg_interface));
2211e209fccSTamas Berghammer             break;
2221e209fccSTamas Berghammer         }
223af245d11STodd Fiala         case llvm::Triple::x86:
224af245d11STodd Fiala         case llvm::Triple::x86_64:
225af245d11STodd Fiala         {
226af245d11STodd Fiala             const uint32_t concrete_frame_idx = 0;
227af245d11STodd Fiala             m_reg_context_sp.reset (new NativeRegisterContextLinux_x86_64(*this, concrete_frame_idx, reg_interface));
228af245d11STodd Fiala             break;
229af245d11STodd Fiala         }
230af245d11STodd Fiala         default:
231af245d11STodd Fiala             break;
232af245d11STodd Fiala     }
233af245d11STodd Fiala 
234af245d11STodd Fiala     return m_reg_context_sp;
235af245d11STodd Fiala }
236af245d11STodd Fiala 
237af245d11STodd Fiala Error
238af245d11STodd Fiala NativeThreadLinux::SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware)
239af245d11STodd Fiala {
24018fe6404SChaoren Lin     if (!hardware)
241af245d11STodd Fiala         return Error ("not implemented");
242f591f69fSChaoren Lin     if (m_state == eStateLaunching)
243f591f69fSChaoren Lin         return Error ();
24418fe6404SChaoren Lin     Error error = RemoveWatchpoint(addr);
24518fe6404SChaoren Lin     if (error.Fail()) return error;
24618fe6404SChaoren Lin     NativeRegisterContextSP reg_ctx = GetRegisterContext ();
24718fe6404SChaoren Lin     uint32_t wp_index =
24818fe6404SChaoren Lin         reg_ctx->SetHardwareWatchpoint (addr, size, watch_flags);
24918fe6404SChaoren Lin     if (wp_index == LLDB_INVALID_INDEX32)
25018fe6404SChaoren Lin         return Error ("Setting hardware watchpoint failed.");
25118fe6404SChaoren Lin     m_watchpoint_index_map.insert({addr, wp_index});
25218fe6404SChaoren Lin     return Error ();
253af245d11STodd Fiala }
254af245d11STodd Fiala 
255af245d11STodd Fiala Error
256af245d11STodd Fiala NativeThreadLinux::RemoveWatchpoint (lldb::addr_t addr)
257af245d11STodd Fiala {
25818fe6404SChaoren Lin     auto wp = m_watchpoint_index_map.find(addr);
25918fe6404SChaoren Lin     if (wp == m_watchpoint_index_map.end())
26018fe6404SChaoren Lin         return Error ();
26118fe6404SChaoren Lin     uint32_t wp_index = wp->second;
26218fe6404SChaoren Lin     m_watchpoint_index_map.erase(wp);
26318fe6404SChaoren Lin     if (GetRegisterContext()->ClearHardwareWatchpoint(wp_index))
26418fe6404SChaoren Lin         return Error ();
26518fe6404SChaoren Lin     return Error ("Clearing hardware watchpoint failed.");
266af245d11STodd Fiala }
267af245d11STodd Fiala 
268af245d11STodd Fiala void
269af245d11STodd Fiala NativeThreadLinux::SetLaunching ()
270af245d11STodd Fiala {
271af245d11STodd Fiala     const StateType new_state = StateType::eStateLaunching;
272af245d11STodd Fiala     MaybeLogStateChange (new_state);
273af245d11STodd Fiala     m_state = new_state;
274af245d11STodd Fiala 
275af245d11STodd Fiala     // Also mark it as stopped since launching temporarily stops the newly created thread
276af245d11STodd Fiala     // in the ptrace machinery.
277af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonSignal;
278af245d11STodd Fiala     m_stop_info.details.signal.signo = SIGSTOP;
279af245d11STodd Fiala }
280af245d11STodd Fiala 
281af245d11STodd Fiala 
282af245d11STodd Fiala void
283af245d11STodd Fiala NativeThreadLinux::SetRunning ()
284af245d11STodd Fiala {
285af245d11STodd Fiala     const StateType new_state = StateType::eStateRunning;
286af245d11STodd Fiala     MaybeLogStateChange (new_state);
287af245d11STodd Fiala     m_state = new_state;
288af245d11STodd Fiala 
289af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonNone;
29028e57429SChaoren Lin     m_stop_description.clear();
29118fe6404SChaoren Lin 
29218fe6404SChaoren Lin     // If watchpoints have been set, but none on this thread,
29318fe6404SChaoren Lin     // then this is a new thread. So set all existing watchpoints.
29418fe6404SChaoren Lin     if (m_watchpoint_index_map.empty())
29518fe6404SChaoren Lin     {
2968bc34f4dSOleksiy Vyalov         const auto process_sp = GetProcess();
2978bc34f4dSOleksiy Vyalov         if (process_sp)
2988bc34f4dSOleksiy Vyalov         {
2998bc34f4dSOleksiy Vyalov             const auto &watchpoint_map = process_sp->GetWatchpointMap();
30018fe6404SChaoren Lin             if (watchpoint_map.empty()) return;
30118fe6404SChaoren Lin             GetRegisterContext()->ClearAllHardwareWatchpoints();
30218fe6404SChaoren Lin             for (const auto &pair : watchpoint_map)
30318fe6404SChaoren Lin             {
30418fe6404SChaoren Lin                 const auto& wp = pair.second;
30518fe6404SChaoren Lin                 SetWatchpoint(wp.m_addr, wp.m_size, wp.m_watch_flags, wp.m_hardware);
30618fe6404SChaoren Lin             }
30718fe6404SChaoren Lin         }
308af245d11STodd Fiala     }
3098bc34f4dSOleksiy Vyalov }
310af245d11STodd Fiala 
311af245d11STodd Fiala void
312af245d11STodd Fiala NativeThreadLinux::SetStepping ()
313af245d11STodd Fiala {
314af245d11STodd Fiala     const StateType new_state = StateType::eStateStepping;
315af245d11STodd Fiala     MaybeLogStateChange (new_state);
316af245d11STodd Fiala     m_state = new_state;
317af245d11STodd Fiala 
318af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonNone;
319af245d11STodd Fiala }
320af245d11STodd Fiala 
321af245d11STodd Fiala void
322af245d11STodd Fiala NativeThreadLinux::SetStoppedBySignal (uint32_t signo)
323af245d11STodd Fiala {
324af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
325af245d11STodd Fiala     if (log)
326b8af31d4SChaoren Lin         log->Printf ("NativeThreadLinux::%s called with signal 0x%02" PRIx32, __FUNCTION__, signo);
327af245d11STodd Fiala 
328af245d11STodd Fiala     const StateType new_state = StateType::eStateStopped;
329af245d11STodd Fiala     MaybeLogStateChange (new_state);
330af245d11STodd Fiala     m_state = new_state;
331af245d11STodd Fiala 
332af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonSignal;
333af245d11STodd Fiala     m_stop_info.details.signal.signo = signo;
334af245d11STodd Fiala }
335af245d11STodd Fiala 
336511e5cdcSTodd Fiala bool
337511e5cdcSTodd Fiala NativeThreadLinux::IsStopped (int *signo)
338511e5cdcSTodd Fiala {
339511e5cdcSTodd Fiala     if (!StateIsStoppedState (m_state, false))
340511e5cdcSTodd Fiala         return false;
341511e5cdcSTodd Fiala 
342511e5cdcSTodd Fiala     // If we are stopped by a signal, return the signo.
343511e5cdcSTodd Fiala     if (signo &&
344511e5cdcSTodd Fiala         m_state == StateType::eStateStopped &&
345511e5cdcSTodd Fiala         m_stop_info.reason == StopReason::eStopReasonSignal)
346511e5cdcSTodd Fiala     {
347511e5cdcSTodd Fiala         *signo = m_stop_info.details.signal.signo;
348511e5cdcSTodd Fiala     }
349511e5cdcSTodd Fiala 
350511e5cdcSTodd Fiala     // Regardless, we are stopped.
351511e5cdcSTodd Fiala     return true;
352511e5cdcSTodd Fiala }
353511e5cdcSTodd Fiala 
354511e5cdcSTodd Fiala 
355af245d11STodd Fiala void
356a9882ceeSTodd Fiala NativeThreadLinux::SetStoppedByExec ()
357a9882ceeSTodd Fiala {
358a9882ceeSTodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
359a9882ceeSTodd Fiala     if (log)
360a9882ceeSTodd Fiala         log->Printf ("NativeThreadLinux::%s()", __FUNCTION__);
361a9882ceeSTodd Fiala 
362a9882ceeSTodd Fiala     const StateType new_state = StateType::eStateStopped;
363a9882ceeSTodd Fiala     MaybeLogStateChange (new_state);
364a9882ceeSTodd Fiala     m_state = new_state;
365a9882ceeSTodd Fiala 
366a9882ceeSTodd Fiala     m_stop_info.reason = StopReason::eStopReasonExec;
367a9882ceeSTodd Fiala     m_stop_info.details.signal.signo = SIGSTOP;
368a9882ceeSTodd Fiala }
369a9882ceeSTodd Fiala 
370a9882ceeSTodd Fiala void
371af245d11STodd Fiala NativeThreadLinux::SetStoppedByBreakpoint ()
372af245d11STodd Fiala {
373af245d11STodd Fiala     const StateType new_state = StateType::eStateStopped;
374af245d11STodd Fiala     MaybeLogStateChange (new_state);
375af245d11STodd Fiala     m_state = new_state;
376af245d11STodd Fiala 
37728e57429SChaoren Lin     m_stop_info.reason = StopReason::eStopReasonBreakpoint;
378af245d11STodd Fiala     m_stop_info.details.signal.signo = SIGTRAP;
37918fe6404SChaoren Lin     m_stop_description.clear();
38018fe6404SChaoren Lin }
38118fe6404SChaoren Lin 
38218fe6404SChaoren Lin void
38318fe6404SChaoren Lin NativeThreadLinux::SetStoppedByWatchpoint ()
38418fe6404SChaoren Lin {
385*eadb2a9eSTamas Berghammer     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
386*eadb2a9eSTamas Berghammer     lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
387*eadb2a9eSTamas Berghammer     if (log)
388*eadb2a9eSTamas Berghammer     {
389*eadb2a9eSTamas Berghammer         NativeProcessProtocolSP process_sp = m_process_wp.lock ();
390*eadb2a9eSTamas Berghammer         if (process_sp)
391*eadb2a9eSTamas Berghammer             pid = process_sp->GetID ();
392*eadb2a9eSTamas Berghammer     }
393*eadb2a9eSTamas Berghammer 
39418fe6404SChaoren Lin     const StateType new_state = StateType::eStateStopped;
39518fe6404SChaoren Lin     MaybeLogStateChange (new_state);
39618fe6404SChaoren Lin     m_state = new_state;
39718fe6404SChaoren Lin 
398*eadb2a9eSTamas Berghammer     NativeRegisterContextSP reg_ctx = GetRegisterContext ();
399*eadb2a9eSTamas Berghammer     const uint32_t num_hw_watchpoints = reg_ctx->NumSupportedHardwareWatchpoints ();
40018fe6404SChaoren Lin 
40118fe6404SChaoren Lin     m_stop_description.clear ();
40218fe6404SChaoren Lin     for (uint32_t wp_index = 0; wp_index < num_hw_watchpoints; ++wp_index)
403*eadb2a9eSTamas Berghammer     {
40418fe6404SChaoren Lin         if (reg_ctx->IsWatchpointHit (wp_index).Success())
40518fe6404SChaoren Lin         {
406*eadb2a9eSTamas Berghammer             if (log)
407*eadb2a9eSTamas Berghammer                 log->Printf ("NativeThreadLinux:%s (pid=%" PRIu64 ", tid=%" PRIu64 ") watchpoint found with idx: %u",
408*eadb2a9eSTamas Berghammer                              __FUNCTION__, pid, GetID (), wp_index);
409*eadb2a9eSTamas Berghammer 
41018fe6404SChaoren Lin             std::ostringstream ostr;
41118fe6404SChaoren Lin             ostr << reg_ctx->GetWatchpointAddress (wp_index) << " " << wp_index;
41218fe6404SChaoren Lin             m_stop_description = ostr.str();
413*eadb2a9eSTamas Berghammer 
414*eadb2a9eSTamas Berghammer             m_stop_info.reason = StopReason::eStopReasonWatchpoint;
415*eadb2a9eSTamas Berghammer             m_stop_info.details.signal.signo = SIGTRAP;
41618fe6404SChaoren Lin             return;
41718fe6404SChaoren Lin         }
41818fe6404SChaoren Lin     }
419*eadb2a9eSTamas Berghammer 
420*eadb2a9eSTamas Berghammer     // The process reported a watchpoint was hit, but we haven't found the
421*eadb2a9eSTamas Berghammer     // watchpoint. Assume that a stopped by trace is reported as a hardware
422*eadb2a9eSTamas Berghammer     // watchpoint what happens on some linux kernels (e.g.: android-arm64
423*eadb2a9eSTamas Berghammer     // platfrom-21).
424*eadb2a9eSTamas Berghammer 
425*eadb2a9eSTamas Berghammer     if (log)
426*eadb2a9eSTamas Berghammer         log->Printf ("NativeThreadLinux:%s (pid=%" PRIu64 ", tid=%" PRIu64 ") none of the watchpoint was hit.",
427*eadb2a9eSTamas Berghammer                      __FUNCTION__, pid, GetID ());
428*eadb2a9eSTamas Berghammer 
429*eadb2a9eSTamas Berghammer     SetStoppedByTrace ();
430af245d11STodd Fiala }
431af245d11STodd Fiala 
432af245d11STodd Fiala bool
433af245d11STodd Fiala NativeThreadLinux::IsStoppedAtBreakpoint ()
434af245d11STodd Fiala {
43518fe6404SChaoren Lin     return GetState () == StateType::eStateStopped &&
43618fe6404SChaoren Lin         m_stop_info.reason == StopReason::eStopReasonBreakpoint;
43718fe6404SChaoren Lin }
438af245d11STodd Fiala 
43918fe6404SChaoren Lin bool
44018fe6404SChaoren Lin NativeThreadLinux::IsStoppedAtWatchpoint ()
44118fe6404SChaoren Lin {
44218fe6404SChaoren Lin     return GetState () == StateType::eStateStopped &&
44318fe6404SChaoren Lin         m_stop_info.reason == StopReason::eStopReasonWatchpoint;
444af245d11STodd Fiala }
445af245d11STodd Fiala 
446af245d11STodd Fiala void
44728e57429SChaoren Lin NativeThreadLinux::SetStoppedByTrace ()
44828e57429SChaoren Lin {
44928e57429SChaoren Lin     const StateType new_state = StateType::eStateStopped;
45028e57429SChaoren Lin     MaybeLogStateChange (new_state);
45128e57429SChaoren Lin     m_state = new_state;
45228e57429SChaoren Lin 
45328e57429SChaoren Lin     m_stop_info.reason = StopReason::eStopReasonTrace;
45428e57429SChaoren Lin     m_stop_info.details.signal.signo = SIGTRAP;
45528e57429SChaoren Lin }
45628e57429SChaoren Lin 
45728e57429SChaoren Lin void
45828e57429SChaoren Lin NativeThreadLinux::SetCrashedWithException (const siginfo_t& info)
459af245d11STodd Fiala {
460af245d11STodd Fiala     const StateType new_state = StateType::eStateCrashed;
461af245d11STodd Fiala     MaybeLogStateChange (new_state);
462af245d11STodd Fiala     m_state = new_state;
463af245d11STodd Fiala 
464af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonException;
46528e57429SChaoren Lin     m_stop_info.details.signal.signo = info.si_signo;
466af245d11STodd Fiala 
46728e57429SChaoren Lin     const auto reason = GetCrashReason (info);
46828e57429SChaoren Lin     m_stop_description = GetCrashReasonString (reason, reinterpret_cast<lldb::addr_t> (info.si_addr));
46928e57429SChaoren Lin }
470af245d11STodd Fiala 
471af245d11STodd Fiala void
472af245d11STodd Fiala NativeThreadLinux::SetSuspended ()
473af245d11STodd Fiala {
474af245d11STodd Fiala     const StateType new_state = StateType::eStateSuspended;
475af245d11STodd Fiala     MaybeLogStateChange (new_state);
476af245d11STodd Fiala     m_state = new_state;
477af245d11STodd Fiala 
478af245d11STodd Fiala     // FIXME what makes sense here? Do we need a suspended StopReason?
479af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonNone;
480af245d11STodd Fiala }
481af245d11STodd Fiala 
482af245d11STodd Fiala void
483af245d11STodd Fiala NativeThreadLinux::SetExited ()
484af245d11STodd Fiala {
485af245d11STodd Fiala     const StateType new_state = StateType::eStateExited;
486af245d11STodd Fiala     MaybeLogStateChange (new_state);
487af245d11STodd Fiala     m_state = new_state;
488af245d11STodd Fiala 
489af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonThreadExiting;
490af245d11STodd Fiala }
491af245d11STodd Fiala 
492af245d11STodd Fiala void
493af245d11STodd Fiala NativeThreadLinux::MaybeLogStateChange (lldb::StateType new_state)
494af245d11STodd Fiala {
495af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
496af245d11STodd Fiala     // If we're not logging, we're done.
497af245d11STodd Fiala     if (!log)
498af245d11STodd Fiala         return;
499af245d11STodd Fiala 
500af245d11STodd Fiala     // If this is a state change to the same state, we're done.
501af245d11STodd Fiala     lldb::StateType old_state = m_state;
502af245d11STodd Fiala     if (new_state == old_state)
503af245d11STodd Fiala         return;
504af245d11STodd Fiala 
505af245d11STodd Fiala     NativeProcessProtocolSP m_process_sp = m_process_wp.lock ();
506af245d11STodd Fiala     lldb::pid_t pid = m_process_sp ? m_process_sp->GetID () : LLDB_INVALID_PROCESS_ID;
507af245d11STodd Fiala 
508af245d11STodd Fiala     // Log it.
509af245d11STodd Fiala     log->Printf ("NativeThreadLinux: thread (pid=%" PRIu64 ", tid=%" PRIu64 ") changing from state %s to %s", pid, GetID (), StateAsCString (old_state), StateAsCString (new_state));
510af245d11STodd Fiala }
511