blob: d9e80d6c459513bbefe2dc29be3cf10a4ba31589 [file] [log] [blame]
Todd Fialaaf245d12014-06-30 21:05:18 +00001//===-- 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"
Todd Fiala2850b1b2014-06-30 23:51:35 +000015#include "NativeRegisterContextLinux_x86_64.h"
16
Todd Fialaaf245d12014-06-30 21:05:18 +000017#include "lldb/Core/Log.h"
18#include "lldb/Core/State.h"
19#include "lldb/Host/Host.h"
Zachary Turner13b18262014-08-20 16:42:51 +000020#include "lldb/Host/HostInfo.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000021#include "lldb/lldb-enumerations.h"
22#include "lldb/lldb-private-log.h"
Todd Fialab71e89e2014-08-29 16:01:35 +000023#include "Plugins/Process/Utility/RegisterContextLinux_arm64.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000024#include "Plugins/Process/Utility/RegisterContextLinux_i386.h"
25#include "Plugins/Process/Utility/RegisterContextLinux_x86_64.h"
26#include "Plugins/Process/Utility/RegisterInfoInterface.h"
27
28using namespace lldb;
29using namespace lldb_private;
30
31namespace
32{
33 void LogThreadStopInfo (Log &log, const ThreadStopInfo &stop_info, const char *const header)
34 {
35 switch (stop_info.reason)
36 {
37 case eStopReasonSignal:
Todd Fialaa9882ce2014-08-28 15:46:54 +000038 log.Printf ("%s: %s signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo);
Todd Fialaaf245d12014-06-30 21:05:18 +000039 return;
40 case eStopReasonException:
Todd Fialaa9882ce2014-08-28 15:46:54 +000041 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);
Todd Fialaaf245d12014-06-30 21:05:18 +000045 return;
46 default:
Todd Fialaa9882ce2014-08-28 15:46:54 +000047 log.Printf ("%s: %s invalid stop reason %" PRIu32, __FUNCTION__, header, static_cast<uint32_t> (stop_info.reason));
Todd Fialaaf245d12014-06-30 21:05:18 +000048 }
49 }
50}
51
52NativeThreadLinux::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
60const char *
61NativeThreadLinux::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
71lldb::StateType
72NativeThreadLinux::GetState ()
73{
74 return m_state;
75}
76
77
78bool
79NativeThreadLinux::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)
Todd Fialaa9882ce2014-08-28 15:46:54 +000090 LogThreadStopInfo (*log, m_stop_info, "m_stop_info in thread:");
Todd Fialaaf245d12014-06-30 21:05:18 +000091 stop_info = m_stop_info;
92 if (log)
Todd Fialaa9882ce2014-08-28 15:46:54 +000093 LogThreadStopInfo (*log, stop_info, "returned stop_info:");
Todd Fialaaf245d12014-06-30 21:05:18 +000094 return true;
95
96 case eStateInvalid:
97 case eStateConnected:
98 case eStateAttaching:
99 case eStateLaunching:
100 case eStateRunning:
101 case eStateStepping:
102 case eStateDetached:
Todd Fialaaf245d12014-06-30 21:05:18 +0000103 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
112lldb_private::NativeRegisterContextSP
113NativeThreadLinux::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 {
Todd Fialab71e89e2014-08-29 16:01:35 +0000134 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;
Todd Fialaaf245d12014-06-30 21:05:18 +0000138 case llvm::Triple::x86:
139 case llvm::Triple::x86_64:
Zachary Turner13b18262014-08-20 16:42:51 +0000140 if (HostInfo::GetArchitecture().GetAddressByteSize() == 4)
Todd Fialaaf245d12014-06-30 21:05:18 +0000141 {
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 {
Zachary Turner13b18262014-08-20 16:42:51 +0000147 assert((HostInfo::GetArchitecture().GetAddressByteSize() == 8) &&
148 "Register setting path assumes this is a 64-bit host");
Todd Fialaaf245d12014-06-30 21:05:18 +0000149 // 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
193Error
194NativeThreadLinux::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
200Error
201NativeThreadLinux::RemoveWatchpoint (lldb::addr_t addr)
202{
203 // TODO implement
204 return Error ("not implemented");
205}
206
207void
208NativeThreadLinux::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
221void
222NativeThreadLinux::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
231void
232NativeThreadLinux::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
241void
242NativeThreadLinux::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
256void
Todd Fialaa9882ce2014-08-28 15:46:54 +0000257NativeThreadLinux::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
271void
Todd Fialaaf245d12014-06-30 21:05:18 +0000272NativeThreadLinux::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
282bool
283NativeThreadLinux::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
294void
295NativeThreadLinux::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
308void
309NativeThreadLinux::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
319void
320NativeThreadLinux::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
329void
330NativeThreadLinux::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
Todd Fialaaf245d12014-06-30 21:05:18 +0000349uint32_t
350NativeThreadLinux::TranslateStopInfoToGdbSignal (const ThreadStopInfo &stop_info) const
351{
352 switch (stop_info.reason)
353 {
354 case eStopReasonSignal:
Todd Fiala22972a72014-08-27 17:11:56 +0000355 // No translation.
356 return stop_info.details.signal.signo;
Todd Fialaaf245d12014-06-30 21:05:18 +0000357
358 case eStopReasonException:
Todd Fiala22972a72014-08-27 17:11:56 +0000359 {
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 }
Todd Fialaaf245d12014-06-30 21:05:18 +0000371
372 default:
373 assert (0 && "unexpected stop_info.reason found");
374 return 0;
375 }
376}
377