]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/fbsd-nat.c
internal_error: remove need to pass __FILE__/__LINE__
[thirdparty/binutils-gdb.git] / gdb / fbsd-nat.c
CommitLineData
578c1c03
MK
1/* Native-dependent code for FreeBSD.
2
4a94e368 3 Copyright (C) 2002-2022 Free Software Foundation, Inc.
578c1c03
MK
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
578c1c03
MK
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
578c1c03
MK
19
20#include "defs.h"
9385df2a 21#include "gdbsupport/block-signals.h"
268a13a5 22#include "gdbsupport/byte-vector.h"
9385df2a 23#include "gdbsupport/event-loop.h"
4de283e4
TT
24#include "gdbcore.h"
25#include "inferior.h"
26#include "regcache.h"
27#include "regset.h"
cbde90f2 28#include "gdbarch.h"
4de283e4
TT
29#include "gdbcmd.h"
30#include "gdbthread.h"
2ddd4c60 31#include "gdbsupport/buildargv.h"
268a13a5 32#include "gdbsupport/gdb_wait.h"
9385df2a 33#include "inf-loop.h"
4de283e4
TT
34#include "inf-ptrace.h"
35#include <sys/types.h>
09db4332
JB
36#ifdef HAVE_SYS_PROCCTL_H
37#include <sys/procctl.h>
38#endif
68b9939a 39#include <sys/procfs.h>
e58e05d6 40#include <sys/ptrace.h>
929edea9 41#include <sys/signal.h>
68b9939a 42#include <sys/sysctl.h>
25268153 43#include <sys/user.h>
4de283e4 44#include <libutil.h>
4de283e4 45
578c1c03
MK
46#include "elf-bfd.h"
47#include "fbsd-nat.h"
92fce24d 48#include "fbsd-tdep.h"
4de283e4
TT
49
50#include <list>
e8c6b620 51
40c23d88
JB
52#ifndef PT_GETREGSET
53#define PT_GETREGSET 42 /* Get a target register set */
54#define PT_SETREGSET 43 /* Set a target register set */
55#endif
56
766062f6 57/* Return the name of a file that can be opened to get the symbols for
578c1c03
MK
58 the child process identified by PID. */
59
0e90c441 60const char *
f6ac5f3d 61fbsd_nat_target::pid_to_exec_file (int pid)
578c1c03 62{
b4ab256d 63 static char buf[PATH_MAX];
f2feec98 64 size_t buflen;
68b9939a 65 int mib[4];
578c1c03 66
68b9939a
MK
67 mib[0] = CTL_KERN;
68 mib[1] = KERN_PROC;
69 mib[2] = KERN_PROC_PATHNAME;
70 mib[3] = pid;
f2feec98
JB
71 buflen = sizeof buf;
72 if (sysctl (mib, 4, buf, &buflen, NULL, 0) == 0)
b999e203
JB
73 /* The kern.proc.pathname.<pid> sysctl returns a length of zero
74 for processes without an associated executable such as kernel
75 processes. */
76 return buflen == 0 ? NULL : buf;
68b9939a 77
b4ab256d 78 return NULL;
578c1c03
MK
79}
80
25268153 81/* Iterate over all the memory regions in the current inferior,
d1076c41 82 calling FUNC for each memory region. DATA is passed as the last
25268153
JB
83 argument to FUNC. */
84
f6ac5f3d
PA
85int
86fbsd_nat_target::find_memory_regions (find_memory_region_ftype func,
d1076c41 87 void *data)
25268153 88{
e99b03dc 89 pid_t pid = inferior_ptid.pid ();
e4a26669 90 struct kinfo_vmentry *kve;
25268153 91 uint64_t size;
25268153
JB
92 int i, nitems;
93
262f62f5 94 gdb::unique_xmalloc_ptr<struct kinfo_vmentry>
e4a26669 95 vmentl (kinfo_getvmmap (pid, &nitems));
25268153 96 if (vmentl == NULL)
deb70aa0 97 perror_with_name (_("Couldn't fetch VM map entries"));
25268153 98
e4a26669 99 for (i = 0, kve = vmentl.get (); i < nitems; i++, kve++)
25268153 100 {
25268153
JB
101 /* Skip unreadable segments and those where MAP_NOCORE has been set. */
102 if (!(kve->kve_protection & KVME_PROT_READ)
103 || kve->kve_flags & KVME_FLAG_NOCOREDUMP)
104 continue;
105
106 /* Skip segments with an invalid type. */
107 if (kve->kve_type != KVME_TYPE_DEFAULT
108 && kve->kve_type != KVME_TYPE_VNODE
109 && kve->kve_type != KVME_TYPE_SWAP
110 && kve->kve_type != KVME_TYPE_PHYS)
111 continue;
112
113 size = kve->kve_end - kve->kve_start;
114 if (info_verbose)
115 {
6cb06a8c
TT
116 gdb_printf ("Save segment, %ld bytes at %s (%c%c%c)\n",
117 (long) size,
118 paddress (target_gdbarch (), kve->kve_start),
119 kve->kve_protection & KVME_PROT_READ ? 'r' : '-',
120 kve->kve_protection & KVME_PROT_WRITE ? 'w' : '-',
121 kve->kve_protection & KVME_PROT_EXEC ? 'x' : '-');
25268153
JB
122 }
123
124 /* Invoke the callback function to create the corefile segment.
125 Pass MODIFIED as true, we do not know the real modification state. */
126 func (kve->kve_start, size, kve->kve_protection & KVME_PROT_READ,
127 kve->kve_protection & KVME_PROT_WRITE,
e01493b7 128 kve->kve_protection & KVME_PROT_EXEC, 1, false, data);
25268153 129 }
25268153
JB
130 return 0;
131}
8f60fe01 132
92fce24d
JB
133/* Fetch the command line for a running process. */
134
135static gdb::unique_xmalloc_ptr<char>
136fbsd_fetch_cmdline (pid_t pid)
137{
138 size_t len;
139 int mib[4];
140
141 len = 0;
142 mib[0] = CTL_KERN;
143 mib[1] = KERN_PROC;
144 mib[2] = KERN_PROC_ARGS;
145 mib[3] = pid;
146 if (sysctl (mib, 4, NULL, &len, NULL, 0) == -1)
147 return nullptr;
148
149 if (len == 0)
150 return nullptr;
151
152 gdb::unique_xmalloc_ptr<char> cmdline ((char *) xmalloc (len));
153 if (sysctl (mib, 4, cmdline.get (), &len, NULL, 0) == -1)
154 return nullptr;
155
424eb552
JB
156 /* Join the arguments with spaces to form a single string. */
157 char *cp = cmdline.get ();
158 for (size_t i = 0; i < len - 1; i++)
159 if (cp[i] == '\0')
160 cp[i] = ' ';
161 cp[len - 1] = '\0';
162
92fce24d
JB
163 return cmdline;
164}
165
166/* Fetch the external variant of the kernel's internal process
167 structure for the process PID into KP. */
168
169static bool
170fbsd_fetch_kinfo_proc (pid_t pid, struct kinfo_proc *kp)
171{
172 size_t len;
173 int mib[4];
174
175 len = sizeof *kp;
176 mib[0] = CTL_KERN;
177 mib[1] = KERN_PROC;
178 mib[2] = KERN_PROC_PID;
179 mib[3] = pid;
180 return (sysctl (mib, 4, kp, &len, NULL, 0) == 0);
181}
182
f6ac5f3d 183/* Implement the "info_proc" target_ops method. */
92fce24d 184
f6ac5f3d
PA
185bool
186fbsd_nat_target::info_proc (const char *args, enum info_proc_what what)
92fce24d 187{
92fce24d
JB
188 gdb::unique_xmalloc_ptr<struct kinfo_file> fdtbl;
189 int nfd = 0;
92fce24d 190 struct kinfo_proc kp;
92fce24d
JB
191 pid_t pid;
192 bool do_cmdline = false;
193 bool do_cwd = false;
194 bool do_exe = false;
7e69672e 195 bool do_files = false;
92fce24d 196 bool do_mappings = false;
92fce24d
JB
197 bool do_status = false;
198
199 switch (what)
200 {
201 case IP_MINIMAL:
202 do_cmdline = true;
203 do_cwd = true;
204 do_exe = true;
205 break;
92fce24d
JB
206 case IP_MAPPINGS:
207 do_mappings = true;
208 break;
92fce24d
JB
209 case IP_STATUS:
210 case IP_STAT:
211 do_status = true;
212 break;
213 case IP_CMDLINE:
214 do_cmdline = true;
215 break;
216 case IP_EXE:
217 do_exe = true;
218 break;
219 case IP_CWD:
220 do_cwd = true;
221 break;
7e69672e
JB
222 case IP_FILES:
223 do_files = true;
224 break;
92fce24d
JB
225 case IP_ALL:
226 do_cmdline = true;
227 do_cwd = true;
228 do_exe = true;
7e69672e 229 do_files = true;
92fce24d 230 do_mappings = true;
92fce24d
JB
231 do_status = true;
232 break;
233 default:
234 error (_("Not supported on this target."));
235 }
236
237 gdb_argv built_argv (args);
238 if (built_argv.count () == 0)
239 {
e99b03dc 240 pid = inferior_ptid.pid ();
92fce24d
JB
241 if (pid == 0)
242 error (_("No current process: you must name one."));
243 }
244 else if (built_argv.count () == 1 && isdigit (built_argv[0][0]))
245 pid = strtol (built_argv[0], NULL, 10);
246 else
247 error (_("Invalid arguments."));
248
6cb06a8c 249 gdb_printf (_("process %d\n"), pid);
7e69672e 250 if (do_cwd || do_exe || do_files)
92fce24d 251 fdtbl.reset (kinfo_getfile (pid, &nfd));
92fce24d
JB
252
253 if (do_cmdline)
254 {
255 gdb::unique_xmalloc_ptr<char> cmdline = fbsd_fetch_cmdline (pid);
256 if (cmdline != nullptr)
6cb06a8c 257 gdb_printf ("cmdline = '%s'\n", cmdline.get ());
92fce24d
JB
258 else
259 warning (_("unable to fetch command line"));
260 }
261 if (do_cwd)
262 {
263 const char *cwd = NULL;
92fce24d
JB
264 struct kinfo_file *kf = fdtbl.get ();
265 for (int i = 0; i < nfd; i++, kf++)
266 {
267 if (kf->kf_type == KF_TYPE_VNODE && kf->kf_fd == KF_FD_TYPE_CWD)
268 {
269 cwd = kf->kf_path;
270 break;
271 }
272 }
92fce24d 273 if (cwd != NULL)
6cb06a8c 274 gdb_printf ("cwd = '%s'\n", cwd);
92fce24d
JB
275 else
276 warning (_("unable to fetch current working directory"));
277 }
278 if (do_exe)
279 {
280 const char *exe = NULL;
92fce24d
JB
281 struct kinfo_file *kf = fdtbl.get ();
282 for (int i = 0; i < nfd; i++, kf++)
283 {
284 if (kf->kf_type == KF_TYPE_VNODE && kf->kf_fd == KF_FD_TYPE_TEXT)
285 {
286 exe = kf->kf_path;
287 break;
288 }
289 }
92fce24d 290 if (exe == NULL)
f6ac5f3d 291 exe = pid_to_exec_file (pid);
92fce24d 292 if (exe != NULL)
6cb06a8c 293 gdb_printf ("exe = '%s'\n", exe);
92fce24d
JB
294 else
295 warning (_("unable to fetch executable path name"));
296 }
7e69672e
JB
297 if (do_files)
298 {
299 struct kinfo_file *kf = fdtbl.get ();
300
301 if (nfd > 0)
302 {
303 fbsd_info_proc_files_header ();
304 for (int i = 0; i < nfd; i++, kf++)
305 fbsd_info_proc_files_entry (kf->kf_type, kf->kf_fd, kf->kf_flags,
306 kf->kf_offset, kf->kf_vnode_type,
307 kf->kf_sock_domain, kf->kf_sock_type,
308 kf->kf_sock_protocol, &kf->kf_sa_local,
309 &kf->kf_sa_peer, kf->kf_path);
310 }
311 else
312 warning (_("unable to fetch list of open files"));
313 }
92fce24d
JB
314 if (do_mappings)
315 {
316 int nvment;
317 gdb::unique_xmalloc_ptr<struct kinfo_vmentry>
318 vmentl (kinfo_getvmmap (pid, &nvment));
319
320 if (vmentl != nullptr)
321 {
6f3b1098
JB
322 int addr_bit = TARGET_CHAR_BIT * sizeof (void *);
323 fbsd_info_proc_mappings_header (addr_bit);
92fce24d
JB
324
325 struct kinfo_vmentry *kve = vmentl.get ();
326 for (int i = 0; i < nvment; i++, kve++)
6f3b1098
JB
327 fbsd_info_proc_mappings_entry (addr_bit, kve->kve_start,
328 kve->kve_end, kve->kve_offset,
329 kve->kve_flags, kve->kve_protection,
330 kve->kve_path);
92fce24d
JB
331 }
332 else
333 warning (_("unable to fetch virtual memory map"));
334 }
92fce24d
JB
335 if (do_status)
336 {
337 if (!fbsd_fetch_kinfo_proc (pid, &kp))
338 warning (_("Failed to fetch process information"));
339 else
340 {
341 const char *state;
342 int pgtok;
343
6cb06a8c 344 gdb_printf ("Name: %s\n", kp.ki_comm);
92fce24d
JB
345 switch (kp.ki_stat)
346 {
347 case SIDL:
348 state = "I (idle)";
349 break;
350 case SRUN:
351 state = "R (running)";
352 break;
353 case SSTOP:
354 state = "T (stopped)";
355 break;
356 case SZOMB:
357 state = "Z (zombie)";
358 break;
359 case SSLEEP:
360 state = "S (sleeping)";
361 break;
362 case SWAIT:
363 state = "W (interrupt wait)";
364 break;
365 case SLOCK:
366 state = "L (blocked on lock)";
367 break;
368 default:
369 state = "? (unknown)";
370 break;
371 }
6cb06a8c
TT
372 gdb_printf ("State: %s\n", state);
373 gdb_printf ("Parent process: %d\n", kp.ki_ppid);
374 gdb_printf ("Process group: %d\n", kp.ki_pgid);
375 gdb_printf ("Session id: %d\n", kp.ki_sid);
376 gdb_printf ("TTY: %s\n", pulongest (kp.ki_tdev));
377 gdb_printf ("TTY owner process group: %d\n", kp.ki_tpgid);
378 gdb_printf ("User IDs (real, effective, saved): %d %d %d\n",
379 kp.ki_ruid, kp.ki_uid, kp.ki_svuid);
380 gdb_printf ("Group IDs (real, effective, saved): %d %d %d\n",
381 kp.ki_rgid, kp.ki_groups[0], kp.ki_svgid);
382 gdb_printf ("Groups: ");
92fce24d 383 for (int i = 0; i < kp.ki_ngroups; i++)
6cb06a8c
TT
384 gdb_printf ("%d ", kp.ki_groups[i]);
385 gdb_printf ("\n");
386 gdb_printf ("Minor faults (no memory page): %ld\n",
387 kp.ki_rusage.ru_minflt);
388 gdb_printf ("Minor faults, children: %ld\n",
389 kp.ki_rusage_ch.ru_minflt);
390 gdb_printf ("Major faults (memory page faults): %ld\n",
391 kp.ki_rusage.ru_majflt);
392 gdb_printf ("Major faults, children: %ld\n",
393 kp.ki_rusage_ch.ru_majflt);
394 gdb_printf ("utime: %s.%06ld\n",
395 plongest (kp.ki_rusage.ru_utime.tv_sec),
396 kp.ki_rusage.ru_utime.tv_usec);
397 gdb_printf ("stime: %s.%06ld\n",
398 plongest (kp.ki_rusage.ru_stime.tv_sec),
399 kp.ki_rusage.ru_stime.tv_usec);
400 gdb_printf ("utime, children: %s.%06ld\n",
401 plongest (kp.ki_rusage_ch.ru_utime.tv_sec),
402 kp.ki_rusage_ch.ru_utime.tv_usec);
403 gdb_printf ("stime, children: %s.%06ld\n",
404 plongest (kp.ki_rusage_ch.ru_stime.tv_sec),
405 kp.ki_rusage_ch.ru_stime.tv_usec);
406 gdb_printf ("'nice' value: %d\n", kp.ki_nice);
407 gdb_printf ("Start time: %s.%06ld\n",
408 plongest (kp.ki_start.tv_sec),
409 kp.ki_start.tv_usec);
92fce24d 410 pgtok = getpagesize () / 1024;
6cb06a8c
TT
411 gdb_printf ("Virtual memory size: %s kB\n",
412 pulongest (kp.ki_size / 1024));
413 gdb_printf ("Data size: %s kB\n",
414 pulongest (kp.ki_dsize * pgtok));
415 gdb_printf ("Stack size: %s kB\n",
416 pulongest (kp.ki_ssize * pgtok));
417 gdb_printf ("Text size: %s kB\n",
418 pulongest (kp.ki_tsize * pgtok));
419 gdb_printf ("Resident set size: %s kB\n",
420 pulongest (kp.ki_rssize * pgtok));
421 gdb_printf ("Maximum RSS: %s kB\n",
422 pulongest (kp.ki_rusage.ru_maxrss));
423 gdb_printf ("Pending Signals: ");
92fce24d 424 for (int i = 0; i < _SIG_WORDS; i++)
6cb06a8c
TT
425 gdb_printf ("%08x ", kp.ki_siglist.__bits[i]);
426 gdb_printf ("\n");
427 gdb_printf ("Ignored Signals: ");
92fce24d 428 for (int i = 0; i < _SIG_WORDS; i++)
6cb06a8c
TT
429 gdb_printf ("%08x ", kp.ki_sigignore.__bits[i]);
430 gdb_printf ("\n");
431 gdb_printf ("Caught Signals: ");
92fce24d 432 for (int i = 0; i < _SIG_WORDS; i++)
6cb06a8c
TT
433 gdb_printf ("%08x ", kp.ki_sigcatch.__bits[i]);
434 gdb_printf ("\n");
92fce24d
JB
435 }
436 }
f6ac5f3d
PA
437
438 return true;
92fce24d
JB
439}
440
929edea9
JB
441/* Return the size of siginfo for the current inferior. */
442
443#ifdef __LP64__
444union sigval32 {
445 int sival_int;
446 uint32_t sival_ptr;
447};
448
449/* This structure matches the naming and layout of `siginfo_t' in
450 <sys/signal.h>. In particular, the `si_foo' macros defined in that
451 header can be used with both types to copy fields in the `_reason'
452 union. */
453
454struct siginfo32
455{
456 int si_signo;
457 int si_errno;
458 int si_code;
459 __pid_t si_pid;
460 __uid_t si_uid;
461 int si_status;
462 uint32_t si_addr;
463 union sigval32 si_value;
464 union
465 {
466 struct
467 {
468 int _trapno;
469 } _fault;
470 struct
471 {
472 int _timerid;
473 int _overrun;
474 } _timer;
475 struct
476 {
477 int _mqd;
478 } _mesgq;
479 struct
480 {
481 int32_t _band;
482 } _poll;
483 struct
484 {
485 int32_t __spare1__;
486 int __spare2__[7];
487 } __spare__;
488 } _reason;
489};
490#endif
491
492static size_t
493fbsd_siginfo_size ()
494{
495#ifdef __LP64__
496 struct gdbarch *gdbarch = get_frame_arch (get_current_frame ());
497
498 /* Is the inferior 32-bit? If so, use the 32-bit siginfo size. */
a181c0bf 499 if (gdbarch_long_bit (gdbarch) == 32)
929edea9
JB
500 return sizeof (struct siginfo32);
501#endif
502 return sizeof (siginfo_t);
503}
504
505/* Convert a native 64-bit siginfo object to a 32-bit object. Note
506 that FreeBSD doesn't support writing to $_siginfo, so this only
507 needs to convert one way. */
508
509static void
510fbsd_convert_siginfo (siginfo_t *si)
511{
512#ifdef __LP64__
513 struct gdbarch *gdbarch = get_frame_arch (get_current_frame ());
514
515 /* Is the inferior 32-bit? If not, nothing to do. */
a181c0bf 516 if (gdbarch_long_bit (gdbarch) != 32)
929edea9
JB
517 return;
518
519 struct siginfo32 si32;
520
521 si32.si_signo = si->si_signo;
522 si32.si_errno = si->si_errno;
523 si32.si_code = si->si_code;
524 si32.si_pid = si->si_pid;
525 si32.si_uid = si->si_uid;
526 si32.si_status = si->si_status;
527 si32.si_addr = (uintptr_t) si->si_addr;
528
529 /* If sival_ptr is being used instead of sival_int on a big-endian
530 platform, then sival_int will be zero since it holds the upper
531 32-bits of the pointer value. */
532#if _BYTE_ORDER == _BIG_ENDIAN
533 if (si->si_value.sival_int == 0)
0335ac6d 534 si32.si_value.sival_ptr = (uintptr_t) si->si_value.sival_ptr;
929edea9
JB
535 else
536 si32.si_value.sival_int = si->si_value.sival_int;
537#else
538 si32.si_value.sival_int = si->si_value.sival_int;
539#endif
540
541 /* Always copy the spare fields and then possibly overwrite them for
542 signal-specific or code-specific fields. */
543 si32._reason.__spare__.__spare1__ = si->_reason.__spare__.__spare1__;
544 for (int i = 0; i < 7; i++)
545 si32._reason.__spare__.__spare2__[i] = si->_reason.__spare__.__spare2__[i];
546 switch (si->si_signo) {
547 case SIGILL:
548 case SIGFPE:
549 case SIGSEGV:
550 case SIGBUS:
551 si32.si_trapno = si->si_trapno;
552 break;
553 }
554 switch (si->si_code) {
555 case SI_TIMER:
556 si32.si_timerid = si->si_timerid;
557 si32.si_overrun = si->si_overrun;
558 break;
559 case SI_MESGQ:
560 si32.si_mqd = si->si_mqd;
561 break;
562 }
563
564 memcpy(si, &si32, sizeof (si32));
565#endif
566}
929edea9 567
f6ac5f3d 568/* Implement the "xfer_partial" target_ops method. */
7697fc9e 569
f6ac5f3d
PA
570enum target_xfer_status
571fbsd_nat_target::xfer_partial (enum target_object object,
572 const char *annex, gdb_byte *readbuf,
573 const gdb_byte *writebuf,
574 ULONGEST offset, ULONGEST len,
575 ULONGEST *xfered_len)
7697fc9e 576{
e99b03dc 577 pid_t pid = inferior_ptid.pid ();
7697fc9e
JB
578
579 switch (object)
580 {
929edea9
JB
581 case TARGET_OBJECT_SIGNAL_INFO:
582 {
583 struct ptrace_lwpinfo pl;
584 size_t siginfo_size;
585
586 /* FreeBSD doesn't support writing to $_siginfo. */
587 if (writebuf != NULL)
588 return TARGET_XFER_E_IO;
589
590 if (inferior_ptid.lwp_p ())
591 pid = inferior_ptid.lwp ();
592
593 siginfo_size = fbsd_siginfo_size ();
594 if (offset > siginfo_size)
595 return TARGET_XFER_E_IO;
596
597 if (ptrace (PT_LWPINFO, pid, (PTRACE_TYPE_ARG3) &pl, sizeof (pl)) == -1)
598 return TARGET_XFER_E_IO;
599
600 if (!(pl.pl_flags & PL_FLAG_SI))
601 return TARGET_XFER_E_IO;
602
603 fbsd_convert_siginfo (&pl.pl_siginfo);
604 if (offset + len > siginfo_size)
605 len = siginfo_size - offset;
606
607 memcpy (readbuf, ((gdb_byte *) &pl.pl_siginfo) + offset, len);
608 *xfered_len = len;
609 return TARGET_XFER_OK;
610 }
f8eb6a9e 611#ifdef KERN_PROC_AUXV
7697fc9e
JB
612 case TARGET_OBJECT_AUXV:
613 {
e4a26669
JB
614 gdb::byte_vector buf_storage;
615 gdb_byte *buf;
7697fc9e
JB
616 size_t buflen;
617 int mib[4];
618
619 if (writebuf != NULL)
620 return TARGET_XFER_E_IO;
621 mib[0] = CTL_KERN;
622 mib[1] = KERN_PROC;
623 mib[2] = KERN_PROC_AUXV;
624 mib[3] = pid;
625 if (offset == 0)
626 {
627 buf = readbuf;
628 buflen = len;
629 }
630 else
631 {
632 buflen = offset + len;
e4a26669
JB
633 buf_storage.resize (buflen);
634 buf = buf_storage.data ();
7697fc9e
JB
635 }
636 if (sysctl (mib, 4, buf, &buflen, NULL, 0) == 0)
637 {
638 if (offset != 0)
639 {
640 if (buflen > offset)
641 {
642 buflen -= offset;
643 memcpy (readbuf, buf + offset, buflen);
644 }
645 else
646 buflen = 0;
647 }
7697fc9e
JB
648 *xfered_len = buflen;
649 return (buflen == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
650 }
7697fc9e
JB
651 return TARGET_XFER_E_IO;
652 }
f8eb6a9e
JB
653#endif
654#if defined(KERN_PROC_VMMAP) && defined(KERN_PROC_PS_STRINGS)
739ab2e9
SS
655 case TARGET_OBJECT_FREEBSD_VMMAP:
656 case TARGET_OBJECT_FREEBSD_PS_STRINGS:
657 {
658 gdb::byte_vector buf_storage;
659 gdb_byte *buf;
660 size_t buflen;
661 int mib[4];
662
663 int proc_target;
664 uint32_t struct_size;
665 switch (object)
666 {
667 case TARGET_OBJECT_FREEBSD_VMMAP:
668 proc_target = KERN_PROC_VMMAP;
669 struct_size = sizeof (struct kinfo_vmentry);
670 break;
671 case TARGET_OBJECT_FREEBSD_PS_STRINGS:
672 proc_target = KERN_PROC_PS_STRINGS;
673 struct_size = sizeof (void *);
674 break;
675 }
676
677 if (writebuf != NULL)
678 return TARGET_XFER_E_IO;
679
680 mib[0] = CTL_KERN;
681 mib[1] = KERN_PROC;
682 mib[2] = proc_target;
683 mib[3] = pid;
684
685 if (sysctl (mib, 4, NULL, &buflen, NULL, 0) != 0)
686 return TARGET_XFER_E_IO;
687 buflen += sizeof (struct_size);
688
689 if (offset >= buflen)
690 {
691 *xfered_len = 0;
692 return TARGET_XFER_EOF;
693 }
694
695 buf_storage.resize (buflen);
696 buf = buf_storage.data ();
697
698 memcpy (buf, &struct_size, sizeof (struct_size));
699 buflen -= sizeof (struct_size);
700 if (sysctl (mib, 4, buf + sizeof (struct_size), &buflen, NULL, 0) != 0)
701 return TARGET_XFER_E_IO;
702 buflen += sizeof (struct_size);
703
704 if (buflen - offset < len)
705 len = buflen - offset;
706 memcpy (readbuf, buf + offset, len);
707 *xfered_len = len;
708 return TARGET_XFER_OK;
709 }
f8eb6a9e 710#endif
7697fc9e 711 default:
f6ac5f3d
PA
712 return inf_ptrace_target::xfer_partial (object, annex,
713 readbuf, writebuf, offset,
714 len, xfered_len);
7697fc9e
JB
715 }
716}
7697fc9e 717
491144b5
CB
718static bool debug_fbsd_lwp;
719static bool debug_fbsd_nat;
6e9567fe 720
6e9567fe
JB
721static void
722show_fbsd_lwp_debug (struct ui_file *file, int from_tty,
723 struct cmd_list_element *c, const char *value)
724{
6cb06a8c 725 gdb_printf (file, _("Debugging of FreeBSD lwp module is %s.\n"), value);
6e9567fe
JB
726}
727
386a8676
JB
728static void
729show_fbsd_nat_debug (struct ui_file *file, int from_tty,
730 struct cmd_list_element *c, const char *value)
731{
6cb06a8c
TT
732 gdb_printf (file, _("Debugging of FreeBSD native target is %s.\n"),
733 value);
386a8676
JB
734}
735
c45ecc9d
JB
736#define fbsd_lwp_debug_printf(fmt, ...) \
737 debug_prefixed_printf_cond (debug_fbsd_lwp, "fbsd-lwp", fmt, ##__VA_ARGS__)
738
739#define fbsd_nat_debug_printf(fmt, ...) \
740 debug_prefixed_printf_cond (debug_fbsd_nat, "fbsd-nat", fmt, ##__VA_ARGS__)
741
742
6e9567fe
JB
743/*
744 FreeBSD's first thread support was via a "reentrant" version of libc
745 (libc_r) that first shipped in 2.2.7. This library multiplexed all
746 of the threads in a process onto a single kernel thread. This
4c7bf4f9 747 library was supported via the bsd-uthread target.
6e9567fe
JB
748
749 FreeBSD 5.1 introduced two new threading libraries that made use of
750 multiple kernel threads. The first (libkse) scheduled M user
751 threads onto N (<= M) kernel threads (LWPs). The second (libthr)
752 bound each user thread to a dedicated kernel thread. libkse shipped
753 as the default threading library (libpthread).
754
755 FreeBSD 5.3 added a libthread_db to abstract the interface across
756 the various thread libraries (libc_r, libkse, and libthr).
757
758 FreeBSD 7.0 switched the default threading library from from libkse
759 to libpthread and removed libc_r.
760
761 FreeBSD 8.0 removed libkse and the in-kernel support for it. The
762 only threading library supported by 8.0 and later is libthr which
763 ties each user thread directly to an LWP. To simplify the
764 implementation, this target only supports LWP-backed threads using
765 ptrace directly rather than libthread_db.
766
767 FreeBSD 11.0 introduced LWP event reporting via PT_LWP_EVENTS.
768*/
769
770/* Return true if PTID is still active in the inferior. */
771
57810aa7 772bool
f6ac5f3d 773fbsd_nat_target::thread_alive (ptid_t ptid)
6e9567fe 774{
15a9e13e 775 if (ptid.lwp_p ())
6e9567fe
JB
776 {
777 struct ptrace_lwpinfo pl;
778
e38504b3 779 if (ptrace (PT_LWPINFO, ptid.lwp (), (caddr_t) &pl, sizeof pl)
6e9567fe 780 == -1)
57810aa7 781 return false;
6e9567fe
JB
782#ifdef PL_FLAG_EXITED
783 if (pl.pl_flags & PL_FLAG_EXITED)
57810aa7 784 return false;
6e9567fe
JB
785#endif
786 }
787
57810aa7 788 return true;
6e9567fe
JB
789}
790
a068643d 791/* Convert PTID to a string. */
6e9567fe 792
a068643d 793std::string
f6ac5f3d 794fbsd_nat_target::pid_to_str (ptid_t ptid)
6e9567fe
JB
795{
796 lwpid_t lwp;
797
e38504b3 798 lwp = ptid.lwp ();
6e9567fe
JB
799 if (lwp != 0)
800 {
e99b03dc 801 int pid = ptid.pid ();
6e9567fe 802
a068643d 803 return string_printf ("LWP %d of process %d", lwp, pid);
6e9567fe
JB
804 }
805
806 return normal_pid_to_str (ptid);
807}
808
809#ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_TDNAME
810/* Return the name assigned to a thread by an application. Returns
811 the string in a static buffer. */
812
f6ac5f3d
PA
813const char *
814fbsd_nat_target::thread_name (struct thread_info *thr)
6e9567fe
JB
815{
816 struct ptrace_lwpinfo pl;
817 struct kinfo_proc kp;
e99b03dc 818 int pid = thr->ptid.pid ();
e38504b3 819 long lwp = thr->ptid.lwp ();
6e9567fe
JB
820 static char buf[sizeof pl.pl_tdname + 1];
821
822 /* Note that ptrace_lwpinfo returns the process command in pl_tdname
823 if a name has not been set explicitly. Return a NULL name in
824 that case. */
92fce24d 825 if (!fbsd_fetch_kinfo_proc (pid, &kp))
d0bbe64c 826 return nullptr;
6e9567fe 827 if (ptrace (PT_LWPINFO, lwp, (caddr_t) &pl, sizeof pl) == -1)
d0bbe64c 828 return nullptr;
6e9567fe
JB
829 if (strcmp (kp.ki_comm, pl.pl_tdname) == 0)
830 return NULL;
831 xsnprintf (buf, sizeof buf, "%s", pl.pl_tdname);
832 return buf;
833}
834#endif
835
da95a26c 836/* Enable additional event reporting on new processes.
6e9567fe 837
da95a26c
JB
838 To catch fork events, PTRACE_FORK is set on every traced process
839 to enable stops on returns from fork or vfork. Note that both the
840 parent and child will always stop, even if system call stops are
841 not enabled.
842
843 To catch LWP events, PTRACE_EVENTS is set on every traced process.
6e9567fe
JB
844 This enables stops on the birth for new LWPs (excluding the "main" LWP)
845 and the death of LWPs (excluding the last LWP in a process). Note
846 that unlike fork events, the LWP that creates a new LWP does not
847 report an event. */
848
849static void
da95a26c 850fbsd_enable_proc_events (pid_t pid)
6e9567fe 851{
da95a26c
JB
852#ifdef PT_GET_EVENT_MASK
853 int events;
854
855 if (ptrace (PT_GET_EVENT_MASK, pid, (PTRACE_TYPE_ARG3)&events,
856 sizeof (events)) == -1)
5efac66c 857 perror_with_name (("ptrace (PT_GET_EVENT_MASK)"));
da95a26c 858 events |= PTRACE_FORK | PTRACE_LWP;
dbaed385
JB
859#ifdef PTRACE_VFORK
860 events |= PTRACE_VFORK;
861#endif
da95a26c
JB
862 if (ptrace (PT_SET_EVENT_MASK, pid, (PTRACE_TYPE_ARG3)&events,
863 sizeof (events)) == -1)
5efac66c 864 perror_with_name (("ptrace (PT_SET_EVENT_MASK)"));
da95a26c
JB
865#else
866#ifdef TDP_RFPPWAIT
867 if (ptrace (PT_FOLLOW_FORK, pid, (PTRACE_TYPE_ARG3)0, 1) == -1)
5efac66c 868 perror_with_name (("ptrace (PT_FOLLOW_FORK)"));
da95a26c
JB
869#endif
870#ifdef PT_LWP_EVENTS
6e9567fe 871 if (ptrace (PT_LWP_EVENTS, pid, (PTRACE_TYPE_ARG3)0, 1) == -1)
5efac66c 872 perror_with_name (("ptrace (PT_LWP_EVENTS)"));
6e9567fe 873#endif
da95a26c
JB
874#endif
875}
6e9567fe
JB
876
877/* Add threads for any new LWPs in a process.
878
879 When LWP events are used, this function is only used to detect existing
880 threads when attaching to a process. On older systems, this function is
881 called to discover new threads each time the thread list is updated. */
882
883static void
5b6d1e4f 884fbsd_add_threads (fbsd_nat_target *target, pid_t pid)
6e9567fe 885{
6e9567fe
JB
886 int i, nlwps;
887
5b6d1e4f 888 gdb_assert (!in_thread_list (target, ptid_t (pid)));
6e9567fe
JB
889 nlwps = ptrace (PT_GETNUMLWPS, pid, NULL, 0);
890 if (nlwps == -1)
5efac66c 891 perror_with_name (("ptrace (PT_GETNUMLWPS)"));
6e9567fe 892
329d5e7e 893 gdb::unique_xmalloc_ptr<lwpid_t[]> lwps (XCNEWVEC (lwpid_t, nlwps));
6e9567fe 894
e4a26669 895 nlwps = ptrace (PT_GETLWPLIST, pid, (caddr_t) lwps.get (), nlwps);
6e9567fe 896 if (nlwps == -1)
5efac66c 897 perror_with_name (("ptrace (PT_GETLWPLIST)"));
6e9567fe
JB
898
899 for (i = 0; i < nlwps; i++)
900 {
184ea2f7 901 ptid_t ptid = ptid_t (pid, lwps[i]);
6e9567fe 902
5b6d1e4f 903 if (!in_thread_list (target, ptid))
6e9567fe
JB
904 {
905#ifdef PT_LWP_EVENTS
906 struct ptrace_lwpinfo pl;
907
908 /* Don't add exited threads. Note that this is only called
909 when attaching to a multi-threaded process. */
329d5e7e 910 if (ptrace (PT_LWPINFO, lwps[i], (caddr_t) &pl, sizeof pl) == -1)
5efac66c 911 perror_with_name (("ptrace (PT_LWPINFO)"));
6e9567fe
JB
912 if (pl.pl_flags & PL_FLAG_EXITED)
913 continue;
914#endif
c45ecc9d 915 fbsd_lwp_debug_printf ("adding thread for LWP %u", lwps[i]);
5b6d1e4f 916 add_thread (target, ptid);
6e9567fe
JB
917 }
918 }
6e9567fe
JB
919}
920
f6ac5f3d 921/* Implement the "update_thread_list" target_ops method. */
6e9567fe 922
f6ac5f3d
PA
923void
924fbsd_nat_target::update_thread_list ()
6e9567fe
JB
925{
926#ifdef PT_LWP_EVENTS
927 /* With support for thread events, threads are added/deleted from the
928 list as events are reported, so just try deleting exited threads. */
929 delete_exited_threads ();
930#else
931 prune_threads ();
932
5b6d1e4f 933 fbsd_add_threads (this, inferior_ptid.pid ());
6e9567fe
JB
934#endif
935}
936
9385df2a
JB
937/* Async mode support. */
938
9385df2a
JB
939/* Implement the "can_async_p" target method. */
940
941bool
942fbsd_nat_target::can_async_p ()
943{
944 /* This flag should be checked in the common target.c code. */
945 gdb_assert (target_async_permitted);
946
947 /* Otherwise, this targets is always able to support async mode. */
948 return true;
949}
950
9385df2a
JB
951/* SIGCHLD handler notifies the event-loop in async mode. */
952
953static void
954sigchld_handler (int signo)
955{
956 int old_errno = errno;
957
b146ba14 958 fbsd_nat_target::async_file_mark_if_open ();
9385df2a
JB
959
960 errno = old_errno;
961}
962
963/* Callback registered with the target events file descriptor. */
964
965static void
966handle_target_event (int error, gdb_client_data client_data)
967{
968 inferior_event_handler (INF_REG_EVENT);
969}
970
971/* Implement the "async" target method. */
972
973void
4a570176 974fbsd_nat_target::async (bool enable)
9385df2a 975{
4a570176 976 if (enable == is_async_p ())
9385df2a
JB
977 return;
978
979 /* Block SIGCHILD while we create/destroy the pipe, as the handler
980 writes to it. */
981 gdb::block_signals blocker;
982
983 if (enable)
984 {
b146ba14 985 if (!async_file_open ())
f34652de 986 internal_error ("failed to create event pipe.");
9385df2a 987
b146ba14 988 add_file_handler (async_wait_fd (), handle_target_event, NULL, "fbsd-nat");
9385df2a
JB
989
990 /* Trigger a poll in case there are pending events to
991 handle. */
b146ba14 992 async_file_mark ();
9385df2a
JB
993 }
994 else
995 {
b146ba14
JB
996 delete_file_handler (async_wait_fd ());
997 async_file_close ();
9385df2a
JB
998 }
999}
1000
e58e05d6
JB
1001#ifdef TDP_RFPPWAIT
1002/*
1003 To catch fork events, PT_FOLLOW_FORK is set on every traced process
1004 to enable stops on returns from fork or vfork. Note that both the
1005 parent and child will always stop, even if system call stops are not
1006 enabled.
1007
1008 After a fork, both the child and parent process will stop and report
1009 an event. However, there is no guarantee of order. If the parent
1010 reports its stop first, then fbsd_wait explicitly waits for the new
1011 child before returning. If the child reports its stop first, then
1012 the event is saved on a list and ignored until the parent's stop is
1013 reported. fbsd_wait could have been changed to fetch the parent PID
1014 of the new child and used that to wait for the parent explicitly.
1015 However, if two threads in the parent fork at the same time, then
1016 the wait on the parent might return the "wrong" fork event.
1017
1018 The initial version of PT_FOLLOW_FORK did not set PL_FLAG_CHILD for
1019 the new child process. This flag could be inferred by treating any
1020 events for an unknown pid as a new child.
1021
1022 In addition, the initial version of PT_FOLLOW_FORK did not report a
1023 stop event for the parent process of a vfork until after the child
1024 process executed a new program or exited. The kernel was changed to
1025 defer the wait for exit or exec of the child until after posting the
1026 stop event shortly after the change to introduce PL_FLAG_CHILD.
1027 This could be worked around by reporting a vfork event when the
1028 child event posted and ignoring the subsequent event from the
1029 parent.
1030
1031 This implementation requires both of these fixes for simplicity's
1032 sake. FreeBSD versions newer than 9.1 contain both fixes.
1033*/
1034
e8c6b620 1035static std::list<ptid_t> fbsd_pending_children;
e58e05d6
JB
1036
1037/* Record a new child process event that is reported before the
1038 corresponding fork event in the parent. */
1039
1040static void
6e9567fe 1041fbsd_remember_child (ptid_t pid)
e58e05d6 1042{
e8c6b620 1043 fbsd_pending_children.push_front (pid);
e58e05d6
JB
1044}
1045
1046/* Check for a previously-recorded new child process event for PID.
6e9567fe 1047 If one is found, remove it from the list and return the PTID. */
e58e05d6 1048
6e9567fe 1049static ptid_t
e58e05d6
JB
1050fbsd_is_child_pending (pid_t pid)
1051{
e8c6b620
JB
1052 for (auto it = fbsd_pending_children.begin ();
1053 it != fbsd_pending_children.end (); it++)
1054 if (it->pid () == pid)
1055 {
1056 ptid_t ptid = *it;
1057 fbsd_pending_children.erase (it);
1058 return ptid;
1059 }
6e9567fe 1060 return null_ptid;
e58e05d6 1061}
2c5c2a33 1062
dbaed385 1063#ifndef PTRACE_VFORK
e8c6b620 1064static std::forward_list<ptid_t> fbsd_pending_vfork_done;
2c5c2a33
JB
1065
1066/* Record a pending vfork done event. */
1067
1068static void
1069fbsd_add_vfork_done (ptid_t pid)
1070{
e8c6b620 1071 fbsd_pending_vfork_done.push_front (pid);
9385df2a
JB
1072
1073 /* If we're in async mode, need to tell the event loop there's
1074 something here to process. */
1075 if (target_is_async_p ())
b146ba14 1076 async_file_mark ();
2c5c2a33
JB
1077}
1078
1079/* Check for a pending vfork done event for a specific PID. */
1080
1081static int
1082fbsd_is_vfork_done_pending (pid_t pid)
1083{
e8c6b620
JB
1084 for (auto it = fbsd_pending_vfork_done.begin ();
1085 it != fbsd_pending_vfork_done.end (); it++)
1086 if (it->pid () == pid)
1087 return 1;
2c5c2a33
JB
1088 return 0;
1089}
1090
1091/* Check for a pending vfork done event. If one is found, remove it
1092 from the list and return the PTID. */
1093
ee950322 1094static ptid_t
2c5c2a33
JB
1095fbsd_next_vfork_done (void)
1096{
e8c6b620 1097 if (!fbsd_pending_vfork_done.empty ())
2c5c2a33 1098 {
e8c6b620
JB
1099 ptid_t ptid = fbsd_pending_vfork_done.front ();
1100 fbsd_pending_vfork_done.pop_front ();
2c5c2a33
JB
1101 return ptid;
1102 }
1103 return null_ptid;
1104}
e58e05d6 1105#endif
dbaed385 1106#endif
e58e05d6 1107
f6ac5f3d 1108/* Implement the "resume" target_ops method. */
8607ea63 1109
f6ac5f3d
PA
1110void
1111fbsd_nat_target::resume (ptid_t ptid, int step, enum gdb_signal signo)
8607ea63 1112{
dbaed385 1113#if defined(TDP_RFPPWAIT) && !defined(PTRACE_VFORK)
2c5c2a33
JB
1114 pid_t pid;
1115
1116 /* Don't PT_CONTINUE a process which has a pending vfork done event. */
d7e15655 1117 if (minus_one_ptid == ptid)
e99b03dc 1118 pid = inferior_ptid.pid ();
2c5c2a33 1119 else
e99b03dc 1120 pid = ptid.pid ();
2c5c2a33
JB
1121 if (fbsd_is_vfork_done_pending (pid))
1122 return;
1123#endif
8607ea63 1124
1188bfcf
JB
1125 fbsd_nat_debug_printf ("[%s], step %d, signo %d (%s)",
1126 target_pid_to_str (ptid).c_str (), step, signo,
1127 gdb_signal_to_name (signo));
15a9e13e 1128 if (ptid.lwp_p ())
8607ea63
JB
1129 {
1130 /* If ptid is a specific LWP, suspend all other LWPs in the process. */
5b6d1e4f 1131 inferior *inf = find_inferior_ptid (this, ptid);
d56060f0 1132
08036331 1133 for (thread_info *tp : inf->non_exited_threads ())
dda83cd7 1134 {
08036331 1135 int request;
d56060f0 1136
e38504b3 1137 if (tp->ptid.lwp () == ptid.lwp ())
d56060f0
JB
1138 request = PT_RESUME;
1139 else
1140 request = PT_SUSPEND;
1141
e38504b3 1142 if (ptrace (request, tp->ptid.lwp (), NULL, 0) == -1)
5efac66c
JB
1143 perror_with_name (request == PT_RESUME ?
1144 ("ptrace (PT_RESUME)") :
1145 ("ptrace (PT_SUSPEND)"));
a3627b54
JB
1146 if (request == PT_RESUME)
1147 low_prepare_to_resume (tp);
d56060f0 1148 }
8607ea63
JB
1149 }
1150 else
1151 {
1152 /* If ptid is a wildcard, resume all matching threads (they won't run
1153 until the process is continued however). */
5b6d1e4f 1154 for (thread_info *tp : all_non_exited_threads (this, ptid))
a3627b54
JB
1155 {
1156 if (ptrace (PT_RESUME, tp->ptid.lwp (), NULL, 0) == -1)
1157 perror_with_name (("ptrace (PT_RESUME)"));
1158 low_prepare_to_resume (tp);
1159 }
8607ea63
JB
1160 ptid = inferior_ptid;
1161 }
f169cfdc
JB
1162
1163#if __FreeBSD_version < 1200052
1164 /* When multiple threads within a process wish to report STOPPED
1165 events from wait(), the kernel picks one thread event as the
1166 thread event to report. The chosen thread event is retrieved via
1167 PT_LWPINFO by passing the process ID as the request pid. If
1168 multiple events are pending, then the subsequent wait() after
1169 resuming a process will report another STOPPED event after
1170 resuming the process to handle the next thread event and so on.
1171
1172 A single thread event is cleared as a side effect of resuming the
1173 process with PT_CONTINUE, PT_STEP, etc. In older kernels,
1174 however, the request pid was used to select which thread's event
1175 was cleared rather than always clearing the event that was just
1176 reported. To avoid clearing the event of the wrong LWP, always
1177 pass the process ID instead of an LWP ID to PT_CONTINUE or
1178 PT_SYSCALL.
1179
1180 In the case of stepping, the process ID cannot be used with
1181 PT_STEP since it would step the thread that reported an event
1182 which may not be the thread indicated by PTID. For stepping, use
1183 PT_SETSTEP to enable stepping on the desired thread before
1184 resuming the process via PT_CONTINUE instead of using
1185 PT_STEP. */
1186 if (step)
1187 {
1188 if (ptrace (PT_SETSTEP, get_ptrace_pid (ptid), NULL, 0) == -1)
5efac66c 1189 perror_with_name (("ptrace (PT_SETSTEP)"));
f169cfdc
JB
1190 step = 0;
1191 }
1192 ptid = ptid_t (ptid.pid ());
1193#endif
f6ac5f3d 1194 inf_ptrace_target::resume (ptid, step, signo);
8607ea63
JB
1195}
1196
7efba073
JB
1197#ifdef USE_SIGTRAP_SIGINFO
1198/* Handle breakpoint and trace traps reported via SIGTRAP. If the
1199 trap was a breakpoint or trace trap that should be reported to the
1200 core, return true. */
1201
1202static bool
5b6d1e4f
PA
1203fbsd_handle_debug_trap (fbsd_nat_target *target, ptid_t ptid,
1204 const struct ptrace_lwpinfo &pl)
7efba073
JB
1205{
1206
1207 /* Ignore traps without valid siginfo or for signals other than
6d78332e
JB
1208 SIGTRAP.
1209
1210 FreeBSD kernels prior to r341800 can return stale siginfo for at
1211 least some events, but those events can be identified by
1212 additional flags set in pl_flags. True breakpoint and
1213 single-step traps should not have other flags set in
1214 pl_flags. */
1215 if (pl.pl_flags != PL_FLAG_SI || pl.pl_siginfo.si_signo != SIGTRAP)
7efba073
JB
1216 return false;
1217
1218 /* Trace traps are either a single step or a hardware watchpoint or
1219 breakpoint. */
1220 if (pl.pl_siginfo.si_code == TRAP_TRACE)
1221 {
c45ecc9d 1222 fbsd_nat_debug_printf ("trace trap for LWP %ld", ptid.lwp ());
7efba073
JB
1223 return true;
1224 }
1225
1226 if (pl.pl_siginfo.si_code == TRAP_BRKPT)
1227 {
1228 /* Fixup PC for the software breakpoint. */
5b6d1e4f 1229 struct regcache *regcache = get_thread_regcache (target, ptid);
7efba073
JB
1230 struct gdbarch *gdbarch = regcache->arch ();
1231 int decr_pc = gdbarch_decr_pc_after_break (gdbarch);
1232
c45ecc9d 1233 fbsd_nat_debug_printf ("sw breakpoint trap for LWP %ld", ptid.lwp ());
7efba073
JB
1234 if (decr_pc != 0)
1235 {
1236 CORE_ADDR pc;
1237
1238 pc = regcache_read_pc (regcache);
1239 regcache_write_pc (regcache, pc - decr_pc);
1240 }
1241 return true;
1242 }
1243
1244 return false;
1245}
1246#endif
1247
e58e05d6
JB
1248/* Wait for the child specified by PTID to do something. Return the
1249 process ID of the child, or MINUS_ONE_PTID in case of error; store
1250 the status in *OURSTATUS. */
1251
f6ac5f3d 1252ptid_t
9385df2a
JB
1253fbsd_nat_target::wait_1 (ptid_t ptid, struct target_waitstatus *ourstatus,
1254 target_wait_flags target_options)
e58e05d6
JB
1255{
1256 ptid_t wptid;
1257
1258 while (1)
1259 {
dbaed385 1260#ifndef PTRACE_VFORK
2c5c2a33 1261 wptid = fbsd_next_vfork_done ();
d7e15655 1262 if (wptid != null_ptid)
2c5c2a33
JB
1263 {
1264 ourstatus->kind = TARGET_WAITKIND_VFORK_DONE;
1265 return wptid;
1266 }
dbaed385 1267#endif
f6ac5f3d 1268 wptid = inf_ptrace_target::wait (ptid, ourstatus, target_options);
183be222 1269 if (ourstatus->kind () == TARGET_WAITKIND_STOPPED)
e58e05d6
JB
1270 {
1271 struct ptrace_lwpinfo pl;
1272 pid_t pid;
1273 int status;
1274
e99b03dc 1275 pid = wptid.pid ();
6e9567fe 1276 if (ptrace (PT_LWPINFO, pid, (caddr_t) &pl, sizeof pl) == -1)
5efac66c 1277 perror_with_name (("ptrace (PT_LWPINFO)"));
e58e05d6 1278
184ea2f7 1279 wptid = ptid_t (pid, pl.pl_lwpid);
6e9567fe 1280
386a8676
JB
1281 if (debug_fbsd_nat)
1282 {
c45ecc9d
JB
1283 fbsd_nat_debug_printf ("stop for LWP %u event %d flags %#x",
1284 pl.pl_lwpid, pl.pl_event, pl.pl_flags);
386a8676 1285 if (pl.pl_flags & PL_FLAG_SI)
c45ecc9d
JB
1286 fbsd_nat_debug_printf ("si_signo %u si_code %u",
1287 pl.pl_siginfo.si_signo,
1288 pl.pl_siginfo.si_code);
386a8676
JB
1289 }
1290
6e9567fe
JB
1291#ifdef PT_LWP_EVENTS
1292 if (pl.pl_flags & PL_FLAG_EXITED)
1293 {
1294 /* If GDB attaches to a multi-threaded process, exiting
f6ac5f3d 1295 threads might be skipped during post_attach that
6e9567fe
JB
1296 have not yet reported their PL_FLAG_EXITED event.
1297 Ignore EXITED events for an unknown LWP. */
5b6d1e4f 1298 thread_info *thr = find_thread_ptid (this, wptid);
b7a08269 1299 if (thr != nullptr)
6e9567fe 1300 {
c45ecc9d
JB
1301 fbsd_lwp_debug_printf ("deleting thread for LWP %u",
1302 pl.pl_lwpid);
6e9567fe 1303 if (print_thread_events)
6cb06a8c
TT
1304 gdb_printf (_("[%s exited]\n"),
1305 target_pid_to_str (wptid).c_str ());
983b1119 1306 low_delete_thread (thr);
b7a08269 1307 delete_thread (thr);
6e9567fe
JB
1308 }
1309 if (ptrace (PT_CONTINUE, pid, (caddr_t) 1, 0) == -1)
5efac66c 1310 perror_with_name (("ptrace (PT_CONTINUE)"));
6e9567fe
JB
1311 continue;
1312 }
1313#endif
1314
1315 /* Switch to an LWP PTID on the first stop in a new process.
1316 This is done after handling PL_FLAG_EXITED to avoid
1317 switching to an exited LWP. It is done before checking
1318 PL_FLAG_BORN in case the first stop reported after
1319 attaching to an existing process is a PL_FLAG_BORN
1320 event. */
5b6d1e4f 1321 if (in_thread_list (this, ptid_t (pid)))
6e9567fe 1322 {
c45ecc9d
JB
1323 fbsd_lwp_debug_printf ("using LWP %u for first thread",
1324 pl.pl_lwpid);
5b6d1e4f 1325 thread_change_ptid (this, ptid_t (pid), wptid);
6e9567fe
JB
1326 }
1327
1328#ifdef PT_LWP_EVENTS
1329 if (pl.pl_flags & PL_FLAG_BORN)
1330 {
1331 /* If GDB attaches to a multi-threaded process, newborn
1332 threads might be added by fbsd_add_threads that have
1333 not yet reported their PL_FLAG_BORN event. Ignore
1334 BORN events for an already-known LWP. */
5b6d1e4f 1335 if (!in_thread_list (this, wptid))
6e9567fe 1336 {
c45ecc9d
JB
1337 fbsd_lwp_debug_printf ("adding thread for LWP %u",
1338 pl.pl_lwpid);
5b6d1e4f 1339 add_thread (this, wptid);
6e9567fe 1340 }
183be222 1341 ourstatus->set_spurious ();
6e9567fe
JB
1342 return wptid;
1343 }
1344#endif
1345
e58e05d6
JB
1346#ifdef TDP_RFPPWAIT
1347 if (pl.pl_flags & PL_FLAG_FORKED)
1348 {
dbaed385 1349#ifndef PTRACE_VFORK
e58e05d6 1350 struct kinfo_proc kp;
dbaed385 1351#endif
183be222 1352 bool is_vfork = false;
6e9567fe 1353 ptid_t child_ptid;
e58e05d6
JB
1354 pid_t child;
1355
1356 child = pl.pl_child_pid;
dbaed385
JB
1357#ifdef PTRACE_VFORK
1358 if (pl.pl_flags & PL_FLAG_VFORKED)
183be222 1359 is_vfork = true;
dbaed385 1360#endif
e58e05d6
JB
1361
1362 /* Make sure the other end of the fork is stopped too. */
6e9567fe 1363 child_ptid = fbsd_is_child_pending (child);
d7e15655 1364 if (child_ptid == null_ptid)
e58e05d6
JB
1365 {
1366 pid = waitpid (child, &status, 0);
1367 if (pid == -1)
1368 perror_with_name (("waitpid"));
1369
1370 gdb_assert (pid == child);
1371
1372 if (ptrace (PT_LWPINFO, child, (caddr_t)&pl, sizeof pl) == -1)
5efac66c 1373 perror_with_name (("ptrace (PT_LWPINFO)"));
e58e05d6
JB
1374
1375 gdb_assert (pl.pl_flags & PL_FLAG_CHILD);
184ea2f7 1376 child_ptid = ptid_t (child, pl.pl_lwpid);
e58e05d6
JB
1377 }
1378
5fa14c6b 1379 /* Enable additional events on the child process. */
e99b03dc 1380 fbsd_enable_proc_events (child_ptid.pid ());
5fa14c6b 1381
dbaed385 1382#ifndef PTRACE_VFORK
e58e05d6
JB
1383 /* For vfork, the child process will have the P_PPWAIT
1384 flag set. */
92fce24d
JB
1385 if (fbsd_fetch_kinfo_proc (child, &kp))
1386 {
1387 if (kp.ki_flag & P_PPWAIT)
183be222 1388 is_vfork = true;
92fce24d
JB
1389 }
1390 else
1391 warning (_("Failed to fetch process information"));
dbaed385 1392#endif
183be222 1393
c77282d8
JB
1394 low_new_fork (wptid, child);
1395
183be222
SM
1396 if (is_vfork)
1397 ourstatus->set_vforked (child_ptid);
1398 else
1399 ourstatus->set_forked (child_ptid);
e58e05d6
JB
1400
1401 return wptid;
1402 }
1403
1404 if (pl.pl_flags & PL_FLAG_CHILD)
1405 {
1406 /* Remember that this child forked, but do not report it
1407 until the parent reports its corresponding fork
1408 event. */
6e9567fe 1409 fbsd_remember_child (wptid);
e58e05d6
JB
1410 continue;
1411 }
dbaed385
JB
1412
1413#ifdef PTRACE_VFORK
1414 if (pl.pl_flags & PL_FLAG_VFORK_DONE)
1415 {
183be222 1416 ourstatus->set_vfork_done ();
dbaed385
JB
1417 return wptid;
1418 }
1419#endif
e58e05d6 1420#endif
d2b41ca0 1421
d2b41ca0
JB
1422 if (pl.pl_flags & PL_FLAG_EXEC)
1423 {
183be222
SM
1424 ourstatus->set_execd
1425 (make_unique_xstrdup (pid_to_exec_file (pid)));
d2b41ca0
JB
1426 return wptid;
1427 }
e6cdd38e 1428
7efba073 1429#ifdef USE_SIGTRAP_SIGINFO
5b6d1e4f 1430 if (fbsd_handle_debug_trap (this, wptid, pl))
7efba073
JB
1431 return wptid;
1432#endif
1433
e6cdd38e
JB
1434 /* Note that PL_FLAG_SCE is set for any event reported while
1435 a thread is executing a system call in the kernel. In
1436 particular, signals that interrupt a sleep in a system
1437 call will report this flag as part of their event. Stops
1438 explicitly for system call entry and exit always use
1439 SIGTRAP, so only treat SIGTRAP events as system call
1440 entry/exit events. */
1441 if (pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX)
183be222 1442 && ourstatus->sig () == SIGTRAP)
e6cdd38e
JB
1443 {
1444#ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_SYSCALL_CODE
1445 if (catch_syscall_enabled ())
1446 {
1447 if (catching_syscall_number (pl.pl_syscall_code))
1448 {
1449 if (pl.pl_flags & PL_FLAG_SCE)
183be222 1450 ourstatus->set_syscall_entry (pl.pl_syscall_code);
e6cdd38e 1451 else
183be222
SM
1452 ourstatus->set_syscall_return (pl.pl_syscall_code);
1453
e6cdd38e
JB
1454 return wptid;
1455 }
1456 }
1457#endif
1458 /* If the core isn't interested in this event, just
1459 continue the process explicitly and wait for another
1460 event. Note that PT_SYSCALL is "sticky" on FreeBSD
1461 and once system call stops are enabled on a process
1462 it stops for all system call entries and exits. */
1463 if (ptrace (PT_CONTINUE, pid, (caddr_t) 1, 0) == -1)
5efac66c 1464 perror_with_name (("ptrace (PT_CONTINUE)"));
e6cdd38e
JB
1465 continue;
1466 }
e58e05d6
JB
1467 }
1468 return wptid;
1469 }
1470}
1471
9385df2a
JB
1472ptid_t
1473fbsd_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
1474 target_wait_flags target_options)
1475{
1476 ptid_t wptid;
1477
1478 fbsd_nat_debug_printf ("[%s], [%s]", target_pid_to_str (ptid).c_str (),
1479 target_options_to_string (target_options).c_str ());
1480
1481 /* Ensure any subsequent events trigger a new event in the loop. */
1482 if (is_async_p ())
b146ba14 1483 async_file_flush ();
9385df2a
JB
1484
1485 wptid = wait_1 (ptid, ourstatus, target_options);
1486
1487 /* If we are in async mode and found an event, there may still be
1488 another event pending. Trigger the event pipe so that that the
1489 event loop keeps polling until no event is returned. */
1490 if (is_async_p ()
1491 && ((ourstatus->kind () != TARGET_WAITKIND_IGNORE
1492 && ourstatus->kind() != TARGET_WAITKIND_NO_RESUMED)
1493 || ptid != minus_one_ptid))
b146ba14 1494 async_file_mark ();
9385df2a
JB
1495
1496 fbsd_nat_debug_printf ("returning [%s], [%s]",
1497 target_pid_to_str (wptid).c_str (),
1498 ourstatus->to_string ().c_str ());
1499 return wptid;
1500}
1501
7efba073 1502#ifdef USE_SIGTRAP_SIGINFO
f6ac5f3d 1503/* Implement the "stopped_by_sw_breakpoint" target_ops method. */
7efba073 1504
57810aa7 1505bool
f6ac5f3d 1506fbsd_nat_target::stopped_by_sw_breakpoint ()
7efba073
JB
1507{
1508 struct ptrace_lwpinfo pl;
1509
1510 if (ptrace (PT_LWPINFO, get_ptrace_pid (inferior_ptid), (caddr_t) &pl,
1511 sizeof pl) == -1)
57810aa7 1512 return false;
7efba073 1513
6d78332e 1514 return (pl.pl_flags == PL_FLAG_SI
7efba073
JB
1515 && pl.pl_siginfo.si_signo == SIGTRAP
1516 && pl.pl_siginfo.si_code == TRAP_BRKPT);
1517}
1518
f6ac5f3d 1519/* Implement the "supports_stopped_by_sw_breakpoint" target_ops
7efba073
JB
1520 method. */
1521
57810aa7 1522bool
f6ac5f3d 1523fbsd_nat_target::supports_stopped_by_sw_breakpoint ()
7efba073 1524{
57810aa7 1525 return true;
7efba073 1526}
7efba073
JB
1527#endif
1528
09db4332
JB
1529#ifdef PROC_ASLR_CTL
1530class maybe_disable_address_space_randomization
1531{
1532public:
1533 explicit maybe_disable_address_space_randomization (bool disable_randomization)
1534 {
1535 if (disable_randomization)
1536 {
1537 if (procctl (P_PID, getpid (), PROC_ASLR_STATUS, &m_aslr_ctl) == -1)
1538 {
1539 warning (_("Failed to fetch current address space randomization "
1540 "status: %s"), safe_strerror (errno));
1541 return;
1542 }
1543
1544 m_aslr_ctl &= ~PROC_ASLR_ACTIVE;
1545 if (m_aslr_ctl == PROC_ASLR_FORCE_DISABLE)
1546 return;
1547
1548 int ctl = PROC_ASLR_FORCE_DISABLE;
1549 if (procctl (P_PID, getpid (), PROC_ASLR_CTL, &ctl) == -1)
1550 {
1551 warning (_("Error disabling address space randomization: %s"),
1552 safe_strerror (errno));
1553 return;
1554 }
1555
1556 m_aslr_ctl_set = true;
1557 }
1558 }
1559
1560 ~maybe_disable_address_space_randomization ()
1561 {
1562 if (m_aslr_ctl_set)
1563 {
1564 if (procctl (P_PID, getpid (), PROC_ASLR_CTL, &m_aslr_ctl) == -1)
1565 warning (_("Error restoring address space randomization: %s"),
1566 safe_strerror (errno));
1567 }
1568 }
1569
1570 DISABLE_COPY_AND_ASSIGN (maybe_disable_address_space_randomization);
1571
1572private:
1573 bool m_aslr_ctl_set = false;
1574 int m_aslr_ctl = 0;
1575};
1576#endif
1577
1578void
1579fbsd_nat_target::create_inferior (const char *exec_file,
1580 const std::string &allargs,
1581 char **env, int from_tty)
1582{
1583#ifdef PROC_ASLR_CTL
1584 maybe_disable_address_space_randomization restore_aslr_ctl
1585 (disable_randomization);
1586#endif
1587
1588 inf_ptrace_target::create_inferior (exec_file, allargs, env, from_tty);
1589}
1590
e58e05d6
JB
1591#ifdef TDP_RFPPWAIT
1592/* Target hook for follow_fork. On entry and at return inferior_ptid is
1593 the ptid of the followed inferior. */
1594
e97007b6 1595void
82d1f134
SM
1596fbsd_nat_target::follow_fork (inferior *child_inf, ptid_t child_ptid,
1597 target_waitkind fork_kind, bool follow_child,
1598 bool detach_fork)
e58e05d6 1599{
82d1f134
SM
1600 inf_ptrace_target::follow_fork (child_inf, child_ptid, fork_kind,
1601 follow_child, detach_fork);
1602
bb2a62e6 1603 if (!follow_child && detach_fork)
e58e05d6 1604 {
3a849a34 1605 pid_t child_pid = child_ptid.pid ();
e58e05d6
JB
1606
1607 /* Breakpoints have already been detached from the child by
1608 infrun.c. */
1609
1610 if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
5efac66c 1611 perror_with_name (("ptrace (PT_DETACH)"));
2c5c2a33 1612
dbaed385 1613#ifndef PTRACE_VFORK
183be222 1614 if (fork_kind () == TARGET_WAITKIND_VFORKED)
2c5c2a33
JB
1615 {
1616 /* We can't insert breakpoints until the child process has
1617 finished with the shared memory region. The parent
1618 process doesn't wait for the child process to exit or
1619 exec until after it has been resumed from the ptrace stop
1620 to report the fork. Once it has been resumed it doesn't
1621 stop again before returning to userland, so there is no
1622 reliable way to wait on the parent.
1623
1624 We can't stay attached to the child to wait for an exec
1625 or exit because it may invoke ptrace(PT_TRACE_ME)
1626 (e.g. if the parent process is a debugger forking a new
1627 child process).
1628
1629 In the end, the best we can do is to make sure it runs
1630 for a little while. Hopefully it will be out of range of
1631 any breakpoints we reinsert. Usually this is only the
1632 single-step breakpoint at vfork's return point. */
1633
1634 usleep (10000);
1635
1636 /* Schedule a fake VFORK_DONE event to report on the next
1637 wait. */
1638 fbsd_add_vfork_done (inferior_ptid);
1639 }
dbaed385 1640#endif
e58e05d6 1641 }
e58e05d6
JB
1642}
1643
f6ac5f3d
PA
1644int
1645fbsd_nat_target::insert_fork_catchpoint (int pid)
e58e05d6
JB
1646{
1647 return 0;
1648}
1649
f6ac5f3d
PA
1650int
1651fbsd_nat_target::remove_fork_catchpoint (int pid)
e58e05d6
JB
1652{
1653 return 0;
1654}
1655
f6ac5f3d
PA
1656int
1657fbsd_nat_target::insert_vfork_catchpoint (int pid)
e58e05d6
JB
1658{
1659 return 0;
1660}
1661
f6ac5f3d
PA
1662int
1663fbsd_nat_target::remove_vfork_catchpoint (int pid)
e58e05d6
JB
1664{
1665 return 0;
1666}
6e9567fe 1667#endif
e58e05d6 1668
200fd287 1669/* Implement the virtual inf_ptrace_target::post_startup_inferior method. */
e58e05d6 1670
f6ac5f3d
PA
1671void
1672fbsd_nat_target::post_startup_inferior (ptid_t pid)
e58e05d6 1673{
e99b03dc 1674 fbsd_enable_proc_events (pid.pid ());
e58e05d6
JB
1675}
1676
f6ac5f3d 1677/* Implement the "post_attach" target_ops method. */
e58e05d6 1678
f6ac5f3d
PA
1679void
1680fbsd_nat_target::post_attach (int pid)
e58e05d6 1681{
da95a26c 1682 fbsd_enable_proc_events (pid);
5b6d1e4f 1683 fbsd_add_threads (this, pid);
6e9567fe 1684}
d2b41ca0 1685
fe5ddfc3 1686/* Traced processes always stop after exec. */
d2b41ca0 1687
f6ac5f3d
PA
1688int
1689fbsd_nat_target::insert_exec_catchpoint (int pid)
d2b41ca0
JB
1690{
1691 return 0;
1692}
1693
f6ac5f3d
PA
1694int
1695fbsd_nat_target::remove_exec_catchpoint (int pid)
d2b41ca0
JB
1696{
1697 return 0;
1698}
e6cdd38e
JB
1699
1700#ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_SYSCALL_CODE
f6ac5f3d
PA
1701int
1702fbsd_nat_target::set_syscall_catchpoint (int pid, bool needed,
1703 int any_count,
1704 gdb::array_view<const int> syscall_counts)
e6cdd38e
JB
1705{
1706
1707 /* Ignore the arguments. inf-ptrace.c will use PT_SYSCALL which
1708 will catch all system call entries and exits. The system calls
1709 are filtered by GDB rather than the kernel. */
1710 return 0;
1711}
1712#endif
e58e05d6 1713
54904d81
JB
1714bool
1715fbsd_nat_target::supports_multi_process ()
1716{
1717 return true;
1718}
1719
09db4332
JB
1720bool
1721fbsd_nat_target::supports_disable_randomization ()
1722{
1723#ifdef PROC_ASLR_CTL
1724 return true;
1725#else
1726 return false;
1727#endif
1728}
1729
5a680bf9
JB
1730/* See fbsd-nat.h. */
1731
b95a31ed 1732bool
5a680bf9
JB
1733fbsd_nat_target::fetch_register_set (struct regcache *regcache, int regnum,
1734 int fetch_op, const struct regset *regset,
1735 void *regs, size_t size)
1736{
1737 const struct regcache_map_entry *map
1738 = (const struct regcache_map_entry *) regset->regmap;
1739 pid_t pid = get_ptrace_pid (regcache->ptid ());
1740
1741 if (regnum == -1 || regcache_map_supplies (map, regnum, regcache->arch(),
1742 size))
1743 {
1744 if (ptrace (fetch_op, pid, (PTRACE_TYPE_ARG3) regs, 0) == -1)
1745 perror_with_name (_("Couldn't get registers"));
1746
1747 regcache->supply_regset (regset, regnum, regs, size);
b95a31ed 1748 return true;
5a680bf9 1749 }
b95a31ed 1750 return false;
5a680bf9
JB
1751}
1752
1753/* See fbsd-nat.h. */
1754
b95a31ed 1755bool
5a680bf9
JB
1756fbsd_nat_target::store_register_set (struct regcache *regcache, int regnum,
1757 int fetch_op, int store_op,
1758 const struct regset *regset, void *regs,
1759 size_t size)
1760{
1761 const struct regcache_map_entry *map
1762 = (const struct regcache_map_entry *) regset->regmap;
1763 pid_t pid = get_ptrace_pid (regcache->ptid ());
1764
1765 if (regnum == -1 || regcache_map_supplies (map, regnum, regcache->arch(),
1766 size))
1767 {
1768 if (ptrace (fetch_op, pid, (PTRACE_TYPE_ARG3) regs, 0) == -1)
1769 perror_with_name (_("Couldn't get registers"));
1770
1771 regcache->collect_regset (regset, regnum, regs, size);
1772
1773 if (ptrace (store_op, pid, (PTRACE_TYPE_ARG3) regs, 0) == -1)
1774 perror_with_name (_("Couldn't write registers"));
b95a31ed 1775 return true;
5a680bf9 1776 }
b95a31ed 1777 return false;
5a680bf9
JB
1778}
1779
6719bc69
JB
1780/* See fbsd-nat.h. */
1781
13db4a0c 1782size_t
40c23d88
JB
1783fbsd_nat_target::have_regset (ptid_t ptid, int note)
1784{
1785 pid_t pid = get_ptrace_pid (ptid);
1786 struct iovec iov;
1787
1788 iov.iov_base = nullptr;
1789 iov.iov_len = 0;
1790 if (ptrace (PT_GETREGSET, pid, (PTRACE_TYPE_ARG3) &iov, note) == -1)
1791 return 0;
1792 return iov.iov_len;
1793}
1794
1795/* See fbsd-nat.h. */
1796
1797bool
1798fbsd_nat_target::fetch_regset (struct regcache *regcache, int regnum, int note,
1799 const struct regset *regset, void *regs,
1800 size_t size)
1801{
1802 const struct regcache_map_entry *map
1803 = (const struct regcache_map_entry *) regset->regmap;
1804 pid_t pid = get_ptrace_pid (regcache->ptid ());
1805
1806 if (regnum == -1 || regcache_map_supplies (map, regnum, regcache->arch(),
1807 size))
1808 {
1809 struct iovec iov;
1810
1811 iov.iov_base = regs;
1812 iov.iov_len = size;
1813 if (ptrace (PT_GETREGSET, pid, (PTRACE_TYPE_ARG3) &iov, note) == -1)
1814 perror_with_name (_("Couldn't get registers"));
1815
1816 regcache->supply_regset (regset, regnum, regs, size);
1817 return true;
1818 }
1819 return false;
1820}
1821
1822bool
1823fbsd_nat_target::store_regset (struct regcache *regcache, int regnum, int note,
1824 const struct regset *regset, void *regs,
1825 size_t size)
1826{
1827 const struct regcache_map_entry *map
1828 = (const struct regcache_map_entry *) regset->regmap;
1829 pid_t pid = get_ptrace_pid (regcache->ptid ());
1830
1831 if (regnum == -1 || regcache_map_supplies (map, regnum, regcache->arch(),
1832 size))
1833 {
1834 struct iovec iov;
1835
1836 iov.iov_base = regs;
1837 iov.iov_len = size;
1838 if (ptrace (PT_GETREGSET, pid, (PTRACE_TYPE_ARG3) &iov, note) == -1)
1839 perror_with_name (_("Couldn't get registers"));
1840
1841 regcache->collect_regset (regset, regnum, regs, size);
1842
1843 if (ptrace (PT_SETREGSET, pid, (PTRACE_TYPE_ARG3) &iov, note) == -1)
1844 perror_with_name (_("Couldn't write registers"));
1845 return true;
1846 }
1847 return false;
1848}
1849
1850/* See fbsd-nat.h. */
1851
6719bc69
JB
1852bool
1853fbsd_nat_get_siginfo (ptid_t ptid, siginfo_t *siginfo)
1854{
1855 struct ptrace_lwpinfo pl;
1856 pid_t pid = get_ptrace_pid (ptid);
1857
1858 if (ptrace (PT_LWPINFO, pid, (caddr_t) &pl, sizeof pl) == -1)
1859 return false;
1860 if (!(pl.pl_flags & PL_FLAG_SI))
1861 return false;;
1862 *siginfo = pl.pl_siginfo;
1863 return (true);
1864}
1865
6c265988 1866void _initialize_fbsd_nat ();
6e9567fe 1867void
6c265988 1868_initialize_fbsd_nat ()
6e9567fe 1869{
6e9567fe
JB
1870 add_setshow_boolean_cmd ("fbsd-lwp", class_maintenance,
1871 &debug_fbsd_lwp, _("\
1872Set debugging of FreeBSD lwp module."), _("\
1873Show debugging of FreeBSD lwp module."), _("\
1874Enables printf debugging output."),
1875 NULL,
1876 &show_fbsd_lwp_debug,
1877 &setdebuglist, &showdebuglist);
386a8676
JB
1878 add_setshow_boolean_cmd ("fbsd-nat", class_maintenance,
1879 &debug_fbsd_nat, _("\
1880Set debugging of FreeBSD native target."), _("\
1881Show debugging of FreeBSD native target."), _("\
1882Enables printf debugging output."),
1883 NULL,
1884 &show_fbsd_nat_debug,
1885 &setdebuglist, &showdebuglist);
9385df2a
JB
1886
1887 /* Install a SIGCHLD handler. */
1888 signal (SIGCHLD, sigchld_handler);
6e9567fe 1889}