• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- NativeThreadFreeBSD.cpp -------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "NativeThreadFreeBSD.h"
10 #include "NativeRegisterContextFreeBSD.h"
11 
12 #include "NativeProcessFreeBSD.h"
13 
14 #include "Plugins/Process/POSIX/CrashReason.h"
15 #include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
16 #include "lldb/Utility/LLDBAssert.h"
17 #include "lldb/Utility/RegisterValue.h"
18 #include "lldb/Utility/State.h"
19 #include "llvm/Support/Errno.h"
20 
21 // clang-format off
22 #include <sys/types.h>
23 #include <sys/ptrace.h>
24 #include <sys/sysctl.h>
25 #include <sys/user.h>
26 // clang-format on
27 
28 #include <sstream>
29 #include <vector>
30 
31 using namespace lldb;
32 using namespace lldb_private;
33 using namespace lldb_private::process_freebsd;
34 
NativeThreadFreeBSD(NativeProcessFreeBSD & process,lldb::tid_t tid)35 NativeThreadFreeBSD::NativeThreadFreeBSD(NativeProcessFreeBSD &process,
36                                          lldb::tid_t tid)
37     : NativeThreadProtocol(process, tid), m_state(StateType::eStateInvalid),
38       m_stop_info(),
39       m_reg_context_up(
40           NativeRegisterContextFreeBSD::CreateHostNativeRegisterContextFreeBSD(
41               process.GetArchitecture(), *this)),
42       m_stop_description() {}
43 
Resume()44 Status NativeThreadFreeBSD::Resume() {
45   Status ret = NativeProcessFreeBSD::PtraceWrapper(PT_RESUME, GetID());
46   if (!ret.Success())
47     return ret;
48   ret = NativeProcessFreeBSD::PtraceWrapper(PT_CLEARSTEP, GetID());
49   if (ret.Success())
50     SetRunning();
51   return ret;
52 }
53 
SingleStep()54 Status NativeThreadFreeBSD::SingleStep() {
55   Status ret = NativeProcessFreeBSD::PtraceWrapper(PT_RESUME, GetID());
56   if (!ret.Success())
57     return ret;
58   ret = NativeProcessFreeBSD::PtraceWrapper(PT_SETSTEP, GetID());
59   if (ret.Success())
60     SetStepping();
61   return ret;
62 }
63 
Suspend()64 Status NativeThreadFreeBSD::Suspend() {
65   Status ret = NativeProcessFreeBSD::PtraceWrapper(PT_SUSPEND, GetID());
66   if (ret.Success())
67     SetStopped();
68   return ret;
69 }
70 
SetStoppedBySignal(uint32_t signo,const siginfo_t * info)71 void NativeThreadFreeBSD::SetStoppedBySignal(uint32_t signo,
72                                              const siginfo_t *info) {
73   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD));
74   LLDB_LOG(log, "tid = {0} in called with signal {1}", GetID(), signo);
75 
76   SetStopped();
77 
78   m_stop_info.reason = StopReason::eStopReasonSignal;
79   m_stop_info.details.signal.signo = signo;
80 
81   m_stop_description.clear();
82   if (info) {
83     switch (signo) {
84     case SIGSEGV:
85     case SIGBUS:
86     case SIGFPE:
87     case SIGILL:
88       const auto reason = GetCrashReason(*info);
89       m_stop_description = GetCrashReasonString(reason, *info);
90       break;
91     }
92   }
93 }
94 
SetStoppedByBreakpoint()95 void NativeThreadFreeBSD::SetStoppedByBreakpoint() {
96   SetStopped();
97   m_stop_info.reason = StopReason::eStopReasonBreakpoint;
98   m_stop_info.details.signal.signo = SIGTRAP;
99 }
100 
SetStoppedByTrace()101 void NativeThreadFreeBSD::SetStoppedByTrace() {
102   SetStopped();
103   m_stop_info.reason = StopReason::eStopReasonTrace;
104   m_stop_info.details.signal.signo = SIGTRAP;
105 }
106 
SetStoppedByExec()107 void NativeThreadFreeBSD::SetStoppedByExec() {
108   SetStopped();
109   m_stop_info.reason = StopReason::eStopReasonExec;
110   m_stop_info.details.signal.signo = SIGTRAP;
111 }
112 
SetStoppedByWatchpoint(uint32_t wp_index)113 void NativeThreadFreeBSD::SetStoppedByWatchpoint(uint32_t wp_index) {
114   lldbassert(wp_index != LLDB_INVALID_INDEX32 && "wp_index cannot be invalid");
115 
116   std::ostringstream ostr;
117   ostr << GetRegisterContext().GetWatchpointAddress(wp_index) << " ";
118   ostr << wp_index;
119 
120   ostr << " " << GetRegisterContext().GetWatchpointHitAddress(wp_index);
121 
122   SetStopped();
123   m_stop_description = ostr.str();
124   m_stop_info.reason = StopReason::eStopReasonWatchpoint;
125   m_stop_info.details.signal.signo = SIGTRAP;
126 }
127 
SetStoppedWithNoReason()128 void NativeThreadFreeBSD::SetStoppedWithNoReason() {
129   SetStopped();
130 
131   m_stop_info.reason = StopReason::eStopReasonNone;
132   m_stop_info.details.signal.signo = 0;
133 }
134 
SetStopped()135 void NativeThreadFreeBSD::SetStopped() {
136   const StateType new_state = StateType::eStateStopped;
137   m_state = new_state;
138   m_stop_description.clear();
139 }
140 
SetRunning()141 void NativeThreadFreeBSD::SetRunning() {
142   m_state = StateType::eStateRunning;
143   m_stop_info.reason = StopReason::eStopReasonNone;
144 }
145 
SetStepping()146 void NativeThreadFreeBSD::SetStepping() {
147   m_state = StateType::eStateStepping;
148   m_stop_info.reason = StopReason::eStopReasonNone;
149 }
150 
GetName()151 std::string NativeThreadFreeBSD::GetName() {
152   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD));
153 
154   std::vector<struct kinfo_proc> kp;
155   int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD,
156                 static_cast<int>(GetProcess().GetID())};
157 
158   while (1) {
159     size_t len = kp.size() * sizeof(struct kinfo_proc);
160     void *ptr = len == 0 ? nullptr : kp.data();
161     int error = ::sysctl(mib, 4, ptr, &len, nullptr, 0);
162     if (ptr == nullptr || (error != 0 && errno == ENOMEM)) {
163       kp.resize(len / sizeof(struct kinfo_proc));
164       continue;
165     }
166     if (error != 0) {
167       len = 0;
168       LLDB_LOG(log, "tid = {0} in state {1} failed to get thread name: {2}",
169                GetID(), m_state, strerror(errno));
170     }
171     kp.resize(len / sizeof(struct kinfo_proc));
172     break;
173   }
174 
175   for (auto &procinfo : kp) {
176     if (procinfo.ki_tid == static_cast<lwpid_t>(GetID()))
177       return procinfo.ki_tdname;
178   }
179 
180   return "";
181 }
182 
GetState()183 lldb::StateType NativeThreadFreeBSD::GetState() { return m_state; }
184 
GetStopReason(ThreadStopInfo & stop_info,std::string & description)185 bool NativeThreadFreeBSD::GetStopReason(ThreadStopInfo &stop_info,
186                                         std::string &description) {
187   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD));
188   description.clear();
189 
190   switch (m_state) {
191   case eStateStopped:
192   case eStateCrashed:
193   case eStateExited:
194   case eStateSuspended:
195   case eStateUnloaded:
196     stop_info = m_stop_info;
197     description = m_stop_description;
198 
199     return true;
200 
201   case eStateInvalid:
202   case eStateConnected:
203   case eStateAttaching:
204   case eStateLaunching:
205   case eStateRunning:
206   case eStateStepping:
207   case eStateDetached:
208     LLDB_LOG(log, "tid = {0} in state {1} cannot answer stop reason", GetID(),
209              StateAsCString(m_state));
210     return false;
211   }
212   llvm_unreachable("unhandled StateType!");
213 }
214 
GetRegisterContext()215 NativeRegisterContextFreeBSD &NativeThreadFreeBSD::GetRegisterContext() {
216   assert(m_reg_context_up);
217   return *m_reg_context_up;
218 }
219 
SetWatchpoint(lldb::addr_t addr,size_t size,uint32_t watch_flags,bool hardware)220 Status NativeThreadFreeBSD::SetWatchpoint(lldb::addr_t addr, size_t size,
221                                           uint32_t watch_flags, bool hardware) {
222   assert(m_state == eStateStopped);
223   if (!hardware)
224     return Status("not implemented");
225   Status error = RemoveWatchpoint(addr);
226   if (error.Fail())
227     return error;
228   uint32_t wp_index =
229       GetRegisterContext().SetHardwareWatchpoint(addr, size, watch_flags);
230   if (wp_index == LLDB_INVALID_INDEX32)
231     return Status("Setting hardware watchpoint failed.");
232   m_watchpoint_index_map.insert({addr, wp_index});
233   return Status();
234 }
235 
RemoveWatchpoint(lldb::addr_t addr)236 Status NativeThreadFreeBSD::RemoveWatchpoint(lldb::addr_t addr) {
237   auto wp = m_watchpoint_index_map.find(addr);
238   if (wp == m_watchpoint_index_map.end())
239     return Status();
240   uint32_t wp_index = wp->second;
241   m_watchpoint_index_map.erase(wp);
242   if (GetRegisterContext().ClearHardwareWatchpoint(wp_index))
243     return Status();
244   return Status("Clearing hardware watchpoint failed.");
245 }
246 
SetHardwareBreakpoint(lldb::addr_t addr,size_t size)247 Status NativeThreadFreeBSD::SetHardwareBreakpoint(lldb::addr_t addr,
248                                                   size_t size) {
249   assert(m_state == eStateStopped);
250   Status error = RemoveHardwareBreakpoint(addr);
251   if (error.Fail())
252     return error;
253 
254   uint32_t bp_index = GetRegisterContext().SetHardwareBreakpoint(addr, size);
255 
256   if (bp_index == LLDB_INVALID_INDEX32)
257     return Status("Setting hardware breakpoint failed.");
258 
259   m_hw_break_index_map.insert({addr, bp_index});
260   return Status();
261 }
262 
RemoveHardwareBreakpoint(lldb::addr_t addr)263 Status NativeThreadFreeBSD::RemoveHardwareBreakpoint(lldb::addr_t addr) {
264   auto bp = m_hw_break_index_map.find(addr);
265   if (bp == m_hw_break_index_map.end())
266     return Status();
267 
268   uint32_t bp_index = bp->second;
269   if (GetRegisterContext().ClearHardwareBreakpoint(bp_index)) {
270     m_hw_break_index_map.erase(bp);
271     return Status();
272   }
273 
274   return Status("Clearing hardware breakpoint failed.");
275 }
276 
277 llvm::Error
CopyWatchpointsFrom(NativeThreadFreeBSD & source)278 NativeThreadFreeBSD::CopyWatchpointsFrom(NativeThreadFreeBSD &source) {
279   llvm::Error s = GetRegisterContext().CopyHardwareWatchpointsFrom(
280       source.GetRegisterContext());
281   if (!s) {
282     m_watchpoint_index_map = source.m_watchpoint_index_map;
283     m_hw_break_index_map = source.m_hw_break_index_map;
284   }
285   return s;
286 }
287