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;
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         {
47*12fd3756SPavel Labath             case eStopReasonNone:
48*12fd3756SPavel Labath                 log.Printf ("%s: %s no stop reason", __FUNCTION__, header);
49*12fd3756SPavel Labath                 return;
50*12fd3756SPavel Labath             case eStopReasonTrace:
51*12fd3756SPavel Labath                 log.Printf ("%s: %s trace, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo);
52*12fd3756SPavel Labath                 return;
53*12fd3756SPavel Labath             case eStopReasonBreakpoint:
54*12fd3756SPavel Labath                 log.Printf ("%s: %s breakpoint, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo);
55*12fd3756SPavel Labath                 return;
56*12fd3756SPavel Labath             case eStopReasonWatchpoint:
57*12fd3756SPavel Labath                 log.Printf ("%s: %s watchpoint, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo);
58*12fd3756SPavel Labath                 return;
59af245d11STodd Fiala             case eStopReasonSignal:
60ae29d395SChaoren Lin                 log.Printf ("%s: %s signal 0x%02" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo);
61af245d11STodd Fiala                 return;
62af245d11STodd Fiala             case eStopReasonException:
63ae29d395SChaoren Lin                 log.Printf ("%s: %s exception type 0x%02" PRIx64, __FUNCTION__, header, stop_info.details.exception.type);
64a9882ceeSTodd Fiala                 return;
65a9882ceeSTodd Fiala             case eStopReasonExec:
66a9882ceeSTodd Fiala                 log.Printf ("%s: %s exec, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo);
67af245d11STodd Fiala                 return;
68*12fd3756SPavel Labath             case eStopReasonPlanComplete:
69*12fd3756SPavel Labath                 log.Printf ("%s: %s plan complete", __FUNCTION__, header);
70*12fd3756SPavel Labath                 return;
71*12fd3756SPavel Labath             case eStopReasonThreadExiting:
72*12fd3756SPavel Labath                 log.Printf ("%s: %s thread exiting", __FUNCTION__, header);
73*12fd3756SPavel Labath                 return;
74*12fd3756SPavel Labath             case eStopReasonInstrumentation:
75*12fd3756SPavel Labath                 log.Printf ("%s: %s instrumentation", __FUNCTION__, header);
76*12fd3756SPavel Labath                 return;
77af245d11STodd Fiala             default:
78a9882ceeSTodd Fiala                 log.Printf ("%s: %s invalid stop reason %" PRIu32, __FUNCTION__, header, static_cast<uint32_t> (stop_info.reason));
79af245d11STodd Fiala         }
80af245d11STodd Fiala     }
81af245d11STodd Fiala }
82af245d11STodd Fiala 
83af245d11STodd Fiala NativeThreadLinux::NativeThreadLinux (NativeProcessLinux *process, lldb::tid_t tid) :
84af245d11STodd Fiala     NativeThreadProtocol (process, tid),
85af245d11STodd Fiala     m_state (StateType::eStateInvalid),
86af245d11STodd Fiala     m_stop_info (),
8728e57429SChaoren Lin     m_reg_context_sp (),
8828e57429SChaoren Lin     m_stop_description ()
89af245d11STodd Fiala {
90af245d11STodd Fiala }
91af245d11STodd Fiala 
927206c6d1STodd Fiala std::string
93af245d11STodd Fiala NativeThreadLinux::GetName()
94af245d11STodd Fiala {
95af245d11STodd Fiala     NativeProcessProtocolSP process_sp = m_process_wp.lock ();
96af245d11STodd Fiala     if (!process_sp)
97af245d11STodd Fiala         return "<unknown: no process>";
98af245d11STodd Fiala 
99af245d11STodd Fiala     // const NativeProcessLinux *const process = reinterpret_cast<NativeProcessLinux*> (process_sp->get ());
10039de3110SZachary Turner     llvm::SmallString<32> thread_name;
10139de3110SZachary Turner     HostNativeThread::GetName(GetID(), thread_name);
10239de3110SZachary Turner     return thread_name.c_str();
103af245d11STodd Fiala }
104af245d11STodd Fiala 
105af245d11STodd Fiala lldb::StateType
106af245d11STodd Fiala NativeThreadLinux::GetState ()
107af245d11STodd Fiala {
108af245d11STodd Fiala     return m_state;
109af245d11STodd Fiala }
110af245d11STodd Fiala 
111af245d11STodd Fiala 
112af245d11STodd Fiala bool
11328e57429SChaoren Lin NativeThreadLinux::GetStopReason (ThreadStopInfo &stop_info, std::string& description)
114af245d11STodd Fiala {
115af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
11628e57429SChaoren Lin 
11728e57429SChaoren Lin     description.clear();
11828e57429SChaoren Lin 
119af245d11STodd Fiala     switch (m_state)
120af245d11STodd Fiala     {
121af245d11STodd Fiala     case eStateStopped:
122af245d11STodd Fiala     case eStateCrashed:
123af245d11STodd Fiala     case eStateExited:
124af245d11STodd Fiala     case eStateSuspended:
125af245d11STodd Fiala     case eStateUnloaded:
126af245d11STodd Fiala         if (log)
127af245d11STodd Fiala             LogThreadStopInfo (*log, m_stop_info, "m_stop_info in thread:");
128af245d11STodd Fiala         stop_info = m_stop_info;
12918fe6404SChaoren Lin         switch (m_stop_info.reason)
13018fe6404SChaoren Lin         {
13118fe6404SChaoren Lin             case StopReason::eStopReasonException:
13218fe6404SChaoren Lin             case StopReason::eStopReasonBreakpoint:
13318fe6404SChaoren Lin             case StopReason::eStopReasonWatchpoint:
13428e57429SChaoren Lin                 description = m_stop_description;
13518fe6404SChaoren Lin             default:
13618fe6404SChaoren Lin                 break;
13718fe6404SChaoren Lin         }
138af245d11STodd Fiala         if (log)
139af245d11STodd Fiala             LogThreadStopInfo (*log, stop_info, "returned stop_info:");
14028e57429SChaoren Lin 
141af245d11STodd Fiala         return true;
142af245d11STodd Fiala 
143af245d11STodd Fiala     case eStateInvalid:
144af245d11STodd Fiala     case eStateConnected:
145af245d11STodd Fiala     case eStateAttaching:
146af245d11STodd Fiala     case eStateLaunching:
147af245d11STodd Fiala     case eStateRunning:
148af245d11STodd Fiala     case eStateStepping:
149af245d11STodd Fiala     case eStateDetached:
150af245d11STodd Fiala         if (log)
151af245d11STodd Fiala         {
152af245d11STodd Fiala             log->Printf ("NativeThreadLinux::%s tid %" PRIu64 " in state %s cannot answer stop reason",
153af245d11STodd Fiala                     __FUNCTION__, GetID (), StateAsCString (m_state));
154af245d11STodd Fiala         }
155af245d11STodd Fiala         return false;
156af245d11STodd Fiala     }
1578faf9370SDavid Majnemer     llvm_unreachable("unhandled StateType!");
158af245d11STodd Fiala }
159af245d11STodd Fiala 
160af245d11STodd Fiala lldb_private::NativeRegisterContextSP
161af245d11STodd Fiala NativeThreadLinux::GetRegisterContext ()
162af245d11STodd Fiala {
163af245d11STodd Fiala     // Return the register context if we already created it.
164af245d11STodd Fiala     if (m_reg_context_sp)
165af245d11STodd Fiala         return m_reg_context_sp;
166af245d11STodd Fiala 
167af245d11STodd Fiala     // First select the appropriate RegisterInfoInterface.
168af245d11STodd Fiala     RegisterInfoInterface *reg_interface = nullptr;
169af245d11STodd Fiala     NativeProcessProtocolSP m_process_sp = m_process_wp.lock ();
170af245d11STodd Fiala     if (!m_process_sp)
171af245d11STodd Fiala         return NativeRegisterContextSP ();
172af245d11STodd Fiala 
173af245d11STodd Fiala     ArchSpec target_arch;
174af245d11STodd Fiala     if (!m_process_sp->GetArchitecture (target_arch))
175af245d11STodd Fiala         return NativeRegisterContextSP ();
176af245d11STodd Fiala 
177af245d11STodd Fiala     switch (target_arch.GetTriple().getOS())
178af245d11STodd Fiala     {
179af245d11STodd Fiala         case llvm::Triple::Linux:
180af245d11STodd Fiala             switch (target_arch.GetMachine())
181af245d11STodd Fiala             {
182b71e89e9STodd Fiala             case llvm::Triple::aarch64:
183b71e89e9STodd Fiala                 assert((HostInfo::GetArchitecture ().GetAddressByteSize() == 8) && "Register setting path assumes this is a 64-bit host");
184b71e89e9STodd Fiala                 reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_arm64(target_arch));
185b71e89e9STodd Fiala                 break;
186af245d11STodd Fiala             case llvm::Triple::x86:
187af245d11STodd Fiala             case llvm::Triple::x86_64:
18813b18261SZachary Turner                 if (HostInfo::GetArchitecture().GetAddressByteSize() == 4)
189af245d11STodd Fiala                 {
190af245d11STodd Fiala                     // 32-bit hosts run with a RegisterContextLinux_i386 context.
191af245d11STodd Fiala                     reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_i386(target_arch));
192af245d11STodd Fiala                 }
193af245d11STodd Fiala                 else
194af245d11STodd Fiala                 {
19513b18261SZachary Turner                     assert((HostInfo::GetArchitecture().GetAddressByteSize() == 8) &&
19613b18261SZachary Turner                            "Register setting path assumes this is a 64-bit host");
197af245d11STodd Fiala                     // X86_64 hosts know how to work with 64-bit and 32-bit EXEs using the x86_64 register context.
198af245d11STodd Fiala                     reg_interface = static_cast<RegisterInfoInterface*> (new RegisterContextLinux_x86_64 (target_arch));
199af245d11STodd Fiala                 }
200af245d11STodd Fiala                 break;
2013df471c3SMohit K. Bhakkad             case llvm::Triple::mips64:
2023df471c3SMohit K. Bhakkad             case llvm::Triple::mips64el:
2033df471c3SMohit K. Bhakkad                 assert((HostInfo::GetArchitecture ().GetAddressByteSize() == 8)
2043df471c3SMohit K. Bhakkad                     && "Register setting path assumes this is a 64-bit host");
2053df471c3SMohit K. Bhakkad                 reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_mips64 (target_arch));
2063df471c3SMohit K. Bhakkad                 break;
207af245d11STodd Fiala             default:
208af245d11STodd Fiala                 break;
209af245d11STodd Fiala             }
210af245d11STodd Fiala             break;
211af245d11STodd Fiala         default:
212af245d11STodd Fiala             break;
213af245d11STodd Fiala     }
214af245d11STodd Fiala 
215af245d11STodd Fiala     assert(reg_interface && "OS or CPU not supported!");
216af245d11STodd Fiala     if (!reg_interface)
217af245d11STodd Fiala         return NativeRegisterContextSP ();
218af245d11STodd Fiala 
219af245d11STodd Fiala     // Now create the register context.
220af245d11STodd Fiala     switch (target_arch.GetMachine())
221af245d11STodd Fiala     {
222af245d11STodd Fiala #if 0
223af245d11STodd Fiala         case llvm::Triple::mips64:
224af245d11STodd Fiala         {
225af245d11STodd Fiala             RegisterContextPOSIXProcessMonitor_mips64 *reg_ctx = new RegisterContextPOSIXProcessMonitor_mips64(*this, 0, reg_interface);
226af245d11STodd Fiala             m_posix_thread = reg_ctx;
227af245d11STodd Fiala             m_reg_context_sp.reset(reg_ctx);
228af245d11STodd Fiala             break;
229af245d11STodd Fiala         }
230af245d11STodd Fiala #endif
2313df471c3SMohit K. Bhakkad         case llvm::Triple::mips64:
2323df471c3SMohit K. Bhakkad         case llvm::Triple::mips64el:
2333df471c3SMohit K. Bhakkad         {
2343df471c3SMohit K. Bhakkad             const uint32_t concrete_frame_idx = 0;
2353df471c3SMohit K. Bhakkad             m_reg_context_sp.reset (new NativeRegisterContextLinux_mips64 (*this, concrete_frame_idx, reg_interface));
2363df471c3SMohit K. Bhakkad             break;
2373df471c3SMohit K. Bhakkad         }
2381e209fccSTamas Berghammer         case llvm::Triple::aarch64:
2391e209fccSTamas Berghammer         {
2401e209fccSTamas Berghammer             const uint32_t concrete_frame_idx = 0;
2411e209fccSTamas Berghammer             m_reg_context_sp.reset (new NativeRegisterContextLinux_arm64(*this, concrete_frame_idx, reg_interface));
2421e209fccSTamas Berghammer             break;
2431e209fccSTamas Berghammer         }
244af245d11STodd Fiala         case llvm::Triple::x86:
245af245d11STodd Fiala         case llvm::Triple::x86_64:
246af245d11STodd Fiala         {
247af245d11STodd Fiala             const uint32_t concrete_frame_idx = 0;
248af245d11STodd Fiala             m_reg_context_sp.reset (new NativeRegisterContextLinux_x86_64(*this, concrete_frame_idx, reg_interface));
249af245d11STodd Fiala             break;
250af245d11STodd Fiala         }
251af245d11STodd Fiala         default:
252af245d11STodd Fiala             break;
253af245d11STodd Fiala     }
254af245d11STodd Fiala 
255af245d11STodd Fiala     return m_reg_context_sp;
256af245d11STodd Fiala }
257af245d11STodd Fiala 
258af245d11STodd Fiala Error
259af245d11STodd Fiala NativeThreadLinux::SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware)
260af245d11STodd Fiala {
26118fe6404SChaoren Lin     if (!hardware)
262af245d11STodd Fiala         return Error ("not implemented");
263f591f69fSChaoren Lin     if (m_state == eStateLaunching)
264f591f69fSChaoren Lin         return Error ();
26518fe6404SChaoren Lin     Error error = RemoveWatchpoint(addr);
26618fe6404SChaoren Lin     if (error.Fail()) return error;
26718fe6404SChaoren Lin     NativeRegisterContextSP reg_ctx = GetRegisterContext ();
26818fe6404SChaoren Lin     uint32_t wp_index =
26918fe6404SChaoren Lin         reg_ctx->SetHardwareWatchpoint (addr, size, watch_flags);
27018fe6404SChaoren Lin     if (wp_index == LLDB_INVALID_INDEX32)
27118fe6404SChaoren Lin         return Error ("Setting hardware watchpoint failed.");
27218fe6404SChaoren Lin     m_watchpoint_index_map.insert({addr, wp_index});
27318fe6404SChaoren Lin     return Error ();
274af245d11STodd Fiala }
275af245d11STodd Fiala 
276af245d11STodd Fiala Error
277af245d11STodd Fiala NativeThreadLinux::RemoveWatchpoint (lldb::addr_t addr)
278af245d11STodd Fiala {
27918fe6404SChaoren Lin     auto wp = m_watchpoint_index_map.find(addr);
28018fe6404SChaoren Lin     if (wp == m_watchpoint_index_map.end())
28118fe6404SChaoren Lin         return Error ();
28218fe6404SChaoren Lin     uint32_t wp_index = wp->second;
28318fe6404SChaoren Lin     m_watchpoint_index_map.erase(wp);
28418fe6404SChaoren Lin     if (GetRegisterContext()->ClearHardwareWatchpoint(wp_index))
28518fe6404SChaoren Lin         return Error ();
28618fe6404SChaoren Lin     return Error ("Clearing hardware watchpoint failed.");
287af245d11STodd Fiala }
288af245d11STodd Fiala 
289af245d11STodd Fiala void
290af245d11STodd Fiala NativeThreadLinux::SetLaunching ()
291af245d11STodd Fiala {
292af245d11STodd Fiala     const StateType new_state = StateType::eStateLaunching;
293af245d11STodd Fiala     MaybeLogStateChange (new_state);
294af245d11STodd Fiala     m_state = new_state;
295af245d11STodd Fiala 
296af245d11STodd Fiala     // Also mark it as stopped since launching temporarily stops the newly created thread
297af245d11STodd Fiala     // in the ptrace machinery.
298af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonSignal;
299af245d11STodd Fiala     m_stop_info.details.signal.signo = SIGSTOP;
300af245d11STodd Fiala }
301af245d11STodd Fiala 
302af245d11STodd Fiala 
303af245d11STodd Fiala void
304af245d11STodd Fiala NativeThreadLinux::SetRunning ()
305af245d11STodd Fiala {
306af245d11STodd Fiala     const StateType new_state = StateType::eStateRunning;
307af245d11STodd Fiala     MaybeLogStateChange (new_state);
308af245d11STodd Fiala     m_state = new_state;
309af245d11STodd Fiala 
310af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonNone;
31128e57429SChaoren Lin     m_stop_description.clear();
31218fe6404SChaoren Lin 
31318fe6404SChaoren Lin     // If watchpoints have been set, but none on this thread,
31418fe6404SChaoren Lin     // then this is a new thread. So set all existing watchpoints.
31518fe6404SChaoren Lin     if (m_watchpoint_index_map.empty())
31618fe6404SChaoren Lin     {
3178bc34f4dSOleksiy Vyalov         const auto process_sp = GetProcess();
3188bc34f4dSOleksiy Vyalov         if (process_sp)
3198bc34f4dSOleksiy Vyalov         {
3208bc34f4dSOleksiy Vyalov             const auto &watchpoint_map = process_sp->GetWatchpointMap();
32118fe6404SChaoren Lin             if (watchpoint_map.empty()) return;
32218fe6404SChaoren Lin             GetRegisterContext()->ClearAllHardwareWatchpoints();
32318fe6404SChaoren Lin             for (const auto &pair : watchpoint_map)
32418fe6404SChaoren Lin             {
32518fe6404SChaoren Lin                 const auto& wp = pair.second;
32618fe6404SChaoren Lin                 SetWatchpoint(wp.m_addr, wp.m_size, wp.m_watch_flags, wp.m_hardware);
32718fe6404SChaoren Lin             }
32818fe6404SChaoren Lin         }
329af245d11STodd Fiala     }
3308bc34f4dSOleksiy Vyalov }
331af245d11STodd Fiala 
332af245d11STodd Fiala void
333af245d11STodd Fiala NativeThreadLinux::SetStepping ()
334af245d11STodd Fiala {
335af245d11STodd Fiala     const StateType new_state = StateType::eStateStepping;
336af245d11STodd Fiala     MaybeLogStateChange (new_state);
337af245d11STodd Fiala     m_state = new_state;
338af245d11STodd Fiala 
339af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonNone;
340af245d11STodd Fiala }
341af245d11STodd Fiala 
342af245d11STodd Fiala void
343af245d11STodd Fiala NativeThreadLinux::SetStoppedBySignal (uint32_t signo)
344af245d11STodd Fiala {
345af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
346af245d11STodd Fiala     if (log)
347b8af31d4SChaoren Lin         log->Printf ("NativeThreadLinux::%s called with signal 0x%02" PRIx32, __FUNCTION__, signo);
348af245d11STodd Fiala 
349af245d11STodd Fiala     const StateType new_state = StateType::eStateStopped;
350af245d11STodd Fiala     MaybeLogStateChange (new_state);
351af245d11STodd Fiala     m_state = new_state;
352af245d11STodd Fiala 
353af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonSignal;
354af245d11STodd Fiala     m_stop_info.details.signal.signo = signo;
355af245d11STodd Fiala }
356af245d11STodd Fiala 
357511e5cdcSTodd Fiala bool
358511e5cdcSTodd Fiala NativeThreadLinux::IsStopped (int *signo)
359511e5cdcSTodd Fiala {
360511e5cdcSTodd Fiala     if (!StateIsStoppedState (m_state, false))
361511e5cdcSTodd Fiala         return false;
362511e5cdcSTodd Fiala 
363511e5cdcSTodd Fiala     // If we are stopped by a signal, return the signo.
364511e5cdcSTodd Fiala     if (signo &&
365511e5cdcSTodd Fiala         m_state == StateType::eStateStopped &&
366511e5cdcSTodd Fiala         m_stop_info.reason == StopReason::eStopReasonSignal)
367511e5cdcSTodd Fiala     {
368511e5cdcSTodd Fiala         *signo = m_stop_info.details.signal.signo;
369511e5cdcSTodd Fiala     }
370511e5cdcSTodd Fiala 
371511e5cdcSTodd Fiala     // Regardless, we are stopped.
372511e5cdcSTodd Fiala     return true;
373511e5cdcSTodd Fiala }
374511e5cdcSTodd Fiala 
375511e5cdcSTodd Fiala 
376af245d11STodd Fiala void
377a9882ceeSTodd Fiala NativeThreadLinux::SetStoppedByExec ()
378a9882ceeSTodd Fiala {
379a9882ceeSTodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
380a9882ceeSTodd Fiala     if (log)
381a9882ceeSTodd Fiala         log->Printf ("NativeThreadLinux::%s()", __FUNCTION__);
382a9882ceeSTodd Fiala 
383a9882ceeSTodd Fiala     const StateType new_state = StateType::eStateStopped;
384a9882ceeSTodd Fiala     MaybeLogStateChange (new_state);
385a9882ceeSTodd Fiala     m_state = new_state;
386a9882ceeSTodd Fiala 
387a9882ceeSTodd Fiala     m_stop_info.reason = StopReason::eStopReasonExec;
388a9882ceeSTodd Fiala     m_stop_info.details.signal.signo = SIGSTOP;
389a9882ceeSTodd Fiala }
390a9882ceeSTodd Fiala 
391a9882ceeSTodd Fiala void
392af245d11STodd Fiala NativeThreadLinux::SetStoppedByBreakpoint ()
393af245d11STodd Fiala {
394af245d11STodd Fiala     const StateType new_state = StateType::eStateStopped;
395af245d11STodd Fiala     MaybeLogStateChange (new_state);
396af245d11STodd Fiala     m_state = new_state;
397af245d11STodd Fiala 
39828e57429SChaoren Lin     m_stop_info.reason = StopReason::eStopReasonBreakpoint;
399af245d11STodd Fiala     m_stop_info.details.signal.signo = SIGTRAP;
40018fe6404SChaoren Lin     m_stop_description.clear();
40118fe6404SChaoren Lin }
40218fe6404SChaoren Lin 
40318fe6404SChaoren Lin void
404c16f5dcaSChaoren Lin NativeThreadLinux::SetStoppedByWatchpoint (uint32_t wp_index)
40518fe6404SChaoren Lin {
40618fe6404SChaoren Lin     const StateType new_state = StateType::eStateStopped;
40718fe6404SChaoren Lin     MaybeLogStateChange (new_state);
40818fe6404SChaoren Lin     m_state = new_state;
40918fe6404SChaoren Lin     m_stop_description.clear ();
410c16f5dcaSChaoren Lin 
411c16f5dcaSChaoren Lin     lldbassert(wp_index != LLDB_INVALID_INDEX32 &&
412c16f5dcaSChaoren Lin                "wp_index cannot be invalid");
413eadb2a9eSTamas Berghammer 
41418fe6404SChaoren Lin     std::ostringstream ostr;
415c16f5dcaSChaoren Lin     ostr << GetRegisterContext()->GetWatchpointAddress(wp_index) << " ";
416c16f5dcaSChaoren Lin     ostr << wp_index;
41718fe6404SChaoren Lin     m_stop_description = ostr.str();
418eadb2a9eSTamas Berghammer 
419eadb2a9eSTamas Berghammer     m_stop_info.reason = StopReason::eStopReasonWatchpoint;
420eadb2a9eSTamas Berghammer     m_stop_info.details.signal.signo = SIGTRAP;
421af245d11STodd Fiala }
422af245d11STodd Fiala 
423af245d11STodd Fiala bool
424af245d11STodd Fiala NativeThreadLinux::IsStoppedAtBreakpoint ()
425af245d11STodd Fiala {
42618fe6404SChaoren Lin     return GetState () == StateType::eStateStopped &&
42718fe6404SChaoren Lin         m_stop_info.reason == StopReason::eStopReasonBreakpoint;
42818fe6404SChaoren Lin }
429af245d11STodd Fiala 
43018fe6404SChaoren Lin bool
43118fe6404SChaoren Lin NativeThreadLinux::IsStoppedAtWatchpoint ()
43218fe6404SChaoren Lin {
43318fe6404SChaoren Lin     return GetState () == StateType::eStateStopped &&
43418fe6404SChaoren Lin         m_stop_info.reason == StopReason::eStopReasonWatchpoint;
435af245d11STodd Fiala }
436af245d11STodd Fiala 
437af245d11STodd Fiala void
43828e57429SChaoren Lin NativeThreadLinux::SetStoppedByTrace ()
43928e57429SChaoren Lin {
44028e57429SChaoren Lin     const StateType new_state = StateType::eStateStopped;
44128e57429SChaoren Lin     MaybeLogStateChange (new_state);
44228e57429SChaoren Lin     m_state = new_state;
44328e57429SChaoren Lin 
44428e57429SChaoren Lin     m_stop_info.reason = StopReason::eStopReasonTrace;
44528e57429SChaoren Lin     m_stop_info.details.signal.signo = SIGTRAP;
44628e57429SChaoren Lin }
44728e57429SChaoren Lin 
44828e57429SChaoren Lin void
44928e57429SChaoren Lin NativeThreadLinux::SetCrashedWithException (const siginfo_t& info)
450af245d11STodd Fiala {
451af245d11STodd Fiala     const StateType new_state = StateType::eStateCrashed;
452af245d11STodd Fiala     MaybeLogStateChange (new_state);
453af245d11STodd Fiala     m_state = new_state;
454af245d11STodd Fiala 
455af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonException;
45628e57429SChaoren Lin     m_stop_info.details.signal.signo = info.si_signo;
457af245d11STodd Fiala 
45828e57429SChaoren Lin     const auto reason = GetCrashReason (info);
45928e57429SChaoren Lin     m_stop_description = GetCrashReasonString (reason, reinterpret_cast<lldb::addr_t> (info.si_addr));
46028e57429SChaoren Lin }
461af245d11STodd Fiala 
462af245d11STodd Fiala void
463af245d11STodd Fiala NativeThreadLinux::SetSuspended ()
464af245d11STodd Fiala {
465af245d11STodd Fiala     const StateType new_state = StateType::eStateSuspended;
466af245d11STodd Fiala     MaybeLogStateChange (new_state);
467af245d11STodd Fiala     m_state = new_state;
468af245d11STodd Fiala 
469af245d11STodd Fiala     // FIXME what makes sense here? Do we need a suspended StopReason?
470af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonNone;
471af245d11STodd Fiala }
472af245d11STodd Fiala 
473af245d11STodd Fiala void
474af245d11STodd Fiala NativeThreadLinux::SetExited ()
475af245d11STodd Fiala {
476af245d11STodd Fiala     const StateType new_state = StateType::eStateExited;
477af245d11STodd Fiala     MaybeLogStateChange (new_state);
478af245d11STodd Fiala     m_state = new_state;
479af245d11STodd Fiala 
480af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonThreadExiting;
481af245d11STodd Fiala }
482af245d11STodd Fiala 
483af245d11STodd Fiala void
484af245d11STodd Fiala NativeThreadLinux::MaybeLogStateChange (lldb::StateType new_state)
485af245d11STodd Fiala {
486af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
487af245d11STodd Fiala     // If we're not logging, we're done.
488af245d11STodd Fiala     if (!log)
489af245d11STodd Fiala         return;
490af245d11STodd Fiala 
491af245d11STodd Fiala     // If this is a state change to the same state, we're done.
492af245d11STodd Fiala     lldb::StateType old_state = m_state;
493af245d11STodd Fiala     if (new_state == old_state)
494af245d11STodd Fiala         return;
495af245d11STodd Fiala 
496af245d11STodd Fiala     NativeProcessProtocolSP m_process_sp = m_process_wp.lock ();
497af245d11STodd Fiala     lldb::pid_t pid = m_process_sp ? m_process_sp->GetID () : LLDB_INVALID_PROCESS_ID;
498af245d11STodd Fiala 
499af245d11STodd Fiala     // Log it.
500af245d11STodd Fiala     log->Printf ("NativeThreadLinux: thread (pid=%" PRIu64 ", tid=%" PRIu64 ") changing from state %s to %s", pid, GetID (), StateAsCString (old_state), StateAsCString (new_state));
501af245d11STodd Fiala }
502