xref: /openbsd-src/gnu/llvm/lldb/source/Plugins/Process/Linux/NativeThreadLinux.cpp (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1dda28197Spatrick //===-- NativeThreadLinux.cpp ---------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9061da546Spatrick #include "NativeThreadLinux.h"
10061da546Spatrick 
11be691f3bSpatrick #include <csignal>
12061da546Spatrick #include <sstream>
13061da546Spatrick 
14061da546Spatrick #include "NativeProcessLinux.h"
15061da546Spatrick #include "NativeRegisterContextLinux.h"
16061da546Spatrick #include "SingleStepCheck.h"
17061da546Spatrick 
18061da546Spatrick #include "lldb/Host/HostNativeThread.h"
19061da546Spatrick #include "lldb/Host/linux/Ptrace.h"
20061da546Spatrick #include "lldb/Host/linux/Support.h"
21061da546Spatrick #include "lldb/Utility/LLDBAssert.h"
22*f6aab3d8Srobert #include "lldb/Utility/LLDBLog.h"
23061da546Spatrick #include "lldb/Utility/Log.h"
24061da546Spatrick #include "lldb/Utility/State.h"
25061da546Spatrick #include "lldb/lldb-enumerations.h"
26061da546Spatrick 
27061da546Spatrick #include "llvm/ADT/SmallString.h"
28061da546Spatrick 
29061da546Spatrick #include "Plugins/Process/POSIX/CrashReason.h"
30be691f3bSpatrick #include "Plugins/Process/Utility/MemoryTagManagerAArch64MTE.h"
31061da546Spatrick 
32061da546Spatrick #include <sys/syscall.h>
33061da546Spatrick // Try to define a macro to encapsulate the tgkill syscall
34061da546Spatrick #define tgkill(pid, tid, sig)                                                  \
35061da546Spatrick   syscall(__NR_tgkill, static_cast<::pid_t>(pid), static_cast<::pid_t>(tid),   \
36061da546Spatrick           sig)
37061da546Spatrick 
38061da546Spatrick using namespace lldb;
39061da546Spatrick using namespace lldb_private;
40061da546Spatrick using namespace lldb_private::process_linux;
41061da546Spatrick 
42061da546Spatrick namespace {
LogThreadStopInfo(Log & log,const ThreadStopInfo & stop_info,const char * const header)43061da546Spatrick void LogThreadStopInfo(Log &log, const ThreadStopInfo &stop_info,
44061da546Spatrick                        const char *const header) {
45061da546Spatrick   switch (stop_info.reason) {
46061da546Spatrick   case eStopReasonNone:
47061da546Spatrick     log.Printf("%s: %s no stop reason", __FUNCTION__, header);
48061da546Spatrick     return;
49061da546Spatrick   case eStopReasonTrace:
50061da546Spatrick     log.Printf("%s: %s trace, stopping signal 0x%" PRIx32, __FUNCTION__, header,
51*f6aab3d8Srobert                stop_info.signo);
52061da546Spatrick     return;
53061da546Spatrick   case eStopReasonBreakpoint:
54061da546Spatrick     log.Printf("%s: %s breakpoint, stopping signal 0x%" PRIx32, __FUNCTION__,
55*f6aab3d8Srobert                header, stop_info.signo);
56061da546Spatrick     return;
57061da546Spatrick   case eStopReasonWatchpoint:
58061da546Spatrick     log.Printf("%s: %s watchpoint, stopping signal 0x%" PRIx32, __FUNCTION__,
59*f6aab3d8Srobert                header, stop_info.signo);
60061da546Spatrick     return;
61061da546Spatrick   case eStopReasonSignal:
62061da546Spatrick     log.Printf("%s: %s signal 0x%02" PRIx32, __FUNCTION__, header,
63*f6aab3d8Srobert                stop_info.signo);
64061da546Spatrick     return;
65061da546Spatrick   case eStopReasonException:
66061da546Spatrick     log.Printf("%s: %s exception type 0x%02" PRIx64, __FUNCTION__, header,
67061da546Spatrick                stop_info.details.exception.type);
68061da546Spatrick     return;
69061da546Spatrick   case eStopReasonExec:
70061da546Spatrick     log.Printf("%s: %s exec, stopping signal 0x%" PRIx32, __FUNCTION__, header,
71*f6aab3d8Srobert                stop_info.signo);
72061da546Spatrick     return;
73061da546Spatrick   case eStopReasonPlanComplete:
74061da546Spatrick     log.Printf("%s: %s plan complete", __FUNCTION__, header);
75061da546Spatrick     return;
76061da546Spatrick   case eStopReasonThreadExiting:
77061da546Spatrick     log.Printf("%s: %s thread exiting", __FUNCTION__, header);
78061da546Spatrick     return;
79061da546Spatrick   case eStopReasonInstrumentation:
80061da546Spatrick     log.Printf("%s: %s instrumentation", __FUNCTION__, header);
81061da546Spatrick     return;
82be691f3bSpatrick   case eStopReasonProcessorTrace:
83be691f3bSpatrick     log.Printf("%s: %s processor trace", __FUNCTION__, header);
84be691f3bSpatrick     return;
85061da546Spatrick   default:
86061da546Spatrick     log.Printf("%s: %s invalid stop reason %" PRIu32, __FUNCTION__, header,
87061da546Spatrick                static_cast<uint32_t>(stop_info.reason));
88061da546Spatrick   }
89061da546Spatrick }
90061da546Spatrick }
91061da546Spatrick 
NativeThreadLinux(NativeProcessLinux & process,lldb::tid_t tid)92061da546Spatrick NativeThreadLinux::NativeThreadLinux(NativeProcessLinux &process,
93061da546Spatrick                                      lldb::tid_t tid)
94061da546Spatrick     : NativeThreadProtocol(process, tid), m_state(StateType::eStateInvalid),
95061da546Spatrick       m_stop_info(),
96061da546Spatrick       m_reg_context_up(
97061da546Spatrick           NativeRegisterContextLinux::CreateHostNativeRegisterContextLinux(
98061da546Spatrick               process.GetArchitecture(), *this)),
99061da546Spatrick       m_stop_description() {}
100061da546Spatrick 
GetName()101061da546Spatrick std::string NativeThreadLinux::GetName() {
102061da546Spatrick   NativeProcessLinux &process = GetProcess();
103061da546Spatrick 
104061da546Spatrick   auto BufferOrError = getProcFile(process.GetID(), GetID(), "comm");
105061da546Spatrick   if (!BufferOrError)
106061da546Spatrick     return "";
107dda28197Spatrick   return std::string(BufferOrError.get()->getBuffer().rtrim('\n'));
108061da546Spatrick }
109061da546Spatrick 
GetState()110061da546Spatrick lldb::StateType NativeThreadLinux::GetState() { return m_state; }
111061da546Spatrick 
GetStopReason(ThreadStopInfo & stop_info,std::string & description)112061da546Spatrick bool NativeThreadLinux::GetStopReason(ThreadStopInfo &stop_info,
113061da546Spatrick                                       std::string &description) {
114*f6aab3d8Srobert   Log *log = GetLog(LLDBLog::Thread);
115061da546Spatrick 
116061da546Spatrick   description.clear();
117061da546Spatrick 
118061da546Spatrick   switch (m_state) {
119061da546Spatrick   case eStateStopped:
120061da546Spatrick   case eStateCrashed:
121061da546Spatrick   case eStateExited:
122061da546Spatrick   case eStateSuspended:
123061da546Spatrick   case eStateUnloaded:
124061da546Spatrick     if (log)
125061da546Spatrick       LogThreadStopInfo(*log, m_stop_info, "m_stop_info in thread:");
126061da546Spatrick     stop_info = m_stop_info;
127061da546Spatrick     description = m_stop_description;
128061da546Spatrick     if (log)
129061da546Spatrick       LogThreadStopInfo(*log, stop_info, "returned stop_info:");
130061da546Spatrick 
131061da546Spatrick     return true;
132061da546Spatrick 
133061da546Spatrick   case eStateInvalid:
134061da546Spatrick   case eStateConnected:
135061da546Spatrick   case eStateAttaching:
136061da546Spatrick   case eStateLaunching:
137061da546Spatrick   case eStateRunning:
138061da546Spatrick   case eStateStepping:
139061da546Spatrick   case eStateDetached:
140061da546Spatrick     if (log) {
141061da546Spatrick       LLDB_LOGF(log,
142061da546Spatrick                 "NativeThreadLinux::%s tid %" PRIu64
143061da546Spatrick                 " in state %s cannot answer stop reason",
144061da546Spatrick                 __FUNCTION__, GetID(), StateAsCString(m_state));
145061da546Spatrick     }
146061da546Spatrick     return false;
147061da546Spatrick   }
148061da546Spatrick   llvm_unreachable("unhandled StateType!");
149061da546Spatrick }
150061da546Spatrick 
SetWatchpoint(lldb::addr_t addr,size_t size,uint32_t watch_flags,bool hardware)151061da546Spatrick Status NativeThreadLinux::SetWatchpoint(lldb::addr_t addr, size_t size,
152061da546Spatrick                                         uint32_t watch_flags, bool hardware) {
153061da546Spatrick   if (!hardware)
154061da546Spatrick     return Status("not implemented");
155061da546Spatrick   if (m_state == eStateLaunching)
156061da546Spatrick     return Status();
157061da546Spatrick   Status error = RemoveWatchpoint(addr);
158061da546Spatrick   if (error.Fail())
159061da546Spatrick     return error;
160061da546Spatrick   uint32_t wp_index =
161061da546Spatrick       m_reg_context_up->SetHardwareWatchpoint(addr, size, watch_flags);
162061da546Spatrick   if (wp_index == LLDB_INVALID_INDEX32)
163061da546Spatrick     return Status("Setting hardware watchpoint failed.");
164061da546Spatrick   m_watchpoint_index_map.insert({addr, wp_index});
165061da546Spatrick   return Status();
166061da546Spatrick }
167061da546Spatrick 
RemoveWatchpoint(lldb::addr_t addr)168061da546Spatrick Status NativeThreadLinux::RemoveWatchpoint(lldb::addr_t addr) {
169061da546Spatrick   auto wp = m_watchpoint_index_map.find(addr);
170061da546Spatrick   if (wp == m_watchpoint_index_map.end())
171061da546Spatrick     return Status();
172061da546Spatrick   uint32_t wp_index = wp->second;
173061da546Spatrick   m_watchpoint_index_map.erase(wp);
174061da546Spatrick   if (m_reg_context_up->ClearHardwareWatchpoint(wp_index))
175061da546Spatrick     return Status();
176061da546Spatrick   return Status("Clearing hardware watchpoint failed.");
177061da546Spatrick }
178061da546Spatrick 
SetHardwareBreakpoint(lldb::addr_t addr,size_t size)179061da546Spatrick Status NativeThreadLinux::SetHardwareBreakpoint(lldb::addr_t addr,
180061da546Spatrick                                                 size_t size) {
181061da546Spatrick   if (m_state == eStateLaunching)
182061da546Spatrick     return Status();
183061da546Spatrick 
184061da546Spatrick   Status error = RemoveHardwareBreakpoint(addr);
185061da546Spatrick   if (error.Fail())
186061da546Spatrick     return error;
187061da546Spatrick 
188061da546Spatrick   uint32_t bp_index = m_reg_context_up->SetHardwareBreakpoint(addr, size);
189061da546Spatrick 
190061da546Spatrick   if (bp_index == LLDB_INVALID_INDEX32)
191061da546Spatrick     return Status("Setting hardware breakpoint failed.");
192061da546Spatrick 
193061da546Spatrick   m_hw_break_index_map.insert({addr, bp_index});
194061da546Spatrick   return Status();
195061da546Spatrick }
196061da546Spatrick 
RemoveHardwareBreakpoint(lldb::addr_t addr)197061da546Spatrick Status NativeThreadLinux::RemoveHardwareBreakpoint(lldb::addr_t addr) {
198061da546Spatrick   auto bp = m_hw_break_index_map.find(addr);
199061da546Spatrick   if (bp == m_hw_break_index_map.end())
200061da546Spatrick     return Status();
201061da546Spatrick 
202061da546Spatrick   uint32_t bp_index = bp->second;
203061da546Spatrick   if (m_reg_context_up->ClearHardwareBreakpoint(bp_index)) {
204061da546Spatrick     m_hw_break_index_map.erase(bp);
205061da546Spatrick     return Status();
206061da546Spatrick   }
207061da546Spatrick 
208061da546Spatrick   return Status("Clearing hardware breakpoint failed.");
209061da546Spatrick }
210061da546Spatrick 
Resume(uint32_t signo)211061da546Spatrick Status NativeThreadLinux::Resume(uint32_t signo) {
212061da546Spatrick   const StateType new_state = StateType::eStateRunning;
213061da546Spatrick   MaybeLogStateChange(new_state);
214061da546Spatrick   m_state = new_state;
215061da546Spatrick 
216061da546Spatrick   m_stop_info.reason = StopReason::eStopReasonNone;
217061da546Spatrick   m_stop_description.clear();
218061da546Spatrick 
219061da546Spatrick   // If watchpoints have been set, but none on this thread, then this is a new
220061da546Spatrick   // thread. So set all existing watchpoints.
221061da546Spatrick   if (m_watchpoint_index_map.empty()) {
222061da546Spatrick     NativeProcessLinux &process = GetProcess();
223061da546Spatrick 
224061da546Spatrick     const auto &watchpoint_map = process.GetWatchpointMap();
225061da546Spatrick     m_reg_context_up->ClearAllHardwareWatchpoints();
226061da546Spatrick     for (const auto &pair : watchpoint_map) {
227061da546Spatrick       const auto &wp = pair.second;
228061da546Spatrick       SetWatchpoint(wp.m_addr, wp.m_size, wp.m_watch_flags, wp.m_hardware);
229061da546Spatrick     }
230061da546Spatrick   }
231061da546Spatrick 
232061da546Spatrick   // Set all active hardware breakpoint on all threads.
233061da546Spatrick   if (m_hw_break_index_map.empty()) {
234061da546Spatrick     NativeProcessLinux &process = GetProcess();
235061da546Spatrick 
236061da546Spatrick     const auto &hw_breakpoint_map = process.GetHardwareBreakpointMap();
237061da546Spatrick     m_reg_context_up->ClearAllHardwareBreakpoints();
238061da546Spatrick     for (const auto &pair : hw_breakpoint_map) {
239061da546Spatrick       const auto &bp = pair.second;
240061da546Spatrick       SetHardwareBreakpoint(bp.m_addr, bp.m_size);
241061da546Spatrick     }
242061da546Spatrick   }
243061da546Spatrick 
244061da546Spatrick   intptr_t data = 0;
245061da546Spatrick 
246061da546Spatrick   if (signo != LLDB_INVALID_SIGNAL_NUMBER)
247061da546Spatrick     data = signo;
248061da546Spatrick 
249061da546Spatrick   return NativeProcessLinux::PtraceWrapper(PTRACE_CONT, GetID(), nullptr,
250061da546Spatrick                                            reinterpret_cast<void *>(data));
251061da546Spatrick }
252061da546Spatrick 
SingleStep(uint32_t signo)253061da546Spatrick Status NativeThreadLinux::SingleStep(uint32_t signo) {
254061da546Spatrick   const StateType new_state = StateType::eStateStepping;
255061da546Spatrick   MaybeLogStateChange(new_state);
256061da546Spatrick   m_state = new_state;
257061da546Spatrick   m_stop_info.reason = StopReason::eStopReasonNone;
258061da546Spatrick 
259061da546Spatrick   if(!m_step_workaround) {
260061da546Spatrick     // If we already hava a workaround inplace, don't reset it. Otherwise, the
261061da546Spatrick     // destructor of the existing instance will run after the new instance has
262061da546Spatrick     // fetched the cpu mask, and the thread will end up with the wrong mask.
263061da546Spatrick     m_step_workaround = SingleStepWorkaround::Get(m_tid);
264061da546Spatrick   }
265061da546Spatrick 
266061da546Spatrick   intptr_t data = 0;
267061da546Spatrick   if (signo != LLDB_INVALID_SIGNAL_NUMBER)
268061da546Spatrick     data = signo;
269061da546Spatrick 
270061da546Spatrick   // If hardware single-stepping is not supported, we just do a continue. The
271061da546Spatrick   // breakpoint on the next instruction has been setup in
272061da546Spatrick   // NativeProcessLinux::Resume.
273061da546Spatrick   return NativeProcessLinux::PtraceWrapper(
274061da546Spatrick       GetProcess().SupportHardwareSingleStepping() ? PTRACE_SINGLESTEP
275061da546Spatrick                                                    : PTRACE_CONT,
276061da546Spatrick       m_tid, nullptr, reinterpret_cast<void *>(data));
277061da546Spatrick }
278061da546Spatrick 
SetStoppedBySignal(uint32_t signo,const siginfo_t * info)279061da546Spatrick void NativeThreadLinux::SetStoppedBySignal(uint32_t signo,
280061da546Spatrick                                            const siginfo_t *info) {
281*f6aab3d8Srobert   Log *log = GetLog(LLDBLog::Thread);
282061da546Spatrick   LLDB_LOGF(log, "NativeThreadLinux::%s called with signal 0x%02" PRIx32,
283061da546Spatrick             __FUNCTION__, signo);
284061da546Spatrick 
285061da546Spatrick   SetStopped();
286061da546Spatrick 
287061da546Spatrick   m_stop_info.reason = StopReason::eStopReasonSignal;
288*f6aab3d8Srobert   m_stop_info.signo = signo;
289061da546Spatrick 
290061da546Spatrick   m_stop_description.clear();
291061da546Spatrick   if (info) {
292061da546Spatrick     switch (signo) {
293061da546Spatrick     case SIGSEGV:
294061da546Spatrick     case SIGBUS:
295061da546Spatrick     case SIGFPE:
296061da546Spatrick     case SIGILL:
297061da546Spatrick       // In case of MIPS64 target, SI_KERNEL is generated for invalid 64bit
298061da546Spatrick       // address.
299061da546Spatrick       const auto reason =
300061da546Spatrick           (info->si_signo == SIGBUS && info->si_code == SI_KERNEL)
301061da546Spatrick               ? CrashReason::eInvalidAddress
302061da546Spatrick               : GetCrashReason(*info);
303061da546Spatrick       m_stop_description = GetCrashReasonString(reason, *info);
304be691f3bSpatrick 
305be691f3bSpatrick       if (reason == CrashReason::eSyncTagCheckFault) {
306be691f3bSpatrick         AnnotateSyncTagCheckFault(info);
307be691f3bSpatrick       }
308be691f3bSpatrick 
309061da546Spatrick       break;
310061da546Spatrick     }
311061da546Spatrick   }
312061da546Spatrick }
313061da546Spatrick 
AnnotateSyncTagCheckFault(const siginfo_t * info)314be691f3bSpatrick void NativeThreadLinux::AnnotateSyncTagCheckFault(const siginfo_t *info) {
315be691f3bSpatrick   int32_t allocation_tag_type = 0;
316be691f3bSpatrick   switch (GetProcess().GetArchitecture().GetMachine()) {
317be691f3bSpatrick   // aarch64_32 deliberately not here because there's no 32 bit MTE
318be691f3bSpatrick   case llvm::Triple::aarch64:
319be691f3bSpatrick   case llvm::Triple::aarch64_be:
320be691f3bSpatrick     allocation_tag_type = MemoryTagManagerAArch64MTE::eMTE_allocation;
321be691f3bSpatrick     break;
322be691f3bSpatrick   default:
323be691f3bSpatrick     return;
324be691f3bSpatrick   }
325be691f3bSpatrick 
326be691f3bSpatrick   auto details =
327be691f3bSpatrick       GetRegisterContext().GetMemoryTaggingDetails(allocation_tag_type);
328be691f3bSpatrick   if (!details) {
329be691f3bSpatrick     llvm::consumeError(details.takeError());
330be691f3bSpatrick     return;
331be691f3bSpatrick   }
332be691f3bSpatrick 
333be691f3bSpatrick   // We assume that the stop description is currently:
334be691f3bSpatrick   // signal SIGSEGV: sync tag check fault (fault address: <addr>)
335be691f3bSpatrick   // Remove the closing )
336be691f3bSpatrick   m_stop_description.pop_back();
337be691f3bSpatrick 
338be691f3bSpatrick   std::stringstream ss;
339be691f3bSpatrick   lldb::addr_t fault_addr = reinterpret_cast<uintptr_t>(info->si_addr);
340be691f3bSpatrick   std::unique_ptr<MemoryTagManager> manager(std::move(details->manager));
341be691f3bSpatrick 
342be691f3bSpatrick   ss << " logical tag: 0x" << std::hex << manager->GetLogicalTag(fault_addr);
343be691f3bSpatrick 
344be691f3bSpatrick   std::vector<uint8_t> allocation_tag_data;
345be691f3bSpatrick   // The fault address may not be granule aligned. ReadMemoryTags will granule
346be691f3bSpatrick   // align any range you give it, potentially making it larger.
347be691f3bSpatrick   // To prevent this set len to 1. This always results in a range that is at
348be691f3bSpatrick   // most 1 granule in size and includes fault_addr.
349be691f3bSpatrick   Status status = GetProcess().ReadMemoryTags(allocation_tag_type, fault_addr,
350be691f3bSpatrick                                               1, allocation_tag_data);
351be691f3bSpatrick 
352be691f3bSpatrick   if (status.Success()) {
353be691f3bSpatrick     llvm::Expected<std::vector<lldb::addr_t>> allocation_tag =
354be691f3bSpatrick         manager->UnpackTagsData(allocation_tag_data, 1);
355be691f3bSpatrick     if (allocation_tag) {
356be691f3bSpatrick       ss << " allocation tag: 0x" << std::hex << allocation_tag->front() << ")";
357be691f3bSpatrick     } else {
358be691f3bSpatrick       llvm::consumeError(allocation_tag.takeError());
359be691f3bSpatrick       ss << ")";
360be691f3bSpatrick     }
361be691f3bSpatrick   } else
362be691f3bSpatrick     ss << ")";
363be691f3bSpatrick 
364be691f3bSpatrick   m_stop_description += ss.str();
365be691f3bSpatrick }
366be691f3bSpatrick 
IsStopped(int * signo)367061da546Spatrick bool NativeThreadLinux::IsStopped(int *signo) {
368061da546Spatrick   if (!StateIsStoppedState(m_state, false))
369061da546Spatrick     return false;
370061da546Spatrick 
371061da546Spatrick   // If we are stopped by a signal, return the signo.
372061da546Spatrick   if (signo && m_state == StateType::eStateStopped &&
373061da546Spatrick       m_stop_info.reason == StopReason::eStopReasonSignal) {
374*f6aab3d8Srobert     *signo = m_stop_info.signo;
375061da546Spatrick   }
376061da546Spatrick 
377061da546Spatrick   // Regardless, we are stopped.
378061da546Spatrick   return true;
379061da546Spatrick }
380061da546Spatrick 
SetStopped()381061da546Spatrick void NativeThreadLinux::SetStopped() {
382061da546Spatrick   if (m_state == StateType::eStateStepping)
383061da546Spatrick     m_step_workaround.reset();
384061da546Spatrick 
385be691f3bSpatrick   // On every stop, clear any cached register data structures
386be691f3bSpatrick   GetRegisterContext().InvalidateAllRegisters();
387be691f3bSpatrick 
388061da546Spatrick   const StateType new_state = StateType::eStateStopped;
389061da546Spatrick   MaybeLogStateChange(new_state);
390061da546Spatrick   m_state = new_state;
391061da546Spatrick   m_stop_description.clear();
392061da546Spatrick }
393061da546Spatrick 
SetStoppedByExec()394061da546Spatrick void NativeThreadLinux::SetStoppedByExec() {
395*f6aab3d8Srobert   Log *log = GetLog(LLDBLog::Thread);
396061da546Spatrick   LLDB_LOGF(log, "NativeThreadLinux::%s()", __FUNCTION__);
397061da546Spatrick 
398061da546Spatrick   SetStopped();
399061da546Spatrick 
400061da546Spatrick   m_stop_info.reason = StopReason::eStopReasonExec;
401*f6aab3d8Srobert   m_stop_info.signo = SIGSTOP;
402061da546Spatrick }
403061da546Spatrick 
SetStoppedByBreakpoint()404061da546Spatrick void NativeThreadLinux::SetStoppedByBreakpoint() {
405061da546Spatrick   SetStopped();
406061da546Spatrick 
407061da546Spatrick   m_stop_info.reason = StopReason::eStopReasonBreakpoint;
408*f6aab3d8Srobert   m_stop_info.signo = SIGTRAP;
409061da546Spatrick   m_stop_description.clear();
410061da546Spatrick }
411061da546Spatrick 
SetStoppedByWatchpoint(uint32_t wp_index)412061da546Spatrick void NativeThreadLinux::SetStoppedByWatchpoint(uint32_t wp_index) {
413061da546Spatrick   SetStopped();
414061da546Spatrick 
415061da546Spatrick   lldbassert(wp_index != LLDB_INVALID_INDEX32 && "wp_index cannot be invalid");
416061da546Spatrick 
417061da546Spatrick   std::ostringstream ostr;
418061da546Spatrick   ostr << m_reg_context_up->GetWatchpointAddress(wp_index) << " ";
419061da546Spatrick   ostr << wp_index;
420061da546Spatrick 
421061da546Spatrick   /*
422061da546Spatrick    * MIPS: Last 3bits of the watchpoint address are masked by the kernel. For
423061da546Spatrick    * example:
424061da546Spatrick    * 'n' is at 0x120010d00 and 'm' is 0x120010d04. When a watchpoint is set at
425061da546Spatrick    * 'm', then
426061da546Spatrick    * watch exception is generated even when 'n' is read/written. To handle this
427061da546Spatrick    * case,
428061da546Spatrick    * find the base address of the load/store instruction and append it in the
429061da546Spatrick    * stop-info
430061da546Spatrick    * packet.
431061da546Spatrick   */
432061da546Spatrick   ostr << " " << m_reg_context_up->GetWatchpointHitAddress(wp_index);
433061da546Spatrick 
434061da546Spatrick   m_stop_description = ostr.str();
435061da546Spatrick 
436061da546Spatrick   m_stop_info.reason = StopReason::eStopReasonWatchpoint;
437*f6aab3d8Srobert   m_stop_info.signo = SIGTRAP;
438061da546Spatrick }
439061da546Spatrick 
IsStoppedAtBreakpoint()440061da546Spatrick bool NativeThreadLinux::IsStoppedAtBreakpoint() {
441061da546Spatrick   return GetState() == StateType::eStateStopped &&
442061da546Spatrick          m_stop_info.reason == StopReason::eStopReasonBreakpoint;
443061da546Spatrick }
444061da546Spatrick 
IsStoppedAtWatchpoint()445061da546Spatrick bool NativeThreadLinux::IsStoppedAtWatchpoint() {
446061da546Spatrick   return GetState() == StateType::eStateStopped &&
447061da546Spatrick          m_stop_info.reason == StopReason::eStopReasonWatchpoint;
448061da546Spatrick }
449061da546Spatrick 
SetStoppedByTrace()450061da546Spatrick void NativeThreadLinux::SetStoppedByTrace() {
451061da546Spatrick   SetStopped();
452061da546Spatrick 
453061da546Spatrick   m_stop_info.reason = StopReason::eStopReasonTrace;
454*f6aab3d8Srobert   m_stop_info.signo = SIGTRAP;
455061da546Spatrick }
456061da546Spatrick 
SetStoppedByFork(bool is_vfork,lldb::pid_t child_pid)457be691f3bSpatrick void NativeThreadLinux::SetStoppedByFork(bool is_vfork, lldb::pid_t child_pid) {
458be691f3bSpatrick   SetStopped();
459be691f3bSpatrick 
460be691f3bSpatrick   m_stop_info.reason =
461be691f3bSpatrick       is_vfork ? StopReason::eStopReasonVFork : StopReason::eStopReasonFork;
462*f6aab3d8Srobert   m_stop_info.signo = SIGTRAP;
463be691f3bSpatrick   m_stop_info.details.fork.child_pid = child_pid;
464be691f3bSpatrick   m_stop_info.details.fork.child_tid = child_pid;
465be691f3bSpatrick }
466be691f3bSpatrick 
SetStoppedByVForkDone()467be691f3bSpatrick void NativeThreadLinux::SetStoppedByVForkDone() {
468be691f3bSpatrick   SetStopped();
469be691f3bSpatrick 
470be691f3bSpatrick   m_stop_info.reason = StopReason::eStopReasonVForkDone;
471*f6aab3d8Srobert   m_stop_info.signo = SIGTRAP;
472be691f3bSpatrick }
473be691f3bSpatrick 
SetStoppedWithNoReason()474061da546Spatrick void NativeThreadLinux::SetStoppedWithNoReason() {
475061da546Spatrick   SetStopped();
476061da546Spatrick 
477061da546Spatrick   m_stop_info.reason = StopReason::eStopReasonNone;
478*f6aab3d8Srobert   m_stop_info.signo = 0;
479061da546Spatrick }
480061da546Spatrick 
SetStoppedByProcessorTrace(llvm::StringRef description)481be691f3bSpatrick void NativeThreadLinux::SetStoppedByProcessorTrace(
482be691f3bSpatrick     llvm::StringRef description) {
483be691f3bSpatrick   SetStopped();
484be691f3bSpatrick 
485be691f3bSpatrick   m_stop_info.reason = StopReason::eStopReasonProcessorTrace;
486*f6aab3d8Srobert   m_stop_info.signo = 0;
487be691f3bSpatrick   m_stop_description = description.str();
488be691f3bSpatrick }
489be691f3bSpatrick 
SetExited()490061da546Spatrick void NativeThreadLinux::SetExited() {
491061da546Spatrick   const StateType new_state = StateType::eStateExited;
492061da546Spatrick   MaybeLogStateChange(new_state);
493061da546Spatrick   m_state = new_state;
494061da546Spatrick 
495061da546Spatrick   m_stop_info.reason = StopReason::eStopReasonThreadExiting;
496061da546Spatrick }
497061da546Spatrick 
RequestStop()498061da546Spatrick Status NativeThreadLinux::RequestStop() {
499*f6aab3d8Srobert   Log *log = GetLog(LLDBLog::Thread);
500061da546Spatrick 
501061da546Spatrick   NativeProcessLinux &process = GetProcess();
502061da546Spatrick 
503061da546Spatrick   lldb::pid_t pid = process.GetID();
504061da546Spatrick   lldb::tid_t tid = GetID();
505061da546Spatrick 
506061da546Spatrick   LLDB_LOGF(log,
507061da546Spatrick             "NativeThreadLinux::%s requesting thread stop(pid: %" PRIu64
508061da546Spatrick             ", tid: %" PRIu64 ")",
509061da546Spatrick             __FUNCTION__, pid, tid);
510061da546Spatrick 
511061da546Spatrick   Status err;
512061da546Spatrick   errno = 0;
513061da546Spatrick   if (::tgkill(pid, tid, SIGSTOP) != 0) {
514061da546Spatrick     err.SetErrorToErrno();
515061da546Spatrick     LLDB_LOGF(log,
516061da546Spatrick               "NativeThreadLinux::%s tgkill(%" PRIu64 ", %" PRIu64
517061da546Spatrick               ", SIGSTOP) failed: %s",
518061da546Spatrick               __FUNCTION__, pid, tid, err.AsCString());
519061da546Spatrick   }
520061da546Spatrick 
521061da546Spatrick   return err;
522061da546Spatrick }
523061da546Spatrick 
MaybeLogStateChange(lldb::StateType new_state)524061da546Spatrick void NativeThreadLinux::MaybeLogStateChange(lldb::StateType new_state) {
525*f6aab3d8Srobert   Log *log = GetLog(LLDBLog::Thread);
526061da546Spatrick   // If we're not logging, we're done.
527061da546Spatrick   if (!log)
528061da546Spatrick     return;
529061da546Spatrick 
530061da546Spatrick   // If this is a state change to the same state, we're done.
531061da546Spatrick   lldb::StateType old_state = m_state;
532061da546Spatrick   if (new_state == old_state)
533061da546Spatrick     return;
534061da546Spatrick 
535061da546Spatrick   LLDB_LOG(log, "pid={0}, tid={1}: changing from state {2} to {3}",
536061da546Spatrick            m_process.GetID(), GetID(), old_state, new_state);
537061da546Spatrick }
538061da546Spatrick 
GetProcess()539061da546Spatrick NativeProcessLinux &NativeThreadLinux::GetProcess() {
540061da546Spatrick   return static_cast<NativeProcessLinux &>(m_process);
541061da546Spatrick }
542*f6aab3d8Srobert 
GetProcess() const543*f6aab3d8Srobert const NativeProcessLinux &NativeThreadLinux::GetProcess() const {
544*f6aab3d8Srobert   return static_cast<const NativeProcessLinux &>(m_process);
545*f6aab3d8Srobert }
546*f6aab3d8Srobert 
547*f6aab3d8Srobert llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
GetSiginfo() const548*f6aab3d8Srobert NativeThreadLinux::GetSiginfo() const {
549*f6aab3d8Srobert   auto siginfo_buf =
550*f6aab3d8Srobert       llvm::WritableMemoryBuffer::getNewUninitMemBuffer(sizeof(siginfo_t));
551*f6aab3d8Srobert   Status error =
552*f6aab3d8Srobert       GetProcess().GetSignalInfo(GetID(), siginfo_buf->getBufferStart());
553*f6aab3d8Srobert   if (!error.Success())
554*f6aab3d8Srobert     return error.ToError();
555*f6aab3d8Srobert   return std::move(siginfo_buf);
556*f6aab3d8Srobert }
557