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>
13af245d11STodd Fiala 
14af245d11STodd Fiala #include "NativeProcessLinux.h"
152850b1beSTodd Fiala #include "NativeRegisterContextLinux_x86_64.h"
162850b1beSTodd Fiala 
17af245d11STodd Fiala #include "lldb/Core/Log.h"
18af245d11STodd Fiala #include "lldb/Core/State.h"
19af245d11STodd Fiala #include "lldb/Host/Host.h"
2013b18261SZachary Turner #include "lldb/Host/HostInfo.h"
2139de3110SZachary Turner #include "lldb/Host/HostNativeThread.h"
22af245d11STodd Fiala #include "lldb/lldb-enumerations.h"
23af245d11STodd Fiala #include "lldb/lldb-private-log.h"
2439de3110SZachary Turner 
2539de3110SZachary Turner #include "llvm/ADT/SmallString.h"
2639de3110SZachary Turner 
27*28e57429SChaoren Lin #include "Plugins/Process/POSIX/CrashReason.h"
28*28e57429SChaoren Lin 
29b71e89e9STodd Fiala #include "Plugins/Process/Utility/RegisterContextLinux_arm64.h"
30af245d11STodd Fiala #include "Plugins/Process/Utility/RegisterContextLinux_i386.h"
31af245d11STodd Fiala #include "Plugins/Process/Utility/RegisterContextLinux_x86_64.h"
32af245d11STodd Fiala #include "Plugins/Process/Utility/RegisterInfoInterface.h"
33af245d11STodd Fiala 
34af245d11STodd Fiala using namespace lldb;
35af245d11STodd Fiala using namespace lldb_private;
36af245d11STodd Fiala 
37af245d11STodd Fiala namespace
38af245d11STodd Fiala {
39af245d11STodd Fiala     void LogThreadStopInfo (Log &log, const ThreadStopInfo &stop_info, const char *const header)
40af245d11STodd Fiala     {
41af245d11STodd Fiala         switch (stop_info.reason)
42af245d11STodd Fiala         {
43af245d11STodd Fiala             case eStopReasonSignal:
44ae29d395SChaoren Lin                 log.Printf ("%s: %s signal 0x%02" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo);
45af245d11STodd Fiala                 return;
46af245d11STodd Fiala             case eStopReasonException:
47ae29d395SChaoren Lin                 log.Printf ("%s: %s exception type 0x%02" PRIx64, __FUNCTION__, header, stop_info.details.exception.type);
48a9882ceeSTodd Fiala                 return;
49a9882ceeSTodd Fiala             case eStopReasonExec:
50a9882ceeSTodd Fiala                 log.Printf ("%s: %s exec, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo);
51af245d11STodd Fiala                 return;
52af245d11STodd Fiala             default:
53a9882ceeSTodd Fiala                 log.Printf ("%s: %s invalid stop reason %" PRIu32, __FUNCTION__, header, static_cast<uint32_t> (stop_info.reason));
54af245d11STodd Fiala         }
55af245d11STodd Fiala     }
56af245d11STodd Fiala }
57af245d11STodd Fiala 
58af245d11STodd Fiala NativeThreadLinux::NativeThreadLinux (NativeProcessLinux *process, lldb::tid_t tid) :
59af245d11STodd Fiala     NativeThreadProtocol (process, tid),
60af245d11STodd Fiala     m_state (StateType::eStateInvalid),
61af245d11STodd Fiala     m_stop_info (),
62*28e57429SChaoren Lin     m_reg_context_sp (),
63*28e57429SChaoren Lin     m_stop_description ()
64af245d11STodd Fiala {
65af245d11STodd Fiala }
66af245d11STodd Fiala 
677206c6d1STodd Fiala std::string
68af245d11STodd Fiala NativeThreadLinux::GetName()
69af245d11STodd Fiala {
70af245d11STodd Fiala     NativeProcessProtocolSP process_sp = m_process_wp.lock ();
71af245d11STodd Fiala     if (!process_sp)
72af245d11STodd Fiala         return "<unknown: no process>";
73af245d11STodd Fiala 
74af245d11STodd Fiala     // const NativeProcessLinux *const process = reinterpret_cast<NativeProcessLinux*> (process_sp->get ());
7539de3110SZachary Turner     llvm::SmallString<32> thread_name;
7639de3110SZachary Turner     HostNativeThread::GetName(GetID(), thread_name);
7739de3110SZachary Turner     return thread_name.c_str();
78af245d11STodd Fiala }
79af245d11STodd Fiala 
80af245d11STodd Fiala lldb::StateType
81af245d11STodd Fiala NativeThreadLinux::GetState ()
82af245d11STodd Fiala {
83af245d11STodd Fiala     return m_state;
84af245d11STodd Fiala }
85af245d11STodd Fiala 
86af245d11STodd Fiala 
87af245d11STodd Fiala bool
88*28e57429SChaoren Lin NativeThreadLinux::GetStopReason (ThreadStopInfo &stop_info, std::string& description)
89af245d11STodd Fiala {
90af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
91*28e57429SChaoren Lin 
92*28e57429SChaoren Lin     description.clear();
93*28e57429SChaoren Lin 
94af245d11STodd Fiala     switch (m_state)
95af245d11STodd Fiala     {
96af245d11STodd Fiala     case eStateStopped:
97af245d11STodd Fiala     case eStateCrashed:
98af245d11STodd Fiala     case eStateExited:
99af245d11STodd Fiala     case eStateSuspended:
100af245d11STodd Fiala     case eStateUnloaded:
101af245d11STodd Fiala         if (log)
102af245d11STodd Fiala             LogThreadStopInfo (*log, m_stop_info, "m_stop_info in thread:");
103af245d11STodd Fiala         stop_info = m_stop_info;
104*28e57429SChaoren Lin         if (m_stop_info.reason == StopReason::eStopReasonException)
105*28e57429SChaoren Lin             description = m_stop_description;
106af245d11STodd Fiala         if (log)
107af245d11STodd Fiala             LogThreadStopInfo (*log, stop_info, "returned stop_info:");
108*28e57429SChaoren Lin 
109af245d11STodd Fiala         return true;
110af245d11STodd Fiala 
111af245d11STodd Fiala     case eStateInvalid:
112af245d11STodd Fiala     case eStateConnected:
113af245d11STodd Fiala     case eStateAttaching:
114af245d11STodd Fiala     case eStateLaunching:
115af245d11STodd Fiala     case eStateRunning:
116af245d11STodd Fiala     case eStateStepping:
117af245d11STodd Fiala     case eStateDetached:
118af245d11STodd Fiala         if (log)
119af245d11STodd Fiala         {
120af245d11STodd Fiala             log->Printf ("NativeThreadLinux::%s tid %" PRIu64 " in state %s cannot answer stop reason",
121af245d11STodd Fiala                     __FUNCTION__, GetID (), StateAsCString (m_state));
122af245d11STodd Fiala         }
123af245d11STodd Fiala         return false;
124af245d11STodd Fiala     }
1258faf9370SDavid Majnemer     llvm_unreachable("unhandled StateType!");
126af245d11STodd Fiala }
127af245d11STodd Fiala 
128af245d11STodd Fiala lldb_private::NativeRegisterContextSP
129af245d11STodd Fiala NativeThreadLinux::GetRegisterContext ()
130af245d11STodd Fiala {
131af245d11STodd Fiala     // Return the register context if we already created it.
132af245d11STodd Fiala     if (m_reg_context_sp)
133af245d11STodd Fiala         return m_reg_context_sp;
134af245d11STodd Fiala 
135af245d11STodd Fiala     // First select the appropriate RegisterInfoInterface.
136af245d11STodd Fiala     RegisterInfoInterface *reg_interface = nullptr;
137af245d11STodd Fiala     NativeProcessProtocolSP m_process_sp = m_process_wp.lock ();
138af245d11STodd Fiala     if (!m_process_sp)
139af245d11STodd Fiala         return NativeRegisterContextSP ();
140af245d11STodd Fiala 
141af245d11STodd Fiala     ArchSpec target_arch;
142af245d11STodd Fiala     if (!m_process_sp->GetArchitecture (target_arch))
143af245d11STodd Fiala         return NativeRegisterContextSP ();
144af245d11STodd Fiala 
145af245d11STodd Fiala     switch (target_arch.GetTriple().getOS())
146af245d11STodd Fiala     {
147af245d11STodd Fiala         case llvm::Triple::Linux:
148af245d11STodd Fiala             switch (target_arch.GetMachine())
149af245d11STodd Fiala             {
150b71e89e9STodd Fiala             case llvm::Triple::aarch64:
151b71e89e9STodd Fiala                 assert((HostInfo::GetArchitecture ().GetAddressByteSize() == 8) && "Register setting path assumes this is a 64-bit host");
152b71e89e9STodd Fiala                 reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_arm64(target_arch));
153b71e89e9STodd Fiala                 break;
154af245d11STodd Fiala             case llvm::Triple::x86:
155af245d11STodd Fiala             case llvm::Triple::x86_64:
15613b18261SZachary Turner                 if (HostInfo::GetArchitecture().GetAddressByteSize() == 4)
157af245d11STodd Fiala                 {
158af245d11STodd Fiala                     // 32-bit hosts run with a RegisterContextLinux_i386 context.
159af245d11STodd Fiala                     reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_i386(target_arch));
160af245d11STodd Fiala                 }
161af245d11STodd Fiala                 else
162af245d11STodd Fiala                 {
16313b18261SZachary Turner                     assert((HostInfo::GetArchitecture().GetAddressByteSize() == 8) &&
16413b18261SZachary Turner                            "Register setting path assumes this is a 64-bit host");
165af245d11STodd Fiala                     // X86_64 hosts know how to work with 64-bit and 32-bit EXEs using the x86_64 register context.
166af245d11STodd Fiala                     reg_interface = static_cast<RegisterInfoInterface*> (new RegisterContextLinux_x86_64 (target_arch));
167af245d11STodd Fiala                 }
168af245d11STodd Fiala                 break;
169af245d11STodd Fiala             default:
170af245d11STodd Fiala                 break;
171af245d11STodd Fiala             }
172af245d11STodd Fiala             break;
173af245d11STodd Fiala         default:
174af245d11STodd Fiala             break;
175af245d11STodd Fiala     }
176af245d11STodd Fiala 
177af245d11STodd Fiala     assert(reg_interface && "OS or CPU not supported!");
178af245d11STodd Fiala     if (!reg_interface)
179af245d11STodd Fiala         return NativeRegisterContextSP ();
180af245d11STodd Fiala 
181af245d11STodd Fiala     // Now create the register context.
182af245d11STodd Fiala     switch (target_arch.GetMachine())
183af245d11STodd Fiala     {
184af245d11STodd Fiala #if 0
185af245d11STodd Fiala         case llvm::Triple::mips64:
186af245d11STodd Fiala         {
187af245d11STodd Fiala             RegisterContextPOSIXProcessMonitor_mips64 *reg_ctx = new RegisterContextPOSIXProcessMonitor_mips64(*this, 0, reg_interface);
188af245d11STodd Fiala             m_posix_thread = reg_ctx;
189af245d11STodd Fiala             m_reg_context_sp.reset(reg_ctx);
190af245d11STodd Fiala             break;
191af245d11STodd Fiala         }
192af245d11STodd Fiala #endif
193af245d11STodd Fiala #if 0
194af245d11STodd Fiala         case llvm::Triple::x86:
195af245d11STodd Fiala #endif
196af245d11STodd Fiala         case llvm::Triple::x86_64:
197af245d11STodd Fiala         {
198af245d11STodd Fiala             const uint32_t concrete_frame_idx = 0;
199af245d11STodd Fiala             m_reg_context_sp.reset (new NativeRegisterContextLinux_x86_64(*this, concrete_frame_idx, reg_interface));
200af245d11STodd Fiala             break;
201af245d11STodd Fiala         }
202af245d11STodd Fiala         default:
203af245d11STodd Fiala             break;
204af245d11STodd Fiala     }
205af245d11STodd Fiala 
206af245d11STodd Fiala     return m_reg_context_sp;
207af245d11STodd Fiala }
208af245d11STodd Fiala 
209af245d11STodd Fiala Error
210af245d11STodd Fiala NativeThreadLinux::SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware)
211af245d11STodd Fiala {
212af245d11STodd Fiala     // TODO implement
213af245d11STodd Fiala     return Error ("not implemented");
214af245d11STodd Fiala }
215af245d11STodd Fiala 
216af245d11STodd Fiala Error
217af245d11STodd Fiala NativeThreadLinux::RemoveWatchpoint (lldb::addr_t addr)
218af245d11STodd Fiala {
219af245d11STodd Fiala     // TODO implement
220af245d11STodd Fiala     return Error ("not implemented");
221af245d11STodd Fiala }
222af245d11STodd Fiala 
223af245d11STodd Fiala void
224af245d11STodd Fiala NativeThreadLinux::SetLaunching ()
225af245d11STodd Fiala {
226af245d11STodd Fiala     const StateType new_state = StateType::eStateLaunching;
227af245d11STodd Fiala     MaybeLogStateChange (new_state);
228af245d11STodd Fiala     m_state = new_state;
229af245d11STodd Fiala 
230af245d11STodd Fiala     // Also mark it as stopped since launching temporarily stops the newly created thread
231af245d11STodd Fiala     // in the ptrace machinery.
232af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonSignal;
233af245d11STodd Fiala     m_stop_info.details.signal.signo = SIGSTOP;
234af245d11STodd Fiala }
235af245d11STodd Fiala 
236af245d11STodd Fiala 
237af245d11STodd Fiala void
238af245d11STodd Fiala NativeThreadLinux::SetRunning ()
239af245d11STodd Fiala {
240af245d11STodd Fiala     const StateType new_state = StateType::eStateRunning;
241af245d11STodd Fiala     MaybeLogStateChange (new_state);
242af245d11STodd Fiala     m_state = new_state;
243af245d11STodd Fiala 
244af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonNone;
245*28e57429SChaoren Lin     m_stop_description.clear();
246af245d11STodd Fiala }
247af245d11STodd Fiala 
248af245d11STodd Fiala void
249af245d11STodd Fiala NativeThreadLinux::SetStepping ()
250af245d11STodd Fiala {
251af245d11STodd Fiala     const StateType new_state = StateType::eStateStepping;
252af245d11STodd Fiala     MaybeLogStateChange (new_state);
253af245d11STodd Fiala     m_state = new_state;
254af245d11STodd Fiala 
255af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonNone;
256af245d11STodd Fiala }
257af245d11STodd Fiala 
258af245d11STodd Fiala void
259af245d11STodd Fiala NativeThreadLinux::SetStoppedBySignal (uint32_t signo)
260af245d11STodd Fiala {
261af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
262af245d11STodd Fiala     if (log)
263b8af31d4SChaoren Lin         log->Printf ("NativeThreadLinux::%s called with signal 0x%02" PRIx32, __FUNCTION__, signo);
264af245d11STodd Fiala 
265af245d11STodd Fiala     const StateType new_state = StateType::eStateStopped;
266af245d11STodd Fiala     MaybeLogStateChange (new_state);
267af245d11STodd Fiala     m_state = new_state;
268af245d11STodd Fiala 
269af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonSignal;
270af245d11STodd Fiala     m_stop_info.details.signal.signo = signo;
271af245d11STodd Fiala }
272af245d11STodd Fiala 
273511e5cdcSTodd Fiala bool
274511e5cdcSTodd Fiala NativeThreadLinux::IsStopped (int *signo)
275511e5cdcSTodd Fiala {
276511e5cdcSTodd Fiala     if (!StateIsStoppedState (m_state, false))
277511e5cdcSTodd Fiala         return false;
278511e5cdcSTodd Fiala 
279511e5cdcSTodd Fiala     // If we are stopped by a signal, return the signo.
280511e5cdcSTodd Fiala     if (signo &&
281511e5cdcSTodd Fiala         m_state == StateType::eStateStopped &&
282511e5cdcSTodd Fiala         m_stop_info.reason == StopReason::eStopReasonSignal)
283511e5cdcSTodd Fiala     {
284511e5cdcSTodd Fiala         *signo = m_stop_info.details.signal.signo;
285511e5cdcSTodd Fiala     }
286511e5cdcSTodd Fiala 
287511e5cdcSTodd Fiala     // Regardless, we are stopped.
288511e5cdcSTodd Fiala     return true;
289511e5cdcSTodd Fiala }
290511e5cdcSTodd Fiala 
291511e5cdcSTodd Fiala 
292af245d11STodd Fiala void
293a9882ceeSTodd Fiala NativeThreadLinux::SetStoppedByExec ()
294a9882ceeSTodd Fiala {
295a9882ceeSTodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
296a9882ceeSTodd Fiala     if (log)
297a9882ceeSTodd Fiala         log->Printf ("NativeThreadLinux::%s()", __FUNCTION__);
298a9882ceeSTodd Fiala 
299a9882ceeSTodd Fiala     const StateType new_state = StateType::eStateStopped;
300a9882ceeSTodd Fiala     MaybeLogStateChange (new_state);
301a9882ceeSTodd Fiala     m_state = new_state;
302a9882ceeSTodd Fiala 
303a9882ceeSTodd Fiala     m_stop_info.reason = StopReason::eStopReasonExec;
304a9882ceeSTodd Fiala     m_stop_info.details.signal.signo = SIGSTOP;
305a9882ceeSTodd Fiala }
306a9882ceeSTodd Fiala 
307a9882ceeSTodd Fiala void
308af245d11STodd Fiala NativeThreadLinux::SetStoppedByBreakpoint ()
309af245d11STodd Fiala {
310af245d11STodd Fiala     const StateType new_state = StateType::eStateStopped;
311af245d11STodd Fiala     MaybeLogStateChange (new_state);
312af245d11STodd Fiala     m_state = new_state;
313af245d11STodd Fiala 
314*28e57429SChaoren Lin     m_stop_info.reason = StopReason::eStopReasonBreakpoint;
315af245d11STodd Fiala     m_stop_info.details.signal.signo = SIGTRAP;
316af245d11STodd Fiala }
317af245d11STodd Fiala 
318af245d11STodd Fiala bool
319af245d11STodd Fiala NativeThreadLinux::IsStoppedAtBreakpoint ()
320af245d11STodd Fiala {
321af245d11STodd Fiala     // Are we stopped? If not, this can't be a breakpoint.
322af245d11STodd Fiala     if (GetState () != StateType::eStateStopped)
323af245d11STodd Fiala         return false;
324af245d11STodd Fiala 
325af245d11STodd Fiala     // Was the stop reason a signal with signal number SIGTRAP? If not, not a breakpoint.
326*28e57429SChaoren Lin     return (m_stop_info.reason == StopReason::eStopReasonBreakpoint) &&
327af245d11STodd Fiala             (m_stop_info.details.signal.signo == SIGTRAP);
328af245d11STodd Fiala }
329af245d11STodd Fiala 
330af245d11STodd Fiala void
331*28e57429SChaoren Lin NativeThreadLinux::SetStoppedByTrace ()
332*28e57429SChaoren Lin {
333*28e57429SChaoren Lin     const StateType new_state = StateType::eStateStopped;
334*28e57429SChaoren Lin     MaybeLogStateChange (new_state);
335*28e57429SChaoren Lin     m_state = new_state;
336*28e57429SChaoren Lin 
337*28e57429SChaoren Lin     m_stop_info.reason = StopReason::eStopReasonTrace;
338*28e57429SChaoren Lin     m_stop_info.details.signal.signo = SIGTRAP;
339*28e57429SChaoren Lin }
340*28e57429SChaoren Lin 
341*28e57429SChaoren Lin void
342*28e57429SChaoren Lin NativeThreadLinux::SetCrashedWithException (const siginfo_t& info)
343af245d11STodd Fiala {
344af245d11STodd Fiala     const StateType new_state = StateType::eStateCrashed;
345af245d11STodd Fiala     MaybeLogStateChange (new_state);
346af245d11STodd Fiala     m_state = new_state;
347af245d11STodd Fiala 
348af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonException;
349*28e57429SChaoren Lin     m_stop_info.details.signal.signo = info.si_signo;
350af245d11STodd Fiala 
351*28e57429SChaoren Lin     const auto reason = GetCrashReason (info);
352*28e57429SChaoren Lin     m_stop_description = GetCrashReasonString (reason, reinterpret_cast<lldb::addr_t> (info.si_addr));
353*28e57429SChaoren Lin }
354af245d11STodd Fiala 
355af245d11STodd Fiala void
356af245d11STodd Fiala NativeThreadLinux::SetSuspended ()
357af245d11STodd Fiala {
358af245d11STodd Fiala     const StateType new_state = StateType::eStateSuspended;
359af245d11STodd Fiala     MaybeLogStateChange (new_state);
360af245d11STodd Fiala     m_state = new_state;
361af245d11STodd Fiala 
362af245d11STodd Fiala     // FIXME what makes sense here? Do we need a suspended StopReason?
363af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonNone;
364af245d11STodd Fiala }
365af245d11STodd Fiala 
366af245d11STodd Fiala void
367af245d11STodd Fiala NativeThreadLinux::SetExited ()
368af245d11STodd Fiala {
369af245d11STodd Fiala     const StateType new_state = StateType::eStateExited;
370af245d11STodd Fiala     MaybeLogStateChange (new_state);
371af245d11STodd Fiala     m_state = new_state;
372af245d11STodd Fiala 
373af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonThreadExiting;
374af245d11STodd Fiala }
375af245d11STodd Fiala 
376af245d11STodd Fiala void
377af245d11STodd Fiala NativeThreadLinux::MaybeLogStateChange (lldb::StateType new_state)
378af245d11STodd Fiala {
379af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
380af245d11STodd Fiala     // If we're not logging, we're done.
381af245d11STodd Fiala     if (!log)
382af245d11STodd Fiala         return;
383af245d11STodd Fiala 
384af245d11STodd Fiala     // If this is a state change to the same state, we're done.
385af245d11STodd Fiala     lldb::StateType old_state = m_state;
386af245d11STodd Fiala     if (new_state == old_state)
387af245d11STodd Fiala         return;
388af245d11STodd Fiala 
389af245d11STodd Fiala     NativeProcessProtocolSP m_process_sp = m_process_wp.lock ();
390af245d11STodd Fiala     lldb::pid_t pid = m_process_sp ? m_process_sp->GetID () : LLDB_INVALID_PROCESS_ID;
391af245d11STodd Fiala 
392af245d11STodd Fiala     // Log it.
393af245d11STodd Fiala     log->Printf ("NativeThreadLinux: thread (pid=%" PRIu64 ", tid=%" PRIu64 ") changing from state %s to %s", pid, GetID (), StateAsCString (old_state), StateAsCString (new_state));
394af245d11STodd Fiala }
395