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"
21af245d11STodd Fiala #include "lldb/lldb-enumerations.h"
22af245d11STodd Fiala #include "lldb/lldb-private-log.h"
23af245d11STodd Fiala #include "Plugins/Process/Utility/RegisterContextLinux_i386.h"
24af245d11STodd Fiala #include "Plugins/Process/Utility/RegisterContextLinux_x86_64.h"
25af245d11STodd Fiala #include "Plugins/Process/Utility/RegisterInfoInterface.h"
26af245d11STodd Fiala 
27af245d11STodd Fiala using namespace lldb;
28af245d11STodd Fiala using namespace lldb_private;
29af245d11STodd Fiala 
30af245d11STodd Fiala namespace
31af245d11STodd Fiala {
32af245d11STodd Fiala     void LogThreadStopInfo (Log &log, const ThreadStopInfo &stop_info, const char *const header)
33af245d11STodd Fiala     {
34af245d11STodd Fiala         switch (stop_info.reason)
35af245d11STodd Fiala         {
36af245d11STodd Fiala             case eStopReasonSignal:
37*a9882ceeSTodd Fiala                 log.Printf ("%s: %s signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo);
38af245d11STodd Fiala                 return;
39af245d11STodd Fiala             case eStopReasonException:
40*a9882ceeSTodd Fiala                 log.Printf ("%s: %s exception type 0x%" PRIx64, __FUNCTION__, header, stop_info.details.exception.type);
41*a9882ceeSTodd Fiala                 return;
42*a9882ceeSTodd Fiala             case eStopReasonExec:
43*a9882ceeSTodd Fiala                 log.Printf ("%s: %s exec, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo);
44af245d11STodd Fiala                 return;
45af245d11STodd Fiala             default:
46*a9882ceeSTodd Fiala                 log.Printf ("%s: %s invalid stop reason %" PRIu32, __FUNCTION__, header, static_cast<uint32_t> (stop_info.reason));
47af245d11STodd Fiala         }
48af245d11STodd Fiala     }
49af245d11STodd Fiala }
50af245d11STodd Fiala 
51af245d11STodd Fiala NativeThreadLinux::NativeThreadLinux (NativeProcessLinux *process, lldb::tid_t tid) :
52af245d11STodd Fiala     NativeThreadProtocol (process, tid),
53af245d11STodd Fiala     m_state (StateType::eStateInvalid),
54af245d11STodd Fiala     m_stop_info (),
55af245d11STodd Fiala     m_reg_context_sp ()
56af245d11STodd Fiala {
57af245d11STodd Fiala }
58af245d11STodd Fiala 
59af245d11STodd Fiala const char *
60af245d11STodd Fiala NativeThreadLinux::GetName()
61af245d11STodd Fiala {
62af245d11STodd Fiala     NativeProcessProtocolSP process_sp = m_process_wp.lock ();
63af245d11STodd Fiala     if (!process_sp)
64af245d11STodd Fiala         return "<unknown: no process>";
65af245d11STodd Fiala 
66af245d11STodd Fiala     // const NativeProcessLinux *const process = reinterpret_cast<NativeProcessLinux*> (process_sp->get ());
67af245d11STodd Fiala     return Host::GetThreadName (process_sp->GetID (), GetID ()).c_str ();
68af245d11STodd Fiala }
69af245d11STodd Fiala 
70af245d11STodd Fiala lldb::StateType
71af245d11STodd Fiala NativeThreadLinux::GetState ()
72af245d11STodd Fiala {
73af245d11STodd Fiala     return m_state;
74af245d11STodd Fiala }
75af245d11STodd Fiala 
76af245d11STodd Fiala 
77af245d11STodd Fiala bool
78af245d11STodd Fiala NativeThreadLinux::GetStopReason (ThreadStopInfo &stop_info)
79af245d11STodd Fiala {
80af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
81af245d11STodd Fiala     switch (m_state)
82af245d11STodd Fiala     {
83af245d11STodd Fiala     case eStateStopped:
84af245d11STodd Fiala     case eStateCrashed:
85af245d11STodd Fiala     case eStateExited:
86af245d11STodd Fiala     case eStateSuspended:
87af245d11STodd Fiala     case eStateUnloaded:
88af245d11STodd Fiala         if (log)
89af245d11STodd Fiala             LogThreadStopInfo (*log, m_stop_info, "m_stop_info in thread:");
90af245d11STodd Fiala         stop_info = m_stop_info;
91af245d11STodd Fiala         if (log)
92af245d11STodd Fiala             LogThreadStopInfo (*log, stop_info, "returned stop_info:");
93af245d11STodd Fiala         return true;
94af245d11STodd Fiala 
95af245d11STodd Fiala     case eStateInvalid:
96af245d11STodd Fiala     case eStateConnected:
97af245d11STodd Fiala     case eStateAttaching:
98af245d11STodd Fiala     case eStateLaunching:
99af245d11STodd Fiala     case eStateRunning:
100af245d11STodd Fiala     case eStateStepping:
101af245d11STodd Fiala     case eStateDetached:
102af245d11STodd Fiala         if (log)
103af245d11STodd Fiala         {
104af245d11STodd Fiala             log->Printf ("NativeThreadLinux::%s tid %" PRIu64 " in state %s cannot answer stop reason",
105af245d11STodd Fiala                     __FUNCTION__, GetID (), StateAsCString (m_state));
106af245d11STodd Fiala         }
107af245d11STodd Fiala         return false;
108af245d11STodd Fiala     }
109af245d11STodd Fiala }
110af245d11STodd Fiala 
111af245d11STodd Fiala lldb_private::NativeRegisterContextSP
112af245d11STodd Fiala NativeThreadLinux::GetRegisterContext ()
113af245d11STodd Fiala {
114af245d11STodd Fiala     // Return the register context if we already created it.
115af245d11STodd Fiala     if (m_reg_context_sp)
116af245d11STodd Fiala         return m_reg_context_sp;
117af245d11STodd Fiala 
118af245d11STodd Fiala     // First select the appropriate RegisterInfoInterface.
119af245d11STodd Fiala     RegisterInfoInterface *reg_interface = nullptr;
120af245d11STodd Fiala     NativeProcessProtocolSP m_process_sp = m_process_wp.lock ();
121af245d11STodd Fiala     if (!m_process_sp)
122af245d11STodd Fiala         return NativeRegisterContextSP ();
123af245d11STodd Fiala 
124af245d11STodd Fiala     ArchSpec target_arch;
125af245d11STodd Fiala     if (!m_process_sp->GetArchitecture (target_arch))
126af245d11STodd Fiala         return NativeRegisterContextSP ();
127af245d11STodd Fiala 
128af245d11STodd Fiala     switch (target_arch.GetTriple().getOS())
129af245d11STodd Fiala     {
130af245d11STodd Fiala         case llvm::Triple::Linux:
131af245d11STodd Fiala             switch (target_arch.GetMachine())
132af245d11STodd Fiala             {
133af245d11STodd Fiala             case llvm::Triple::x86:
134af245d11STodd Fiala             case llvm::Triple::x86_64:
13513b18261SZachary Turner                 if (HostInfo::GetArchitecture().GetAddressByteSize() == 4)
136af245d11STodd Fiala                 {
137af245d11STodd Fiala                     // 32-bit hosts run with a RegisterContextLinux_i386 context.
138af245d11STodd Fiala                     reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_i386(target_arch));
139af245d11STodd Fiala                 }
140af245d11STodd Fiala                 else
141af245d11STodd Fiala                 {
14213b18261SZachary Turner                     assert((HostInfo::GetArchitecture().GetAddressByteSize() == 8) &&
14313b18261SZachary Turner                            "Register setting path assumes this is a 64-bit host");
144af245d11STodd Fiala                     // X86_64 hosts know how to work with 64-bit and 32-bit EXEs using the x86_64 register context.
145af245d11STodd Fiala                     reg_interface = static_cast<RegisterInfoInterface*> (new RegisterContextLinux_x86_64 (target_arch));
146af245d11STodd Fiala                 }
147af245d11STodd Fiala                 break;
148af245d11STodd Fiala             default:
149af245d11STodd Fiala                 break;
150af245d11STodd Fiala             }
151af245d11STodd Fiala             break;
152af245d11STodd Fiala         default:
153af245d11STodd Fiala             break;
154af245d11STodd Fiala     }
155af245d11STodd Fiala 
156af245d11STodd Fiala     assert(reg_interface && "OS or CPU not supported!");
157af245d11STodd Fiala     if (!reg_interface)
158af245d11STodd Fiala         return NativeRegisterContextSP ();
159af245d11STodd Fiala 
160af245d11STodd Fiala     // Now create the register context.
161af245d11STodd Fiala     switch (target_arch.GetMachine())
162af245d11STodd Fiala     {
163af245d11STodd Fiala #if 0
164af245d11STodd Fiala         case llvm::Triple::mips64:
165af245d11STodd Fiala         {
166af245d11STodd Fiala             RegisterContextPOSIXProcessMonitor_mips64 *reg_ctx = new RegisterContextPOSIXProcessMonitor_mips64(*this, 0, reg_interface);
167af245d11STodd Fiala             m_posix_thread = reg_ctx;
168af245d11STodd Fiala             m_reg_context_sp.reset(reg_ctx);
169af245d11STodd Fiala             break;
170af245d11STodd Fiala         }
171af245d11STodd Fiala #endif
172af245d11STodd Fiala #if 0
173af245d11STodd Fiala         case llvm::Triple::x86:
174af245d11STodd Fiala #endif
175af245d11STodd Fiala         case llvm::Triple::x86_64:
176af245d11STodd Fiala         {
177af245d11STodd Fiala             const uint32_t concrete_frame_idx = 0;
178af245d11STodd Fiala             m_reg_context_sp.reset (new NativeRegisterContextLinux_x86_64(*this, concrete_frame_idx, reg_interface));
179af245d11STodd Fiala             break;
180af245d11STodd Fiala         }
181af245d11STodd Fiala         default:
182af245d11STodd Fiala             break;
183af245d11STodd Fiala     }
184af245d11STodd Fiala 
185af245d11STodd Fiala     return m_reg_context_sp;
186af245d11STodd Fiala }
187af245d11STodd Fiala 
188af245d11STodd Fiala Error
189af245d11STodd Fiala NativeThreadLinux::SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware)
190af245d11STodd Fiala {
191af245d11STodd Fiala     // TODO implement
192af245d11STodd Fiala     return Error ("not implemented");
193af245d11STodd Fiala }
194af245d11STodd Fiala 
195af245d11STodd Fiala Error
196af245d11STodd Fiala NativeThreadLinux::RemoveWatchpoint (lldb::addr_t addr)
197af245d11STodd Fiala {
198af245d11STodd Fiala     // TODO implement
199af245d11STodd Fiala     return Error ("not implemented");
200af245d11STodd Fiala }
201af245d11STodd Fiala 
202af245d11STodd Fiala void
203af245d11STodd Fiala NativeThreadLinux::SetLaunching ()
204af245d11STodd Fiala {
205af245d11STodd Fiala     const StateType new_state = StateType::eStateLaunching;
206af245d11STodd Fiala     MaybeLogStateChange (new_state);
207af245d11STodd Fiala     m_state = new_state;
208af245d11STodd Fiala 
209af245d11STodd Fiala     // Also mark it as stopped since launching temporarily stops the newly created thread
210af245d11STodd Fiala     // in the ptrace machinery.
211af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonSignal;
212af245d11STodd Fiala     m_stop_info.details.signal.signo = SIGSTOP;
213af245d11STodd Fiala }
214af245d11STodd Fiala 
215af245d11STodd Fiala 
216af245d11STodd Fiala void
217af245d11STodd Fiala NativeThreadLinux::SetRunning ()
218af245d11STodd Fiala {
219af245d11STodd Fiala     const StateType new_state = StateType::eStateRunning;
220af245d11STodd Fiala     MaybeLogStateChange (new_state);
221af245d11STodd Fiala     m_state = new_state;
222af245d11STodd Fiala 
223af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonNone;
224af245d11STodd Fiala }
225af245d11STodd Fiala 
226af245d11STodd Fiala void
227af245d11STodd Fiala NativeThreadLinux::SetStepping ()
228af245d11STodd Fiala {
229af245d11STodd Fiala     const StateType new_state = StateType::eStateStepping;
230af245d11STodd Fiala     MaybeLogStateChange (new_state);
231af245d11STodd Fiala     m_state = new_state;
232af245d11STodd Fiala 
233af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonNone;
234af245d11STodd Fiala }
235af245d11STodd Fiala 
236af245d11STodd Fiala void
237af245d11STodd Fiala NativeThreadLinux::SetStoppedBySignal (uint32_t signo)
238af245d11STodd Fiala {
239af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
240af245d11STodd Fiala     if (log)
241af245d11STodd Fiala         log->Printf ("NativeThreadLinux::%s called with signal 0x%" PRIx32, __FUNCTION__, signo);
242af245d11STodd Fiala 
243af245d11STodd Fiala     const StateType new_state = StateType::eStateStopped;
244af245d11STodd Fiala     MaybeLogStateChange (new_state);
245af245d11STodd Fiala     m_state = new_state;
246af245d11STodd Fiala 
247af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonSignal;
248af245d11STodd Fiala     m_stop_info.details.signal.signo = signo;
249af245d11STodd Fiala }
250af245d11STodd Fiala 
251af245d11STodd Fiala void
252*a9882ceeSTodd Fiala NativeThreadLinux::SetStoppedByExec ()
253*a9882ceeSTodd Fiala {
254*a9882ceeSTodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
255*a9882ceeSTodd Fiala     if (log)
256*a9882ceeSTodd Fiala         log->Printf ("NativeThreadLinux::%s()", __FUNCTION__);
257*a9882ceeSTodd Fiala 
258*a9882ceeSTodd Fiala     const StateType new_state = StateType::eStateStopped;
259*a9882ceeSTodd Fiala     MaybeLogStateChange (new_state);
260*a9882ceeSTodd Fiala     m_state = new_state;
261*a9882ceeSTodd Fiala 
262*a9882ceeSTodd Fiala     m_stop_info.reason = StopReason::eStopReasonExec;
263*a9882ceeSTodd Fiala     m_stop_info.details.signal.signo = SIGSTOP;
264*a9882ceeSTodd Fiala }
265*a9882ceeSTodd Fiala 
266*a9882ceeSTodd Fiala void
267af245d11STodd Fiala NativeThreadLinux::SetStoppedByBreakpoint ()
268af245d11STodd Fiala {
269af245d11STodd Fiala     const StateType new_state = StateType::eStateStopped;
270af245d11STodd Fiala     MaybeLogStateChange (new_state);
271af245d11STodd Fiala     m_state = new_state;
272af245d11STodd Fiala 
273af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonSignal;
274af245d11STodd Fiala     m_stop_info.details.signal.signo = SIGTRAP;
275af245d11STodd Fiala }
276af245d11STodd Fiala 
277af245d11STodd Fiala bool
278af245d11STodd Fiala NativeThreadLinux::IsStoppedAtBreakpoint ()
279af245d11STodd Fiala {
280af245d11STodd Fiala     // Are we stopped? If not, this can't be a breakpoint.
281af245d11STodd Fiala     if (GetState () != StateType::eStateStopped)
282af245d11STodd Fiala         return false;
283af245d11STodd Fiala 
284af245d11STodd Fiala     // Was the stop reason a signal with signal number SIGTRAP? If not, not a breakpoint.
285af245d11STodd Fiala     return (m_stop_info.reason == StopReason::eStopReasonSignal) &&
286af245d11STodd Fiala             (m_stop_info.details.signal.signo == SIGTRAP);
287af245d11STodd Fiala }
288af245d11STodd Fiala 
289af245d11STodd Fiala void
290af245d11STodd Fiala NativeThreadLinux::SetCrashedWithException (uint64_t exception_type, lldb::addr_t exception_addr)
291af245d11STodd Fiala {
292af245d11STodd Fiala     const StateType new_state = StateType::eStateCrashed;
293af245d11STodd Fiala     MaybeLogStateChange (new_state);
294af245d11STodd Fiala     m_state = new_state;
295af245d11STodd Fiala 
296af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonException;
297af245d11STodd Fiala     m_stop_info.details.exception.type = exception_type;
298af245d11STodd Fiala     m_stop_info.details.exception.data_count = 1;
299af245d11STodd Fiala     m_stop_info.details.exception.data[0] = exception_addr;
300af245d11STodd Fiala }
301af245d11STodd Fiala 
302af245d11STodd Fiala 
303af245d11STodd Fiala void
304af245d11STodd Fiala NativeThreadLinux::SetSuspended ()
305af245d11STodd Fiala {
306af245d11STodd Fiala     const StateType new_state = StateType::eStateSuspended;
307af245d11STodd Fiala     MaybeLogStateChange (new_state);
308af245d11STodd Fiala     m_state = new_state;
309af245d11STodd Fiala 
310af245d11STodd Fiala     // FIXME what makes sense here? Do we need a suspended StopReason?
311af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonNone;
312af245d11STodd Fiala }
313af245d11STodd Fiala 
314af245d11STodd Fiala void
315af245d11STodd Fiala NativeThreadLinux::SetExited ()
316af245d11STodd Fiala {
317af245d11STodd Fiala     const StateType new_state = StateType::eStateExited;
318af245d11STodd Fiala     MaybeLogStateChange (new_state);
319af245d11STodd Fiala     m_state = new_state;
320af245d11STodd Fiala 
321af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonThreadExiting;
322af245d11STodd Fiala }
323af245d11STodd Fiala 
324af245d11STodd Fiala void
325af245d11STodd Fiala NativeThreadLinux::MaybeLogStateChange (lldb::StateType new_state)
326af245d11STodd Fiala {
327af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
328af245d11STodd Fiala     // If we're not logging, we're done.
329af245d11STodd Fiala     if (!log)
330af245d11STodd Fiala         return;
331af245d11STodd Fiala 
332af245d11STodd Fiala     // If this is a state change to the same state, we're done.
333af245d11STodd Fiala     lldb::StateType old_state = m_state;
334af245d11STodd Fiala     if (new_state == old_state)
335af245d11STodd Fiala         return;
336af245d11STodd Fiala 
337af245d11STodd Fiala     NativeProcessProtocolSP m_process_sp = m_process_wp.lock ();
338af245d11STodd Fiala     lldb::pid_t pid = m_process_sp ? m_process_sp->GetID () : LLDB_INVALID_PROCESS_ID;
339af245d11STodd Fiala 
340af245d11STodd Fiala     // Log it.
341af245d11STodd Fiala     log->Printf ("NativeThreadLinux: thread (pid=%" PRIu64 ", tid=%" PRIu64 ") changing from state %s to %s", pid, GetID (), StateAsCString (old_state), StateAsCString (new_state));
342af245d11STodd Fiala }
343af245d11STodd Fiala 
344af245d11STodd Fiala uint32_t
345af245d11STodd Fiala NativeThreadLinux::TranslateStopInfoToGdbSignal (const ThreadStopInfo &stop_info) const
346af245d11STodd Fiala {
347af245d11STodd Fiala     switch (stop_info.reason)
348af245d11STodd Fiala     {
349af245d11STodd Fiala         case eStopReasonSignal:
35022972a7cSTodd Fiala             // No translation.
35122972a7cSTodd Fiala             return stop_info.details.signal.signo;
352af245d11STodd Fiala 
353af245d11STodd Fiala         case eStopReasonException:
35422972a7cSTodd Fiala             {
35522972a7cSTodd Fiala                 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
35622972a7cSTodd Fiala                 // FIXME I think the eStopReasonException is a xnu/Mach exception, which we
35722972a7cSTodd Fiala                 // shouldn't see on Linux.
35822972a7cSTodd Fiala                 // No translation.
35922972a7cSTodd Fiala                 if (log)
36022972a7cSTodd Fiala                     log->Printf ("NativeThreadLinux::%s saw an exception stop type (signo %"
36122972a7cSTodd Fiala                                  PRIu64 "), not expecting to see exceptions on Linux",
36222972a7cSTodd Fiala                                  __FUNCTION__,
36322972a7cSTodd Fiala                                  stop_info.details.exception.type);
36422972a7cSTodd Fiala                 return static_cast<uint32_t> (stop_info.details.exception.type);
36522972a7cSTodd Fiala             }
366af245d11STodd Fiala 
367af245d11STodd Fiala         default:
368af245d11STodd Fiala             assert (0 && "unexpected stop_info.reason found");
369af245d11STodd Fiala             return 0;
370af245d11STodd Fiala     }
371af245d11STodd Fiala }
372af245d11STodd Fiala 
373