blob: c24e3fd1a8fb1e1640b4461c81d37a9ebd01f0db [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"
Zachary Turner39de3112014-09-09 20:54:56 +000021#include "lldb/Host/HostNativeThread.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000022#include "lldb/lldb-enumerations.h"
23#include "lldb/lldb-private-log.h"
Zachary Turner39de3112014-09-09 20:54:56 +000024
25#include "llvm/ADT/SmallString.h"
26
Todd Fialab71e89e2014-08-29 16:01:35 +000027#include "Plugins/Process/Utility/RegisterContextLinux_arm64.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000028#include "Plugins/Process/Utility/RegisterContextLinux_i386.h"
29#include "Plugins/Process/Utility/RegisterContextLinux_x86_64.h"
30#include "Plugins/Process/Utility/RegisterInfoInterface.h"
31
32using namespace lldb;
33using namespace lldb_private;
34
35namespace
36{
37 void LogThreadStopInfo (Log &log, const ThreadStopInfo &stop_info, const char *const header)
38 {
39 switch (stop_info.reason)
40 {
41 case eStopReasonSignal:
Chaoren Linae29d392015-02-03 01:50:46 +000042 log.Printf ("%s: %s signal 0x%02" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo);
Todd Fialaaf245d12014-06-30 21:05:18 +000043 return;
44 case eStopReasonException:
Chaoren Linae29d392015-02-03 01:50:46 +000045 log.Printf ("%s: %s exception type 0x%02" PRIx64, __FUNCTION__, header, stop_info.details.exception.type);
Todd Fialaa9882ce2014-08-28 15:46:54 +000046 return;
47 case eStopReasonExec:
48 log.Printf ("%s: %s exec, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo);
Todd Fialaaf245d12014-06-30 21:05:18 +000049 return;
50 default:
Todd Fialaa9882ce2014-08-28 15:46:54 +000051 log.Printf ("%s: %s invalid stop reason %" PRIu32, __FUNCTION__, header, static_cast<uint32_t> (stop_info.reason));
Todd Fialaaf245d12014-06-30 21:05:18 +000052 }
53 }
54}
55
56NativeThreadLinux::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
Todd Fiala7206c6d2014-09-12 22:51:49 +000064std::string
Todd Fialaaf245d12014-06-30 21:05:18 +000065NativeThreadLinux::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 ());
Zachary Turner39de3112014-09-09 20:54:56 +000072 llvm::SmallString<32> thread_name;
73 HostNativeThread::GetName(GetID(), thread_name);
74 return thread_name.c_str();
Todd Fialaaf245d12014-06-30 21:05:18 +000075}
76
77lldb::StateType
78NativeThreadLinux::GetState ()
79{
80 return m_state;
81}
82
83
84bool
85NativeThreadLinux::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)
Todd Fialaa9882ce2014-08-28 15:46:54 +000096 LogThreadStopInfo (*log, m_stop_info, "m_stop_info in thread:");
Todd Fialaaf245d12014-06-30 21:05:18 +000097 stop_info = m_stop_info;
98 if (log)
Todd Fialaa9882ce2014-08-28 15:46:54 +000099 LogThreadStopInfo (*log, stop_info, "returned stop_info:");
Todd Fialaaf245d12014-06-30 21:05:18 +0000100 return true;
101
102 case eStateInvalid:
103 case eStateConnected:
104 case eStateAttaching:
105 case eStateLaunching:
106 case eStateRunning:
107 case eStateStepping:
108 case eStateDetached:
Todd Fialaaf245d12014-06-30 21:05:18 +0000109 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 }
David Majnemer8faf9372014-09-16 06:34:29 +0000116 llvm_unreachable("unhandled StateType!");
Todd Fialaaf245d12014-06-30 21:05:18 +0000117}
118
119lldb_private::NativeRegisterContextSP
120NativeThreadLinux::GetRegisterContext ()
121{
122 // Return the register context if we already created it.
123 if (m_reg_context_sp)
124 return m_reg_context_sp;
125
126 // First select the appropriate RegisterInfoInterface.
127 RegisterInfoInterface *reg_interface = nullptr;
128 NativeProcessProtocolSP m_process_sp = m_process_wp.lock ();
129 if (!m_process_sp)
130 return NativeRegisterContextSP ();
131
132 ArchSpec target_arch;
133 if (!m_process_sp->GetArchitecture (target_arch))
134 return NativeRegisterContextSP ();
135
136 switch (target_arch.GetTriple().getOS())
137 {
138 case llvm::Triple::Linux:
139 switch (target_arch.GetMachine())
140 {
Todd Fialab71e89e2014-08-29 16:01:35 +0000141 case llvm::Triple::aarch64:
142 assert((HostInfo::GetArchitecture ().GetAddressByteSize() == 8) && "Register setting path assumes this is a 64-bit host");
143 reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_arm64(target_arch));
144 break;
Todd Fialaaf245d12014-06-30 21:05:18 +0000145 case llvm::Triple::x86:
146 case llvm::Triple::x86_64:
Zachary Turner13b18262014-08-20 16:42:51 +0000147 if (HostInfo::GetArchitecture().GetAddressByteSize() == 4)
Todd Fialaaf245d12014-06-30 21:05:18 +0000148 {
149 // 32-bit hosts run with a RegisterContextLinux_i386 context.
150 reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_i386(target_arch));
151 }
152 else
153 {
Zachary Turner13b18262014-08-20 16:42:51 +0000154 assert((HostInfo::GetArchitecture().GetAddressByteSize() == 8) &&
155 "Register setting path assumes this is a 64-bit host");
Todd Fialaaf245d12014-06-30 21:05:18 +0000156 // X86_64 hosts know how to work with 64-bit and 32-bit EXEs using the x86_64 register context.
157 reg_interface = static_cast<RegisterInfoInterface*> (new RegisterContextLinux_x86_64 (target_arch));
158 }
159 break;
160 default:
161 break;
162 }
163 break;
164 default:
165 break;
166 }
167
168 assert(reg_interface && "OS or CPU not supported!");
169 if (!reg_interface)
170 return NativeRegisterContextSP ();
171
172 // Now create the register context.
173 switch (target_arch.GetMachine())
174 {
175#if 0
176 case llvm::Triple::mips64:
177 {
178 RegisterContextPOSIXProcessMonitor_mips64 *reg_ctx = new RegisterContextPOSIXProcessMonitor_mips64(*this, 0, reg_interface);
179 m_posix_thread = reg_ctx;
180 m_reg_context_sp.reset(reg_ctx);
181 break;
182 }
183#endif
184#if 0
185 case llvm::Triple::x86:
186#endif
187 case llvm::Triple::x86_64:
188 {
189 const uint32_t concrete_frame_idx = 0;
190 m_reg_context_sp.reset (new NativeRegisterContextLinux_x86_64(*this, concrete_frame_idx, reg_interface));
191 break;
192 }
193 default:
194 break;
195 }
196
197 return m_reg_context_sp;
198}
199
200Error
201NativeThreadLinux::SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware)
202{
203 // TODO implement
204 return Error ("not implemented");
205}
206
207Error
208NativeThreadLinux::RemoveWatchpoint (lldb::addr_t addr)
209{
210 // TODO implement
211 return Error ("not implemented");
212}
213
214void
215NativeThreadLinux::SetLaunching ()
216{
217 const StateType new_state = StateType::eStateLaunching;
218 MaybeLogStateChange (new_state);
219 m_state = new_state;
220
221 // Also mark it as stopped since launching temporarily stops the newly created thread
222 // in the ptrace machinery.
223 m_stop_info.reason = StopReason::eStopReasonSignal;
224 m_stop_info.details.signal.signo = SIGSTOP;
225}
226
227
228void
229NativeThreadLinux::SetRunning ()
230{
231 const StateType new_state = StateType::eStateRunning;
232 MaybeLogStateChange (new_state);
233 m_state = new_state;
234
235 m_stop_info.reason = StopReason::eStopReasonNone;
236}
237
238void
239NativeThreadLinux::SetStepping ()
240{
241 const StateType new_state = StateType::eStateStepping;
242 MaybeLogStateChange (new_state);
243 m_state = new_state;
244
245 m_stop_info.reason = StopReason::eStopReasonNone;
246}
247
248void
249NativeThreadLinux::SetStoppedBySignal (uint32_t signo)
250{
251 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
252 if (log)
Chaoren Linb8af31d2015-02-03 01:50:51 +0000253 log->Printf ("NativeThreadLinux::%s called with signal 0x%02" PRIx32, __FUNCTION__, signo);
Todd Fialaaf245d12014-06-30 21:05:18 +0000254
255 const StateType new_state = StateType::eStateStopped;
256 MaybeLogStateChange (new_state);
257 m_state = new_state;
258
259 m_stop_info.reason = StopReason::eStopReasonSignal;
260 m_stop_info.details.signal.signo = signo;
261}
262
Todd Fiala511e5cd2014-09-11 23:29:14 +0000263bool
264NativeThreadLinux::IsStopped (int *signo)
265{
266 if (!StateIsStoppedState (m_state, false))
267 return false;
268
269 // If we are stopped by a signal, return the signo.
270 if (signo &&
271 m_state == StateType::eStateStopped &&
272 m_stop_info.reason == StopReason::eStopReasonSignal)
273 {
274 *signo = m_stop_info.details.signal.signo;
275 }
276
277 // Regardless, we are stopped.
278 return true;
279}
280
281
Todd Fialaaf245d12014-06-30 21:05:18 +0000282void
Todd Fialaa9882ce2014-08-28 15:46:54 +0000283NativeThreadLinux::SetStoppedByExec ()
284{
285 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
286 if (log)
287 log->Printf ("NativeThreadLinux::%s()", __FUNCTION__);
288
289 const StateType new_state = StateType::eStateStopped;
290 MaybeLogStateChange (new_state);
291 m_state = new_state;
292
293 m_stop_info.reason = StopReason::eStopReasonExec;
294 m_stop_info.details.signal.signo = SIGSTOP;
295}
296
297void
Todd Fialaaf245d12014-06-30 21:05:18 +0000298NativeThreadLinux::SetStoppedByBreakpoint ()
299{
300 const StateType new_state = StateType::eStateStopped;
301 MaybeLogStateChange (new_state);
302 m_state = new_state;
303
304 m_stop_info.reason = StopReason::eStopReasonSignal;
305 m_stop_info.details.signal.signo = SIGTRAP;
306}
307
308bool
309NativeThreadLinux::IsStoppedAtBreakpoint ()
310{
311 // Are we stopped? If not, this can't be a breakpoint.
312 if (GetState () != StateType::eStateStopped)
313 return false;
314
315 // Was the stop reason a signal with signal number SIGTRAP? If not, not a breakpoint.
316 return (m_stop_info.reason == StopReason::eStopReasonSignal) &&
317 (m_stop_info.details.signal.signo == SIGTRAP);
318}
319
320void
321NativeThreadLinux::SetCrashedWithException (uint64_t exception_type, lldb::addr_t exception_addr)
322{
323 const StateType new_state = StateType::eStateCrashed;
324 MaybeLogStateChange (new_state);
325 m_state = new_state;
326
327 m_stop_info.reason = StopReason::eStopReasonException;
328 m_stop_info.details.exception.type = exception_type;
329 m_stop_info.details.exception.data_count = 1;
330 m_stop_info.details.exception.data[0] = exception_addr;
331}
332
333
334void
335NativeThreadLinux::SetSuspended ()
336{
337 const StateType new_state = StateType::eStateSuspended;
338 MaybeLogStateChange (new_state);
339 m_state = new_state;
340
341 // FIXME what makes sense here? Do we need a suspended StopReason?
342 m_stop_info.reason = StopReason::eStopReasonNone;
343}
344
345void
346NativeThreadLinux::SetExited ()
347{
348 const StateType new_state = StateType::eStateExited;
349 MaybeLogStateChange (new_state);
350 m_state = new_state;
351
352 m_stop_info.reason = StopReason::eStopReasonThreadExiting;
353}
354
355void
356NativeThreadLinux::MaybeLogStateChange (lldb::StateType new_state)
357{
358 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
359 // If we're not logging, we're done.
360 if (!log)
361 return;
362
363 // If this is a state change to the same state, we're done.
364 lldb::StateType old_state = m_state;
365 if (new_state == old_state)
366 return;
367
368 NativeProcessProtocolSP m_process_sp = m_process_wp.lock ();
369 lldb::pid_t pid = m_process_sp ? m_process_sp->GetID () : LLDB_INVALID_PROCESS_ID;
370
371 // Log it.
372 log->Printf ("NativeThreadLinux: thread (pid=%" PRIu64 ", tid=%" PRIu64 ") changing from state %s to %s", pid, GetID (), StateAsCString (old_state), StateAsCString (new_state));
373}
374
Todd Fialaaf245d12014-06-30 21:05:18 +0000375uint32_t
376NativeThreadLinux::TranslateStopInfoToGdbSignal (const ThreadStopInfo &stop_info) const
377{
378 switch (stop_info.reason)
379 {
380 case eStopReasonSignal:
Todd Fiala22972a72014-08-27 17:11:56 +0000381 // No translation.
382 return stop_info.details.signal.signo;
Todd Fialaaf245d12014-06-30 21:05:18 +0000383
384 case eStopReasonException:
Todd Fiala22972a72014-08-27 17:11:56 +0000385 {
386 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
387 // FIXME I think the eStopReasonException is a xnu/Mach exception, which we
388 // shouldn't see on Linux.
389 // No translation.
390 if (log)
391 log->Printf ("NativeThreadLinux::%s saw an exception stop type (signo %"
392 PRIu64 "), not expecting to see exceptions on Linux",
393 __FUNCTION__,
394 stop_info.details.exception.type);
395 return static_cast<uint32_t> (stop_info.details.exception.type);
396 }
Todd Fialaaf245d12014-06-30 21:05:18 +0000397
398 default:
399 assert (0 && "unexpected stop_info.reason found");
400 return 0;
401 }
402}
403