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