LLDB mainline
NativeThreadNetBSD.cpp
Go to the documentation of this file.
1//===-- NativeThreadNetBSD.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
12#include "NativeProcessNetBSD.h"
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// clang-format on
25
26#include <sstream>
27
28// clang-format off
29#include <sys/types.h>
30#include <sys/sysctl.h>
31// clang-format on
32
33using namespace lldb;
34using namespace lldb_private;
35using namespace lldb_private::process_netbsd;
36
38 lldb::tid_t tid)
39 : NativeThreadProtocol(process, tid), m_state(StateType::eStateInvalid),
40 m_stop_info(), m_reg_context_up(
41NativeRegisterContextNetBSD::CreateHostNativeRegisterContextNetBSD(process.GetArchitecture(), *this)
42), m_stop_description() {}
43
46 nullptr, GetID());
47 if (!ret.Success())
48 return ret;
50 nullptr, GetID());
51 if (ret.Success())
52 SetRunning();
53 return ret;
54}
55
58 nullptr, GetID());
59 if (!ret.Success())
60 return ret;
62 nullptr, GetID());
63 if (ret.Success())
65 return ret;
66}
67
70 nullptr, GetID());
71 if (ret.Success())
72 SetStopped();
73 return ret;
74}
75
77 const siginfo_t *info) {
79 LLDB_LOG(log, "tid = {0} in called with signal {1}", GetID(), signo);
80
81 SetStopped();
82
83 m_stop_info.reason = StopReason::eStopReasonSignal;
84 m_stop_info.signo = signo;
85
86 m_stop_description.clear();
87 if (info) {
88 switch (signo) {
89 case SIGSEGV:
90 case SIGBUS:
91 case SIGFPE:
92 case SIGILL:
94 break;
95 }
96 }
97}
98
100 SetStopped();
101 m_stop_info.reason = StopReason::eStopReasonBreakpoint;
103}
104
106 SetStopped();
107 m_stop_info.reason = StopReason::eStopReasonTrace;
109}
110
112 SetStopped();
113 m_stop_info.reason = StopReason::eStopReasonExec;
115}
116
118 lldbassert(wp_index != LLDB_INVALID_INDEX32 && "wp_index cannot be invalid");
119
120 std::ostringstream ostr;
121 ostr << GetRegisterContext().GetWatchpointAddress(wp_index) << " ";
122 ostr << wp_index;
123
124 ostr << " " << GetRegisterContext().GetWatchpointHitAddress(wp_index);
125
126 SetStopped();
127 m_stop_description = ostr.str();
128 m_stop_info.reason = StopReason::eStopReasonWatchpoint;
130}
131
133 lldb::tid_t child_tid) {
134 SetStopped();
135
136 m_stop_info.reason = StopReason::eStopReasonFork;
138 m_stop_info.details.fork.child_pid = child_pid;
139 m_stop_info.details.fork.child_tid = child_tid;
140}
141
143 lldb::tid_t child_tid) {
144 SetStopped();
145
146 m_stop_info.reason = StopReason::eStopReasonVFork;
148 m_stop_info.details.fork.child_pid = child_pid;
149 m_stop_info.details.fork.child_tid = child_tid;
150}
151
153 SetStopped();
154
155 m_stop_info.reason = StopReason::eStopReasonVForkDone;
157}
158
160 SetStopped();
161
162 m_stop_info.reason = StopReason::eStopReasonNone;
163 m_stop_info.signo = 0;
164}
165
167 const StateType new_state = StateType::eStateStopped;
168 m_state = new_state;
169 m_stop_description.clear();
170}
171
173 m_state = StateType::eStateRunning;
174 m_stop_info.reason = StopReason::eStopReasonNone;
175}
176
178 m_state = StateType::eStateStepping;
179 m_stop_info.reason = StopReason::eStopReasonNone;
180}
181
184
185#ifdef PT_LWPSTATUS
186 struct ptrace_lwpstatus info = {};
187 info.pl_lwpid = m_tid;
189 PT_LWPSTATUS, static_cast<int>(m_process.GetID()), &info, sizeof(info));
190 if (error.Fail()) {
191 return "";
192 }
193 return info.pl_name;
194#else
195 std::vector<struct kinfo_lwp> infos;
196 int mib[5] = {CTL_KERN, KERN_LWP, static_cast<int>(m_process.GetID()),
197 sizeof(struct kinfo_lwp), 0};
198 size_t size;
199
200 if (::sysctl(mib, 5, nullptr, &size, nullptr, 0) == -1 || size == 0) {
201 LLDB_LOG(log, "sysctl() for LWP info size failed: {0}",
202 llvm::sys::StrError());
203 return "";
204 }
205
206 mib[4] = size / sizeof(size_t);
207 infos.resize(size / sizeof(struct kinfo_lwp));
208
209 if (sysctl(mib, 5, infos.data(), &size, NULL, 0) == -1 || size == 0) {
210 LLDB_LOG(log, "sysctl() for LWP info failed: {0}", llvm::sys::StrError());
211 return "";
212 }
213
214 size_t nlwps = size / sizeof(struct kinfo_lwp);
215 for (size_t i = 0; i < nlwps; i++) {
216 if (static_cast<lldb::tid_t>(infos[i].l_lid) == m_tid) {
217 return infos[i].l_name;
218 }
219 }
220
221 LLDB_LOG(log, "unable to find lwp {0} in LWP infos", m_tid);
222 return "";
223#endif
224}
225
227
229 std::string &description) {
231 description.clear();
232
233 switch (m_state) {
234 case eStateStopped:
235 case eStateCrashed:
236 case eStateExited:
237 case eStateSuspended:
238 case eStateUnloaded:
239 stop_info = m_stop_info;
240 description = m_stop_description;
241
242 return true;
243
244 case eStateInvalid:
245 case eStateConnected:
246 case eStateAttaching:
247 case eStateLaunching:
248 case eStateRunning:
249 case eStateStepping:
250 case eStateDetached:
251 LLDB_LOG(log, "tid = {0} in state {1} cannot answer stop reason", GetID(),
253 return false;
254 }
255 llvm_unreachable("unhandled StateType!");
256}
257
259 assert(m_reg_context_up);
260 return *m_reg_context_up;
261}
262
264 uint32_t watch_flags, bool hardware) {
265 assert(m_state == eStateStopped);
266 if (!hardware)
267 return Status("not implemented");
269 if (error.Fail())
270 return error;
271 uint32_t wp_index =
272 GetRegisterContext().SetHardwareWatchpoint(addr, size, watch_flags);
273 if (wp_index == LLDB_INVALID_INDEX32)
274 return Status("Setting hardware watchpoint failed.");
275 m_watchpoint_index_map.insert({addr, wp_index});
276 return Status();
277}
278
280 auto wp = m_watchpoint_index_map.find(addr);
281 if (wp == m_watchpoint_index_map.end())
282 return Status();
283 uint32_t wp_index = wp->second;
284 m_watchpoint_index_map.erase(wp);
285 if (GetRegisterContext().ClearHardwareWatchpoint(wp_index))
286 return Status();
287 return Status("Clearing hardware watchpoint failed.");
288}
289
291 size_t size) {
292 assert(m_state == eStateStopped);
294 if (error.Fail())
295 return error;
296
297 uint32_t bp_index = GetRegisterContext().SetHardwareBreakpoint(addr, size);
298
299 if (bp_index == LLDB_INVALID_INDEX32)
300 return Status("Setting hardware breakpoint failed.");
301
302 m_hw_break_index_map.insert({addr, bp_index});
303 return Status();
304}
305
307 auto bp = m_hw_break_index_map.find(addr);
308 if (bp == m_hw_break_index_map.end())
309 return Status();
310
311 uint32_t bp_index = bp->second;
312 if (GetRegisterContext().ClearHardwareBreakpoint(bp_index)) {
313 m_hw_break_index_map.erase(bp);
314 return Status();
315 }
316
317 return Status("Clearing hardware breakpoint failed.");
318}
319
320llvm::Error
323 source.GetRegisterContext());
324 if (!s) {
327 }
328 return s;
329}
static llvm::raw_ostream & error(Stream &strm)
std::string GetCrashReasonString(const siginfo_t &info)
Definition: CrashReason.cpp:13
#define lldbassert(x)
Definition: LLDBAssert.h:15
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:342
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)
An error handling class.
Definition: Status.h:44
bool Success() const
Test for success condition.
Definition: Status.cpp:279
Manages communication with the inferior (debugee) process.
static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr=nullptr, int data=0, int *result=nullptr)
virtual llvm::Error CopyHardwareWatchpointsFrom(NativeRegisterContextNetBSD &source)=0
void SetStoppedByFork(lldb::pid_t child_pid, lldb::tid_t child_tid)
void SetStoppedBySignal(uint32_t signo, const siginfo_t *info=nullptr)
bool GetStopReason(ThreadStopInfo &stop_info, std::string &description) override
Status SetWatchpoint(lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware) override
Status RemoveHardwareBreakpoint(lldb::addr_t addr) override
std::unique_ptr< NativeRegisterContextNetBSD > m_reg_context_up
Status SetHardwareBreakpoint(lldb::addr_t addr, size_t size) override
NativeRegisterContextNetBSD & GetRegisterContext() override
void SetStoppedByVFork(lldb::pid_t child_pid, lldb::tid_t child_tid)
NativeThreadNetBSD(NativeProcessNetBSD &process, lldb::tid_t tid)
Status RemoveWatchpoint(lldb::addr_t addr) override
llvm::Error CopyWatchpointsFrom(NativeThreadNetBSD &source)
#define LLDB_INVALID_INDEX32
Definition: lldb-defines.h:83
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:314
const char * StateAsCString(lldb::StateType state)
Converts a StateType to a C string.
Definition: State.cpp:14
Definition: SBAddress.h:15
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:81
uint64_t addr_t
Definition: lldb-types.h:79
uint64_t tid_t
Definition: lldb-types.h:82
lldb::StopReason reason
Definition: Debug.h:132
union lldb_private::ThreadStopInfo::@12 details
struct lldb_private::ThreadStopInfo::@12::@14 fork
#define SIGTRAP