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