20#include "gdbsupport/common-defs.h"
22#include "gdbsupport/common-debug.h"
25#include <sys/ptrace.h>
26#include <sys/sysctl.h>
30#include "gdbsupport/function-view.h"
40 static char buf[PATH_MAX];
41 int mib[4] = {CTL_KERN, KERN_PROC_ARGS, pid, KERN_PROC_PATHNAME};
42 size_t buflen =
sizeof (buf);
43 if (::sysctl (mib, ARRAY_SIZE (mib), buf, &buflen, NULL, 0) != 0)
57 gdb::function_view<
bool (
const struct kinfo_lwp *)>
60 int mib[5] = {CTL_KERN, KERN_LWP, pid,
sizeof (
struct kinfo_lwp), 0};
63 if (sysctl (mib, ARRAY_SIZE (mib), NULL, &size, NULL, 0) == -1 || size == 0)
64 perror_with_name ((
"sysctl"));
66 mib[4] = size /
sizeof (size_t);
68 gdb::unique_xmalloc_ptr<struct kinfo_lwp[]> kl
69 ((
struct kinfo_lwp *) xcalloc (size, 1));
71 if (sysctl (mib, ARRAY_SIZE (mib), kl.get (), &size, NULL, 0) == -1
73 perror_with_name ((
"sysctl"));
75 for (
size_t i = 0; i < size /
sizeof (
struct kinfo_lwp); i++)
77 struct kinfo_lwp *l = &kl[i];
81 = [] (
struct kinfo_lwp *lwp)
112 pid_t pid = ptid.pid ();
113 lwpid_t lwp = ptid.lwp ();
116 = [=] (
const struct kinfo_lwp *kl)
118 return kl->l_lid == lwp;
129 pid_t pid = ptid.pid ();
130 lwpid_t lwp = ptid.lwp ();
132 static char buf[KI_LNAMELEN] = {};
135 = [=] (
const struct kinfo_lwp *kl)
137 if (kl->l_lid == lwp)
139 xsnprintf (buf,
sizeof buf,
"%s", kl->l_name);
157 = [=, &callback] (
const struct kinfo_lwp *kl)
159 ptid_t ptid = ptid_t (pid, kl->l_lid, 0);
174 if (
ptrace (PT_GET_EVENT_MASK, pid, &events,
sizeof (events)) == -1)
175 perror_with_name ((
"ptrace"));
177 events |= PTRACE_LWP_CREATE;
178 events |= PTRACE_LWP_EXIT;
180 if (
ptrace (PT_SET_EVENT_MASK, pid, &events,
sizeof (events)) == -1)
181 perror_with_name ((
"ptrace"));
188 unsigned const char *writebuf, CORE_ADDR offset,
int len)
192 if (offset >
sizeof (siginfo_t))
195 if (
ptrace (PT_GET_SIGINFO, pid, &psi,
sizeof (psi)) == -1)
198 if (offset + len >
sizeof (siginfo_t))
199 len =
sizeof (siginfo_t) - offset;
202 memcpy (readbuf, ((gdb_byte *) &psi.psi_siginfo) + offset, len);
205 memcpy (((gdb_byte *) &psi.psi_siginfo) + offset, writebuf, len);
207 if (
ptrace (PT_SET_SIGINFO, pid, &psi,
sizeof (psi)) == -1)
216write_memory (pid_t pid,
unsigned const char *writebuf, CORE_ADDR offset,
217 size_t len,
size_t *xfered_len)
219 struct ptrace_io_desc io;
220 io.piod_op = PIOD_WRITE_D;
223 size_t bytes_written = 0;
230 io.piod_addr = (
void *)(writebuf + bytes_written);
231 io.piod_offs = (
void *)(offset + bytes_written);
234 int rv =
ptrace (PT_IO, pid, &io, 0);
237 gdb_assert (errno != 0);
240 if (io.piod_len == 0)
243 bytes_written += io.piod_len;
244 io.piod_len = len - bytes_written;
246 while (bytes_written < len);
249 if (xfered_len !=
nullptr)
250 *xfered_len = bytes_written;
259 size_t len,
size_t *xfered_len)
261 struct ptrace_io_desc io;
262 io.piod_op = PIOD_READ_D;
265 size_t bytes_read = 0;
272 io.piod_offs = (
void *)(offset + bytes_read);
273 io.piod_addr = readbuf + bytes_read;
275 int rv =
ptrace (PT_IO, pid, &io, 0);
278 if (io.piod_len == 0)
281 bytes_read += io.piod_len;
282 io.piod_len = len - bytes_read;
284 while (bytes_read < len);
287 if (xfered_len !=
nullptr)
288 *xfered_len = bytes_read;
#define ptrace(request, pid, addr, data)
static bool netbsd_thread_lister(const pid_t pid, gdb::function_view< bool(const struct kinfo_lwp *)> callback)
int write_memory(pid_t pid, unsigned const char *writebuf, CORE_ADDR offset, size_t len, size_t *xfered_len)
void enable_proc_events(pid_t pid)
void for_each_thread(pid_t pid, gdb::function_view< void(ptid_t)> callback)
const char * pid_to_exec_file(pid_t pid)
const char * thread_name(ptid_t ptid)
int qxfer_siginfo(pid_t pid, const char *annex, unsigned char *readbuf, unsigned const char *writebuf, CORE_ADDR offset, int len)
bool thread_alive(ptid_t ptid)
int read_memory(pid_t pid, unsigned char *readbuf, CORE_ADDR offset, size_t len, size_t *xfered_len)