blob: f97816e18b39c0d82df9d748cfea74ed41e23074 [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>
Chaoren Lin18fe6402015-02-03 01:51:47 +000013#include <sstream>
Todd Fialaaf245d12014-06-30 21:05:18 +000014
15#include "NativeProcessLinux.h"
Pavel Labath0f4b17d2015-08-24 13:25:54 +000016#include "NativeRegisterContextLinux.h"
Pavel Labath605b51b2016-02-23 13:56:30 +000017#include "SingleStepCheck.h"
Todd Fiala2850b1b2014-06-30 23:51:35 +000018
Todd Fialaaf245d12014-06-30 21:05:18 +000019#include "lldb/Core/State.h"
Zachary Turner39de3112014-09-09 20:54:56 +000020#include "lldb/Host/HostNativeThread.h"
Pavel Labath605b51b2016-02-23 13:56:30 +000021#include "lldb/Host/linux/Ptrace.h"
Chaoren Linc16f5dc2015-03-19 23:28:10 +000022#include "lldb/Utility/LLDBAssert.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000023#include "lldb/Utility/Log.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000024#include "lldb/lldb-enumerations.h"
Zachary Turner39de3112014-09-09 20:54:56 +000025
26#include "llvm/ADT/SmallString.h"
27
Chaoren Lin28e57422015-02-03 01:51:25 +000028#include "Plugins/Process/POSIX/CrashReason.h"
29
Pavel Labath8c8ff7a2015-05-11 10:03:10 +000030#include <sys/syscall.h>
31// Try to define a macro to encapsulate the tgkill syscall
Kate Stoneb9c1b512016-09-06 20:57:50 +000032#define tgkill(pid, tid, sig) \
33 syscall(__NR_tgkill, static_cast<::pid_t>(pid), static_cast<::pid_t>(tid), \
34 sig)
Pavel Labath8c8ff7a2015-05-11 10:03:10 +000035
Todd Fialaaf245d12014-06-30 21:05:18 +000036using namespace lldb;
37using namespace lldb_private;
Tamas Berghammerdb264a62015-03-31 09:52:22 +000038using namespace lldb_private::process_linux;
Todd Fialaaf245d12014-06-30 21:05:18 +000039
Kate Stoneb9c1b512016-09-06 20:57:50 +000040namespace {
41void LogThreadStopInfo(Log &log, const ThreadStopInfo &stop_info,
42 const char *const header) {
43 switch (stop_info.reason) {
44 case eStopReasonNone:
45 log.Printf("%s: %s no stop reason", __FUNCTION__, header);
46 return;
47 case eStopReasonTrace:
48 log.Printf("%s: %s trace, stopping signal 0x%" PRIx32, __FUNCTION__, header,
49 stop_info.details.signal.signo);
50 return;
51 case eStopReasonBreakpoint:
52 log.Printf("%s: %s breakpoint, stopping signal 0x%" PRIx32, __FUNCTION__,
53 header, stop_info.details.signal.signo);
54 return;
55 case eStopReasonWatchpoint:
56 log.Printf("%s: %s watchpoint, stopping signal 0x%" PRIx32, __FUNCTION__,
57 header, stop_info.details.signal.signo);
58 return;
59 case eStopReasonSignal:
60 log.Printf("%s: %s signal 0x%02" PRIx32, __FUNCTION__, header,
61 stop_info.details.signal.signo);
62 return;
63 case eStopReasonException:
64 log.Printf("%s: %s exception type 0x%02" PRIx64, __FUNCTION__, header,
65 stop_info.details.exception.type);
66 return;
67 case eStopReasonExec:
68 log.Printf("%s: %s exec, stopping signal 0x%" PRIx32, __FUNCTION__, header,
69 stop_info.details.signal.signo);
70 return;
71 case eStopReasonPlanComplete:
72 log.Printf("%s: %s plan complete", __FUNCTION__, header);
73 return;
74 case eStopReasonThreadExiting:
75 log.Printf("%s: %s thread exiting", __FUNCTION__, header);
76 return;
77 case eStopReasonInstrumentation:
78 log.Printf("%s: %s instrumentation", __FUNCTION__, header);
79 return;
80 default:
81 log.Printf("%s: %s invalid stop reason %" PRIu32, __FUNCTION__, header,
82 static_cast<uint32_t>(stop_info.reason));
83 }
84}
Todd Fialaaf245d12014-06-30 21:05:18 +000085}
86
Kate Stoneb9c1b512016-09-06 20:57:50 +000087NativeThreadLinux::NativeThreadLinux(NativeProcessLinux *process,
88 lldb::tid_t tid)
89 : NativeThreadProtocol(process, tid), m_state(StateType::eStateInvalid),
90 m_stop_info(), m_reg_context_sp(), m_stop_description() {}
91
92std::string NativeThreadLinux::GetName() {
93 NativeProcessProtocolSP process_sp = m_process_wp.lock();
94 if (!process_sp)
95 return "<unknown: no process>";
96
97 // const NativeProcessLinux *const process =
98 // reinterpret_cast<NativeProcessLinux*> (process_sp->get ());
99 llvm::SmallString<32> thread_name;
100 HostNativeThread::GetName(GetID(), thread_name);
101 return thread_name.c_str();
Todd Fialaaf245d12014-06-30 21:05:18 +0000102}
103
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104lldb::StateType NativeThreadLinux::GetState() { return m_state; }
Todd Fialaaf245d12014-06-30 21:05:18 +0000105
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106bool NativeThreadLinux::GetStopReason(ThreadStopInfo &stop_info,
107 std::string &description) {
108 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
Todd Fialaaf245d12014-06-30 21:05:18 +0000109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110 description.clear();
Todd Fialaaf245d12014-06-30 21:05:18 +0000111
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112 switch (m_state) {
113 case eStateStopped:
114 case eStateCrashed:
115 case eStateExited:
116 case eStateSuspended:
117 case eStateUnloaded:
Todd Fialaaf245d12014-06-30 21:05:18 +0000118 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119 LogThreadStopInfo(*log, m_stop_info, "m_stop_info in thread:");
120 stop_info = m_stop_info;
121 description = m_stop_description;
122 if (log)
123 LogThreadStopInfo(*log, stop_info, "returned stop_info:");
Todd Fialaaf245d12014-06-30 21:05:18 +0000124
Todd Fiala511e5cd2014-09-11 23:29:14 +0000125 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126
127 case eStateInvalid:
128 case eStateConnected:
129 case eStateAttaching:
130 case eStateLaunching:
131 case eStateRunning:
132 case eStateStepping:
133 case eStateDetached:
134 if (log) {
135 log->Printf("NativeThreadLinux::%s tid %" PRIu64
136 " in state %s cannot answer stop reason",
137 __FUNCTION__, GetID(), StateAsCString(m_state));
138 }
139 return false;
140 }
141 llvm_unreachable("unhandled StateType!");
Todd Fiala511e5cd2014-09-11 23:29:14 +0000142}
143
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144NativeRegisterContextSP NativeThreadLinux::GetRegisterContext() {
145 // Return the register context if we already created it.
146 if (m_reg_context_sp)
147 return m_reg_context_sp;
Pavel Labath605b51b2016-02-23 13:56:30 +0000148
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149 NativeProcessProtocolSP m_process_sp = m_process_wp.lock();
150 if (!m_process_sp)
151 return NativeRegisterContextSP();
152
153 ArchSpec target_arch;
154 if (!m_process_sp->GetArchitecture(target_arch))
155 return NativeRegisterContextSP();
156
157 const uint32_t concrete_frame_idx = 0;
158 m_reg_context_sp.reset(
159 NativeRegisterContextLinux::CreateHostNativeRegisterContextLinux(
160 target_arch, *this, concrete_frame_idx));
161
162 return m_reg_context_sp;
Pavel Labath605b51b2016-02-23 13:56:30 +0000163}
Todd Fiala511e5cd2014-09-11 23:29:14 +0000164
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165Error NativeThreadLinux::SetWatchpoint(lldb::addr_t addr, size_t size,
166 uint32_t watch_flags, bool hardware) {
167 if (!hardware)
168 return Error("not implemented");
169 if (m_state == eStateLaunching)
Mehdi Amini665be502016-11-11 05:07:57 +0000170 return Error();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171 Error error = RemoveWatchpoint(addr);
172 if (error.Fail())
173 return error;
174 NativeRegisterContextSP reg_ctx = GetRegisterContext();
175 uint32_t wp_index = reg_ctx->SetHardwareWatchpoint(addr, size, watch_flags);
176 if (wp_index == LLDB_INVALID_INDEX32)
177 return Error("Setting hardware watchpoint failed.");
178 m_watchpoint_index_map.insert({addr, wp_index});
Mehdi Amini665be502016-11-11 05:07:57 +0000179 return Error();
Todd Fialaa9882ce2014-08-28 15:46:54 +0000180}
181
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182Error NativeThreadLinux::RemoveWatchpoint(lldb::addr_t addr) {
183 auto wp = m_watchpoint_index_map.find(addr);
184 if (wp == m_watchpoint_index_map.end())
Mehdi Amini665be502016-11-11 05:07:57 +0000185 return Error();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186 uint32_t wp_index = wp->second;
187 m_watchpoint_index_map.erase(wp);
188 if (GetRegisterContext()->ClearHardwareWatchpoint(wp_index))
Mehdi Amini665be502016-11-11 05:07:57 +0000189 return Error();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190 return Error("Clearing hardware watchpoint failed.");
Chaoren Lin18fe6402015-02-03 01:51:47 +0000191}
192
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000193Error NativeThreadLinux::SetHardwareBreakpoint(lldb::addr_t addr, size_t size) {
194 if (m_state == eStateLaunching)
195 return Error();
196
197 Error error = RemoveHardwareBreakpoint(addr);
198 if (error.Fail())
199 return error;
200
201 NativeRegisterContextSP reg_ctx = GetRegisterContext();
202 uint32_t bp_index = reg_ctx->SetHardwareBreakpoint(addr, size);
203
204 if (bp_index == LLDB_INVALID_INDEX32)
205 return Error("Setting hardware breakpoint failed.");
206
207 m_hw_break_index_map.insert({addr, bp_index});
208 return Error();
209}
210
211Error NativeThreadLinux::RemoveHardwareBreakpoint(lldb::addr_t addr) {
212 auto bp = m_hw_break_index_map.find(addr);
213 if (bp == m_hw_break_index_map.end())
214 return Error();
215
216 uint32_t bp_index = bp->second;
217 if (GetRegisterContext()->ClearHardwareBreakpoint(bp_index)) {
218 m_hw_break_index_map.erase(bp);
219 return Error();
220 }
221
222 return Error("Clearing hardware breakpoint failed.");
223}
224
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225Error NativeThreadLinux::Resume(uint32_t signo) {
226 const StateType new_state = StateType::eStateRunning;
227 MaybeLogStateChange(new_state);
228 m_state = new_state;
Tamas Berghammereadb2a92015-03-17 14:40:57 +0000229
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230 m_stop_info.reason = StopReason::eStopReasonNone;
231 m_stop_description.clear();
Tamas Berghammereadb2a92015-03-17 14:40:57 +0000232
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233 // If watchpoints have been set, but none on this thread,
234 // then this is a new thread. So set all existing watchpoints.
235 if (m_watchpoint_index_map.empty()) {
Pavel Labath605b51b2016-02-23 13:56:30 +0000236 NativeProcessLinux &process = GetProcess();
Pavel Labath8c8ff7a2015-05-11 10:03:10 +0000237
Kate Stoneb9c1b512016-09-06 20:57:50 +0000238 const auto &watchpoint_map = process.GetWatchpointMap();
239 GetRegisterContext()->ClearAllHardwareWatchpoints();
240 for (const auto &pair : watchpoint_map) {
241 const auto &wp = pair.second;
242 SetWatchpoint(wp.m_addr, wp.m_size, wp.m_watch_flags, wp.m_hardware);
Pavel Labath8c8ff7a2015-05-11 10:03:10 +0000243 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244 }
Pavel Labath8c8ff7a2015-05-11 10:03:10 +0000245
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000246 // Set all active hardware breakpoint on all threads.
247 if (m_hw_break_index_map.empty()) {
248 NativeProcessLinux &process = GetProcess();
249
250 const auto &hw_breakpoint_map = process.GetHardwareBreakpointMap();
251 GetRegisterContext()->ClearAllHardwareBreakpoints();
252 for (const auto &pair : hw_breakpoint_map) {
253 const auto &bp = pair.second;
254 SetHardwareBreakpoint(bp.m_addr, bp.m_size);
255 }
256 }
257
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258 intptr_t data = 0;
259
260 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
261 data = signo;
262
263 return NativeProcessLinux::PtraceWrapper(PTRACE_CONT, GetID(), nullptr,
264 reinterpret_cast<void *>(data));
Pavel Labath8c8ff7a2015-05-11 10:03:10 +0000265}
266
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267Error NativeThreadLinux::SingleStep(uint32_t signo) {
268 const StateType new_state = StateType::eStateStepping;
269 MaybeLogStateChange(new_state);
270 m_state = new_state;
271 m_stop_info.reason = StopReason::eStopReasonNone;
Pavel Labatha37bbbd2017-02-17 11:48:34 +0000272
273 if(!m_step_workaround) {
274 // If we already hava a workaround inplace, don't reset it. Otherwise, the
275 // destructor of the existing instance will run after the new instance has
276 // fetched the cpu mask, and the thread will end up with the wrong mask.
277 m_step_workaround = SingleStepWorkaround::Get(m_tid);
278 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279
280 intptr_t data = 0;
281 if (signo != LLDB_INVALID_SIGNAL_NUMBER)
282 data = signo;
283
284 // If hardware single-stepping is not supported, we just do a continue. The
285 // breakpoint on the
286 // next instruction has been setup in NativeProcessLinux::Resume.
287 return NativeProcessLinux::PtraceWrapper(
288 GetProcess().SupportHardwareSingleStepping() ? PTRACE_SINGLESTEP
289 : PTRACE_CONT,
290 m_tid, nullptr, reinterpret_cast<void *>(data));
291}
292
293void NativeThreadLinux::SetStoppedBySignal(uint32_t signo,
294 const siginfo_t *info) {
295 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
296 if (log)
297 log->Printf("NativeThreadLinux::%s called with signal 0x%02" PRIx32,
298 __FUNCTION__, signo);
299
300 SetStopped();
301
302 m_stop_info.reason = StopReason::eStopReasonSignal;
303 m_stop_info.details.signal.signo = signo;
304
305 m_stop_description.clear();
306 if (info) {
307 switch (signo) {
308 case SIGSEGV:
309 case SIGBUS:
310 case SIGFPE:
311 case SIGILL:
312 // In case of MIPS64 target, SI_KERNEL is generated for invalid 64bit
313 // address.
314 const auto reason =
315 (info->si_signo == SIGBUS && info->si_code == SI_KERNEL)
316 ? CrashReason::eInvalidAddress
317 : GetCrashReason(*info);
Valentina Giusti6f8c1f82016-10-06 18:05:12 +0000318 m_stop_description = GetCrashReasonString(reason, *info);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000319 break;
320 }
321 }
322}
323
324bool NativeThreadLinux::IsStopped(int *signo) {
325 if (!StateIsStoppedState(m_state, false))
326 return false;
327
328 // If we are stopped by a signal, return the signo.
329 if (signo && m_state == StateType::eStateStopped &&
330 m_stop_info.reason == StopReason::eStopReasonSignal) {
331 *signo = m_stop_info.details.signal.signo;
332 }
333
334 // Regardless, we are stopped.
335 return true;
336}
337
338void NativeThreadLinux::SetStopped() {
339 if (m_state == StateType::eStateStepping)
Pavel Labath8abd34f2017-01-25 11:19:45 +0000340 m_step_workaround.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000341
342 const StateType new_state = StateType::eStateStopped;
343 MaybeLogStateChange(new_state);
344 m_state = new_state;
345 m_stop_description.clear();
346}
347
348void NativeThreadLinux::SetStoppedByExec() {
349 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
350 if (log)
351 log->Printf("NativeThreadLinux::%s()", __FUNCTION__);
352
353 SetStopped();
354
355 m_stop_info.reason = StopReason::eStopReasonExec;
356 m_stop_info.details.signal.signo = SIGSTOP;
357}
358
359void NativeThreadLinux::SetStoppedByBreakpoint() {
360 SetStopped();
361
362 m_stop_info.reason = StopReason::eStopReasonBreakpoint;
363 m_stop_info.details.signal.signo = SIGTRAP;
364 m_stop_description.clear();
365}
366
367void NativeThreadLinux::SetStoppedByWatchpoint(uint32_t wp_index) {
368 SetStopped();
369
370 lldbassert(wp_index != LLDB_INVALID_INDEX32 && "wp_index cannot be invalid");
371
372 std::ostringstream ostr;
373 ostr << GetRegisterContext()->GetWatchpointAddress(wp_index) << " ";
374 ostr << wp_index;
375
376 /*
377 * MIPS: Last 3bits of the watchpoint address are masked by the kernel. For
378 * example:
379 * 'n' is at 0x120010d00 and 'm' is 0x120010d04. When a watchpoint is set at
380 * 'm', then
381 * watch exception is generated even when 'n' is read/written. To handle this
382 * case,
383 * find the base address of the load/store instruction and append it in the
384 * stop-info
385 * packet.
386 */
387 ostr << " " << GetRegisterContext()->GetWatchpointHitAddress(wp_index);
388
389 m_stop_description = ostr.str();
390
391 m_stop_info.reason = StopReason::eStopReasonWatchpoint;
392 m_stop_info.details.signal.signo = SIGTRAP;
393}
394
395bool NativeThreadLinux::IsStoppedAtBreakpoint() {
396 return GetState() == StateType::eStateStopped &&
397 m_stop_info.reason == StopReason::eStopReasonBreakpoint;
398}
399
400bool NativeThreadLinux::IsStoppedAtWatchpoint() {
401 return GetState() == StateType::eStateStopped &&
402 m_stop_info.reason == StopReason::eStopReasonWatchpoint;
403}
404
405void NativeThreadLinux::SetStoppedByTrace() {
406 SetStopped();
407
408 m_stop_info.reason = StopReason::eStopReasonTrace;
409 m_stop_info.details.signal.signo = SIGTRAP;
410}
411
412void NativeThreadLinux::SetStoppedWithNoReason() {
413 SetStopped();
414
415 m_stop_info.reason = StopReason::eStopReasonNone;
416 m_stop_info.details.signal.signo = 0;
417}
418
419void NativeThreadLinux::SetExited() {
420 const StateType new_state = StateType::eStateExited;
421 MaybeLogStateChange(new_state);
422 m_state = new_state;
423
424 m_stop_info.reason = StopReason::eStopReasonThreadExiting;
425}
426
427Error NativeThreadLinux::RequestStop() {
428 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
429
430 NativeProcessLinux &process = GetProcess();
431
432 lldb::pid_t pid = process.GetID();
433 lldb::tid_t tid = GetID();
434
435 if (log)
436 log->Printf("NativeThreadLinux::%s requesting thread stop(pid: %" PRIu64
437 ", tid: %" PRIu64 ")",
438 __FUNCTION__, pid, tid);
439
440 Error err;
441 errno = 0;
442 if (::tgkill(pid, tid, SIGSTOP) != 0) {
443 err.SetErrorToErrno();
444 if (log)
445 log->Printf("NativeThreadLinux::%s tgkill(%" PRIu64 ", %" PRIu64
446 ", SIGSTOP) failed: %s",
447 __FUNCTION__, pid, tid, err.AsCString());
448 }
449
450 return err;
451}
452
453void NativeThreadLinux::MaybeLogStateChange(lldb::StateType new_state) {
454 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
455 // If we're not logging, we're done.
456 if (!log)
457 return;
458
459 // If this is a state change to the same state, we're done.
460 lldb::StateType old_state = m_state;
461 if (new_state == old_state)
462 return;
463
464 NativeProcessProtocolSP m_process_sp = m_process_wp.lock();
465 lldb::pid_t pid =
466 m_process_sp ? m_process_sp->GetID() : LLDB_INVALID_PROCESS_ID;
467
468 // Log it.
469 log->Printf("NativeThreadLinux: thread (pid=%" PRIu64 ", tid=%" PRIu64
470 ") changing from state %s to %s",
471 pid, GetID(), StateAsCString(old_state),
472 StateAsCString(new_state));
473}
474
475NativeProcessLinux &NativeThreadLinux::GetProcess() {
476 auto process_sp = std::static_pointer_cast<NativeProcessLinux>(
477 NativeThreadProtocol::GetProcess());
478 assert(process_sp);
479 return *process_sp;
Pavel Labath605b51b2016-02-23 13:56:30 +0000480}