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