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