LLDB mainline
NativeThreadFreeBSD.cpp
Go to the documentation of this file.
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
11
13
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
31using namespace lldb;
32using namespace lldb_private;
33using namespace lldb_private::process_freebsd;
34
36 lldb::tid_t tid)
39 NativeRegisterContextFreeBSD::CreateHostNativeRegisterContextFreeBSD(
40 process.GetArchitecture(), *this)) {}
41
44 if (!ret.Success())
45 return ret;
46 ret = NativeProcessFreeBSD::PtraceWrapper(PT_CLEARSTEP, GetID());
47 // we can get EINVAL if the architecture in question does not support
48 // hardware single-stepping -- that's fine, we have nothing to clear
49 // then
50 if (ret.GetError() == EINVAL)
51 ret.Clear();
52 if (ret.Success())
53 SetRunning();
54 return ret;
55}
56
59 if (!ret.Success())
60 return ret;
61 ret = NativeProcessFreeBSD::PtraceWrapper(PT_SETSTEP, GetID());
62 if (ret.Success())
64 return ret;
65}
66
69 if (ret.Success())
70 SetStopped();
71 return ret;
72}
73
75 const siginfo_t *info) {
77 LLDB_LOG(log, "tid = {0} in called with signal {1}", GetID(), signo);
78
79 SetStopped();
80
82 m_stop_info.signo = signo;
83
84 m_stop_description.clear();
85 if (info) {
86 switch (signo) {
87 case SIGSEGV:
88 case SIGBUS:
89 case SIGFPE:
90 case SIGILL:
92 break;
93 }
94 }
95}
96
102
108
114
116 lldbassert(wp_index != LLDB_INVALID_INDEX32 && "wp_index cannot be invalid");
117
118 std::ostringstream ostr;
119 ostr << GetRegisterContext().GetWatchpointAddress(wp_index) << " ";
120 ostr << wp_index;
121
122 ostr << " " << GetRegisterContext().GetWatchpointHitAddress(wp_index);
123
124 SetStopped();
125 m_stop_description = ostr.str();
127 m_stop_info.signo = SIGTRAP;
128}
129
131 lldb::tid_t child_tid) {
132 SetStopped();
133
135 m_stop_info.signo = SIGTRAP;
136 m_stop_info.details.fork.child_pid = child_pid;
137 m_stop_info.details.fork.child_tid = child_tid;
138}
139
141 lldb::tid_t child_tid) {
142 SetStopped();
143
145 m_stop_info.signo = SIGTRAP;
146 m_stop_info.details.fork.child_pid = child_pid;
147 m_stop_info.details.fork.child_tid = child_tid;
148}
149
156
163
165 const StateType new_state = StateType::eStateStopped;
166 m_state = new_state;
167 m_stop_description.clear();
168}
169
174
179
182
183 std::vector<struct kinfo_proc> kp;
184 int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD,
185 static_cast<int>(GetProcess().GetID())};
186
187 while (1) {
188 size_t len = kp.size() * sizeof(struct kinfo_proc);
189 void *ptr = len == 0 ? nullptr : kp.data();
190 int error = ::sysctl(mib, 4, ptr, &len, nullptr, 0);
191 if (ptr == nullptr || (error != 0 && errno == ENOMEM)) {
192 kp.resize(len / sizeof(struct kinfo_proc));
193 continue;
194 }
195 if (error != 0) {
196 len = 0;
197 LLDB_LOG(log, "tid = {0} in state {1} failed to get thread name: {2}",
198 GetID(), m_state, strerror(errno));
199 }
200 kp.resize(len / sizeof(struct kinfo_proc));
201 break;
202 }
203
204 for (auto &procinfo : kp) {
205 if (procinfo.ki_tid == static_cast<lwpid_t>(GetID()))
206 return procinfo.ki_tdname;
207 }
208
209 return "";
210}
211
213
215 std::string &description) {
217 description.clear();
218
219 switch (m_state) {
220 case eStateStopped:
221 case eStateCrashed:
222 case eStateExited:
223 case eStateSuspended:
224 case eStateUnloaded:
225 stop_info = m_stop_info;
226 description = m_stop_description;
227
228 return true;
229
230 case eStateInvalid:
231 case eStateConnected:
232 case eStateAttaching:
233 case eStateLaunching:
234 case eStateRunning:
235 case eStateStepping:
236 case eStateDetached:
237 LLDB_LOG(log, "tid = {0} in state {1} cannot answer stop reason", GetID(),
239 return false;
240 }
241 llvm_unreachable("unhandled StateType!");
242}
243
248
250 uint32_t watch_flags, bool hardware) {
251 assert(m_state == eStateStopped);
252 if (!hardware)
253 return Status::FromErrorString("not implemented");
255 if (error.Fail())
256 return error;
257 uint32_t wp_index =
258 GetRegisterContext().SetHardwareWatchpoint(addr, size, watch_flags);
259 if (wp_index == LLDB_INVALID_INDEX32)
260 return Status::FromErrorString("Setting hardware watchpoint failed.");
261 m_watchpoint_index_map.insert({addr, wp_index});
262 return Status();
263}
264
266 auto wp = m_watchpoint_index_map.find(addr);
267 if (wp == m_watchpoint_index_map.end())
268 return Status();
269 uint32_t wp_index = wp->second;
270 m_watchpoint_index_map.erase(wp);
271 if (GetRegisterContext().ClearHardwareWatchpoint(wp_index))
272 return Status();
273 return Status::FromErrorString("Clearing hardware watchpoint failed.");
274}
275
277 size_t size) {
278 assert(m_state == eStateStopped);
280 if (error.Fail())
281 return error;
282
283 uint32_t bp_index = GetRegisterContext().SetHardwareBreakpoint(addr, size);
284
285 if (bp_index == LLDB_INVALID_INDEX32)
286 return Status::FromErrorString("Setting hardware breakpoint failed.");
287
288 m_hw_break_index_map.insert({addr, bp_index});
289 return Status();
290}
291
293 auto bp = m_hw_break_index_map.find(addr);
294 if (bp == m_hw_break_index_map.end())
295 return Status();
296
297 uint32_t bp_index = bp->second;
298 if (GetRegisterContext().ClearHardwareBreakpoint(bp_index)) {
299 m_hw_break_index_map.erase(bp);
300 return Status();
301 }
302
303 return Status::FromErrorString("Clearing hardware breakpoint failed.");
304}
305
306llvm::Error
316
320
321llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
324
325 struct ptrace_lwpinfo info;
326 const auto siginfo_err = NativeProcessFreeBSD::PtraceWrapper(
327 PT_LWPINFO, GetID(), &info, sizeof(info));
328 if (siginfo_err.Fail()) {
329 LLDB_LOG(log, "PT_LWPINFO failed {0}", siginfo_err);
330 return siginfo_err.ToError();
331 }
332
333 if (info.pl_event != PL_EVENT_SIGNAL)
334 return llvm::createStringError(llvm::inconvertibleErrorCode(),
335 "Thread not signaled");
336 if (!(info.pl_flags & PL_FLAG_SI))
337 return llvm::createStringError(llvm::inconvertibleErrorCode(),
338 "No siginfo for thread");
339
340 return llvm::MemoryBuffer::getMemBufferCopy(
341 llvm::StringRef(reinterpret_cast<const char *>(&info.pl_siginfo),
342 sizeof(info.pl_siginfo)));
343}
static llvm::raw_ostream & error(Stream &strm)
std::string GetCrashReasonString(const siginfo_t &info)
#define lldbassert(x)
Definition LLDBAssert.h:16
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:364
#define SIGILL
#define SIGFPE
#define SIGSEGV
#define SIGBUS
virtual lldb::addr_t GetWatchpointAddress(uint32_t wp_index)
virtual lldb::addr_t GetWatchpointHitAddress(uint32_t wp_index)
virtual uint32_t SetHardwareWatchpoint(lldb::addr_t addr, size_t size, uint32_t watch_flags)
virtual uint32_t SetHardwareBreakpoint(lldb::addr_t addr, size_t size)
NativeThreadProtocol(NativeProcessProtocol &process, lldb::tid_t tid)
An error handling class.
Definition Status.h:118
void Clear()
Clear the object state.
Definition Status.cpp:214
ValueType GetError() const
Access the error value.
Definition Status.cpp:221
static Status FromErrorString(const char *str)
Definition Status.h:141
bool Success() const
Test for success condition.
Definition Status.cpp:303
static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr=nullptr, int data=0, int *result=nullptr)
virtual llvm::Error CopyHardwareWatchpointsFrom(NativeRegisterContextFreeBSD &source)=0
Status SetHardwareBreakpoint(lldb::addr_t addr, size_t size) override
llvm::Expected< std::unique_ptr< llvm::MemoryBuffer > > GetSiginfo() const override
NativeRegisterContextFreeBSD & GetRegisterContext() override
llvm::Error CopyWatchpointsFrom(NativeThreadFreeBSD &source)
std::unique_ptr< NativeRegisterContextFreeBSD > m_reg_context_up
void SetStoppedByVFork(lldb::pid_t child_pid, lldb::tid_t child_tid)
NativeThreadFreeBSD(NativeProcessFreeBSD &process, lldb::tid_t tid)
void SetStoppedByFork(lldb::pid_t child_pid, lldb::tid_t child_tid)
Status SetWatchpoint(lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware) override
void SetStoppedBySignal(uint32_t signo, const siginfo_t *info=nullptr)
bool GetStopReason(ThreadStopInfo &stop_info, std::string &description) override
Status RemoveHardwareBreakpoint(lldb::addr_t addr) override
#define LLDB_INVALID_INDEX32
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:327
const char * StateAsCString(lldb::StateType state)
Converts a StateType to a C string.
Definition State.cpp:14
StateType
Process and Thread States.
@ eStateUnloaded
Process is object is valid, but not currently loaded.
@ eStateConnected
Process is connected to remote debug services, but not launched or attached to anything yet.
@ eStateDetached
Process has been detached and can't be examined.
@ eStateStopped
Process or thread is stopped and can be examined.
@ eStateSuspended
Process or thread is in a suspended state as far as the debugger is concerned while other processes o...
@ eStateRunning
Process or thread is running and can't be examined.
@ eStateLaunching
Process is in the process of launching.
@ eStateAttaching
Process is currently trying to attach.
@ eStateExited
Process has exited and can't be examined.
@ eStateStepping
Process or thread is in the process of stepping and can not be examined.
@ eStateCrashed
Process or thread has crashed and can be examined.
uint64_t pid_t
Definition lldb-types.h:83
uint64_t addr_t
Definition lldb-types.h:80
@ eStopReasonBreakpoint
@ eStopReasonExec
Program was re-exec'ed.
@ eStopReasonVForkDone
@ eStopReasonWatchpoint
uint64_t tid_t
Definition lldb-types.h:84
#define SIGTRAP